git.delta.rocks / unique-network / refs/commits / 930910430200

difftreelog

source

tests/src/eth/proxy/fungibleProxy.test.ts7.8 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {createCollectionExpectSuccess, createFungibleItemExpectSuccess} from '../../util/helpers';18import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents} from '../util/helpers';19import fungibleAbi from '../fungibleAbi.json';20import {expect} from 'chai';21import {ApiPromise} from '@polkadot/api';22import Web3 from 'web3';23import {readFile} from 'fs/promises';24import {IKeyringPair} from '@polkadot/types/types';2526async function proxyWrap(api: ApiPromise, web3: Web3, wrapped: any, privateKeyWrapper: (account: string) => IKeyringPair) {27  // Proxy owner has no special privilegies, we don't need to reuse them28  const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);29  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {30    from: owner,31    ...GAS_ARGS,32  });33  const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});34  return proxy;35}3637describe('Fungible (Via EVM proxy): Information getting', () => {38  itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {39    const collection = await createCollectionExpectSuccess({40      name: 'token name',41      mode: {type: 'Fungible', decimalPoints: 0},42    });43    const alice = privateKeyWrapper('//Alice');44    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);4546    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Substrate: alice.address});4748    const address = collectionIdToAddress(collection);49    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);50    const totalSupply = await contract.methods.totalSupply().call();5152    expect(totalSupply).to.equal('200');53  });5455  itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {56    const collection = await createCollectionExpectSuccess({57      name: 'token name',58      mode: {type: 'Fungible', decimalPoints: 0},59    });60    const alice = privateKeyWrapper('//Alice');61    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);6263    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: caller});6465    const address = collectionIdToAddress(collection);66    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);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  itWeb3('Can perform approve()', async ({web3, api, privateKeyWrapper}) => {75    const collection = await createCollectionExpectSuccess({76      name: 'token name',77      mode: {type: 'Fungible', decimalPoints: 0},78    });79    const alice = privateKeyWrapper('//Alice');80    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);81    const spender = createEthAccount(web3);8283    const address = collectionIdToAddress(collection);84    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);85    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: contract.options.address});8687    {88      const result = await contract.methods.approve(spender, 100).send({from: caller});89      const events = normalizeEvents(result.events);9091      expect(events).to.be.deep.equal([92        {93          address,94          event: 'Approval',95          args: {96            owner: contract.options.address,97            spender,98            value: '100',99          },100        },101      ]);102    }103104    {105      const allowance = await contract.methods.allowance(contract.options.address, spender).call();106      expect(+allowance).to.equal(100);107    }108  });109110  itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {111    const collection = await createCollectionExpectSuccess({112      name: 'token name',113      mode: {type: 'Fungible', decimalPoints: 0},114    });115    const alice = privateKeyWrapper('//Alice');116    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);117    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);118119    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});120121    const receiver = createEthAccount(web3);122123    const address = collectionIdToAddress(collection);124    const evmCollection = new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS});125    const contract = await proxyWrap(api, web3, evmCollection, privateKeyWrapper);126127    await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});128129    {130      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});131      const events = normalizeEvents(result.events);132      expect(events).to.be.deep.equal([133        {134          address,135          event: 'Transfer',136          args: {137            from: owner,138            to: receiver,139            value: '49',140          },141        },142        {143          address,144          event: 'Approval',145          args: {146            owner,147            spender: contract.options.address,148            value: '51',149          },150        },151      ]);152    }153154    {155      const balance = await contract.methods.balanceOf(receiver).call();156      expect(+balance).to.equal(49);157    }158159    {160      const balance = await contract.methods.balanceOf(owner).call();161      expect(+balance).to.equal(151);162    }163  });164165  itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {166    const collection = await createCollectionExpectSuccess({167      name: 'token name',168      mode: {type: 'Fungible', decimalPoints: 0},169    });170    const alice = privateKeyWrapper('//Alice');171    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);172    const receiver = await createEthAccountWithBalance(api, web3, privateKeyWrapper);173174    const address = collectionIdToAddress(collection);175    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);176    await createFungibleItemExpectSuccess(alice, collection, {Value: 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});