1234567891011121314151617import {expect} from 'chai';18import {readFile} from 'fs/promises';19import {IKeyringPair} from '@polkadot/types/types';20import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util';2122async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {23 24 const owner = await helper.eth.createAccountWithBalance(donor);25 const web3 = helper.getWeb3();26 const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {27 from: owner,28 gas: helper.eth.DEFAULT_GAS,29 });30 const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});31 return proxy;32}3334describe('Fungible (Via EVM proxy): Information getting', () => {35 let alice: IKeyringPair;36 let donor: IKeyringPair;3738 before(async function() {39 await usingEthPlaygrounds(async (helper, privateKey) => {40 donor = await privateKey({filename: __filename});41 [alice] = await helper.arrange.createAccounts([10n], donor);42 });43 });4445 itEth('totalSupply', async ({helper}) => {46 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);47 const caller = await helper.eth.createAccountWithBalance(donor);48 await collection.mint(alice, 200n, {Substrate: alice.address});4950 const address = helper.ethAddress.fromCollectionId(collection.collectionId);51 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);52 const contract = await proxyWrap(helper, evmCollection, donor);53 const totalSupply = await contract.methods.totalSupply().call();5455 expect(totalSupply).to.equal('200');56 });5758 itEth('balanceOf', async ({helper}) => {59 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);60 const caller = await helper.eth.createAccountWithBalance(donor);6162 await collection.mint(alice, 200n, {Ethereum: caller});6364 const address = helper.ethAddress.fromCollectionId(collection.collectionId);65 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);66 const contract = await proxyWrap(helper, evmCollection, donor);67 const balance = await contract.methods.balanceOf(caller).call();6869 expect(balance).to.equal('200');70 });71});7273describe('Fungible (Via EVM proxy): Plain calls', () => {74 let alice: IKeyringPair;75 let donor: IKeyringPair;7677 before(async function() {78 await usingEthPlaygrounds(async (helper, privateKey) => {79 donor = await privateKey({filename: __filename});80 [alice] = await helper.arrange.createAccounts([10n], donor);81 });82 });8384 itEth('Can perform approve()', async ({helper}) => {85 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);86 const caller = await helper.eth.createAccountWithBalance(donor);87 const spender = helper.eth.createAccount();8889 const address = helper.ethAddress.fromCollectionId(collection.collectionId);90 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);91 const contract = await proxyWrap(helper, evmCollection, donor);92 await collection.mint(alice, 200n, {Ethereum: contract.options.address});9394 {95 const result = await contract.methods.approve(spender, 100).send({from: caller});96 const events = helper.eth.normalizeEvents(result.events);9798 expect(events).to.be.deep.equal([99 {100 address,101 event: 'Approval',102 args: {103 owner: contract.options.address,104 spender,105 value: '100',106 },107 },108 ]);109 }110111 {112 const allowance = await contract.methods.allowance(contract.options.address, spender).call();113 expect(+allowance).to.equal(100);114 }115 });116117 itEth('Can perform transferFrom()', async ({helper}) => {118 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);119 const caller = await helper.eth.createAccountWithBalance(donor);120 const owner = await helper.eth.createAccountWithBalance(donor);121122 await collection.mint(alice, 200n, {Ethereum: owner});123 const receiver = helper.eth.createAccount();124125 const address = helper.ethAddress.fromCollectionId(collection.collectionId);126 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);127 const contract = await proxyWrap(helper, evmCollection, donor);128129 await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});130131 {132 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});133 const events = helper.eth.normalizeEvents(result.events);134 expect(events).to.be.deep.equal([135 {136 address,137 event: 'Transfer',138 args: {139 from: owner,140 to: receiver,141 value: '49',142 },143 },144 {145 address,146 event: 'Approval',147 args: {148 owner,149 spender: contract.options.address,150 value: '51',151 },152 },153 ]);154 }155156 {157 const balance = await contract.methods.balanceOf(receiver).call();158 expect(+balance).to.equal(49);159 }160161 {162 const balance = await contract.methods.balanceOf(owner).call();163 expect(+balance).to.equal(151);164 }165 });166167 itEth('Can perform transfer()', async ({helper}) => {168 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);169 const caller = await helper.eth.createAccountWithBalance(donor);170 const receiver = await helper.eth.createAccountWithBalance(donor);171172 const address = helper.ethAddress.fromCollectionId(collection.collectionId);173 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);174 const contract = await proxyWrap(helper, evmCollection, donor);175 await collection.mint(alice, 200n, {Ethereum: contract.options.address});176177 {178 const result = await contract.methods.transfer(receiver, 50).send({from: caller});179 const events = helper.eth.normalizeEvents(result.events);180 expect(events).to.be.deep.equal([181 {182 address,183 event: 'Transfer',184 args: {185 from: contract.options.address,186 to: receiver,187 value: '50',188 },189 },190 ]);191 }192193 {194 const balance = await contract.methods.balanceOf(contract.options.address).call();195 expect(+balance).to.equal(150);196 }197198 {199 const balance = await contract.methods.balanceOf(receiver).call();200 expect(+balance).to.equal(50);201 }202 });203});