adding support for user donations, added all required changes into this commit

This commit is contained in:
Sebastian Grewe 2013-05-20 19:38:21 +02:00
parent 6ae51aa63d
commit e4732f55ca
6 changed files with 149 additions and 65 deletions

View File

@ -45,7 +45,7 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
}
$aAccountShares = $share->getSharesForAccounts($share->getLastUpstreamId(), $iCurrentUpstreamId);
$iRoundShares = $share->getRoundShares($share->getLastUpstreamId(), $iCurrentUpstreamId);
verbose("ID\tHeight\tTime\t\tShares\tFinder\t\tShare ID\tPrev Share\tStatus\n");
verbose("ID\tHeight\tTime\t\tShares\tFinder\t\tShare ID\tPrev Share\t\tStatus\n");
verbose($aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $share->getUpstreamFinder() . "\t" . $share->getUpstreamId() . "\t\t" . $share->getLastUpstreamId());
if (empty($aAccountShares)) {
verbose("\nNo shares found for this block\n\n");
@ -58,24 +58,37 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
if (!$block->setShares($aBlock['id'], $iRoundShares))
$strStatus = "Shares Failed";
verbose("\t\t$strStatus\n\n");
verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tStatus\n");
verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tDonation\t\tStatus\n");
foreach ($aAccountShares as $key => $aData) {
// Payout based on shares, PPS system
$aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 8), 8);
$aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 8), 8);
// Calculate donation amount for Donation transaction
$aData['donation'] = $user->getDonatePercent($user->getUserId($aData['username'])) / 100 * $aData['payout'];
// Verbose output of this users calculations
verbose($aData['id'] . "\t" .
$aData['username'] . "\t" .
$aData['valid'] . "\t" .
$aData['invalid'] . "\t" .
$aData['percentage'] . "\t" .
$aData['payout'] . "\t");
$aData['payout'] . "\t" .
$aData['donation'] . "\t");
// Do all database updates for block, statistics and payouts
$strStatus = "OK";
// Update user share statistics
if (!$statistics->updateShareStatistics($aData, $aBlock['id']))
$strStatus = "Stats Failed";
// Add new credit transaction
if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit', $aBlock['id']))
$strStatus = "Transaction Failed";
verbose("$strStatus\n");
// Add new donation debit
if ($aData['donation'] > 0) {
if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation', $aBlock['id']))
$strStatus = "Donation Failed";
}
verbose("\t\t$strStatus\n");
}
verbose("------------------------------------------------------------------------\n\n");

View File

