1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';1819import {itEth, expect, usingEthPlaygrounds} from './util/playgrounds';2021describe('EVM payable contracts', () => {22 let donor: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (_, privateKey) => {26 donor = privateKey('//Alice');27 });28 });2930 itEth('Evm contract can receive wei from eth account', async ({helper}) => {31 const deployer = await helper.eth.createAccountWithBalance(donor);32 const contract = await helper.eth.deployCollectorContract(deployer);3334 const web3 = helper.getWeb3();3536 await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: '10000', gas: helper.eth.DEFAULT_GAS});3738 expect(await contract.methods.getCollected().call()).to.be.equal('10000');39 });4041 itEth('Evm contract can receive wei from substrate account', async ({helper}) => {42 const deployer = await helper.eth.createAccountWithBalance(donor);43 const contract = await helper.eth.deployCollectorContract(deployer);44 const [alice] = await helper.arrange.createAccounts([10n], donor);4546 const weiCount = '10000';4748 49 50 await helper.eth.transferBalanceFromSubstrate(alice, helper.address.substrateToEth(alice.address), 5n);515253 await helper.eth.sendEVM(alice, contract.options.address, contract.methods.giveMoney().encodeABI(), weiCount);5455 expect(await contract.methods.getCollected().call()).to.be.equal(weiCount);56 });5758 59 itEth('Wei sent directly to backing storage of evm contract balance is unaccounted', async({helper}) => {60 const deployer = await helper.eth.createAccountWithBalance(donor);61 const contract = await helper.eth.deployCollectorContract(deployer);62 const [alice] = await helper.arrange.createAccounts([10n], donor);6364 const weiCount = 10_000n;6566 await helper.eth.transferBalanceFromSubstrate(alice, contract.options.address, weiCount, false);6768 expect(await contract.methods.getUnaccounted().call()).to.be.equal(weiCount.toString());69 });7071 itEth('Balance can be retrieved from evm contract', async({helper, privateKey}) => {72 const FEE_BALANCE = 10n * helper.balance.getOneTokenNominal();73 const CONTRACT_BALANCE = 1n * helper.balance.getOneTokenNominal();7475 const deployer = await helper.eth.createAccountWithBalance(donor);76 const contract = await helper.eth.deployCollectorContract(deployer);77 const [alice] = await helper.arrange.createAccounts([20n], donor);7879 const web3 = helper.getWeb3();8081 await web3.eth.sendTransaction({from: deployer, to: contract.options.address, value: CONTRACT_BALANCE.toString(), gas: helper.eth.DEFAULT_GAS});8283 const receiver = privateKey(`//Receiver${Date.now()}`);8485 86 {87 const ethReceiver = helper.address.substrateToEth(receiver.address);88 expect(await web3.eth.getBalance(ethReceiver)).to.be.equal('0');89 await contract.methods.withdraw(ethReceiver).send({from: deployer});90 expect(await web3.eth.getBalance(ethReceiver)).to.be.equal(CONTRACT_BALANCE.toString());91 }9293 94 await helper.balance.transferToSubstrate(alice, receiver.address, FEE_BALANCE);95 9697 98 {99 const initialReceiverBalance = await helper.balance.getSubstrate(receiver.address);100 await helper.executeExtrinsic(receiver, 'api.tx.evm.withdraw', [helper.address.substrateToEth(receiver.address), CONTRACT_BALANCE.toString()], true);101 const finalReceiverBalance = await helper.balance.getSubstrate(receiver.address);102103 expect(finalReceiverBalance > initialReceiverBalance).to.be.true;104 }105 });106});