difftreelog
test refungible
in: master
2 files changed
tests/src/refungible.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {default as usingApi} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20 createCollectionExpectSuccess,21 createItemExpectSuccess,22 transferExpectSuccess,23 transferExpectFailure,24 getBalance,25 burnItemExpectSuccess,26 createMultipleItemsExpectSuccess,27 approveExpectSuccess,28 transferFromExpectSuccess,29 isTokenExists,30 getLastTokenId,31 getAllowance,32} from './util/helpers';3334import chai from 'chai';35import chaiAsPromised from 'chai-as-promised';36chai.use(chaiAsPromised);37const expect = chai.expect;3839let alice: IKeyringPair;40let bob: IKeyringPair;4142describe('integration test: Refungible functionality:', () => {43 before(async () => {44 await usingApi(async (api, privateKeyWrapper) => {45 alice = privateKeyWrapper('//Alice');46 bob = privateKeyWrapper('//Bob');47 });48 });4950 it('Create refungible collection and token. Token pieces transfer. Token repartition.', async () => {51 const createMode = 'ReFungible';52 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});53 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);5455 const args = [56 {ReFungible: {pieces: 1}},57 {ReFungible: {pieces: 2}},58 {ReFungible: {pieces: 3}},59 ];60 await createMultipleItemsExpectSuccess(alice, collectionId, args);6162 let aliceBalance = BigInt(0);63 await usingApi(async api => {64 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;65 66 const itemsListIndexAfter = await getLastTokenId(api, collectionId);67 expect(itemsListIndexAfter).to.be.equal(4);6869 aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);70 });71 const transferAmount = BigInt(60);72 await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');73 aliceBalance = aliceBalance - transferAmount;74 await transferExpectFailure(collectionId, tokenId, alice, bob, aliceBalance + BigInt(1));75 await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance - BigInt(1));76 await approveExpectSuccess(collectionId, tokenId, bob, alice.address, transferAmount);77 const bobBalance = transferAmount;78 const allowedAmount = BigInt(60);79 await transferFromExpectSuccess(collectionId, tokenId, alice, bob, alice, 40, 'ReFungible');8081 await usingApi(async api => {82 expect(await getAllowance(api, collectionId, bob.address, alice.address, 0)).to.equal(bobBalance - allowedAmount);83 });84 85 });86});tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -800,7 +800,7 @@
ReFungible: CreateReFungibleData;
};
-export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value = 1) {
+export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {
await usingApi(async (api) => {
const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);
// if burning token by admin - use adminButnItemExpectSuccess
@@ -1084,7 +1084,7 @@
const to = normalizeAccountId(recipient);
let balanceBefore = 0n;
- if (type === 'Fungible') {
+ if (type === 'Fungible' || type === 'ReFungible') {
balanceBefore = await getBalance(api, collectionId, to, tokenId);
}
const transferTx = api.tx.unique.transfer(to, collectionId, tokenId, value);
@@ -1100,7 +1100,7 @@
if (type === 'NFT') {
expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);
}
- if (type === 'Fungible') {
+ if (type === 'Fungible' || type === 'ReFungible') {
const balanceAfter = await getBalance(api, collectionId, to, tokenId);
if (JSON.stringify(to) !== JSON.stringify(from)) {
expect(balanceAfter - balanceBefore).to.be.equal(BigInt(value));
@@ -1108,9 +1108,6 @@
expect(balanceAfter).to.be.equal(balanceBefore);
}
}
- if (type === 'ReFungible') {
- expect(await getBalance(api, collectionId, to, tokenId) >= value).to.be.true;
- }
});
}
@@ -1226,6 +1223,16 @@
});
}
+export async function createMultipleItemsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {
+ await usingApi(async (api) => {
+ const to = normalizeAccountId(owner);
+ const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);
+
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getCreateItemsResult(events);
+ });
+}
+
export async function createMultipleItemsWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {
await usingApi(async (api) => {
const to = normalizeAccountId(owner);