Ethers.js: encodeABI to get call data with encoded parameters of contract method

Created on 29 Mar 2019  路  12Comments  路  Source: ethers-io/ethers.js

Is there a method which returns the encoded function selector and parameters of a given contract call?

This would be equivalent to encodeABI from web3js https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-encodeabi

discussion

Most helpful comment

Indeed there is! :)

In v4:

> let ABI = [
    "function transfer(address to, uint amount)"
];
> let iface = new ethers.utils.Interface(ABI);
> iface.functions.transfer.encode([ "0x1234567890123456789012345678901234567890", ethers.utils.parseEther("1.0") ]);
'0xa9059cbb00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000de0b6b3a7640000'

In v5:

> let ABI = [
    "function transfer(address to, uint amount)"
];
> let iface = new ethers.utils.Interface(ABI);
> iface.encodeFunctionData("transfer", [ "0x1234567890123456789012345678901234567890", parseEther("1.0") ])
'0xa9059cbb00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000de0b6b3a7640000'

Let me know if you have any problems.

All 12 comments

Indeed there is! :)

In v4:

> let ABI = [
    "function transfer(address to, uint amount)"
];
> let iface = new ethers.utils.Interface(ABI);
> iface.functions.transfer.encode([ "0x1234567890123456789012345678901234567890", ethers.utils.parseEther("1.0") ]);
'0xa9059cbb00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000de0b6b3a7640000'

In v5:

> let ABI = [
    "function transfer(address to, uint amount)"
];
> let iface = new ethers.utils.Interface(ABI);
> iface.encodeFunctionData("transfer", [ "0x1234567890123456789012345678901234567890", parseEther("1.0") ])
'0xa9059cbb00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000de0b6b3a7640000'

Let me know if you have any problems.

Closing this now, but please feel free to re-open if you are having problems.

Thanks! :)

i used this as encoded data parameter of "sendTransaction" function but didn't work and transaction got failed as contract might not run please refer to below link
https://ropsten.etherscan.io/tx/0x8d65f9a83c803a6871b589458ebfb327524aa795a96f1406013d84fab1b0d364
is there anyway i can encode executable contract and pass it into the data parameter like this myContract.methods.myMethod(123).encodeABI(); . Thanks.

@vshallRepoM can you include the code you used to generate the data?

Is encodeFunctionData only available on the interface and not on the contract? I have a bunch of contract objects, but need to use encodeFunctionData on just a few of the functions.

@WyseNynja It is only on the interface, but it's available on the contract; you can use contract.interface.encodeFunctionData. ;)

In v5:

let ABI = [
"function transfer(address to, uint amount)"
];
let iface = new ethers.utils.Interface(ABI);
iface.encodeFunctionData("transfer", [ "0x1234567890123456789012345678901234567890", parseEther("1.0") ])
'0xa9059cbb00000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000de0b6b3a7640000'

Here how to pass payable type in encodeFunctionData

Open this issue please.

@venkateshsys

let ABI = [
// add "payable" to the Solidity signature
"function transfer(address to, uint amount) payable"
];

But the interface doesn鈥檛 care about payable. You can use the Contract objects directly with contract.populateTransaction.transfer(addr, tokenValue, { value: etherValue }) which will generate the tx with data, to and value for you.

Does that help?

No @ricmoo
I am asking there adding the payable parameter for the encodeFunctionData

@venkateshsys I don鈥檛 understand what that would do though. The interface doesn鈥檛 care about payable. It has nothing to do with encoding the data field of a transaction, which is what an Interface does.

But if you add payable like in my above example to the ABI, the Fragment object will have payable set to true, constant set to false and the mutabilityState set to payable...

Here how to pass payable type in encodeFunctionData

@venkateshsys Do you mean address payable solidity data type?

I'm going to close this again. But let me know if it should be re-opened.

Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings