How can I get ERC-20
token transfer transactions for some address with JSON-RPC
node?
Input:
- Token Contract address
- Token Contract ABI
- Address
- Start block
- End block
Output:
- JSON with list of token transfer transactions
Thanks!
I hope my solution on PHP will help somebody:
public static function getTransactionsERC20($nodeCode, $tokenContractAddress, $address, $fromBlock, $toBlock) {
$nodes = Config::getSettings('nodes');
if (!array_key_exists($nodeCode, $nodes)) {
throw new \Exception("Invalid node code '{$nodeCode}'");
}
$logs = self::_request($nodes[$nodeCode], [
'method' => "eth_getLogs",
'params' => [[
'fromBlock' => $fromBlock,
'toBlock' => $toBlock,
'address' => $tokenContractAddress,
'topics' => [
null, null,
"0x000000000000000000000000" . substr($address, 2)
]
]],
'id' => 1,
'jsonrpc' => "2.0"
]);
if (!array_key_exists('result', $logs) || empty($logs['result'])) {
return [];
}
$txnsData = array_filter($logs['result'], function($e) {
return ($e['type'] == "mined");
});
$txns = [];
foreach ($txnsData as $txData) {
$tx = self::_request($nodes[$nodeCode], [
'method' => "eth_getTransactionByHash",
'params' => [
$txData['transactionHash']
],
'id' => 1,
'jsonrpc' => "2.0"
]);
if (!array_key_exists('result', $tx)) {
continue;
}
//var_dump($tx);
$input = $tx['result']['input'];
$method = substr($input, 0, 10);
$toAddress = substr($input, 10, 64);
$amount = substr($input, 74, 64);
if ($method !== "0xa9059cbb") { // transfer
continue;
}
$txn = $tx['result'];
$txn['action']['from'] = $txn['from'];
$txn['action']['to'] = "0x" . substr($toAddress, 24);
$txn['action']['value'] = $amount;
$txns[] = $txn;
}
return $txns;
}
hexdec()
function in PHP [php.net/manual/en/function.hexdec.php]
Commented
Feb 26, 2018 at 14:55
curl --data '{"method":"eth_getLogs","params":[{"fromBlock":"earliest","toBlock":"latest","address":["0xd497e2c79564a444c9e2f9e53a821790a70edf22"]}],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST http://[MY_ROPSTEN_NODE_IP]:8545
address
should just be a single address, not a list. ("address": "0xd497..."
, no brackets).{"jsonrpc":"2.0","result":[],"id":1}
. Have you some code examples how to getERC-20
transactions fromJSON-RPC
node?