-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFT.test.ts
173 lines (143 loc) Β· 5.95 KB
/
NFT.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
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 { getRoyaltyAttributes, RoyaltyAttributes } from '../../src/nevermined/api/AssetsApi'
import { getMetadata } from '../utils/ddo-metadata-generator'
import { RoyaltyKind } from '../../src/types/MetadataTypes'
import { NFTAttributes } from '../../src/models/NFTAttributes'
import { parseEther } from '../../src/nevermined/utils/BlockchainViemUtils'
import { ZeroAddress } from '../../src/constants/AssetConstants'
describe('Nfts operations', () => {
let nevermined: Nevermined
let artist: NvmAccount
let collector: NvmAccount
let ddo: DDO
let payload: JWTPayload
let royaltyAttributes: RoyaltyAttributes
before(async () => {
nevermined = await Nevermined.getInstance(config)
// Accounts
;[artist, collector] = nevermined.accounts.list()
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(artist)
await nevermined.services.marketplace.login(clientAssertion)
payload = decodeJwt(config.marketplaceAuthToken)
})
describe('with default token', async () => {
before(async () => {
const metadata = getMetadata()
metadata.userId = payload.sub
royaltyAttributes = getRoyaltyAttributes(nevermined, RoyaltyKind.Standard, 0)
const nftAttributes = NFTAttributes.getNFT1155Instance({
metadata,
services: [
{ serviceType: 'nft-sales', nft: { amount: 2n, nftTransfer: true } },
{ serviceType: 'nft-access' },
],
nftContractAddress: nevermined.nfts1155.nftContract.address,
cap: 10n,
royaltyAttributes,
preMint: true,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist)
})
it('nft contract address is correct', async () => {
assert.equal(
nevermined.assets.getNftContractAddress(ddo),
nevermined.keeper.nftUpgradeable.address,
)
})
it('artist should have balance because are pre-minted', async () => {
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, artist)) === 10n)
})
it('should transfer 2 nft tokens with default token', async () => {
const agreementId = await nevermined.nfts1155.order(ddo.id, 2n, collector)
await nevermined.nfts1155.transfer(agreementId, ddo.id, 2n, artist)
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, artist)) === 8n)
console.log(`After Assert Balance`)
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, collector)) === 2n)
})
it('should the operation be approved', async () => {
await nevermined.nfts1155.setApprovalForAll(config.neverminedNodeAddress, true, artist)
const isApproved = await nevermined.nfts1155.isApprovedForAll(
config.neverminedNodeAddress,
artist.getId(),
)
assert.equal(Boolean(isApproved), true)
})
it('should burn nft tokens', async () => {
await nevermined.nfts1155.burn(ddo.id, 6n, artist)
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, artist)) === 2n)
})
})
describe('with custom token', async () => {
before(async () => {
const metadata = getMetadata()
metadata.userId = payload.sub
const nftAttributes = NFTAttributes.getNFT1155Instance({
metadata,
services: [
{ serviceType: 'nft-sales', nft: { nftTransfer: true } },
{ serviceType: 'nft-access' },
],
nftContractAddress: nevermined.nfts1155.nftContract.address,
cap: 10n,
preMint: true,
royaltyAttributes,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist)
})
it('should mint 10 nft tokens', async () => {
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, artist)) === 10n)
})
it('should transfer 2 nft tokens with custom token', async () => {
const agreementId = await nevermined.nfts1155.order(ddo.id, 2n, collector)
await nevermined.nfts1155.transfer(agreementId, ddo.id, 2n, artist)
assert.equal(await nevermined.nfts1155.balance(ddo.id, artist), 8n)
assert.equal(await nevermined.nfts1155.balance(ddo.id, collector), 2n)
})
it('should burn nft tokens', async () => {
await nevermined.nfts1155.burn(ddo.id, 6n, artist)
assert.deepEqual(await nevermined.nfts1155.balance(ddo.id, artist), 2n)
})
})
describe('with ether', async () => {
before(async () => {
const metadata = getMetadata()
metadata.userId = payload.sub
const nftAttributes = NFTAttributes.getNFT1155Instance({
metadata,
services: [
{
serviceType: 'nft-sales',
price: new AssetPrice(artist.getId(), parseEther('0.1')).setTokenAddress(ZeroAddress),
nft: { nftTransfer: true },
},
{
serviceType: 'nft-access',
},
],
nftContractAddress: nevermined.nfts1155.nftContract.address,
cap: 10n,
royaltyAttributes,
})
ddo = await nevermined.nfts1155.create(nftAttributes, artist)
})
it('should mint 10 nft tokens', async () => {
assert.equal(await nevermined.nfts1155.balance(ddo.id, artist), 10n)
})
it('should transfer 2 nft tokens with ether', async () => {
const agreementId = await nevermined.nfts1155.order(ddo.id, 2n, collector)
await nevermined.nfts1155.transfer(agreementId, ddo.id, 2n, artist)
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, artist)) === 8n)
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, collector)) === 2n)
})
it('should burn nft tokens', async () => {
await nevermined.nfts1155.burn(ddo.id, 6n, artist)
assert.isTrue((await nevermined.nfts1155.balance(ddo.id, artist)) === 2n)
})
})
})