-
Notifications
You must be signed in to change notification settings - Fork 12
/
multiasset.spec.ts
353 lines (325 loc) · 11.5 KB
/
multiasset.spec.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
351
352
353
import { expect, use } from "chai";
import chaiAsPromised from "chai-as-promised";
import { KeyringPair } from "@polkadot/keyring/types";
import { ApiPromise, WsProvider, Keyring } from "@polkadot/api";
import BN from "bn.js";
import Rmrk_factory from "../types/constructors/rmrk_example_equippable_lazy";
import Rmrk from "../types/contracts/rmrk_example_equippable_lazy";
import Catalog_Factory from "../types/constructors/catalog_example";
import Contract from "../types/contracts/catalog_example";
import { emit } from "./helper";
use(chaiAsPromised);
const MAX_SUPPLY = 888;
const BASE_URI = "ipfs://tokenUriPrefix/";
const COLLECTION_METADATA = "ipfs://collectionMetadata/data.json";
const CATALOG_METADATA = "ipfs://catalogMetadata/data.json";
const ONE = new BN(10).pow(new BN(18));
const PRICE_PER_MINT = ONE;
const ADMIN_ROLE = 0;
// Create a new instance of contract
const wsProvider = new WsProvider("ws://127.0.0.1:9944");
// Create a keyring instance
const keyring = new Keyring({ type: "sr25519" });
describe("RMRK Multi Asset tests", () => {
let kanariaFactory: Rmrk_factory;
let gemFactory: Rmrk_factory;
let catalogFactory: Catalog_Factory;
let api: ApiPromise;
let deployer: KeyringPair;
let bob: KeyringPair;
let dave: KeyringPair;
let kanaria: Rmrk;
let gem: Rmrk;
let catalog: Contract;
beforeEach(async function(): Promise<void> {
api = await ApiPromise.create({ provider: wsProvider, noInitWarn: true });
deployer = keyring.addFromUri("//Alice");
bob = keyring.addFromUri("//Bob");
dave = keyring.addFromUri("//Dave");
kanariaFactory = new Rmrk_factory(api, deployer);
kanaria = new Rmrk(
(
await kanariaFactory.new(
["Kanaria"],
["KAN"],
[BASE_URI],
MAX_SUPPLY,
PRICE_PER_MINT,
[COLLECTION_METADATA],
deployer.address,
10
)
).address,
deployer,
api
);
gemFactory = new Rmrk_factory(api, deployer);
gem = new Rmrk(
(
await gemFactory.new(
["Gem"],
["GM"],
[BASE_URI],
MAX_SUPPLY,
PRICE_PER_MINT,
[COLLECTION_METADATA],
dave.address,
100
)
).address,
deployer,
api
);
catalogFactory = new Catalog_Factory(api, deployer);
catalog = new Contract(
(
await catalogFactory.new([CATALOG_METADATA])
).address,
deployer,
api
);
});
it("Init two rmrk contracts works", async () => {
expect(
(await kanaria.query.totalSupply()).value.unwrap().toNumber()
).to.equal(0);
expect(
(await kanaria.query.hasRole(ADMIN_ROLE, deployer.address)).value.ok
).to.equal(true);
expect((await kanaria.query.maxSupply()).value.unwrap()).to.equal(MAX_SUPPLY);
expect((await kanaria.query.price()).value.unwrap().toString()).to.equal(
PRICE_PER_MINT.toString()
);
const kanariaCollectionId = (await kanaria.query.collectionId()).value;
expect((await gem.query.totalSupply()).value.unwrap().toNumber()).to.equal(
0
);
expect(
(await gem.query.hasRole(ADMIN_ROLE, deployer.address)).value.ok
).to.equal(true);
expect((await gem.query.maxSupply()).value.unwrap()).to.equal(MAX_SUPPLY);
expect((await gem.query.price()).value.unwrap().toString()).to.equal(
PRICE_PER_MINT.toString()
);
const gemCollectionId = (await gem.query.collectionId()).value.ok;
expect(kanariaCollectionId).to.not.be.equal(gemCollectionId);
});
it("Add assets to token and approve them", async () => {
// This test follows MergesEquippable user story, but without Equipalble tests.
// https://github.com/rmrk-team/evm-sample-contracts/tree/master/contracts/MergedEquippable
// The scenarrio is different only when it comes to procedure of nesting child tokens,
// but the end result is the same.
// First Bob mints tokens from kanaria and gem contracts.
// After Deployer (contract owner) adds new assets to gem and kanaria contracts, the same deployer will
// add those assets to the tokens.
// Bob accepts new assets on all of his tokens (both kanaria and gem tokens)
// Bob addds gem tokens (children) to kanaria tokens (parent)
// Equipping is covered in merged_equippable.spec.ts
const assetDefaultId = 1;
const assetComposedId = 2;
// bob mints 5 kanaria
let kanariaMintResult = await kanaria.withSigner(bob).tx.mintMany(5, {
value: PRICE_PER_MINT.muln(5),
});
emit(kanariaMintResult, "Transfer", {
from: null,
to: bob.address,
id: { u64: 1 },
});
// bob mints 15 gem
for (let i = 1; i < 16; i++) {
const gemMintResult = await gem.withSigner(bob).tx.mint({
value: PRICE_PER_MINT,
});
emit(gemMintResult, "Transfer", {
from: null,
to: bob.address,
id: { u64: i },
});
}
expect((await gem.query.balanceOf(bob.address)).value.unwrap()).to.equal(15);
// deployer adds two asset entries for kanaria
const addAssetResult = await kanaria
.withSigner(deployer)
.tx.addAssetEntry(catalog.address, assetDefaultId, "1", ["ipfs://default.png"], [0])
emit(addAssetResult, "AssetSet", { asset: 1 });
const addAssetResult2 = await kanaria
.withSigner(deployer)
.tx.addAssetEntry(
catalog.address,
assetComposedId,
"1",
["ipfs://meta1.json"],
[1, 3, 5, 7, 9, 10, 11],
);
emit(addAssetResult2, "AssetSet", { asset: 2 });
expect(
(await kanaria.withSigner(deployer).query.totalAssets())?.value.unwrap().toString()
).to.be.equal("2");
// Deployer adds both assets to token 1
await addAssetToToken(kanaria, deployer, 1, assetDefaultId)
await addAssetToToken(kanaria, deployer, 1, assetComposedId)
expect(
(await kanaria.query.totalTokenAssets({ u64: 1 }))?.value.unwrap().ok.toString()
).to.be.equal("0,2");
// bob accepts both assets
await acceptAsset(kanaria, bob, 1, assetDefaultId);
await acceptAsset(kanaria, bob, 1, assetComposedId);
expect(
(await kanaria.query.totalTokenAssets({ u64: 1 }))?.value.unwrap().ok.toString()
).to.be.equal("2,0");
// We'll add 4 assets for each gem, a full version and 3 versions matching each slot.
// We will have only 2 types of gems -> 4x2: 8 assets.
// This is not composed by others, so fixed and slot parts are never used.
const equippableRefIdLeftGem = 1;
const equippableRefIdMidGem = 2;
const equippableRefIdRightGem = 3;
await gem
.withSigner(deployer)
.tx.addAssetEntry(catalog.address, 1, 0, ["ipfs://gems/typeA/full.svg"], [0]);
await gem
.withSigner(deployer)
.tx.addAssetEntry(
catalog.address,
2,
equippableRefIdLeftGem,
["ipfs://gems/typeA/left.svg"],
[0],
);
await gem
.withSigner(deployer)
.tx.addAssetEntry(
catalog.address,
3,
equippableRefIdMidGem,
["ipfs://gems/typeA/mid.svg"],
[0],
);
await gem
.withSigner(deployer)
.tx.addAssetEntry(
catalog.address,
4,
equippableRefIdRightGem,
["ipfs://gems/typeA/right.svg"],
[0],
);
await gem
.withSigner(deployer)
.tx.addAssetEntry(catalog.address, 5, 0, ["ipfs://gems/typeB/full.svg"], [0]);
await gem
.withSigner(deployer)
.tx.addAssetEntry(
catalog.address,
6,
equippableRefIdLeftGem,
["ipfs://gems/typeB/left.svg"],
[0],
);
await gem
.withSigner(deployer)
.tx.addAssetEntry(
catalog.address,
7,
equippableRefIdMidGem,
["ipfs://gems/typeB/mid.svg"],
[0],
);
await gem
.withSigner(deployer)
.tx.addAssetEntry(
catalog.address,
8,
equippableRefIdRightGem,
["ipfs://gems/typeB/right.svg"],
[0],
);
expect(
(await gem.withSigner(deployer).query.totalAssets())?.value.unwrap().toString()
).to.be.equal("8");
// We add assets of type A to gem 1 and 2, and type Bto gem 3. Both are nested into the first kanaria
// This means gems 1 and 2 will have the same asset, which is totally valid.
await addAssetToToken(gem, deployer, 1, 1)
await addAssetToToken(gem, deployer, 1, 2)
await addAssetToToken(gem, deployer, 1, 3)
await addAssetToToken(gem, deployer, 1, 4)
await addAssetToToken(gem, deployer, 2, 1)
await addAssetToToken(gem, deployer, 2, 2)
await addAssetToToken(gem, deployer, 2, 3)
await addAssetToToken(gem, deployer, 2, 4)
await addAssetToToken(gem, deployer, 3, 5)
await addAssetToToken(gem, deployer, 3, 6)
await addAssetToToken(gem, deployer, 3, 7)
await addAssetToToken(gem, deployer, 3, 8)
expect(
(await gem.query.totalTokenAssets({ u64: 1 }))?.value.unwrap().ok.toString()
).to.be.equal("0,4");
expect(
(await gem.query.totalTokenAssets({ u64: 2 }))?.value.unwrap().ok.toString()
).to.be.equal("0,4");
expect(
(await gem.query.totalTokenAssets({ u64: 3 }))?.value.unwrap().ok.toString()
).to.be.equal("0,4");
// We accept each asset for all gems
await acceptAsset(gem, bob, 1, 1);
await acceptAsset(gem, bob, 1, 2)
await acceptAsset(gem, bob, 1, 3)
await acceptAsset(gem, bob, 1, 4)
await acceptAsset(gem, bob, 2, 1)
await acceptAsset(gem, bob, 2, 2)
await acceptAsset(gem, bob, 2, 3)
await acceptAsset(gem, bob, 2, 4)
await acceptAsset(gem, bob, 3, 5)
await acceptAsset(gem, bob, 3, 6)
await acceptAsset(gem, bob, 3, 7)
await acceptAsset(gem, bob, 3, 8)
expect(
(await gem.query.totalTokenAssets({ u64: 1 }))?.value.unwrap().ok.toString()
).to.be.equal("4,0");
expect(
(await gem.query.totalTokenAssets({ u64: 2 }))?.value.unwrap().ok.toString()
).to.be.equal("4,0");
expect(
(await gem.query.totalTokenAssets({ u64: 3 }))?.value.unwrap().ok.toString()
).to.be.equal("4,0");
// bob approves kanaria Contract on gem (for nesting gem on kanaria)
for (let i = 1; i < 16; i++) {
await gem.withSigner(bob).tx.approve(kanaria.address, { u64: i }, true);
expect(
(await gem.query.allowance(bob.address, kanaria.address, { u64: 1 }))
.value.ok
).to.equal(true);
}
// bob adds 3 gem nfts to bob's 5 kanaria nfts (kanaria is now parent of gem tokens)
for (let k = 1; k < 6; k++) {
for (let g = 1; g < 4; g++) {
const res = await kanaria
.withSigner(bob)
.tx.addChild({ u64: k }, [gem.address, { u64: g }]);
const balance = (
await kanaria.query.childrenBalance({ u64: k })
)?.value.unwrap().ok.toString();
}
expect(
(await kanaria.query.childrenBalance({ u64: k }))?.value.unwrap().ok.toString()
).to.be.equal("3,0");
}
});
});
// Helper function to add an asset to a token
const addAssetToToken = async (contract: Rmrk, signer: KeyringPair, token: number, asset: number): Promise<void> => {
const addAssetTokenResult = await contract
.withSigner(signer)
.tx.addAssetToToken({ u64: token }, asset, null);
emit(addAssetTokenResult, "AssetAddedToToken", {
token: { u64: token },
asset,
replaces: null,
});
}
// Helper function to accept an asset to a token
const acceptAsset = async (contract: Rmrk, signer: KeyringPair, token: number, asset: number): Promise<void> => {
const acceptAssetResult = await contract.withSigner(signer)
.tx.acceptAsset({ u64: token }, asset);
emit(acceptAssetResult, "AssetAccepted", { token: { u64: token }, asset });
}