123456import {ApiPromise} from '@polkadot/api';7import {IKeyringPair} from '@polkadot/types/types';8import {expect} from 'chai';9import {alicesPublicKey, bobsPublicKey} from './accounts';10import getBalance from './substrate/get-balance';11import privateKey from './substrate/privateKey';12import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api';13import {14 burnItemExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess,15 destroyCollectionExpectSuccess,16 findUnusedAddress,17 getCreateCollectionResult,18 getCreateItemResult,19 transferExpectFailure,20 transferExpectSuccess,21 addCollectionAdminExpectSuccess,22 getCreatedCollectionCount,23} from './util/helpers';2425let alice: IKeyringPair;26let bob: IKeyringPair;27let charlie: IKeyringPair;2829describe('Integration Test Transfer(recipient, collection_id, item_id, value)', () => {30 it('Balance transfers and check balance', async () => {31 await usingApi(async (api: ApiPromise) => {32 const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);3334 const alicePrivateKey = privateKey('//Alice');3536 const transfer = api.tx.balances.transfer(bobsPublicKey, 1n);37 const events = await submitTransactionAsync(alicePrivateKey, transfer);38 const result = getCreateItemResult(events);39 40 expect(result.success).to.be.true;4142 const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);4344 45 expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;46 47 expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;48 });49 });5051 it('Inability to pay fees error message is correct', async () => {52 await usingApi(async (api) => {53 54 const pk = await findUnusedAddress(api);5556 const badTransfer = api.tx.balances.transfer(bobsPublicKey, 1n);57 58 const badTransaction = async () => {59 const events = await submitTransactionAsync(pk, badTransfer);60 const result = getCreateCollectionResult(events);61 62 expect(result.success).to.be.false;63 };64 await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees , e.g. account balance too low');65 });66 });6768 it('User can transfer owned token', async () => {69 await usingApi(async () => {70 const alice = privateKey('//Alice');71 const bob = privateKey('//Bob');72 73 const nftCollectionId = await createCollectionExpectSuccess();74 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');75 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 1, 'NFT');76 77 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});78 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');79 await transferExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob, 1, 'Fungible');80 81 const reFungibleCollectionId = await82 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});83 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');84 await transferExpectSuccess(85 reFungibleCollectionId,86 newReFungibleTokenId,87 alice,88 bob,89 100,90 'ReFungible',91 );92 });93 });9495 it('Collection admin can transfer owned token', async () => {96 await usingApi(async () => {97 const alice = privateKey('//Alice');98 const bob = privateKey('//Bob');99 100 const nftCollectionId = await createCollectionExpectSuccess();101 await addCollectionAdminExpectSuccess(alice, nftCollectionId, bob.address);102 const newNftTokenId = await createItemExpectSuccess(bob, nftCollectionId, 'NFT', bob.address);103 await transferExpectSuccess(nftCollectionId, newNftTokenId, bob, alice, 1, 'NFT');104 105 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});106 await addCollectionAdminExpectSuccess(alice, fungibleCollectionId, bob.address);107 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible', bob.address);108 await transferExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, 1, 'Fungible');109 110 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});111 await addCollectionAdminExpectSuccess(alice, reFungibleCollectionId, bob.address);112 const newReFungibleTokenId = await createItemExpectSuccess(bob, reFungibleCollectionId, 'ReFungible', bob.address);113 await transferExpectSuccess(114 reFungibleCollectionId,115 newReFungibleTokenId,116 bob,117 alice,118 100,119 'ReFungible',120 );121 });122 });123});124125describe('Negative Integration Test Transfer(recipient, collection_id, item_id, value)', () => {126 before(async () => {127 await usingApi(async () => {128 alice = privateKey('//Alice');129 bob = privateKey('//Bob');130 charlie = privateKey('//Charlie');131 });132 });133 it('Transfer with not existed collection_id', async () => {134 await usingApi(async (api) => {135 136 const nftCollectionCount = await getCreatedCollectionCount(api);137 await transferExpectFailure(nftCollectionCount + 1, 1, alice, bob, 1);138 139 const fungibleCollectionCount = await getCreatedCollectionCount(api);140 await transferExpectFailure(fungibleCollectionCount + 1, 0, alice, bob, 1);141 142 const reFungibleCollectionCount = await getCreatedCollectionCount(api);143 await transferExpectFailure(reFungibleCollectionCount + 1, 1, alice, bob, 1);144 });145 });146 it('Transfer with deleted collection_id', async () => {147 148 const nftCollectionId = await createCollectionExpectSuccess();149 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');150 await destroyCollectionExpectSuccess(nftCollectionId);151 await transferExpectFailure(nftCollectionId, newNftTokenId, alice, bob, 1);152 153 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});154 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');155 await destroyCollectionExpectSuccess(fungibleCollectionId);156 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, alice, bob, 1);157 158 const reFungibleCollectionId = await159 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});160 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');161 await destroyCollectionExpectSuccess(reFungibleCollectionId);162 await transferExpectFailure(163 reFungibleCollectionId,164 newReFungibleTokenId,165 alice,166 bob,167 1,168 );169 });170 it('Transfer with not existed item_id', async () => {171 172 const nftCollectionId = await createCollectionExpectSuccess();173 await transferExpectFailure(nftCollectionId, 2, alice, bob, 1);174 175 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});176 await transferExpectFailure(fungibleCollectionId, 2, alice, bob, 1);177 178 const reFungibleCollectionId = await179 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});180 await transferExpectFailure(181 reFungibleCollectionId,182 2,183 alice,184 bob,185 1,186 );187 });188 it('Transfer with deleted item_id', async () => {189 190 const nftCollectionId = await createCollectionExpectSuccess();191 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');192 await burnItemExpectSuccess(alice, nftCollectionId, newNftTokenId, 1);193 await transferExpectFailure(nftCollectionId, newNftTokenId, alice, bob, 1);194 195 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});196 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');197 await burnItemExpectSuccess(alice, fungibleCollectionId, newFungibleTokenId, 10);198 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, alice, bob, 1);199 200 const reFungibleCollectionId = await201 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});202 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');203 await burnItemExpectSuccess(alice, reFungibleCollectionId, newReFungibleTokenId, 100);204 await transferExpectFailure(205 reFungibleCollectionId,206 newReFungibleTokenId,207 alice,208 bob,209 1,210 );211 });212 it('Transfer with recipient that is not owner', async () => {213 214 const nftCollectionId = await createCollectionExpectSuccess();215 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');216 await transferExpectFailure(nftCollectionId, newNftTokenId, charlie, bob, 1);217 218 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});219 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');220 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, charlie, bob, 1);221 222 const reFungibleCollectionId = await223 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});224 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');225 await transferExpectFailure(226 reFungibleCollectionId,227 newReFungibleTokenId,228 charlie,229 bob,230 1,231 );232 });233});