1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {U128_MAX} from './util/helpers';1920import {usingPlaygrounds} from './util/playgrounds';2122import chai from 'chai';23import chaiAsPromised from 'chai-as-promised';24chai.use(chaiAsPromised);25const expect = chai.expect;2627let alice: IKeyringPair;28let bob: IKeyringPair;2930describe('integration test: Fungible functionality:', () => {31 before(async () => {32 await usingPlaygrounds(async (helper, privateKey) => {33 alice = privateKey('//Alice');34 bob = privateKey('//Bob');35 });36 });3738 it('Create fungible collection and token', async () => {39 await usingPlaygrounds(async helper => {40 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'trest'});41 const defaultTokenId = await collection.getLastTokenId();42 expect(defaultTokenId).to.be.equal(0);4344 await collection.mint(alice, {Substrate: alice.address}, U128_MAX);45 const aliceBalance = await collection.getBalance({Substrate: alice.address});46 const itemCountAfter = await collection.getLastTokenId();4748 expect(itemCountAfter).to.be.equal(defaultTokenId);49 expect(aliceBalance).to.be.equal(U128_MAX);50 });51 });52 53 it('RPC method tokenOnewrs for fungible collection and token', async () => {54 await usingPlaygrounds(async (helper, privateKey) => {55 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};56 const facelessCrowd = Array(7).fill(0).map((_, i) => ({Substrate: privateKey(`//Alice+${i}`).address}));5758 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});5960 await collection.mint(alice, {Substrate: alice.address}, U128_MAX);6162 await collection.transfer(alice, {Substrate: bob.address}, 1000n);63 await collection.transfer(alice, ethAcc, 900n);64 65 for (let i = 0; i < 7; i++) {66 await collection.transfer(alice, facelessCrowd[i], 1n);67 } 6869 const owners = await collection.getTop10Owners();7071 72 expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);73 expect(owners.length).to.be.equal(10);74 75 const eleven = privateKey('//ALice+11');76 expect(await collection.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;77 expect((await collection.getTop10Owners()).length).to.be.equal(10);78 });79 });80 81 it('Transfer token', async () => {82 await usingPlaygrounds(async helper => {83 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};84 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});85 await collection.mint(alice, {Substrate: alice.address}, 500n);8687 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(500n);88 expect(await collection.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;89 expect(await collection.transfer(alice, ethAcc, 140n)).to.be.true;9091 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(300n);92 expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(60n);93 expect(await collection.getBalance(ethAcc)).to.be.equal(140n);9495 await expect(collection.transfer(alice, {Substrate: bob.address}, 350n)).to.eventually.be.rejected;96 });97 });9899 it('Tokens multiple creation', async () => {100 await usingPlaygrounds(async helper => {101 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});102103 await collection.mintWithOneOwner(alice, {Substrate: alice.address}, [104 {value: 500n},105 {value: 400n},106 {value: 300n},107 ]);108109 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(1200n);110 });111 });112113 it('Burn some tokens ', async () => {114 await usingPlaygrounds(async helper => {115 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});116 await collection.mint(alice, {Substrate: alice.address}, 500n);117118 expect(await collection.isTokenExists(0)).to.be.true;119 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(500n);120 expect(await collection.burnTokens(alice, 499n)).to.be.true;121 expect(await collection.isTokenExists(0)).to.be.true;122 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(1n);123 });124 });125 126 it('Burn all tokens ', async () => {127 await usingPlaygrounds(async helper => {128 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});129 await collection.mint(alice, {Substrate: alice.address}, 500n);130131 expect(await collection.isTokenExists(0)).to.be.true;132 expect(await collection.burnTokens(alice, 500n)).to.be.true;133 expect(await collection.isTokenExists(0)).to.be.true;134135 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(0n);136 expect(await collection.getTotalPieces()).to.be.equal(0n);137 });138 });139140 it('Set allowance for token', async () => {141 await usingPlaygrounds(async helper => {142 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});143 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};144 await collection.mint(alice, {Substrate: alice.address}, 100n);145146 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(100n);147 148 expect(await collection.approveTokens(alice, {Substrate: bob.address}, 60n)).to.be.true;149 expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);150 expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(0n);151152 expect(await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;153 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(80n);154 expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(20n);155 expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);156157 await collection.burnTokensFrom(bob, {Substrate: alice.address}, 10n);158159 expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(70n);160 expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(30n);161 expect(await collection.transferFrom(bob, {Substrate: alice.address}, ethAcc, 10n)).to.be.true;162 expect(await collection.getBalance(ethAcc)).to.be.equal(10n);163 });164 });165});