Last active
March 2, 2021 02:54
-
-
Save sogoiii/f0ced0a4e569b5f38d302e7072d78b43 to your computer and use it in GitHub Desktop.
Example of call, callcode and delegateCall in solidity: Taken from https://ethereum.stackexchange.com/questions/3667/difference-between-call-callcode-and-delegatecall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.18; | |
contract D { | |
uint public n; | |
address public sender; | |
function callSetN(address _e, uint _n) public { | |
_e.call(bytes4(keccak256("setN(uint256)")), _n); // E's storage is set, D is not modified | |
} | |
function callcodeSetN(address _e, uint _n) public { | |
_e.callcode(bytes4(keccak256("setN(uint256)")), _n); // D's storage is set, E is not modified | |
} | |
function delegatecallSetN(address _e, uint _n) public { | |
_e.delegatecall(bytes4(keccak256("setN(uint256)")), _n); // D's storage is set, E is not modified | |
} | |
} | |
contract E { | |
uint public n; | |
address public sender; | |
function setN(uint _n) public { | |
n = _n; | |
sender = msg.sender; | |
// msg.sender is D if invoked by D's callcodeSetN. None of E's storage is updated | |
// msg.sender is C if invoked by C.foo(). None of E's storage is updated | |
// the value of "this" is D, when invoked by either D's callcodeSetN or C.foo() | |
} | |
} | |
contract C { | |
function foo(D _d, E _e, uint _n) public { | |
_d.delegatecallSetN(_e, _n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment