git.delta.rocks / unique-network / refs/commits / 1203e2d3b334

difftreelog

source

tests/src/eth/sponsoring.test.ts4.4 KiBsourcehistory
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      await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });21      await waitNewBlocks(api, 1);22      await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });23      await waitNewBlocks(api, 1);2425      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;26      await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});27      await waitNewBlocks(api, 1);28      await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});29      await waitNewBlocks(api, 1);30      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;3132      await transferBalanceToEth(api, alice, flipper.options.address);33      await waitNewBlocks(api, 2);3435      const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);36      expect(originalFlipperBalance).to.be.not.equal('0');3738      await flipper.methods.flip().send({ from: caller });39      await waitNewBlocks(api, 1);40      expect(await flipper.methods.getValue().call()).to.be.true;4142      // Balance should be taken from flipper instead of caller43      expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);44      expect(await web3.eth.getBalance(flipper.options.address)).to.be.not.equals(originalFlipperBalance);45    });46  });47  itWeb3('...but this doesn\'t applies to payable value', async ({api, web3}) => {48    await usingWeb3Http(async web3Http => {49      const alice = privateKey('//Alice');5051      const owner = await createEthAccountWithBalance(api, web3Http);52      const caller = await createEthAccountWithBalance(api, web3Http);53      await waitNewBlocks(api, 1);54      const originalCallerBalance = await web3.eth.getBalance(caller);55      expect(originalCallerBalance).to.be.not.equal('0');5657      const collector = await deployCollector(web3Http, owner);58      await waitNewBlocks(api, 1);59     60      const helpers = contractHelpers(web3Http, owner);61      await helpers.methods.toggleAllowlist(collector.options.address, true).send({ from: owner });62      await waitNewBlocks(api, 1);63      await helpers.methods.toggleAllowed(collector.options.address, caller, true).send({ from: owner });64      await waitNewBlocks(api, 1);6566      expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.false;67      await helpers.methods.toggleSponsoring(collector.options.address, true).send({ from: owner });68      await waitNewBlocks(api, 1);69      await helpers.methods.setSponsoringRateLimit(collector.options.address, 0).send({ from: owner });70      await waitNewBlocks(api, 1);71      expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.true;7273      await transferBalanceToEth(api, alice, collector.options.address);74      await waitNewBlocks(api, 2);7576      const originalCollectorBalance = await web3.eth.getBalance(collector.options.address);77      expect(originalCollectorBalance).to.be.not.equal('0');7879      await collector.methods.giveMoney().send({ from: caller, value: '10000' });80      await waitNewBlocks(api, 1);8182      // Balance will be taken from both caller (value) and from collector (fee)83      expect(await web3.eth.getBalance(caller)).to.be.equals((BigInt(originalCallerBalance) - 10000n).toString());84      expect(await web3.eth.getBalance(collector.options.address)).to.be.not.equals(originalCollectorBalance);85      expect(await collector.methods.getCollected().call()).to.be.equal('10000');86    });87  });88});