-
Notifications
You must be signed in to change notification settings - Fork 1
/
Subscriptions.e2e.test.ts
350 lines (291 loc) Β· 12 KB
/
Subscriptions.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
// @ts-nocheck
import chai, { assert } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import config from '../../test/config'
import { decodeJwt, JWTPayload } from 'jose'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { NvmAccount } from '../../src/models/NvmAccount'
import { DDO } from '../../src/ddo/DDO'
import { EscrowPaymentCondition, TransferNFT721Condition, Token } from '../../src/keeper'
import { getMetadata } from '../utils'
import TestContractHandler from '../../test/keeper/TestContractHandler'
import { AssetPrice } from '../../src/models/AssetPrice'
import { getRoyaltyAttributes, RoyaltyAttributes } from '../../src/nevermined/api/AssetsApi'
import { MetaData } from '../../src/types/DDOTypes'
import { NFT721Api } from '../../src/nevermined/api/nfts/NFT721Api'
import { RoyaltyKind } from '../../src/types/MetadataTypes'
import { SubscriptionNFTApi } from '../../src/nevermined/api/nfts/SubscriptionNFTApi'
import { NFTAttributes } from '../../src/models/NFTAttributes'
import { jsonReplacer } from '../../src/common/helpers'
import { EventOptions } from '../../src/types/EventTypes'
import { didZeroX } from '../../src/utils/ConversionTypeHelpers'
import { getChecksumAddress } from '../../src/nevermined/utils/BlockchainViemUtils'
import { ZeroAddress } from '../../src/constants/AssetConstants'
chai.use(chaiAsPromised)
describe('Subscriptions using NFT ERC-721 End-to-End', () => {
let editor: NvmAccount
let subscriber: NvmAccount
let reseller: NvmAccount
let nevermined: Nevermined
let token: Token
let escrowPaymentCondition: EscrowPaymentCondition
let transferNft721Condition: TransferNFT721Condition
let subscriptionDDO: DDO
let assetDDO: DDO
let agreementId: string
// Configuration of First Sale:
// Editor -> Subscriber, the Reseller get a cut (25%)
let subscriptionPrice = 20n
let amounts = [15n, 5n]
let receivers: string[]
let assetPrice1: AssetPrice
let royaltyAttributes: RoyaltyAttributes
let subscriptionMetadata: MetaData
let assetMetadata: MetaData
const preMint = false
const royalties = 0
const nftTransfer = false
const subscriptionDuration = 1000 // in blocks
let initialBalances: any
let scale: bigint
let subscriptionNFT: NFT721Api
let neverminedNodeAddress
let payload: JWTPayload
before(async () => {
nevermined = await Nevermined.getInstance(config)
;[, editor, subscriber, , reseller] = nevermined.accounts.list()
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(editor)
await nevermined.services.marketplace.login(clientAssertion)
payload = decodeJwt(config.marketplaceAuthToken)
assetMetadata = getMetadata()
subscriptionMetadata = getMetadata(undefined, 'Subscription NFT')
subscriptionMetadata.main.type = 'subscription'
assetMetadata.userId = payload.sub
neverminedNodeAddress = await nevermined.services.node.getProviderAddress()
// conditions
;({ escrowPaymentCondition, transferNft721Condition } = nevermined.keeper.conditions)
// components
;({ token } = nevermined.keeper)
scale = 10n ** BigInt(await token.decimals())
subscriptionPrice = subscriptionPrice * scale
amounts = amounts.map((v) => v * scale)
receivers = [editor.getId(), reseller.getId()]
assetPrice1 = new AssetPrice(
new Map([
[receivers[0], amounts[0]],
[receivers[1], amounts[1]],
]),
).setTokenAddress(token.address)
royaltyAttributes = getRoyaltyAttributes(nevermined, RoyaltyKind.Standard, royalties)
initialBalances = {
editor: await token.balanceOf(editor.getId()),
subscriber: await token.balanceOf(subscriber.getId()),
reseller: await token.balanceOf(reseller.getId()),
escrowPaymentCondition: Number(await token.balanceOf(escrowPaymentCondition.address)),
}
})
describe('As an editor I want to register new content and provide a subscriptions to my content', () => {
it('I want to register a subscriptions NFT that gives access to exclusive contents to the holders', async () => {
// Deploy NFT
TestContractHandler.setConfig(config)
const contractABI = await TestContractHandler.getABIArtifact(
// 'NFT721SubscriptionUpgradeable',
`NFT721SubscriptionUpgradeable.${await nevermined.keeper.getNetworkName()}`,
'./artifacts/',
)
subscriptionNFT = await SubscriptionNFTApi.deployInstance(config, contractABI, editor, [
editor.getId(),
nevermined.keeper.didRegistry.address,
'Subscription NFT',
'',
'',
0,
nevermined.keeper.nvmConfig.address,
])
await nevermined.contracts.loadNft721Api(subscriptionNFT)
await subscriptionNFT.grantOperatorRole(transferNft721Condition.address, editor)
assert.equal(await subscriptionNFT.balanceOf(editor.getId()), 0n)
const nftAttributes = NFTAttributes.getSubscriptionInstance({
metadata: subscriptionMetadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
nft: { duration: subscriptionDuration, nftTransfer },
},
],
providers: [neverminedNodeAddress],
nftContractAddress: subscriptionNFT.address,
preMint,
royaltyAttributes: royaltyAttributes,
})
subscriptionDDO = await nevermined.nfts721.create(nftAttributes, editor)
assert.equal(await subscriptionNFT.balanceOf(editor.getId()), 0n)
assert.isDefined(subscriptionDDO)
})
it('should grant Nevermined the operator role', async () => {
assert.isTrue(
await nevermined.nfts721.isOperator(
subscriptionNFT.address,
nevermined.keeper.conditions.transferNft721Condition.address,
721,
),
)
})
it('I want to register a new asset and tokenize (via NFT)', async () => {
const nftAttributes = NFTAttributes.getSubscriptionInstance({
metadata: assetMetadata,
services: [
{
serviceType: 'nft-access',
nft: { duration: subscriptionDuration, nftTransfer },
},
],
providers: [neverminedNodeAddress],
nftContractAddress: subscriptionNFT.address,
preMint,
royaltyAttributes: royaltyAttributes,
})
assetDDO = await nevermined.nfts721.create(nftAttributes, editor)
assert.isDefined(assetDDO)
})
})
describe('As a subscriber I want to get access to some contents', () => {
it('I check the details of the subscription NFT', async () => {
const details = await nevermined.nfts721.details(subscriptionDDO.id)
assert.equal(details.owner, editor.getId())
})
it('I am ordering the subscription NFT', async () => {
await nevermined.accounts.requestTokens(subscriber, subscriptionPrice / scale)
const subscriberBalanceBefore = await token.balanceOf(subscriber.getId())
assert.equal(subscriberBalanceBefore, initialBalances.subscriber + subscriptionPrice)
agreementId = await nevermined.nfts721.order(subscriptionDDO.id, subscriber)
assert.isDefined(agreementId)
const subscriberBalanceAfter = await token.balanceOf(subscriber.getId())
assert.equal(subscriberBalanceAfter, initialBalances.subscriber)
})
it('The seller can check the payment and transfer the NFT to the subscriber', async () => {
// Let's use the Node to mint the subscription and release the payments
assert.equal(await subscriptionNFT.balanceOf(subscriber.getId()), 0n)
const receipt = await nevermined.nfts721.claim(
agreementId,
editor.getId(),
subscriber.getId(),
subscriptionDDO.id,
)
assert.isTrue(receipt)
assert.equal(
await nevermined.nfts721.ownerOfAssetByAgreement(subscriptionDDO.shortId(), agreementId),
subscriber.getId(),
)
const minted = await subscriptionNFT.getContract.getMintedEntries(subscriber.getId())
console.log(`Minted: ${JSON.stringify(minted, jsonReplacer)}`)
assert.equal(await subscriptionNFT.balanceOf(subscriber.getId()), 1n)
})
it('the editor and reseller can receive their payment', async () => {
const receiver0Balance = await token.balanceOf(assetPrice1.getReceivers()[0])
const receiver1Balance = await token.balanceOf(assetPrice1.getReceivers()[1])
assert.equal(receiver0Balance, initialBalances.editor + assetPrice1.getAmounts()[0])
assert.equal(receiver1Balance, initialBalances.reseller + assetPrice1.getAmounts()[1])
})
it('the subscription can be checked on chain', async () => {
const eventOptions: EventOptions = {
eventName: 'Fulfilled',
filterSubgraph: {
where: {
_did: didZeroX(subscriptionDDO.id),
_receiver: subscriber.getId(),
},
},
filterJsonRpc: {
_did: didZeroX(subscriptionDDO.id),
_receiver: subscriber.getId(),
},
result: {
_agreementId: true,
_did: true,
_receiver: true,
},
}
// wait for the event to be picked by the subgraph
await nevermined.keeper.conditions.transferNft721Condition.events.once((e) => e, eventOptions)
const [event] =
await nevermined.keeper.conditions.transferNft721Condition.events.getPastEvents(
eventOptions,
)
// subgraph event or json-rpc event?
const eventValues = event.args || event
assert.equal(eventValues._agreementId, agreementId)
assert.equal(eventValues._did, didZeroX(subscriptionDDO.id))
// thegraph stores the addresses in lower case
assert.equal(getChecksumAddress(eventValues._receiver), subscriber.getId())
})
})
describe('As subscriber I want to get access to assets include as part of my subscription', () => {
it('The Subscriber should have an NFT balance', async () => {
const balance = await subscriptionNFT.balanceOf(subscriber.getId())
assert.equal(balance, 1n)
})
it('The collector access the files', async () => {
const result = await nevermined.nfts721.access(
assetDDO.id,
subscriber,
'/tmp/',
undefined,
agreementId,
)
assert.isTrue(result)
})
})
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 () => {
// Deploy NFT
TestContractHandler.setConfig(config)
const contractABI = await TestContractHandler.getABIArtifact(
'NFT721SubscriptionUpgradeable',
'./test/resources/artifacts/',
)
subscriptionNFT = await SubscriptionNFTApi.deployInstance(config, contractABI, editor, [
editor.getId(),
nevermined.keeper.didRegistry.address,
'Subscription NFT',
'',
'',
0,
ZeroAddress,
])
await nevermined.contracts.loadNft721Api(subscriptionNFT)
const nftAttributes = NFTAttributes.getSubscriptionInstance({
metadata: getMetadata(),
services: [
{
serviceType: 'nft-sales',
price: new AssetPrice(editor.getId(), 0n),
nft: { duration: subscriptionDuration, nftTransfer },
},
],
providers: [neverminedNodeAddress],
nftContractAddress: subscriptionNFT.address,
preMint,
royaltyAttributes: royaltyAttributes,
})
subscriptionDDO = await nevermined.nfts721.create(nftAttributes, editor)
assert.isDefined(subscriptionDDO)
})
it('subscriber should be able to order the nft', async () => {
agreementId = await nevermined.nfts721.order(subscriptionDDO.id, subscriber)
assert.isDefined(agreementId)
})
it('nevermined should not allow the subscriber to claim through the node', async () => {
await assert.isRejected(
nevermined.nfts721.claim(
agreementId,
editor.getId(),
subscriber.getId(),
subscriptionDDO.id,
),
/Nevermined does not have operator role/,
)
})
})
})