-
Notifications
You must be signed in to change notification settings - Fork 1
/
NFTTemplatesWithEther.test.ts
410 lines (355 loc) Β· 14.4 KB
/
NFTTemplatesWithEther.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
import { assert } from 'chai'
import config from '../../test/config'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { NvmAccount } from '../../src/models/NvmAccount'
import { DDO } from '../../src/ddo/DDO'
import { AssetPrice } from '../../src/models/AssetPrice'
import { getMetadata } from '../utils/ddo-metadata-generator'
import { NFTAttributes } from '../../src/models/NFTAttributes'
import { generateId } from '../../src/common/helpers'
import { ConditionStoreManager } from '../../src/keeper/contracts/managers/ConditionStoreManager'
import {
EscrowPaymentCondition,
LockPaymentCondition,
NFTAccessCondition,
TransferNFTCondition,
NFTHolderCondition,
} from '../../src/keeper/contracts/conditions'
import { RoyaltyAttributes, getRoyaltyAttributes } from '../../src/nevermined/api/AssetsApi'
import { RoyaltyKind } from '../../src/types/MetadataTypes'
import { ConditionState } from '../../src/types/ContractTypes'
import { Nft1155Contract } from '../../src/keeper/contracts/Nft1155Contract'
import { NFTSalesTemplate } from '../../src/keeper/contracts/templates/NFTSalesTemplate'
import { NFTAccessTemplate } from '../../src/keeper/contracts/templates/NFTAccessTemplate'
import { AssetAttributes } from '../../src/models/AssetAttributes'
import { parseEther } from '../../src/nevermined/utils/BlockchainViemUtils'
import { ZeroAddress } from '../../src/constants/AssetConstants'
import { decodeJwt } from 'jose'
describe('NFTTemplates With Ether E2E', async () => {
let artist: NvmAccount
let collector1: NvmAccount
let collector2: NvmAccount
let gallery: NvmAccount
let sender: NvmAccount
let governor: NvmAccount
let nevermined: Nevermined
let conditionStoreManager: ConditionStoreManager
let nftUpgradeable: Nft1155Contract
let transferNftCondition: TransferNFTCondition
let lockPaymentCondition: LockPaymentCondition
let escrowPaymentCondition: EscrowPaymentCondition
let nftHolderCondition: NFTHolderCondition
let nftAccessCondition: NFTAccessCondition
let nftSalesTemplate: NFTSalesTemplate
let nftAccessTemplate: NFTAccessTemplate
let conditionIdLockPayment: [string, string]
let conditionIdTransferNFT: [string, string]
let conditionIdEscrow: [string, string]
let conditionIdNFTHolder: [string, string]
let conditionIdNFTAccess: [string, string]
let ddo: DDO
const royalties = 10 // 10% of royalties in the secondary market
const cappedAmount = 5n
let agreementId: string
let agreementAccessId: string
let agreementIdSeed: string
let agreementAccessIdSeed: string
const networkFee = 200000
// Configuration of First Sale:
// Artist -> Collector1, the gallery get a cut (25%)
const numberNFTs = 1n
const amounts = [parseEther('0.3'), parseEther('0.1'), parseEther('0.1')]
let receivers: string[]
let assetPrice: AssetPrice
let royaltyAttributes: RoyaltyAttributes
let initialBalances: any
before(async () => {
nevermined = await Nevermined.getInstance(config)
;[sender, artist, collector1, collector2, gallery, , , , , governor] =
nevermined.accounts.list()
console.debug(`ACCOUNT GOVERNOR = ${governor.getId()}`)
await nevermined.keeper.nvmConfig.setNetworkFees(networkFee, governor.getId(), governor)
const feeReceiver = await nevermined.keeper.nvmConfig.getFeeReceiver()
console.debug(`FEE RECEIVER = ${feeReceiver}`)
const fee = await nevermined.keeper.nvmConfig.getNetworkFee()
console.debug(`NETWORK FEE = ${fee}`)
receivers = [artist.getId(), gallery.getId(), governor.getId()]
// components
;({ conditionStoreManager, nftUpgradeable } = nevermined.keeper)
// conditions
;({
transferNftCondition,
lockPaymentCondition,
escrowPaymentCondition,
nftHolderCondition,
nftAccessCondition,
} = nevermined.keeper.conditions)
// templates
;({ nftSalesTemplate, nftAccessTemplate } = nevermined.keeper.templates)
// ether
assetPrice = new AssetPrice(
new Map([
[receivers[0], amounts[0]],
[receivers[1], amounts[1]],
[receivers[2], amounts[2]],
]),
).setTokenAddress(ZeroAddress)
})
after(async () => {
await nevermined.keeper.nvmConfig.setNetworkFees(0, ZeroAddress, governor)
console.debug(` --- Resetting Network Fees after the test`)
const feeReceiver = await nevermined.keeper.nvmConfig.getFeeReceiver()
console.debug(`FEE RECEIVER = ${feeReceiver}`)
const fee = await nevermined.keeper.nvmConfig.getNetworkFee()
console.debug(`NETWORK FEE = ${fee}`)
})
describe('Full flow', async () => {
before(async () => {
// initial balances
initialBalances = {
artist: await nevermined.accounts.getEtherBalance(artist),
collector1: await nevermined.accounts.getEtherBalance(collector1),
collector2: await nevermined.accounts.getEtherBalance(collector2),
gallery: await nevermined.accounts.getEtherBalance(gallery),
governor: await nevermined.accounts.getEtherBalance(governor),
escrowPaymentCondition: await nevermined.accounts.getEtherBalance(
escrowPaymentCondition.address,
),
}
agreementIdSeed = generateId()
agreementAccessIdSeed = generateId()
agreementId = await nevermined.keeper.agreementStoreManager.agreementId(
agreementIdSeed,
sender.getId(),
)
agreementAccessId = await nevermined.keeper.agreementStoreManager.agreementId(
agreementAccessIdSeed,
collector1.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: assetPrice,
nft: { amount: numberNFTs },
},
{
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', async () => {
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.shortId())
assert.deepEqual(balance, 5n)
})
})
describe('As a collector I want to buy some art', async () => {
it('I am setting an agreement for buying a NFT', async () => {
conditionIdLockPayment = await lockPaymentCondition.generateIdWithSeed(
agreementId,
await lockPaymentCondition.hashValues(
ddo.shortId(),
escrowPaymentCondition.address,
ZeroAddress,
assetPrice.getAmounts(),
assetPrice.getReceivers(),
),
)
conditionIdTransferNFT = await transferNftCondition.generateIdWithSeed(
agreementId,
await transferNftCondition.hashValues(
ddo.shortId(),
artist.getId(),
collector1.getId(),
numberNFTs,
conditionIdLockPayment[1],
),
)
conditionIdEscrow = await escrowPaymentCondition.generateIdWithSeed(
agreementId,
await escrowPaymentCondition.hashValues(
ddo.shortId(),
assetPrice.getAmounts(),
assetPrice.getReceivers(),
collector1.getId(),
escrowPaymentCondition.address,
ZeroAddress,
conditionIdLockPayment[1],
conditionIdTransferNFT[1],
),
)
const result = await nftSalesTemplate.createAgreement(
agreementIdSeed,
ddo.shortId(),
[conditionIdLockPayment[0], conditionIdTransferNFT[0], conditionIdEscrow[0]],
[0, 0, 0],
[0, 0, 0],
[collector1.getId()],
sender,
)
assert.equal(result.status, 'success')
const logs = nftSalesTemplate.getTransactionLogs(result, 'AgreementCreated')
assert.isTrue(logs.length > 0)
assert.equal(
(await conditionStoreManager.getCondition(conditionIdLockPayment[1])).state,
ConditionState.Unfulfilled,
)
assert.equal(
(await conditionStoreManager.getCondition(conditionIdEscrow[1])).state,
ConditionState.Unfulfilled,
)
assert.equal(
(await conditionStoreManager.getCondition(conditionIdTransferNFT[1])).state,
ConditionState.Unfulfilled,
)
})
it('I am locking the payment', async () => {
await lockPaymentCondition.fulfill(
agreementId,
ddo.shortId(),
escrowPaymentCondition.address,
ZeroAddress,
assetPrice.getAmounts(),
assetPrice.getReceivers(),
collector1,
{ value: assetPrice.getTotalPrice() },
)
const { state } = await conditionStoreManager.getCondition(conditionIdLockPayment[1])
assert.equal(state, ConditionState.Fulfilled)
})
it('The artist can check the payment and transfer the NFT to the collector', async () => {
const nftBalanceArtistBefore = await nftUpgradeable.balance(artist.getId(), ddo.shortId())
const nftBalanceCollectorBefore = await nftUpgradeable.balance(
collector1.getId(),
ddo.shortId(),
)
await transferNftCondition.fulfill(
agreementId,
ddo.shortId(),
collector1.getId(),
numberNFTs,
nevermined.keeper.nftUpgradeable.address,
conditionIdLockPayment[1],
artist,
true,
TransferNFTCondition.NO_EXPIRY,
)
const { state } = await conditionStoreManager.getCondition(conditionIdTransferNFT[1])
assert.equal(state, ConditionState.Fulfilled)
const nftBalanceArtistAfter = await nftUpgradeable.balance(artist.getId(), ddo.shortId())
const nftBalanceCollectorAfter = await nftUpgradeable.balance(
collector1.getId(),
ddo.shortId(),
)
assert.equal(nftBalanceArtistAfter, nftBalanceArtistBefore - numberNFTs)
assert.equal(nftBalanceCollectorAfter, nftBalanceCollectorBefore + numberNFTs)
})
it('the artist asks and receives the payment', async function () {
await escrowPaymentCondition.fulfill(
agreementId,
ddo.shortId(),
assetPrice.getAmounts(),
assetPrice.getReceivers(),
collector1.getId(),
escrowPaymentCondition.address,
ZeroAddress,
conditionIdLockPayment[1],
conditionIdTransferNFT[1],
artist,
)
const { state } = await conditionStoreManager.getCondition(conditionIdEscrow[1])
assert.equal(state, ConditionState.Fulfilled)
const escrowPaymentConditionBalance = await nevermined.accounts.getEtherBalance(
escrowPaymentCondition.address,
)
const receiver0Balance = await nevermined.accounts.getEtherBalance(receivers[0])
const receiver1Balance = await nevermined.accounts.getEtherBalance(receivers[1])
const receiver2Balance = await nevermined.accounts.getEtherBalance(receivers[2])
// for this assert we use a delta to account for the transaction fees
// of all the transactions from the artist
const delta = 10n ** 16n
assert.isTrue(receiver0Balance >= initialBalances.artist + amounts[0] - delta)
assert.equal(receiver1Balance, initialBalances.gallery + amounts[1])
assert.equal(receiver2Balance, initialBalances.governor + amounts[2])
assert.equal(escrowPaymentConditionBalance, initialBalances.escrowPaymentCondition)
})
})
describe('As an artist I want to give exclusive access to the collectors owning a specific NFT', async () => {
it('The collector sets up the NFT access agreement', async () => {
// Collector1: Create NFT access agreement
conditionIdNFTHolder = await nftHolderCondition.generateIdWithSeed(
agreementAccessId,
await nftHolderCondition.hashValues(ddo.id, collector1.getId(), numberNFTs),
)
conditionIdNFTAccess = await nftAccessCondition.generateIdWithSeed(
agreementAccessId,
await nftAccessCondition.hashValues(ddo.id, collector1.getId()),
)
const result = await nftAccessTemplate.createAgreement(
agreementAccessIdSeed,
ddo.shortId(),
[conditionIdNFTHolder[0], conditionIdNFTAccess[0]],
[0, 0],
[0, 0],
[collector1.getId()],
collector1,
)
assert.equal(result.status, 'success')
const logs = nftAccessTemplate.getTransactionLogs(result, 'AgreementCreated')
assert.isTrue(logs.length > 0)
assert.equal(
(await conditionStoreManager.getCondition(conditionIdNFTAccess[1])).state,
ConditionState.Unfulfilled,
)
assert.equal(
(await conditionStoreManager.getCondition(conditionIdNFTHolder[1])).state,
ConditionState.Unfulfilled,
)
})
it('The collector demonstrates it owns the NFT', async function () {
await nftHolderCondition.fulfill(
agreementAccessId,
ddo.shortId(),
collector1.getId(),
numberNFTs,
nevermined.keeper.nftUpgradeable.address,
collector1,
)
assert.equal(
(await conditionStoreManager.getCondition(conditionIdNFTHolder[1])).state,
ConditionState.Fulfilled,
)
})
it(' The artist gives access to the collector to the content', async function () {
await nftAccessCondition.fulfill(
agreementAccessId,
ddo.shortId(),
collector1.getId(),
artist,
)
assert.equal(
(await conditionStoreManager.getCondition(conditionIdNFTAccess[1])).state,
ConditionState.Fulfilled,
)
})
})
})
})