Advertisement
Guest User

coin_results.php

a guest
Jan 18th, 2022
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.14 KB | None | 0 0
  1. <?php
  2.  
  3. $coin = getdbo('db_coins', getiparam('id'));
  4. if (!$coin) $this->goback()
  5.  
  6. $PoS = ($coin->algo == 'PoS'); // or if 'stake' key is present in 'getinfo' method
  7. $DCR = ($coin->rpcencoding == 'DCR' || $coin->getOfficialSymbol() == 'DCR');
  8. $DGB = ($coin->rpcencoding == 'DGB' || $coin->getOfficialSymbol() == 'DGB');
  9. $ETH = ($coin->rpcencoding == 'GETH');
  10.  
  11. $remote = new WalletRPC($coin);
  12.  
  13. $reserved1 = dboscalar("SELECT SUM(balance) FROM accounts WHERE coinid={$coin->id}");
  14. $reserved1 = altcoinvaluetoa($reserved1);
  15. $balance = altcoinvaluetoa($coin->balance);
  16.  
  17. $owed = dboscalar("SELECT SUM(E.amount) AS owed FROM earnings E ".
  18.     "LEFT JOIN blocks B ON E.blockid = B.id ".
  19.     "WHERE E.status!=2 AND E.coinid={$coin->id} "//."AND B.category NOT IN ('stake','generated')"
  20. );
  21. $owed_btc = bitcoinvaluetoa($owed*$coin->price);
  22. $owed = altcoinvaluetoa($owed);
  23.  
  24. $symbol = $coin->symbol;
  25. if (!empty($coin->symbol2)) $symbol = $coin->symbol2;
  26.  
  27. echo "<br/>";
  28. if (YAAMP_ALLOW_EXCHANGE) {
  29.     $reserved2 = bitcoinvaluetoa(dboscalar("SELECT SUM(amount*price) FROM earnings
  30.         WHERE status!=2 AND userid IN (SELECT id FROM accounts WHERE coinid={$coin->id})"));
  31.     echo "Earnings $reserved2 BTC, ";
  32. }
  33. echo "Balance (db) $balance $symbol";
  34. echo ", Owned ".bitcoinvaluetoa($coin->available)." $symbol";
  35. echo ", Owed ".CHtml::link($owed, "/site/earning?id=".$coin->id)." $symbol ($owed_btc BTC)";
  36. echo ", ".CHtml::link($reserved1, "/site/payments?id=".$coin->id)." $symbol cleared<br/><br/>";
  37.  
  38. //////////////////////////////////////////////////////////////////////////////////////
  39.  
  40. $bookmarkAdd = CHtml::link('+', "/site/bookmarkAdd?id=".$coin->id, array('title'=>'Add a bookmark'));
  41.  
  42. echo <<<end
  43. <div id="markets">
  44. <table class="dataGrid">
  45. <thead><tr>
  46. <th width="100">Market</th>
  47. <th width="100">Bid</th>
  48. <th width="100">Ask</th>
  49. <th width="500">Deposit</th>
  50. <th width="100">Balance</th>
  51. <th width="100">Locked</th>
  52. <th width="100">Sent</th>
  53. <th width="100">Traded</th>
  54. <th width="40">Late</th>
  55. <th align="center" width="500">Message</th>
  56. <th align="right" width="100">{$bookmarkAdd} Actions</th>
  57. </tr></thead><tbody>
  58. end;
  59.  
  60. $list = getdbolist('db_markets', "coinid={$coin->id} AND NOT deleted ORDER BY disabled, priority DESC, price DESC");
  61.  
  62. $bestmarket = getBestMarket($coin);
  63. foreach($list as $market)
  64. {
  65.     $marketurl = '#';
  66.     $price = bitcoinvaluetoa($market->price);
  67.     $price2 = bitcoinvaluetoa($market->price2);
  68.  
  69.     $marketurl = getMarketUrl($coin, $market->name);
  70.  
  71.     $rowclass = 'ssrow';
  72.     if($bestmarket && $market->id == $bestmarket->id) $rowclass .= ' bestmarket';
  73.     if($market->disabled) $rowclass .= ' disabled';
  74.  
  75.     echo '<tr class="'.$rowclass.'">';
  76.  
  77.     echo '<td><b><a href="'.$marketurl.'" target=_blank>';
  78.     echo $market->name;
  79.     echo '</a></b></td>';
  80.  
  81.     $updated = "last updated: ".strip_tags(datetoa2($market->pricetime));
  82.     echo '<td title="'.$updated.'">'.$price.'</td>';
  83.     echo '<td title="'.$updated.'">'.$price2.'</td>';
  84.  
  85.     echo '<td style="max-width: 800px; text-overflow: ellipsis; overflow: hidden;">';
  86.     if (!empty($market->deposit_address)) {
  87.         $name = CJavaScript::encode($market->name);
  88.         $addr = CJavaScript::encode($market->deposit_address);
  89.         echo CHtml::link(
  90.             YAAMP_ALLOW_EXCHANGE ? "sell" : "send",
  91.             "javascript:;", array(
  92.                 'onclick'=>"return showSellAmountDialog($name, $addr, {$market->id});"
  93.             )
  94.         );
  95.         echo ' '.$market->deposit_address;
  96.     }
  97.     echo ' <a href="/market/update?id='.$market->id.'">edit</a>';
  98.     echo '</td>';
  99.  
  100.     $updated = "last updated: ".strip_tags(datetoa2($market->balancetime));
  101.     $balance = $market->balance > 0 ? bitcoinvaluetoa($market->balance) : '';
  102.     echo '<td title="'.$updated.'">'.$balance.'</td>';
  103.  
  104.     $ontrade = $market->ontrade > 0 ? bitcoinvaluetoa($market->ontrade) : '';
  105.     echo '<td title="'.$updated.'">'.$ontrade.'</td>';
  106.  
  107.     $sent = datetoa2($market->lastsent);
  108.     $traded = datetoa2($market->lasttraded);
  109.     $late = $market->lastsent > $market->lasttraded && $market->lasttraded ? 'late': '';
  110.  
  111.     echo '<td>'.(empty($sent)   ? "" : "$sent ago").'</td>';
  112.     echo '<td>'.(empty($traded) ? "" : "$traded ago").'</td>';
  113.     echo '<td>'.$late.'</td>';
  114.  
  115.     echo '<td align="center">'.$market->message.'</td>';
  116.  
  117.     echo '<td align="right">';
  118.     if ($market->disabled)
  119.         echo '<a title="Enable this market" href="/market/enable?id='.$market->id.'&en=1">enable</a>';
  120.     else
  121.         echo '<a title="Disable this market" href="/market/enable?id='.$market->id.'&en=0">disable</a>';
  122.     echo '&nbsp;<a class="red" title="Remove this market" href="/market/delete?id='.$market->id.'">delete</a>';
  123.     echo '</td>';
  124.  
  125.     echo "</tr>";
  126. }
  127.  
  128. // in the list after the markets, made for quick send between wallets
  129. $list = getdbolist('db_bookmarks', "idcoin={$coin->id} ORDER BY lastused DESC");
  130.  
  131. foreach($list as $bookmark)
  132. {
  133.     echo '<tr class="ssrow bookmark">';
  134.  
  135.     echo '<td><b>'.$bookmark->label.'<b></td>';
  136.     echo '<td></td>';
  137.     echo '<td></td>';
  138.  
  139.     echo '<td>';
  140.     if (!empty($bookmark->address)) {
  141.         $name = CJavaScript::encode($bookmark->label);
  142.         $addr = CJavaScript::encode($bookmark->address);
  143.         echo CHtml::link(
  144.             "send",
  145.             "javascript:;", array(
  146.                 'onclick'=>"return showSellAmountDialog($name, $addr, 0, {$bookmark->id});"
  147.             )
  148.         );
  149.         echo ' '.$bookmark->address;
  150.     }
  151.     echo ' <a href="/site/bookmarkEdit?id='.$bookmark->id.'">edit</a>';
  152.     echo '</td>';
  153.  
  154.     echo '<td></td>';
  155.     echo '<td></td>';
  156.  
  157.     $sent = datetoa2($bookmark->lastused);
  158.     echo '<td>'.(empty($sent)   ? "" : "$sent ago").'</td>';
  159.     echo '<td></td>';
  160.     echo '<td></td>';
  161.  
  162.     echo '<td align="center"></td>';
  163.  
  164.     echo '<td align="right">';
  165.     echo '<a class="red" href="/site/bookmarkDel?id='.$bookmark->id.'">delete</a>';
  166.     echo '</td>';
  167.  
  168.     echo "</tr>";
  169. }
  170.  
  171. echo "</tbody></table></div>";
  172.  
  173. //////////////////////////////////////////////////////////////////////////////////////
  174.  
  175. $info = $remote->getinfo();
  176. if (!empty($info)) {
  177.     $stake = arraySafeVal($info, 'stake', '');
  178.     if ($stake !== '') $PoS = true;
  179. }
  180.  
  181. if ($DCR) {
  182.     // Decred Tickets
  183.     $stake = 0;
  184.     $balances = $remote->getbalance('*',0);
  185.     if (isset($balances["balances"])) {
  186.         foreach ($balances["balances"] as $accb) {
  187.             $stake += arraySafeVal($accb, 'lockedbytickets', 0);
  188.         }
  189.     }
  190.     $stakeinfo = $remote->getstakeinfo();
  191.     $ticketprice = arraySafeVal($stakeinfo,'difficulty');
  192.     $tickets  = arraySafeVal($stakeinfo, 'live', 0);
  193.     $tickets += arraySafeVal($stakeinfo, 'immature', 0);
  194. }
  195.  
  196. echo '<table id="infos" class="dataGrid">';
  197. echo '<thead><tr>';
  198. echo '<th width="30"></th>';
  199. echo '<th width="30"></th>';
  200. echo "<th>Name</th>";
  201. echo "<th>Symbol</th>";
  202. echo "<th>Algo</th>";
  203. echo "<th>Difficulty</th>";
  204. echo "<th>Blocks</th>";
  205. echo "<th>Balance</th>";
  206. echo "<th>BTC</th>";
  207. if ($PoS || $DCR) echo "<th>Stake</th>";
  208. if ($DCR) echo "<th>Ticket price</th>";
  209. echo "<th>Conns</th>";
  210.  
  211. echo "<th>Price</th>";
  212. echo "<th>Reward</th>";
  213. echo "<th>Index *</th>";
  214.  
  215. echo "</tr>";
  216. echo "</thead><tbody>";
  217.  
  218. echo "<tr class='ssrow'>";
  219. echo "<td><img src='$coin->image' width=24></td>";
  220.  
  221. if($coin->enable)
  222.     echo "<td>[ + ]</td>";
  223. else
  224.     echo "<td>[&nbsp;&nbsp;&nbsp;&nbsp;]</td>";
  225.  
  226. echo '<td><b><a href="/site/block?id='.$coin->id.'">'.$coin->name.'</a></b></td>';
  227. echo '<td width="60"><b>'.$coin->symbol.'</b></td>';
  228. echo '<td><a href="/site/gomining?algo='.$coin->algo.'">'.$coin->algo.'</a></td>';
  229.  
  230. if(!$info)
  231. {
  232.     echo '<td colspan="5">ERROR '.$remote->error.'</td>';
  233.     echo '<td>'.bitcoinvaluetoa($coin->price).'</td>';
  234.     echo '<td colspan="2">';
  235.     echo "</tr></tbody></table><br/>";
  236.     return;
  237. }
  238.  
  239. $errors = isset($info['errors'])? $info['errors']: '';
  240. $balance = isset($info['balance'])? $info['balance']: '';
  241. $txfee = isset($info['paytxfee'])? $info['paytxfee']: '';
  242. $connections = isset($info['connections'])? CHtml::link($info['connections'],'/site/peers?id='.$coin->id): '';
  243. $blocks = isset($info['blocks'])? $info['blocks']: '';
  244.  
  245. echo '<td>'.round_difficulty($coin->difficulty).'</td>';
  246. if(!empty($errors))
  247.     echo '<td class="red" title="'.$errors.'">'.$blocks.'</td>';
  248. else
  249.     echo "<td>$blocks</td>";
  250.  
  251. echo '<td>'.altcoinvaluetoa($balance).'</td>';
  252.  
  253. $btc = bitcoinvaluetoa($balance*$coin->price);
  254. echo "<td>$btc</td>";
  255. if ($PoS) echo '<td>'.$stake.'</td>';
  256. else if ($DCR) {
  257.     echo '<td>'.CHtml::link("$stake ($tickets)", '/site/tickets?id='.$coin->id).'</td>';
  258.     echo '<td>'.CHtml::link($ticketprice, "https://dcrstats.com/", array('target'=>'_blank')).'</td>';
  259. }
  260. echo "<td>$connections</td>";
  261.  
  262. echo '<td>'.bitcoinvaluetoa($coin->price).'</td>';
  263. echo '<td>'.$coin->reward.'</td>';
  264.  
  265. $index = '';
  266. if($coin->difficulty)
  267.     $index = round($coin->reward * $coin->price / $coin->difficulty * 10000, 3);
  268. echo '<td>'.$index.'</td>';
  269.  
  270. echo '</tr>';
  271. echo '</tbody></table>';
  272.  
  273. //////////////////////////////////////////////////////////////////////////////////////
  274.  
  275. // last week
  276. $list_since = arraySafeVal($_GET,'since',time()-(7*24*3600));
  277.  
  278. $maxrows = (int) arraySafeVal($_GET,'rows', 500);
  279. $maxrows = max($maxrows,  250);
  280. $maxrows = min($maxrows, 2500);
  281.  
  282. echo <<<end
  283. <div id="transactions">
  284. <table class="dataGrid">
  285. <thead class="">
  286. <tr>
  287. <th>Time</th>
  288. <th>Category</th>
  289. <th>Amount</th>
  290. <th>Height</th>
  291. <th>Difficulty</th>
  292. <th>Confirm</th>
  293. <th>Address</th>
  294. <th>Tx</th>
  295. </tr>
  296. </thead><tbody>
  297. end;
  298.  
  299. $account = '';
  300. if ($DCR || $DGB) $account = '*';
  301. else if ($ETH) $account = $coin->master_wallet;
  302.  
  303. $txs = $remote->listtransactions($account, $maxrows);
  304.  
  305. if (empty($txs)) {
  306.     if (!empty($remote->error)) {
  307.         echo "<b>RPC Error: {$remote->error}</b><p/>";
  308.     }
  309.     // retry...
  310.     $txs = $remote->listtransactions($account, 200);
  311. }
  312.  
  313. $txs_array = array(); $lastday = '';
  314.  
  315. if (!empty($txs)) {
  316.     // to hide truncated days sums
  317.     $tx = reset($txs);
  318.  
  319.     if (count($txs) == $maxrows && isset($tx['time']))
  320.         $lastday = strftime('%F', $tx['time']);
  321.  
  322.     if (!empty($txs)) foreach($txs as $tx)
  323.     {
  324.         if (arraySafeVal($tx, 'time', $list_since+1) > $list_since)
  325.             $txs_array[] = $tx;
  326.     }
  327.  
  328.     krsort($txs_array);
  329. }
  330.  
  331. // filter useless decred spent transactions
  332. if ($DCR) {
  333.  
  334.     // normal value since 0.1.5
  335.     $amountin_mul = $info['version'] >= 10500 ? 1.0 : 0.00000001;
  336.  
  337.     $prev_tx = array(); $lastday = '';
  338.     foreach($txs_array as $key => $tx)
  339.     {
  340.         // required after a wallet resynch/import
  341.         $txs_array[$key]['time'] = min($tx['timereceived'], arraySafeVal($tx,'blocktime', $tx['time']));
  342.  
  343.         $prev_txid = arraySafeVal($prev_tx,"txid");
  344.         $category = $tx['category'];
  345.         if (arraySafeVal($tx, 'txtype') == 'ticket') {
  346.             $txs_array[$key]['category'] = 'ticket';
  347.             if ($category != 'receive' || $prev_txid === arraySafeVal($tx,"txid"))
  348.                 unset($txs_array[$key]);
  349.             else
  350.                 $txs_array[$key]['amount'] = 0 - $tx['amount'];
  351.             continue;
  352.         }
  353.         if ($category == 'send' && arraySafeVal($tx,'generated')) {
  354.             $txs_array[$key]['category'] = 'spent';
  355.         }
  356.         else if ($category == 'send' && $tx['amount'] == -0) {
  357.             // vote accepted (listed twice ? in listtransactions)
  358.             if ($tx['vout'] > 0)
  359.                 $category = 'spent';
  360.             else if (arraySafeVal($tx,"confirmations") >= 256)
  361.                 $category = 'receive';
  362.             else
  363.                 $category = 'immature';
  364.  
  365.             if ($category=='spent' && arraySafeVal($tx, 'txtype') == 'vote') {
  366.                 // todo: ticket unlocked amount
  367.                 $category='unlock';
  368.             }
  369.  
  370.             $txs_array[$key]['category'] = $category;
  371.  
  372.             if ($tx['vout'] == 0) {
  373.                 // won ticket value
  374.                 $t = $remote->getrawtransaction($tx['txid'], 1);
  375.                 if ($t && isset($t['vin'][0])) {
  376.                     $txs_array[$key]['amount'] = $t['vin'][0]['amountin'] * $amountin_mul;
  377.                 }
  378.             }
  379.             if ($category == 'unlock') {
  380.                 // unlocked amount
  381.                 $t = $remote->getrawtransaction($tx['txid'], 1);
  382.                 if ($t && isset($t['vin'][1])) {
  383.                     $txs_array[$key]['amount'] = $t['vin'][1]['amountin'] * $amountin_mul;
  384.                 }
  385.             }
  386.         }
  387.         else if ($category == 'send' && $prev_txid === arraySafeVal($tx,"txid")) {
  388.             // if txid is the same as the last income... it's not a real "send"
  389.             if ($prev_tx['amount'] == 0 - $tx['amount'])
  390.                 $txs_array[$key]['category'] = 'spent';
  391.         }
  392.         else if ($category == 'receive') {
  393.             $prev_tx = $tx;
  394.         }
  395.         // for truncated day sums
  396.         if ($lastday == '' && count($txs) == $maxrows)
  397.             $lastday = strftime('%F', $tx['time']);
  398.     }
  399.     if ($info['version'] < 1010200)
  400.         ksort($txs_array); // was in reversed order
  401. }
  402.  
  403. $rows = 0;
  404. foreach($txs_array as $tx)
  405. {
  406.     $category = arraySafeVal($tx,'category');
  407.     if ($category == 'spent') continue;
  408.  
  409.     $block = null;
  410.     if(isset($tx['blockhash']))
  411.         $block = $remote->getblock($tx['blockhash']);
  412.  
  413.     echo '<tr class="ssrow '.$category.'">';
  414.  
  415.     if (!isset($tx['time'])) {
  416.         // martian wallets
  417.         echo '<td colspan="8">'.json_encode($tx).'</td>';
  418.         continue;
  419.     }
  420.  
  421.     $d = datetoa2($tx['time']);
  422.     echo '<td><b>'.$d.'</b></td>';
  423.  
  424.     $eta = '';
  425.     if ($category == 'immature') {
  426.         if ($coin->block_time && $coin->mature_blocks) {
  427.             $t = (int) ($coin->mature_blocks - arraySafeVal($tx,'confirmations',0)) * $coin->block_time;
  428.             $eta = "ETA: ".sprintf('%dh %02dmn', ($t/3600), ($t/60)%60);
  429.         }
  430.     }
  431.     echo '<td title="'.$eta.'">'.$category.'</td>';
  432.     echo '<td>'.$tx['amount'].'</td>';
  433.  
  434.     if($block) {
  435.         echo '<td>'.$block['height'].'</td><td>'.round_difficulty($block['difficulty']).'</td>';
  436.     } else
  437.         echo '<td></td><td></td>';
  438.  
  439.     echo '<td>'.arraySafeVal($tx,'confirmations').'</td>';
  440.  
  441.     echo '<td width="280">';
  442.     if(isset($tx['address']))
  443.     {
  444.         $address = $tx['address'];
  445.         $exists = dboscalar("SELECT count(*) AS nb FROM accounts WHERE username=:address", array(':address'=>$address));
  446.         if ($exists)
  447.             echo CHtml::link($address, '/?address='.$address);
  448.         else
  449.             echo $address.'<br>';
  450.     }
  451.     echo '</td>';
  452.  
  453.     echo '<td>';
  454.     if(!empty($block)) {
  455.         $txid = arraySafeVal($tx, 'txid');
  456.         $label = substr($txid, 0, 7);
  457.         echo $coin->createExplorerLink($label, array('txid'=>$txid), array('target'=>'_blank'));
  458.     }
  459.     echo '</td>';
  460.  
  461.     echo '</tr>';
  462.  
  463.     $rows++;
  464.     if ($rows >= $maxrows) break;
  465. }
  466.  
  467. echo '</tbody></table>';
  468.  
  469. $url = '/site/coin?id='.$coin->id.'&since='.(time()-31*24*3600).'&rows='.($maxrows*2);
  470. $moreurl = CHtml::link('Click here to show more transactions...', $url);
  471.  
  472. echo '<div class="loadfooter" style="margin-top: 4px;">'.$moreurl.'</div>';
  473. echo '</div>';
  474.  
  475. //////////////////////////////////////////////////////////////////////////////////////
  476.  
  477. echo <<<end
  478. <div id="sums">
  479. <table class="dataGrid">
  480. <thead class="">
  481. <tr>
  482. <th>Day</th>
  483. <th>Category</th>
  484. <th>Sum</th>
  485. <th>BTC</th>
  486. </tr>
  487. </thead><tbody>
  488. end;
  489.  
  490. $sums = array();
  491. foreach($txs_array as $tx)
  492. {
  493.     if (!isset($tx['time'])) continue;
  494.  
  495.     $day = strftime('%F', $tx['time']); // YYYY-MM-DD
  496.     if ($day == $lastday) break; // do not show truncated days
  497.  
  498.     $category = $tx['category'];
  499.     if ($category == 'spent') continue;
  500.  
  501.     $key = $day.' '.$category;
  502.     $sums[$key] = arraySafeVal($sums, $key) + $tx['amount'];
  503. }
  504.  
  505. foreach($sums as $key => $amount)
  506. {
  507.     $keys = explode(' ', $key);
  508.     $day = substr($keys[0], 5); // remove year
  509.     $category = $keys[1];
  510.     echo '<tr class="ssrow '.$category.'">';
  511.     echo '<td><b>'.$day.'</b></td>';
  512.  
  513.     echo '<td>'.$category.'</td>';
  514.     echo '<td>'.$amount.'</td>';
  515.     echo '<td>'.bitcoinvaluetoa($coin->price * $amount).'</td>';
  516.  
  517.     echo '</tr>';
  518. }
  519.  
  520. if (empty($sums)) {
  521.     echo '<tr class="ssrow">';
  522.     echo '<td colspan="4"><i>No activity during the last 7 days</i></td>';
  523.     echo '</tr>';
  524. }
  525.  
  526. echo '</tbody></table></div>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement