-
Notifications
You must be signed in to change notification settings - Fork 17
/
EditionNFTContract.test.ts
383 lines (292 loc) · 12.2 KB
/
EditionNFTContract.test.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
import { EditionNFTContract, EditionResult, NFTMintResult } from './EditionNFTContract';
import { FreshmintClaimSaleContract } from './FreshmintClaimSaleContract';
import {
client,
contractHashAlgorithm,
contractPublicKey,
ownerAuthorizer,
getTestSchema,
setupEmulator,
teardownEmulator,
collectionMetadata,
} from '../testHelpers';
describe('EditionNFTContract', () => {
beforeAll(setupEmulator);
afterAll(teardownEmulator);
const contract = new EditionNFTContract({
name: 'EditionNFT_Test',
schema: getTestSchema(false),
owner: ownerAuthorizer,
});
it('should generate a contract', async () => {
expect(contract.getSource(client.config.imports)).toMatchSnapshot();
});
it('should deploy a contract', async () => {
await client.send(
contract.deploy({
publicKey: contractPublicKey,
hashAlgorithm: contractHashAlgorithm,
collectionMetadata,
}),
);
});
// TODO: refactor this test case into a separate test suite
it('should deploy a contract and save the admin resource to the contract account', async () => {
const contractWithAdmin = new EditionNFTContract({
name: 'EditionNFT_Test_ContractAdmin',
schema: getTestSchema(false),
owner: ownerAuthorizer,
});
await client.send(
contractWithAdmin.deploy({
publicKey: contractPublicKey,
hashAlgorithm: contractHashAlgorithm,
collectionMetadata,
saveAdminResourceToContractAccount: true,
}),
);
});
describe('Edition with limit and full mint', () => {
let edition: EditionResult;
const allMintedNFTs: NFTMintResult[] = [];
const editionInput = {
limit: 5,
metadata: {
name: 'Edition 1',
description: 'This is the first edition.',
thumbnail: 'edition-1.jpeg',
},
};
it('should create the edition', async () => {
edition = await client.send(contract.createEdition(editionInput));
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.size).toEqual(0);
expect(onChainEdition.burned).toEqual(0);
});
it('should fail to create an edition that already exists', async () => {
await expect(async () => {
await client.send(contract.createEdition(editionInput));
}).rejects.toThrow('an edition has already been created with mintID');
});
it('should mint 4 NFTs into the edition', async () => {
const count = 4;
const mintedNFTs = await client.send(contract.mintNFTs({ editionId: edition.id, count }));
for (const nft of mintedNFTs) {
expect(nft.editionId).toEqual(edition.id);
}
allMintedNFTs.push(...mintedNFTs);
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.size).toEqual(count);
expect(onChainEdition.burned).toEqual(0);
});
it('should fail to mint more than the edition limit', async () => {
await expect(async () => {
await client.send(contract.mintNFTs({ editionId: edition.id, count: 2 }));
}).rejects.toThrow('edition is closed for minting');
const onChainEdition = await client.query(contract.getEdition(edition.id));
// Size should be unchanged
expect(onChainEdition.size).toEqual(4);
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.burned).toEqual(0);
});
it('should mint 1 remaining NFT into the edition', async () => {
const count = 1;
const mintedNFTs = await client.send(contract.mintNFTs({ editionId: edition.id, count }));
for (const nft of mintedNFTs) {
expect(nft.editionId).toEqual(edition.id);
}
allMintedNFTs.push(...mintedNFTs);
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.size).toEqual(edition.limit);
expect(onChainEdition.burned).toEqual(0);
// Edition should now be closed
expect(onChainEdition.isClosed).toBe(true);
});
it('should fail to mint into a closed edition', async () => {
await expect(async () => {
await client.send(contract.mintNFTs({ editionId: edition.id, count: 1 }));
}).rejects.toThrow('edition is closed for minting');
const onChainEdition = await client.query(contract.getEdition(edition.id));
// Edition should still be closed
expect(onChainEdition.isClosed).toBe(true);
// Edition size should be unchanged
expect(onChainEdition.size).toEqual(edition.limit);
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.burned).toEqual(0);
});
it('should burn an NFT and increment the edition burn count', async () => {
const nft = allMintedNFTs[0];
// Destroy one NFT from the edition
await client.send(contract.destroyNFT(nft.id));
const onChainEdition = await client.query(contract.getEdition(edition.id));
// Burn count should increase to 1
expect(onChainEdition.burned).toEqual(1);
// All other edition properties should be unchanged
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.size).toEqual(edition.limit);
expect(onChainEdition.isClosed).toBe(true);
});
});
describe('Edition with limit and partial mint', () => {
let edition: EditionResult;
it('should create the edition', async () => {
const editionInput = {
limit: 5,
metadata: {
name: 'Edition 2',
description: 'This is the second edition.',
thumbnail: 'edition-2.jpeg',
},
};
edition = await client.send(contract.createEdition(editionInput));
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.size).toEqual(0);
});
const count = 3;
it('should mint part of the edition', async () => {
const mintedNFTs = await client.send(contract.mintNFTs({ editionId: edition.id, count }));
for (const nft of mintedNFTs) {
expect(nft.editionId).toEqual(edition.id);
}
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.size).toEqual(count);
});
it('should close the edition early', async () => {
await client.send(contract.closeEdition(edition.id));
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
expect(onChainEdition.size).toEqual(count);
});
it('should fail to mint after closing', async () => {
await expect(async () => {
await client.send(contract.mintNFTs({ editionId: edition.id, count: 1 }));
}).rejects.toThrow('edition is closed for minting');
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toEqual(edition.limit);
// Size should still be the count
expect(onChainEdition.size).toEqual(count);
// Edition should still be closed
expect(onChainEdition.isClosed).toBe(true);
});
});
describe('Edition with no limit', () => {
let edition: EditionResult;
it('should create the edition', async () => {
const editionInput = {
metadata: {
name: 'Edition 3',
description: 'This is the third edition.',
thumbnail: 'edition-3.jpeg',
},
};
edition = await client.send(contract.createEdition(editionInput));
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toBeUndefined();
expect(onChainEdition.size).toEqual(0);
});
const count = 3;
it('should mint NFTs into the edition', async () => {
await client.send(contract.mintNFTs({ editionId: edition.id, count }));
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toBeUndefined();
expect(onChainEdition.size).toEqual(count);
});
it('should close the edition', async () => {
await client.send(contract.closeEdition(edition.id));
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toBeUndefined();
expect(onChainEdition.size).toEqual(count);
});
it('should fail to mint after closing', async () => {
await expect(async () => {
await client.send(contract.mintNFTs({ editionId: edition.id, count: 1 }));
}).rejects.toThrow('edition is closed for minting');
const onChainEdition = await client.query(contract.getEdition(edition.id));
expect(onChainEdition.id).toEqual(edition.id);
expect(onChainEdition.limit).toBeUndefined();
// Size should still be the count
expect(onChainEdition.size).toEqual(count);
// Edition should still be closed
expect(onChainEdition.isClosed).toBe(true);
});
it('should fail close the edition twice', async () => {
await expect(async () => {
await client.send(contract.closeEdition(edition.id));
}).rejects.toThrow('edition is already closed');
});
});
describe('Edition with a custom bucket', () => {
const customBucketName = 'custom_bucket';
let editionA: EditionResult;
let editionB: EditionResult;
it('should create two editions', async () => {
const editionAInput = {
limit: 5,
metadata: {
name: 'Edition 4',
description: 'This is the fourth edition.',
thumbnail: 'edition-4.jpeg',
},
};
const editionBInput = {
limit: 5,
metadata: {
name: 'Edition 5',
description: 'This is the fifth edition.',
thumbnail: 'edition-5.jpeg',
},
};
editionA = await client.send(contract.createEdition(editionAInput));
editionB = await client.send(contract.createEdition(editionBInput));
});
it('should mint all edition A NFTs into the default bucket', async () => {
await client.send(contract.mintNFTs({ editionId: editionA.id, count: 5 }));
});
it('should mint all edition B NFTs into a custom bucket', async () => {
await client.send(contract.mintNFTs({ editionId: editionB.id, count: 5, bucket: customBucketName }));
});
const sale = new FreshmintClaimSaleContract(contract);
const sale1 = 'sale1';
const sale2 = 'sale2';
describe('sale 1', () => {
it('should start a sale from default bucket', async () => {
await client.send(sale.start({ id: sale1, price: '10.0' }));
});
it('should claim an NFT', async () => {
await client.send(sale.claimNFT(ownerAuthorizer.address, ownerAuthorizer, sale1));
});
it('should stop a sale', async () => {
await client.send(sale.stop(sale1));
});
});
describe('sale 2', () => {
it('should start a sale from custom bucket', async () => {
await client.send(sale.start({ id: sale2, price: '10.0', bucket: customBucketName }));
});
it('should claim an NFT', async () => {
await client.send(sale.claimNFT(ownerAuthorizer.address, ownerAuthorizer, sale2));
});
it('should stop a sale', async () => {
await client.send(sale.stop(sale2));
});
});
});
});