generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanageNft.ts
227 lines (185 loc) · 6.4 KB
/
manageNft.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
import { LocalSigningManager } from '@polymeshassociation/local-signing-manager';
import { BigNumber, Polymesh } from '@polymeshassociation/polymesh-sdk';
import { Nft } from '@polymeshassociation/polymesh-sdk/internal';
import {
DefaultPortfolio,
Identity,
Instruction,
KnownNftType,
MetadataType,
NftCollection,
NumberedPortfolio,
Venue,
VenueType,
} from '@polymeshassociation/polymesh-sdk/types';
import { TestFactory } from '~/helpers';
import { createNftCollection } from '~/sdk/assets/createNftCollection';
import { awaitMiddlewareSynced } from '~/util';
let factory: TestFactory;
describe('manageNft', () => {
let ticker: string;
let sdk: Polymesh;
let collection: NftCollection;
let nft: Nft;
let venue: Venue;
let holder: Identity;
let portfolio: NumberedPortfolio;
let defaultPortfolio: DefaultPortfolio;
let receiver: Identity;
let receiverAddress: string;
let instruction: Instruction;
beforeAll(async () => {
factory = await TestFactory.create({});
sdk = factory.polymeshSdk;
ticker = factory.nextTicker();
collection = await createNftCollection(sdk, {
ticker,
nftType: KnownNftType.Derivative,
collectionKeys: [
{ type: MetadataType.Local, name: 'img', spec: { url: 'https://example.com/nft/{id}' } },
{ type: MetadataType.Local, name: 'imgHash', spec: { description: 'SHA256' } },
],
});
const targetMnemonic = LocalSigningManager.generateAccount();
receiverAddress = factory.signingManager.addAccount({ mnemonic: targetMnemonic });
const {
results: [{ did: receiverDid }],
} = await factory.createIdentityForAddresses([receiverAddress]);
receiver = await sdk.identities.getIdentity({ did: receiverDid });
const portfolioTx = await sdk.identities.createPortfolio({ name: 'NFT portfolio' });
const venueTx = await sdk.settlements.createVenue({
description: 'test exchange',
type: VenueType.Exchange,
});
const pauseTx = await collection.compliance.requirements.pause();
const batchTx = await sdk.createTransactionBatch({
transactions: [portfolioTx, venueTx, pauseTx] as const,
});
[portfolio, venue] = await batchTx.run();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
holder = (await sdk.getSigningIdentity())!;
defaultPortfolio = await holder.portfolios.getPortfolio();
});
afterAll(async () => {
await factory.close();
});
it('should be defined', async () => {
const details = await collection.details();
expect(details.ticker).toEqual(ticker);
});
it('should return the needed metadata', async () => {
const requiredMetadata = await collection.collectionKeys();
expect(requiredMetadata).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: 'img', type: MetadataType.Local }),
expect.objectContaining({ name: 'imgHash', type: MetadataType.Local }),
])
);
});
it('should issue an Nft', async () => {
const issueTx = await collection.issue({
metadata: [
{ type: MetadataType.Local, id: new BigNumber(1), value: 'https://example.com/nft/1' },
{
type: MetadataType.Local,
id: new BigNumber(2),
value: '0x35987a0f9ae77012a5146a982966661b75cdeaa4161d1d62b1e18d39438e7396',
},
],
});
nft = await issueTx.run();
expect(nft.id).toEqual(new BigNumber(1));
});
it('should allow holder to transfer NFTs between portfolios', async () => {
const moveTx = await defaultPortfolio.moveFunds({
to: portfolio,
items: [
{
asset: collection,
nfts: [nft],
},
],
});
await moveTx.run();
const [defaultCollections, portfolioCollections] = await Promise.all([
defaultPortfolio.getCollections(),
portfolio.getCollections(),
]);
expect(defaultCollections).toEqual([]);
expect(portfolioCollections).toEqual(
expect.arrayContaining([
expect.objectContaining({
collection: expect.objectContaining({ id: collection.id }),
total: new BigNumber(1),
}),
])
);
});
it('should allow the holder to check if they can transfer the NFT', async () => {
const { result } = await collection.settlements.canTransfer({
from: portfolio,
to: receiver,
nfts: [nft],
});
expect(result).toEqual(true);
});
it('should let the holder send instructions with an NFT', async () => {
const instructionTx = await sdk.settlements.addInstruction({
venueId: venue.id,
legs: [
{
from: portfolio,
to: receiver,
asset: collection,
nfts: [nft],
},
],
});
instruction = await instructionTx.run();
await awaitMiddlewareSynced(instructionTx, sdk);
});
it('should return legs for an instruction when they contain an NFT', async () => {
expect(instruction).toBeDefined();
const { data: legs } = await instruction.getLegs();
expect(legs).toEqual(
expect.arrayContaining([
expect.objectContaining({
asset: expect.objectContaining({ id: collection.id }),
nfts: expect.arrayContaining([expect.objectContaining({ id: nft.id })]),
}),
])
);
});
it('should allow the receiver to accept an NFT settlement', async () => {
const affirmTx = await instruction.affirm({}, { signingAccount: receiverAddress });
await affirmTx.run();
const receiverPortfolio = await receiver.portfolios.getPortfolio();
const receiverCollections = await receiverPortfolio.getCollections();
expect(receiverCollections).toEqual(
expect.arrayContaining([
expect.objectContaining({
collection: expect.objectContaining({
id: collection.id,
}),
free: expect.arrayContaining([expect.objectContaining({ id: nft.id })]),
total: new BigNumber(1),
}),
])
);
});
it('should allow the issuer to redeem an NFT', async () => {
const createNftTx = await collection.issue({
metadata: [
{ type: MetadataType.Local, id: new BigNumber(1), value: 'https://example.com/nft/1' },
{
type: MetadataType.Local,
id: new BigNumber(2),
value: '0x1234',
},
],
});
const redeemNft = await createNftTx.run();
const redeemTx = await redeemNft.redeem();
expect(redeemTx.run()).resolves.not.toThrow();
});
});