-
Notifications
You must be signed in to change notification settings - Fork 10
/
L1StandardBridge.sol
896 lines (841 loc) · 35.2 KB
/
L1StandardBridge.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import { Predeploys } from "../libraries/Predeploys.sol";
import { StandardBridge } from "../universal/StandardBridge.sol";
import { Semver } from "../universal/Semver.sol";
import { SafeCall } from "../libraries/SafeCall.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { OptimismMintableERC20 } from "../universal/OptimismMintableERC20.sol";
import { L2StandardBridge } from "../L2/L2StandardBridge.sol";
/**
* @custom:proxied
* @title L1StandardBridge
* @notice The L1StandardBridge is responsible for transfering ETH and ERC20 tokens between L1 and
* L2. In the case that an ERC20 token is native to L1, it will be escrowed within this
* contract. If the ERC20 token is native to L2, it will be burnt. Before Bedrock, ETH was
* stored within this contract. After Bedrock, ETH is instead stored inside the
* OptimismPortal contract.
* NOTE: this contract is not intended to support all variations of ERC20 tokens. Examples
* of some token types that may not be properly supported by this contract include, but are
* not limited to: tokens with transfer fees, rebasing tokens, and tokens with blocklists.
*/
contract L1StandardBridge is StandardBridge, Semver {
using SafeERC20 for IERC20;
address public immutable L1_MNT_ADDRESS;
/**
* @custom:legacy
* @notice Emitted whenever a deposit of MNT from L1 into L2 is initiated.
*
* @param from Address of the depositor.
* @param to Address of the recipient on L2.
* @param amount Amount of MNT deposited.
* @param extraData Extra data attached to the deposit.
*/
event MNTDepositInitiated(
address indexed from,
address indexed to,
uint256 amount,
bytes extraData
);
/**
* @custom:legacy
* @notice Emitted whenever a withdrawal of MNT from L2 to L1 is finalized.
*
* @param from Address of the withdrawer.
* @param to Address of the recipient on L1.
* @param amount Amount of MNT withdrawn.
* @param extraData Extra data attached to the withdrawal.
*/
event MNTWithdrawalFinalized(
address indexed from,
address indexed to,
uint256 amount,
bytes extraData
);
/**
* @custom:legacy
* @notice Emitted whenever a deposit of ETH from L1 into L2 is initiated.
*
* @param from Address of the depositor.
* @param to Address of the recipient on L2.
* @param amount Amount of ETH deposited.
* @param extraData Extra data attached to the deposit.
*/
event ETHDepositInitiated(
address indexed from,
address indexed to,
uint256 amount,
bytes extraData
);
/**
* @custom:legacy
* @notice Emitted whenever a withdrawal of ETH from L2 to L1 is finalized.
*
* @param from Address of the withdrawer.
* @param to Address of the recipient on L1.
* @param amount Amount of ETH withdrawn.
* @param extraData Extra data attached to the withdrawal.
*/
event ETHWithdrawalFinalized(
address indexed from,
address indexed to,
uint256 amount,
bytes extraData
);
/**
* @custom:legacy
* @notice Emitted whenever an ERC20 deposit is initiated.
*
* @param l1Token Address of the token on L1.
* @param l2Token Address of the corresponding token on L2.
* @param from Address of the depositor.
* @param to Address of the recipient on L2.
* @param amount Amount of the ERC20 deposited.
* @param extraData Extra data attached to the deposit.
*/
event ERC20DepositInitiated(
address indexed l1Token,
address indexed l2Token,
address indexed from,
address to,
uint256 amount,
bytes extraData
);
/**
* @custom:legacy
* @notice Emitted whenever an ERC20 withdrawal is finalized.
*
* @param l1Token Address of the token on L1.
* @param l2Token Address of the corresponding token on L2.
* @param from Address of the withdrawer.
* @param to Address of the recipient on L1.
* @param amount Amount of the ERC20 withdrawn.
* @param extraData Extra data attached to the withdrawal.
*/
event ERC20WithdrawalFinalized(
address indexed l1Token,
address indexed l2Token,
address indexed from,
address to,
uint256 amount,
bytes extraData
);
/**
* @custom:semver 1.1.0
*
* @param _messenger Address of the L1CrossDomainMessenger.
*/
constructor(address payable _messenger, address _l1mnt)
Semver(1, 1, 0)
StandardBridge(_messenger, payable(Predeploys.L2_STANDARD_BRIDGE))
{
L1_MNT_ADDRESS = _l1mnt;
}
/**
* @notice Allows EOAs to bridge ETH by sending directly to the bridge.
*/
receive() external payable override onlyEOA {
_initiateETHDeposit(msg.sender, msg.sender, RECEIVE_DEFAULT_GAS_LIMIT, bytes(""));
}
/**
* @custom:legacy
* @notice Deposits some amount of ETH into the sender's account on L2.
*
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2. Data supplied here will not be used to
* execute any code on L2 and is only emitted as extra data for the
* convenience of off-chain tooling.
*/
function depositETH(uint32 _minGasLimit, bytes calldata _extraData) external payable onlyEOA {
_initiateETHDeposit(msg.sender, msg.sender, _minGasLimit, _extraData);
}
/**
* @custom:legacy
* @notice Deposits some amount of ETH into a target account on L2.
* Note that if ETH is sent to a contract on L2 and the call fails, then that ETH will
* be locked in the L2StandardBridge. ETH may be recoverable if the call can be
* successfully replayed by increasing the amount of gas supplied to the call. If the
* call will fail for any amount of gas, then the ETH will be locked permanently.
*
* @param _to Address of the recipient on L2.
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2. Data supplied here will not be used to
* execute any code on L2 and is only emitted as extra data for the
* convenience of off-chain tooling.
*/
function depositETHTo(
address _to,
uint32 _minGasLimit,
bytes calldata _extraData
) external payable {
_initiateETHDeposit(msg.sender, _to, _minGasLimit, _extraData);
}
/**
* @custom:legacy
* @notice Deposits some amount of MNT into the sender's account on L2.
*
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2. Data supplied here will not be used to
* execute any code on L2 and is only emitted as extra data for the
* convenience of off-chain tooling.
*/
function depositMNT(
uint256 _amount,
uint32 _minGasLimit,
bytes calldata _extraData
) external onlyEOA {
_initiateMNTDeposit(msg.sender, msg.sender, _amount, _minGasLimit, _extraData);
}
/**
* @custom:legacy
* @notice Deposits some amount of MNT into a target account on L2.
* Note that if MNT is sent to a contract on L2 and the call fails, then that ETH will
* be locked in the L2StandardBridge. ETH may be recoverable if the call can be
* successfully replayed by increasing the amount of gas supplied to the call. If the
* call will fail for any amount of gas, then the ETH will be locked permanently.
*
* @param _to Address of the recipient on L2.
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2. Data supplied here will not be used to
* execute any code on L2 and is only emitted as extra data for the
* convenience of off-chain tooling.
*/
function depositMNTTo(
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes calldata _extraData
) external payable {
_initiateMNTDeposit(msg.sender, _to, _amount, _minGasLimit, _extraData);
}
/**
* @custom:legacy
* @notice Deposits some amount of ERC20 tokens into the sender's account on L2.
*
* @param _l1Token Address of the L1 token being deposited.
* @param _l2Token Address of the corresponding token on L2.
* @param _amount Amount of the ERC20 to deposit.
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2. Data supplied here will not be used to
* execute any code on L2 and is only emitted as extra data for the
* convenience of off-chain tooling.
*/
function depositERC20(
address _l1Token,
address _l2Token,
uint256 _amount,
uint32 _minGasLimit,
bytes calldata _extraData
) external virtual onlyEOA {
_initiateERC20Deposit(
_l1Token,
_l2Token,
msg.sender,
msg.sender,
_amount,
_minGasLimit,
_extraData
);
}
/**
* @custom:legacy
* @notice Deposits some amount of ERC20 tokens into a target account on L2.
*
* @param _l1Token Address of the L1 token being deposited.
* @param _l2Token Address of the corresponding token on L2.
* @param _to Address of the recipient on L2.
* @param _amount Amount of the ERC20 to deposit.
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2. Data supplied here will not be used to
* execute any code on L2 and is only emitted as extra data for the
* convenience of off-chain tooling.
*/
function depositERC20To(
address _l1Token,
address _l2Token,
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes calldata _extraData
) external virtual {
_initiateERC20Deposit(
_l1Token,
_l2Token,
msg.sender,
_to,
_amount,
_minGasLimit,
_extraData
);
}
/**
* @custom:legacy
* @notice Finalizes a withdrawal of MNT from L2.
*
* @param _from Address of the withdrawer on L2.
* @param _to Address of the recipient on L1.
* @param _amount Amount of MNT to withdraw.
* @param _extraData Optional data forwarded from L2.
*/
function finalizeMantleWithdrawal(
address _from,
address _to,
uint256 _amount,
bytes calldata _extraData
) external payable {
finalizeBridgeMNT(_from, _to, _amount, _extraData);
}
/**
* @custom:legacy
* @notice Finalizes a withdrawal of ETH from L2.
*
* @param _from Address of the withdrawer on L2.
* @param _to Address of the recipient on L1.
* @param _amount Amount of ETH to withdraw.
* @param _extraData Optional data forwarded from L2.
*/
function finalizeETHWithdrawal(
address _from,
address _to,
uint256 _amount,
bytes calldata _extraData
) external payable {
finalizeBridgeETH(_from, _to, _amount, _extraData);
}
/**
* @custom:legacy
* @notice Finalizes a withdrawal of ERC20 tokens from L2.
*
* @param _l1Token Address of the token on L1.
* @param _l2Token Address of the corresponding token on L2.
* @param _from Address of the withdrawer on L2.
* @param _to Address of the recipient on L1.
* @param _amount Amount of the ERC20 to withdraw.
* @param _extraData Optional data forwarded from L2.
*/
function finalizeERC20Withdrawal(
address _l1Token,
address _l2Token,
address _from,
address _to,
uint256 _amount,
bytes calldata _extraData
) external {
finalizeBridgeERC20(_l1Token, _l2Token, _from, _to, _amount, _extraData);
}
/**
* @custom:legacy
* @notice Retrieves the access of the corresponding L2 bridge contract.
*
* @return Address of the corresponding L2 bridge contract.
*/
function l2TokenBridge() external view returns (address) {
return address(OTHER_BRIDGE);
}
/**
* @notice Internal function for initiating an ETH deposit.
*
* @param _from Address of the sender on L1.
* @param _to Address of the recipient on L2.
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2.
*/
function _initiateETHDeposit(
address _from,
address _to,
uint32 _minGasLimit,
bytes memory _extraData
) internal {
_initiateBridgeETH(_from, _to, msg.value, _minGasLimit, _extraData);
}
/**
* @notice Internal function for initiating an ERC20 deposit.
*
* @param _l1Token Address of the L1 token being deposited.
* @param _l2Token Address of the corresponding token on L2.
* @param _from Address of the sender on L1.
* @param _to Address of the recipient on L2.
* @param _amount Amount of the ERC20 to deposit.
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2.
*/
function _initiateERC20Deposit(
address _l1Token,
address _l2Token,
address _from,
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes memory _extraData
) internal {
_initiateBridgeERC20(_l1Token, _l2Token, _from, _to, _amount, _minGasLimit, _extraData);
}
/**
* @notice Internal function for initiating an MNT deposit.
*
* @param _from Address of the sender on L1.
* @param _to Address of the recipient on L2.
* @param _amount Amount of the ERC20 to deposit.
* @param _minGasLimit Minimum gas limit for the deposit message on L2.
* @param _extraData Optional data to forward to L2.
*/
function _initiateMNTDeposit(
address _from,
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes memory _extraData
) internal {
_initiateBridgeMNT(_from, _to, _amount, _minGasLimit, _extraData);
}
/**
* @notice Emits the legacy ETHDepositInitiated event followed by the ETHBridgeInitiated event.
* This is necessary for backwards compatibility with the legacy bridge.
*
* @inheritdoc StandardBridge
*/
function _emitETHBridgeInitiated(
address _from,
address _to,
uint256 _amount,
bytes memory _extraData
) internal override {
emit ETHDepositInitiated(_from, _to, _amount, _extraData);
super._emitETHBridgeInitiated(_from, _to, _amount, _extraData);
}
/**
* @notice Emits the legacy ETHWithdrawalFinalized event followed by the ETHBridgeFinalized
* event. This is necessary for backwards compatibility with the legacy bridge.
*
* @inheritdoc StandardBridge
*/
function _emitETHBridgeFinalized(
address _from,
address _to,
uint256 _amount,
bytes memory _extraData
) internal override {
emit ETHWithdrawalFinalized(_from, _to, _amount, _extraData);
super._emitETHBridgeFinalized(_from, _to, _amount, _extraData);
}
/**
* @notice Emits the legacy ETHDepositInitiated event followed by the ETHBridgeInitiated event.
* This is necessary for backwards compatibility with the legacy bridge.
*
* @inheritdoc StandardBridge
*/
function _emitMNTBridgeInitiated(
address _from,
address _to,
uint256 _amount,
bytes memory _extraData
) internal override {
emit MNTDepositInitiated(_from, _to, _amount, _extraData);
super._emitMNTBridgeInitiated(_from, _to, _amount, _extraData);
}
/**
* @notice Emits the legacy MNTWithdrawalFinalized event followed by the MNTBridgeFinalized
* event. This is necessary for backwards compatibility with the legacy bridge.
*
* @inheritdoc StandardBridge
*/
function _emitMNTBridgeFinalized(
address _from,
address _to,
uint256 _amount,
bytes memory _extraData
) internal override {
emit MNTWithdrawalFinalized(_from, _to, _amount, _extraData);
super._emitMNTBridgeFinalized(_from, _to, _amount, _extraData);
}
/**
* @notice Emits the legacy ERC20DepositInitiated event followed by the ERC20BridgeInitiated
* event. This is necessary for backwards compatibility with the legacy bridge.
*
* @inheritdoc StandardBridge
*/
function _emitERC20BridgeInitiated(
address _localToken,
address _remoteToken,
address _from,
address _to,
uint256 _amount,
bytes memory _extraData
) internal override {
emit ERC20DepositInitiated(_localToken, _remoteToken, _from, _to, _amount, _extraData);
super._emitERC20BridgeInitiated(_localToken, _remoteToken, _from, _to, _amount, _extraData);
}
/**
* @notice Emits the legacy ERC20WithdrawalFinalized event followed by the ERC20BridgeFinalized
* event. This is necessary for backwards compatibility with the legacy bridge.
*
* @inheritdoc StandardBridge
*/
function _emitERC20BridgeFinalized(
address _localToken,
address _remoteToken,
address _from,
address _to,
uint256 _amount,
bytes memory _extraData
) internal override {
emit ERC20WithdrawalFinalized(_localToken, _remoteToken, _from, _to, _amount, _extraData);
super._emitERC20BridgeFinalized(_localToken, _remoteToken, _from, _to, _amount, _extraData);
}
/**
* @notice Sends ETH to the sender's address on the other chain.
*
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function bridgeETH(uint32 _minGasLimit, bytes calldata _extraData) public payable onlyEOA {
_initiateBridgeETH(msg.sender, msg.sender, msg.value, _minGasLimit, _extraData);
}
/**
* @notice Sends ETH to a receiver's address on the other chain. Note that if ETH is sent to a
* smart contract and the call fails, the ETH will be temporarily locked in the
* StandardBridge on the other chain until the call is replayed. If the call cannot be
* replayed with any amount of gas (call always reverts), then the ETH will be
* permanently locked in the StandardBridge on the other chain. ETH will also
* be locked if the receiver is the other bridge, because finalizeBridgeETH will revert
* in that case.
*
* @param _to Address of the receiver.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function bridgeETHTo(
address _to,
uint32 _minGasLimit,
bytes calldata _extraData
) public payable {
_initiateBridgeETH(msg.sender, _to, msg.value, _minGasLimit, _extraData);
}
/**
* @notice Sends MNT to the sender's address on the other chain.
*
* @param _amount Amount of the MNT.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function bridgeMNT(uint256 _amount, uint32 _minGasLimit, bytes calldata _extraData) public payable onlyEOA {
_initiateBridgeMNT(msg.sender, msg.sender, _amount, _minGasLimit, _extraData);
}
/**
* @notice Sends MNT to the sender's address on the other chain.
*
* @param _to Address of the receiver.
* @param _amount Amount of the MNT.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function bridgeMNTTo(
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes calldata _extraData
) public payable {
_initiateBridgeMNT(msg.sender, _to, _amount, _minGasLimit, _extraData);
}
/**
* @notice Sends ERC20 tokens to the sender's address on the other chain. Note that if the
* ERC20 token on the other chain does not recognize the local token as the correct
* pair token, the ERC20 bridge will fail and the tokens will be returned to sender on
* this chain.
*
* @param _localToken Address of the ERC20 on this chain.
* @param _remoteToken Address of the corresponding token on the remote chain.
* @param _amount Amount of local tokens to deposit.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function bridgeERC20(
address _localToken,
address _remoteToken,
uint256 _amount,
uint32 _minGasLimit,
bytes calldata _extraData
) public onlyEOA override {
_initiateBridgeERC20(
_localToken,
_remoteToken,
msg.sender,
msg.sender,
_amount,
_minGasLimit,
_extraData
);
}
/**
* @notice Sends ERC20 tokens to a receiver's address on the other chain. Note that if the
* ERC20 token on the other chain does not recognize the local token as the correct
* pair token, the ERC20 bridge will fail and the tokens will be returned to sender on
* this chain.
*
* @param _localToken Address of the ERC20 on this chain.
* @param _remoteToken Address of the corresponding token on the remote chain.
* @param _to Address of the receiver.
* @param _amount Amount of local tokens to deposit.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function bridgeERC20To(
address _localToken,
address _remoteToken,
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes calldata _extraData
) public override {
_initiateBridgeERC20(
_localToken,
_remoteToken,
msg.sender,
_to,
_amount,
_minGasLimit,
_extraData
);
}
/**
* @notice Finalizes an ETH bridge on this chain. Can only be triggered by the other
* StandardBridge contract on the remote chain.
*
* @param _from Address of the sender.
* @param _to Address of the receiver.
* @param _amount Amount of ETH being bridged.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function finalizeBridgeETH(
address _from,
address _to,
uint256 _amount,
bytes calldata _extraData
) public payable override onlyOtherBridge {
require(msg.value == _amount, "StandardBridge: amount sent does not match amount required");
require(_to != address(this), "StandardBridge: cannot send to self");
require(_to != address(MESSENGER), "StandardBridge: cannot send to messenger");
// Emit the correct events. By default this will be _amount, but child
// contracts may override this function in order to emit legacy events as well.
_emitETHBridgeFinalized(_from, _to, _amount, _extraData);
bool success = SafeCall.call(_to, gasleft(), _amount, hex"");
require(success, "StandardBridge: ETH transfer failed");
}
/**
* @notice Finalizes an ERC20 bridge on this chain. Can only be triggered by the other
* StandardBridge contract on the remote chain.
*
* @param _localToken Address of the ERC20 on this chain.
* @param _remoteToken Address of the corresponding token on the remote chain.
* @param _from Address of the sender.
* @param _to Address of the receiver.
* @param _amount Amount of the ERC20 being bridged.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function finalizeBridgeERC20(
address _localToken,
address _remoteToken,
address _from,
address _to,
uint256 _amount,
bytes calldata _extraData
) public onlyOtherBridge override {
if (_isOptimismMintableERC20(_localToken)) {
require(
_isCorrectTokenPair(_localToken, _remoteToken),
"StandardBridge: wrong remote token for Optimism Mintable ERC20 local token"
);
OptimismMintableERC20(_localToken).mint(_to, _amount);
} else {
uint256 balanceBefore = IERC20(_localToken).balanceOf(address(this));
IERC20(_localToken).safeTransfer(_to, _amount);
uint256 balanceAfter = IERC20(_localToken).balanceOf(address(this));
uint256 sentAmount = balanceBefore - balanceAfter;
deposits[_localToken][_remoteToken] = deposits[_localToken][_remoteToken] - sentAmount;
}
// Emit the correct events. By default this will be ERC20BridgeFinalized, but child
// contracts may override this function in order to emit legacy events as well.
_emitERC20BridgeFinalized(_localToken, _remoteToken, _from, _to, _amount, _extraData);
}
/**
* @notice Finalizes an MNT bridge on this chain. Can only be triggered by the other
* StandardBridge contract on the remote chain.
*
* @param _from Address of the sender.
* @param _to Address of the receiver.
* @param _amount Amount of the MNT being bridged.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function finalizeBridgeMNT(
address _from,
address _to,
uint256 _amount,
bytes calldata _extraData
) public payable override onlyOtherBridge {
require(_to != address(this), "StandardBridge: cannot send to self");
require(_to != address(MESSENGER), "StandardBridge: cannot send to messenger");
IERC20(L1_MNT_ADDRESS).safeTransferFrom(address(MESSENGER), _to, _amount);
// Emit the correct events. By default this will be ERC20BridgeFinalized, but child
// contracts may override this function in order to emit legacy events as well.
_emitMNTBridgeFinalized(_from, _to, _amount, _extraData);
}
/**
* @notice Initiates a bridge of ETH through the CrossDomainMessenger.
*
* @param _from Address of the sender.
* @param _to Address of the receiver.
* @param _amount Amount of ETH being bridged.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function _initiateBridgeETH(
address _from,
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes memory _extraData
) internal override {
require(
msg.value == _amount,
"L1StandardBridge: bridging ETH must include sufficient ETH value"
);
// Emit the correct events. By default this will be _amount, but child
// contracts may override this function in order to emit legacy events as well.
_emitETHBridgeInitiated(_from, _to, _amount, _extraData);
uint256 zeroMNTValue = 0;
MESSENGER.sendMessage{value: msg.value}(
zeroMNTValue,
address(OTHER_BRIDGE),
abi.encodeWithSelector(
L2StandardBridge.finalizeBridgeETH.selector,
_from,
_to,
_amount,
_extraData
),
_minGasLimit
);
}
/**
* @notice Sends ERC20 tokens to a receiver's address on the other chain.
*
* @param _localToken Address of the ERC20 on this chain.
* @param _remoteToken Address of the corresponding token on the remote chain.
* @param _to Address of the receiver.
* @param _amount Amount of local tokens to deposit.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function _initiateBridgeERC20(
address _localToken,
address _remoteToken,
address _from,
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes memory _extraData
) internal override {
require(_localToken != address(0) && _remoteToken != Predeploys.BVM_ETH,
"L1StandardBridge: BridgeERC20 do not support ETH bridging.");
require(_localToken != L1_MNT_ADDRESS && _remoteToken != address(0x0),
"L1StandardBridge: BridgeERC20 do not support MNT bridging.");
if (_isOptimismMintableERC20(_localToken)) {
require(
_isCorrectTokenPair(_localToken, _remoteToken),
"StandardBridge: wrong remote token for Optimism Mintable ERC20 local token"
);
OptimismMintableERC20(_localToken).burn(_from, _amount);
} else {
uint256 balanceBefore = IERC20(_localToken).balanceOf(address(this));
IERC20(_localToken).safeTransferFrom(_from, address(this), _amount);
uint256 balanceAfter = IERC20(_localToken).balanceOf(address(this));
uint256 receivedAmount = balanceAfter - balanceBefore;
deposits[_localToken][_remoteToken] = deposits[_localToken][_remoteToken] + receivedAmount;
}
// Emit the correct events. By default this will be ERC20BridgeInitiated, but child
// contracts may override this function in order to emit legacy events as well.
_emitERC20BridgeInitiated(_localToken, _remoteToken, _from, _to, _amount, _extraData);
uint256 zeroMNTValue = 0;
MESSENGER.sendMessage(
zeroMNTValue,
address(OTHER_BRIDGE),
abi.encodeWithSelector(
L2StandardBridge.finalizeBridgeERC20.selector,
// Because this call will be executed on the remote chain, we reverse the order of
// the remote and local token addresses relative to their order in the
// finalizeBridgeERC20 function.
_remoteToken,
_localToken,
_from,
_to,
_amount,
_extraData
),
_minGasLimit
);
}
/**
* @notice Sends MNT tokens to a receiver's address on the other chain.
*
* @param _to Address of the receiver.
* @param _amount Amount of local tokens to deposit.
* @param _minGasLimit Minimum amount of gas that the bridge can be relayed with.
* @param _extraData Extra data to be sent with the transaction. Note that the recipient will
* not be triggered with this data, but it will be emitted and can be used
* to identify the transaction.
*/
function _initiateBridgeMNT(
address _from,
address _to,
uint256 _amount,
uint32 _minGasLimit,
bytes memory _extraData
) internal override {
require(msg.value==0, "L1StandardBridge: deposit MNT should not include ETH value.");
IERC20(L1_MNT_ADDRESS).safeTransferFrom(_from, address(this), _amount);
// L1StandardBridge: approve for L1 MNT either reverts or returns true, there is no case in
// which its result value is false.
IERC20(L1_MNT_ADDRESS).approve(address(MESSENGER), _amount);
// Emit the correct events. By default this will be ERC20BridgeInitiated, but child
// contracts may override this function in order to emit legacy events as well.
_emitMNTBridgeInitiated(_from, _to, _amount, _extraData);
MESSENGER.sendMessage{value: msg.value}(
_amount,
address(OTHER_BRIDGE),
abi.encodeWithSelector(
L2StandardBridge.finalizeBridgeMNT.selector,
// Because this call will be executed on the remote chain, we reverse the order of
// the remote and local token addresses relative to their order in the
// finalizeBridgeERC20 function.
_from,
_to,
_amount,
_extraData
),
_minGasLimit
);
}
}