Skip to content

Instantly share code, notes, and snippets.

@ConsenSys-Academy
Last active October 19, 2022 09:42
Show Gist options
  • Select an option

  • Save ConsenSys-Academy/de1b2000f3682f0cfba784d0cb5400e7 to your computer and use it in GitHub Desktop.

Select an option

Save ConsenSys-Academy/de1b2000f3682f0cfba784d0cb5400e7 to your computer and use it in GitHub Desktop.

Revisions

  1. ConsenSys-Academy revised this gist Aug 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ContextSwitcher.sol
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    pragma solidity ^0.5.0;
    pragma solidity >=0.7.0 <0.9.0;

    contract Base {
    uint public num;
  2. ConsenSys-Academy revised this gist Nov 3, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ContextSwitcher.sol
    Original file line number Diff line number Diff line change
    @@ -19,11 +19,11 @@ contract FirstCaller {
    }

    function callSetNum(address _base, uint _num) public {
    (bool status, bytes memory returnData) = _base.call(abi.encodeWithSignature("setNum(uint256)", _num)); // Base's num is set
    (bool status, bytes memory returnData) = _base.call(abi.encodeWithSignature("setNum(uint256)", _num));
    }

    function delegatecallSetNum(address _base, uint _num) public {
    (bool status, bytes memory returnData) = _base.delegatecall(abi.encodeWithSignature("setNum(uint256)", _num)); // Base's num is set
    (bool status, bytes memory returnData) = _base.delegatecall(abi.encodeWithSignature("setNum(uint256)", _num));
    }
    }

  3. ConsenSys-Academy revised this gist Oct 25, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ContextSwitcher.sol
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,6 @@ contract Base {
    function setNum(uint _num) public {
    num = _num;
    sender = msg.sender;
    // msg.sender is FirstCaller if invoked by FirstCaller.callSetNum() or FirstCaller.setBaseNum()
    // msg.sender is SecondCaller if invoked by SecondCaller.callThrough()
    }
    }

  4. ConsenSys-Academy revised this gist Oct 25, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ContextSwitcher.sol
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,6 @@ contract Base {

    contract FirstCaller {
    uint public num;
    address public sender;

    function setBaseNum(address _base, uint _num) public{
    Base base = Base(_base);
  5. ConsenSys-Academy created this gist Oct 25, 2019.
    37 changes: 37 additions & 0 deletions ContextSwitcher.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    pragma solidity ^0.5.0;

    contract Base {
    uint public num;
    address public sender;

    function setNum(uint _num) public {
    num = _num;
    sender = msg.sender;
    // msg.sender is FirstCaller if invoked by FirstCaller.callSetNum() or FirstCaller.setBaseNum()
    // msg.sender is SecondCaller if invoked by SecondCaller.callThrough()
    }
    }

    contract FirstCaller {
    uint public num;
    address public sender;

    function setBaseNum(address _base, uint _num) public{
    Base base = Base(_base);
    base.setNum(_num);
    }

    function callSetNum(address _base, uint _num) public {
    (bool status, bytes memory returnData) = _base.call(abi.encodeWithSignature("setNum(uint256)", _num)); // Base's num is set
    }

    function delegatecallSetNum(address _base, uint _num) public {
    (bool status, bytes memory returnData) = _base.delegatecall(abi.encodeWithSignature("setNum(uint256)", _num)); // Base's num is set
    }
    }

    contract SecondCaller {
    function callThrough(FirstCaller _fc, Base _base, uint _num) public {
    _fc.delegatecallSetNum(address(_base), _num);
    }
    }