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 proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {19 from: owner,20 ...GAS_ARGS,21 });22 const proxy = await proxyContract.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);3435 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 expect(totalSupply).to.equal('200');42 });4344 itWeb3('balanceOf', async ({api, web3}) => {45 const collection = await createCollectionExpectSuccess({46 name: 'token name',47 mode: {type: 'Fungible', decimalPoints: 0},48 });49 const alice = privateKey('//Alice');50 const caller = await createEthAccountWithBalance(api, web3);5152 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: caller});5354 const address = collectionIdToAddress(collection);55 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));56 const balance = await contract.methods.balanceOf(caller).call();5758 expect(balance).to.equal('200');59 });60});6162describe('Fungible (Via EVM proxy): Plain calls', () => {63 itWeb3('Can perform approve()', async ({web3, api}) => {64 const collection = await createCollectionExpectSuccess({65 name: 'token name',66 mode: {type: 'Fungible', decimalPoints: 0},67 });68 const alice = privateKey('//Alice');69 const caller = await createEthAccountWithBalance(api, web3);70 const spender = createEthAccount(web3);7172 const address = collectionIdToAddress(collection);73 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));74 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: contract.options.address});7576 {77 const result = await contract.methods.approve(spender, 100).send({from: caller});78 const events = normalizeEvents(result.events);7980 expect(events).to.be.deep.equal([81 {82 address,83 event: 'Approval',84 args: {85 owner: contract.options.address,86 spender,87 value: '100',88 },89 },90 ]);91 }9293 {94 const allowance = await contract.methods.allowance(contract.options.address, spender).call();95 expect(+allowance).to.equal(100);96 }97 });9899 itWeb3('Can perform transferFrom()', async ({web3, api}) => {100 const collection = await createCollectionExpectSuccess({101 name: 'token name',102 mode: {type: 'Fungible', decimalPoints: 0},103 });104 const alice = privateKey('//Alice');105 const caller = await createEthAccountWithBalance(api, web3);106 const owner = await createEthAccountWithBalance(api, web3);107108 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});109110 const receiver = createEthAccount(web3);111112 const address = collectionIdToAddress(collection);113 const evmCollection = new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS});114 const contract = await proxyWrap(api, web3, evmCollection);115116 await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});117118 {119 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});120 const events = normalizeEvents(result.events);121 expect(events).to.be.deep.equal([122 {123 address,124 event: 'Transfer',125 args: {126 from: owner,127 to: receiver,128 value: '49',129 },130 },131 {132 address,133 event: 'Approval',134 args: {135 owner,136 spender: contract.options.address,137 value: '51',138 },139 },140 ]);141 }142143 {144 const balance = await contract.methods.balanceOf(receiver).call();145 expect(+balance).to.equal(49);146 }147148 {149 const balance = await contract.methods.balanceOf(owner).call();150 expect(+balance).to.equal(151);151 }152 });153154 itWeb3('Can perform transfer()', async ({web3, api}) => {155 const collection = await createCollectionExpectSuccess({156 name: 'token name',157 mode: {type: 'Fungible', decimalPoints: 0},158 });159 const alice = privateKey('//Alice');160 const caller = await createEthAccountWithBalance(api, web3);161 const receiver = await createEthAccountWithBalance(api, web3);162163 const address = collectionIdToAddress(collection);164 const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));165 await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: contract.options.address});166167 {168 const result = await contract.methods.transfer(receiver, 50).send({from: caller});169 const events = normalizeEvents(result.events);170 expect(events).to.be.deep.equal([171 {172 address,173 event: 'Transfer',174 args: {175 from: contract.options.address,176 to: receiver,177 value: '50',178 },179 },180 ]);181 }182183 {184 const balance = await contract.methods.balanceOf(contract.options.address).call();185 expect(+balance).to.equal(150);186 }187188 {189 const balance = await contract.methods.balanceOf(receiver).call();190 expect(+balance).to.equal(50);191 }192 });193});