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

difftreelog

source

tests/src/eth/payable.test.ts4.1 KiBsourcehistory
1import { expect } from 'chai';2import privateKey from '../substrate/privateKey';3import { submitTransactionAsync } from '../substrate/substrate-api';4import waitNewBlocks from '../substrate/wait-new-blocks';5import { createEthAccountWithBalance, deployCollector, GAS_ARGS, itWeb3, subToEth } from './util/helpers';6import {evmToAddress} from '@polkadot/util-crypto';7import { getGenericResult } from '../util/helpers';8import { getBalanceSingle, transferBalanceExpectSuccess } from '../substrate/get-balance';910describe('EVM payable contracts', ()=>{11  itWeb3('Evm contract can receive wei from eth account', async ({api, web3}) => {12    const deployer = await createEthAccountWithBalance(api, web3);13    const contract = await deployCollector(web3, deployer);1415    await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: '10000', ...GAS_ARGS});16    await waitNewBlocks(api, 1);1718    expect(await contract.methods.getCollected().call()).to.be.equal('10000');19  });2021  itWeb3('Evm contract can receive wei from substrate account', async ({api, web3}) => {22    const deployer = await createEthAccountWithBalance(api, web3);23    const contract = await deployCollector(web3, deployer);24    const alice = privateKey('//Alice');2526    // Transaction fee/value will be payed from subToEth(sender) evm balance,27    // which is backed by evmToAddress(subToEth(sender)) substrate balance28    await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), '1000000000000');2930    {31      const tx = api.tx.evm.call(32        subToEth(alice.address),33        contract.options.address,34        contract.methods.giveMoney().encodeABI(),35        '10000',36        GAS_ARGS.gas,37        GAS_ARGS.gasPrice,38        null,39      );40      const events = await submitTransactionAsync(alice, tx);41      const result = getGenericResult(events);42      expect(result.success).to.be.true;43    }4445    expect(await contract.methods.getCollected().call()).to.be.equal('10000');46  });4748  // We can't handle sending balance to backing storage of evm balance, because evmToAddress operation is irreversible49  itWeb3('Wei sent directly to backing storage of evm contract balance is unaccounted', async({api, web3}) => {50    const deployer = await createEthAccountWithBalance(api, web3);51    const contract = await deployCollector(web3, deployer);52    const alice = privateKey('//Alice');5354    await transferBalanceExpectSuccess(api, alice, evmToAddress(contract.options.address), '10000');55  56    expect(await contract.methods.getUnaccounted().call()).to.be.equal('10000');57  });5859  itWeb3('Balance can be retrieved from evm contract', async({api, web3}) => {60    const FEE_BALANCE = 10n ** 18n;61    const CONTRACT_BALANCE = 10n ** 14n;6263    const deployer = await createEthAccountWithBalance(api, web3);64    const contract = await deployCollector(web3, deployer);65    const alice = privateKey('//Alice');6667    await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: CONTRACT_BALANCE.toString(), ...GAS_ARGS});68    await waitNewBlocks(api, 1);6970    const receiver = privateKey(`//Receiver${Date.now()}`);7172    // First receive balance on eth balance of bob73    {74      const ethReceiver = subToEth(receiver.address);75      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal('0');76      await contract.methods.withdraw(ethReceiver).send({from: deployer});77      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal(CONTRACT_BALANCE.toString());78    }7980    // Some balance is required to pay fee for evm.withdraw call81    await transferBalanceExpectSuccess(api, alice, receiver.address, FEE_BALANCE.toString());8283    // Withdraw balance from eth to substrate84    {85      const initialReceiverBalance = await getBalanceSingle(api, receiver.address);86      const tx = api.tx.evm.withdraw(87        subToEth(receiver.address),88        CONTRACT_BALANCE.toString(),89      );90      const events = await submitTransactionAsync(receiver, tx);91      const result = getGenericResult(events);92      expect(result.success).to.be.true;93      const finalReceiverBalance = await getBalanceSingle(api, receiver.address);9495      expect(finalReceiverBalance > initialReceiverBalance).to.be.true;96    }97  });98});