git.delta.rocks / unique-network / refs/commits / 448eded3df3f

difftreelog

test(evm) sponsoring

Yaroslav Bolyukin2021-07-27parent: #bb85132.patch.diff
in: master

1 file changed

addedtests/src/eth/sponsoring.test.tsdiffbeforeafterboth
after · tests/src/eth/sponsoring.test.ts
1import { expect } from 'chai';2import privateKey from '../substrate/privateKey';3import waitNewBlocks from '../substrate/wait-new-blocks';4import { contractHelpers, createEthAccount, createEthAccountWithBalance, deployCollector, deployFlipper, itWeb3, transferBalanceToEth, usingWeb3Http } from './util/helpers';56describe('EVM sponsoring', () => {7  itWeb3('Fee is deducted from contract if sponsoring is enabled', async ({api, web3}) => {8    await usingWeb3Http(async web3Http => {9      const alice = privateKey('//Alice');1011      const owner = await createEthAccountWithBalance(api, web3Http);12      const caller = createEthAccount(web3Http);13      const originalCallerBalance = await web3.eth.getBalance(caller);14      expect(originalCallerBalance).to.be.equal('0');1516      const flipper = await deployFlipper(web3Http, owner);17      await waitNewBlocks(api, 1);18      19      const helpers = contractHelpers(web3Http, owner);20      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;21      await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});22      await waitNewBlocks(api, 1);23      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;2425      await transferBalanceToEth(api, alice, flipper.options.address);26      await waitNewBlocks(api, 2);2728      const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);29      expect(originalFlipperBalance).to.be.not.equal('0');3031      await flipper.methods.flip().send({ from: caller });32      await waitNewBlocks(api, 1);33      expect(await flipper.methods.getValue().call()).to.be.true;3435      // Balance should be taken from flipper instead of caller36      expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);37      expect(await web3.eth.getBalance(flipper.options.address)).to.be.not.equals(originalFlipperBalance);38    });39  });40  itWeb3('...but this doesn\'t applies to payable value', async ({api, web3}) => {41    await usingWeb3Http(async web3Http => {42      const alice = privateKey('//Alice');4344      const owner = await createEthAccountWithBalance(api, web3Http);45      const caller = await createEthAccountWithBalance(api, web3Http);46      await waitNewBlocks(api, 1);47      const originalCallerBalance = await web3.eth.getBalance(caller);48      expect(originalCallerBalance).to.be.not.equal('0');4950      const collector = await deployCollector(web3Http, owner);51      await waitNewBlocks(api, 1);52      53      const helpers = contractHelpers(web3Http, owner);54      expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.false;55      await helpers.methods.toggleSponsoring(collector.options.address, true).send({from: owner});56      await waitNewBlocks(api, 1);57      expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.true;5859      await transferBalanceToEth(api, alice, collector.options.address);60      await waitNewBlocks(api, 2);6162      const originalCollectorBalance = await web3.eth.getBalance(collector.options.address);63      expect(originalCollectorBalance).to.be.not.equal('0');6465      await collector.methods.giveMoney().send({ from: caller, value: '10000' });66      await waitNewBlocks(api, 1);6768      // Balance will be taken from both caller (value) and from collector (fee)69      expect(await web3.eth.getBalance(caller)).to.be.equals((BigInt(originalCallerBalance) - 10000n).toString());70      expect(await web3.eth.getBalance(collector.options.address)).to.be.not.equals(originalCollectorBalance);71      expect(await collector.methods.getCollected().call()).to.be.equal('10000');72    });73  });74});