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

difftreelog

source

tests/src/eth/payable.test.ts4.9 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 {submitTransactionAsync} from '../substrate/substrate-api';19import {createEthAccountWithBalance, deployCollector, GAS_ARGS, itWeb3, subToEth, transferBalanceToEth} from './util/helpers';20import {evmToAddress} from '@polkadot/util-crypto';21import {getGenericResult, UNIQUE} from '../util/helpers';22import {getBalanceSingle, transferBalanceExpectSuccess} from '../substrate/get-balance';2324describe('EVM payable contracts', () => {25  itWeb3('Evm contract can receive wei from eth account', async ({api, web3, privateKeyWrapper}) => {26    const deployer = await createEthAccountWithBalance(api, web3, privateKeyWrapper);27    const contract = await deployCollector(web3, deployer);2829    await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: '10000', ...GAS_ARGS});3031    expect(await contract.methods.getCollected().call()).to.be.equal('10000');32  });3334  itWeb3('Evm contract can receive wei from substrate account', async ({api, web3, privateKeyWrapper}) => {35    const deployer = await createEthAccountWithBalance(api, web3, privateKeyWrapper);36    const contract = await deployCollector(web3, deployer);37    const alice = privateKeyWrapper('//Alice');3839    // Transaction fee/value will be payed from subToEth(sender) evm balance,40    // which is backed by evmToAddress(subToEth(sender)) substrate balance41    await transferBalanceToEth(api, alice, subToEth(alice.address));4243    {44      const tx = api.tx.evm.call(45        subToEth(alice.address),46        contract.options.address,47        contract.methods.giveMoney().encodeABI(),48        '10000',49        GAS_ARGS.gas,50        await web3.eth.getGasPrice(),51        null,52        null,53        [],54      );55      const events = await submitTransactionAsync(alice, tx);56      const result = getGenericResult(events);57      expect(result.success).to.be.true;58    }5960    expect(await contract.methods.getCollected().call()).to.be.equal('10000');61  });6263  // We can't handle sending balance to backing storage of evm balance, because evmToAddress operation is irreversible64  itWeb3('Wei sent directly to backing storage of evm contract balance is unaccounted', async({api, web3, privateKeyWrapper}) => {65    const deployer = await createEthAccountWithBalance(api, web3, privateKeyWrapper);66    const contract = await deployCollector(web3, deployer);67    const alice = privateKeyWrapper('//Alice');6869    await transferBalanceExpectSuccess(api, alice, evmToAddress(contract.options.address), '10000');7071    expect(await contract.methods.getUnaccounted().call()).to.be.equal('10000');72  });7374  itWeb3('Balance can be retrieved from evm contract', async({api, web3, privateKeyWrapper}) => {75    const FEE_BALANCE = 1000n * UNIQUE;76    const CONTRACT_BALANCE = 1n * UNIQUE;7778    const deployer = await createEthAccountWithBalance(api, web3, privateKeyWrapper);79    const contract = await deployCollector(web3, deployer);80    const alice = privateKeyWrapper('//Alice');8182    await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: CONTRACT_BALANCE.toString(), ...GAS_ARGS});8384    const receiver = privateKeyWrapper(`//Receiver${Date.now()}`);8586    // First receive balance on eth balance of bob87    {88      const ethReceiver = subToEth(receiver.address);89      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal('0');90      await contract.methods.withdraw(ethReceiver).send({from: deployer});91      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal(CONTRACT_BALANCE.toString());92    }9394    // Some balance is required to pay fee for evm.withdraw call95    await transferBalanceExpectSuccess(api, alice, receiver.address, FEE_BALANCE.toString());9697    // Withdraw balance from eth to substrate98    {99      const initialReceiverBalance = await getBalanceSingle(api, receiver.address);100      const tx = api.tx.evm.withdraw(101        subToEth(receiver.address),102        CONTRACT_BALANCE.toString(),103      );104      const events = await submitTransactionAsync(receiver, tx);105      const result = getGenericResult(events);106      expect(result.success).to.be.true;107      const finalReceiverBalance = await getBalanceSingle(api, receiver.address);108109      expect(finalReceiverBalance > initialReceiverBalance).to.be.true;110    }111  });112});