123456import privateKey from '../../substrate/privateKey';7import { createCollectionExpectSuccess, createFungibleItemExpectSuccess } from '../../util/helpers';8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents } from '../util/helpers';9import fungibleAbi from '../fungibleAbi.json';10import { expect } from 'chai';11import { ApiPromise } from '@polkadot/api';12import Web3 from 'web3';13import { readFile } from 'fs/promises';1415async function proxyWrap(api: ApiPromise, web3: Web3, wrapped: any) {16 17 const owner = await createEthAccountWithBalance(api, web3);18 const Proxy = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {19 from: owner,20 ...GAS_ARGS,21 });22 const proxy = await Proxy.deploy({ data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address] }).send({ from: owner });23 return proxy;24}2526describe('Fungible (Via EVM proxy): Information getting', () => {27 itWeb3('totalSupply', async ({ api, web3 }) => {28 const collection = await createCollectionExpectSuccess({29 name: 'token name',30 mode: { type: 'Fungible', decimalPoints: 0 },31 });32 const alice = privateKey('//Alice');33 const caller = await createEthAccountWithBalance(api, web3);34 35 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { substrate: alice.address });3637 const address = collectionIdToAddress(collection);38 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));39 const totalSupply = await contract.methods.totalSupply().call();4041 42 expect(totalSupply).to.equal('0');43 });4445 itWeb3('balanceOf', async ({ api, web3 }) => {46 const collection = await createCollectionExpectSuccess({47 name: 'token name',48 mode: { type: 'Fungible', decimalPoints: 0 },49 });50 const alice = privateKey('//Alice');51 const caller = await createEthAccountWithBalance(api, web3);5253 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: caller });5455 const address = collectionIdToAddress(collection);56 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));57 const balance = await contract.methods.balanceOf(caller).call();5859 expect(balance).to.equal('200');60 });61});6263describe('Fungible (Via EVM proxy): Plain calls', () => {64 itWeb3('Can perform approve()', async ({ web3, api }) => {65 const collection = await createCollectionExpectSuccess({66 name: 'token name',67 mode: { type: 'Fungible', decimalPoints: 0 },68 });69 const alice = privateKey('//Alice');70 const caller = await createEthAccountWithBalance(api, web3);71 const spender = createEthAccount(web3);7273 const address = collectionIdToAddress(collection);74 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));75 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: contract.options.address });7677 {78 const result = await contract.methods.approve(spender, 100).send({from: caller});79 const events = normalizeEvents(result.events);8081 expect(events).to.be.deep.equal([82 {83 address,84 event: 'Approval',85 args: {86 owner: contract.options.address,87 spender,88 value: '100',89 },90 },91 ]);92 }9394 {95 const allowance = await contract.methods.allowance(contract.options.address, spender).call();96 expect(+allowance).to.equal(100);97 }98 });99100 itWeb3('Can perform transferFrom()', async ({ web3, api }) => {101 const collection = await createCollectionExpectSuccess({102 name: 'token name',103 mode: { type: 'Fungible', decimalPoints: 0 },104 });105 const alice = privateKey('//Alice');106 const caller = await createEthAccountWithBalance(api, web3);107 const owner = await createEthAccountWithBalance(api, web3);108109 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });110111 const receiver = createEthAccount(web3);112113 const address = collectionIdToAddress(collection);114 const evmCollection = new web3.eth.Contract(fungibleAbi as any, address, { from: caller, ...GAS_ARGS });115 const contract = await proxyWrap(api, web3, evmCollection);116117 await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});118119 {120 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});121 const events = normalizeEvents(result.events);122 expect(events).to.be.deep.equal([123 {124 address,125 event: 'Transfer',126 args: {127 from: owner,128 to: receiver,129 value: '49',130 },131 },132 {133 address,134 event: 'Approval',135 args: {136 owner,137 spender: contract.options.address,138 value: '51',139 },140 },141 ]);142 }143144 {145 const balance = await contract.methods.balanceOf(receiver).call();146 expect(+balance).to.equal(49);147 }148149 {150 const balance = await contract.methods.balanceOf(owner).call();151 expect(+balance).to.equal(151);152 }153 });154155 itWeb3('Can perform transfer()', async ({ web3, api }) => {156 const collection = await createCollectionExpectSuccess({157 name: 'token name',158 mode: { type: 'Fungible', decimalPoints: 0 },159 });160 const alice = privateKey('//Alice');161 const caller = await createEthAccountWithBalance(api, web3);162 const receiver = await createEthAccountWithBalance(api, web3);163164 const address = collectionIdToAddress(collection);165 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));166 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: contract.options.address });167168 {169 const result = await contract.methods.transfer(receiver, 50).send({ from: caller});170 const events = normalizeEvents(result.events);171 expect(events).to.be.deep.equal([172 {173 address,174 event: 'Transfer',175 args: {176 from: contract.options.address,177 to: receiver,178 value: '50',179 },180 },181 ]);182 }183184 {185 const balance = await contract.methods.balanceOf(contract.options.address).call();186 expect(+balance).to.equal(150);187 }188189 {190 const balance = await contract.methods.balanceOf(receiver).call();191 expect(+balance).to.equal(50);192 }193 });194});