difftreelog
Merge pull request #185 from UniqueNetwork/test/evm-payable-tests
in: master
EVM payable tests
3 files changed
tests/src/eth/payable.test.tsdiffbeforeafterboth1import { 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});tests/src/eth/util/helpers.tsdiffbeforeafterboth--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -197,16 +197,27 @@
return flipper;
}
-export async function deployCollector(web3: Web3 & Web3HttpMarker, deployer: string) {
+export async function deployCollector(web3: Web3, deployer: string) {
const compiled = compileContract('Collector', `
contract Collector {
uint256 collected;
+ fallback() external payable {
+ giveMoney();
+ }
function giveMoney() public payable {
collected += msg.value;
}
function getCollected() public view returns (uint256) {
return collected;
}
+ function getUnaccounted() public view returns (uint256) {
+ return address(this).balance - collected;
+ }
+
+ function withdraw(address payable target) public {
+ target.transfer(collected);
+ collected = 0;
+ }
}
`);
const Collector = new web3.eth.Contract(compiled.abi, undefined, {
tests/src/substrate/get-balance.tsdiffbeforeafterboth--- a/tests/src/substrate/get-balance.ts
+++ b/tests/src/substrate/get-balance.ts
@@ -6,9 +6,24 @@
import { ApiPromise } from '@polkadot/api';
import {AccountInfo} from '@polkadot/types/interfaces/system';
import promisifySubstrate from './promisify-substrate';
+import { IKeyringPair } from '@polkadot/types/types';
+import { submitTransactionAsync } from './substrate-api';
+import { getGenericResult } from '../util/helpers';
+import { expect } from 'chai';
export default async function getBalance(api: ApiPromise, accounts: string[]): Promise<Array<bigint>> {
const balance = promisifySubstrate(api, (acc: string[]) => api.query.system.account.multi(acc));
const responce = await balance(accounts) as unknown as AccountInfo[];
return responce.map((r) => r.data.free.toBigInt().valueOf());
}
+
+export async function getBalanceSingle(api: ApiPromise, account: string): Promise<bigint> {
+ return (await getBalance(api, [account]))[0];
+}
+
+export async function transferBalanceExpectSuccess(api: ApiPromise, from: IKeyringPair, to: string, amount: bigint | string) {
+ const tx = api.tx.balances.transfer(to, amount);
+ const events = await submitTransactionAsync(from, tx);
+ const result = getGenericResult(events);
+ expect(result.success).to.be.true;
+}
\ No newline at end of file