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?

1
  • Nice i was looking for this Dec 11, 2020 at 2:22

2 Answers 2

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

1
  • sucks tbh, why not straightforward like truffles?
    – Edwin O.
    Mar 23, 2022 at 16:03
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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