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

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 {createEthAccountWithBalance, deployCollector, GAS_ARGS, itWeb3, subToEth, transferBalanceToEth} from './util/helpers';5import {evmToAddress} from '@polkadot/util-crypto';6import {getGenericResult, UNIQUE} from '../util/helpers';7import {getBalanceSingle, transferBalanceExpectSuccess} from '../substrate/get-balance';89describe('EVM payable contracts', () => {10  itWeb3('Evm contract can receive wei from eth account', async ({api, web3}) => {11    const deployer = await createEthAccountWithBalance(api, web3);12    const contract = await deployCollector(web3, deployer);1314    await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: '10000', ...GAS_ARGS});1516    expect(await contract.methods.getCollected().call()).to.be.equal('10000');17  });1819  itWeb3('Evm contract can receive wei from substrate account', async ({api, web3}) => {20    const deployer = await createEthAccountWithBalance(api, web3);21    const contract = await deployCollector(web3, deployer);22    const alice = privateKey('//Alice');2324    // Transaction fee/value will be payed from subToEth(sender) evm balance,25    // which is backed by evmToAddress(subToEth(sender)) substrate balance26    await transferBalanceToEth(api, alice, subToEth(alice.address));2728    {29      const tx = api.tx.evm.call(30        subToEth(alice.address),31        contract.options.address,32        contract.methods.giveMoney().encodeABI(),33        '10000',34        GAS_ARGS.gas,35        await web3.eth.getGasPrice(),36        null,37        null,38        [],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');5556    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 = 1000n * UNIQUE;61    const CONTRACT_BALANCE = 1n * UNIQUE;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});6869    const receiver = privateKey(`//Receiver${Date.now()}`);7071    // First receive balance on eth balance of bob72    {73      const ethReceiver = subToEth(receiver.address);74      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal('0');75      await contract.methods.withdraw(ethReceiver).send({from: deployer});76      expect(await web3.eth.getBalance(ethReceiver)).to.be.equal(CONTRACT_BALANCE.toString());77    }7879    // Some balance is required to pay fee for evm.withdraw call80    await transferBalanceExpectSuccess(api, alice, receiver.address, FEE_BALANCE.toString());8182    // Withdraw balance from eth to substrate83    {84      const initialReceiverBalance = await getBalanceSingle(api, receiver.address);85      const tx = api.tx.evm.withdraw(86        subToEth(receiver.address),87        CONTRACT_BALANCE.toString(),88      );89      const events = await submitTransactionAsync(receiver, tx);90      const result = getGenericResult(events);91      expect(result.success).to.be.true;92      const finalReceiverBalance = await getBalanceSingle(api, receiver.address);9394      expect(finalReceiverBalance > initialReceiverBalance).to.be.true;95    }96  });97});