-
Notifications
You must be signed in to change notification settings - Fork 175
/
Upgradable.sol
61 lines (43 loc) · 1.61 KB
/
Upgradable.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (C) 2020 d-xo
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.6.12;
import {ERC20} from "./ERC20.sol";
contract Proxy {
bytes32 constant ADMIN_KEY = bytes32(uint256(keccak256('eip1967.proxy.admin')) - 1);
bytes32 constant IMPLEMENTATION_KEY = bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1);
// --- init ---
constructor(uint totalSupply) public {
// Manual give()
bytes32 slot = ADMIN_KEY;
address usr = msg.sender;
assembly { sstore(slot, usr) }
upgrade(address(new ERC20(totalSupply)));
}
// --- auth ---
modifier auth() { require(msg.sender == owner(), "unauthorised"); _; }
function owner() public view returns (address usr) {
bytes32 slot = ADMIN_KEY;
assembly { usr := sload(slot) }
}
function give(address usr) public auth {
bytes32 slot = ADMIN_KEY;
assembly { sstore(slot, usr) }
}
// --- upgrade ---
function implementation() public view returns (address impl) {
bytes32 slot = IMPLEMENTATION_KEY;
assembly { impl := sload(slot) }
}
function upgrade(address impl) public auth {
bytes32 slot = IMPLEMENTATION_KEY;
assembly { sstore(slot, impl) }
}
// --- proxy ---
fallback() external payable {
address impl = implementation();
(bool success, bytes memory returndata) = impl.delegatecall{gas: gasleft()}(msg.data);
require(success);
assembly { return(add(returndata, 0x20), mload(returndata)) }
}
receive() external payable { revert("don't send me ETH!"); }
}