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

difftreelog

source

tests/src/refungible.test.ts5.2 KiBsourcehistory
1// 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.', async () => {51    const createMode = 'ReFungible';52    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});53    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);5455    let aliceBalance = BigInt(0);56    await usingApi(async api => {57      aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);58    });59    const transferAmount = BigInt(60);60    await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');61    aliceBalance = aliceBalance - transferAmount;62    await transferExpectFailure(collectionId, tokenId, alice, bob, aliceBalance + BigInt(1));63  });6465  it('Create multiple tokens', async () => {66    const createMode = 'ReFungible';67    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});68    const args = [69      {ReFungible: {pieces: 1}},70      {ReFungible: {pieces: 2}},71      {ReFungible: {pieces: 100}},72    ];73    await createMultipleItemsExpectSuccess(alice, collectionId, args);7475    let tokenId = 0;76    await usingApi(async api => {      77      tokenId = await getLastTokenId(api, collectionId);78      expect(tokenId).to.be.equal(3);79    });8081    const transferAmount = BigInt(60);82    await transferExpectSuccess(collectionId, tokenId, alice, bob, transferAmount, 'ReFungible');83  });8485  it('Burn some pieces', async () => {86    const createMode = 'ReFungible';87    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});88    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);8990    let aliceBalance = BigInt(0);91    await usingApi(async api => {92      aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);93      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;94    });95    await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance - BigInt(1));96    await usingApi(async api => {97      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;98    });99  });100101  it('Burn all pieces', async () => {102    const createMode = 'ReFungible';103    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});104    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);105106    let aliceBalance = BigInt(0);107    await usingApi(async api => {108      aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);109      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;110    });111    await burnItemExpectSuccess(alice, collectionId, tokenId, aliceBalance);112    await usingApi(async api => {113      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;114    });115  });116117  it('Set allowance for token', async () => {118    const createMode = 'ReFungible';119    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});120    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);121122    let aliceBalance = BigInt(0);123    await usingApi(async api => {124      aliceBalance = await getBalance(api, collectionId, alice.address, tokenId);125    });126    const allowedAmount = BigInt(60);127    await approveExpectSuccess(collectionId, tokenId, alice, bob.address, allowedAmount);128    const transferAmount = BigInt(20);129    await transferFromExpectSuccess(collectionId, tokenId, bob, alice, bob,  transferAmount, 'ReFungible');130131    await usingApi(async api => {132      expect(await getAllowance(api, collectionId, alice.address, bob.address, tokenId)).to.equal(allowedAmount - transferAmount);133    });134    135  });136});