git.delta.rocks / unique-network / refs/commits / 9fb4be30085d

difftreelog

source

js-packages/tests/transferFrom.test.ts19.5 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 type {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect} from '@unique/test-utils/util.js';19import {NON_EXISTENT_COLLECTION_ID} from '@unique-nft/playgrounds/types.js';2021describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {22  let alice: IKeyringPair;23  let bob: IKeyringPair;24  let charlie: IKeyringPair;2526  before(async () => {27    await usingPlaygrounds(async (helper, privateKey) => {28      const donor = await privateKey({url: import.meta.url});29      [alice, bob, charlie] = await helper.arrange.createAccounts([20n, 10n, 10n], donor);30    });31  });3233  itSub('[nft] Execute the extrinsic and check nftItemList - owner of token', async ({helper}) => {34    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-1', description: '', tokenPrefix: 'TF'});35    const nft = await collection.mintToken(alice);36    await nft.approve(alice, {Substrate: bob.address});37    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;3839    await nft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address});40    expect(await nft.getOwner()).to.be.deep.equal({Substrate: charlie.address});41  });4243  itSub('[fungible] Execute the extrinsic and check nftItemList - owner of token', async ({helper}) => {44    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-2', description: '', tokenPrefix: 'TF'});45    await collection.mint(alice, 10n);46    await collection.approveTokens(alice, {Substrate: bob.address}, 7n);47    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(7n);4849    await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 6n);50    expect(await collection.getBalance({Substrate: charlie.address})).to.be.equal(6n);51    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(4n);52    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(1n);53  });5455  itSub.ifWithPallets('[refungible] Execute the extrinsic and check nftItemList - owner of token', [Pallets.ReFungible], async ({helper}) => {56    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-3', description: '', tokenPrefix: 'TF'});57    const rft = await collection.mintToken(alice, 10n);58    await rft.approve(alice, {Substrate: bob.address}, 7n);59    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(7n);6061    await rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 6n);62    expect(await rft.getBalance({Substrate: charlie.address})).to.be.equal(6n);63    expect(await rft.getBalance({Substrate: alice.address})).to.be.equal(4n);64    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(1n);65  });6667  itSub('Should reduce allowance if value is big', async ({helper}) => {68    // fungible69    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-4', description: '', tokenPrefix: 'TF'});70    await collection.mint(alice, 500000n);7172    await collection.approveTokens(alice, {Substrate: bob.address}, 500000n);73    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(500000n);74    await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 500000n);75    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(0n);76  });7778  itSub('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async ({helper}) => {79    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-5', description: '', tokenPrefix: 'TF'});80    await collection.setLimits(alice, {ownerCanTransfer: true});8182    const nft = await collection.mintToken(alice, {Substrate: bob.address});83    await nft.transferFrom(alice, {Substrate: bob.address}, {Substrate: charlie.address});84    expect(await nft.getOwner()).to.be.deep.equal({Substrate: charlie.address});85  });86});8788describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {89  let alice: IKeyringPair;90  let bob: IKeyringPair;91  let charlie: IKeyringPair;9293  before(async () => {94    await usingPlaygrounds(async (helper, privateKey) => {95      const donor = await privateKey({url: import.meta.url});96      [alice, bob, charlie] = await helper.arrange.createAccounts([50n, 10n, 10n], donor);97    });98  });99100  itSub('transferFrom for a collection that does not exist', async ({helper}) => {101    await expect(helper.collection.approveToken(alice, NON_EXISTENT_COLLECTION_ID, 0, {Substrate: bob.address}, 1n))102      .to.be.rejectedWith(/common\.CollectionNotFound/);103    await expect(helper.collection.transferTokenFrom(bob, NON_EXISTENT_COLLECTION_ID, 0, {Substrate: alice.address}, {Substrate: bob.address}, 1n))104      .to.be.rejectedWith(/common\.CollectionNotFound/);105  });106107  /* itSub('transferFrom for a collection that was destroyed', async ({helper}) => {108      this test copies approve negative test109  }); */110111  /* itSub('transferFrom a token that does not exist', async ({helper}) => {112    this test copies approve negative test113  }); */114115  /* itSub('transferFrom a token that was deleted', async ({helper}) => {116    this test copies approve negative test117  }); */118119  itSub('[nft] transferFrom for not approved address', async ({helper}) => {120    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-1', description: '', tokenPrefix: 'TF'});121    const nft = await collection.mintToken(alice);122123    await expect(nft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}))124      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);125    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});126  });127128  itSub('[fungible] transferFrom for not approved address', async ({helper}) => {129    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-1', description: '', tokenPrefix: 'TF'});130    await collection.mint(alice, 10n);131132    await expect(collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 5n))133      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);134    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);135    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);136    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);137  });138139  itSub.ifWithPallets('[refungible] transferFrom for not approved address', [Pallets.ReFungible], async({helper}) => {140    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-3', description: '', tokenPrefix: 'TF'});141    const rft = await collection.mintToken(alice, 10n);142143    await expect(rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}))144      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);145    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);146    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);147    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);148  });149150  itSub('[nft] transferFrom incorrect token count', async ({helper}) => {151    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-4', description: '', tokenPrefix: 'TF'});152    const nft = await collection.mintToken(alice);153154    await nft.approve(alice, {Substrate: bob.address});155    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;156157    await expect(helper.collection.transferTokenFrom(158      bob,159      collection.collectionId,160      nft.tokenId,161      {Substrate: alice.address},162      {Substrate: charlie.address},163      2n,164    )).to.be.rejectedWith(/nonfungible\.NonfungibleItemsHaveNoAmount/);165    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});166  });167168  itSub('[fungible] transferFrom incorrect token count', async ({helper}) => {169    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-5', description: '', tokenPrefix: 'TF'});170    await collection.mint(alice, 10n);171172    await collection.approveTokens(alice, {Substrate: bob.address}, 2n);173    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(2n);174175    await expect(collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 5n))176      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);177    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);178    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);179    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);180  });181182  itSub.ifWithPallets('[refungible] transferFrom incorrect token count', [Pallets.ReFungible], async ({helper}) => {183    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-6', description: '', tokenPrefix: 'TF'});184    const rft = await collection.mintToken(alice, 10n);185186    await rft.approve(alice, {Substrate: bob.address}, 5n);187    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(5n);188189    await expect(rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 7n))190      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);191    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);192    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);193    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);194  });195196  itSub('[nft] execute transferFrom from account that is not owner of collection', async ({helper}) => {197    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-7', description: '', tokenPrefix: 'TF'});198    const nft = await collection.mintToken(alice);199200    await expect(nft.approve(charlie, {Substrate: bob.address})).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);201    expect(await nft.isApproved({Substrate: bob.address})).to.be.false;202203    await expect(nft.transferFrom(204      charlie,205      {Substrate: alice.address},206      {Substrate: charlie.address},207    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);208    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});209  });210211  itSub('[fungible] execute transferFrom from account that is not owner of collection', async ({helper}) => {212    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-8', description: '', tokenPrefix: 'TF'});213    await collection.mint(alice, 10000n);214215    await expect(collection.approveTokens(charlie, {Substrate: bob.address}, 1n)).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);216    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(0n);217    expect(await collection.getApprovedTokens({Substrate: charlie.address}, {Substrate: bob.address})).to.be.eq(0n);218219    await expect(collection.transferFrom(220      charlie,221      {Substrate: alice.address},222      {Substrate: charlie.address},223    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);224    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10000n);225    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);226    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);227  });228229  itSub.ifWithPallets('[refungible] execute transferFrom from account that is not owner of collection', [Pallets.ReFungible], async ({helper}) => {230    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-9', description: '', tokenPrefix: 'TF'});231    const rft = await collection.mintToken(alice, 10000n);232233    await expect(rft.approve(charlie, {Substrate: bob.address}, 1n)).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);234    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(0n);235    expect(await rft.getApprovedPieces({Substrate: charlie.address}, {Substrate: bob.address})).to.be.eq(0n);236237    await expect(rft.transferFrom(238      charlie,239      {Substrate: alice.address},240      {Substrate: charlie.address},241    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);242    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10000n);243    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);244    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);245  });246247  itSub('transferFrom burnt token before approve NFT', async ({helper}) => {248    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-10', description: '', tokenPrefix: 'TF'});249    await collection.setLimits(alice, {ownerCanTransfer: true});250    const nft = await collection.mintToken(alice);251252    await nft.burn(alice);253    await expect(nft.approve(alice, {Substrate: bob.address})).to.be.rejectedWith(/common\.TokenNotFound/);254255    await expect(nft.transferFrom(256      bob,257      {Substrate: alice.address},258      {Substrate: charlie.address},259    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);260  });261262  itSub('transferFrom burnt token before approve Fungible', async ({helper}) => {263    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-11', description: '', tokenPrefix: 'TF'});264    await collection.setLimits(alice, {ownerCanTransfer: true});265    await collection.mint(alice, 10n);266267    await collection.burnTokens(alice, 10n);268    await expect(collection.approveTokens(alice, {Substrate: bob.address})).to.be.not.rejected;269270    await expect(collection.transferFrom(271      alice,272      {Substrate: alice.address},273      {Substrate: charlie.address},274    )).to.be.rejectedWith(/common\.TokenValueTooLow/);275  });276277  itSub.ifWithPallets('transferFrom burnt token before approve ReFungible', [Pallets.ReFungible], async ({helper}) => {278    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-12', description: '', tokenPrefix: 'TF'});279    await collection.setLimits(alice, {ownerCanTransfer: true});280    const rft = await collection.mintToken(alice, 10n);281282    await rft.burn(alice, 10n);283    await expect(rft.approve(alice, {Substrate: bob.address})).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);284285    await expect(rft.transferFrom(286      alice,287      {Substrate: alice.address},288      {Substrate: charlie.address},289    )).to.be.rejectedWith(/common\.TokenValueTooLow/);290  });291292  itSub('transferFrom burnt token after approve NFT', async ({helper}) => {293    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-13', description: '', tokenPrefix: 'TF'});294    const nft = await collection.mintToken(alice);295296    await nft.approve(alice, {Substrate: bob.address});297    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;298299    await nft.burn(alice);300301    await expect(nft.transferFrom(302      bob,303      {Substrate: alice.address},304      {Substrate: charlie.address},305    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);306  });307308  itSub('transferFrom burnt token after approve Fungible', async ({helper}) => {309    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-14', description: '', tokenPrefix: 'TF'});310    await collection.mint(alice, 10n);311312    await collection.approveTokens(alice, {Substrate: bob.address});313    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(1n);314315    await collection.burnTokens(alice, 10n);316317    await expect(collection.transferFrom(318      bob,319      {Substrate: alice.address},320      {Substrate: charlie.address},321    )).to.be.rejectedWith(/common\.TokenValueTooLow/);322  });323324  itSub.ifWithPallets('transferFrom burnt token after approve ReFungible', [Pallets.ReFungible], async ({helper}) => {325    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-15', description: '', tokenPrefix: 'TF'});326    const rft = await collection.mintToken(alice, 10n);327328    await rft.approve(alice, {Substrate: bob.address}, 10n);329    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(10n);330331    await rft.burn(alice, 10n);332333    await expect(rft.transferFrom(334      bob,335      {Substrate: alice.address},336      {Substrate: charlie.address},337    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);338  });339340  itSub('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async ({helper}) => {341    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-16', description: '', tokenPrefix: 'TF'});342    const nft = await collection.mintToken(alice, {Substrate: bob.address});343344    await collection.setLimits(alice, {ownerCanTransfer: false});345346    await expect(nft.transferFrom(347      alice,348      {Substrate: bob.address},349      {Substrate: charlie.address},350    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);351  });352353  itSub('zero transfer NFT', async ({helper}) => {354    const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});355    const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});356    const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});357    await approvedNft.approve(bob, {Substrate: alice.address});358359    // 1. Cannot zero transferFrom (non-existing token)360    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');361    // 2. Cannot zero transferFrom (not approved token)362    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');363    // 3. Can zero transferFrom (approved token):364    await helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, approvedNft.tokenId, 0]);365366    // 4.1 approvedNft still approved:367    expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;368    // 4.2 bob is still the owner:369    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});370    expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});371    // 4.3 Alice can transfer approved nft:372    await approvedNft.transferFrom(alice, {Substrate: bob.address}, {Substrate: alice.address});373    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: alice.address});374  });375});