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

difftreelog

source

tests/src/eth/proxy/fungibleProxy.test.ts7.5 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';2425async function proxyWrap(api: ApiPromise, web3: Web3, wrapped: any) {26  // Proxy owner has no special privilegies, we don't need to reuse them27  const owner = await createEthAccountWithBalance(api, web3);28  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {29    from: owner,30    ...GAS_ARGS,31  });32  const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});33  return proxy;34}3536describe('Fungible (Via EVM proxy): Information getting', () => {37  itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {38    const collection = await createCollectionExpectSuccess({39      name: 'token name',40      mode: {type: 'Fungible', decimalPoints: 0},41    });42    const alice = privateKeyWrapper('//Alice');43    const caller = await createEthAccountWithBalance(api, web3);4445    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Substrate: alice.address});4647    const address = collectionIdToAddress(collection);48    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));49    const totalSupply = await contract.methods.totalSupply().call();5051    expect(totalSupply).to.equal('200');52  });5354  itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {55    const collection = await createCollectionExpectSuccess({56      name: 'token name',57      mode: {type: 'Fungible', decimalPoints: 0},58    });59    const alice = privateKeyWrapper('//Alice');60    const caller = await createEthAccountWithBalance(api, web3);6162    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: caller});6364    const address = collectionIdToAddress(collection);65    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));66    const balance = await contract.methods.balanceOf(caller).call();6768    expect(balance).to.equal('200');69  });70});7172describe('Fungible (Via EVM proxy): Plain calls', () => {73  itWeb3('Can perform approve()', async ({web3, api, privateKeyWrapper}) => {74    const collection = await createCollectionExpectSuccess({75      name: 'token name',76      mode: {type: 'Fungible', decimalPoints: 0},77    });78    const alice = privateKeyWrapper('//Alice');79    const caller = await createEthAccountWithBalance(api, web3);80    const spender = createEthAccount(web3);8182    const address = collectionIdToAddress(collection);83    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));84    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: contract.options.address});8586    {87      const result = await contract.methods.approve(spender, 100).send({from: caller});88      const events = normalizeEvents(result.events);8990      expect(events).to.be.deep.equal([91        {92          address,93          event: 'Approval',94          args: {95            owner: contract.options.address,96            spender,97            value: '100',98          },99        },100      ]);101    }102103    {104      const allowance = await contract.methods.allowance(contract.options.address, spender).call();105      expect(+allowance).to.equal(100);106    }107  });108109  itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {110    const collection = await createCollectionExpectSuccess({111      name: 'token name',112      mode: {type: 'Fungible', decimalPoints: 0},113    });114    const alice = privateKeyWrapper('//Alice');115    const caller = await createEthAccountWithBalance(api, web3);116    const owner = await createEthAccountWithBalance(api, web3);117118    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});119120    const receiver = createEthAccount(web3);121122    const address = collectionIdToAddress(collection);123    const evmCollection = new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS});124    const contract = await proxyWrap(api, web3, evmCollection);125126    await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});127128    {129      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});130      const events = normalizeEvents(result.events);131      expect(events).to.be.deep.equal([132        {133          address,134          event: 'Transfer',135          args: {136            from: owner,137            to: receiver,138            value: '49',139          },140        },141        {142          address,143          event: 'Approval',144          args: {145            owner,146            spender: contract.options.address,147            value: '51',148          },149        },150      ]);151    }152153    {154      const balance = await contract.methods.balanceOf(receiver).call();155      expect(+balance).to.equal(49);156    }157158    {159      const balance = await contract.methods.balanceOf(owner).call();160      expect(+balance).to.equal(151);161    }162  });163164  itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {165    const collection = await createCollectionExpectSuccess({166      name: 'token name',167      mode: {type: 'Fungible', decimalPoints: 0},168    });169    const alice = privateKeyWrapper('//Alice');170    const caller = await createEthAccountWithBalance(api, web3);171    const receiver = await createEthAccountWithBalance(api, web3);172173    const address = collectionIdToAddress(collection);174    const contract = await proxyWrap(api, web3, new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS}));175    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: contract.options.address});176177    {178      const result = await contract.methods.transfer(receiver, 50).send({from: caller});179      const events = 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});