-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathL1GatewayRouter.sol
326 lines (292 loc) · 11.9 KB
/
L1GatewayRouter.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
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 2020, Offchain Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pragma solidity ^0.6.11;
import "arb-bridge-eth/contracts/libraries/Whitelist.sol";
import { ArbitrumEnabledToken } from "../ICustomToken.sol";
import "../L1ArbitrumMessenger.sol";
import "../../libraries/gateway/GatewayRouter.sol";
import "../../arbitrum/gateway/L2GatewayRouter.sol";
import "../../libraries/ERC165.sol";
import "./IL1GatewayRouter.sol";
import "./IL1ArbitrumGateway.sol";
/**
* @title Handles deposits from Erhereum into Arbitrum. Tokens are routered to their appropriate L1 gateway (Router itself also conforms to the Gateway itnerface).
* @notice Router also serves as an L1-L2 token address oracle.
* @notice DEPRECATED - see new repo(https://github.com/OffchainLabs/token-bridge-contracts) for new updates
*/
contract L1GatewayRouter is
WhitelistConsumer,
L1ArbitrumMessenger,
GatewayRouter,
ERC165,
IL1GatewayRouter
{
address public override owner;
address public override inbox;
modifier onlyOwner() {
require(msg.sender == owner, "ONLY_OWNER");
_;
}
function initialize(
address _owner,
address _defaultGateway,
address, // was _whitelist, now unused
address _counterpartGateway,
address _inbox
) public {
GatewayRouter._initialize(_counterpartGateway, address(0), _defaultGateway);
owner = _owner;
WhitelistConsumer.whitelist = address(0);
inbox = _inbox;
}
function setDefaultGateway(
address newL1DefaultGateway,
uint256 _maxGas,
uint256 _gasPriceBid,
uint256 _maxSubmissionCost
) external payable onlyOwner returns (uint256) {
defaultGateway = newL1DefaultGateway;
emit DefaultGatewayUpdated(newL1DefaultGateway);
address l2NewDefaultGateway;
if (newL1DefaultGateway != address(0)) {
l2NewDefaultGateway = TokenGateway(newL1DefaultGateway).counterpartGateway();
}
bytes memory data = abi.encodeWithSelector(
L2GatewayRouter.setDefaultGateway.selector,
l2NewDefaultGateway
);
return
sendTxToL2(
inbox,
counterpartGateway,
msg.sender,
msg.value,
0,
L2GasParams({
_maxSubmissionCost: _maxSubmissionCost,
_maxGas: _maxGas,
_gasPriceBid: _gasPriceBid
}),
data
);
}
function setOwner(address newOwner) external onlyOwner {
require(newOwner != address(0), "INVALID_OWNER");
// set newOwner to address(1) to disable owner and keep `initialize` safe
owner = newOwner;
}
function _setGateways(
address[] memory _token,
address[] memory _gateway,
uint256 _maxGas,
uint256 _gasPriceBid,
uint256 _maxSubmissionCost,
address _creditBackAddress
) internal returns (uint256) {
require(_token.length == _gateway.length, "WRONG_LENGTH");
for (uint256 i = 0; i < _token.length; i++) {
l1TokenToGateway[_token[i]] = _gateway[i];
emit GatewaySet(_token[i], _gateway[i]);
// overwrite memory so the L2 router receives the L2 address of each gateway
if (_gateway[i] != address(0) && _gateway[i] != DISABLED) {
// if we are assigning a gateway to the token, the address oracle of the gateway
// must return something other than the 0 address
// this check helps avoid misconfiguring gateways
require(
TokenGateway(_gateway[i]).calculateL2TokenAddress(_token[i]) != address(0),
"TOKEN_NOT_HANDLED_BY_GATEWAY"
);
_gateway[i] = TokenGateway(_gateway[i]).counterpartGateway();
}
}
bytes memory data = abi.encodeWithSelector(
L2GatewayRouter.setGateway.selector,
_token,
_gateway
);
return
sendTxToL2(
inbox,
counterpartGateway,
_creditBackAddress,
msg.value,
0,
L2GasParams({
_maxSubmissionCost: _maxSubmissionCost,
_maxGas: _maxGas,
_gasPriceBid: _gasPriceBid
}),
data
);
}
/**
* @notice Allows L1 Token contract to trustlessly register its gateway. (other setGateway method allows excess eth recovery from _maxSubmissionCost and is recommended)
* @param _gateway l1 gateway address
* @param _maxGas max gas for L2 retryable exrecution
* @param _gasPriceBid gas price for L2 retryable ticket
* @param _maxSubmissionCost base submission cost L2 retryable tick3et
* @return Retryable ticket ID
*/
function setGateway(
address _gateway,
uint256 _maxGas,
uint256 _gasPriceBid,
uint256 _maxSubmissionCost
) external payable override returns (uint256) {
return setGateway(_gateway, _maxGas, _gasPriceBid, _maxSubmissionCost, msg.sender);
}
/**
* @notice Allows L1 Token contract to trustlessly register its gateway.
* @param _gateway l1 gateway address
* @param _maxGas max gas for L2 retryable exrecution
* @param _gasPriceBid gas price for L2 retryable ticket
* @param _maxSubmissionCost base submission cost L2 retryable tick3et
* @param _creditBackAddress address for crediting back overpayment of _maxSubmissionCost
* @return Retryable ticket ID
*/
function setGateway(
address _gateway,
uint256 _maxGas,
uint256 _gasPriceBid,
uint256 _maxSubmissionCost,
address _creditBackAddress
) public payable override returns (uint256) {
require(
ArbitrumEnabledToken(msg.sender).isArbitrumEnabled() == uint8(0xa4b1),
"NOT_ARB_ENABLED"
);
require(_gateway.isContract(), "NOT_TO_CONTRACT");
address currGateway = getGateway(msg.sender);
if (currGateway != address(0) && currGateway != defaultGateway) {
// if gateway is already set to a non-default gateway, don't allow it to set a different gateway
require(currGateway == _gateway, "NO_UPDATE_TO_DIFFERENT_ADDR");
}
address[] memory _tokenArr = new address[](1);
_tokenArr[0] = address(msg.sender);
address[] memory _gatewayArr = new address[](1);
_gatewayArr[0] = _gateway;
return
_setGateways(
_tokenArr,
_gatewayArr,
_maxGas,
_gasPriceBid,
_maxSubmissionCost,
_creditBackAddress
);
}
function setGateways(
address[] memory _token,
address[] memory _gateway,
uint256 _maxGas,
uint256 _gasPriceBid,
uint256 _maxSubmissionCost
) external payable onlyOwner returns (uint256) {
// it is assumed that token and gateway are both contracts
// require(_token[i].isContract() && _gateway[i].isContract(), "NOT_CONTRACT");
return
_setGateways(_token, _gateway, _maxGas, _gasPriceBid, _maxSubmissionCost, msg.sender);
}
function _outboundTransferChecks(
uint256 _maxGas,
uint256 _gasPriceBid,
bytes calldata _data
) internal view {
// when sending a L1 to L2 transaction, we expect the user to send
// eth in flight in order to pay for L2 gas costs
// this check prevents users from misconfiguring the msg.value
// _data is (uint256, bytes) encoded, but we don't need the bytes
uint256 _maxSubmissionCost = abi.decode(_data, (uint256));
// here we don't use SafeMath since this validation is to prevent users
// from shooting themselves on the foot.
require(_maxSubmissionCost != 0, "NO_SUBMISSION_COST");
require(msg.value == _maxSubmissionCost + (_maxGas * _gasPriceBid), "WRONG_ETH_VALUE");
}
function outboundTransfer(
address _token,
address _to,
uint256 _amount,
uint256 _maxGas,
uint256 _gasPriceBid,
bytes calldata _data
) public payable override(GatewayRouter, ITokenGateway) returns (bytes memory) {
_outboundTransferChecks(_maxGas, _gasPriceBid, _data);
return super.outboundTransfer(_token, _to, _amount, _maxGas, _gasPriceBid, _data);
}
/**
* @notice Deposit ERC20 token from Ethereum into Arbitrum using the registered or otherwise default gateway
* @dev Some legacy gateway might not have the outboundTransferCustomRefund method and will revert, in such case use outboundTransfer instead
* L2 address alias will not be applied to the following types of addresses on L1:
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* @param _token L1 address of ERC20
* @param _refundTo Account, or its L2 alias if it have code in L1, to be credited with excess gas refund in L2
* @param _to Account to be credited with the tokens in the L2 (can be the user's L2 account or a contract), not subject to L2 aliasing
This account, or its L2 alias if it have code in L1, will also be able to cancel the retryable ticket and receive callvalue refund
* @param _amount Token Amount
* @param _maxGas Max gas deducted from user's L2 balance to cover L2 execution
* @param _gasPriceBid Gas price for L2 execution
* @param _data encoded data from router and user
* @return res abi encoded inbox sequence number
*/
function outboundTransferCustomRefund(
address _token,
address _refundTo,
address _to,
uint256 _amount,
uint256 _maxGas,
uint256 _gasPriceBid,
bytes calldata _data
) public payable override returns (bytes memory) {
address gateway = getGateway(_token);
bytes memory gatewayData = GatewayMessageHandler.encodeFromRouterToGateway(
msg.sender,
_data
);
emit TransferRouted(_token, msg.sender, _to, gateway);
// here we use `IL1ArbitrumGateway` since we don't assume all ITokenGateway implements `outboundTransferCustomRefund`
return
IL1ArbitrumGateway(gateway).outboundTransferCustomRefund{ value: msg.value }(
_token,
_refundTo,
_to,
_amount,
_maxGas,
_gasPriceBid,
gatewayData
);
}
modifier onlyCounterpartGateway() override {
// don't expect messages from L2 router
revert("ONLY_COUNTERPART_GATEWAY");
_;
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC165, IERC165)
returns (bool)
{
// registering interfaces that is added after arb-bridge-peripherals >1.0.11
// using function selector instead of single function interfaces to reduce bloat
return
interfaceId == this.outboundTransferCustomRefund.selector ||
super.supportsInterface(interfaceId);
}
}