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

difftreelog

source

js-packages/tests/eth/proxy/fungibleProxy.test.ts7.7 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 type {IKeyringPair} from '@polkadot/types/types';20import {itEth, usingEthPlaygrounds} from '@unique/test-utils/eth/util.js';21import {EthUniqueHelper} from '@unique/test-utils/eth/index.js';22import {makeNames} from '@unique/test-utils/util.js';2324const {dirname} = makeNames(import.meta.url);2526async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {27  // Proxy owner has no special privilegies, we don't need to reuse them28  const owner = await helper.eth.createAccountWithBalance(donor);29  const web3 = helper.getWeb3();30  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {31    from: owner,32    gas: helper.eth.DEFAULT_GAS,33  });34  const proxy = await proxyContract.deploy({data: (await readFile(`${dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});35  return proxy;36}3738describe('Fungible (Via EVM proxy): Information getting', () => {39  let alice: IKeyringPair;40  let donor: IKeyringPair;4142  before(async function() {43    await usingEthPlaygrounds(async (helper, privateKey) => {44      donor = await privateKey({url: import.meta.url});45      [alice] = await helper.arrange.createAccounts([10n], donor);46    });47  });4849  itEth('totalSupply', async ({helper}) => {50    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);51    const caller = await helper.eth.createAccountWithBalance(donor);52    await collection.mint(alice, 200n, {Substrate: alice.address});5354    const address = helper.ethAddress.fromCollectionId(collection.collectionId);55    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);56    const contract = await proxyWrap(helper, evmCollection, donor);57    const totalSupply = await contract.methods.totalSupply().call();5859    expect(totalSupply).to.equal('200');60  });6162  itEth('balanceOf', async ({helper}) => {63    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);64    const caller = await helper.eth.createAccountWithBalance(donor);6566    await collection.mint(alice, 200n, {Ethereum: caller});6768    const address = helper.ethAddress.fromCollectionId(collection.collectionId);69    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);70    const contract = await proxyWrap(helper, evmCollection, donor);71    const balance = await contract.methods.balanceOf(caller).call();7273    expect(balance).to.equal('200');74  });75});7677describe('Fungible (Via EVM proxy): Plain calls', () => {78  let alice: IKeyringPair;79  let donor: IKeyringPair;8081  before(async function() {82    await usingEthPlaygrounds(async (helper, privateKey) => {83      donor = await privateKey({url: import.meta.url});84      [alice] = await helper.arrange.createAccounts([10n], donor);85    });86  });8788  itEth('Can perform approve()', async ({helper}) => {89    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);90    const caller = await helper.eth.createAccountWithBalance(donor);91    const spender = helper.eth.createAccount();9293    const address = helper.ethAddress.fromCollectionId(collection.collectionId);94    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);95    const contract = await proxyWrap(helper, evmCollection, donor);96    await collection.mint(alice, 200n, {Ethereum: contract.options.address});9798    {99      const result = await contract.methods.approve(spender, 100).send({from: caller});100      const events = helper.eth.normalizeEvents(result.events);101102      expect(events).to.be.deep.equal([103        {104          address,105          event: 'Approval',106          args: {107            owner: contract.options.address,108            spender,109            value: '100',110          },111        },112      ]);113    }114115    {116      const allowance = await contract.methods.allowance(contract.options.address, spender).call();117      expect(+allowance).to.equal(100);118    }119  });120121  itEth('Can perform transferFrom()', async ({helper}) => {122    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);123    const caller = await helper.eth.createAccountWithBalance(donor);124    const owner = await helper.eth.createAccountWithBalance(donor);125126    await collection.mint(alice, 200n, {Ethereum: owner});127    const receiver = helper.eth.createAccount();128129    const address = helper.ethAddress.fromCollectionId(collection.collectionId);130    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);131    const contract = await proxyWrap(helper, evmCollection, donor);132133    await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});134135    {136      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});137      const events = helper.eth.normalizeEvents(result.events);138      expect(events).to.be.deep.equal([139        {140          address,141          event: 'Transfer',142          args: {143            from: owner,144            to: receiver,145            value: '49',146          },147        },148        {149          address,150          event: 'Approval',151          args: {152            owner,153            spender: contract.options.address,154            value: '51',155          },156        },157      ]);158    }159160    {161      const balance = await contract.methods.balanceOf(receiver).call();162      expect(+balance).to.equal(49);163    }164165    {166      const balance = await contract.methods.balanceOf(owner).call();167      expect(+balance).to.equal(151);168    }169  });170171  itEth('Can perform transfer()', async ({helper}) => {172    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);173    const caller = await helper.eth.createAccountWithBalance(donor);174    const receiver = await helper.eth.createAccountWithBalance(donor);175176    const address = helper.ethAddress.fromCollectionId(collection.collectionId);177    const evmCollection = await helper.ethNativeContract.collection(address, 'ft', caller);178    const contract = await proxyWrap(helper, evmCollection, donor);179    await collection.mint(alice, 200n, {Ethereum: contract.options.address});180181    {182      const result = await contract.methods.transfer(receiver, 50).send({from: caller});183      const events = helper.eth.normalizeEvents(result.events);184      expect(events).to.be.deep.equal([185        {186          address,187          event: 'Transfer',188          args: {189            from: contract.options.address,190            to: receiver,191            value: '50',192          },193        },194      ]);195    }196197    {198      const balance = await contract.methods.balanceOf(contract.options.address).call();199      expect(+balance).to.equal(150);200    }201202    {203      const balance = await contract.methods.balanceOf(receiver).call();204      expect(+balance).to.equal(50);205    }206  });207});