Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting function signatures from a Contract's interface without giving parameters. #44

Closed
GFJHogue opened this issue Oct 26, 2017 · 4 comments
Assignees
Labels
enhancement New feature or improvement.

Comments

@GFJHogue
Copy link

Hello!

So I'm interested in grabbing a function's signature (or the 4 byte hash of it).

This works fine:

someContract.interface.functions.someFunction().signature;

// or the hash
someContract.interface.functions.someFunction().data.substring(0,10);

but if someFunction() has parameters, interface.js requires me to provide them despite them not being needed for the signature.

One way to work around this is to pass dummy, yet valid, values to the function:

// someFunction(uint, uint[5])

someContract.interface.functions.someFunction(0, new Array(5)) //...

but this sort of hardcoding pretty much defeats the point of grabbing the signature.

I just want to see if there is a way of doing this, and if not, if this would be something you would consider adding to ethers.

GFJHogue added a commit to GFJHogue/ethers.js that referenced this issue Oct 27, 2017
…contractFn() takes params. In this case, omit result.data. ethers-io#44
@ricmoo ricmoo added the enhancement New feature or improvement. label Oct 27, 2017
@ricmoo ricmoo self-assigned this Oct 27, 2017
@GFJHogue
Copy link
Author

I took a crack at how I would handle this: e31bdc4

This way we only throw when 0 < # of params < # required and omit result.data when there are required params and none are provided.

@ricmoo
Copy link
Member

ricmoo commented Oct 27, 2017

This is a good idea, since it is known without requiring parameters. I will add it to the properties attached to the function.

So, you will be able to use: someContract.interface.functions.someFunction.signature.

@ricmoo
Copy link
Member

ricmoo commented Oct 27, 2017

Added in fb65772.

It has been published to npm and will get pushed to the CDN tomorrow.

Thanks! Great idea.

@wellitongervickas
Copy link

wellitongervickas commented Jan 5, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement.
Projects
None yet
Development

No branches or pull requests

3 participants