/
LightBridge.sol
500 lines (430 loc) · 19 KB
/
LightBridge.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
/* External Imports */
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/MulticallUpgradeable.sol";
import '@openzeppelin/contracts/utils/Address.sol';
/**
* @title LightBridge
*
* Bridge the native asset or whitelisted ERC20 tokens between whitelisted networks (L2's/L1's).
*
* @notice The contract itself emits events and locks the funds to be bridged/teleported. These events are then picked up by a backend service that releases the corresponding token or native asset on the destination network. Withdrawal periods (e.g. for optimistic rollups) are not handled by the contract itself, but would be handled on the Teleportation service if deemed necessary.
* @dev Implementation of the Teleportation service can be found at https://github.com/bobanetwork/boba within /packages/boba/teleportation if not moved.
*/
contract LightBridge is PausableUpgradeable, MulticallUpgradeable {
using Address for address;
using SafeERC20 for IERC20;
/**************
* Struct *
**************/
struct Disbursement {
address token;
uint256 amount;
address addr;
uint32 sourceChainId;
uint256 depositId;
}
struct FailedNativeDisbursement {
bool failed;
Disbursement disbursement;
}
struct SupportedToken {
bool supported;
// The minimum amount that needs to be deposited in a receive.
uint256 minDepositAmount;
// The maximum amount that can be deposited in a receive.
uint256 maxDepositAmount;
// set maximum amount of tokens that can be transferred in 24 hours
uint256 maxTransferAmountPerDay;
// The total amount of tokens transferred in 24 hours
uint256 transferredAmount;
// The timestamp of the checkpoint
uint256 transferTimestampCheckPoint;
}
/*************
* Variables *
*************/
/// @dev Wallet that is being used to release teleported assets on the destination network.
address public disburser;
/// @dev General owner wallet to change configurations.
address public owner;
/// @dev Assets and networks to be supported. ZeroAddress for native asset
/// {assetAddress} => {targetChainId} => {tokenConfig}
mapping(address => mapping(uint32 => SupportedToken)) public supportedTokens;
/// @dev The total number of successful deposits received.
mapping(uint256 => uint256) public totalDeposits;
/// @dev The total number of disbursements processed.
mapping(uint256 => uint256) public totalDisbursements;
// @dev depositId to failed status and disbursement info
mapping(uint256 => FailedNativeDisbursement) public failedNativeDisbursements;
/********************
* Events *
********************/
event MinDepositAmountSet(
/* @dev Zero Address = native asset **/
address token,
uint32 toChainId,
uint256 previousAmount,
uint256 newAmount
);
event MaxDepositAmountSet(
/* @dev Zero Address = native asset **/
address token,
uint32 toChainId,
uint256 previousAmount,
uint256 newAmount
);
event MaxTransferAmountPerDaySet(
address token,
uint32 toChainId,
uint256 previousAmount,
uint256 newAmount
);
event AssetBalanceWithdrawn(
address indexed token,
address indexed owner,
uint256 balance
);
event AssetReceived(
/** @dev Must be ZeroAddress for nativeAsset */
address token,
uint32 sourceChainId,
uint32 indexed toChainId,
uint256 indexed depositId,
address indexed emitter,
uint256 amount
);
event DisbursementSuccess(
uint256 indexed depositId,
address indexed to,
address indexed token,
uint256 amount,
uint32 sourceChainId
);
/** @dev only for native assets */
event DisbursementFailed(
uint256 indexed depositId,
address indexed to,
uint256 amount,
uint32 sourceChainId
);
/** @dev Only for native assets */
event DisbursementRetrySuccess(
uint256 indexed depositId,
address indexed to,
uint256 amount,
uint32 sourceChainId
);
event DisburserTransferred(
address newDisburser
);
event OwnershipTransferred(
address newOwner
);
event TokenSupported(
address indexed token,
uint32 indexed toChainId,
bool supported
);
/**********************
* Function Modifiers *
**********************/
modifier onlyDisburser() {
require(msg.sender == disburser, 'Caller is not the disburser');
_;
}
modifier onlyOwner() {
require(msg.sender == owner, 'Caller is not the owner');
_;
}
modifier onlyNotInitialized() {
require(address(disburser) == address(0), "Contract has been initialized");
_;
}
modifier onlyInitialized() {
require(address(disburser) != address(0), "Contract has not yet been initialized");
_;
}
/********************
* Public Functions *
********************/
/// @dev Initialize this contract
function initialize() external onlyNotInitialized() initializer() {
disburser = msg.sender;
owner = msg.sender;
__Context_init_unchained();
__Pausable_init_unchained();
__Multicall_init_unchained();
emit DisburserTransferred(msg.sender);
emit OwnershipTransferred(msg.sender);
}
/**
* @dev Add support of a specific ERC20 token on this network.
*
* @param _token Token address to support or ZeroAddress for native asset.
*/
function addSupportedToken(address _token, uint32 _toChainId, uint256 _minDepositAmount, uint256 _maxDepositAmount, uint256 _maxTransferAmountPerDay) public onlyOwner() onlyInitialized() {
require(supportedTokens[_token][_toChainId].supported == false, "Already supported");
// Not added ERC165 as implemented for L1 ERC20
require(address(0) == _token || Address.isContract(_token), "Not contract or native");
// doesn't ensure it's ERC20
require(_minDepositAmount > 0 && _minDepositAmount <= _maxDepositAmount, "incorrect min/max deposit");
// set maximum amount of tokens that can be transferred in 24 hours
require(_maxDepositAmount <= _maxTransferAmountPerDay, "max deposit amount more than daily limit");
supportedTokens[_token][_toChainId] = SupportedToken(true, _minDepositAmount, _maxDepositAmount, _maxTransferAmountPerDay, 0, block.timestamp);
emit TokenSupported(_token, _toChainId, true);
emit MinDepositAmountSet(_token, _toChainId, 0, _minDepositAmount);
emit MaxDepositAmountSet(_token, _toChainId, 0, _maxDepositAmount);
emit MaxTransferAmountPerDaySet(_token, _toChainId, 0, _maxTransferAmountPerDay);
}
/**
* @dev remove the support for a specific token.
*
* @param _token The token not to support.
*/
function removeSupportedToken(address _token, uint32 _toChainId) external onlyOwner() onlyInitialized() {
require(supportedTokens[_token][_toChainId].supported == true, "Already not supported");
delete supportedTokens[_token][_toChainId];
emit TokenSupported(_token, _toChainId, false);
}
/**
* @dev Accepts deposits that will be disbursed to the sender's address on target L2.
* The method reverts if the amount is less than the current
* minDepositAmount, the amount is greater than the current
* maxDepositAmount.
*
* @param _token ERC20 address of the token to deposit. Zero-Address indicates native asset.
* @param _amount The amount of token or native asset to deposit (must be the same as msg.value if native asset)
* @param _toChainId The destination chain ID.
*/
function teleportAsset(address _token, uint256 _amount, uint32 _toChainId)
external
payable
whenNotPaused()
{
SupportedToken memory supToken = supportedTokens[_token][_toChainId];
require(supToken.supported == true, "Token or chain not supported");
require(_amount >= supToken.minDepositAmount, "Deposit amount too small");
require(_amount <= supToken.maxDepositAmount, "Deposit amount too big");
// minimal workaround to keep logic concise
require((address(0) != _token && msg.value == 0) || (address(0) == _token && _amount == msg.value), "Native amount invalid");
// check if the total amount transferred is smaller than the maximum amount of tokens can be transferred in 24 hours
// if it's out of 24 hours, reset the transferred amount to 0 and set the transferTimestampCheckPoint to the current time
if (block.timestamp < supToken.transferTimestampCheckPoint + 1 days) {
supToken.transferredAmount += _amount;
require(supToken.transferredAmount <= supToken.maxTransferAmountPerDay, "max amount per day exceeded");
} else {
supToken.transferredAmount = _amount;
require(supToken.transferredAmount <= supToken.maxTransferAmountPerDay, "max amount per day exceeded");
supToken.transferTimestampCheckPoint = block.timestamp;
}
supportedTokens[_token][_toChainId] = supToken;
totalDeposits[_toChainId] += 1;
if (_token != address(0)) {
IERC20(_token).safeTransferFrom(msg.sender, address(this), _amount);
}
emit AssetReceived(_token, uint32(block.chainid), _toChainId, totalDeposits[_toChainId] - 1, msg.sender, _amount);
}
/**
* @dev Accepts a list of Disbursements and forwards the amount paid to
* the contract to each recipient. The method reverts if there are zero
* disbursements, the total amount to forward differs from the amount sent
* in the transaction, or the _nextDepositId is unexpected. Failed
* disbursements will not cause the method to revert, but will instead be
* held by the contract and availabe for the owner to withdraw.
*
* @param _disbursements A list of Disbursements to process.
*/
function disburseAsset(Disbursement[] calldata _disbursements)
external
payable
onlyDisburser()
whenNotPaused()
{
// Ensure there are disbursements to process.
uint256 _numDisbursements = _disbursements.length;
require(_numDisbursements > 0, "No disbursements");
// Process disbursements.
uint256 remainingValue = msg.value;
for (uint256 i = 0; i < _numDisbursements; i++) {
uint256 _amount = _disbursements[i].amount;
address _addr = _disbursements[i].addr;
uint32 _sourceChainId = _disbursements[i].sourceChainId;
uint256 _depositId = _disbursements[i].depositId;
address _token = _disbursements[i].token;
// Bidirectional support expected
require(supportedTokens[_token][_sourceChainId].supported, "Token or chain not supported");
// Ensure the depositId matches our expected value.
require(_depositId == totalDisbursements[_sourceChainId], "Unexpected next deposit id");
totalDisbursements[_sourceChainId] += 1;
// ensure amount sent in the tx is equal to disbursement (moved into loop to ensure token flexibility)
if (_token == address(0)) {
require(_amount <= remainingValue, "Disbursement total != amount sent");
remainingValue -= _amount;
} else {
IERC20(_token).safeTransferFrom(msg.sender, address(this), _amount);
}
if (_token == address(0)) {
// Deliver the disbursement amount to the receiver. If the
// disbursement fails, the amount will be kept by the contract
// rather than reverting to prevent blocking progress on other
// disbursements.
// slither-disable-next-line calls-loop,reentrancy-events
(bool success,) = _addr.call{gas: 3000, value: _amount}("");
if (success) emit DisbursementSuccess(_depositId, _addr, _token, _amount, _sourceChainId);
else {
failedNativeDisbursements[_depositId] = FailedNativeDisbursement(true, _disbursements[i]);
emit DisbursementFailed(_depositId, _addr, _amount, _sourceChainId);
}
} else {
// slither-disable-next-line calls-loop,reentrancy-events
IERC20(_token).safeTransfer(_addr, _amount);
emit DisbursementSuccess(_depositId, _addr, _token, _amount, _sourceChainId);
}
}
}
/**
* @dev Retry native disbursement if it failed previously. Only applies to native disbursements bc. of low-level call.
*
* @param _depositIds A list of DepositIds to process.
*/
function retryDisburseNative(uint256[] memory _depositIds)
external
payable
onlyDisburser()
whenNotPaused()
{
// Ensure there are disbursements to process.
uint256 _numDisbursements = _depositIds.length;
require(_numDisbursements > 0, "No disbursements");
// Failed Disbursement amounts should remain in the contract
// Process disbursements.
for (uint256 i = 0; i < _numDisbursements; i++) {
FailedNativeDisbursement storage failedDisbursement = failedNativeDisbursements[_depositIds[i]];
require(failedDisbursement.failed, "DepositId not failed disbursement");
uint256 _amount = failedDisbursement.disbursement.amount;
address _addr = failedDisbursement.disbursement.addr;
uint32 _sourceChainId = failedDisbursement.disbursement.sourceChainId;
// slither-disable-next-line calls-loop,reentrancy-events
(bool success,) = _addr.call{gas: 3000, value: _amount}("");
if (success) {
delete failedNativeDisbursements[_depositIds[i]];
emit DisbursementRetrySuccess(_depositIds[i], _addr, _amount, _sourceChainId);
}
}
}
/********************
* Admin Functions *
********************/
/**
* @dev Pause contract
*/
function pause() external onlyOwner() {
_pause();
}
/**
* @dev UnPause contract
*/
function unpause() external onlyOwner() {
_unpause();
}
/**
* @dev Sends the contract's current balance to the owner.
*/
function withdrawBalance(address _token, uint256 _amount)
external
onlyOwner()
onlyInitialized()
{
require(_amount != 0, "Amount is 0");
if (address(0) == _token) {
uint256 _balance = address(this).balance;
require(_balance >= _amount, "Too high");
(bool sent,) = owner.call{gas: 2300, value: _amount}("");
require(sent, "Failed to send Ether");
} else {
// no supportedToken check in case of generally lost tokens
uint256 _balance = IERC20(_token).balanceOf(address(this));
require(_balance >= _amount, "Too high");
IERC20(_token).safeTransfer(owner, _amount);
}
emit AssetBalanceWithdrawn(_token, owner, _amount);
}
/**
* @dev transfer disburser role to new address
*
* @param _newDisburser new disburser of this contract
*/
function transferDisburser(
address _newDisburser
)
external
onlyOwner()
{
require(_newDisburser != address(0), 'New disburser cannot be the zero address');
disburser = _newDisburser;
emit DisburserTransferred(_newDisburser);
}
/**
* @dev transfer ownership
*
* @param _newOwner new admin owner of this contract
*/
function transferOwnership(
address _newOwner
)
external
onlyOwner()
{
require(_newOwner != address(0), 'New owner cannot be the zero address');
owner = _newOwner;
emit OwnershipTransferred(_newOwner);
}
/**
* @notice Sets the minimum amount that can be deposited in a receive.
*
* @param _token configure for which token or ZeroAddress for native
* @param _toChainId The destination network associated with the minimum deposit amount.
* @param _minDepositAmount The new minimum deposit amount.
*/
function setMinAmount(address _token, uint32 _toChainId, uint256 _minDepositAmount) external onlyOwner() {
SupportedToken memory supToken = supportedTokens[_token][_toChainId];
require(supToken.supported, "Token or chain not supported");
require(_minDepositAmount > 0 && _minDepositAmount <= supToken.maxDepositAmount, "incorrect min deposit amount");
uint256 pastMinDepositAmount = supToken.minDepositAmount;
supportedTokens[_token][_toChainId].minDepositAmount = _minDepositAmount;
emit MinDepositAmountSet(_token, _toChainId, pastMinDepositAmount, _minDepositAmount);
}
/**
* @dev Sets the maximum amount that can be deposited in a receive.
*
* @param _token configure for which token or ZeroAddr for native asset
* @param _toChainId target chain id to set configuration for
* @param _maxDepositAmount The new maximum deposit amount.
*/
function setMaxAmount(address _token, uint32 _toChainId, uint256 _maxDepositAmount) external onlyOwner() {
SupportedToken memory supToken = supportedTokens[_token][_toChainId];
require(supToken.supported, "Token or chain not supported");
require(_maxDepositAmount <= supToken.maxTransferAmountPerDay, "max deposit amount more than daily limit");
require(_maxDepositAmount > 0 && _maxDepositAmount >= supToken.minDepositAmount, "incorrect max deposit amount");
uint256 pastMaxDepositAmount = supToken.maxDepositAmount;
supportedTokens[_token][_toChainId].maxDepositAmount = _maxDepositAmount;
emit MaxDepositAmountSet(_token, _toChainId, pastMaxDepositAmount, _maxDepositAmount);
}
/**
* @dev Sets maximum amount of disbursements that can be processed in a day
*
* @param _token Token or native asset (ZeroAddr) to set value for
* @param _maxTransferAmountPerDay The new maximum daily transfer amount.
*/
function setMaxTransferAmountPerDay(address _token, uint32 _toChainId, uint256 _maxTransferAmountPerDay) external onlyOwner() {
SupportedToken memory supToken = supportedTokens[_token][_toChainId];
require(supToken.supported, "Token or chain not supported");
require(_maxTransferAmountPerDay > 0 && _maxTransferAmountPerDay >= supToken.maxDepositAmount, "incorrect daily limit");
uint256 pastMaxTransferAmountPerDay = supToken.maxTransferAmountPerDay;
supportedTokens[_token][_toChainId].maxTransferAmountPerDay = _maxTransferAmountPerDay;
emit MaxTransferAmountPerDaySet(_token, _toChainId, pastMaxTransferAmountPerDay, _maxTransferAmountPerDay);
}
}