9

I'm encoding parameters on-chain to pass into new OZ Beacon proxy instances. For verification purposes, I need to encode the same arguments via ethers.js

The solidity function:

abi.encodeWithSignature(string memory signature, ...) returns (bytes memory)

And an ethers function that I tried, but returns an undesired result (longer, does not start with function selector)

  const abiCoder = ethers.utils.defaultAbiCoder;
  const encoded = abiCoder.encode(...

1 Answer 1

16

This is how it is done as of ethers.js v5:

let ABI = [signature];
let iface = new ethers.utils.Interface(ABI);
iface.encodeFunctionData(functionName, [param1, param2, ...]);

Note that signature needs to be in the format of function functionName(type1 param1, type2 param2, ...).

Also answered here in a GitHub issue.

1
  • how about with ethers js version 6? they changed quite a bit in the new version Jul 10 at 0:38

Your Answer

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

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