1234567891011121314151617import {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 Pallets36} 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 93 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 154155156157158159 160161162163164165 166167168169170171 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 242 expect(e).to.be.exist;243 }244245 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 260 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 278 expect(e).to.be.exist;279 }280 });281 });282 it('transferFrom burnt token before approve NFT', async () => {283 await usingApi(async () => {284 285 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 321 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});