git.delta.rocks / unique-network / refs/commits / 2df3f6b6e145

difftreelog

source

tests/src/eth/proxy/fungibleProxy.test.ts7.6 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 {expect} from 'chai';18import {readFile} from 'fs/promises';19import {IKeyringPair} from '@polkadot/types/types';20import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util';21import {makeNames} from '../../util';2223const {dirname} = makeNames(import.meta.url);2425async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {26  // Proxy owner has no special privilegies, we don't need to reuse them27  const owner = await helper.eth.createAccountWithBalance(donor);28  const web3 = helper.getWeb3();29  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {30    from: owner,31    gas: helper.eth.DEFAULT_GAS,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  let alice: IKeyringPair;39  let donor: IKeyringPair;4041  before(async function() {42    await usingEthPlaygrounds(async (helper, privateKey) => {43      donor = await privateKey({url: import.meta.url});44      [alice] = await helper.arrange.createAccounts([10n], donor);45    });46  });4748  itEth('totalSupply', async ({helper}) => {49    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);50    const caller = await helper.eth.createAccountWithBalance(donor);51    await collection.mint(alice, 200n, {Substrate: alice.address});5253    const address = helper.ethAddress.fromCollectionId(collection.collectionId);54    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);55    const contract = await proxyWrap(helper, evmCollection, donor);56    const totalSupply = await contract.methods.totalSupply().call();5758    expect(totalSupply).to.equal('200');59  });6061  itEth('balanceOf', async ({helper}) => {62    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);63    const caller = await helper.eth.createAccountWithBalance(donor);6465    await collection.mint(alice, 200n, {Ethereum: caller});6667    const address = helper.ethAddress.fromCollectionId(collection.collectionId);68    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);69    const contract = await proxyWrap(helper, evmCollection, donor);70    const balance = await contract.methods.balanceOf(caller).call();7172    expect(balance).to.equal('200');73  });74});7576describe('Fungible (Via EVM proxy): Plain calls', () => {77  let alice: IKeyringPair;78  let donor: IKeyringPair;7980  before(async function() {81    await usingEthPlaygrounds(async (helper, privateKey) => {82      donor = await privateKey({url: import.meta.url});83      [alice] = await helper.arrange.createAccounts([10n], donor);84    });85  });8687  itEth('Can perform approve()', async ({helper}) => {88    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);89    const caller = await helper.eth.createAccountWithBalance(donor);90    const spender = helper.eth.createAccount();9192    const address = helper.ethAddress.fromCollectionId(collection.collectionId);93    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);94    const contract = await proxyWrap(helper, evmCollection, donor);95    await collection.mint(alice, 200n, {Ethereum: contract.options.address});9697    {98      const result = await contract.methods.approve(spender, 100).send({from: caller});99      const events = helper.eth.normalizeEvents(result.events);100101      expect(events).to.be.deep.equal([102        {103          address,104          event: 'Approval',105          args: {106            owner: contract.options.address,107            spender,108            value: '100',109          },110        },111      ]);112    }113114    {115      const allowance = await contract.methods.allowance(contract.options.address, spender).call();116      expect(+allowance).to.equal(100);117    }118  });119120  itEth('Can perform transferFrom()', async ({helper}) => {121    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);122    const caller = await helper.eth.createAccountWithBalance(donor);123    const owner = await helper.eth.createAccountWithBalance(donor);124125    await collection.mint(alice, 200n, {Ethereum: owner});126    const receiver = helper.eth.createAccount();127128    const address = helper.ethAddress.fromCollectionId(collection.collectionId);129    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);130    const contract = await proxyWrap(helper, evmCollection, donor);131132    await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});133134    {135      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});136      const events = helper.eth.normalizeEvents(result.events);137      expect(events).to.be.deep.equal([138        {139          address,140          event: 'Transfer',141          args: {142            from: owner,143            to: receiver,144            value: '49',145          },146        },147        {148          address,149          event: 'Approval',150          args: {151            owner,152            spender: contract.options.address,153            value: '51',154          },155        },156      ]);157    }158159    {160      const balance = await contract.methods.balanceOf(receiver).call();161      expect(+balance).to.equal(49);162    }163164    {165      const balance = await contract.methods.balanceOf(owner).call();166      expect(+balance).to.equal(151);167    }168  });169170  itEth('Can perform transfer()', async ({helper}) => {171    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);172    const caller = await helper.eth.createAccountWithBalance(donor);173    const receiver = await helper.eth.createAccountWithBalance(donor);174175    const address = helper.ethAddress.fromCollectionId(collection.collectionId);176    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);177    const contract = await proxyWrap(helper, evmCollection, donor);178    await collection.mint(alice, 200n, {Ethereum: contract.options.address});179180    {181      const result = await contract.methods.transfer(receiver, 50).send({from: caller});182      const events = helper.eth.normalizeEvents(result.events);183      expect(events).to.be.deep.equal([184        {185          address,186          event: 'Transfer',187          args: {188            from: contract.options.address,189            to: receiver,190            value: '50',191          },192        },193      ]);194    }195196    {197      const balance = await contract.methods.balanceOf(contract.options.address).call();198      expect(+balance).to.equal(150);199    }200201    {202      const balance = await contract.methods.balanceOf(receiver).call();203      expect(+balance).to.equal(50);204    }205  });206});