Merge pull request #9 from TheSerapher/payout-system

Transactions, PPS Payout and Site Template
This commit is contained in:
Sebastian Grewe 2013-05-11 07:53:46 -07:00
commit 3cf5049193
8 changed files with 134 additions and 109 deletions

View File

@ -1,25 +0,0 @@
<?php
/*
Copyright:: 2013, Sebastian Grewe
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Include all settings and classes
require_once('shared.inc.php');
// Confirm any outstanding credits
$ledger->confirmCredits();

View File

@ -44,15 +44,20 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
$aData['invalid'] . "\t" .
$aData['percentage'] . "\t" .
$aData['payout'] . "\t";
if (!$statistics->updateShareStatistics($aData, $aBlock['id'])) {
echo "Stats Failed" . "\n";
}
// Do all database updates for statistics and payouts
$strStatus = "OK";
// if (!$statistics->updateShareStatistics($aData, $aBlock['id']))
// $strStatus = "Stats Failed";
if (!$transaction->addCredit($aData['id'], $aData['payout'], $aBlock['id']))
$strStatus = "Transaction Failed";
echo "$strStatus\n";
}
echo "------------------------------------------------------------------------\n\n";
// Now that we have all shares counted internally let's update the tables
// Set shares as counted and mark block as accounted for
// $share->setCountedByTimeframe($aBlock['time'], $iPrevBlockTime);
// $block->setAccounted($aBlock['blockhash']);
$share->setCountedByTimeframe($aBlock['time'], $iPrevBlockTime);
$block->setAccounted($aBlock['blockhash']);
}
}

View File

@ -9,5 +9,5 @@ require_once(CLASS_DIR . '/user.class.php');
require_once(CLASS_DIR . '/block.class.php');
require_once(CLASS_DIR . '/share.class.php');
require_once(CLASS_DIR . '/statistics.class.php');
require_once(CLASS_DIR . '/ledger.class.php');
require_once(CLASS_DIR . '/transaction.class.php');
require_once(CLASS_DIR . '/settings.class.php');

View File

@ -1,56 +0,0 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
class Ledger {
private $sError = '';
private $table = 'blocks';
// This defines each block
public $height, $blockhash, $confirmations, $difficulty, $time;
public function __construct($debug, $mysqli, $salt) {
$this->debug = $debug;
$this->mysqli = $mysqli;
$this->debug->append("Instantiated Ledger class", 2);
}
// get and set methods
private function setErrorMessage($msg) {
$this->sError = $msg;
}
public function getError() {
return $this->sError;
}
public function confirmCredits() {
$stmt = $this->mysqli->prepare("UPDATE
ledger AS l
INNER JOIN blocks as b ON l.assocBlock = b.height
SET l.confirmed = 1
WHERE b.confirmations > 120
AND l.confirmed = 0");
if ($this->checkStmt($stmt)) {
if (!$stmt->execute()) {
$this->debug->append("Failed to execute statement: " . $stmt->error);
$stmt->close();
return false;
}
$stmt->close();
return true;
}
return false;
}
private function checkStmt($bState) {
if ($bState ===! true) {
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error);
$this->setErrorMessage('Internal application Error');
return false;
}
return true;
}
}
$ledger = new Ledger($debug, $mysqli, SALT);

View File

@ -25,9 +25,9 @@ class Statistics {
}
public function updateShareStatistics($aStats, $iBlockId) {
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?, ?)");
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?)");
if ($this->checkStmt($stmt)) {
$stmt->bind_param('iiiddi', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId);
$stmt->bind_param('iiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId);
if ($stmt->execute()) {
return true;
}

View File

@ -0,0 +1,95 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
class Transaction {
private $sError = '';
private $table = 'transactions';
// This defines each block
public $account_id;
public function __construct($debug, $mysqli) {
$this->debug = $debug;
$this->mysqli = $mysqli;
$this->debug->append("Instantiated Ledger class", 2);
}
// get and set methods
private function setErrorMessage($msg) {
$this->sError = $msg;
}
public function getError() {
return $this->sError;
}
public function addCredit($account_id, $amount, $block_id) {
$strType = 'Credit';
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, amount, block_id, type) VALUES (?, ?, ?, ?)");
echo $this->mysqli->error;
if ($this->checkStmt($stmt)) {
$stmt->bind_param("idis", $account_id, $amount, $block_id, $strType);
if ($stmt->execute()) {
$stmt->close();
return true;
}
}
return false;
}
public function confirmCredits() {
$stmt = $this->mysqli->prepare("UPDATE
ledger AS l
INNER JOIN blocks as b ON l.assocBlock = b.height
SET l.confirmed = 1
WHERE b.confirmations > 120
AND l.confirmed = 0");
if ($this->checkStmt($stmt)) {
if (!$stmt->execute()) {
$this->debug->append("Failed to execute statement: " . $stmt->error);
$stmt->close();
return false;
}
$stmt->close();
return true;
}
return false;
}
public function getTransactions($account_id, $start=0) {
$stmt = $this->mysqli->prepare("
SELECT
t.id AS id,
t.type AS type,
t.amount AS amount,
t.sendAddress AS sendAddress,
t.timestamp AS timestamp,
b.height AS height,
b.confirmations AS confirmations
FROM transactions AS t
LEFT JOIN blocks AS b ON t.block_id = b.id
WHERE t.account_id = ?
ORDER BY timestamp DESC
LIMIT ? , 30");
if ($this->checkStmt($stmt)) {
if(!$stmt->bind_param('ii', $account_id, $start)) return false;
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
$this->debug->append('Unable to fetch transactions');
return false;
}
private function checkStmt($bState) {
if ($bState ===! true) {
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error);
$this->setErrorMessage('Internal application Error');
return false;
}
return true;
}
}
$transaction = new Transaction($debug, $mysqli);

View File

@ -4,7 +4,7 @@
if (!defined('SECURITY')) die('Hacking attempt');
if (!$_SESSION['AUTHENTICATED']) header('Location: index.php?page=home');
$aTransactions = $user->getTransactions($_SESSION['USERDATA']['id']);
$aTransactions = $transaction->getTransactions($_SESSION['USERDATA']['id']);
if (!$aTransactions) $_SESSION['POPUP'][] = array('CONTENT' => 'Could not find any transaction', 'TYPE' => 'errormsg');
$smarty->assign('TRANSACTIONS', $aTransactions);

View File

@ -19,14 +19,16 @@
</thead>
<tbody style="font-size:12px;">
{section transaction $TRANSACTIONS}
{if (($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= 120) or $TRANSACTIONS[transaction].type != 'Credit')}
<tr class="{cycle values="odd,even"}">
<td>{$TRANSACTIONS[transaction].id}</td>
<td>{$TRANSACTIONS[transaction].timestamp}</td>
<td>{$TRANSACTIONS[transaction].transType}</td>
<td>{$TRANSACTIONS[transaction].type}</td>
<td>{$TRANSACTIONS[transaction].sendAddress}</td>
<td>{if $TRANSACTIONS[transaction].assocBlock == 0}n/a{else}{$TRANSACTIONS[transaction].assocBlock}{/if}</td>
<td><font color="{if $TRANSACTIONS[transaction].transType == Credit}green{else}red{/if}">{$TRANSACTIONS[transaction].amount}</td>
<td>{if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if}</td>
<td><font color="{if $TRANSACTIONS[transaction].type == Credit}green{else}red{/if}">{$TRANSACTIONS[transaction].amount}</td>
</tr>
{/if}
{/section}
</tbody>
</table>
@ -38,27 +40,31 @@
<table cellpadding="1" cellspacing="1" width="98%" class="sortable">
<thead style="font-size:13px;">
<tr>
<th>Block #</th>
<th>Estimated Reward</th>
<th>Valid Shares</th>
<th>Donation / Fee</th>
<th>Validity</th>
<th class="header" style="cursor: pointer;">TX #</th>
<th class="header" style="cursor: pointer;">Date</th>
<th class="header" style="cursor: pointer;">TX Type</th>
<th class="header" style="cursor: pointer;">Payment Address</th>
<th class="header" style="cursor: pointer;">Block #</th>
<th class="header" style="cursor: pointer;">Amount</th>
</tr>
</thead>
<tbody style="font-size:12px;">
<tr>
<td>TODO</td>
<td>TODO</td>
<td>TODO</td>
<td>TODO</td>
<td>TODO</td>
{section transaction $TRANSACTIONS}
{if $TRANSACTIONS[transaction].type == 'Credit' && $TRANSACTIONS[transaction].confirmations < 120}
<tr class="{cycle values="odd,even"}">
<td>{$TRANSACTIONS[transaction].id}</td>
<td>{$TRANSACTIONS[transaction].timestamp}</td>
<td>{$TRANSACTIONS[transaction].type}</td>
<td>{$TRANSACTIONS[transaction].sendAddress}</td>
<td>{if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if}</td>
<td><font color="{if $TRANSACTIONS[transaction].type == Credit}green{else}red{/if}">{$TRANSACTIONS[transaction].amount}</td>
</tr>
{assign var="sum" value="`$sum+$TRANSACTIONS[transaction].amount`"}
{/if}
{/section}
<tr>
<td><b>Unconfirmed Totals:</b></td>
<td><b>0.00000000</b></td>
<td></td>
<td><b>0.00000000</b></td>
<td></td>
<td colspan="5"><b>Unconfirmed Totals:</b></td>
<td><b>{$sum}</b></td>
</tr>
</tbody>
</table>