1

Can delegatecall and call be chained?

A calls B via delegatecall(). Then, when B calls C, execution reverts. Is this expected?

When A calls B via delegatecall, Why can't contract B call contract C?

Set up:

  (delegatecall)    (call)
A -------------> B ----> C

contract C {
    uint32 public num;
    address public sender;

    function setVars(uint32 _num) public {
        num = _num;
        sender = msg.sender;
    }
}

contract B {
    uint32 public num;
    address public sender;
    C public c;
    
    constructor(address _contract) {
        c = C(_contract);
    }

    function setVars(uint32 _num) public {
        c.setVars(_num); //REVERT HAPPENS HERE
        num = _num;
        sender = msg.sender;
    }
}

contract A {
    uint32 public num;
    address public sender;

    function setVars(address _contract, uint32 _num) public {
        _contract.delegatecall(
            abi.encodeWithSignature("setVars(uint32)", _num)
        );
    }
}
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • this code shows the delegatecall vulnerability. It should not revert, contract code is correct. you probably pass wrong address when you deployed B contract. you have to pass C's address. deploy A first, then deploy C and get the address of C and pass it to contract B – Yilmaz Nov 20 '22 at 22:34

0 Answers0