Ethers.js: Calculate functions signatures from contract ABI

Created on 16 Feb 2021  路  3Comments  路  Source: ethers-io/ethers.js

Is there any suggested implementation to calculate Solidity functions signatures from their ABI using only ethers?

e.g. web3: https://ethereum.stackexchange.com/a/78316

discussion

Most helpful comment

Try this?

const abi = ["function transfer(address,uint256)"];
const iface = new ethers.utils.Interface(abi);
console.log('sighash', iface.getSighash("transfer"));

From doc: interface.getSighash

All 3 comments

Try this?

const abi = ["function transfer(address,uint256)"];
const iface = new ethers.utils.Interface(abi);
console.log('sighash', iface.getSighash("transfer"));

From doc: interface.getSighash

Did you want the sighash (also called the selector) or the Solidity source code version of the method?

If you have a contract, you can use contract.interface.getFragment("transfer").format() to get the minimal solidity source. There are multiple formats a available.

If you are looking for the selector (the 4 byte prefix for the data), the answer by @yuetloo is what you are looking for (if you already have a contract object, its interface is available via contract.interface) :)

Did you want the sighash (also called the selector) or the Solidity source code version of the method?

If you have a contract, you can use contract.interface.getFragment("transfer").format() to get the minimal solidity source. There are multiple formats a available.

If you are looking for the selector (the 4 byte prefix for the data), the answer by @yuetloo is what you are looking for (if you already have a contract object, its interface is available via contract.interface) :)

Yes, it was just the selectors in my case, but thank you for the interesting ethers v5 API about Function Fragments

I had to calculate the methods selectors out of this object in Hardhat: https://github.com/wighawag/hardhat-deploy/blob/a842674b5a5a47ad2ee5b91c2fae1d3e1840e9b6/types.ts#L282

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abhishekp1996 picture abhishekp1996  路  3Comments

jochenonline picture jochenonline  路  3Comments

rekmarks picture rekmarks  路  3Comments

ricmoo picture ricmoo  路  3Comments

naddison36 picture naddison36  路  3Comments