Skip to content

Instantly share code, notes, and snippets.

@chlp
Last active December 19, 2018 15:00
Show Gist options
  • Save chlp/ec8dfc42d2ff87865c0f05ec71f984f3 to your computer and use it in GitHub Desktop.
Save chlp/ec8dfc42d2ff87865c0f05ec71f984f3 to your computer and use it in GitHub Desktop.
collect all cpt transfers transactions (ETH) from block number
<?php
$currentBlock = 6430271;
$fileName = 'transactions.csv';
file_put_contents($fileName, "txhash;from;to;timestamp;amount\r\n");
$max_iters = 999;
$txhashes = [];
$tx_count = 0;
while (--$max_iters) {
echo "Progress block: $currentBlock count: $tx_count (iterations limit: $max_iters)\r";
$out = '';
$data_raw = @file_get_contents("https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=$currentBlock&toBlock=latest&address=0x88d50b466be55222019d71f9e8fae17f5f45fca1&topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef");
$data = @json_decode($data_raw, true);
if (!$data || (@$data['status'] != 1 && @$data['message'] != 'No records found')) {
echo "\r\nERROR! $currentBlock\r\n";
exit;
}
$txs = $data['result'];
if (count($txs) === 0) {
echo "\r\nstop on $currentBlock\r\n";
break;
}
foreach ($txs as $tx) {
// {"address":"0x88d50b466be55222019d71f9e8fae17f5f45fca1","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x000000000000000000000000ebeaf5cdaff460a0bd21c297349a3cc982718551","0x0000000000000000000000000861fca546225fbf8806986d211c8398f7457734"],"data":"0x0000000000000000000000000000000000000000000000000000246139ca8000","blockNumber":"0x654720","timeStamp":"0x5bdde91a","gasPrice":"0x1dcd65000","gasUsed":"0x557d","logIndex":"0x32","transactionHash":"0x1bb6acc470f7a0498b26b9219cfbac669b2b8afc40b8d53a937602fca3475dc5","transactionIndex":"0x43"}
$txhash = $tx['transactionHash'];
if (isset($txhashes[$txhash])) {
++$currentBlock;
continue;
}
$txhashes[$txhash] = true;
$from = '0x' . substr($tx['topics'][1], -40);
$to = '0x' . substr($tx['topics'][2], -40);
$timestamp = bchexdec($tx['timeStamp']);
$amount = bcdiv(bchexdec(substr($tx['data'], 2)), '100000000', 8);
++$tx_count;
file_put_contents($fileName, "$txhash;$from;$to;$timestamp;$amount\r\n", FILE_APPEND);
$currentBlock = (int)bchexdec($tx['blockNumber']);
}
sleep(1);
}
echo "\r\nEnd\r\n";
function bchexdec($hex)
{
if (substr(strtolower($hex), 0, 2) === '0x') {
$hex = substr($hex, 2);
}
if (strlen($hex) <= 1) {
return hexdec($hex);
} else {
$remain = substr($hex, 0, -1);
$last = substr($hex, -1);
return \bcadd(bcmul(16, bchexdec($remain)), hexdec($last));
}
}
function bcdechex($dec)
{
$hex = '';
do {
$last = bcmod($dec, 16);
$hex = dechex($last) . $hex;
$dec = bcdiv(bcsub($dec, $last), 16);
} while ($dec > 0);
return $hex;
}
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
txhash;from;to;timestamp;amount
0x2e8568fa0253d290d81e10427eac229ddbde85301a408e81f40b775490a52223;0x0f85b755d7889a13b88460fbb0;0x01d9530c0d2acce194b07defa8;1538353818;50000.00000000
0x23c23ddcf456630d9dc472258e089c91681424d13380e4e77898c039a2f230be;0xd8b4cd92f1d1f5c828b85b3f2c;0x8467ddbb84413c450bf351ab14;1538356779;30000.00000000
0x536b9eed39a0bd9dffcbd8f6e3a6a0e7987571a5ace43f610ed9f1e0df7b1601;0x8467ddbb84413c450bf351ab14;0xbf8806986d211c8398f7457734;1538357915;30000.00000000
0x3368297ce9c032ab4ad749830931318b5a1b542225e7c5674a0a6c85d4492c7b;0x0635afa55883dc47a059579885;0xea517ec5ab2b361baf8325bb21;1538359625;6112.43318231
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment