git.delta.rocks / unique-network / refs/commits / f8da043c3088

difftreelog

test refungible

Grigoriy Simonov2022-06-22parent: #421f362.patch.diff
in: master

2 files changed

addedtests/src/refungible.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/refungible.test.ts
@@ -0,0 +1,86 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {default as usingApi} from './substrate/substrate-api';
+import {IKeyringPair} from '@polkadot/types/types';
+import {
+  createCollectionExpectSuccess,
+  createItemExpectSuccess,
+  transferExpectSuccess,
+  transferExpectFailure,
+  getBalance,
+  burnItemExpectSuccess,
+  createMultipleItemsExpectSuccess,
+  approveExpectSuccess,
+  transferFromExpectSuccess,
+  isTokenExists,
+  getLastTokenId,
+  getAllowance,
+} from './util/helpers';
+
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+
+describe('integration test: Refungible functionality:', () => {
+  before(async () => {
+    await usingApi(async (api, privateKeyWrapper) => {
+      alice = privateKeyWrapper('//Alice');
+      bob = privateKeyWrapper('//Bob');
+    });
+  });
+
+  it('Create refungible collection and token. Token pieces transfer. Token repartition.', async () => {
+    const createMode = 'ReFungible';
+    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});
+    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+
+    const args = [
+      {ReFungible: {pieces: 1}},
+      {ReFungible: {pieces: 2}},
+      {ReFungible: {pieces: 3}},
+    ];
+    await createMultipleItemsExpectSuccess(alice, collectionId, args);
+
+    let aliceBalance = BigInt(0);
+    await usingApi(async api => {
+      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;
+      
+      const itemsListIndexAfter = await getLastTokenId(api, collectionId);
+      expect(itemsListIndexAfter).to.be.equal(4);
+
+      aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);
+    });
+    const transferAmount = BigInt(60);
+    await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');
+    aliceBalance = aliceBalance - transferAmount;
+    await transferExpectFailure(collectionId, tokenId, alice, bob, aliceBalance + BigInt(1));
+    await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance - BigInt(1));
+    await approveExpectSuccess(collectionId, tokenId, bob, alice.address, transferAmount);
+    const bobBalance = transferAmount;
+    const allowedAmount = BigInt(60);
+    await transferFromExpectSuccess(collectionId, tokenId, alice, bob, alice, 40, 'ReFungible');
+
+    await usingApi(async api => {
+      expect(await getAllowance(api, collectionId, bob.address, alice.address, 0)).to.equal(bobBalance - allowedAmount);
+    });
+    
+  });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
800 ReFungible: CreateReFungibleData;800 ReFungible: CreateReFungibleData;
801};801};
802802
803export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value = 1) {803export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {
804 await usingApi(async (api) => {804 await usingApi(async (api) => {
805 const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);805 const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);
806 // if burning token by admin - use adminButnItemExpectSuccess806 // if burning token by admin - use adminButnItemExpectSuccess
1084 const to = normalizeAccountId(recipient);1084 const to = normalizeAccountId(recipient);
10851085
1086 let balanceBefore = 0n;1086 let balanceBefore = 0n;
1087 if (type === 'Fungible') {1087 if (type === 'Fungible' || type === 'ReFungible') {
1088 balanceBefore = await getBalance(api, collectionId, to, tokenId);1088 balanceBefore = await getBalance(api, collectionId, to, tokenId);
1089 }1089 }
1090 const transferTx = api.tx.unique.transfer(to, collectionId, tokenId, value);1090 const transferTx = api.tx.unique.transfer(to, collectionId, tokenId, value);
1100 if (type === 'NFT') {1100 if (type === 'NFT') {
1101 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);1101 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);
1102 }1102 }
1103 if (type === 'Fungible') {1103 if (type === 'Fungible' || type === 'ReFungible') {
1104 const balanceAfter = await getBalance(api, collectionId, to, tokenId);1104 const balanceAfter = await getBalance(api, collectionId, to, tokenId);
1105 if (JSON.stringify(to) !== JSON.stringify(from)) {1105 if (JSON.stringify(to) !== JSON.stringify(from)) {
1106 expect(balanceAfter - balanceBefore).to.be.equal(BigInt(value));1106 expect(balanceAfter - balanceBefore).to.be.equal(BigInt(value));
1107 } else {1107 } else {
1108 expect(balanceAfter).to.be.equal(balanceBefore);1108 expect(balanceAfter).to.be.equal(balanceBefore);
1109 }1109 }
1110 }1110 }
1111 if (type === 'ReFungible') {
1112 expect(await getBalance(api, collectionId, to, tokenId) >= value).to.be.true;
1113 }
1114 });1111 });
1115}1112}
11161113
1226 });1223 });
1227}1224}
1225
1226export async function createMultipleItemsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {
1227 await usingApi(async (api) => {
1228 const to = normalizeAccountId(owner);
1229 const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);
1230
1231 const events = await submitTransactionAsync(sender, tx);
1232 const result = getCreateItemsResult(events);
1233 });
1234}
12281235
1229export async function createMultipleItemsWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {1236export async function createMultipleItemsWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {
1230 await usingApi(async (api) => {1237 await usingApi(async (api) => {