-
Notifications
You must be signed in to change notification settings - Fork 403
/
HypERC721.t.sol
426 lines (362 loc) · 13.8 KB
/
HypERC721.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
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
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.13;
/*@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ HYPERLANE @@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/
import "forge-std/Test.sol";
import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {ERC721URIStorageUpgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import {TestMailbox} from "../../contracts/test/TestMailbox.sol";
import {TestPostDispatchHook} from "../../contracts/test/TestPostDispatchHook.sol";
import {TypeCasts} from "../../contracts/libs/TypeCasts.sol";
import {ERC721Test} from "../../contracts/test/ERC721Test.sol";
import {TokenRouter} from "../../contracts/token/libs/TokenRouter.sol";
import {HypERC721} from "../../contracts/token/HypERC721.sol";
import {HypERC721Collateral} from "../../contracts/token/HypERC721Collateral.sol";
import {HypERC721URIStorage} from "../../contracts/token/extensions/HypERC721URIStorage.sol";
import {HypERC721URICollateral} from "../../contracts/token/extensions/HypERC721URICollateral.sol";
abstract contract HypTokenTest is Test, IERC721Receiver {
using TypeCasts for address;
uint256 internal constant INITIAL_SUPPLY = 10;
string internal constant NAME = "Hyperlane Hedgehogs";
string internal constant SYMBOL = "HHH";
address internal constant ALICE = address(0x1);
address internal constant BOB = address(0x2);
address internal constant PROXY_ADMIN = address(0x37);
uint32 internal constant ORIGIN = 11;
uint32 internal constant DESTINATION = 22;
uint256 internal constant TRANSFER_ID = 0;
string internal constant URI = "http://bit.ly/3reJLpx";
ERC721Test internal localPrimaryToken =
new ERC721Test(NAME, SYMBOL, INITIAL_SUPPLY * 2);
ERC721Test internal remotePrimaryToken =
new ERC721Test(NAME, SYMBOL, INITIAL_SUPPLY * 2);
TestMailbox internal localMailbox;
TestMailbox internal remoteMailbox;
TokenRouter internal localToken;
TokenRouter internal remoteToken;
TestPostDispatchHook internal noopHook;
event Dispatch(
address indexed sender,
uint32 indexed destination,
bytes32 indexed recipient,
bytes message
);
function setUp() public virtual {
noopHook = new TestPostDispatchHook();
localMailbox = new TestMailbox(ORIGIN);
localMailbox.setDefaultHook(address(noopHook));
localMailbox.setRequiredHook(address(noopHook));
remoteMailbox = new TestMailbox(DESTINATION);
remoteToken = new HypERC721Collateral(
address(remotePrimaryToken),
address(remoteMailbox)
);
}
function _deployRemoteToken(bool isCollateral) internal {
if (isCollateral) {
HypERC721Collateral implementation = new HypERC721Collateral(
address(remotePrimaryToken),
address(remoteMailbox)
);
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721Collateral.initialize.selector,
address(0),
address(0),
address(this)
)
);
remoteToken = HypERC721Collateral(address(proxy));
remotePrimaryToken.transferFrom(
address(this),
address(remoteToken),
0
); // need for processing messages
} else {
HypERC721 implementation = new HypERC721(address(remoteMailbox));
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721.initialize.selector,
0,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
)
);
remoteToken = TokenRouter(address(proxy));
}
remoteToken.enrollRemoteRouter(
ORIGIN,
address(localToken).addressToBytes32()
);
}
function _processTransfers(address _recipient, uint256 _tokenId) internal {
vm.prank(address(remoteMailbox));
remoteToken.handle(
ORIGIN,
address(localToken).addressToBytes32(),
abi.encodePacked(_recipient.addressToBytes32(), _tokenId)
);
}
function _performRemoteTransfer(
uint256 _msgValue,
uint256 _tokenId
) public {
localToken.transferRemote{value: _msgValue}(
DESTINATION,
ALICE.addressToBytes32(),
_tokenId
);
_processTransfers(BOB, _tokenId);
assertEq(remoteToken.balanceOf(BOB), 1);
}
function testBenchmark_overheadGasUsage() public {
vm.prank(address(localMailbox));
uint256 gasBefore = gasleft();
localToken.handle(
DESTINATION,
address(remoteToken).addressToBytes32(),
abi.encodePacked(BOB.addressToBytes32(), INITIAL_SUPPLY + 1)
);
uint256 gasAfter = gasleft();
console.log("Overhead gas usage: %d", gasBefore - gasAfter);
}
function onERC721Received(
address /*operator*/,
address /*from*/,
uint256 /*tokenId*/,
bytes calldata /*data*/
) external pure returns (bytes4) {
return IERC721Receiver.onERC721Received.selector;
}
}
contract HypERC721Test is HypTokenTest {
using TypeCasts for address;
HypERC721 internal hyp721;
function setUp() public virtual override {
super.setUp();
HypERC721 implementation = new HypERC721(address(localMailbox));
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721.initialize.selector,
INITIAL_SUPPLY,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
)
);
localToken = HypERC721(address(proxy));
hyp721 = HypERC721(address(proxy));
hyp721.enrollRemoteRouter(
DESTINATION,
address(remoteToken).addressToBytes32()
);
}
function testInitialize_revert_ifAlreadyInitialized() public {
vm.expectRevert("Initializable: contract is already initialized");
hyp721.initialize(
INITIAL_SUPPLY,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
);
}
function testTotalSupply() public {
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY);
}
function testOwnerOf() public {
assertEq(hyp721.ownerOf(0), address(this));
}
function testLocalTransfer() public {
hyp721.transferFrom(address(this), ALICE, 0);
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY - 1);
assertEq(hyp721.balanceOf(ALICE), 1);
}
function testLocalYTransfer_revert_invalidTokenId() public {
vm.expectRevert("ERC721: invalid token ID");
hyp721.transferFrom(address(this), ALICE, INITIAL_SUPPLY);
}
function testRemoteTransfer(bool isCollateral) public {
_deployRemoteToken(isCollateral);
_performRemoteTransfer(25000, 0);
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY - 1);
}
function testRemoteTransfer_revert_unowned() public {
hyp721.transferFrom(address(this), BOB, 1);
_deployRemoteToken(false);
vm.expectRevert("!owner");
_performRemoteTransfer(25000, 1);
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY - 1);
}
function testRemoteTransfer_revert_invalidTokenId() public {
_deployRemoteToken(false);
vm.expectRevert("ERC721: invalid token ID");
_performRemoteTransfer(25000, INITIAL_SUPPLY);
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY);
}
}
contract MockHypERC721URIStorage is HypERC721URIStorage {
constructor(address mailbox) HypERC721URIStorage(mailbox) {}
function setTokenURI(uint256 tokenId, string memory uri) public {
_setTokenURI(tokenId, uri);
}
}
contract HypERC721URIStorageTest is HypTokenTest {
using TypeCasts for address;
MockHypERC721URIStorage internal hyp721Storage;
function setUp() public override {
super.setUp();
localToken = new MockHypERC721URIStorage(address(localMailbox));
hyp721Storage = MockHypERC721URIStorage(address(localToken));
hyp721Storage.initialize(
INITIAL_SUPPLY,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
);
hyp721Storage.setTokenURI(0, URI);
hyp721Storage.enrollRemoteRouter(
DESTINATION,
address(remoteToken).addressToBytes32()
);
}
function testRemoteTransfers_revert_burned() public {
_deployRemoteToken(false);
_performRemoteTransfer(25000, 0);
assertEq(hyp721Storage.balanceOf(address(this)), INITIAL_SUPPLY - 1);
vm.expectRevert("ERC721: invalid token ID");
assertEq(hyp721Storage.tokenURI(0), "");
}
}
contract HypERC721CollateralTest is HypTokenTest {
using TypeCasts for address;
HypERC721Collateral internal hyp721Collateral;
function setUp() public override {
super.setUp();
HypERC721Collateral implementation = new HypERC721Collateral(
address(localPrimaryToken),
address(localMailbox)
);
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721Collateral.initialize.selector,
address(0),
address(0),
address(this)
)
);
localToken = HypERC721Collateral(address(proxy));
hyp721Collateral = HypERC721Collateral(address(localToken));
hyp721Collateral.enrollRemoteRouter(
DESTINATION,
address(remoteToken).addressToBytes32()
);
localPrimaryToken.transferFrom(
address(this),
address(hyp721Collateral),
INITIAL_SUPPLY + 1
);
}
function testInitialize_revert_ifAlreadyInitialized() public {}
function testRemoteTransfer(bool isCollateral) public {
localPrimaryToken.approve(address(hyp721Collateral), 0);
_deployRemoteToken(isCollateral);
_performRemoteTransfer(25000, 0);
assertEq(
hyp721Collateral.balanceOf(address(this)),
INITIAL_SUPPLY * 2 - 2
);
}
function testRemoteTransfer_revert_unowned() public {
localPrimaryToken.transferFrom(address(this), BOB, 1);
_deployRemoteToken(false);
vm.expectRevert("ERC721: caller is not token owner or approved");
_performRemoteTransfer(25000, 1);
assertEq(
hyp721Collateral.balanceOf(address(this)),
INITIAL_SUPPLY * 2 - 2
);
}
function testRemoteTransfer_revert_invalidTokenId() public {
_deployRemoteToken(false);
vm.expectRevert("ERC721: invalid token ID");
_performRemoteTransfer(25000, INITIAL_SUPPLY * 2);
assertEq(
hyp721Collateral.balanceOf(address(this)),
INITIAL_SUPPLY * 2 - 1
);
}
}
contract HypERC721CollateralURIStorageTest is HypTokenTest {
using TypeCasts for address;
HypERC721URICollateral internal hyp721URICollateral;
function setUp() public override {
super.setUp();
localToken = new HypERC721URICollateral(
address(localPrimaryToken),
address(localMailbox)
);
hyp721URICollateral = HypERC721URICollateral(address(localToken));
hyp721URICollateral.enrollRemoteRouter(
DESTINATION,
address(remoteToken).addressToBytes32()
);
// localPrimaryToken.setTokenURI(0, URI);
localPrimaryToken.transferFrom(
address(this),
address(hyp721URICollateral),
INITIAL_SUPPLY + 1
);
localPrimaryToken.ownerOf(0);
}
function testRemoteTransfers_revert_burned() public {
_deployRemoteToken(false);
localPrimaryToken.approve(address(hyp721URICollateral), 0);
bytes
memory message = hex"03000000000000000b0000000000000000000000001d1499e622d69689cdf9004d05ec547d650ff21100000016000000000000000000000000a0cb889707d426a7a386870a03bc70d1b069759800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000544553542d424153452d55524930";
vm.expectEmit(true, true, false, true);
emit Dispatch(
address(localToken),
DESTINATION,
address(remoteToken).addressToBytes32(),
message
);
localToken.transferRemote{value: 25000}(
DESTINATION,
BOB.addressToBytes32(),
TRANSFER_ID
);
_processTransfers(BOB, 0);
assertEq(remoteToken.balanceOf(BOB), 1);
assertEq(
hyp721URICollateral.balanceOf(address(this)),
INITIAL_SUPPLY * 2 - 2
);
}
}