This repository has been archived by the owner on May 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.php
executable file
·209 lines (158 loc) · 7.92 KB
/
calculate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
//calculate.php - calculate fldcPlatform tokens for awarding each awarding period
//Copyright © 2015 FoldingCoin Inc., All Rights Reserved
$projBase='/home/fldcPlatform/mergedFolding/';
$projBin='bin';
include($projBase.$projBin.'/includes/functions.php');
include($projBase.$projBin.'/includes/classes.php');
include($projBase.'/config/db.php');
$runStart=time();
echo "Start ".date("c",$runStart)."\n";
$platformAssets=populateAssets();
foreach($platformAssets as $platformAsset){
$payoutRecords='';
$assetName=$platformAsset->assetName;
$awardingPeriod=$platformAsset->awardingPeriod;
$allFolderCredits=0;
echo "Now processing ".$assetName."\n";
//determine most current snapshot timestamp
$mostRecentSnapshot=getMostRecentSnapshot($assetName,$mode);
//echo "mostRecentSnapshot $mostRecentSnapshot ".date("c",$mostRecentSnapshot)."\n";
///date debug
//$mostRecentSnapshot=1423891201;
///date debug
$payoutPlusMinus=5400;
$mostRecentPayout=getMostRecentPayout($assetName,$mode);
echo "mostRecentSnapshot $mostRecentSnapshot mostRecentPayout $mostRecentPayout\n";
/*
$mostRecentPayout<($mostRecentSnapshot-$payoutPlusMinus)
($mostRecentSnapshot>(($mostRecentPayout+$awardingPeriod)+$payoutPlusMinus)) OR
($mostRecentSnapshot>(($mostRecentPayout+$awardingPeriod)-$payoutPlusMinus))
*/
if(
($mostRecentSnapshot>(($mostRecentPayout+$awardingPeriod)+$payoutPlusMinus)) OR
($mostRecentSnapshot>(($mostRecentPayout+$awardingPeriod)-$payoutPlusMinus))
){//THIS IS WHERE to check if the awarding period has gone by
$currentSnapRecords=populateCurrentSnapRecords($mostRecentSnapshot,$assetName,$mode);
echo "dumping current snap records\n";
var_dump($currentSnapRecords);
foreach($currentSnapRecords as $currentSnapRecord){
$normalizedFolder= new normalizedFolder();
$normalizedFolder->assetName=$currentSnapRecord->assetName;
$normalizedFolder->snaptype=$currentSnapRecord->snaptype;
$normalizedFolder->address=$currentSnapRecord->address;
$normalizedFolder->friendlyName=$currentSnapRecord->friendlyName;
$normalizedFolder->fahName=$currentSnapRecord->fahName;
$normalizedFolder->fahTeam=$currentSnapRecord->fahTeam;
$normalizedFolder->cumulativeCredits=$currentSnapRecord->cumulativeCredits;
$folderFahSHA256=$currentSnapRecord->fahSHA256;
$previousSnapRecord='';
$previousSnapRecord=getPreviousSnapRecord($folderFahSHA256,$assetName,$normalizedFolder,$mostRecentSnapshot,$awardingPeriod,$mode);
//echo "dumping currentSnapRecord\n";
//var_dump($currentSnapRecord);
//echo "dumping previousSnapRecord\n";
//var_dump($previousSnapRecord);
$periodCredits=$currentSnapRecord->cumulativeCredits-$previousSnapRecord->cumulativeCredits;
$allFolderCredits=$allFolderCredits+$periodCredits;
//this is a temporary placeholder, as we will calculate the full token payout after getting all the deltas
$periodTokens=0;
$payoutTimestamp=$currentSnapRecord->snapshotTimestamp;
$payoutRecords[$folderFahSHA256]=new payoutRecord($payoutTimestamp,$assetName,$normalizedFolder,$mode,$periodCredits,$periodTokens);
}
if($allFolderCredits==0){
$allFolderCredits=.00000001;
}
$tokensPerFahCredit=$platformAsset->tokensPerPeriod/$allFolderCredits;
echo "For this snapshot, 1 FAH credit gets you ".sprintf("%01.8f",$tokensPerFahCredit)." tokens of $assetName\n";
foreach($payoutRecords as $payoutRecord){
$payoutFahSHA256=$payoutRecord->fahSHA256;
$payoutTokens=$tokensPerFahCredit*$payoutRecord->periodCredits;
$payoutTokens=sprintf("%01.8f",$payoutTokens);
$payoutRecord->periodTokens=$payoutTokens;
if($payoutRecord->periodTokens > 0){
$csv[$platformAsset->assetName][$payoutFahSHA256]=$payoutRecord->address.",".sprintf("%01.8f",$payoutRecord->periodTokens)."\n";
}
$payoutInsertResult=$payoutRecord->insertPayout();
}
}
}
//now render CSV
foreach($csv as $assetName => $csvLines){
$db=dbConnect();
//echo "CSV for $assetName...\n";
$csvEmailBody='';
$csvEmailBody=$csvEmailBody."<html><body><p>$assetName Payouts for ".date("c",$payoutTimestamp)."</p>\n<p>Valid Payouts</p>\n<pre>\n";
foreach($csvLines as $csvLine){
$csvEmailBody=$csvEmailBody.$csvLine;
}
$csvEmailBody=$csvEmailBody."</pre>\n</body></html>";
//echo "$csvEmailBody";
if(preg_match("/live/",$mode)){
$assetEmailQuery="SELECT * FROM fldcPlatform.platformAssets WHERE assetName = '$assetName'";
if ($assetEmailResults=$db->query($assetEmailQuery)) {
while($assetEmailRow=$assetEmailResults->fetch_assoc()){
$toEmail=$assetEmailRow['emailAddress'];
//override while running catch up days
//$toEmail="[email protected]";
}
}
$subject='';
}elseif(preg_match("/test/",$mode)){
$assetEmailQuery="SELECT * FROM fldcPlatform.platformAssets WHERE assetName = '$assetName'";
if ($assetEmailResults=$db->query($assetEmailQuery)) {
while($assetEmailRow=$assetEmailResults->fetch_assoc()){
$toEmail=$assetEmailRow['emailAddress'];
}
}
//if we're testing, override the email to just me, rather than DB
$toEmail="[email protected]";
$subject='Test Mode ';
}
$subject = $subject."$assetName Merged Folding VPS Production Daily Distribution ".date("c",$payoutTimestamp);
$fromEmail = "[email protected]";
$headers = 'From: ' . $fromEmail . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mailResult = mail($toEmail,$subject,$csvEmailBody,$headers);
echo "mailresult $mailResult\n";
}
function getMostRecentPayout($assetName,$mode){
$mostRecentPayout='';
$db=dbConnect();
$mostRecentPayoutQuery="SELECT * FROM fldcPlatform.platformPayouts WHERE assetName = '$assetName' AND mode = '$mode' ORDER BY payoutTimestamp DESC LIMIT 1";
if ($mostRecentPayoutResults=$db->query($mostRecentPayoutQuery)) {
while($mostRecentPayoutRow=$mostRecentPayoutResults->fetch_assoc()){
$mostRecentPayout=$mostRecentPayoutRow['payoutTimestamp'];
}
}
return($mostRecentPayout);
}
function getPreviousSnapRecord($folderFahSHA256,$assetName,$normalizedFolder,$mostRecentSnapshot,$awardingPeriod,$mode){
$previousSnapRecord='';
$previousTimestampBase=$mostRecentSnapshot-$awardingPeriod;
//right now just doing +/- 7200 secs (2 hrs +/-)
//may want to do some more precise/generalized math if there is a coin on a different awardingPeriod
$previousTimestampStart=$previousTimestampBase-7200;
$previousTimestampEnd=$previousTimestampBase+7200;
$endStartDiff=$previousTimestampEnd-$previousTimestampStart;
//echo "mostRecentSnapshot $mostRecentSnapshot, previousTimestampBase $previousTimestampBase,\n";
//echo "previousTimestampStart $previousTimestampStart, previousTimestampEnd $previousTimestampEnd, endStartDiff $endStartDiff\n";
$previousSnapRecord=new snapshotRecord($assetName,$normalizedFolder,$mostRecentSnapshot,$mode);
$previousSnapRecord->cumulativeCredits=0;
$db=dbConnect();
$previousSnapQuery="SELECT * FROM fldcPlatform.platformCredits WHERE fahSHA256 = '$folderFahSHA256' AND snapshotTimestamp > $previousTimestampStart AND snapshotTimestamp < $previousTimestampEnd AND assetName = '$assetName' AND mode = '$mode'";
//echo "$previousSnapQuery\n";
if ($previousSnapResults=$db->query($previousSnapQuery)) {
while($previousSnapRow=$previousSnapResults->fetch_assoc()){
//echo "retrieved previous snap from DB:\n";
//echo "timestamp ".$previousSnapRow['snapshotTimestamp']."\n";
//echo "credits ".$previousSnapRow['cumulativeCredits']."\n";
$previousSnapRecord->address=$previousSnapRow['address'];
$previousSnapRecord->snapshotTimestamp=$previousSnapRow['snapshotTimestamp'];
$previousSnapRecord->cumulativeCredits=$previousSnapRow['cumulativeCredits'];
}
}
//var_dump($previousSnapRecord);
return($previousSnapRecord);
}
?>