5

How can I get ERC-20 token transfer transactions for some address with JSON-RPC node?

Input:

  1. Token Contract address
  2. Token Contract ABI
  3. Address
  4. Start block
  5. End block

Output:

  1. JSON with list of token transfer transactions

Thanks!

8
  • 1
    Start with github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs. Post again if you run into any specific issues.
    – user19510
    Jan 26, 2018 at 14:12
  • @smarx Thanks for your answer, but I can't receive any data from the node with this command 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 Jan 29, 2018 at 17:17
  • The value for address should just be a single address, not a list. ("address": "0xd497...", no brackets).
    – user19510
    Jan 29, 2018 at 20:13
  • @smarx Result the same {"jsonrpc":"2.0","result":[],"id":1}. Have you some code examples how to get ERC-20 transactions from JSON-RPC node? Jan 29, 2018 at 23:09
  • 1
    The address you're passing isn't a contract, so it doesn't have any logs.
    – user19510
    Jan 29, 2018 at 23:10

1 Answer 1

3

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;
    }
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.