--- /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 . + +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); + }); + + }); +}); --- 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);