-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBundleCollateralWrapper.spec.ts
457 lines (366 loc) · 18.6 KB
/
BundleCollateralWrapper.spec.ts
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
import { expect } from "chai";
import { ethers, network } from "hardhat";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/dist/src/signer-with-address";
import { TestERC20, TestERC721, BundleCollateralWrapper } from "../../typechain";
import { extractEvent, expectEvent } from "../helpers/EventUtilities";
import { BigNumber } from "ethers";
describe("BundleCollateralWrapper", function () {
let accounts: SignerWithAddress[];
let tok1: TestERC20;
let nft1: TestERC721;
let nft2: TestERC721;
let bundleCollateralWrapper: BundleCollateralWrapper;
let accountBorrower: SignerWithAddress;
let snapshotId: string;
before("deploy fixture", async () => {
accounts = await ethers.getSigners();
const testERC20Factory = await ethers.getContractFactory("TestERC20");
const testERC721Factory = await ethers.getContractFactory("TestERC721");
const bundleCollateralWrapperFactory = await ethers.getContractFactory("BundleCollateralWrapper");
tok1 = (await testERC20Factory.deploy("Token 1", "TOK1", 18, ethers.parseEther("1000"))) as TestERC20;
await tok1.waitForDeployment();
nft1 = (await testERC721Factory.deploy("NFT 1", "NFT1", "https://nft1.com/token/")) as TestERC721;
await nft1.waitForDeployment();
nft2 = (await testERC721Factory.deploy("NFT 2", "NFT2", "https://nft2.com/token/")) as TestERC721;
await nft2.waitForDeployment();
bundleCollateralWrapper = (await bundleCollateralWrapperFactory.deploy()) as BundleCollateralWrapper;
await bundleCollateralWrapper.waitForDeployment();
accountBorrower = accounts[1];
/* Mint NFTs to borrower */
await nft1.mint(await accountBorrower.getAddress(), 123);
await nft1.mint(await accountBorrower.getAddress(), 456);
await nft1.mint(await accountBorrower.getAddress(), 768);
await nft2.mint(await accountBorrower.getAddress(), 111);
await nft2.mint(await accountBorrower.getAddress(), 222);
for (let i = 223; i < 223 + 16; i++) {
await nft2.mint(await accountBorrower.getAddress(), i);
}
/* Approve bundle token to transfer NFTs */
await nft1.connect(accountBorrower).setApprovalForAll(await bundleCollateralWrapper.getAddress(), true);
await nft2.connect(accountBorrower).setApprovalForAll(await bundleCollateralWrapper.getAddress(), true);
});
beforeEach("snapshot blockchain", async () => {
snapshotId = await network.provider.send("evm_snapshot", []);
});
afterEach("restore blockchain snapshot", async () => {
await network.provider.send("evm_revert", [snapshotId]);
});
/****************************************************************************/
/* Constants */
/****************************************************************************/
describe("constants", async function () {
it("matches expected implementation version", async function () {
expect(await bundleCollateralWrapper.IMPLEMENTATION_VERSION()).to.equal("2.2");
});
it("returns correct name", async function () {
expect(await bundleCollateralWrapper.name()).to.equal("MetaStreet Bundle Collateral Wrapper");
});
it("returns correct symbol", async function () {
expect(await bundleCollateralWrapper.symbol()).to.equal("MSBCW");
});
});
/****************************************************************************/
/* Primary API */
/****************************************************************************/
describe("#enumerate", async function () {
it("enumerate bundle", async function () {
/* Mint bundle */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
/* Enumerate */
const [token, tokenIds] = await bundleCollateralWrapper.enumerate(tokenId1, context);
/* Validate return */
expect(token).to.equal(await nft1.getAddress());
expect(tokenIds[0]).to.equal(123);
expect(tokenIds[1]).to.equal(456);
expect(tokenIds[2]).to.equal(768);
});
it("fails on incorrect tokenId", async function () {
/* Mint bundle */
await bundleCollateralWrapper.connect(accountBorrower).mint(await nft1.getAddress(), [123, 456, 768]);
/* Use different token id */
const badTokenId = BigInt("80530570786821071483259871300278421257638987008682429097249700923201294947214");
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
await expect(bundleCollateralWrapper.enumerate(badTokenId, context)).to.be.revertedWithCustomError(
bundleCollateralWrapper,
"InvalidContext"
);
});
});
describe("#enumerateWithQuantities", async function () {
it("enumerate bundle", async function () {
/* Mint bundle */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
/* Enumerate */
const [token, tokenIds, quantities] = await bundleCollateralWrapper.enumerateWithQuantities(tokenId1, context);
/* Validate return */
expect(token).to.equal(await nft1.getAddress());
expect(tokenIds[0]).to.equal(123);
expect(tokenIds[1]).to.equal(456);
expect(tokenIds[2]).to.equal(768);
expect(quantities[0]).to.equal(1);
expect(quantities[1]).to.equal(1);
expect(quantities[2]).to.equal(1);
});
});
describe("#count", async function () {
it("count bundle", async function () {
/* Mint bundle */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
/* Enumerate */
const count = await bundleCollateralWrapper.count(tokenId1, context);
/* Validate return */
expect(count).to.equal(3);
});
it("fails on incorrect tokenId", async function () {
/* Mint bundle */
await bundleCollateralWrapper.connect(accountBorrower).mint(await nft1.getAddress(), [123, 456, 768]);
/* Use different token id */
const badTokenId = BigInt("80530570786821071483259871300278421257638987008682429097249700923201294947214");
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
await expect(bundleCollateralWrapper.count(badTokenId, context)).to.be.revertedWithCustomError(
bundleCollateralWrapper,
"InvalidContext"
);
});
});
describe("#transferCalldata", async function () {
it("transfer calldata", async function () {
/* Get transferCalldata */
const [target, calldata] = await bundleCollateralWrapper.transferCalldata(
await nft1.getAddress(),
await accountBorrower.getAddress(),
accounts[0].address,
123,
0
);
const tx = {
to: target,
data: calldata,
};
await accountBorrower.sendTransaction(tx);
/* Validate balance */
const balance = await nft1.balanceOf(accounts[0].address);
expect(balance).to.equal(1);
});
});
describe("#mint", async function () {
it("mints bundle", async function () {
/* Mint two bundles */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
const mintTx2 = await bundleCollateralWrapper.connect(accountBorrower).mint(await nft2.getAddress(), [111, 222]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
const tokenId2 = (await extractEvent(mintTx2, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Validate events */
await expectEvent(mintTx1, bundleCollateralWrapper, "Transfer", {
from: ethers.ZeroAddress,
to: await accountBorrower.getAddress(),
tokenId: tokenId1,
});
await expectEvent(mintTx1, bundleCollateralWrapper, "BundleMinted", {
tokenId: tokenId1,
encodedBundle: ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]),
account: await accountBorrower.getAddress(),
});
await expectEvent(mintTx2, bundleCollateralWrapper, "Transfer", {
from: ethers.ZeroAddress,
to: await accountBorrower.getAddress(),
tokenId: tokenId2,
});
await expectEvent(mintTx2, bundleCollateralWrapper, "BundleMinted", {
tokenId: tokenId2,
encodedBundle: ethers.solidityPacked(["address", "uint256[]"], [await nft2.getAddress(), [111, 222]]),
account: await accountBorrower.getAddress(),
});
/* Validate state */
expect(await bundleCollateralWrapper.exists(tokenId1)).to.equal(true);
expect(await bundleCollateralWrapper.exists(tokenId2)).to.equal(true);
expect(await bundleCollateralWrapper.ownerOf(tokenId1)).to.equal(await accountBorrower.getAddress());
expect(await bundleCollateralWrapper.ownerOf(tokenId2)).to.equal(await accountBorrower.getAddress());
expect(await nft1.ownerOf(123)).to.equal(await bundleCollateralWrapper.getAddress());
expect(await nft1.ownerOf(456)).to.equal(await bundleCollateralWrapper.getAddress());
expect(await nft1.ownerOf(768)).to.equal(await bundleCollateralWrapper.getAddress());
});
it("can transfer BundleCollateralWrapperToken", async function () {
/* Mint bundle */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Validate owner */
expect(await bundleCollateralWrapper.ownerOf(tokenId1)).to.equal(await accountBorrower.getAddress());
/* Transfer token */
await bundleCollateralWrapper
.connect(accountBorrower)
.transferFrom(await accountBorrower.getAddress(), accounts[2].address, tokenId1);
/* Validate owner */
expect(await bundleCollateralWrapper.ownerOf(tokenId1)).to.equal(accounts[2].address);
});
it("fails on non-existent nft", async function () {
await expect(
bundleCollateralWrapper.connect(accountBorrower).mint(await nft1.getAddress(), [123, 456, 3])
).to.be.revertedWith("ERC721: invalid token ID");
});
it("fails on empty list of token ids", async function () {
await expect(
bundleCollateralWrapper.connect(accountBorrower).mint(await nft1.getAddress(), [])
).to.be.revertedWithCustomError(bundleCollateralWrapper, "InvalidSize");
});
it("fails on more than 16 token ids", async function () {
await expect(
bundleCollateralWrapper.connect(accountBorrower).mint(
await nft1.getAddress(),
Array.from({ length: 33 }, (_, n) => n + 222) // 222 to 255 (33 token ids in total)
)
).to.be.revertedWithCustomError(bundleCollateralWrapper, "InvalidSize");
});
});
describe("#unwrap", async function () {
it("unwrap bundle", async function () {
/* Mint bundle */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
/* Validate current owner */
expect(await bundleCollateralWrapper.ownerOf(tokenId1)).to.equal(await accountBorrower.getAddress());
/* Unwrap and validate events */
await expect(bundleCollateralWrapper.connect(accountBorrower).unwrap(tokenId1, context))
.to.emit(nft1, "Transfer")
.withArgs(await bundleCollateralWrapper.getAddress(), await accountBorrower.getAddress(), 123)
.to.emit(nft1, "Transfer")
.withArgs(await bundleCollateralWrapper.getAddress(), await accountBorrower.getAddress(), 456)
.to.emit(nft1, "Transfer")
.withArgs(await bundleCollateralWrapper.getAddress(), await accountBorrower.getAddress(), 768)
.to.emit(bundleCollateralWrapper, "Transfer")
.withArgs(await accountBorrower.getAddress(), ethers.ZeroAddress, tokenId1)
.to.emit(bundleCollateralWrapper, "BundleUnwrapped")
.withArgs(tokenId1, await accountBorrower.getAddress());
expect(await bundleCollateralWrapper.exists(tokenId1)).to.equal(false);
expect(await nft1.ownerOf(123)).to.equal(await accountBorrower.getAddress());
expect(await nft1.ownerOf(456)).to.equal(await accountBorrower.getAddress());
expect(await nft1.ownerOf(768)).to.equal(await accountBorrower.getAddress());
});
it("only token holder can unwrap bundle", async function () {
/* Mint bundle */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
/* Validate current owner */
expect(await bundleCollateralWrapper.ownerOf(tokenId1)).to.equal(await accountBorrower.getAddress());
/* Attempt to unwrap */
await expect(
bundleCollateralWrapper.connect(accounts[2]).unwrap(tokenId1, context)
).to.be.revertedWithCustomError(bundleCollateralWrapper, "InvalidCaller");
await expect(bundleCollateralWrapper.unwrap(tokenId1, context)).to.be.revertedWithCustomError(
bundleCollateralWrapper,
"InvalidCaller"
);
});
it("fails on incorrect tokenId", async function () {
/* Mint bundle */
await bundleCollateralWrapper.connect(accountBorrower).mint(await nft1.getAddress(), [123, 456, 768]);
/* Use bad token id */
const badTokenId = BigInt("80530570786821071483259871300278421257638987008682429097249700923201294947214");
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
/* Attempt to unwrap */
await expect(
bundleCollateralWrapper.connect(accountBorrower).unwrap(badTokenId, context)
).to.be.revertedWithCustomError(bundleCollateralWrapper, "InvalidContext");
});
it("transferee can unwrap bundle", async function () {
/* Mint bundle */
const mintTx1 = await bundleCollateralWrapper
.connect(accountBorrower)
.mint(await nft1.getAddress(), [123, 456, 768]);
/* Get token id */
const tokenId1 = (await extractEvent(mintTx1, bundleCollateralWrapper, "BundleMinted")).args.tokenId;
/* Validate owner */
expect(await bundleCollateralWrapper.ownerOf(tokenId1)).to.equal(await accountBorrower.getAddress());
/* Transfer token */
await bundleCollateralWrapper
.connect(accountBorrower)
.transferFrom(await accountBorrower.getAddress(), accounts[2].address, tokenId1);
/* Validate owner */
expect(await bundleCollateralWrapper.ownerOf(tokenId1)).to.equal(accounts[2].address);
/* Create context */
const context = ethers.solidityPacked(["address", "uint256[]"], [await nft1.getAddress(), [123, 456, 768]]);
/* Unwrap and validate events */
await expect(bundleCollateralWrapper.connect(accounts[2]).unwrap(tokenId1, context))
.to.emit(nft1, "Transfer")
.withArgs(await bundleCollateralWrapper.getAddress(), accounts[2].address, 123)
.to.emit(nft1, "Transfer")
.withArgs(await bundleCollateralWrapper.getAddress(), accounts[2].address, 456)
.to.emit(nft1, "Transfer")
.withArgs(await bundleCollateralWrapper.getAddress(), accounts[2].address, 768)
.to.emit(bundleCollateralWrapper, "Transfer")
.withArgs(accounts[2].address, ethers.ZeroAddress, tokenId1)
.to.emit(bundleCollateralWrapper, "BundleUnwrapped")
.withArgs(tokenId1, accounts[2].address);
expect(await bundleCollateralWrapper.exists(tokenId1)).to.equal(false);
expect(await nft1.ownerOf(123)).to.equal(accounts[2].address);
expect(await nft1.ownerOf(456)).to.equal(accounts[2].address);
expect(await nft1.ownerOf(768)).to.equal(accounts[2].address);
});
});
/****************************************************************************/
/* ERC165 Interface */
/****************************************************************************/
describe("#supportsInterface", async function () {
it("returns true on supported interfaces", async function () {
/* ERC165 */
expect(
await bundleCollateralWrapper.supportsInterface(ethers.id("supportsInterface(bytes4)").substring(0, 10))
).to.equal(true);
/* ICollateralWrapper */
expect(
await bundleCollateralWrapper.supportsInterface(
ethers.toBeHex(
BigInt(ethers.id("name()").substring(0, 10)) ^
BigInt(ethers.id("unwrap(uint256,bytes)").substring(0, 10)) ^
BigInt(ethers.id("enumerate(uint256,bytes)").substring(0, 10)) ^
BigInt(ethers.id("count(uint256,bytes)").substring(0, 10)) ^
BigInt(ethers.id("enumerateWithQuantities(uint256,bytes)").substring(0, 10)) ^
BigInt(ethers.id("transferCalldata(address,address,address,uint256,uint256)").substring(0, 10))
)
)
).to.equal(true);
it("returns false on unsupported interfaces", async function () {
expect(await bundleCollateralWrapper.supportsInterface("0xaabbccdd")).to.equal(false);
expect(await bundleCollateralWrapper.supportsInterface("0x00000000")).to.equal(false);
expect(await bundleCollateralWrapper.supportsInterface("0xffffffff")).to.equal(false);
});
});
});
});