-
Notifications
You must be signed in to change notification settings - Fork 1
/
NFT721.test.ts
228 lines (185 loc) Β· 7.42 KB
/
NFT721.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
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 { parseEther } from '../../src/nevermined/utils/BlockchainViemUtils'
import { ZeroAddress } from '../../src/constants/AssetConstants'
import { Nft721Contract } from '../../src/keeper/contracts/Nft721Contract'
import { TransferNFT721Condition } from '../../src/keeper/contracts/conditions/NFTs/TransferNFT721Condition'
import { TokenUtils } from '../../src/nevermined/Token'
import { ContractHandler } from '../../src/keeper/ContractHandler'
import { generateId } from '../../src/common/helpers'
import { zeroX } from '../../src/utils/ConversionTypeHelpers'
describe('Nfts721 operations', async () => {
let nevermined: Nevermined
let transferNft721Condition: TransferNFT721Condition
let nft
let nftContract: Nft721Contract
let deployer: NvmAccount
let artist: NvmAccount
let collector: NvmAccount
let ddo: DDO
let token: TokenUtils
let payload: JWTPayload
before(async () => {
nevermined = await Nevermined.getInstance(config)
// Accounts
;[deployer, artist, collector] = nevermined.accounts.list()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABIArtifact(
'NFT721Upgradeable',
config.artifactsFolder,
networkName,
)
// deploy a nft contract we can use
nft = await nevermined.utils.blockchain.deployAbi(erc721ABI, deployer, [
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)
;({ transferNft721Condition } = nevermined.keeper.conditions)
const nftOwner = NvmAccount.fromAddress((await nftContract.owner()) as `0x${string}`)
nftContract.grantOperatorRole(transferNft721Condition.address, nftOwner)
await nevermined.services.marketplace.login(clientAssertion)
payload = decodeJwt(config.marketplaceAuthToken)
;({ token } = nevermined.utils)
})
describe('with default token', async () => {
before(async () => {
const metadata = getMetadata()
metadata.userId = payload.sub
const nftAttributes = NFTAttributes.getNFT721Instance({
metadata,
services: [
{
serviceType: 'nft-sales',
nft: { nftTransfer: true },
},
{
serviceType: 'nft-access',
},
],
nftContractAddress: await nft.address,
preMint: true,
})
assert.equal(nftAttributes.fulfillAccessTimelock, 0)
ddo = await nevermined.nfts721.create(nftAttributes, artist)
})
it('should clone an existing erc-721 nft contract', async () => {
const cloneAddress = await nftContract.createClone('My New NFT', 'xyz', '', 10n, [], artist)
assert.isDefined(cloneAddress)
console.log(`NFT (ERC-721) cloned into address ${cloneAddress}`)
})
it('should mint and burn a nft token', async () => {
// artist mints the nft
const tokenId = generateId()
await nftContract.mint(zeroX(tokenId), artist)
await nftContract.burn(zeroX(tokenId), artist)
})
it('should transfer an nft token with default token', async () => {
console.log(`Checking owner of DID ${ddo.id}`)
assert.equal(await nevermined.nfts721.ownerOfAsset(zeroX(ddo.shortId())), artist.getId())
assert.equal(await nevermined.nfts721.balanceOf(collector.getId()), 0n)
// collector orders the nft
const agreementId = await nevermined.nfts721.order(ddo.id, collector)
// artists sends the nft
await nevermined.nfts721.transfer(agreementId, ddo.id, artist)
assert.equal(await nevermined.nfts721.ownerOfAsset(zeroX(ddo.shortId())), collector.getId())
assert.equal(await nevermined.nfts721.balanceOf(collector.getId()), 1n)
// artist fetches the payment
await nevermined.nfts721.releaseRewards(agreementId, ddo.id, artist)
})
})
describe('with custom token', async () => {
before(async () => {
const metadata = getMetadata()
metadata.userId = payload.sub
// artist creates the nft
const nftAttributes = NFTAttributes.getNFT721Instance({
metadata,
services: [
{
serviceType: 'nft-sales',
price: new AssetPrice().setTokenAddress(token.getAddress()),
nft: { nftTransfer: true },
},
{
serviceType: 'nft-access',
},
],
nftContractAddress: await nft.address,
preMint: false,
})
ddo = await nevermined.nfts721.create(nftAttributes, artist)
})
it('should mint an nft token', async () => {
// artist mints the nft
await nftContract.mint(zeroX(ddo.shortId()), artist)
})
it('should transfer an nft token with custom token', async () => {
assert.equal(await nevermined.nfts721.ownerOfAsset(zeroX(ddo.shortId())), artist.getId())
// collector orders the nft
const agreementId = await nevermined.nfts721.order(ddo.id, collector)
// artists sends the nft
await nevermined.nfts721.transfer(agreementId, ddo.id, artist)
assert.equal(await nevermined.nfts721.ownerOfAsset(zeroX(ddo.shortId())), collector.getId())
// artist fetches the payment
await nevermined.nfts721.releaseRewards(agreementId, ddo.id, artist)
})
})
describe('with ether', async () => {
before(async () => {
const metadata = getMetadata()
metadata.userId = payload.sub
// artist creates the nft
const assetPrice = new AssetPrice(artist.getId(), parseEther('0.1')).setTokenAddress(
ZeroAddress,
) // With ETH
const nftAttributes = NFTAttributes.getNFT721Instance({
metadata,
services: [
{
serviceType: 'nft-sales',
price: assetPrice,
nft: { nftTransfer: true },
},
{
serviceType: 'nft-access',
},
],
nftContractAddress: await nft.address,
preMint: false,
})
ddo = await nevermined.nfts721.create(nftAttributes, artist)
})
it('should mint an nft token', async () => {
// artist mints the nft
await nftContract.mint(zeroX(ddo.shortId()), artist)
})
it('should transfer an nft token with ether', async () => {
assert.equal(await nevermined.nfts721.ownerOfAsset(zeroX(ddo.shortId())), artist.getId())
// collector orders the nft
const agreementId = await nevermined.nfts721.order(ddo.id, collector)
// artists sends the nft
await nevermined.nfts721.transfer(agreementId, ddo.id, artist)
assert.equal(await nevermined.nfts721.ownerOfAsset(zeroX(ddo.shortId())), collector.getId())
// artist fetches the payment
await nevermined.nfts721.releaseRewards(agreementId, ddo.id, artist)
})
})
})