-
So, I have Is there any easy way to figure out to get the abi of the function that my 4 byte signature matches in the abi of the contract ? One way to do would be to loop through the abi of the contract, hash each one of function, and compare its 4 byte signature to my signature, but if the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I don't see an explicit method, but function doesFunctionExist(contractInst: ethers.Contract, functionSig: string) {
try {
// this method needs 2 args and if second one is undefined, it throws error anyway
contractInst.interface.decodeFunctionData('0x208b3804')
} catch (err) {
return !err.message.includes('no matching function');
}
} |
Beta Was this translation helpful? Give feedback.
-
Either of those will work, but you can also just pass a sighash (or any allowed fragment specifier) into the It will throw though, if it isn't available in the Interface, so you will still need the try...catch. e.g. homestead> iface = new ethers.utils.Interface([ "function foo()" ])
homestead> iface.getFunction("foo")
FunctionFragment {
type: 'function',
name: 'foo',
constant: false,
inputs: [],
outputs: [],
payable: false,
stateMutability: 'nonpayable',
gas: null,
_isFragment: true
}
homestead> id("foo()")
'0xc2985578b8f3b75f7dc66a767be2a4ef7d7c2224896a1c86e92ccf30bae678b7'
homestead> iface.getFunction("0xc2985578")
FunctionFragment {
type: 'function',
name: 'foo',
constant: false,
inputs: [],
outputs: [],
payable: false,
stateMutability: 'nonpayable',
gas: null,
_isFragment: true
} Let me know if that works for you. :) |
Beta Was this translation helpful? Give feedback.
-
(moving to discussions) |
Beta Was this translation helpful? Give feedback.
Either of those will work, but you can also just pass a sighash (or any allowed fragment specifier) into the
interface.getFunction
method, and it will find it for you. :)It will throw though, if it isn't available in the Interface, so you will still need the try...catch.
e.g.