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

difftreelog

source

tests/src/transferFrom.test.ts16.0 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  requirePallets,35  Pallets,36} from './util/helpers';3738chai.use(chaiAsPromised);39const expect = chai.expect;4041describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {42  let alice: IKeyringPair;43  let bob: IKeyringPair;44  let charlie: IKeyringPair;4546  before(async () => {47    await usingApi(async (api, privateKeyWrapper) => {48      alice = privateKeyWrapper('//Alice');49      bob = privateKeyWrapper('//Bob');50      charlie = privateKeyWrapper('//Charlie');51    });52  });5354  it('[nft] Execute the extrinsic and check nftItemList - owner of token', async () => {55    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');60  });6162  it('[fungible] Execute the extrinsic and check nftItemList - owner of token', async () => {63    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});64    const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');65    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);66    await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1, 'Fungible');67  });6869  it('[refungible] Execute the extrinsic and check nftItemList - owner of token', async function() {70    await requirePallets(this, [Pallets.ReFungible]);7172    const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});73    const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');74    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 100);75    await transferFromExpectSuccess(76      reFungibleCollectionId,77      newReFungibleTokenId,78      bob,79      alice,80      charlie,81      100,82      'ReFungible',83    );84  });8586  it('Should reduce allowance if value is big', async () => {87    await usingApi(async (api, privateKeyWrapper) => {88      const alice = privateKeyWrapper('//Alice');89      const bob = privateKeyWrapper('//Bob');90      const charlie = privateKeyWrapper('//Charlie');9192      // fungible93      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});94      const newFungibleTokenId = await createFungibleItemExpectSuccess(alice, fungibleCollectionId, {Value: 500000n});9596      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 500000n);97      await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 500000n, 'Fungible');98      expect(await getAllowance(api, fungibleCollectionId, alice.address, bob.address, newFungibleTokenId)).to.equal(0n);99    });100  });101102  it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {103    const collectionId = await createCollectionExpectSuccess();104    await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});105    const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);106107    await transferFromExpectSuccess(collectionId, itemId, alice, bob, charlie);108  });109});110111describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {112  let alice: IKeyringPair;113  let bob: IKeyringPair;114  let charlie: IKeyringPair;115116  before(async () => {117    await usingApi(async (api, privateKeyWrapper) => {118      alice = privateKeyWrapper('//Alice');119      bob = privateKeyWrapper('//Bob');120      charlie = privateKeyWrapper('//Charlie');121    });122  });123124  it('[nft] transferFrom for a collection that does not exist', async () => {125    await usingApi(async (api: ApiPromise) => {126      const nftCollectionCount = await getCreatedCollectionCount(api);127      await approveExpectFail(nftCollectionCount + 1, 1, alice, bob);128129      await transferFromExpectFail(nftCollectionCount + 1, 1, bob, alice, charlie, 1);130    });131  });132133  it('[fungible] transferFrom for a collection that does not exist', async () => {134    await usingApi(async (api: ApiPromise) => {135      const fungibleCollectionCount = await getCreatedCollectionCount(api);136      await approveExpectFail(fungibleCollectionCount + 1, 0, alice, bob);137138      await transferFromExpectFail(fungibleCollectionCount + 1, 0, bob, alice, charlie, 1);139    });140  });141142  it('[refungible] transferFrom for a collection that does not exist', async function() {143    await requirePallets(this, [Pallets.ReFungible]);144145    await usingApi(async (api: ApiPromise) => {146      const reFungibleCollectionCount = await getCreatedCollectionCount(api);147      await approveExpectFail(reFungibleCollectionCount + 1, 1, alice, bob);148149      await transferFromExpectFail(reFungibleCollectionCount + 1, 1, bob, alice, charlie, 1);150    });151  });152153  /* it('transferFrom for a collection that was destroyed', async () => {154    await usingApi(async (api: ApiPromise) => {155      this test copies approve negative test156    });157  }); */158159  /* it('transferFrom a token that does not exist', async () => {160    await usingApi(async (api: ApiPromise) => {161      this test copies approve negative test162    });163  }); */164165  /* it('transferFrom a token that was deleted', async () => {166    await usingApi(async (api: ApiPromise) => {167      this test copies approve negative test168    });169  }); */170171  it('[nft] transferFrom for not approved address', async () => {172    const nftCollectionId = await createCollectionExpectSuccess();173    const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');174175    await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 1);176  });177178  it('[fungible] transferFrom for not approved address', async () => {179    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});180    const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');181    await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1);182  });183184  it('[refungible] transferFrom for not approved address', async function() {185    await requirePallets(this, [Pallets.ReFungible]);186187    const reFungibleCollectionId = await188    createCollectionExpectSuccess({mode: {type: 'ReFungible'}});189    const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');190    await transferFromExpectFail(191      reFungibleCollectionId,192      newReFungibleTokenId,193      bob,194      alice,195      charlie,196      1,197    );198  });199200  it('[nft] transferFrom incorrect token count', async () => {201    const nftCollectionId = await createCollectionExpectSuccess();202    const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');203    await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);204205    await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 2);206  });207208  it('[fungible] transferFrom incorrect token count', async () => {209    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});210    const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');211    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);212    await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 2);213  });214215  it('[refungible] transferFrom incorrect token count', async function() {216    await requirePallets(this, [Pallets.ReFungible]);217218    const reFungibleCollectionId = await219    createCollectionExpectSuccess({mode: {type: 'ReFungible'}});220    const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');221    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address);222    await transferFromExpectFail(223      reFungibleCollectionId,224      newReFungibleTokenId,225      bob,226      alice,227      charlie,228      2,229    );230  });231232  it('[nft] execute transferFrom from account that is not owner of collection', async () => {233    await usingApi(async (api, privateKeyWrapper) => {234      const dave = privateKeyWrapper('//Dave');235      const nftCollectionId = await createCollectionExpectSuccess();236      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');237      try {238        await approveExpectFail(nftCollectionId, newNftTokenId, dave, bob);239        await transferFromExpectFail(nftCollectionId, newNftTokenId, dave, alice, charlie, 1);240      } catch (e) {241        // tslint:disable-next-line:no-unused-expression242        expect(e).to.be.exist;243      }244245      // await transferFromExpectFail(nftCollectionId, newNftTokenId, Dave, Alice, Charlie, 1);246    });247  });248249  it('[fungible] execute transferFrom from account that is not owner of collection', async () => {250    await usingApi(async (api, privateKeyWrapper) => {251      const dave = privateKeyWrapper('//Dave');252253      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});254      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');255      try {256        await approveExpectFail(fungibleCollectionId, newFungibleTokenId, dave, bob);257        await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, dave, alice, charlie, 1);258      } catch (e) {259        // tslint:disable-next-line:no-unused-expression260        expect(e).to.be.exist;261      }262    });263  });264265  it('[refungible] execute transferFrom from account that is not owner of collection', async function() {266    await requirePallets(this, [Pallets.ReFungible]);267268    await usingApi(async (api, privateKeyWrapper) => {269      const dave = privateKeyWrapper('//Dave');270      const reFungibleCollectionId = await271      createCollectionExpectSuccess({mode: {type: 'ReFungible'}});272      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');273      try {274        await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, dave, bob);275        await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, dave, alice, charlie, 1);276      } catch (e) {277        // tslint:disable-next-line:no-unused-expression278        expect(e).to.be.exist;279      }280    });281  });282  it('transferFrom burnt token before approve NFT', async () => {283    await usingApi(async () => {284      // nft285      const nftCollectionId = await createCollectionExpectSuccess();286      await setCollectionLimitsExpectSuccess(alice, nftCollectionId, {ownerCanTransfer: true});287      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');288      await burnItemExpectSuccess(alice, nftCollectionId, newNftTokenId, 1);289      await approveExpectFail(nftCollectionId, newNftTokenId, alice, bob);290      await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 1);291    });292  });293  it('transferFrom burnt token before approve Fungible', async () => {294    await usingApi(async () => {295      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});296      await setCollectionLimitsExpectSuccess(alice, fungibleCollectionId, {ownerCanTransfer: true});297      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');298      await burnItemExpectSuccess(alice, fungibleCollectionId, newFungibleTokenId, 10);299      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);300      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1);301302    });303  });304  it('transferFrom burnt token before approve ReFungible', async function() {305    await requirePallets(this, [Pallets.ReFungible]);306307    await usingApi(async () => {308      const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});309      await setCollectionLimitsExpectSuccess(alice, reFungibleCollectionId, {ownerCanTransfer: true});310      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');311      await burnItemExpectSuccess(alice, reFungibleCollectionId, newReFungibleTokenId, 100);312      await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, alice, bob);313      await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice, charlie, 1);314315    });316  });317318  it('transferFrom burnt token after approve NFT', async () => {319    await usingApi(async () => {320      // nft321      const nftCollectionId = await createCollectionExpectSuccess();322      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');323      await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);324      await burnItemExpectSuccess(alice, nftCollectionId, newNftTokenId, 1);325      await transferFromExpectFail(nftCollectionId, newNftTokenId, bob, alice, charlie, 1);326    });327  });328  it('transferFrom burnt token after approve Fungible', async () => {329    await usingApi(async () => {330      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});331      const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');332      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);333      await burnItemExpectSuccess(alice, fungibleCollectionId, newFungibleTokenId, 10);334      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 1);335336    });337  });338  it('transferFrom burnt token after approve ReFungible', async function() {339    await requirePallets(this, [Pallets.ReFungible]);340341    await usingApi(async () => {342      const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});343      const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');344      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address);345      await burnItemExpectSuccess(alice, reFungibleCollectionId, newReFungibleTokenId, 100);346      await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice, charlie, 1);347348    });349  });350351  it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {352    const collectionId = await createCollectionExpectSuccess();353    const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);354    await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: false});355356    await transferFromExpectFail(collectionId, itemId, alice, bob, charlie);357  });358});