forked from scroll-tech/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L2ERC721Gateway.t.sol
343 lines (292 loc) · 11.8 KB
/
L2ERC721Gateway.t.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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {DSTestPlus} from "solmate/test/utils/DSTestPlus.sol";
import {MockERC721} from "solmate/test/utils/mocks/MockERC721.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {L1ERC721Gateway} from "../L1/gateways/L1ERC721Gateway.sol";
import {L2ERC721Gateway} from "../L2/gateways/L2ERC721Gateway.sol";
import {MockScrollMessenger} from "./mocks/MockScrollMessenger.sol";
import {MockERC721Recipient} from "./mocks/MockERC721Recipient.sol";
contract L2ERC721GatewayTest is DSTestPlus {
/**********
* Errors *
**********/
// from IScrollGateway
error ErrorZeroAddress();
error ErrorCallerIsNotMessenger();
error ErrorCallerIsNotCounterpartGateway();
error ErrorNotInDropMessageContext();
uint256 private constant TOKEN_COUNT = 100;
uint256 private constant NOT_OWNED_TOKEN_ID = 233333;
MockScrollMessenger private messenger;
L1ERC721Gateway private counterpart;
L2ERC721Gateway private gateway;
MockERC721 private token;
MockERC721Recipient private mockRecipient;
function setUp() public {
messenger = new MockScrollMessenger();
counterpart = new L1ERC721Gateway(address(1), address(1));
gateway = _deployGateway(address(messenger));
gateway.initialize(address(counterpart), address(messenger));
token = new MockERC721("Mock", "M");
for (uint256 i = 0; i < TOKEN_COUNT; i++) {
token.mint(address(this), i);
}
mockRecipient = new MockERC721Recipient();
token.mint(address(mockRecipient), NOT_OWNED_TOKEN_ID);
}
function testReinitilize() public {
hevm.expectRevert("Initializable: contract is already initialized");
gateway.initialize(address(1), address(messenger));
}
function testUpdateTokenMappingFailed(address token1) public {
// call by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("Ownable: caller is not the owner");
gateway.updateTokenMapping(token1, token1);
hevm.stopPrank();
// l1 token is zero, should revert
hevm.expectRevert("token address cannot be 0");
gateway.updateTokenMapping(token1, address(0));
}
function testUpdateTokenMappingSuccess(address token1, address token2) public {
if (token2 == address(0)) token2 = address(1);
assertEq(gateway.tokenMapping(token1), address(0));
gateway.updateTokenMapping(token1, token2);
assertEq(gateway.tokenMapping(token1), token2);
}
/// @dev failed to withdraw erc721
function testWithdrawERC721WithGatewayFailed(address to) public {
// token not support
hevm.expectRevert("no corresponding l1 token");
if (to == address(0)) {
gateway.withdrawERC721(address(token), 0, 0);
} else {
gateway.withdrawERC721(address(token), to, 0, 0);
}
// token not owned
gateway.updateTokenMapping(address(token), address(token));
hevm.expectRevert("token not owned");
if (to == address(0)) {
gateway.withdrawERC721(address(token), NOT_OWNED_TOKEN_ID, 0);
} else {
gateway.withdrawERC721(address(token), to, NOT_OWNED_TOKEN_ID, 0);
}
}
/// @dev withdraw erc721 without recipient
function testWithdrawERC721WithGatewaySuccess(uint256 tokenId) public {
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
gateway.updateTokenMapping(address(token), address(token));
gateway.withdrawERC721(address(token), tokenId, 0);
hevm.expectRevert("NOT_MINTED");
token.ownerOf(tokenId);
assertEq(token.balanceOf(address(this)), TOKEN_COUNT - 1);
// @todo check event
}
/// @dev withdraw erc721 with recipient
function testWithdrawERC721WithGatewaySuccess(uint256 tokenId, address to) public {
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
gateway.updateTokenMapping(address(token), address(token));
gateway.withdrawERC721(address(token), to, tokenId, 0);
hevm.expectRevert("NOT_MINTED");
token.ownerOf(tokenId);
assertEq(token.balanceOf(address(this)), TOKEN_COUNT - 1);
// @todo check event
}
/// @dev failed to batch withdraw erc721
function testBatchWithdrawERC721WithGatewayFailed(address to) public {
// token not support
hevm.expectRevert("no corresponding l1 token");
if (to == address(0)) {
gateway.batchWithdrawERC721(address(token), new uint256[](1), 0);
} else {
gateway.batchWithdrawERC721(address(token), to, new uint256[](1), 0);
}
// no token to withdraw
hevm.expectRevert("no token to withdraw");
if (to == address(0)) {
gateway.batchWithdrawERC721(address(token), new uint256[](0), 0);
} else {
gateway.batchWithdrawERC721(address(token), to, new uint256[](0), 0);
}
// token not owned
gateway.updateTokenMapping(address(token), address(token));
uint256[] memory tokenIds = new uint256[](1);
tokenIds[0] = NOT_OWNED_TOKEN_ID;
hevm.expectRevert("token not owned");
if (to == address(0)) {
gateway.batchWithdrawERC721(address(token), tokenIds, 0);
} else {
gateway.batchWithdrawERC721(address(token), to, tokenIds, 0);
}
}
/// @dev batch withdraw erc721 without recipient
function testBatchWithdrawERC721WithGatewaySuccess(uint256 count) public {
count = bound(count, 1, TOKEN_COUNT);
gateway.updateTokenMapping(address(token), address(token));
uint256[] memory _tokenIds = new uint256[](count);
for (uint256 i = 0; i < count; i++) {
_tokenIds[i] = i;
}
gateway.batchWithdrawERC721(address(token), _tokenIds, 0);
for (uint256 i = 0; i < count; i++) {
hevm.expectRevert("NOT_MINTED");
token.ownerOf(i);
}
assertEq(token.balanceOf(address(this)), TOKEN_COUNT - count);
// @todo check event
}
/// @dev batch withdraw erc721 with recipient
function testBatchWithdrawERC721WithGatewaySuccess(uint256 count, address to) public {
count = bound(count, 1, TOKEN_COUNT);
gateway.updateTokenMapping(address(token), address(token));
uint256[] memory _tokenIds = new uint256[](count);
for (uint256 i = 0; i < count; i++) {
_tokenIds[i] = i;
}
gateway.batchWithdrawERC721(address(token), to, _tokenIds, 0);
for (uint256 i = 0; i < count; i++) {
hevm.expectRevert("NOT_MINTED");
token.ownerOf(i);
}
assertEq(token.balanceOf(address(this)), TOKEN_COUNT - count);
// @todo check event
}
/// @dev failed to finalize withdraw erc721
function testFinalizeDepositERC721Failed() public {
// should revert, called by non-messenger
hevm.expectRevert(ErrorCallerIsNotMessenger.selector);
gateway.finalizeDepositERC721(address(0), address(0), address(0), address(0), 0);
// should revert, called by messenger, xDomainMessageSender not set
hevm.expectRevert(ErrorCallerIsNotCounterpartGateway.selector);
messenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L2ERC721Gateway.finalizeDepositERC721.selector,
address(0),
address(0),
address(0),
address(0),
0
)
);
// should revert, called by messenger, xDomainMessageSender set wrong
messenger.setXDomainMessageSender(address(2));
hevm.expectRevert(ErrorCallerIsNotCounterpartGateway.selector);
messenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L2ERC721Gateway.finalizeDepositERC721.selector,
address(0),
address(0),
address(0),
address(0),
0
)
);
}
/// @dev finalize withdraw erc721
function testFinalizeDepositERC721(
address from,
address to,
uint256 tokenId
) public {
hevm.assume(to != address(0));
hevm.assume(to.code.length == 0);
tokenId = bound(tokenId, NOT_OWNED_TOKEN_ID + 1, type(uint256).max);
gateway.updateTokenMapping(address(token), address(token));
// finalize deposit
messenger.setXDomainMessageSender(address(counterpart));
messenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L2ERC721Gateway.finalizeDepositERC721.selector,
address(token),
address(token),
from,
to,
tokenId
)
);
assertEq(token.balanceOf(to), 1);
assertEq(token.ownerOf(tokenId), to);
}
/// @dev failed to finalize batch withdraw erc721
function testFinalizeBatchDepositERC721Failed() public {
// should revert, called by non-messenger
hevm.expectRevert(ErrorCallerIsNotMessenger.selector);
gateway.finalizeBatchDepositERC721(address(0), address(0), address(0), address(0), new uint256[](0));
// should revert, called by messenger, xDomainMessageSender not set
hevm.expectRevert(ErrorCallerIsNotCounterpartGateway.selector);
messenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L2ERC721Gateway.finalizeBatchDepositERC721.selector,
address(0),
address(0),
address(0),
address(0),
new uint256[](0)
)
);
// should revert, called by messenger, xDomainMessageSender set wrong
messenger.setXDomainMessageSender(address(2));
hevm.expectRevert(ErrorCallerIsNotCounterpartGateway.selector);
messenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L2ERC721Gateway.finalizeBatchDepositERC721.selector,
address(0),
address(0),
address(0),
address(0),
new uint256[](0)
)
);
}
/// @dev finalize batch withdraw erc721
function testFinalizeBatchDepositERC721(
address from,
address to,
uint256 count
) public {
if (to == address(0)) to = address(1);
if (to == address(mockRecipient)) to = address(1);
if (to == address(this)) to = address(1);
gateway.updateTokenMapping(address(token), address(token));
// deposit first
count = bound(count, 1, TOKEN_COUNT);
uint256[] memory _tokenIds = new uint256[](count);
for (uint256 i = 0; i < count; i++) {
_tokenIds[i] = i + NOT_OWNED_TOKEN_ID + 1;
}
// then withdraw
messenger.setXDomainMessageSender(address(counterpart));
messenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L2ERC721Gateway.finalizeBatchDepositERC721.selector,
address(token),
address(token),
from,
to,
_tokenIds
)
);
assertEq(token.balanceOf(to), count);
for (uint256 i = 0; i < count; i++) {
assertEq(token.ownerOf(_tokenIds[i]), to);
}
}
function _deployGateway(address _messenger) internal returns (L2ERC721Gateway) {
return
L2ERC721Gateway(
address(
new ERC1967Proxy(
address(new L2ERC721Gateway(address(counterpart), address(_messenger))),
new bytes(0)
)
)
);
}
}