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

difftreelog

source

tests/src/transferFrom.test.ts14.8 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 {ApiPromise} from '@polkadot/api';18import {IKeyringPair} from '@polkadot/types/types';19import chai from 'chai';20import chaiAsPromised from 'chai-as-promised';21import {default as usingApi} from './substrate/substrate-api';22import {23  approveExpectFail,24  approveExpectSuccess,25  createCollectionExpectSuccess,26  createFungibleItemExpectSuccess,27  createItemExpectSuccess,28  getAllowance,29  transferFromExpectFail,30  transferFromExpectSuccess,31  burnItemExpectSuccess,32  setCollectionLimitsExpectSuccess,33  getCreatedCollectionCount,34} from './util/helpers';3536chai.use(chaiAsPromised);37const expect = chai.expect;3839describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {40  let alice: IKeyringPair;41  let bob: IKeyringPair;42  let charlie: IKeyringPair;4344  before(async () => {45    await usingApi(async (api, privateKeyWrapper) => {46      alice = privateKeyWrapper('//Alice');47      bob = privateKeyWrapper('//Bob');48      charlie = privateKeyWrapper('//Charlie');49    });50  });5152  it('Execute the extrinsic and check nftItemList - owner of token', async () => {53    await usingApi(async () => {54      // nft55      const nftCollectionId = await createCollectionExpectSuccess();56      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');57      await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);5859      await transferFromExpectSuccess(nftCollectionId, newNftTokenId, bob, alice, charlie, 1, 'NFT');6061      // fungible62      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});63      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');64      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);65      await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1, 'Fungible');6667      // reFungible68      const reFungibleCollectionId = await69      createCollectionExpectSuccess({mode: {type: 'ReFungible'}});70      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');71      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 100);72      await transferFromExpectSuccess(73        reFungibleCollectionId,74        newReFungibleTokenId,75        bob,76        alice,77        charlie,78        100,79        'ReFungible',80      );81    });82  });8384  it('Should reduce allowance if value is big', async () => {85    await usingApi(async (api, privateKeyWrapper) => {86      const alice = privateKeyWrapper('//Alice');87      const bob = privateKeyWrapper('//Bob');88      const charlie = privateKeyWrapper('//Charlie');8990      // fungible91      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});92      const newFungibleTokenId = await createFungibleItemExpectSuccess(alice, fungibleCollectionId, {Value: 500000n});9394      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 500000n);95      await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 500000n, 'Fungible');96      expect(await getAllowance(api, fungibleCollectionId, alice.address, bob.address, newFungibleTokenId)).to.equal(0n);97    });98  });99100  it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {101    const collectionId = await createCollectionExpectSuccess();102    await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});103    const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);104105    await transferFromExpectSuccess(collectionId, itemId, alice, bob, charlie);106  });107});108109describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {110  let alice: IKeyringPair;111  let bob: IKeyringPair;112  let charlie: IKeyringPair;113114  before(async () => {115    await usingApi(async (api, privateKeyWrapper) => {116      alice = privateKeyWrapper('//Alice');117      bob = privateKeyWrapper('//Bob');118      charlie = privateKeyWrapper('//Charlie');119    });120  });121122  it('transferFrom for a collection that does not exist', async () => {123    await usingApi(async (api: ApiPromise) => {124      // nft125      const nftCollectionCount = await getCreatedCollectionCount(api);126      await approveExpectFail(nftCollectionCount + 1, 1, alice, bob);127128      await transferFromExpectFail(nftCollectionCount + 1, 1, bob, alice, charlie, 1);129130      // fungible131      const fungibleCollectionCount = await getCreatedCollectionCount(api);132      await approveExpectFail(fungibleCollectionCount + 1, 0, alice, bob);133134      await transferFromExpectFail(fungibleCollectionCount + 1, 0, bob, alice, charlie, 1);135      // reFungible136      const reFungibleCollectionCount = await getCreatedCollectionCount(api);137      await approveExpectFail(reFungibleCollectionCount + 1, 1, alice, bob);138139      await transferFromExpectFail(reFungibleCollectionCount + 1, 1, bob, alice, charlie, 1);140    });141  });142143  /* it('transferFrom for a collection that was destroyed', async () => {144    await usingApi(async (api: ApiPromise) => {145      this test copies approve negative test146    });147  }); */148149  /* it('transferFrom a token that does not exist', async () => {150    await usingApi(async (api: ApiPromise) => {151      this test copies approve negative test152    });153  }); */154155  /* it('transferFrom a token that was deleted', async () => {156    await usingApi(async (api: ApiPromise) => {157      this test copies approve negative test158    });159  }); */160161  it('transferFrom for not approved address', async () => {162    await usingApi(async () => {163      // nft164      const nftCollectionId = await createCollectionExpectSuccess();165      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');166167      await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 1);168169      // fungible170      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});171      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');172      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1);173      // reFungible174      const reFungibleCollectionId = await175      createCollectionExpectSuccess({mode: {type: 'ReFungible'}});176      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');177      await transferFromExpectFail(178        reFungibleCollectionId,179        newReFungibleTokenId,180        bob,181        alice,182        charlie,183        1,184      );185    });186  });187188  it('transferFrom incorrect token count', async () => {189    await usingApi(async () => {190      // nft191      const nftCollectionId = await createCollectionExpectSuccess();192      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');193      await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);194195      await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 2);196197      // fungible198      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});199      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');200      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);201      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 2);202      // reFungible203      const reFungibleCollectionId = await204      createCollectionExpectSuccess({mode: {type: 'ReFungible'}});205      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');206      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address);207      await transferFromExpectFail(208        reFungibleCollectionId,209        newReFungibleTokenId,210        bob,211        alice,212        charlie,213        2,214      );215    });216  });217218  it('execute transferFrom from account that is not owner of collection', async () => {219    await usingApi(async (api, privateKeyWrapper) => {220      const dave = privateKeyWrapper('//Dave');221      // nft222      const nftCollectionId = await createCollectionExpectSuccess();223      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');224      try {225        await approveExpectFail(nftCollectionId, newNftTokenId, dave, bob);226        await transferFromExpectFail(nftCollectionId, newNftTokenId, dave, alice, charlie, 1);227      } catch (e) {228        // tslint:disable-next-line:no-unused-expression229        expect(e).to.be.exist;230      }231232      // await transferFromExpectFail(nftCollectionId, newNftTokenId, Dave, Alice, Charlie, 1);233234      // fungible235      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});236      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');237      try {238        await approveExpectFail(fungibleCollectionId, newFungibleTokenId, dave, bob);239        await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, dave, alice, charlie, 1);240      } catch (e) {241        // tslint:disable-next-line:no-unused-expression242        expect(e).to.be.exist;243      }244      // reFungible245      const reFungibleCollectionId = await246      createCollectionExpectSuccess({mode: {type: 'ReFungible'}});247      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');248      try {249        await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, dave, bob);250        await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, dave, alice, charlie, 1);251      } catch (e) {252        // tslint:disable-next-line:no-unused-expression253        expect(e).to.be.exist;254      }255    });256  });257  it('transferFrom burnt token before approve NFT', async () => {258    await usingApi(async () => {259      // nft260      const nftCollectionId = await createCollectionExpectSuccess();261      await setCollectionLimitsExpectSuccess(alice, nftCollectionId, {ownerCanTransfer: true});262      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');263      await burnItemExpectSuccess(alice, nftCollectionId, newNftTokenId, 1);264      await approveExpectFail(nftCollectionId, newNftTokenId, alice, bob);265      await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 1);266    });267  });268  it('transferFrom burnt token before approve Fungible', async () => {269    await usingApi(async () => {270      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});271      await setCollectionLimitsExpectSuccess(alice, fungibleCollectionId, {ownerCanTransfer: true});272      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');273      await burnItemExpectSuccess(alice, fungibleCollectionId, newFungibleTokenId, 10);274      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);275      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1);276277    });278  });279  it('transferFrom burnt token before approve ReFungible', async () => {280    await usingApi(async () => {281      const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});282      await setCollectionLimitsExpectSuccess(alice, reFungibleCollectionId, {ownerCanTransfer: true});283      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');284      await burnItemExpectSuccess(alice, reFungibleCollectionId, newReFungibleTokenId, 100);285      await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, alice, bob);286      await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice, charlie, 1);287288    });289  });290291  it('transferFrom burnt token after approve NFT', async () => {292    await usingApi(async () => {293      // nft294      const nftCollectionId = await createCollectionExpectSuccess();295      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');296      await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);297      await burnItemExpectSuccess(alice, nftCollectionId, newNftTokenId, 1);298      await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 1);299    });300  });301  it('transferFrom burnt token after approve Fungible', async () => {302    await usingApi(async () => {303      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});304      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');305      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);306      await burnItemExpectSuccess(alice, fungibleCollectionId, newFungibleTokenId, 10);307      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1);308309    });310  });311  it('transferFrom burnt token after approve ReFungible', async () => {312    await usingApi(async () => {313      const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});314      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');315      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address);316      await burnItemExpectSuccess(alice, reFungibleCollectionId, newReFungibleTokenId, 100);317      await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice, charlie, 1);318319    });320  });321322  it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {323    const collectionId = await createCollectionExpectSuccess();324    const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);325    await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: false});326327    await transferFromExpectFail(collectionId, itemId, alice, bob, charlie);328  });329});