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

Add support for multiple contract methods with same name #661

Conversation

rinat-enikeev
Copy link
Contributor

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.

@rinat-enikeev rinat-enikeev marked this pull request as ready for review November 10, 2022 06:45
@JeneaVranceanu
Copy link
Collaborator

@yaroslavyaroslav LGTM
Ready to be merged.

@JeneaVranceanu JeneaVranceanu added the bug Something isn't working label Nov 10, 2022
@@ -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 }),
Copy link
Collaborator

@janndriessen janndriessen Nov 10, 2022

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;
    }
}

Copy link
Collaborator

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.

Copy link
Collaborator

@JeneaVranceanu JeneaVranceanu Nov 10, 2022

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.

@yaroslavyaroslav
Copy link
Collaborator

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

@JeneaVranceanu
Copy link
Collaborator

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.
Now it's approved for sure.

@JeneaVranceanu JeneaVranceanu merged commit c6ec043 into web3swift-team:develop Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants