Web3 Encoding – Is There a Way to Get the Result of Web3’s encodeFunctionCall with Ethers?

ethers.js

I'm using web3 to get the RLP encoded data for a function call like so

const safeTransferFrom: any = tokenERC1155Abi.find((f) => f.name === 'safeTransferFrom');
const params: Array<any> = [identity, minterAddr, 1, amount, '0x0'];
web3.eth.abi.encodeFunctionCall(safeTransferFrom, params);

is there a way to achieve the same result with the ethers.js library?

Best Answer

The equivalent in ethers.js involves first creating an "interface" object and calling a method on that:

> let ABI = [ "function transfer(address to, uint amount)" ];
> let iface = new ethers.utils.Interface(ABI);
> iface.encodeFunctionData("transfer", [ <function_params> ])

Taken from here

Related Topic