forked from symbioticfi/burners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BurnerRouter.sol
315 lines (253 loc) · 9 KB
/
BurnerRouter.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
import {IBurnerRouter} from "../../interfaces/router/IBurnerRouter.sol";
import {IBurner} from "@symbioticfi/core/src/interfaces/slasher/IBurner.sol";
import {Subnetwork} from "@symbioticfi/core/src/contracts/libraries/Subnetwork.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {SafeERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Time} from "@openzeppelin/contracts/utils/types/Time.sol";
contract BurnerRouter is OwnableUpgradeable, IBurnerRouter {
using Subnetwork for bytes32;
using SafeERC20 for IERC20;
/**
* @inheritdoc IBurnerRouter
*/
address public collateral;
/**
* @inheritdoc IBurnerRouter
*/
uint256 public lastBalance;
/**
* @inheritdoc IBurnerRouter
*/
Uint48 public delay;
/**
* @inheritdoc IBurnerRouter
*/
PendingUint48 public pendingDelay;
/**
* @inheritdoc IBurnerRouter
*/
Address public globalReceiver;
/**
* @inheritdoc IBurnerRouter
*/
PendingAddress public pendingGlobalReceiver;
/**
* @inheritdoc IBurnerRouter
*/
mapping(address network => Address receiver) public networkReceiver;
/**
* @inheritdoc IBurnerRouter
*/
mapping(address network => PendingAddress pendingReceiver) public pendingNetworkReceiver;
/**
* @inheritdoc IBurnerRouter
*/
mapping(address network => mapping(address operator => Address receiver)) public operatorNetworkReceiver;
/**
* @inheritdoc IBurnerRouter
*/
mapping(address network => mapping(address operator => PendingAddress pendingReceiver)) public
pendingOperatorNetworkReceiver;
/**
* @inheritdoc IBurnerRouter
*/
mapping(address receiver => uint256 amount) public balanceOf;
constructor() {
_disableInitializers();
}
/**
* @inheritdoc IBurner
*/
function onSlash(
bytes32 subnetwork,
address operator,
uint256, /* amount */
uint48 /* captureTimestamp */
) external {
uint256 currentBalance = IERC20(collateral).balanceOf(address(this));
balanceOf[_getReceiver(subnetwork.network(), operator)] += currentBalance - lastBalance;
lastBalance = currentBalance;
}
/**
* @inheritdoc IBurnerRouter
*/
function triggerTransfer(
address receiver
) external returns (uint256 amount) {
amount = balanceOf[receiver];
if (amount == 0) {
revert InsufficientBalance();
}
lastBalance -= amount;
balanceOf[receiver] = 0;
IERC20(collateral).safeTransfer(receiver, amount);
emit TriggerTransfer(receiver, amount);
}
/**
* @inheritdoc IBurnerRouter
*/
function setGlobalReceiver(
address receiver
) external onlyOwner {
_tryAcceptDelay();
_setReceiver(receiver, globalReceiver, pendingGlobalReceiver);
emit SetGlobalReceiver(receiver);
}
/**
* @inheritdoc IBurnerRouter
*/
function acceptGlobalReceiver() external {
_acceptReceiver(globalReceiver, pendingGlobalReceiver);
emit AcceptGlobalReceiver();
}
/**
* @inheritdoc IBurnerRouter
*/
function setNetworkReceiver(address network, address receiver) external onlyOwner {
_tryAcceptDelay();
_setReceiver(receiver, networkReceiver[network], pendingNetworkReceiver[network]);
emit SetNetworkReceiver(network, receiver);
}
/**
* @inheritdoc IBurnerRouter
*/
function acceptNetworkReceiver(
address network
) external {
_acceptReceiver(networkReceiver[network], pendingNetworkReceiver[network]);
emit AcceptNetworkReceiver(network);
}
/**
* @inheritdoc IBurnerRouter
*/
function setOperatorNetworkReceiver(address network, address operator, address receiver) external onlyOwner {
_tryAcceptDelay();
_setReceiver(
receiver, operatorNetworkReceiver[network][operator], pendingOperatorNetworkReceiver[network][operator]
);
emit SetOperatorNetworkReceiver(network, operator, receiver);
}
/**
* @inheritdoc IBurnerRouter
*/
function acceptOperatorNetworkReceiver(address network, address operator) external {
_acceptReceiver(operatorNetworkReceiver[network][operator], pendingOperatorNetworkReceiver[network][operator]);
emit AcceptOperatorNetworkReceiver(network, operator);
}
/**
* @inheritdoc IBurnerRouter
*/
function setDelay(
uint48 newDelay
) external onlyOwner {
_tryAcceptDelay();
if (pendingDelay.timestamp != 0) {
pendingDelay.value = 0;
pendingDelay.timestamp = 0;
} else if (newDelay == delay.value) {
revert AlreadySet();
}
if (newDelay != delay.value) {
pendingDelay.value = newDelay;
pendingDelay.timestamp = Time.timestamp() + delay.value;
}
emit SetDelay(newDelay);
}
/**
* @inheritdoc IBurnerRouter
*/
function acceptDelay() external {
if (pendingDelay.timestamp == 0 || pendingDelay.timestamp > Time.timestamp()) {
revert NotReady();
}
_tryAcceptDelay();
}
function initialize(
InitParams calldata params
) external initializer {
if (params.collateral == address(0)) {
revert InvalidCollateral();
}
if (params.owner != address(0)) {
__Ownable_init(params.owner);
}
collateral = params.collateral;
delay.value = params.delay;
globalReceiver.value = params.globalReceiver;
for (uint256 i; i < params.networkReceivers.length; ++i) {
address network = params.networkReceivers[i].network;
address receiver = params.networkReceivers[i].receiver;
Address storage networkReceiver_ = networkReceiver[network];
if (receiver == address(0)) {
revert InvalidReceiver();
}
if (networkReceiver_.value != address(0)) {
revert DuplicateNetworkReceiver();
}
networkReceiver_.value = receiver;
}
for (uint256 i; i < params.operatorNetworkReceivers.length; ++i) {
address network = params.operatorNetworkReceivers[i].network;
address operator = params.operatorNetworkReceivers[i].operator;
address receiver = params.operatorNetworkReceivers[i].receiver;
Address storage operatorNetworkReceiver_ = operatorNetworkReceiver[network][operator];
if (receiver == address(0)) {
revert InvalidReceiver();
}
if (operatorNetworkReceiver_.value != address(0)) {
revert DuplicateOperatorNetworkReceiver();
}
operatorNetworkReceiver_.value = receiver;
}
}
function _getReceiver(address network, address operator) internal view returns (address receiver) {
address operatorNetworkReceiver_ = operatorNetworkReceiver[network][operator].value;
if (operatorNetworkReceiver_ != address(0)) {
return operatorNetworkReceiver_;
}
address networkReceiver_ = networkReceiver[network].value;
if (networkReceiver_ != address(0)) {
return networkReceiver_;
}
return globalReceiver.value;
}
function _setReceiver(
address newReceiver,
Address storage currentReceiver,
PendingAddress storage pendingReceiver
) internal {
if (pendingReceiver.timestamp != 0 && pendingReceiver.timestamp <= Time.timestamp()) {
currentReceiver.value = pendingReceiver.value;
pendingReceiver.value = address(0);
pendingReceiver.timestamp = 0;
}
if (pendingReceiver.timestamp != 0) {
pendingReceiver.value = address(0);
pendingReceiver.timestamp = 0;
} else if (newReceiver == currentReceiver.value) {
revert AlreadySet();
}
if (newReceiver != currentReceiver.value) {
pendingReceiver.value = newReceiver;
pendingReceiver.timestamp = Time.timestamp() + delay.value;
}
}
function _acceptReceiver(Address storage currentReceiver, PendingAddress storage pendingReceiver) internal {
if (pendingReceiver.timestamp == 0 || pendingReceiver.timestamp > Time.timestamp()) {
revert NotReady();
}
currentReceiver.value = pendingReceiver.value;
pendingReceiver.value = address(0);
pendingReceiver.timestamp = 0;
}
function _tryAcceptDelay() internal {
if (pendingDelay.timestamp != 0 && pendingDelay.timestamp <= Time.timestamp()) {
delay.value = pendingDelay.value;
pendingDelay.value = 0;
pendingDelay.timestamp = 0;
emit AcceptDelay();
}
}
}