This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
ESM.sol
159 lines (125 loc) · 4.43 KB
/
ESM.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: AGPL-3.0-or-later
/// ESM.sol
// Copyright (C) 2019-2022 Dai Foundation
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity >=0.6.12;
interface GemLike {
function balanceOf(address) external view returns (uint256);
function burn(uint256) external;
function transfer(address, uint256) external returns (bool);
function transferFrom(address, address, uint256) external returns (bool);
}
interface EndLike {
function live() external view returns (uint256);
function vat() external view returns (address);
function cage() external;
}
interface DenyLike {
function deny(address) external;
}
contract ESM {
uint256 constant WAD = 10 ** 18;
GemLike public immutable gem; // collateral (MKR token)
address public immutable proxy; // Pause proxy
mapping(address => uint256) public wards; // auth
mapping(address => uint256) public sum; // per-address balance
uint256 public Sum; // total balance
uint256 public min; // minimum activation threshold [wad]
EndLike public end; // cage module
uint256 public live; // active flag
event Fire();
event Join(address indexed usr, uint256 wad);
event File(bytes32 indexed what, uint256 data);
event File(bytes32 indexed what, address data);
event Rely(address indexed usr);
event Deny(address indexed usr);
event DenyProxy(address indexed base, address indexed pause);
constructor(address gem_, address end_, address proxy_, uint256 min_) public {
gem = GemLike(gem_);
end = EndLike(end_);
proxy = proxy_;
min = min_;
live = 1;
wards[msg.sender] = 1;
emit Rely(msg.sender);
}
function revokesGovernanceAccess() external view returns (bool ret) {
ret = proxy != address(0);
}
// -- math --
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = x + y;
require(z >= x);
}
// --- Auth ---
function rely(address usr) external auth {
wards[usr] = 1;
emit Rely(usr);
}
function deny(address usr) external auth {
wards[usr] = 0;
emit Deny(usr);
}
modifier auth {
require(wards[msg.sender] == 1, "ESM/not-authorized");
_;
}
// -- admin --
function file(bytes32 what, uint256 data) external auth {
if (what == "min") {
require(data > WAD, "ESM/min-too-small");
min = data;
} else {
revert("ESM/file-unrecognized-param");
}
emit File(what, data);
}
function file(bytes32 what, address data) external auth {
if (what == "end") {
end = EndLike(data);
} else {
revert("ESM/file-unrecognized-param");
}
emit File(what, data);
}
function cage() external auth {
live = 0;
}
function fire() external {
require(live == 1, "ESM/permanently-disabled");
require(Sum >= min, "ESM/min-not-reached");
if (proxy != address(0)) {
DenyLike(end.vat()).deny(proxy);
}
end.cage();
emit Fire();
}
function denyProxy(address target) external {
require(live == 1, "ESM/permanently-disabled");
require(Sum >= min, "ESM/min-not-reached");
DenyLike(target).deny(proxy);
emit DenyProxy(target, proxy);
}
function join(uint256 wad) external {
require(live == 1, "ESM/permanently-disabled");
require(end.live() == 1, "ESM/system-already-shutdown");
sum[msg.sender] = add(sum[msg.sender], wad);
Sum = add(Sum, wad);
require(gem.transferFrom(msg.sender, address(this), wad), "ESM/transfer-failed");
emit Join(msg.sender, wad);
}
function burn() external {
gem.burn(gem.balanceOf(address(this)));
}
}