-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFT721Api.e2e.test.ts
207 lines (165 loc) Β· 6.8 KB
/
NFT721Api.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
import { assert } from 'chai'
import { decodeJwt, JWTPayload } from 'jose'
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 { Nft721Contract } from '../../src/keeper/contracts/Nft721Contract'
import { ContractHandler } from '../../src/keeper/ContractHandler'
import '../globals'
import { EscrowPaymentCondition, Token, TransferNFT721Condition } from '../../src/keeper/contracts'
describe('NFTs721 Api End-to-End', () => {
let artist: NvmAccount
let collector1: NvmAccount
let gallery: NvmAccount
let nevermined: Nevermined
let token: Token
let escrowPaymentCondition: EscrowPaymentCondition
let transferNft721Condition: TransferNFT721Condition
let ddo: DDO
const metadata = getMetadata()
let agreementId: string
// Configuration of First Sale:
// Artist -> Collector1, the gallery get a cut (25%)
let nftPrice = 20n
let amounts = [15n, 5n]
let receivers: string[]
let assetPrice1: AssetPrice
let initialBalances: any
let scale: bigint
let nft
let nftContract: Nft721Contract
let payload: JWTPayload
before(async () => {
nevermined = await Nevermined.getInstance(config)
;[, artist, collector1, , gallery] = nevermined.accounts.list()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABIArtifact(
'NFT721Upgradeable',
config.artifactsFolder,
networkName,
)
nft = await nevermined.utils.blockchain.deployAbi(erc721ABI, artist, [
artist.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)
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(artist)
await nevermined.services.marketplace.login(clientAssertion)
payload = decodeJwt(config.marketplaceAuthToken)
metadata.userId = payload.sub
// conditions
;({ escrowPaymentCondition, transferNft721Condition } = nevermined.keeper.conditions)
// components
;({ token } = nevermined.keeper)
scale = 10n ** BigInt(await token.decimals())
nftPrice = nftPrice * scale
amounts = amounts.map((v) => v * scale)
receivers = [artist.getId(), gallery.getId()]
assetPrice1 = new AssetPrice(
new Map([
[receivers[0], amounts[0]],
[receivers[1], amounts[1]],
]),
)
await nftContract.grantOperatorRole(transferNft721Condition.address, artist)
initialBalances = {
artist: await token.balanceOf(artist.getId()),
collector1: await token.balanceOf(collector1.getId()),
gallery: await token.balanceOf(gallery.getId()),
escrowPaymentCondition: Number(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 nftAttributes = NFTAttributes.getNFT721Instance({
metadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
nft: { nftTransfer: true },
},
{
serviceType: 'nft-access',
nft: { nftTransfer: true },
},
],
nftContractAddress: nftContract.address,
preMint: true,
})
ddo = await nevermined.nfts721.create(nftAttributes, artist)
assert.isDefined(ddo)
const owner = await nevermined.nfts721.ownerOfAsset(ddo.id)
assert.equal(owner, artist.getId())
})
it('should give operator role to Nevermined', async () => {
assert.isTrue(
await nevermined.nfts721.isOperatorOfDID(
ddo.id,
nevermined.keeper.conditions.transferNft721Condition.address,
),
)
})
})
describe('As a collector I want to buy some art', () => {
it('I check the details of the NFT', async () => {
const details = await nevermined.nfts1155.details(ddo.id)
assert.equal(details.owner, artist.getId())
})
it('I am ordering the NFT', async () => {
await nevermined.accounts.requestTokens(collector1, nftPrice / scale)
const collector1BalanceBefore = await token.balanceOf(collector1.getId())
assert.equal(initialBalances.collector1 + nftPrice, collector1BalanceBefore)
agreementId = await nevermined.nfts721.order(ddo.id, collector1)
console.log(`DID: ${ddo.id}`)
assert.isDefined(agreementId)
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
assert.equal(collector1BalanceAfter - initialBalances.collector1, 0n)
})
it('The artist can check the payment and transfer the NFT to the collector', async () => {
assert.equal(await nevermined.nfts721.ownerOfAsset(ddo.id), artist.getId())
const receipt = await nevermined.nfts721.transfer(agreementId, ddo.id, artist)
assert.isTrue(receipt)
assert.equal(await nevermined.nfts721.ownerOfAsset(ddo.id), collector1.getId())
})
it('the artist asks and receives the payment', async () => {
const escrowPaymentConditionBalanceBefore = await token.balanceOf(
escrowPaymentCondition.address,
)
const receipt = await nevermined.nfts721.releaseRewards(agreementId, ddo.id, 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())
assert.equal(initialBalances.artist + assetPrice1.getAmounts()[0], receiver0Balance)
assert.equal(initialBalances.gallery + assetPrice1.getAmounts()[1], receiver1Balance)
assert.equal(collectorBalance - initialBalances.collector1, 0n)
assert.equal(
escrowPaymentConditionBalanceBefore - assetPrice1.getTotalPrice(),
escrowPaymentConditionBalanceAfter,
)
})
})
describe('As an artist I want to give exclusive access to the collectors owning a specific NFT', () => {
it('The collector access the files to download', async () => {
const result = await nevermined.nfts1155.access(ddo.id, collector1, '/tmp/')
assert.isTrue(result)
})
})
})