-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFTsWithMultipleServices.test.ts
249 lines (196 loc) Β· 7.6 KB
/
NFTsWithMultipleServices.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
import { assert } from 'chai'
import { decodeJwt, JWTPayload } from 'jose'
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 { NFTAttributes } from '../../src/models/NFTAttributes'
import { Token } from '../../src/keeper/contracts/Token'
import { ConditionState } from '../../src/types/ContractTypes'
import { ContractHandler } from '../../src/keeper/ContractHandler'
import { Nft721Contract } from '../../src/keeper/contracts/Nft721Contract'
import { MetaData } from '../../src/types/DDOTypes'
import { repeat } from '../utils/utils'
let nevermined: Nevermined
let publisher: NvmAccount
let collector1: NvmAccount
let metadata: MetaData
let assetPrice1: AssetPrice
let assetPrice2: AssetPrice
let payload: JWTPayload
let ddo: DDO
let token: Token
let service
let agreementId
let neverminedNodeAddress
let nft
let nftContract: Nft721Contract
let salesServices
const totalAmount1 = '100'
const totalAmount2 = '150'
describe('E2E Flow for NFTs with multiple services', () => {
before(async () => {
nevermined = await Nevermined.getInstance(config)
;({ token } = nevermined.keeper)
// Accounts
;[publisher, collector1] = nevermined.accounts.list()
neverminedNodeAddress = await nevermined.services.node.getProviderAddress()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABIArtifact(
'NFT721Upgradeable',
config.artifactsFolder,
networkName,
)
nft = await nevermined.utils.blockchain.deployAbi(erc721ABI, publisher, [
publisher.getId(),
nevermined.keeper.didRegistry.address,
'NFT721',
'NVM',
'',
'0',
nevermined.keeper.nvmConfig.address,
])
nftContract = await Nft721Contract.getInstance(
(nevermined.keeper as any).instanceConfig,
await nft.address,
)
await nevermined.contracts.loadNft721(nftContract.address)
await nftContract.grantOperatorRole(
nevermined.keeper.conditions.transferNft721Condition.address,
publisher,
)
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(publisher)
await nevermined.services.marketplace.login(clientAssertion)
payload = decodeJwt(config.marketplaceAuthToken)
assetPrice1 = new AssetPrice(publisher.getId(), BigInt(totalAmount1))
assetPrice2 = new AssetPrice(publisher.getId(), BigInt(totalAmount2))
try {
await nevermined.accounts.requestTokens(collector1, BigInt(totalAmount1) * 10n)
} catch (error) {
console.error(error)
}
metadata = getMetadata()
metadata.userId = payload.sub
})
describe('Asset with multiple NFTs registration', () => {
it('Register with multiple access services', async () => {
const nftAttributes = NFTAttributes.getNFT721Instance({
metadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
nft: { nftTransfer: true },
},
{
serviceType: 'nft-sales',
price: assetPrice2,
nft: { nftTransfer: false },
},
{
serviceType: 'nft-access',
},
],
nftContractAddress: nftContract.address,
providers: [neverminedNodeAddress],
})
ddo = await nevermined.nfts721.create(nftAttributes, publisher)
assert.isDefined(ddo)
console.log(ddo.id)
const salesServices = ddo.getServicesByType('nft-sales')
assert.equal(salesServices.length, 2)
const accessServices = ddo.getServicesByType('nft-access')
assert.equal(accessServices.length, 1)
assert.equal(
DDO.getParameterFromCondition(salesServices[0], 'lockPayment', '_amounts')[0],
totalAmount1,
)
assert.equal(
DDO.getParameterFromCondition(salesServices[0], 'transferNFT', '_nftTransfer'),
'true',
)
assert.equal(
DDO.getParameterFromCondition(salesServices[1], 'lockPayment', '_amounts')[0],
totalAmount2,
)
assert.equal(
DDO.getParameterFromCondition(salesServices[1], 'transferNFT', '_nftTransfer'),
'false',
)
assert.isDefined(ddo.findServiceByType('metadata'))
})
it('The collector is ordering the NFT between multiple nft-sales services', async () => {
const collector1BalanceBefore = await token.balanceOf(collector1.getId())
salesServices = ddo.getServicesByType('nft-sales')
service = salesServices[1]
agreementId = await nevermined.nfts721.order(ddo.id, collector1, service.index)
assert.isDefined(agreementId)
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
const price = DDO.getAssetPriceFromService(service)
assert.isTrue(collector1BalanceBefore - price.getTotalPrice() === collector1BalanceAfter)
const status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
transferNFT: ConditionState.Unfulfilled,
escrowPayment: ConditionState.Unfulfilled,
})
})
it('The publisher can check the payment and transfer the NFT to the collector', async () => {
service = salesServices[1]
const receipt = await nevermined.nfts721.transfer(
agreementId,
ddo.id,
publisher,
service.index,
)
assert.isTrue(receipt)
const status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
transferNFT: ConditionState.Fulfilled,
escrowPayment: ConditionState.Unfulfilled,
})
})
it('The publisher can get the rewards', async () => {
service = salesServices[1]
const receipt = await nevermined.nfts721.releaseRewards(
agreementId,
ddo.id,
publisher,
service.index,
)
assert.isTrue(receipt)
const status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
transferNFT: ConditionState.Fulfilled,
escrowPayment: ConditionState.Fulfilled,
})
})
it('The collector can order the other service too', async () => {
const collector1BalanceBefore = await token.balanceOf(collector1.getId())
service = salesServices[0]
agreementId = await nevermined.nfts721.order(ddo.id, collector1, service.index)
assert.isDefined(agreementId)
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
const price = DDO.getAssetPriceFromService(service)
assert.isTrue(collector1BalanceBefore - price.getTotalPrice() === collector1BalanceAfter)
let status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
transferNFT: ConditionState.Unfulfilled,
escrowPayment: ConditionState.Unfulfilled,
})
await nevermined.nfts721.transfer(agreementId, ddo.id, publisher)
await nevermined.nfts721.releaseRewards(agreementId, ddo.id, publisher)
status = await repeat(3, nevermined.agreements.status(agreementId))
assert.deepEqual(status, {
lockPayment: ConditionState.Fulfilled,
transferNFT: ConditionState.Fulfilled,
escrowPayment: ConditionState.Fulfilled,
})
})
})
})