forked from lidofinance/lido-l2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathL1ERC20ExtendedTokensBridge.sol
179 lines (163 loc) · 6.95 KB
/
L1ERC20ExtendedTokensBridge.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
// SPDX-FileCopyrightText: 2024 Lido <info@lido.fi>
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IL1ERC20Bridge} from "./interfaces/IL1ERC20Bridge.sol";
import {IL2ERC20Bridge} from "./interfaces/IL2ERC20Bridge.sol";
import {IERC20Wrapper} from "../token/interfaces/IERC20Wrapper.sol";
import {BridgingManager} from "../BridgingManager.sol";
import {RebasableAndNonRebasableTokens} from "./RebasableAndNonRebasableTokens.sol";
import {CrossDomainEnabled} from "./CrossDomainEnabled.sol";
import {DepositDataCodec} from "../lib//DepositDataCodec.sol";
/// @author psirex, kovalgek
/// @notice The L1 ERC20 token bridge locks bridged tokens on the L1 side, sends deposit messages
/// on the L2 side, and finalizes token withdrawals from L2. Additionally, adds the methods for
/// bridging management: enabling and disabling withdrawals/deposits
abstract contract L1ERC20ExtendedTokensBridge is
IL1ERC20Bridge,
BridgingManager,
RebasableAndNonRebasableTokens,
CrossDomainEnabled
{
using SafeERC20 for IERC20;
address private immutable L2_TOKEN_BRIDGE;
/// @param messenger_ L1 messenger address being used for cross-chain communications
/// @param l2TokenBridge_ Address of the corresponding L2 bridge
/// @param l1TokenNonRebasable_ Address of the bridged token in the L1 chain
/// @param l1TokenRebasable_ Address of the bridged token in the L1 chain
/// @param l2TokenNonRebasable_ Address of the token minted on the L2 chain when token bridged
/// @param l2TokenRebasable_ Address of the token minted on the L2 chain when token bridged
constructor(
address messenger_,
address l2TokenBridge_,
address l1TokenNonRebasable_,
address l1TokenRebasable_,
address l2TokenNonRebasable_,
address l2TokenRebasable_
) CrossDomainEnabled(messenger_) RebasableAndNonRebasableTokens(
l1TokenNonRebasable_,
l1TokenRebasable_,
l2TokenNonRebasable_,
l2TokenRebasable_
) {
L2_TOKEN_BRIDGE = l2TokenBridge_;
}
/// @inheritdoc IL1ERC20Bridge
function l2TokenBridge() external view returns (address) {
return L2_TOKEN_BRIDGE;
}
/// @inheritdoc IL1ERC20Bridge
function depositERC20(
address l1Token_,
address l2Token_,
uint256 amount_,
uint32 l2Gas_,
bytes calldata data_
)
external
whenDepositsEnabled
onlySupportedL1L2TokensPair(l1Token_, l2Token_)
{
if (Address.isContract(msg.sender)) {
revert ErrorSenderNotEOA();
}
bytes memory encodedDepositData = _encodeInputDepositData(data_);
_depositERC20To(l1Token_, l2Token_, msg.sender, msg.sender, amount_, l2Gas_, encodedDepositData);
emit ERC20DepositInitiated(l1Token_, l2Token_, msg.sender, msg.sender, amount_, encodedDepositData);
}
/// @inheritdoc IL1ERC20Bridge
function depositERC20To(
address l1Token_,
address l2Token_,
address to_,
uint256 amount_,
uint32 l2Gas_,
bytes calldata data_
)
external
whenDepositsEnabled
onlyNonZeroAccount(to_)
onlySupportedL1L2TokensPair(l1Token_, l2Token_)
{
bytes memory encodedDepositData = _encodeInputDepositData(data_);
_depositERC20To(l1Token_, l2Token_, msg.sender, to_, amount_, l2Gas_, encodedDepositData);
emit ERC20DepositInitiated(l1Token_, l2Token_, msg.sender, to_, amount_, encodedDepositData);
}
/// @inheritdoc IL1ERC20Bridge
function finalizeERC20Withdrawal(
address l1Token_,
address l2Token_,
address from_,
address to_,
uint256 amount_,
bytes calldata data_
)
external
whenWithdrawalsEnabled
onlyFromCrossDomainAccount(L2_TOKEN_BRIDGE)
onlySupportedL1L2TokensPair(l1Token_, l2Token_)
{
uint256 withdrawnL1TokenAmount = (l1Token_ == L1_TOKEN_REBASABLE && amount_ != 0) ?
IERC20Wrapper(L1_TOKEN_NON_REBASABLE).unwrap(amount_) :
amount_;
IERC20(l1Token_).safeTransfer(to_, withdrawnL1TokenAmount);
emit ERC20WithdrawalFinalized(l1Token_, l2Token_, from_, to_, withdrawnL1TokenAmount, data_);
}
/// @notice Performs the logic for deposits by informing the L2 token bridge contract
/// of the deposit and calling safeTransferFrom to lock the L1 funds.
/// @param l1Token_ Address of the L1 ERC20 we are depositing
/// @param l2Token_ Address of the L1 respective L2 ERC20
/// @param from_ Account to pull the deposit from on L1
/// @param to_ Account to give the deposit to on L2
/// @param amount_ Amount of the ERC20 to deposit.
/// @param l2Gas_ Gas limit required to complete the deposit on L2.
/// @param encodedDepositData_ a concatenation of packed token rate with L1 time and
/// optional data passed by external contract
function _depositERC20To(
address l1Token_,
address l2Token_,
address from_,
address to_,
uint256 amount_,
uint32 l2Gas_,
bytes memory encodedDepositData_
) internal {
uint256 nonRebaseableAmountToDeposit = _transferToBridge(l1Token_, from_, amount_);
bytes memory message = abi.encodeWithSelector(
IL2ERC20Bridge.finalizeDeposit.selector,
l1Token_, l2Token_, from_, to_, nonRebaseableAmountToDeposit, encodedDepositData_
);
sendCrossDomainMessage(L2_TOKEN_BRIDGE, l2Gas_, message);
}
/// @notice Transfers tokens to the bridge and wraps if needed.
/// @param l1Token_ Address of the L1 ERC20 we are depositing.
/// @param from_ Account to pull the deposit from on L1.
/// @param amount_ Amount of the ERC20 to deposit.
/// @return Amount of non-rebasable token.
function _transferToBridge(
address l1Token_,
address from_,
uint256 amount_
) internal returns (uint256) {
if (amount_ != 0) {
IERC20(l1Token_).safeTransferFrom(from_, address(this), amount_);
if(l1Token_ == L1_TOKEN_REBASABLE) {
IERC20(l1Token_).safeIncreaseAllowance(L1_TOKEN_NON_REBASABLE, amount_);
return IERC20Wrapper(L1_TOKEN_NON_REBASABLE).wrap(amount_);
}
}
return amount_;
}
function _encodeInputDepositData(bytes calldata data_) internal view returns (bytes memory) {
return DepositDataCodec.encodeDepositData(DepositDataCodec.DepositData({
rate: uint96(_tokenRate()),
timestamp: uint40(block.timestamp),
data: data_
}));
}
/// @notice required to abstact a way token rate is requested.
function _tokenRate() virtual internal view returns (uint256);
error ErrorSenderNotEOA();
}