1

I made two simple contracts to test delegatecall on remix:

contract Delegate{

    uint public x;

 
    function add(uint a, uint b)public{

        x = a + b;    
    }

    fallback() external{
       x = 123;
    }

}

contract Test{
    uint public x;


    function add(address _a) public
     {
       _a.delegatecall(abi.encodeWithSignature("add(uint,uint)", 10, 5));
       
    }

}

When I deploy both contracts, I pass the Delegate contract's address to the add() function and it executes fine, but the variable 'x' from Test gets the value 123, why is that?

2 Answers 2

1

with abi.encodeWithSignature two gotchas are

  1. you can't have spaces in the literal string
  2. you have to use uint256 instead of uint

uint being an alias to uint256

with an error in the signature, the signature does not match a function in the contract, so the fallback function is called.

couple of vids from the excellent SCP

https://youtu.be/uawCDnxFJ-0 - includes encodeWithSelector https://youtu.be/xIAs2S9aCKo - includes encodeWithSignature gotchas

1

Apparently the problem was that in the signature it should be add(uint256,uint256) and not add(uint,uint).

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.