Skip to content

Instantly share code, notes, and snippets.

@sanasol
Created July 13, 2017 08:50
Show Gist options
  • Save sanasol/7bd371c84b915a30de65db26a297bbed to your computer and use it in GitHub Desktop.
Save sanasol/7bd371c84b915a30de65db26a297bbed to your computer and use it in GitHub Desktop.
Siacoin get balance by addresses list.
<?php
require "vendor/autoload.php";
/*
5018ff227a5e13e1664f9ff74d337ae4109c1708506bc0a1931efc93b766578042001a1975e6
a7f15ce6e947a11eba8666290dba12190a0f4f6db8e73fd1d9e6ea0b48fd3d9e731ca0a6dcb9
f34d8086947c6afa319f0e0d591495d4aa52858e17fa9bb903b39589445ab3fc32a906857584
**/
$addresses = file('addresses.txt');
$addresses = array_filter(array_map('trim', $addresses));
//
// $addresses = [
// '5018ff227a5e13e1664f9ff74d337ae4109c1708506bc0a1931efc93b766578042001a1975e6',
// 'a7f15ce6e947a11eba8666290dba12190a0f4f6db8e73fd1d9e6ea0b48fd3d9e731ca0a6dcb9',
// 'f34d8086947c6afa319f0e0d591495d4aa52858e17fa9bb903b39589445ab3fc32a906857584',
//
// '9433bff12b3d30414a673bbfc028b0c6c0e7dd4759ebe800aebc903b351994122bc2a89d6843',
// '61fdad9f692c28255926edcee1f9df0b6429defea27def4257f96724a40fd81927655e1ebc29'
// ];
$promises = [];
$checked = [];
function block_to_flat($blocks, &$flat, $level = 0, $parent_id = 0){
global $client, $checked, $promises;
if($level > 10) {
return false;
}
if(!is_array($blocks)) { return false; }
foreach($blocks as $block){
if(isset($checked[$block['height'].$parent_id])) return true;
$checked[$block['height'].$parent_id] = true;
// echo "{$block['height']}{$parent_id}".PHP_EOL;
foreach($block['transactions'] as $key => $tr){
try {
$res = $client->request('GET', 'https://explorer.siahub.info/api/hash/'.$key);
$data = json_decode($res->getBody(), true);
$promise = $client->postAsync('https://explorer.siahub.info/api/blocks/', [
'form_params' => [
'type' => 'transactionid',
'hash' => $key,
'blocks' => array_map(function($b){return $b['height'];}, $data['blocks']),
]
])->then(function ($response) use ($level, &$flat, $key) {
$data = json_decode($response->getBody(), true);
block_to_flat($data, $flat, $level+1, $key);
});
$flat[$key] = [
'id' => $key,
'parent_id' => $parent_id,
'type' => 'transaction'
];
} catch(Exception $e) {}
if(isset($tr['siacoinoutputs'])) {
foreach($tr['siacoinoutputs'] as $out) {
try {
$res = $client->request('GET', 'https://explorer.siahub.info/api/hash/'.$out['id']);
$data = json_decode($res->getBody(), true);
$promise = $client->postAsync('https://explorer.siahub.info/api/blocks/', [
'form_params' => [
'type' => 'siacoinoutputid',
'hash' => $out['id'],
'blocks' => array_map(function($b){return $b['height'];}, $data['blocks']),
]
])->then(function ($response) use ($level, &$flat, $out) {
$data = json_decode($response->getBody(), true);
block_to_flat($data, $flat, $level+1, $out['id']);
});
$promises[] = $promise;
} catch(Exception $e) {}
$flat[$out['id']] = [
'id' => $out['id'],
'parent_id' => $key,
'raw' => $out,
'type' => 'out'
];
}
}
if(isset($tr['siacoininputs'])) {
foreach($tr['siacoininputs'] as $parent => $in) {
$flat['in_'.$parent] = [
'parent_id' => $parent,
'id' => 'in_'.$parent,
'raw' => $in,
'type' => 'in'
];
}
}
}
}
}
$client = new \GuzzleHttp\Client();
$flatdata = [];
foreach($addresses as $address) {
try {
$res = $client->request('GET', 'https://explorer.siahub.info/api/hash/'.$address);
$data = json_decode($res->getBody(), true);
// error_log(var_export((string)$res->getBody(), true));
// die();
$promise = $client->postAsync('https://explorer.siahub.info/api/blocks/', [
'form_params' => [
'type' => 'unlockhash',
'hash' => $address,
'blocks' => array_map(function($b){return $b['height'];}, $data['blocks']),
]
])->then(function ($response) use ($address, &$flatdata) {
$flatdata[$address] = [
'id' => $address,
'parent_id' => 0,
'type' => 'address'
];
$data = json_decode($response->getBody(), true);
block_to_flat($data, $flatdata, 0, $address);
});
$promises[] = $promise;
} catch(Exception $e) {}
error_log($address);
}
foreach($promises as $promise) {
error_log('wait');
$promise->wait();
}
//
// function buildTree(array &$elements, $parentId = 0) {
// $branch = array();
//
// foreach ($elements as $element) {
// if ($element['parent_id'] == $parentId) {
// echo "build {$element['id']} p:{$parentId} eP:{$element['parent_id']}".PHP_EOL;
// unset($elements[$element['id']]);
// $children = buildTree($elements, $element['id']);
// if ($children) {
// $element['children'] = $children;
// }
// $branch[$element['id']] = $element;
// }
// }
// return $branch;
// }
echo "<pre>";
//print_r($flatdata);
$ins = [];
$outs = [];
$counted = [];
$ocounted = [];
$spend = [];
foreach ($flatdata as $key => $value) {
if($value['type'] == "in") {
foreach($flatdata as $serachout){
if($serachout['type'] == "out" && $serachout['id'] == $value['parent_id']) {
$out = $serachout['raw'];
}
}
if(!isset($counted[$value['parent_id']])) {
$counted[$value['parent_id']] = true;
$ins[$out['unlockhash']] += $out['value']/1e24;
}
}
if($value['type'] == "out") {
if(!isset($ocounted[$value['id']])) {
$spent = false;
foreach($flatdata as $serachout){
if($serachout['type'] == "in" && $serachout['parent_id'] == $value['id']) {
$spent = true;
}
}
$ocounted[$value['id']] = true;
if($spent) {
$spend[$value['raw']['unlockhash']] += $value['raw']['value']/1e24;
}
//if(!$spent) {
$outs[$value['raw']['unlockhash']] += $value['raw']['value']/1e24;
//}
}
}
}
foreach($outs as $addr => $out) {
if(in_array($addr, $addresses)) {
}
$in = $ins[$addr];
$spent = $spend[$addr];
echo "(".((in_array($addr, $addresses)) ? "+++":"---")."){$addr} - Out: {$out} SC ---- in: {$in} SC ---- Spent: {$spent} SC<br>";
if(in_array($addr, $addresses) && empty($spent)) {
$balance = $out;
}
}
echo "<h3>current balance: {$balance} SC</h3>";
// var_dump(buildTree($flatdata));
//
//
// $request = new \GuzzleHttp\Psr7\Request('GET', 'https://explorer.siahub.info/api/hash/hash');
//
// $promise = $client->sendAsync($request)->then(function ($response) {
// echo 'I completed! ' . $response->getBody();
// });
function has_children($rows,$id) {
foreach ($rows as $row) {
if ($row['parent_id'] == $id)
return true;
}
return false;
}
function build_menu($rows,$parent=0)
{
$result = "<ul>";
foreach ($rows as $row)
{
if ($row['parent_id'] == $parent){
$result.= "<li>{$row['id']} - {$row['type']}";
unset($rows[$row['id']]);
if (has_children($rows,$row['id']))
$result.= build_menu($rows,$row['id']);
$result.= "</li>";
}
}
$result.= "</ul>";
return $result;
}
echo build_menu($flatdata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment