Created
April 24, 2017 14:07
-
-
Save benjaminion/26c67ad7832b100fc9c53e2d7b51f5a4 to your computer and use it in GitHub Desktop.
PHP script for checking asset balances at Kraken and private ETH accounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Kraken tracker</title> | |
<style> | |
td.curr {text-align:right;} | |
td.rate {text-align:right;} | |
th {text-align:left;} | |
</style> | |
<meta name="viewport" content="width=400"/> | |
</head> | |
<body> | |
<?php | |
date_default_timezone_set('Europe/London'); | |
$my_assets = array(); | |
// | |
// Kraken set-up | |
// | |
require_once 'KrakenAPIClient.php'; | |
// The values in this file are for accessing my Kraken account | |
// They aren't required if just polling public info such as asset prices | |
require_once '../../inc/kraken.inc'; | |
$key = APIKEY; | |
$secret = SECRET; | |
$url = 'https://api.kraken.com'; | |
$sslverify = true; | |
$version = 0; | |
$kraken = new KrakenAPI($key, $secret, $url, $version, $sslverify); | |
// Show server time | |
$res = $kraken->QueryPublic('Time'); | |
$time = $res['result']; | |
echo '<p>'.date('Y/m/d H:i:s', $time['unixtime']).'</p>'; | |
// Query a public list of active assets and their properties: | |
$res = $kraken->QueryPublic('Assets'); | |
$assets = $res['result']; | |
// | |
// Kraken assets | |
// | |
// Query private asset balances | |
$res = $kraken->QueryPrivate('Balance'); | |
$balances = $res['result']; | |
$base_curr = 'ZEUR'; | |
$rates = array(); | |
$rates[$base_curr] = 1.0; | |
foreach ($balances as $curr=>$amount) { | |
// Get latest rates | |
if ($curr != $base_curr) { | |
$pair = $curr . $base_curr; | |
$res = $kraken->QueryPublic('Ticker', array('pair' => $pair)); | |
$rates[$curr] = $res['result'][$pair]['b'][0]; // Use bid price | |
} | |
$my_assets[] = array( | |
'place' => 'Kraken', | |
'asset' => $curr, | |
'name' => $assets[$curr]['altname'], | |
'points' => $assets[$curr]['display_decimals'], | |
'amount' => $amount, | |
'rate' => $rates[$curr], | |
'value' => $amount * $rates[$curr] | |
); | |
} | |
// | |
// Off-exchange assets | |
// | |
// Ledger Wallet | |
$ledger_addrs = ['0xLEDGER_ADDRESS_1', '0xLEDGER_ADDRESS_2']; // List as many as you like | |
foreach ($ledger_addrs as $addr) { | |
$curl = curl_init('https://api.etherscan.io/api?module=account&action=balance&address=' . $addr . '&tag=latest&apikey=INSERT_ETHERSCAN_API_KEY_HERE'); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$res = json_decode(curl_exec($curl), true); | |
$amount = $res['result']/1.0e18; | |
$my_assets[] = array( | |
'place' => 'Ledger', | |
'asset' => 'XETH', | |
'name' => $assets['XETH']['altname'], | |
'points' => $assets['XETH']['display_decimals'], | |
'amount' => $amount, | |
'rate' => $rates['XETH'], | |
'value' => $amount * $rates['XETH'] | |
); | |
} | |
// | |
// Build table | |
// | |
$total_base = 0; | |
echo "<table>\n"; | |
echo "<tr><th>Place</th><th>Asset</th><th>Quantity</th><th>Rate</th><th>Base Value</th></tr>\n"; | |
foreach ($my_assets as $a) { | |
echo "<tr>"; | |
echo '<td>'.$a['place'].'</td>'; | |
echo '<td>'.$a['name'].'</td>'; | |
echo '<td class="curr">' . number_format($a['amount'], $a['points']) . '</td>'; | |
echo '<td class="rate">' . $a['rate'] . '</td>'; | |
echo '<td class="curr">' . number_format($a['value'], $assets[$base_curr]['display_decimals']).'</td>'; | |
$total_base += $a['value']; | |
echo "</tr>\n"; | |
} | |
echo " <tr><td colspan=\"4\"> </td><td class=\"curr\">" . number_format($total_base, $assets[$base_curr]['display_decimals']) . "</td></tr>\n"; | |
echo "</table>\n"; | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Insert your Ledger accounts on line 82 and your Etherscan API key on line 84. You'll also need a Kraken API key if you want to see your assets there.