1import privateKey from "../substrate/privateKey";2import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from "../util/helpers";3import { collectionIdToAddress, createEthAccount, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from "./util/helpers"4import fungibleAbi from './fungibleAbi.json';5import { expect } from "chai";67describe('Information getting', () => {8 itWeb3('totalSupply', async ({ web3 }) => {9 const collection = await createCollectionExpectSuccess({10 name: 'token name',11 mode: { type: 'Fungible', decimalPoints: 0 }12 });13 const alice = privateKey('//Alice');1415 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { substrate: alice.address });1617 const address = collectionIdToAddress(collection);18 const contract = new web3.eth.Contract(fungibleAbi as any, address);19 const totalSupply = await contract.methods.totalSupply().call();2021 22 expect(totalSupply).to.equal('0');23 });2425 itWeb3('balanceOf', async ({ web3 }) => {26 const collection = await createCollectionExpectSuccess({27 name: 'token name',28 mode: { type: 'Fungible', decimalPoints: 0 }29 });30 const alice = privateKey('//Alice');3132 const caller = createEthAccount(web3);33 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: caller });3435 const address = collectionIdToAddress(collection);36 const contract = new web3.eth.Contract(fungibleAbi as any, address);37 const balance = await contract.methods.balanceOf(caller).call();3839 expect(balance).to.equal('200');40 });41});4243describe('Plain calls', () => {44 itWeb3('Can perform approve()', async ({ web3, api }) => {45 const collection = await createCollectionExpectSuccess({46 name: 'token name',47 mode: { type: 'Fungible', decimalPoints: 0 }48 });49 const alice = privateKey('//Alice');5051 const owner = createEthAccount(web3);52 await transferBalanceToEth(api, alice, owner, 999999999999999);5354 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });5556 const spender = createEthAccount(web3);5758 const address = collectionIdToAddress(collection);59 const contract = new web3.eth.Contract(fungibleAbi as any, address);6061 {62 const result = await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });63 const events = normalizeEvents(result.events);6465 expect(events).to.be.deep.equal([66 {67 address,68 event: 'Approval',69 args: {70 owner,71 spender,72 value: '100',73 }74 }75 ]);76 }7778 {79 const allowance = await contract.methods.allowance(owner, spender).call();80 expect(+allowance).to.equal(100);81 }82 });8384 itWeb3('Can perform transferFrom()', async ({ web3, api }) => {85 const collection = await createCollectionExpectSuccess({86 name: 'token name',87 mode: { type: 'Fungible', decimalPoints: 0 }88 });89 const alice = privateKey('//Alice');9091 const owner = createEthAccount(web3);92 await transferBalanceToEth(api, alice, owner, 999999999999999);9394 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });9596 const spender = createEthAccount(web3);97 await transferBalanceToEth(api, alice, spender, 999999999999999);9899 const receiver = createEthAccount(web3);100101 const address = collectionIdToAddress(collection);102 const contract = new web3.eth.Contract(fungibleAbi as any, address);103104 await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });105106 {107 const result = await contract.methods.transferFrom(owner, receiver, 49).send({ from: spender, gas: '0x1000000', gasPrice: '0x01' });108 const events = normalizeEvents(result.events);109 expect(events).to.be.deep.equal([110 {111 address,112 event: 'Transfer',113 args: {114 from: owner,115 to: receiver,116 value: '49',117 }118 },119 {120 address,121 event: 'Approval',122 args: {123 owner,124 spender,125 value: '51',126 }127 }128 ]);129 }130131 {132 const balance = await contract.methods.balanceOf(receiver).call();133 expect(+balance).to.equal(49);134 }135136 {137 const balance = await contract.methods.balanceOf(owner).call();138 expect(+balance).to.equal(151);139 }140 });141142 itWeb3('Can perform transfer()', async ({ web3, api }) => {143 const collection = await createCollectionExpectSuccess({144 name: 'token name',145 mode: { type: 'Fungible', decimalPoints: 0 }146 });147 const alice = privateKey('//Alice');148149 const owner = createEthAccount(web3);150 await transferBalanceToEth(api, alice, owner, 999999999999999);151152 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });153154 const receiver = createEthAccount(web3);155 await transferBalanceToEth(api, alice, receiver, 999999999999999);156157 const address = collectionIdToAddress(collection);158 const contract = new web3.eth.Contract(fungibleAbi as any, address);159160 {161 const result = await contract.methods.transfer(receiver, 50).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });162 const events = normalizeEvents(result.events);163 expect(events).to.be.deep.equal([164 {165 address,166 event: 'Transfer',167 args: {168 from: owner,169 to: receiver,170 value: '50',171 }172 },173 ])174 }175176 {177 const balance = await contract.methods.balanceOf(owner).call();178 expect(+balance).to.equal(150);179 }180181 {182 const balance = await contract.methods.balanceOf(receiver).call();183 expect(+balance).to.equal(50);184 }185 });186});187188describe('Substrate calls', () => {189 itWeb3('Events emitted for approve()', async ({ web3 }) => {190 const collection = await createCollectionExpectSuccess({191 mode: { type: 'Fungible', decimalPoints: 0 }192 });193 const alice = privateKey('//Alice');194195 const receiver = createEthAccount(web3);196197 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });198199 const address = collectionIdToAddress(collection);200 const contract = new web3.eth.Contract(fungibleAbi as any, address);201202 const events = await recordEvents(contract, async () => {203 await approveExpectSuccess(collection, 1, alice, { ethereum: receiver }, 100);204 });205206 expect(events).to.be.deep.equal([207 {208 address,209 event: 'Approval',210 args: {211 owner: subToEth(alice.address),212 spender: receiver,213 value: '100',214 }215 }216 ]);217 });218219 itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {220 const collection = await createCollectionExpectSuccess({221 mode: { type: 'Fungible', decimalPoints: 0 }222 });223 const alice = privateKey('//Alice');224 const bob = privateKey('//Bob');225226 const receiver = createEthAccount(web3);227228 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });229 await approveExpectSuccess(collection, 1, alice, bob, 100);230231 const address = collectionIdToAddress(collection);232 const contract = new web3.eth.Contract(fungibleAbi as any, address);233234 const events = await recordEvents(contract, async () => {235 await transferFromExpectSuccess(collection, 1, bob, alice, { ethereum: receiver }, 51, 'Fungible');236 });237238 expect(events).to.be.deep.equal([239 {240 address,241 event: 'Transfer',242 args: {243 from: subToEth(alice.address),244 to: receiver,245 value: '51',246 }247 },248 {249 address,250 event: 'Approval',251 args: {252 owner: subToEth(alice.address),253 spender: subToEth(bob.address),254 value: '49',255 }256 }257 ]);258 });259260 itWeb3('Events emitted for transfer()', async ({ web3 }) => {261 const collection = await createCollectionExpectSuccess({262 mode: { type: 'Fungible', decimalPoints: 0 }263 });264 const alice = privateKey('//Alice');265266 const receiver = createEthAccount(web3);267268 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });269270 const address = collectionIdToAddress(collection);271 const contract = new web3.eth.Contract(fungibleAbi as any, address);272273 const events = await recordEvents(contract, async () => {274 await transferExpectSuccess(collection, 1, alice, { ethereum: receiver }, 51, 'Fungible');275 });276277 expect(events).to.be.deep.equal([278 {279 address,280 event: 'Transfer',281 args: {282 from: subToEth(alice.address),283 to: receiver,284 value: '51',285 }286 },287 ]);288 });289});