0

I am using ropsten testnet, I wish to detect deposit of an erc-20 token. I am using infura and use method of eth_getBlockByNumber to get all the transactions and check whether the to field match an address in my database. However, when I transfer token, the to will show the token as the address. How can I get the actual to and amount transfered when detecting deposit?

1
  • erc20 token has events ... so a transfer function will emit an event and you need to trace the event "Transfer" of the specific Erc20Contract (which include to- from- and amount..). That is much better and easier than checking all transactions – Majd TL Apr 7 '20 at 6:46
1

With web3.js v1.x, you can use this:

const PARTIAL_ABI = [{
    "anonymous": false,
    "inputs": [
        {"indexed": true , "name": "from" , "type": "address"},
        {"indexed": true , "name": "to"   , "type": "address"},
        {"indexed": false, "name": "value", "type": "uint256"},
    ],
    "name": "Transfer",
    "type": "event",
}];

async function getTransferEvents(tokenAddress, fromBlock, toBlock) {
    const token = new web3.eth.Contract(PARTIAL_ABI, tokenAddress);
    return await token.getPastEvents("Transfer", {fromBlock: fromBlock, toBlock: toBlock});
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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