Search code examples
blockchainethereumgo-ethereum

How to get current approval allowance in Ethereum


After one approve is made, I want to know how to get current approval allowance(from an address, to a specified contract and gateway), in json-rpc way. e.g. like eth_call?


Solution

  • The to field of the call is the token contract, and the data param is ABI-encoded signature of allowance() function followed by its arguments. eth_call also requires to specify at which block you're requesting the data - you can use the "latest" value for the current block

    curl -X POST \
    --url "<your_node_url>" \
    --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"to":"0xdAC17F958D2ee523a2206206994597C13D831ec7","data":"0xdd62ed3e00000000000000000000000086987cca9f86da6b4d8a805c1ebd4130ae120a24000000000000000000000000b2723beacce4bc54f23544343927f048cef6bd5a"}, "latest"],"id":1}'
    

    Docs: https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_call


    I used this JS snippet to build the data param value:

    const data = web3.eth.abi.encodeFunctionCall({
        name: 'allowance',
        type: 'function',
        inputs: [{
            type: 'address',
            name: 'from'
        },{
            type: 'address',
            name: 'to'
        }]
    }, [
        "0x86987cca9f86da6b4d8a805c1ebd4130ae120a24",
        "0xB2723BEacce4BC54F23544343927f048CeF6bD5A"
    ]);
    console.log(data);