-
Notifications
You must be signed in to change notification settings - Fork 445
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
Add support for multiple contract methods with same name #661
Add support for multiple contract methods with same name #661
Conversation
@yaroslavyaroslav LGTM |
@@ -263,7 +263,7 @@ extension DefaultContractProtocol { | |||
let method = Data.fromHex(method) == nil ? method : method.addHexPrefix().lowercased() | |||
|
|||
// MARK: - Encoding ABI Data flow | |||
guard let abiMethod = methods[method]?.first, | |||
guard let abiMethod = methods[method]?.first(where: { $0.inputs.count == parameters.count }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct me if I'm wrong but I think this won't work for methods where there is multiple overloads but with the same parameters amount. As far as I understand this exists and is possible in solidity: https://docs.soliditylang.org/en/v0.4.21/contracts.html#overload-resolution-and-argument-matching
contract A {
function f(uint8 _in) public pure returns (uint8 out) {
out = _in;
}
function f(uint256 _in) public pure returns (uint256 out) {
out = _in;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It must work. Some time ago the support for function overloading was added and now it all depends on the method
value that you pass in. As per docs for the methods
(see here the source):
Functions are mapped to:
- name, like `getData` that is defined in ``ABI/Element/Function/name``;
- name with input parameters that is a combination of ``ABI/Element/Function/name`` and
``ABI/Element/Function/inputs``, e.g. `getData(bytes32)`;
- and 4 bytes signature `0xffffffff` (expected to be lowercased).
The mapping by name (e.g. `getData`) is the one most likely expected to return arrays with
more than one entry due to the fact that solidity allows method overloading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So per your example if you pass in f
as a function name you will get two result and the first one will be picked while you probably expect the second one (let's think that just for the sake of example).
But if you specify the full function selector which includes input types (e.g. f(uint8)
) - you will get 0 or 1 results in the returned array depending on the availability of such function in the ABI of a smart contract.
If method ID is passed (which is 4 bytes of sha3 keccak256 of function name and it's parameters) you will get 0 or more results as some clashes can still happen but most of the time it should be 0 or 1 because it's somewhat rare to have first 4 bytes of two hashes of two different functions in the same smart contract to clash.
Note: if you'll try to parse ABI that has two functions with the same name and input type but different output the parsing of ABI will fail. Just saying.
You need to press some button that means that PR passed your review. For now it's unable to merge any without at least one team members approve @JeneaVranceanu @janndriessen |
Hm, I thought that I approved it. |
Motivation: given ABI with multiple methods with same name and different input count [example], add ability to select correct method.
Implementation: instead of just
first
method - select by number of input parameters.