-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathProxy.sol
43 lines (35 loc) · 1.28 KB
/
Proxy.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
pragma solidity ^0.8.0;
contract Proxy {
address public implementation;
address public owner;
event Upgraded(address implementation);
modifier onlyImplementation() {
require(msg.sender == implementation, "Proxy: only implementation allowed");
_;
}
modifier onlyOwner() {
require(msg.sender == owner, "Proxy: only owner allowed");
_;
}
constructor(address initialImplementation) {
implementation = initialImplementation;
owner = msg.sender;
}
function upgradeTo(address newImplementation) external onlyOwner {
require(newImplementation != address(0), "Proxy: Cannot upgrade to the zero address");
require(newImplementation != implementation, "Proxy: Cannot upgrade to the same implementation");
implementation = newImplementation;
emit Upgraded(newImplementation);
}
fallback() external payable {
address _impl = implementation;
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), _impl, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}
}