1234567891011121314151617import {default as usingApi} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20 getBalance,21 createMultipleItemsExpectSuccess,22 isTokenExists,23 getLastTokenId,24 getAllowance,25 approve,26 transferFrom,27 createCollection,28 transfer,29 burnItem,30 normalizeAccountId,31 CrossAccountId,32 createFungibleItemExpectSuccess,33 U128_MAX,34 burnFromExpectSuccess,35} from './util/helpers';3637import chai from 'chai';38import chaiAsPromised from 'chai-as-promised';39chai.use(chaiAsPromised);40const expect = chai.expect;4142let alice: IKeyringPair;43let bob: IKeyringPair;4445describe('integration test: Fungible functionality:', () => {46 before(async () => {47 await usingApi(async (api, privateKeyWrapper) => {48 alice = privateKeyWrapper('//Alice');49 bob = privateKeyWrapper('//Bob');50 });51 });5253 it('Create fungible collection and token', async () => {54 await usingApi(async api => {55 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}});56 expect(createCollectionResult.success).to.be.true;57 const collectionId = createCollectionResult.collectionId;58 const defaultTokenId = await getLastTokenId(api, collectionId);59 const aliceTokenId = await createFungibleItemExpectSuccess(alice, collectionId, {Value: U128_MAX}, alice.address);60 const aliceBalance = await getBalance(api, collectionId, alice, aliceTokenId); 61 const itemCountAfter = await getLastTokenId(api, collectionId);62 63 64 65 expect(itemCountAfter).to.be.equal(defaultTokenId);66 expect(aliceBalance).to.be.equal(U128_MAX);67 });68 });69 70 it('RPC method tokenOnewrs for fungible collection and token', async () => {71 await usingApi(async (api, privateKeyWrapper) => {72 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};73 const facelessCrowd = Array.from(Array(7).keys()).map(i => normalizeAccountId(privateKeyWrapper(i.toString())));74 75 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}});76 const collectionId = createCollectionResult.collectionId;77 const aliceTokenId = await createFungibleItemExpectSuccess(alice, collectionId, {Value: U128_MAX}, alice.address);78 79 await transfer(api, collectionId, aliceTokenId, alice, bob, 1000n);80 await transfer(api, collectionId, aliceTokenId, alice, ethAcc, 900n);81 82 for (let i = 0; i < 7; i++) {83 await transfer(api, collectionId, aliceTokenId, alice, facelessCrowd[i], 1);84 } 85 86 const owners = await api.rpc.unique.tokenOwners(collectionId, aliceTokenId);87 const ids = (owners.toJSON() as CrossAccountId[]).map(s => normalizeAccountId(s));88 const aliceID = normalizeAccountId(alice);89 const bobId = normalizeAccountId(bob);9091 92 93 expect(ids).to.deep.include.members([aliceID, ethAcc, bobId, ...facelessCrowd]);94 expect(owners.length == 10).to.be.true;95 96 const eleven = privateKeyWrapper('11');97 expect(await transfer(api, collectionId, aliceTokenId, alice, eleven, 10n)).to.be.true;98 expect((await api.rpc.unique.tokenOwners(collectionId, aliceTokenId)).length).to.be.equal(10);99 });100 });101 102 it('Transfer token', async () => {103 await usingApi(async api => {104 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};105 const collectionId = (await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}})).collectionId;106 const tokenId = await createFungibleItemExpectSuccess(alice, collectionId, {Value: 500n}, alice.address);107108 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(500n);109 expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;110 expect(await transfer(api, collectionId, tokenId, alice, ethAcc, 140n)).to.be.true;111112 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(300n);113 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);114 expect(await getBalance(api, collectionId, ethAcc, tokenId)).to.be.equal(140n);115 await expect(transfer(api, collectionId, tokenId, alice, bob, 350n)).to.eventually.be.rejected;116 });117 });118119 it('Tokens multiple creation', async () => {120 await usingApi(async api => {121 const collectionId = (await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}})).collectionId;122 123 const args = [124 {Fungible: {Value: 500n}},125 {Fungible: {Value: 400n}},126 {Fungible: {Value: 300n}},127 ];128 129 await createMultipleItemsExpectSuccess(alice, collectionId, args);130 expect(await getBalance(api, collectionId, alice, 0)).to.be.equal(1200n);131 }); 132 });133134 it('Burn some tokens ', async () => {135 await usingApi(async api => { 136 const collectionId = (await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}})).collectionId;137 const tokenId = (await createFungibleItemExpectSuccess(alice, collectionId, {Value: 500n}, alice.address));138 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;139 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(500n);140 expect(await burnItem(api, alice, collectionId, tokenId, 499n)).to.be.true;141 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;142 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);143 });144 });145 146 it('Burn all tokens ', async () => {147 await usingApi(async api => { 148 const collectionId = (await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}})).collectionId;149 const tokenId = (await createFungibleItemExpectSuccess(alice, collectionId, {Value: 500n}, alice.address));150 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;151 expect(await burnItem(api, alice, collectionId, tokenId, 500n)).to.be.true;152 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;153 154 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);155 expect((await api.rpc.unique.totalPieces(collectionId, tokenId)).value.toBigInt()).to.be.equal(0n);156 });157 });158159 it('Set allowance for token', async () => {160 await usingApi(async api => {161 const collectionId = (await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}})).collectionId;162 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};163 164 const tokenId = (await createFungibleItemExpectSuccess(alice, collectionId, {Value: 100n}, alice.address));165 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);166167 expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;168 expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);169 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(0n);170 171 expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob, 20n)).to.be.true;172 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);173 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);174 expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);175 176 await burnFromExpectSuccess(bob, alice, collectionId, tokenId, 10n);177 178 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(70n);179 expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(30n);180 expect(await transferFrom(api, collectionId, tokenId, bob, alice, ethAcc, 10n)).to.be.true;181 expect(await getBalance(api, collectionId, ethAcc, tokenId)).to.be.equal(10n);182 });183 });184});