-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecondaryMarket.test.ts
628 lines (538 loc) Β· 23.3 KB
/
SecondaryMarket.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import chai, { assert } from 'chai'
import { decodeJwt } from 'jose'
import chaiAsPromised from 'chai-as-promised'
import config from '../../test/config'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { NvmAccount } from '../../src/models/NvmAccount'
import { DDO, getConditionsByParams } from '../../src/ddo/DDO'
import { AssetPrice } from '../../src/models/AssetPrice'
import { getRoyaltyAttributes, RoyaltyAttributes } from '../../src/nevermined/api/AssetsApi'
import { getMetadata } from '../utils/ddo-metadata-generator'
import { RoyaltyKind } from '../../src/types/MetadataTypes'
import { NFTAttributes } from '../../src/models/NFTAttributes'
import { Token } from '../../src/keeper/contracts/Token'
import { Nft1155Contract } from '../../src/keeper/contracts/Nft1155Contract'
import {
LockPaymentCondition,
EscrowPaymentCondition,
TransferNFTCondition,
} from '../../src/keeper/contracts/conditions'
import { NFTSalesTemplate } from '../../src/keeper/contracts/templates/NFTSalesTemplate'
import { NFTAccessTemplate } from '../../src/keeper/contracts/templates/NFTAccessTemplate'
import { Service } from '../../src/types/DDOTypes'
import { formatUnits, parseUnits } from '../../src/nevermined/utils/BlockchainViemUtils'
import { generateId } from '../../src/common/helpers'
import { AssetAttributes } from '../../src/models/AssetAttributes'
import { ConditionState } from '../../src/types/ContractTypes'
chai.use(chaiAsPromised)
describe('Secondary Markets', () => {
let owner: NvmAccount
let artist: NvmAccount
let collector1: NvmAccount
let collector2: NvmAccount
let gallery: NvmAccount
let nevermined: Nevermined
let token: Token
let nftUpgradeable: Nft1155Contract
let transferNftCondition: TransferNFTCondition
let lockPaymentCondition: LockPaymentCondition
let escrowPaymentCondition: EscrowPaymentCondition
let nftSalesTemplate: NFTSalesTemplate
let nftAccessTemplate: NFTAccessTemplate
let ddo: DDO
const royalties = 10 // 10% of royalties in the secondary market
const cappedAmount = 5n
let royaltyAttributes: RoyaltyAttributes
let agreementId: string
let agreementId2: string
let agreementId3: string
let agreementAccessId: string
let agreementAccessId2: string
let agreementIdSeed: string
let agreementId2Seed: string
let agreementAccessIdSeed: string
let agreementAccessId2Seed: string
let nftSalesServiceAgreement: Service
// Configuration of First Sale:
// Artist -> Collector1, the gallery get a cut (25%)
const numberNFTs = 1n
let nftPrice = 20n
let amounts = [15n, 5n]
let receivers: string[]
let assetPrice1: AssetPrice
// Configuration of Sale in secondary market:
// Collector1 -> Collector2, the artist get 10% royalties
const numberNFTs2 = 1n
const nftTransfer = true
let nftPrice2 = 100n
let amounts2 = [90n, 10n]
let receivers2: string[]
let receivers3: string[]
let assetPrice2: AssetPrice
let assetPrice3: AssetPrice
let initialBalances: any
let decimals: number
let nftBalanceCollector1Before: bigint
let nftBalanceCollector2Before: bigint
before(async () => {
nevermined = await Nevermined.getInstance(config)
;[owner, artist, collector1, collector2, gallery] = nevermined.accounts.list()
receivers = [artist.getId(), gallery.getId()]
receivers2 = [collector1.getId(), artist.getId()]
receivers3 = [collector2.getId(), artist.getId()]
// components
;({ token, nftUpgradeable } = nevermined.keeper)
// conditions
;({ transferNftCondition, lockPaymentCondition, escrowPaymentCondition } =
nevermined.keeper.conditions)
// templates
;({ nftSalesTemplate, nftAccessTemplate } = nevermined.keeper.templates)
decimals = await token.decimals()
nftPrice = parseUnits(nftPrice.toString(), decimals)
amounts = amounts.map((v) => parseUnits(v.toString(), decimals))
nftPrice2 = parseUnits(nftPrice2.toString(), decimals)
amounts2 = amounts2.map((v) => parseUnits(v.toString(), decimals))
assetPrice1 = new AssetPrice(
new Map([
[receivers[0], amounts[0]],
[receivers[1], amounts[1]],
]),
).setTokenAddress(token.address)
assetPrice2 = new AssetPrice(
new Map([
[receivers2[0], amounts2[0]],
[receivers2[1], amounts2[1]],
]),
).setTokenAddress(token.address)
assetPrice3 = new AssetPrice(
new Map([
[receivers3[0], amounts2[0]],
[receivers3[1], amounts2[1]],
]),
).setTokenAddress(token.address)
})
describe('Collector1 initiates the sales agreement', () => {
before(async () => {
// initial balances
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),
}
agreementIdSeed = generateId()
agreementId2Seed = generateId()
agreementAccessIdSeed = generateId()
agreementAccessId2Seed = generateId()
agreementId = await nevermined.keeper.agreementStoreManager.agreementId(
agreementIdSeed,
collector1.getId(),
)
agreementId2 = await nevermined.keeper.agreementStoreManager.agreementId(
agreementId2Seed,
collector2.getId(),
)
agreementAccessId = await nevermined.keeper.agreementStoreManager.agreementId(
agreementAccessIdSeed,
collector1.getId(),
)
agreementAccessId2 = await nevermined.keeper.agreementStoreManager.agreementId(
agreementAccessId2Seed,
collector2.getId(),
)
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(artist)
await nevermined.services.marketplace.login(clientAssertion)
const payload = decodeJwt(config.marketplaceAuthToken)
const metadata = getMetadata()
metadata.userId = payload.sub
royaltyAttributes = getRoyaltyAttributes(nevermined, RoyaltyKind.Standard, royalties)
const assetAttributes = AssetAttributes.getInstance({
metadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
nft: { amount: numberNFTs, nftTransfer },
},
{
serviceType: 'nft-access',
nft: { amount: numberNFTs },
},
],
})
const nftAttributes = NFTAttributes.getNFT1155Instance({
...assetAttributes,
nftContractAddress: nftUpgradeable.address,
cap: cappedAmount,
royaltyAttributes,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist)
})
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 () => {
await nftUpgradeable.setApprovalForAll(transferNftCondition.address, true, artist)
const balance = await nftUpgradeable.balance(artist.getId(), ddo.id)
assert.equal(balance, 5n)
})
})
describe('As a collector I want to buy some art', () => {
it('I am setting an agreement for buying a NFT', async () => {
const result = await nftSalesTemplate.createAgreementFromDDO(
agreementIdSeed,
ddo,
nftSalesTemplate.params(collector1.getId(), numberNFTs),
collector1,
)
assert.isDefined(result)
const status = await nftSalesTemplate.getAgreementStatus(agreementId)
assert.equal(status && status.lockPayment.state, ConditionState.Unfulfilled)
assert.equal(status && status.transferNFT.state, ConditionState.Unfulfilled)
assert.equal(status && status.escrowPayment.state, ConditionState.Unfulfilled)
})
it('I am locking the payment', async () => {
await nevermined.accounts.requestTokens(collector1, BigInt(formatUnits(nftPrice, decimals)))
const collector1BalanceBefore = await token.balanceOf(collector1.getId())
assert.equal(collector1BalanceBefore, initialBalances.collector1 + nftPrice)
const receipt = await nevermined.agreements.conditions.lockPayment(
agreementId,
ddo.id,
assetPrice1.getAmounts(),
assetPrice1.getReceivers(),
collector1,
token.address,
)
assert.isTrue(receipt)
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
const escrowPaymentConditionBalance = await token.balanceOf(escrowPaymentCondition.address)
assert.equal(collector1BalanceAfter, initialBalances.collector1)
assert.equal(
escrowPaymentConditionBalance - initialBalances.escrowPaymentCondition,
nftPrice,
)
})
it('The artist can check the payment and transfer the NFT to the collector', async () => {
const nftBalanceArtistBefore = await nftUpgradeable.balance(artist.getId(), ddo.id)
const nftBalanceCollectorBefore = await nftUpgradeable.balance(collector1.getId(), ddo.id)
const service = ddo.findServiceByType('nft-sales')
const receipt = await nevermined.agreements.conditions.transferNft(
agreementId,
ddo,
service.index,
numberNFTs,
artist,
)
assert.isTrue(receipt)
const nftBalanceArtistAfter = await nftUpgradeable.balance(artist.getId(), ddo.id)
const nftBalanceCollectorAfter = await nftUpgradeable.balance(collector1.getId(), ddo.id)
assert.equal(nftBalanceArtistAfter, nftBalanceArtistBefore - numberNFTs)
assert.equal(nftBalanceCollectorAfter, nftBalanceCollectorBefore + numberNFTs)
})
it('the artist asks and receives the payment', async () => {
const receipt = await nevermined.agreements.conditions.releaseNftReward(
agreementId,
ddo,
'nft-sales',
numberNFTs,
artist,
)
assert.isTrue(receipt)
const escrowPaymentConditionBalance = await token.balanceOf(escrowPaymentCondition.address)
const receiver0Balance = await token.balanceOf(receivers[0])
const receiver1Balance = await token.balanceOf(receivers[1])
const collectorBalance = await token.balanceOf(collector1.getId())
assert.equal(receiver0Balance, initialBalances.artist + amounts[0])
assert.equal(receiver1Balance, initialBalances.gallery + amounts[1])
assert.equal(collectorBalance, initialBalances.collector1)
assert.equal(escrowPaymentConditionBalance, initialBalances.escrowPaymentCondition)
})
})
describe('As an artist I want to give exclusive access to the collectors owning a specific NFT', () => {
it('The collector sets up the NFT access agreement', async () => {
// Collector1: Create NFT access agreement
const result = await nftAccessTemplate.createAgreementFromDDO(
agreementAccessIdSeed,
ddo,
nftAccessTemplate.params(collector1.getId(), numberNFTs),
collector1,
)
assert.isDefined(result)
const status = await nftAccessTemplate.getAgreementStatus(agreementAccessId)
assert.equal(status && status.nftHolder.state, ConditionState.Unfulfilled)
assert.equal(status && status.nftAccess.state, ConditionState.Unfulfilled)
})
it('The collector demonstrates it owns the NFT', async function () {
const result = await nevermined.agreements.conditions.holderNft(
agreementAccessId,
ddo.id,
collector1.getId(),
numberNFTs,
nftUpgradeable.address,
collector1,
)
assert.isTrue(result)
})
it(' The artist gives access to the collector to the content', async function () {
const result = await nevermined.agreements.conditions.grantNftAccess(
agreementAccessId,
ddo.id,
collector1.getId(),
artist,
)
assert.isTrue(result)
})
})
describe('As collector1 I want to sell my NFT to collector2 for a higher price', () => {
before(async () => {
// initial balances
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()),
owner: await token.balanceOf(owner.getId()),
lockPaymentCondition: await token.balanceOf(lockPaymentCondition.address),
escrowPaymentCondition: await token.balanceOf(escrowPaymentCondition.address),
}
ddo.setNFTRewardsFromService('nft-sales', assetPrice2, collector1.getId())
})
it('As collector1 I create and store an off-chain service agreement', async () => {
const nftSalesServiceAgreementTemplate = nftSalesTemplate.getServiceAgreementTemplate()
const nftSalesTemplateConditions = nftSalesTemplate.getServiceAgreementTemplateConditions()
nftSalesServiceAgreementTemplate.conditions = getConditionsByParams(
'nft-sales',
nftSalesTemplateConditions,
collector1.getId(),
assetPrice2,
ddo.id,
token.address,
undefined,
collector1.getId(),
numberNFTs2,
true,
)
nftSalesServiceAgreement = {
type: 'nft-sales',
index: 6,
serviceEndpoint: nevermined.services.node.getNftEndpoint(),
templateId: nftSalesTemplate.address,
attributes: {
main: {
name: 'nftSalesAgreement',
creator: collector1.getId(),
datePublished: new Date().toISOString().replace(/\.[0-9]{3}/, ''),
timeout: 86400,
},
additionalInformation: {
description: '',
},
serviceAgreementTemplate: nftSalesServiceAgreementTemplate,
},
}
console.log('nftSalesServiceAgreement', JSON.stringify(nftSalesServiceAgreement))
// This Service agreement is stored on elasticsearch through the metadata api
})
it('As collector2 I am setting an agreement up for buying an NFT', async () => {
const result = await nftSalesTemplate.createAgreementFromDDO(
agreementId2Seed,
ddo,
nftSalesTemplate.params(collector2.getId(), numberNFTs2),
collector2,
)
assert.isDefined(result)
const status = await nftSalesTemplate.getAgreementStatus(agreementId2)
assert.equal(status && status.lockPayment.state, ConditionState.Unfulfilled)
assert.equal(status && status.transferNFT.state, ConditionState.Unfulfilled)
assert.equal(status && status.escrowPayment.state, ConditionState.Unfulfilled)
})
it('As collector2 I am locking the payment', async () => {
// Collector2 gets the price from some marketplace
// (query the service agreements from the metadata)
await nevermined.accounts.requestTokens(
collector2,
BigInt(formatUnits(nftPrice2, decimals)),
)
const collector2BalanceBefore = await token.balanceOf(collector2.getId())
assert.equal(collector2BalanceBefore, initialBalances.collector2 + nftPrice2)
// After fetching the previously created sales agreement
const assetPriceFromServiceAgreement =
DDO.getAssetPriceFromService(nftSalesServiceAgreement)
const payment = DDO.findServiceConditionByName(nftSalesServiceAgreement, 'lockPayment')
const receipt = await nevermined.agreements.conditions.lockPayment(
agreementId2,
ddo.id,
assetPriceFromServiceAgreement.getAmounts(),
assetPriceFromServiceAgreement.getReceivers(),
collector2,
payment.parameters.find((p) => p.name === '_tokenAddress')?.value as string,
)
assert.isTrue(receipt)
const collector2BalanceAfter = await token.balanceOf(collector2.getId())
const escrowPaymentConditionBalance = await token.balanceOf(escrowPaymentCondition.address)
assert.equal(collector2BalanceAfter, initialBalances.collector2)
assert.equal(
escrowPaymentConditionBalance - initialBalances.escrowPaymentCondition,
nftPrice2,
)
})
it('As collector1 I can check the payment and transfer the NFT to collector2', async () => {
const nftBalanceCollector1Before = await nftUpgradeable.balance(collector1.getId(), ddo.id)
const nftBalanceCollector2Before = await nftUpgradeable.balance(collector2.getId(), ddo.id)
const service = ddo.findServiceByType('nft-sales')
const receipt = await nevermined.agreements.conditions.transferNft(
agreementId2,
ddo,
service.index,
numberNFTs2,
collector1,
)
assert.isTrue(receipt)
const nftBalanceCollector1After = await nftUpgradeable.balance(collector1.getId(), ddo.id)
const nftBalanceCollector2After = await nftUpgradeable.balance(collector2.getId(), ddo.id)
assert.equal(
Number(nftBalanceCollector1After),
Number(nftBalanceCollector1Before) - Number(numberNFTs2),
)
assert.equal(
Number(nftBalanceCollector2After),
Number(nftBalanceCollector2Before) + Number(numberNFTs2),
)
})
it('Collector1 and Artist get the payment', async () => {
// After fetching the previously created sales agreement
const assetPriceFromServiceAgreement =
DDO.getAssetPriceFromService(nftSalesServiceAgreement)
const escrowPaymentConditionBalanceBefore = await token.balanceOf(
escrowPaymentCondition.address,
)
const receipt = await nevermined.agreements.conditions.releaseNftReward(
agreementId2,
ddo,
'nft-sales',
numberNFTs2,
collector1,
)
assert.isTrue(receipt)
const escrowPaymentConditionBalanceAfter = await token.balanceOf(
escrowPaymentCondition.address,
)
const receiver0Balance = await token.balanceOf(receivers2[0])
const receiver1Balance = await token.balanceOf(receivers2[1])
const collectorBalance = await token.balanceOf(collector2.getId())
assert.equal(receiver0Balance, initialBalances.collector1 + amounts2[0])
assert.equal(receiver1Balance, initialBalances.artist + amounts2[1])
assert.equal(collectorBalance, initialBalances.collector2)
assert.equal(
escrowPaymentConditionBalanceBefore - assetPriceFromServiceAgreement.getTotalPrice(),
escrowPaymentConditionBalanceAfter,
)
})
})
describe('As collector1 I want to give exclusive access to the collectors owning a specific NFT', () => {
before(async () => {
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(collector2)
ddo.setNFTRewardsFromService('nft-sales', assetPrice3, collector2.getId())
await nevermined.services.marketplace.login(clientAssertion)
})
it('The collector2 sets up the NFT access agreement', async () => {
// Collector1: Create NFT access agreement
const result = await nftAccessTemplate.createAgreementFromDDO(
agreementAccessId2Seed,
ddo,
nftAccessTemplate.params(collector2.getId(), numberNFTs),
collector2,
)
assert.isDefined(result)
const status = await nftAccessTemplate.getAgreementStatus(agreementAccessId2)
assert.equal(status && status.nftHolder.state, ConditionState.Unfulfilled)
assert.equal(status && status.nftAccess.state, ConditionState.Unfulfilled)
})
it('The collector2 demonstrates it owns the NFT', async function () {
const result = await nevermined.agreements.conditions.holderNft(
agreementAccessId2,
ddo.id,
collector2.getId(),
numberNFTs,
nftUpgradeable.address,
collector2,
)
assert.isTrue(result)
})
it('The artist gives access to the collector2 to the content', async function () {
const result = await nevermined.agreements.conditions.grantNftAccess(
agreementAccessId2,
ddo.id,
collector2.getId(),
artist,
)
assert.isTrue(result)
})
it('Collector 1 no longer has access the to the content', async () => {
// Not the best way to do this but on localnet we don't get the revert reasons
await assert.isRejected(
nevermined.agreements.conditions.holderNft(
agreementAccessId,
ddo.id,
collector1.getId(),
numberNFTs,
nftUpgradeable.address,
collector1,
),
)
})
it('As collector2 I setup an agreement for selling my NFT', async () => {
agreementId3 = await nevermined.nfts1155.listOnSecondaryMarkets(
ddo,
assetPrice3,
numberNFTs2,
nftTransfer,
collector2.getId(),
token,
collector2,
)
assert.isNotNull(agreementId3)
const service = await nevermined.services.metadata.retrieveService(agreementId3)
assert.isDefined(service)
})
it('As collector1 I buy the secondary market NFT', async () => {
await nevermined.accounts.requestTokens(
collector1,
BigInt(formatUnits(nftPrice2, decimals)),
)
nftBalanceCollector1Before = await nftUpgradeable.balance(collector1.getId(), ddo.id)
nftBalanceCollector2Before = await nftUpgradeable.balance(collector2.getId(), ddo.id)
const result = await nevermined.nfts1155.buySecondaryMarketNft(collector1, 1n, agreementId3)
assert.isTrue(result)
})
it('As collector2 I see the bid locked and trigger the reward release', async () => {
const escrowPaymentConditionBalanceBefore = await token.balanceOf(
escrowPaymentCondition.address,
)
const result = await nevermined.nfts1155.releaseSecondaryMarketRewards(
collector2,
collector1,
agreementId3,
)
assert.isTrue(result)
const nftBalanceCollector1After = await nftUpgradeable.balance(collector1.getId(), ddo.id)
const nftBalanceCollector2After = await nftUpgradeable.balance(collector2.getId(), ddo.id)
assert.equal(nftBalanceCollector1After, nftBalanceCollector1Before + numberNFTs2)
assert.equal(nftBalanceCollector2After, nftBalanceCollector2Before - numberNFTs2)
const escrowPaymentConditionBalanceAfter = await token.balanceOf(
escrowPaymentCondition.address,
)
const receiver0Balance = await token.balanceOf(receivers3[0])
const receiver1Balance = await token.balanceOf(receivers3[1])
const artistBalance = await token.balanceOf(artist.getId())
const collector1Balance = await token.balanceOf(collector1.getId())
assert.equal(receiver0Balance, initialBalances.collector2 + amounts2[0])
assert.equal(receiver1Balance, artistBalance)
assert.isTrue(collector1Balance >= nftPrice2)
assert.equal(
escrowPaymentConditionBalanceBefore - AssetPrice.sumAmounts(amounts2),
escrowPaymentConditionBalanceAfter,
)
})
})
})
})