Skip to content

Instantly share code, notes, and snippets.

@sogoiii
Last active March 2, 2021 02:54
Show Gist options
  • Save sogoiii/f0ced0a4e569b5f38d302e7072d78b43 to your computer and use it in GitHub Desktop.
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
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