git.delta.rocks / unique-network / refs/commits / 2986834a0c9e

difftreelog

test refactoring. burn pieces for multiple users test.

Grigoriy Simonov2022-06-23parent: #d22ad5f.patch.diff
in: master

2 files changed

modifiedtests/src/refungible.test.tsdiffbeforeafterboth
18import {IKeyringPair} from '@polkadot/types/types';18import {IKeyringPair} from '@polkadot/types/types';
19import {19import {
20 createCollectionExpectSuccess,20 createCollectionExpectSuccess,
21 createItemExpectSuccess,
22 transferExpectSuccess,
23 transferExpectFailure,
24 getBalance,21 getBalance,
25 burnItemExpectSuccess,
26 createMultipleItemsExpectSuccess,22 createMultipleItemsExpectSuccess,
27 approveExpectSuccess,
28 transferFromExpectSuccess,
29 isTokenExists,23 isTokenExists,
30 getLastTokenId,24 getLastTokenId,
31 getAllowance,25 getAllowance,
26 approve,
27 transferFrom,
28 createCollection,
29 createRefungibleToken,
30 transfer,
31 burnItem,
32} from './util/helpers';32} from './util/helpers';
3333
34import chai from 'chai';34import chai from 'chai';
47 });47 });
48 });48 });
49
50 it('Create refungible collection and token', async () => {
51 await usingApi(async api => {
52 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});
53 expect(createCollectionResult.success).to.be.true;
54 const collectionId = createCollectionResult.collectionId;
55
56 const itemCountBefore = await getLastTokenId(api, collectionId);
57 const result = await createRefungibleToken(api, alice, collectionId, 100n);
58
59 const itemCountAfter = await getLastTokenId(api, collectionId);
60
61 // What to expect
62 // tslint:disable-next-line:no-unused-expression
63 expect(result.success).to.be.true;
64 expect(itemCountAfter).to.be.equal(itemCountBefore + 1);
65 expect(collectionId).to.be.equal(result.collectionId);
66 expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());
67 });
68 });
4969
50 it('Create refungible collection and token. Token pieces transfer.', async () => {70 it('Transfer token pieces', async () => {
51 const createMode = 'ReFungible';71 await usingApi(async api => {
72 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
52 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});73 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
74
53 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);75 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
54
55 let aliceBalance = BigInt(0);76 expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;
56 await usingApi(async api => {77
57 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);78 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);
58 });
59 const transferAmount = BigInt(60);
60 await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');79 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);
61 aliceBalance = aliceBalance - transferAmount;
62 await transferExpectFailure(collectionId, tokenId, alice, bob, aliceBalance + BigInt(1));80 await expect(transfer(api, collectionId, tokenId, alice, bob, 41n)).to.eventually.be.rejected;
81 });
63 });82 });
6483
65 it('Create multiple tokens', async () => {84 it('Create multiple tokens', async () => {
66 const createMode = 'ReFungible';
67 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});85 const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
68 const args = [86 const args = [
69 {ReFungible: {pieces: 1}},87 {ReFungible: {pieces: 1}},
70 {ReFungible: {pieces: 2}},88 {ReFungible: {pieces: 2}},
71 {ReFungible: {pieces: 100}},89 {ReFungible: {pieces: 100}},
72 ];90 ];
73 await createMultipleItemsExpectSuccess(alice, collectionId, args);91 await createMultipleItemsExpectSuccess(alice, collectionId, args);
7492
75 let tokenId = 0;
76 await usingApi(async api => { 93 await usingApi(async api => {
77 tokenId = await getLastTokenId(api, collectionId);94 const tokenId = await getLastTokenId(api, collectionId);
78 expect(tokenId).to.be.equal(3);95 expect(tokenId).to.be.equal(3);
96 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
79 });97 });
80
81 const transferAmount = BigInt(60);
82 await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');
83 });98 });
8499
85 it('Burn some pieces', async () => {100 it('Burn some pieces', async () => {
86 const createMode = 'ReFungible';101 await usingApi(async api => {
102 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
87 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});103 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
88 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);104 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
89
90 let aliceBalance = BigInt(0);
91 await usingApi(async api => {
92 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);105 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
93 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
94 });
95 await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance - BigInt(1));106 expect(await burnItem(api, alice, collectionId, tokenId, 99n)).to.be.true;
96 await usingApi(async api => {
97 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;107 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
108 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);
98 });109 });
99 });110 });
111
112 it('Burn all pieces', async () => {
113 await usingApi(async api => {
114 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
115 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
116 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
117 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
118 expect(await burnItem(api, alice, collectionId, tokenId, 100n)).to.be.true;
119 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;
120 });
121 });
100122
101 it('Burn all pieces', async () => {123 it('Burn some pieces for multiple users', async () => {
102 const createMode = 'ReFungible';124 await usingApi(async api => {
125 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
103 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});126 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
127 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
128
129 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
130 expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;
131
132
133 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);
134 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);
104 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);135 expect(await burnItem(api, alice, collectionId, tokenId, 40n)).to.be.true;
105136
137 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);
106 let aliceBalance = BigInt(0);138 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
107 await usingApi(async api => {139 expect(await burnItem(api, bob, collectionId, tokenId, 59n)).to.be.true;
140
108 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);141 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(1n);
109 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;142 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
110 });
111 await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance);143 expect(await burnItem(api, bob, collectionId, tokenId, 1n)).to.be.true;
112 await usingApi(async api => {144
113 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;145 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;
114 });146 });
115 });147 });
116148
117 it('Set allowance for token', async () => {149 it('Set allowance for token', async () => {
118 const createMode = 'ReFungible';150 await usingApi(async api => {
151 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;
119 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});152 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;
153
120 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);154 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);
121155
122 let aliceBalance = BigInt(0);156 expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;
123 await usingApi(async api => {
124 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);157 expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);
125 });158
126 const allowedAmount = BigInt(60);
127 await approveExpectSuccess(collectionId, tokenId, alice, bob.address, allowedAmount);159 expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob, 20n)).to.be.true;
128 const transferAmount = BigInt(20);160 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);
129 await transferFromExpectSuccess(collectionId, tokenId, bob, alice, bob, transferAmount, 'ReFungible');161 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);
130
131 await usingApi(async api => {
132 expect(await getAllowance(api, collectionId, alice.address, bob.address, tokenId)).to.equal(allowedAmount - transferAmount);162 expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);
133 });163 });
134
135 });164 });
136});165});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
318 tokenPrefix: 'prefix',318 tokenPrefix: 'prefix',
319};319};
320
321export async function
322createCollection(
323 api: ApiPromise,
324 sender: IKeyringPair,
325 params: Partial<CreateCollectionParams> = {},
326): Promise<CreateCollectionResult> {
327 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};
328
329 let modeprm = {};
330 if (mode.type === 'NFT') {
331 modeprm = {nft: null};
332 } else if (mode.type === 'Fungible') {
333 modeprm = {fungible: mode.decimalPoints};
334 } else if (mode.type === 'ReFungible') {
335 modeprm = {refungible: null};
336 }
337
338 const tx = api.tx.unique.createCollectionEx({
339 name: strToUTF16(name),
340 description: strToUTF16(description),
341 tokenPrefix: strToUTF16(tokenPrefix),
342 mode: modeprm as any,
343 });
344 const events = await submitTransactionAsync(sender, tx);
345 return getCreateCollectionResult(events);
346}
320347
321export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {348export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {
322 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};349 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};
329 // Run the CreateCollection transaction356 // Run the CreateCollection transaction
330 const alicePrivateKey = privateKeyWrapper('//Alice');357 const alicePrivateKey = privateKeyWrapper('//Alice');
331358
332 let modeprm = {};
333 if (mode.type === 'NFT') {
334 modeprm = {nft: null};
335 } else if (mode.type === 'Fungible') {
336 modeprm = {fungible: mode.decimalPoints};
337 } else if (mode.type === 'ReFungible') {
338 modeprm = {refungible: null};
339 }
340
341 const tx = api.tx.unique.createCollectionEx({
342 name: strToUTF16(name),
343 description: strToUTF16(description),
344 tokenPrefix: strToUTF16(tokenPrefix),
345 mode: modeprm as any,
346 });
347 const events = await submitTransactionAsync(alicePrivateKey, tx);359 const result = await createCollection(api, alicePrivateKey, params);
348 const result = getCreateCollectionResult(events);
349360
350 // Get number of collections after the transaction361 // Get number of collections after the transaction
351 const collectionCountAfter = await getCreatedCollectionCount(api);362 const collectionCountAfter = await getCreatedCollectionCount(api);
490 return unused;501 return unused;
491}502}
492503
493export async function getAllowance(api: ApiPromise, collectionId: number, owner: CrossAccountId | string, approved: CrossAccountId | string, tokenId: number) {504export async function getAllowance(api: ApiPromise, collectionId: number, owner: CrossAccountId | string | IKeyringPair, approved: CrossAccountId | string | IKeyringPair, tokenId: number) {
494 return (await api.rpc.unique.allowance(collectionId, normalizeAccountId(owner), normalizeAccountId(approved), tokenId)).toBigInt();505 return (await api.rpc.unique.allowance(collectionId, normalizeAccountId(owner), normalizeAccountId(approved), tokenId)).toBigInt();
495}506}
496507
800 ReFungible: CreateReFungibleData;811 ReFungible: CreateReFungibleData;
801};812};
813
814export async function burnItem(api: ApiPromise, sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint) : Promise<boolean> {
815 const tx = api.tx.unique.burnItem(collectionId, tokenId, value);
816 const events = await submitTransactionAsync(sender, tx);
817 return getGenericResult(events).success;
818}
802819
803export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {820export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {
804 await usingApi(async (api) => {821 await usingApi(async (api) => {
805 const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);822 const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);
806 // if burning token by admin - use adminButnItemExpectSuccess823 // if burning token by admin - use adminButnItemExpectSuccess
807 expect(balanceBefore >= BigInt(value)).to.be.true;824 expect(balanceBefore >= BigInt(value)).to.be.true;
808825
809 const tx = api.tx.unique.burnItem(collectionId, tokenId, value);
810 const events = await submitTransactionAsync(sender, tx);
811 const result = getGenericResult(events);
812 expect(result.success).to.be.true;826 expect(await burnItem(api, sender, collectionId, tokenId, value)).to.be.true;
813827
814 const balanceAfter = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);828 const balanceAfter = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);
815 expect(balanceAfter + BigInt(value)).to.be.equal(balanceBefore);829 expect(balanceAfter + BigInt(value)).to.be.equal(balanceBefore);
816 });830 });
817}831}
832
833export async function
834approve(
835 api: ApiPromise,
836 collectionId: number,
837 tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string | IKeyringPair, amount: number | bigint = 1,
838) {
839 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);
840 const events = await submitTransactionAsync(owner, approveUniqueTx);
841 return getGenericResult(events).success;
842}
818843
819export async function844export async function
820approveExpectSuccess(845approveExpectSuccess(
821 collectionId: number,846 collectionId: number,
822 tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string, amount: number | bigint = 1,847 tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string, amount: number | bigint = 1,
823) {848) {
824 await usingApi(async (api: ApiPromise) => {849 await usingApi(async (api: ApiPromise) => {
825 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);850 const result = approve(api, collectionId, tokenId, owner, approved);
826 const events = await submitTransactionAsync(owner, approveUniqueTx);
827 const result = getGenericResult(events);
828 expect(result.success).to.be.true;851 expect(result).to.be.true;
829852
830 expect(await getAllowance(api, collectionId, owner.address, approved, tokenId)).to.be.equal(BigInt(amount));853 expect(await getAllowance(api, collectionId, owner.address, approved, tokenId)).to.be.equal(BigInt(amount));
831 });854 });
845 });868 });
846}869}
870
871export async function
872transferFrom(
873 api: ApiPromise,
874 collectionId: number,
875 tokenId: number,
876 accountApproved: IKeyringPair,
877 accountFrom: IKeyringPair | CrossAccountId,
878 accountTo: IKeyringPair | CrossAccountId,
879 value: number | bigint,
880) {
881 const from = normalizeAccountId(accountFrom);
882 const to = normalizeAccountId(accountTo);
883 const transferFromTx = api.tx.unique.transferFrom(from, to, collectionId, tokenId, value);
884 const events = await submitTransactionAsync(accountApproved, transferFromTx);
885 return getGenericResult(events).success;
886}
847887
848export async function888export async function
849transferFromExpectSuccess(889transferFromExpectSuccess(
862 if (type === 'Fungible' || type === 'ReFungible') {902 if (type === 'Fungible' || type === 'ReFungible') {
863 balanceBefore = await getBalance(api, collectionId, to, tokenId);903 balanceBefore = await getBalance(api, collectionId, to, tokenId);
864 }904 }
865 const transferFromTx = api.tx.unique.transferFrom(normalizeAccountId(accountFrom), to, collectionId, tokenId, value);
866 const events = await submitTransactionAsync(accountApproved, transferFromTx);
867 const result = getGenericResult(events);
868 // tslint:disable-next-line:no-unused-expression
869 expect(result.success).to.be.true;905 expect(transferFrom(api, collectionId, tokenId, accountApproved, accountFrom, accountTo, value)).to.be.true;
870 if (type === 'NFT') {906 if (type === 'NFT') {
871 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);907 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);
872 }908 }
1070 });1106 });
1071}1107}
1108
1109export async function
1110transfer(
1111 api: ApiPromise,
1112 collectionId: number,
1113 tokenId: number,
1114 sender: IKeyringPair,
1115 recipient: IKeyringPair | CrossAccountId,
1116 value: number | bigint,
1117) : Promise<boolean> {
1118 const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);
1119 const events = await executeTransaction(api, sender, transferTx);
1120 return getGenericResult(events).success;
1121}
10721122
1073export async function1123export async function
1074transferExpectSuccess(1124transferExpectSuccess(
1088 balanceBefore = await getBalance(api, collectionId, to, tokenId);1138 balanceBefore = await getBalance(api, collectionId, to, tokenId);
1089 }1139 }
1140
1090 const transferTx = api.tx.unique.transfer(to, collectionId, tokenId, value);1141 const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);
1091 const events = await executeTransaction(api, sender, transferTx);1142 const events = await executeTransaction(api, sender, transferTx);
1092
1093 const result = getTransferResult(api, events);1143 const result = getTransferResult(api, events);
1148export async function getBalance(1199export async function getBalance(
1149 api: ApiPromise,1200 api: ApiPromise,
1150 collectionId: number,1201 collectionId: number,
1151 owner: string | CrossAccountId,1202 owner: string | CrossAccountId | IKeyringPair,
1152 token: number,1203 token: number,
1153): Promise<bigint> {1204): Promise<bigint> {
1154 return (await api.rpc.unique.balance(collectionId, normalizeAccountId(owner), token)).toBigInt();1205 return (await api.rpc.unique.balance(collectionId, normalizeAccountId(owner), token)).toBigInt();
1229 const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);1280 const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);
12301281
1231 const events = await submitTransactionAsync(sender, tx);1282 const events = await submitTransactionAsync(sender, tx);
1232 const result = getCreateItemsResult(events);1283 expect(getGenericResult(events).success).to.be.true;
1233 });1284 });
1234}1285}
12351286
1366 return newItemId;1417 return newItemId;
1367}1418}
1419
1420export async function createRefungibleToken(api: ApiPromise, sender: IKeyringPair, collectionId: number, amount: bigint, owner: CrossAccountId | IKeyringPair | string = sender.address) : Promise<CreateItemResult> {
1421 const createData = {refungible: {pieces: amount}};
1422 const tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), createData as any);
1423
1424 const events = await submitTransactionAsync(sender, tx);
1425 return getCreateItemResult(events);
1426}
13681427
1369export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {1428export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {
1370 await usingApi(async (api) => {1429 await usingApi(async (api) => {