difftreelog
Merge pull request #234 from UniqueNetwork/feature/core-214
in: master
CORE-214. Zero value transfer tests
1 file changed
tests/src/transfer.test.tsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import {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} from './util/helpers';2324let alice: IKeyringPair;25let bob: IKeyringPair;26let charlie: IKeyringPair;2728describe('Integration Test Transfer(recipient, collection_id, item_id, value)', () => {29 it('Balance transfers and check balance', async () => {30 await usingApi(async (api: ApiPromise) => {31 const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);3233 const alicePrivateKey = privateKey('//Alice');3435 const transfer = api.tx.balances.transfer(bobsPublicKey, 1n);36 const events = await submitTransactionAsync(alicePrivateKey, transfer);37 const result = getCreateItemResult(events);38 // tslint:disable-next-line:no-unused-expression39 expect(result.success).to.be.true;4041 const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);4243 // tslint:disable-next-line:no-unused-expression44 expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;45 // tslint:disable-next-line:no-unused-expression46 expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;47 });48 });4950 it('Inability to pay fees error message is correct', async () => {51 await usingApi(async (api) => {52 // Find unused address53 const pk = await findUnusedAddress(api);5455 const badTransfer = api.tx.balances.transfer(bobsPublicKey, 1n);56 // const events = await submitTransactionAsync(pk, badTransfer);57 const badTransaction = async () => {58 const events = await submitTransactionAsync(pk, badTransfer);59 const result = getCreateCollectionResult(events);60 // tslint:disable-next-line:no-unused-expression61 expect(result.success).to.be.false;62 };63 await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees , e.g. account balance too low');64 });65 });6667 it('User can transfer owned token', async () => {68 await usingApi(async () => {69 const alice = privateKey('//Alice');70 const bob = privateKey('//Bob');71 // nft72 const nftCollectionId = await createCollectionExpectSuccess();73 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');74 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 1, 'NFT');75 // fungible76 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});77 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');78 await transferExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob, 1, 'Fungible');79 // reFungible80 const reFungibleCollectionId = await81 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});82 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');83 await transferExpectSuccess(84 reFungibleCollectionId,85 newReFungibleTokenId,86 alice,87 bob,88 100,89 'ReFungible',90 );91 });92 });9394 it('Collection admin can transfer owned token', async () => {95 await usingApi(async () => {96 const alice = privateKey('//Alice');97 const bob = privateKey('//Bob');98 // nft99 const nftCollectionId = await createCollectionExpectSuccess();100 await addCollectionAdminExpectSuccess(alice, nftCollectionId, bob.address);101 const newNftTokenId = await createItemExpectSuccess(bob, nftCollectionId, 'NFT', bob.address);102 await transferExpectSuccess(nftCollectionId, newNftTokenId, bob, alice, 1, 'NFT');103 // fungible104 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});105 await addCollectionAdminExpectSuccess(alice, fungibleCollectionId, bob.address);106 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible', bob.address);107 await transferExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, 1, 'Fungible');108 // reFungible109 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});110 await addCollectionAdminExpectSuccess(alice, reFungibleCollectionId, bob.address);111 const newReFungibleTokenId = await createItemExpectSuccess(bob, reFungibleCollectionId, 'ReFungible', bob.address);112 await transferExpectSuccess(113 reFungibleCollectionId,114 newReFungibleTokenId,115 bob,116 alice,117 100,118 'ReFungible',119 );120 });121 });122});123124describe('Negative Integration Test Transfer(recipient, collection_id, item_id, value)', () => {125 before(async () => {126 await usingApi(async () => {127 alice = privateKey('//Alice');128 bob = privateKey('//Bob');129 charlie = privateKey('//Charlie');130 });131 });132 it('Transfer with not existed collection_id', async () => {133 await usingApi(async (api) => {134 // nft135 const nftCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();136 await transferExpectFailure(nftCollectionCount + 1, 1, alice, bob, 1);137 // fungible138 const fungibleCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();139 await transferExpectFailure(fungibleCollectionCount + 1, 0, alice, bob, 1);140 // reFungible141 const reFungibleCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();142 await transferExpectFailure(reFungibleCollectionCount + 1, 1, alice, bob, 1);143 });144 });145 it('Transfer with deleted collection_id', async () => {146 // nft147 const nftCollectionId = await createCollectionExpectSuccess();148 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');149 await destroyCollectionExpectSuccess(nftCollectionId);150 await transferExpectFailure(nftCollectionId, newNftTokenId, alice, bob, 1);151 // fungible152 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});153 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');154 await destroyCollectionExpectSuccess(fungibleCollectionId);155 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, alice, bob, 1);156 // reFungible157 const reFungibleCollectionId = await158 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});159 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');160 await destroyCollectionExpectSuccess(reFungibleCollectionId);161 await transferExpectFailure(162 reFungibleCollectionId,163 newReFungibleTokenId,164 alice,165 bob,166 1,167 );168 });169 it('Transfer with not existed item_id', async () => {170 // nft171 const nftCollectionId = await createCollectionExpectSuccess();172 await transferExpectFailure(nftCollectionId, 2, alice, bob, 1);173 // fungible174 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});175 await transferExpectFailure(fungibleCollectionId, 2, alice, bob, 1);176 // reFungible177 const reFungibleCollectionId = await178 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});179 await transferExpectFailure(180 reFungibleCollectionId,181 2,182 alice,183 bob,184 1,185 );186 });187 it('Transfer with deleted item_id', async () => {188 // nft189 const nftCollectionId = await createCollectionExpectSuccess();190 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');191 await burnItemExpectSuccess(alice, nftCollectionId, newNftTokenId, 1);192 await transferExpectFailure(nftCollectionId, newNftTokenId, alice, bob, 1);193 // fungible194 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});195 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');196 await burnItemExpectSuccess(alice, fungibleCollectionId, newFungibleTokenId, 10);197 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, alice, bob, 1);198 // reFungible199 const reFungibleCollectionId = await200 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});201 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');202 await burnItemExpectSuccess(alice, reFungibleCollectionId, newReFungibleTokenId, 100);203 await transferExpectFailure(204 reFungibleCollectionId,205 newReFungibleTokenId,206 alice,207 bob,208 1,209 );210 });211 it('Transfer with recipient that is not owner', async () => {212 // nft213 const nftCollectionId = await createCollectionExpectSuccess();214 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');215 await transferExpectFailure(nftCollectionId, newNftTokenId, charlie, bob, 1);216 // fungible217 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});218 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');219 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, charlie, bob, 1);220 // reFungible221 const reFungibleCollectionId = await222 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});223 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');224 await transferExpectFailure(225 reFungibleCollectionId,226 newReFungibleTokenId,227 charlie,228 bob,229 1,230 );231 });232});1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import {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 toSubstrateAddress,23 getTokenOwner,24 normalizeAccountId,25 getBalance as getTokenBalance,26} from './util/helpers';2728let alice: IKeyringPair;29let bob: IKeyringPair;30let charlie: IKeyringPair;3132describe('Integration Test Transfer(recipient, collection_id, item_id, value)', () => {33 it('Balance transfers and check balance', async () => {34 await usingApi(async (api: ApiPromise) => {35 const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);3637 const alicePrivateKey = privateKey('//Alice');3839 const transfer = api.tx.balances.transfer(bobsPublicKey, 1n);40 const events = await submitTransactionAsync(alicePrivateKey, transfer);41 const result = getCreateItemResult(events);42 // tslint:disable-next-line:no-unused-expression43 expect(result.success).to.be.true;4445 const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);4647 // tslint:disable-next-line:no-unused-expression48 expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;49 // tslint:disable-next-line:no-unused-expression50 expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;51 });52 });5354 it('Inability to pay fees error message is correct', async () => {55 await usingApi(async (api) => {56 // Find unused address57 const pk = await findUnusedAddress(api);5859 const badTransfer = api.tx.balances.transfer(bobsPublicKey, 1n);60 // const events = await submitTransactionAsync(pk, badTransfer);61 const badTransaction = async () => {62 const events = await submitTransactionAsync(pk, badTransfer);63 const result = getCreateCollectionResult(events);64 // tslint:disable-next-line:no-unused-expression65 expect(result.success).to.be.false;66 };67 await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees , e.g. account balance too low');68 });69 });7071 it('User can transfer owned token', async () => {72 await usingApi(async () => {73 const alice = privateKey('//Alice');74 const bob = privateKey('//Bob');75 // nft76 const nftCollectionId = await createCollectionExpectSuccess();77 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');78 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 1, 'NFT');79 // fungible80 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});81 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');82 await transferExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob, 1, 'Fungible');83 // reFungible84 const reFungibleCollectionId = await85 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});86 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');87 await transferExpectSuccess(88 reFungibleCollectionId,89 newReFungibleTokenId,90 alice,91 bob,92 100,93 'ReFungible',94 );95 });96 });9798 it('Collection admin can transfer owned token', async () => {99 await usingApi(async () => {100 const alice = privateKey('//Alice');101 const bob = privateKey('//Bob');102 // nft103 const nftCollectionId = await createCollectionExpectSuccess();104 await addCollectionAdminExpectSuccess(alice, nftCollectionId, bob.address);105 const newNftTokenId = await createItemExpectSuccess(bob, nftCollectionId, 'NFT', bob.address);106 await transferExpectSuccess(nftCollectionId, newNftTokenId, bob, alice, 1, 'NFT');107 // fungible108 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});109 await addCollectionAdminExpectSuccess(alice, fungibleCollectionId, bob.address);110 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible', bob.address);111 await transferExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, 1, 'Fungible');112 // reFungible113 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});114 await addCollectionAdminExpectSuccess(alice, reFungibleCollectionId, bob.address);115 const newReFungibleTokenId = await createItemExpectSuccess(bob, reFungibleCollectionId, 'ReFungible', bob.address);116 await transferExpectSuccess(117 reFungibleCollectionId,118 newReFungibleTokenId,119 bob,120 alice,121 100,122 'ReFungible',123 );124 });125 });126});127128describe('Negative Integration Test Transfer(recipient, collection_id, item_id, value)', () => {129 before(async () => {130 await usingApi(async () => {131 alice = privateKey('//Alice');132 bob = privateKey('//Bob');133 charlie = privateKey('//Charlie');134 });135 });136 it('Transfer with not existed collection_id', async () => {137 await usingApi(async (api) => {138 // nft139 const nftCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();140 await transferExpectFailure(nftCollectionCount + 1, 1, alice, bob, 1);141 // fungible142 const fungibleCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();143 await transferExpectFailure(fungibleCollectionCount + 1, 0, alice, bob, 1);144 // reFungible145 const reFungibleCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();146 await transferExpectFailure(reFungibleCollectionCount + 1, 1, alice, bob, 1);147 });148 });149 it('Transfer with deleted collection_id', async () => {150 // nft151 const nftCollectionId = await createCollectionExpectSuccess();152 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');153 await destroyCollectionExpectSuccess(nftCollectionId);154 await transferExpectFailure(nftCollectionId, newNftTokenId, alice, bob, 1);155 // fungible156 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});157 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');158 await destroyCollectionExpectSuccess(fungibleCollectionId);159 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, alice, bob, 1);160 // reFungible161 const reFungibleCollectionId = await162 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});163 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');164 await destroyCollectionExpectSuccess(reFungibleCollectionId);165 await transferExpectFailure(166 reFungibleCollectionId,167 newReFungibleTokenId,168 alice,169 bob,170 1,171 );172 });173 it('Transfer with not existed item_id', async () => {174 // nft175 const nftCollectionId = await createCollectionExpectSuccess();176 await transferExpectFailure(nftCollectionId, 2, alice, bob, 1);177 // fungible178 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});179 await transferExpectFailure(fungibleCollectionId, 2, alice, bob, 1);180 // reFungible181 const reFungibleCollectionId = await182 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});183 await transferExpectFailure(184 reFungibleCollectionId,185 2,186 alice,187 bob,188 1,189 );190 });191 it('Transfer with deleted item_id', async () => {192 // nft193 const nftCollectionId = await createCollectionExpectSuccess();194 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');195 await burnItemExpectSuccess(alice, nftCollectionId, newNftTokenId, 1);196 await transferExpectFailure(nftCollectionId, newNftTokenId, alice, bob, 1);197 // fungible198 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});199 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');200 await burnItemExpectSuccess(alice, fungibleCollectionId, newFungibleTokenId, 10);201 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, alice, bob, 1);202 // reFungible203 const reFungibleCollectionId = await204 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});205 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');206 await burnItemExpectSuccess(alice, reFungibleCollectionId, newReFungibleTokenId, 100);207 await transferExpectFailure(208 reFungibleCollectionId,209 newReFungibleTokenId,210 alice,211 bob,212 1,213 );214 });215 it('Transfer with recipient that is not owner', async () => {216 // nft217 const nftCollectionId = await createCollectionExpectSuccess();218 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');219 await transferExpectFailure(nftCollectionId, newNftTokenId, charlie, bob, 1);220 // fungible221 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});222 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');223 await transferExpectFailure(fungibleCollectionId, newFungibleTokenId, charlie, bob, 1);224 // reFungible225 const reFungibleCollectionId = await226 createCollectionExpectSuccess({mode: {type: 'ReFungible'}});227 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');228 await transferExpectFailure(229 reFungibleCollectionId,230 newReFungibleTokenId,231 charlie,232 bob,233 1,234 );235 });236});237238describe('Zero value transfer(From)', () => {239 before(async () => {240 await usingApi(async () => {241 alice = privateKey('//Alice');242 bob = privateKey('//Bob');243 });244 });245246 it('NFT', async () => {247 await usingApi(async (api: ApiPromise) => {248 const nftCollectionId = await createCollectionExpectSuccess();249 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');250251 const transferTx = api.tx.nft.transfer(normalizeAccountId(bob), nftCollectionId, newNftTokenId, 0);252 await submitTransactionAsync(alice, transferTx);253 const address = normalizeAccountId(await getTokenOwner(api, nftCollectionId, newNftTokenId));254255 expect(toSubstrateAddress(address)).to.be.equal(alice.address);256 });257 });258259 it('RFT', async () => {260 await usingApi(async (api: ApiPromise) => {261 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});262 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');263 const balanceBeforeAlice = await getTokenBalance(api, reFungibleCollectionId, normalizeAccountId(alice), newReFungibleTokenId);264 const balanceBeforeBob = await getTokenBalance(api, reFungibleCollectionId, normalizeAccountId(bob), newReFungibleTokenId);265266 const transferTx = api.tx.nft.transfer(normalizeAccountId(bob), reFungibleCollectionId, newReFungibleTokenId, 0);267 await submitTransactionAsync(alice, transferTx);268269 const balanceAfterAlice = await getTokenBalance(api, reFungibleCollectionId, normalizeAccountId(alice), newReFungibleTokenId);270 const balanceAfterBob = await getTokenBalance(api, reFungibleCollectionId, normalizeAccountId(bob), newReFungibleTokenId);271272 expect((balanceBeforeAlice)).to.be.equal(balanceAfterAlice);273 expect((balanceBeforeBob)).to.be.equal(balanceAfterBob);274 });275 });276277 it('Fungible', async () => {278 await usingApi(async (api: ApiPromise) => {279 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});280 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');281 const balanceBeforeAlice = await getTokenBalance(api, fungibleCollectionId, normalizeAccountId(alice), newFungibleTokenId);282 const balanceBeforeBob = await getTokenBalance(api, fungibleCollectionId, normalizeAccountId(bob), newFungibleTokenId);283284 const transferTx = api.tx.nft.transfer(normalizeAccountId(bob), fungibleCollectionId, newFungibleTokenId, 0);285 await submitTransactionAsync(alice, transferTx);286287 const balanceAfterAlice = await getTokenBalance(api, fungibleCollectionId, normalizeAccountId(alice), newFungibleTokenId);288 const balanceAfterBob = await getTokenBalance(api, fungibleCollectionId, normalizeAccountId(bob), newFungibleTokenId);289290 expect((balanceBeforeAlice)).to.be.equal(balanceAfterAlice);291 expect((balanceBeforeBob)).to.be.equal(balanceAfterBob);292 });293 });294});