@ -9,10 +9,11 @@ class Transaction {
private $table = 'transactions';
private $tableBlocks = 'blocks';
public function __construct($debug, $mysqli, $config) {
public function __construct($debug, $mysqli, $config, $block) {
$this->debug = $debug;
$this->mysqli = $mysqli;
$this->config = $config;
$this->block = $block;
$this->debug->append("Instantiated Transaction class", 2);
}
@ -75,23 +76,33 @@ class Transaction {
public function getBalance($account_id) {
$stmt = $this->mysqli->prepare("
SELECT IFNULL(c.credit, 0) - IFNULL(d.debit,0) AS balance
FROM (
SELECT t.account_id, sum(t.amount) AS credit
SELECT IFNULL(t1.credit, 0) - IFNULL(t2.debit, 0) - IFNULL(t3.other, 0) AS balance
FROM
(
SELECT sum(t.amount) AS credit
FROM $this->table AS t
LEFT JOIN $this->tableBlocks AS b ON t.block_id = b.id
WHERE type = 'Credit'
AND b.confirmations > ?
AND t.account_id = ? ) AS c
LEFT JOIN (
SELECT t.account_id, sum(amount) AS debit
LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id
WHERE t.type = 'Credit'
AND b.confirmations >= ?
AND t.account_id = ?
) AS t1,
(
SELECT sum(t.amount) AS debit
FROM $this->table AS t
WHERE type IN ('Debit_MP','Debit_AP')
AND t.account_id = ? ) AS d
ON c.account_id = d.account_id
WHERE t.type IN ('Debit_MP', 'Debit_AP')
AND t.account_id = ?
) AS t2,
(
SELECT sum(t.amount) AS other
FROM $this->table AS t
LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id
WHERE t.type IN ('Donation')
AND b.confirmations >= ?
AND t.account_id = ?
) AS t3
");
if ($this->checkStmt($stmt)) {
$stmt->bind_param("iii", $this->config['confirmations'], $account_id, $account_id);
$stmt->bind_param("iiiii", $this->config['confirmations'], $account_id, $account_id, $this->config['confirmations'], $account_id);
if (!$stmt->execute()) {
$this->debug->append("Unable to execute statement: " . $stmt->error);
$this->setErrorMessage("Fetching balance failed");
@ -104,4 +115,4 @@ class Transaction {
}
}
$transaction = new Transaction($debug, $mysqli, $config);
$transaction = new Transaction($debug, $mysqli, $config, $block);

View File

@ -115,7 +115,19 @@ class User {
* @return data string Coin Address
**/
public function getCoinAddress($userID) {
return $this->getSingle($userID, 'coin_address', 'id', 's');
return $this->getSingle($userID, 'coin_address', 'id');
}
/**
* Fetch users donation value
* @param userID int UserID
* @return data string Coin Address
**/
public function getDonatePercent($userID) {
$dPercent = $this->getSingle($userID, 'donate_percent', 'id');
if ($dPercent > 100) $dPercent = 100;
if ($dPercent < 0) $dPercent = 0;
return $dPercent;
}
/**

View File

@ -1,11 +1,6 @@
{include file="global/block_header.tpl" BLOCK_HEADER="Transaction Log" BUTTONS=array(Confirmed,Unconfirmed)}
<div class="block_content tab_content" id="Confirmed" style="clear:;">
<center>
<p>
<font color="" size="1">
<b>ATP</b> = Auto Threshold Payment, <b>MP</b> = Manual Payment, <b>Don_Fee</b> = donation amount + pool fees (if applicable)
</font>
</p>
<table cellpadding="1" cellspacing="1" width="98%" class="sortable">
<thead style="font-size:13px;">
<tr>
@ -19,7 +14,12 @@
</thead>
<tbody style="font-size:12px;">
{section transaction $TRANSACTIONS}
{if (($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or $TRANSACTIONS[transaction].type != 'Credit')}
{if (
($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations)
or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations)
or $TRANSACTIONS[transaction].type == 'Debit_AP'
or $TRANSACTIONS[transaction].type == 'Debit_MP'
)}
<tr class="{cycle values="odd,even"}">
<td>{$TRANSACTIONS[transaction].id}</td>
<td>{$TRANSACTIONS[transaction].timestamp}</td>
@ -32,11 +32,15 @@
{/section}
</tbody>
</table>
<p>
<font color="" size="1">
<b>Credit_AP</b> = Auto Threshold Payment, <b>Credit_MP</b> = Manual Payment, <b>Donation</b> = Donation, <b>Fee</b> = Pool Fees (if applicable)
</font>
</p>
</center>
</div>
<div class="block_content tab_content" id="Unconfirmed" style="">
<center>
<p><font color="" sizeze="1">Listed below are your estimated rewards and donations/fees for all blocks awaiting 120 confirmations.</font></p>
<table cellpadding="1" cellspacing="1" width="98%" class="sortable">
<thead style="font-size:13px;">
<tr>
@ -50,7 +54,10 @@
</thead>
<tbody style="font-size:12px;">
{section transaction $TRANSACTIONS}
{if $TRANSACTIONS[transaction].type == 'Credit' && $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations}
{if (
$TRANSACTIONS[transaction].type == 'Credit' && $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations
or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations)
)}
<tr class="{cycle values="odd,even"}">
<td>{$TRANSACTIONS[transaction].id}</td>
<td>{$TRANSACTIONS[transaction].timestamp}</td>
@ -59,15 +66,20 @@
<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 $TRANSACTIONS[transaction].type == Credit}
{assign var="credits" value="`$credits+$TRANSACTIONS[transaction].amount`"}
{else}
{assign var="debits" value="`$debits+$TRANSACTIONS[transaction].amount`"}
{/if}
{/if}
{/section}
<tr>
<td colspan="5"><b>Unconfirmed Totals:</b></td>
<td><b>{$sum}</b></td>
<td><b>{$credits - $debits}</b></td>
</tr>
</tbody>
</table>
<p><font color="" sizeze="1">Listed are your estimated rewards and donations/fees for all blocks awaiting {$GLOBAL.confirmations} confirmations.</font></p>
</center>
</div>
{include file="global/block_footer.tpl"}

View File

@ -1,25 +1,61 @@
<div class="block" style="clear:none; margin-top:15px; margin-left:13px;">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h1>Dashboard</h1>
</div>
<div class="block_content" style="padding-top:10px;">
<p>
<b><u>Your Current Hashrate</u></b><br/>
<i><b>{$GLOBAL.userdata.hashrate} KH/s</b></i><br/><br/>
<u><b>Unpaid Shares</b></u><span id='tt'><img src='{$PATH}/images/questionmark.png' height='15px' width='15px' title='Submitted shares between the last 120 confirms block until now.'></span><br/>
Your Valid: <b><i>{$GLOBAL.userdata.shares.valid}</i><font size='1px'></font></b><br/>
Pool Valid: <b><i>{$GLOBAL.roundshares.valid}</i> <font size='1px'></font></b><br/><br>
<u><b>Round Shares </b></u><span id='tt'><img src='{$PATH}/images/questionmark.png' height='15px' width='15px' title='Submitted shares since last found block (ie. round shares)'></span><br/>
Pool Valid: <b><i>{$GLOBAL.roundshares.valid}</i></b><br>
Pool Inalid: <b><i>{$GLOBAL.roundshares.invalid}</i></b><br>
Your Invalid: <b><i>{$GLOBAL.userdata.shares.invalid}</i><font size='1px'></font></b><br/><br>
<u><b>Round Estimate</b></u><font size='1'></font></u><br>
<b><i>{math equation="round(( x / y ) * z, 8)" x=$GLOBAL.userdata.shares.valid y=$GLOBAL.roundshares.valid z=$GLOBAL.reward}</i> <font size='1px'>LTC</font></b><br><br>
<u><b>Account Balance</b></u><br><b><i>{$GLOBAL.userdata.balance|default:"0"}</i><font size='1px'> LTC</font></b><br/><br>
</p>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
<div class="block" style="clear:none; margin-top:15px; margin-left:13px;">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h1>Dashboard</h1>
</div>
<div class="block_content" style="padding-top:10px;">
<table class="sidebar">
<tr><td colspan="2"><b>Your Current Hashrate</b></td></tr>
<tr><td colspan="2">{$GLOBAL.userdata.hashrate} KH/s</td></tr>
<tr>
<td colspan="2"><b><u>Unpaid Shares</u></b> <span id='tt'><img src='{$PATH}/images/questionmark.png' height='15px' width='15px' title='Submitted shares between the last 120 confirms block until now.'></span><td>
</tr>
<tr>
<td><b>Your Valid<b></td>
<td><i>{$GLOBAL.userdata.shares.valid}</i><font size='1px'></font></b></td>
</tr>
<tr>
<td><b>Pool Valid</td>
<td><i>{$GLOBAL.roundshares.valid}</i> <font size='1px'></font></b></td>
</tr>
<tr>
<td colspan="2"><b><u>Round Shares</u></b> <span id='tt'><img src='{$PATH}/images/questionmark.png' height='15px' width='15px' title='Submitted shares since last found block (ie. round shares)'></span></td>
</tr>
<tr>
<td><b>Pool Valid</b></td>
<td><i>{$GLOBAL.roundshares.valid}</i></td>
</tr>
<tr>
<td><b>Pool Invalid</b></td>
<td><i>{$GLOBAL.roundshares.invalid}</i></td>
</tr>
<tr>
<td><b>Your Invalid</b></td>
<td><i>{$GLOBAL.userdata.shares.invalid}</i><font size='1px'></font></td>
</tr>
{math assign="block" equation="round(( x / y ) * z, 3)" x=$GLOBAL.userdata.shares.valid y=$GLOBAL.roundshares.valid z=$GLOBAL.reward}
{math assign="donation" equation="round(((d / 100) * est), 3)" d=$GLOBAL.userdata.donate_percent est=$block}
<tr>
<td colspan="2"><b><u>Round Estimate</u></b></td>
</tr>
<tr>
<td><b>Block</b></td>
<td>{$block} LTC</td>
</tr>
<tr>
<td><b>Donation</b></td>
<td>{$donation} LTC</td>
</tr>
<tr>
<td><b>Payout</b></td>
<td>{math equation="block - donation" block=$block donation=$donation} LTC</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td colspan="2"><b><u>Account Balance</u></b></td></tr>
<tr><td colspan="2"><b>{$GLOBAL.userdata.balance|default:"0"} LTC</td></tr>
</table>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>

View File

@ -1,11 +1,11 @@
-- phpMyAdmin SQL Dump
-- version 3.5.1
-- version 3.5.8.1deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 16, 2013 at 09:25 PM
-- Server version: 5.5.31-log
-- PHP Version: 5.4.15
-- Generation Time: May 20, 2013 at 07:35 PM
-- Server version: 5.5.31-0ubuntu0.13.04.1
-- PHP Version: 5.4.9-4ubuntu2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
@ -17,7 +17,7 @@ SET time_zone = "+00:00";
/*!40101 SET NAMES utf8 */;
--
-- Database: `mmcfe_ng_db`
-- Database: `mmcfe_ng`
--
-- --------------------------------------------------------
@ -63,7 +63,7 @@ CREATE TABLE IF NOT EXISTS `blocks` (
PRIMARY KEY (`id`),
UNIQUE KEY `height` (`height`,`blockhash`),
KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service';
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service';
-- --------------------------------------------------------
@ -115,7 +115,7 @@ CREATE TABLE IF NOT EXISTS `shares_archive` (
`time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `share_id` (`share_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Archive shares for potential later debugging purposes';
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Archive shares for potential later debugging purposes';
-- --------------------------------------------------------
@ -132,7 +132,7 @@ CREATE TABLE IF NOT EXISTS `statistics_shares` (
PRIMARY KEY (`id`),
KEY `account_id` (`account_id`),
KEY `block_id` (`block_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
@ -143,7 +143,7 @@ CREATE TABLE IF NOT EXISTS `statistics_shares` (
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`account_id` int(255) unsigned NOT NULL,
`type` enum('Credit','Debit_MP','Debit_AP') DEFAULT NULL,
`type` enum('Credit','Debit_MP','Debit_AP','Fee','Donation') DEFAULT NULL,
`coin_address` varchar(255) DEFAULT NULL,
`amount` double DEFAULT '0',
`fee_amount` float DEFAULT '0',
@ -153,7 +153,7 @@ CREATE TABLE IF NOT EXISTS `transactions` (
KEY `block_id` (`block_id`),
KEY `account_id` (`account_id`),
KEY `type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------