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

difftreelog

source

tests/src/eth/payable.test.ts4.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 {expect} from 'chai';18import privateKey from '../substrate/privateKey';19import {submitTransactionAsync} from '../substrate/substrate-api';20import {createEthAccountWithBalance, deployCollector, GAS_ARGS, itWeb3, subToEth, transferBalanceToEth} from './util/helpers';21import {evmToAddress} from '@polkadot/util-crypto';22import {getGenericResult, UNIQUE} from '../util/helpers';23import {getBalanceSingle, transferBalanceExpectSuccess} from '../substrate/get-balance';2425describe('EVM payable contracts', () => {26  itWeb3('Evm contract can receive wei from eth account', async ({api, web3}) => {27    const deployer = await createEthAccountWithBalance(api, web3);28    const contract = await deployCollector(web3, deployer);2930    await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: '10000', ...GAS_ARGS});3132    expect(await contract.methods.getCollected().call()).to.be.equal('10000');33  });3435  itWeb3('Evm contract can receive wei from substrate account', async ({api, web3}) => {36    const deployer = await createEthAccountWithBalance(api, web3);37    const contract = await deployCollector(web3, deployer);38    const alice = privateKey('//Alice');3940    // Transaction fee/value will be payed from subToEth(sender) evm balance,41    // which is backed by evmToAddress(subToEth(sender)) substrate balance42    await transferBalanceToEth(api, alice, subToEth(alice.address));4344    {45      const tx = api.tx.evm.call(46        subToEth(alice.address),47        contract.options.address,48        contract.methods.giveMoney().encodeABI(),49        '10000',50        GAS_ARGS.gas,51        await web3.eth.getGasPrice(),52        null,53        null,54        [],55      );56      const events = await submitTransactionAsync(alice, tx);57      const result = getGenericResult(events);58      expect(result.success).to.be.true;59    }6061    expect(await contract.methods.getCollected().call()).to.be.equal('10000');62  });6364  // We can't handle sending balance to backing storage of evm balance, because evmToAddress operation is irreversible65  itWeb3('Wei sent directly to backing storage of evm contract balance is unaccounted', async({api, web3}) => {66    const deployer = await createEthAccountWithBalance(api, web3);67    const contract = await deployCollector(web3, deployer);68    const alice = privateKey('//Alice');6970    await transferBalanceExpectSuccess(api, alice, evmToAddress(contract.options.address), '10000');7172    expect(await contract.methods.getUnaccounted().call()).to.be.equal('10000');73  });7475  itWeb3('Balance can be retrieved from evm contract', async({api, web3}) => {76    const FEE_BALANCE = 1000n * UNIQUE;77    const CONTRACT_BALANCE = 1n * UNIQUE;7879    const deployer = await createEthAccountWithBalance(api, web3);80    const contract = await deployCollector(web3, deployer);81    const alice = privateKey('//Alice');8283    await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: CONTRACT_BALANCE.toString(), ...GAS_ARGS});8485    const receiver = privateKey(`//Receiver${Date.now()}`);8687    // First receive balance on eth balance of bob88    {89      const ethReceiver = subToEth(receiver.address);90      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal('0');91      await contract.methods.withdraw(ethReceiver).send({from: deployer});92      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal(CONTRACT_BALANCE.toString());93    }9495    // Some balance is required to pay fee for evm.withdraw call96    await transferBalanceExpectSuccess(api, alice, receiver.address, FEE_BALANCE.toString());9798    // Withdraw balance from eth to substrate99    {100      const initialReceiverBalance = await getBalanceSingle(api, receiver.address);101      const tx = api.tx.evm.withdraw(102        subToEth(receiver.address),103        CONTRACT_BALANCE.toString(),104      );105      const events = await submitTransactionAsync(receiver, tx);106      const result = getGenericResult(events);107      expect(result.success).to.be.true;108      const finalReceiverBalance = await getBalanceSingle(api, receiver.address);109110      expect(finalReceiverBalance > initialReceiverBalance).to.be.true;111    }112  });113});