3

I have the following contract:

pragma solidity ^0.4.17;

contract test{

mapping(bytes4 => bool) internal supportedInterfaces;
uint public a;

function test() public{
          supportedInterfaces[this.transferFrom.selector] = true;
  }

  function supportsInterface(bytes4 interfaceID) external view returns (bool) {
        return supportedInterfaces[interfaceID];
  }


  function transferFrom(address _from, address _to, uint256 _tokenId) public{
     a = 1;
  }

  function transferFrom(address _from, address _to, uint256 _tokenId, bytes data) public{
    a = 2;
  }

}

But I get the error:

Member 'transferFrom' is not unique after argument dependent lookup.

So how do I refer to the selector (or two different ones) in the test function?

Edit: https://github.com/ethereum/solidity/issues/1256 --does this issue mean that it's not resolved?

1 Answer 1

5

I don't know if there's a better way, but you can compute the function selectors directly:

bytes4(keccak256("transferFrom(address,address,uint256)"))

and

bytes4(keccak256("transferFrom(address,address,uint256,bytes)"))
2
  • 1
    You forgot to also convert the function signature into bytes : Should be: bytes4(keccak256(bytes("transferFrom(address,address,uint256))")) - bytes4(keccak256(bytes("transferFrom(address,address,uint256,bytes"))) Mar 21, 2020 at 14:55
  • Casting the string in bytes is not necessary here.You can hash the string with keccak256 directly, like -> bytes4(keccak256("transferFrom(address,address,uint256)")) instead of -> bytes4(keccak256(bytes("transferFrom(address,address,uint256)")) It is slightly cheaper in gas, as casting to bytes(...) add 84 extra gas units.
    – CJ42
    Jun 3, 2022 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.