1

I have created two contracts A and B as follow:

Contract A:

pragma solidity 0.4.25;

contract A{
    // uint public X ;
    event MyName(address _from, address sender);
    function checkMe(uint val) public{
        emit MyName(this, msg.sender);
        // X = val;
    }
}

Contract B

pragma solidity 0.4.25;
import "browser/A.sol";

contract B{
    A a;
    constructor() public{
        a = A(0xE6c28FEF0491c7626c002131a2c833094f6135D3);       
    }
    function useCall() public{
        require(address(a).call(bytes4(sha3("checkMe(uint)")), 2));
    }

    function useDelegateCall() public{
        require(address(a).delegatecall(bytes4(sha3("checkMe(uint)")), 3));
    }
}

Upon calling the useCall and useDelegateCall function of contract B, it reverts. Can someone help me to diagnose what I am doing wrong here?

Thanks,

1

The problem is you are using uint when calculating the selector. You should use the full type uint256.

function useCall() public{
    require(address(a).call(bytes4(sha3("checkMe(uint256)")), 2));
}

function useDelegateCall() public{
    require(address(a).delegatecall(bytes4(sha3("checkMe(uint256)")), 3));
}
  • useCall should work without problems.
  • useDelegateCall might work but it requires B and A have the same storage layout. You will be using A's code in B's storage.
| improve this answer | |
  • let me try it, what should be done to make useDelegateCall works fine as well? – Fariha Abbasi Nov 29 '19 at 16:51
  • To use delegatecall both contracts have to have compatible storage layout. For example define the variables in the same order and same type and the same inheritance. – Ismael Nov 29 '19 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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