0

Here is my example:

contract A {
    function a() public returns(uint256) {
        return 1;
    }
}
contract B {
    address addrOfA;
    function b() public returns(uint256) {
        (, bytes memory returnData) = addrOfA.delegatecall(abi.encodeWithSingature("a()");
        return uint256(returnData);
    }
}

What should I do to make the b() return 1? How to deal with the return value returnData so that I can get the value 1 from the delegate call on a()?

1 Answer 1

1

Try this:

pragma solidity ^0.5.0;

contract A {
    function a() public returns(uint256) {
        return 2;
    }
}

contract B {
    address addrOfA;
    constructor (address _addrOfA) public {
        addrOfA = _addrOfA;
    }
    
    function b() public returns(uint256) {
        (bool success, bytes memory result) = address(addrOfA).delegatecall(abi.encodeWithSignature("a()"));
        require(success);
        
        return abi.decode(result, (uint256));
    }
}

I set the contract A address in contract B constructor just for the sake of the test, you can change that.

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.