1234567891011121314151617import {GAS_ARGS, normalizeEvents} from '../util/helpers';18import {expect} from 'chai';19import {readFile} from 'fs/promises';20import {IKeyringPair} from '@polkadot/types/types';21import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util/playgrounds';2223async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {24 25 const owner = await helper.eth.createAccountWithBalance(donor);26 const web3 = helper.getWeb3();27 const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {28 from: owner,29 ...GAS_ARGS,30 });31 const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});32 return proxy;33}3435describe('Fungible (Via EVM proxy): Information getting', () => {36 let alice: IKeyringPair;37 let donor: IKeyringPair;3839 before(async function() {40 await usingEthPlaygrounds(async (helper, privateKey) => {41 donor = privateKey('//Alice');42 [alice] = await helper.arrange.createAccounts([10n], donor);43 });44 });4546 itEth('totalSupply', async ({helper}) => {47 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);48 const caller = await helper.eth.createAccountWithBalance(donor);49 await collection.mint(alice, 200n, {Substrate: alice.address});5051 const address = helper.ethAddress.fromCollectionId(collection.collectionId);52 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);53 const contract = await proxyWrap(helper, evmCollection, donor);54 const totalSupply = await contract.methods.totalSupply().call();5556 expect(totalSupply).to.equal('200');57 });5859 itEth('balanceOf', async ({helper}) => {60 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);61 const caller = await helper.eth.createAccountWithBalance(donor);6263 await collection.mint(alice, 200n, {Ethereum: caller});6465 const address = helper.ethAddress.fromCollectionId(collection.collectionId);66 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);67 const contract = await proxyWrap(helper, evmCollection, donor);68 const balance = await contract.methods.balanceOf(caller).call();6970 expect(balance).to.equal('200');71 });72});7374describe('Fungible (Via EVM proxy): Plain calls', () => {75 let alice: IKeyringPair;76 let donor: IKeyringPair;7778 before(async function() {79 await usingEthPlaygrounds(async (helper, privateKey) => {80 donor = privateKey('//Alice');81 [alice] = await helper.arrange.createAccounts([10n], donor);82 });83 });8485 itEth('Can perform approve()', async ({helper}) => {86 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);87 const caller = await helper.eth.createAccountWithBalance(donor);88 const spender = helper.eth.createAccount();8990 const address = helper.ethAddress.fromCollectionId(collection.collectionId);91 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);92 const contract = await proxyWrap(helper, evmCollection, donor);93 await collection.mint(alice, 200n, {Ethereum: contract.options.address});9495 {96 const result = await contract.methods.approve(spender, 100).send({from: caller});97 const events = normalizeEvents(result.events);9899 expect(events).to.be.deep.equal([100 {101 address,102 event: 'Approval',103 args: {104 owner: contract.options.address,105 spender,106 value: '100',107 },108 },109 ]);110 }111112 {113 const allowance = await contract.methods.allowance(contract.options.address, spender).call();114 expect(+allowance).to.equal(100);115 }116 });117118 itEth('Can perform transferFrom()', async ({helper}) => {119 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);120 const caller = await helper.eth.createAccountWithBalance(donor);121 const owner = await helper.eth.createAccountWithBalance(donor);122123 await collection.mint(alice, 200n, {Ethereum: owner});124 const receiver = helper.eth.createAccount();125126 const address = helper.ethAddress.fromCollectionId(collection.collectionId);127 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);128 const contract = await proxyWrap(helper, evmCollection, donor);129130 await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});131132 {133 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});134 const events = normalizeEvents(result.events);135 expect(events).to.be.deep.equal([136 {137 address,138 event: 'Transfer',139 args: {140 from: owner,141 to: receiver,142 value: '49',143 },144 },145 {146 address,147 event: 'Approval',148 args: {149 owner,150 spender: contract.options.address,151 value: '51',152 },153 },154 ]);155 }156157 {158 const balance = await contract.methods.balanceOf(receiver).call();159 expect(+balance).to.equal(49);160 }161162 {163 const balance = await contract.methods.balanceOf(owner).call();164 expect(+balance).to.equal(151);165 }166 });167168 itEth('Can perform transfer()', async ({helper}) => {169 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);170 const caller = await helper.eth.createAccountWithBalance(donor);171 const receiver = await helper.eth.createAccountWithBalance(donor);172173 const address = helper.ethAddress.fromCollectionId(collection.collectionId);174 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);175 const contract = await proxyWrap(helper, evmCollection, donor);176 await collection.mint(alice, 200n, {Ethereum: contract.options.address});177178 {179 const result = await contract.methods.transfer(receiver, 50).send({from: caller});180 const events = normalizeEvents(result.events);181 expect(events).to.be.deep.equal([182 {183 address,184 event: 'Transfer',185 args: {186 from: contract.options.address,187 to: receiver,188 value: '50',189 },190 },191 ]);192 }193194 {195 const balance = await contract.methods.balanceOf(contract.options.address).call();196 expect(+balance).to.equal(150);197 }198199 {200 const balance = await contract.methods.balanceOf(receiver).call();201 expect(+balance).to.equal(50);202 }203 });204});