-
Notifications
You must be signed in to change notification settings - Fork 1
/
NFT1155Api.e2e.test.ts
548 lines (464 loc) Β· 19.6 KB
/
NFT1155Api.e2e.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import chai, { assert } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { DDO } from '../../src/ddo/DDO'
import { AssetPrice } from '../../src/models/AssetPrice'
import { NvmAccount } from '../../src/models/NvmAccount'
import { Nevermined } from '../../src/nevermined/Nevermined'
import config from '../../test/config'
import { getMetadata } from '../utils/ddo-metadata-generator'
import { ZeroAddress } from '../../src/constants/AssetConstants'
import { ContractHandler } from '../../src/keeper/ContractHandler'
import { Nft1155Contract } from '../../src/keeper/contracts/Nft1155Contract'
import { Token } from '../../src/keeper/contracts/Token'
import { EscrowPaymentCondition, TransferNFTCondition } from '../../src/keeper/contracts/conditions'
import { AssetAttributes } from '../../src/models/AssetAttributes'
import { NFTAttributes } from '../../src/models/NFTAttributes'
import { getRoyaltyAttributes } from '../../src/nevermined/api/AssetsApi'
import { getChecksumAddress } from '../../src/nevermined/utils/BlockchainViemUtils'
import {
DIDResolvePolicy,
PublishMetadataOptions,
RoyaltyKind,
} from '../../src/types/MetadataTypes'
import '../globals'
import { JWTPayload, decodeJwt } from 'jose'
chai.use(chaiAsPromised)
function makeTest(isCustom) {
describe(`NFTs 1155 Api End-to-End (${isCustom ? 'custom' : 'builtin'} token)`, () => {
let artist: NvmAccount
let collector1: NvmAccount
let collector2: NvmAccount
let gallery: NvmAccount
let nevermined: Nevermined
let token: Token
let escrowPaymentCondition: EscrowPaymentCondition
let transferNftCondition: TransferNFTCondition
let ddo: DDO
const metadata = getMetadata()
const royalties1 = 100000 // 10% of royalties in the secondary market
const royalties = 10 // 10% of royalties in the secondary market
const cappedAmount = 5n
let agreementId: string
let agreementId2: string
// Configuration of First Sale:
// Artist -> Collector1, the gallery get a cut (25%)
const numberEditions = 1n
let nftPrice = 100n
let amounts = [75n, 25n]
let receivers: string[]
let assetPrice1: AssetPrice
let initialBalances: any
let scale: bigint
let payload: JWTPayload
before(async () => {
nevermined = await Nevermined.getInstance(config, {
loadCore: true,
loadServiceAgreements: true,
loadNFTs1155: true,
loadNFTs721: false,
loadDispenser: true,
loadERC20Token: true,
loadAccessFlow: false,
loadDIDTransferFlow: false,
loadRewards: false,
loadRoyalties: true,
loadCompute: false,
})
;[, artist, collector1, collector2, , gallery] = nevermined.accounts.list()
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(artist)
await nevermined.services.marketplace.login(clientAssertion)
payload = decodeJwt(config.marketplaceAuthToken)
metadata.userId = payload.sub
// conditions
;({ escrowPaymentCondition, transferNftCondition } = nevermined.keeper.conditions)
const feeReceiver = await nevermined.keeper.nvmConfig.getFeeReceiver()
console.debug(`FEE RECEIVER = ${feeReceiver}`)
const fee = await nevermined.keeper.nvmConfig.getNetworkFee()
console.debug(`NETWORK FEE = ${fee}`)
console.debug(
`Fee receiver: ${feeReceiver}, contract: ${
escrowPaymentCondition.address
}, artist: ${artist.getId()}, gallery: ${gallery.getId()}`,
)
if (isCustom) {
const networkName = await nevermined.keeper.getNetworkName()
const erc1155ABI = await ContractHandler.getABIArtifact(
'NFT1155Upgradeable',
config.artifactsFolder,
networkName,
)
const nft = await nevermined.utils.blockchain.deployAbi(erc1155ABI, artist, [
artist.getId(),
nevermined.keeper.didRegistry.address,
'NFT1155',
'NVM',
'',
nevermined.keeper.nvmConfig.address,
])
const nftContract = await Nft1155Contract.getInstance(
(nevermined.keeper as any).instanceConfig,
await nft.address,
)
await nevermined.contracts.loadNft1155(nftContract.address)
await nftContract.grantOperatorRole(transferNftCondition.address, artist)
}
// components
// eslint-disable-next-line @typescript-eslint/no-extra-semi
;({ token } = nevermined.keeper)
scale = 10n ** BigInt(await token.decimals())
amounts = amounts.map((v) => v * scale)
receivers = [artist.getId(), gallery.getId()]
const lst: [string, bigint][] = [
[receivers[0], amounts[0]],
[receivers[1], amounts[1]],
]
if (feeReceiver !== '0x0000000000000000000000000000000000000000') {
receivers.push(feeReceiver)
const price = amounts.reduce((a, b) => a + b, 0n)
amounts.push((price * fee) / 1000000n - fee)
lst.push([receivers[2], amounts[2]])
}
nftPrice = amounts.reduce((a, b) => a + b, 0n)
assetPrice1 = new AssetPrice(new Map(lst))
await nevermined.accounts.requestTokens(collector1, nftPrice / scale)
console.debug(
`Contract balance (initial) ${await token.balanceOf(escrowPaymentCondition.address)}`,
)
initialBalances = {
artist: await token.balanceOf(artist.getId()),
collector1: await token.balanceOf(collector1.getId()),
collector2: await token.balanceOf(collector2.getId()),
gallery: await token.balanceOf(gallery.getId()),
escrowPaymentCondition: await token.balanceOf(escrowPaymentCondition.address),
}
})
describe('As an artist I want to register a new artwork', () => {
it('I want to register a new artwork and tokenize (via NFT). I want to get 10% royalties', async () => {
const royaltyAttributes = getRoyaltyAttributes(nevermined, RoyaltyKind.Standard, royalties1)
const assetAttributes = AssetAttributes.getInstance({
metadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
nft: { amount: numberEditions, nftTransfer: true },
},
{
serviceType: 'nft-access',
nft: { amount: numberEditions },
},
],
providers: [config.neverminedNodeAddress],
})
const nftAttributes = NFTAttributes.getNFT1155Instance({
...assetAttributes,
nftContractAddress: nevermined.nfts1155.nftContract.address,
cap: cappedAmount,
royaltyAttributes,
preMint: true,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist, {
metadata: PublishMetadataOptions.IPFS,
})
assert.isDefined(ddo)
const balance = await nevermined.nfts1155.balance(ddo.id, artist.getId())
assert.isTrue(balance === 5n)
})
it('should give Nevermined the operator role', async () => {
assert.isTrue(
await nevermined.nfts1155.isOperatorOfDID(
ddo.id,
nevermined.keeper.conditions.transferNftCondition.address,
),
)
})
it('Should set the Node as a provider by default', async () => {
const providers = await nevermined.assets.providers.list(ddo.id)
assert.deepEqual(providers, [getChecksumAddress(config.neverminedNodeAddress)])
})
})
describe('As a collector I want to buy some art', () => {
it('I check the details of the NFT', async () => {
await nevermined.assets.resolve(ddo.id, DIDResolvePolicy.ImmutableFirst)
const details = await nevermined.nfts1155.details(ddo.id)
assert.equal(details.mintCap, 5n)
assert.equal(details.nftSupply, 5n)
assert.equal(details.royaltyScheme, RoyaltyKind.Standard)
assert.equal(details.royalties, 100000)
assert.equal(details.owner, artist.getId())
assert.isTrue(details.mintCap === 5n)
assert.equal(details.nftSupply, 5n)
})
it('I am ordering the NFT', async () => {
const collector1BalanceBefore = await token.balanceOf(collector1.getId())
assert.isTrue(collector1BalanceBefore >= nftPrice)
const escrowPaymentConditionBalanceBefore = await token.balanceOf(
escrowPaymentCondition.address,
)
agreementId = await nevermined.nfts1155.order(ddo.id, numberEditions, collector1)
assert.isDefined(agreementId)
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
const escrowPaymentConditionBalanceAfter = await token.balanceOf(
escrowPaymentCondition.address,
)
console.debug(
`${collector1BalanceBefore} - ${nftPrice} == ${collector1BalanceAfter}\n`,
`${escrowPaymentConditionBalanceBefore} + ${nftPrice} == ${escrowPaymentConditionBalanceAfter}`,
)
assert.equal(collector1BalanceBefore - nftPrice, collector1BalanceAfter)
assert.equal(
escrowPaymentConditionBalanceBefore + nftPrice,
escrowPaymentConditionBalanceAfter,
)
})
it('The artist can check the payment and transfer the NFT to the collector', async () => {
const nftBalanceArtistBefore = await nevermined.nfts1155.balance(ddo.id, artist)
const nftBalanceCollectorBefore = await nevermined.nfts1155.balance(ddo.id, collector1)
console.debug(`Contract balance ${await token.balanceOf(escrowPaymentCondition.address)}`)
const receipt = await nevermined.nfts1155.transfer(
agreementId,
ddo.id,
numberEditions,
artist,
)
assert.isTrue(receipt)
console.debug(
`Contract balance (after) ${await token.balanceOf(escrowPaymentCondition.address)}`,
)
const nftBalanceArtistAfter = await nevermined.nfts1155.balance(ddo.id, artist)
const nftBalanceCollectorAfter = await nevermined.nfts1155.balance(ddo.id, collector1)
assert.equal(
Number(nftBalanceArtistAfter),
Number(nftBalanceArtistBefore) - Number(numberEditions),
)
assert.equal(
Number(nftBalanceCollectorAfter),
Number(nftBalanceCollectorBefore) + Number(numberEditions),
)
})
it('the artist asks and receives the payment', async () => {
const escrowPaymentConditionBefore = await token.balanceOf(escrowPaymentCondition.address)
const service = ddo.findServiceByReference('nft-sales')
const receipt = await nevermined.nfts1155.releaseRewards(
agreementId,
ddo.id,
service.index,
numberEditions,
artist,
)
assert.isTrue(receipt)
const escrowPaymentConditionBalanceAfter = await token.balanceOf(
escrowPaymentCondition.address,
)
const receiver0Balance = await token.balanceOf(assetPrice1.getReceivers()[0])
const receiver1Balance = await token.balanceOf(assetPrice1.getReceivers()[1])
const collectorBalance = await token.balanceOf(collector1.getId())
console.debug(
`${receiver0Balance} == ${initialBalances.artist} + ${assetPrice1.getAmounts()[0]}\n`,
`${receiver1Balance} == ${initialBalances.gallery} + ${assetPrice1.getAmounts()[1]}\n`,
`${initialBalances.collector1} - ${nftPrice} == ${collectorBalance}\n`,
`${escrowPaymentConditionBefore} - ${nftPrice} == ${escrowPaymentConditionBalanceAfter}`,
)
assert.equal(receiver0Balance, initialBalances.artist + assetPrice1.getAmounts()[0])
assert.equal(receiver1Balance, initialBalances.gallery + assetPrice1.getAmounts()[1])
assert.equal(initialBalances.collector1 - nftPrice, collectorBalance)
assert.equal(escrowPaymentConditionBefore - nftPrice, escrowPaymentConditionBalanceAfter)
})
})
describe('As a collector I want to order and access the NFT without the intervention of the artist', () => {
it('The artist gives the Node permissions to transfer his nfts', async () => {
const message = 'should throw this error message'
try {
await nevermined.nfts1155.setApprovalForAll(transferNftCondition.address, true, artist)
await nevermined.nfts1155.setApprovalForAll(config.neverminedNodeAddress, true, artist)
assert.fail(message)
} catch (error) {
assert.equal(error.message, message)
}
})
it('The artist creates and mints the nfts', async () => {
const newMetadata = getMetadata()
newMetadata.userId = payload.sub
const royaltyAttributes = getRoyaltyAttributes(nevermined, RoyaltyKind.Standard, royalties)
const assetAttributes = AssetAttributes.getInstance({
metadata: newMetadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
nft: { nftTransfer: true },
},
{
serviceType: 'nft-access',
},
],
})
const nftAttributes = NFTAttributes.getNFT1155Instance({
...assetAttributes,
nftContractAddress: nevermined.nfts1155.nftContract.address,
cap: cappedAmount,
royaltyAttributes,
preMint: true,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist)
assert.isDefined(ddo)
const balance = await nevermined.nfts1155.balance(ddo.id, artist)
assert.isTrue(balance === 5n)
await nevermined.nfts1155.setApprovalForAll(config.neverminedNodeAddress, true, artist)
})
it('The collector orders the nft', async () => {
await nevermined.accounts.requestTokens(collector1, nftPrice / scale)
agreementId = await nevermined.nfts1155.order(ddo.id, numberEditions, collector1)
assert.isDefined(agreementId)
})
it('Ask the Node to transfer the nft and release the rewards', async () => {
const result = await nevermined.nfts1155.claim(
agreementId,
artist.getId(),
collector1.getId(),
numberEditions,
ddo.id,
)
assert.isTrue(result)
})
it('The Node should fulfill the NFTHolder and NFTAccess conditions', async () => {
const result = await nevermined.nfts1155.access(ddo.id, collector1, '/tmp/', undefined)
assert.isTrue(result)
})
})
describe('As an artist I want to give exclusive access to the collectors owning a specific NFT', () => {
it('The collector access the files', async () => {
const result = await nevermined.nfts1155.access(ddo.id, collector1, '/tmp/')
assert.isTrue(result)
})
})
describe('As a collector I should not be able to buy a sold out nft', () => {
it('The artist gives the Node permissions to transfer his nfts', async () => {
const message = 'should throw this error message'
try {
await nevermined.nfts1155.setApprovalForAll(config.neverminedNodeAddress, true, artist)
assert.fail(message)
} catch (error) {
assert.equal(error.message, message)
}
})
it('The artist creates and mints one nft', async () => {
const newMetadata = getMetadata()
newMetadata.userId = payload.sub
const royaltyAttributes = getRoyaltyAttributes(nevermined, RoyaltyKind.Standard, royalties)
const assetAttributes = AssetAttributes.getInstance({
metadata: newMetadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
nft: { nftTransfer: true },
},
{
serviceType: 'nft-access',
},
],
})
const nftAttributes = NFTAttributes.getNFT1155Instance({
...assetAttributes,
nftContractAddress: nevermined.nfts1155.nftContract.address,
cap: 1n,
royaltyAttributes,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist)
assert.isDefined(ddo)
const balance = await nevermined.nfts1155.balance(ddo.id, artist)
assert.deepEqual(balance, 1n)
})
it('Collector1 orders the nft', async () => {
await nevermined.accounts.requestTokens(collector1, nftPrice / scale)
agreementId = await nevermined.nfts1155.order(ddo.id, numberEditions, collector1)
assert.isDefined(agreementId)
})
it('Ask the Node to transfer the nft and release the rewards', async () => {
const result = await nevermined.nfts1155.claim(
agreementId,
artist.getId(),
collector1.getId(),
1n,
)
assert.isTrue(result)
})
it('The Node should fulfill the NFTHolder and NFTAccess conditions', async () => {
const result = await nevermined.nfts1155.access(ddo.id, collector1, '/tmp/', undefined)
assert.isTrue(result)
})
it('The artist nft balance should be zero', async () => {
const balance = await nevermined.nfts1155.balance(ddo.id, artist)
assert.equal(balance, 0n)
})
it('Collector 2 setups a service agreement to buy the nft', async () => {
await nevermined.accounts.requestTokens(collector2, nftPrice / scale)
agreementId2 = await nevermined.nfts1155.order(ddo.id, numberEditions, collector2)
assert.isDefined(agreementId2)
})
it('The Node should not be able to transfer the nft', async () => {
await assert.isRejected(
nevermined.nfts1155.claim(agreementId2, artist.getId(), collector2.getId(), 1n),
)
})
})
describe('Node should not be able to transfer the nft without the operator role', () => {
it('should create the subscription NFT without granting Nevermined the operator role', async () => {
const networkName = await nevermined.keeper.getNetworkName()
const erc1155ABI = await ContractHandler.getABIArtifact(
'NFT1155Upgradeable',
config.artifactsFolder,
networkName,
)
const nft = await nevermined.utils.blockchain.deployAbi(erc1155ABI, artist, [
artist.getId(),
nevermined.keeper.didRegistry.address,
'NFT1155',
'NVM',
'',
ZeroAddress,
])
const nftContract = await Nft1155Contract.getInstance(
(nevermined.keeper as any).instanceConfig,
await nft.address,
)
await nevermined.contracts.loadNft1155(nftContract.address)
const assetAttributes = AssetAttributes.getInstance({
metadata: getMetadata(),
services: [
{
serviceType: 'nft-sales',
price: new AssetPrice(artist.getId(), 0n),
},
],
})
const nftAttributes = NFTAttributes.getNFT1155Instance({
...assetAttributes,
nftContractAddress: nevermined.nfts1155.nftContract.address,
cap: 1n,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist)
assert.isDefined(ddo)
})
it('subscriber should be able to order the nft', async () => {
agreementId = await nevermined.nfts1155.order(ddo.id, numberEditions, collector1)
assert.isDefined(agreementId)
})
it('nevermined should not allow the subscriber to claim through the node', async () => {
await assert.isRejected(
nevermined.nfts1155.claim(
agreementId,
artist.getId(),
collector1.getId(),
numberEditions,
ddo.id,
),
/Nevermined does not have operator role/,
)
})
})
})
}
makeTest(false)
makeTest(true)