10

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?

Micha Roon
  • 2,203
  • 21
  • 36

2 Answers2

11

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

habibipapi
  • 181
  • 2
  • 5
2

Get first the fragment of abi function

const fragment = contract.interface.getFunction(method)

Then, get bytes4 signature of function

const selectorHash = contract.interface.getSighash(fragment)