Skip to content

Instantly share code, notes, and snippets.

@xoriole
Created May 30, 2019 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoriole/18d8a46d298b87c0a07bc418b62c90c4 to your computer and use it in GitHub Desktop.
Save xoriole/18d8a46d298b87c0a07bc418b62c90c4 to your computer and use it in GitHub Desktop.
Example contracts showing delegate call
pragma solidity ^0.5.1;
contract Application {
uint public n;
address public sender;
function setN(uint _n) public {
n = _n;
sender = msg.sender;
}
}
contract Proxy {
uint public n;
address public sender;
function callSetN(address _e, uint _n) public {
_e.call(abi.encodeWithSignature("setN(uint256)", _n));
}
function delegatecallSetN(address _e, uint _n) public {
_e.delegatecall(abi.encodeWithSignature("setN(uint256)", _n));
}
}
contract TestContract1 {
function foo(Proxy _d, Application _e, uint _n) public{
_d.delegatecallSetN(address(_e), _n);
}
}
contract TestContract2 {
function foo(Proxy _d, Application _e, uint _n) public{
_d.callSetN(address(_e), _n);
}
}
contract Token {
string public name;
string public symbol;
uint8 public decimals = 18;
uint256 public totalSupply;
function balanceOf(address _owner) public returns (uint256 balance);
function transfer(address _to, uint256 _value) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
function approve(address _spender, uint256 _value) public returns (bool success);
function allowance(address _owner, address _spender) public view returns (uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contract TokenProxy {
string public name;
string public symbol;
uint8 public decimals = 18;
uint256 public totalSupply;
function balanceOf(address _token, address _owner) public returns (uint256 balance){
// application logic using delegatecall
_token.delegatecall(abi.encodeWithSignature("balanceOf(address)", _owner));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment