git.delta.rocks / unique-network / refs/commits / 9ec63641102a

difftreelog

Tests fix

str-mv2021-09-13parent: #159c438.patch.diff
in: master

1 file changed

modifiedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
before · tests/src/eth/contractSponsoring.test.ts
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import privateKey from '../substrate/privateKey';7import { expect } from 'chai';8import {  9  contractHelpers,10  createEthAccountWithBalance,11  transferBalanceToEth,12  deployFlipper,13  itWeb3 } from './util/helpers';14import waitNewBlocks from '../substrate/wait-new-blocks';1516describe.only('Sponsoring EVM contracts', () => {17  itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {18    const owner = await createEthAccountWithBalance(api, web3);19    const flipper = await deployFlipper(web3, owner);20    await waitNewBlocks(api, 1);21    const helpers = contractHelpers(web3, owner);22    await waitNewBlocks(api, 1);23    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;24    await waitNewBlocks(api, 1);25    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});26    await waitNewBlocks(api, 1);27    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;28  });2930  itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {31    const owner = await createEthAccountWithBalance(api, web3);32    const notOwner = await createEthAccountWithBalance(api, web3);33    const flipper = await deployFlipper(web3, owner);34    await waitNewBlocks(api, 1);35    const helpers = contractHelpers(web3, owner);36    await waitNewBlocks(api, 1);37    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;38    await waitNewBlocks(api, 1);39    await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;40    await waitNewBlocks(api, 1);41    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;42  });4344  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (whitelisted)', async ({api, web3}) => {45    const alice = privateKey('//Alice');4647    const owner = await createEthAccountWithBalance(api, web3);48    const caller = await createEthAccountWithBalance(api, web3);4950    const flipper = await deployFlipper(web3, owner);51    await waitNewBlocks(api, 1);52      53    const helpers = contractHelpers(web3, owner);54    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });55    await waitNewBlocks(api, 1);56    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });57    await waitNewBlocks(api, 1);5859    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;60    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});61    await waitNewBlocks(api, 1);62    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});63    await waitNewBlocks(api, 1);64    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;6566    await transferBalanceToEth(api, alice, flipper.options.address);67    await waitNewBlocks(api, 2);6869    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);70    expect(originalFlipperBalance).to.be.not.equal('0');7172    await flipper.methods.flip().send({ from: caller });73    await waitNewBlocks(api, 1);74    expect(await flipper.methods.getValue().call()).to.be.true;7576    // Balance should be taken from flipper instead of caller77    const balanceAfter = await web3.eth.getBalance(flipper.options.address);78    expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);79  });8081  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-whitelisted)', async ({api, web3}) => {82    const alice = privateKey('//Alice');8384    const owner = await createEthAccountWithBalance(api, web3);85    const caller = await createEthAccountWithBalance(api, web3);8687    const flipper = await deployFlipper(web3, owner);88    await waitNewBlocks(api, 1);89    90    const helpers = contractHelpers(web3, owner);9192    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;93    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});94    await waitNewBlocks(api, 1);95    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});96    await waitNewBlocks(api, 1);97    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;9899    await transferBalanceToEth(api, alice, flipper.options.address);100    await waitNewBlocks(api, 2);101102    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);103    expect(originalFlipperBalance).to.be.not.equal('0');104105    await flipper.methods.flip().send({ from: caller });106    await waitNewBlocks(api, 1);107    expect(await flipper.methods.getValue().call()).to.be.true;108109    // Balance should be taken from flipper instead of caller110    const balanceAfter = await web3.eth.getBalance(flipper.options.address);111    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);112  });113114  itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3}) => {115    const alice = privateKey('//Alice');116117    const owner = await createEthAccountWithBalance(api, web3);118    const caller = await createEthAccountWithBalance(api, web3);119    const originalCallerBalance = await web3.eth.getBalance(caller);120121    const flipper = await deployFlipper(web3, owner);122    await waitNewBlocks(api, 1);123      124    const helpers = contractHelpers(web3, owner);125    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });126    await waitNewBlocks(api, 1);127    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });128    await waitNewBlocks(api, 1);129130    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;131    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});132    await waitNewBlocks(api, 1);133    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});134    await waitNewBlocks(api, 1);135    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;136137    await transferBalanceToEth(api, alice, flipper.options.address);138    await waitNewBlocks(api, 2);139140    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);141    expect(originalFlipperBalance).to.be.not.equal('0');142143    await flipper.methods.flip().send({ from: caller });144    await waitNewBlocks(api, 1);145    expect(await flipper.methods.getValue().call()).to.be.true;146147    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);148  });149150  itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3}) => {151    const alice = privateKey('//Alice');152153    const owner = await createEthAccountWithBalance(api, web3);154    const caller = await createEthAccountWithBalance(api, web3);155    const originalCallerBalance = await web3.eth.getBalance(caller);156157    const flipper = await deployFlipper(web3, owner);158    await waitNewBlocks(api, 1);159      160    const helpers = contractHelpers(web3, owner);161    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });162    await waitNewBlocks(api, 1);163    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });164    await waitNewBlocks(api, 1);165166    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;167    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});168    await waitNewBlocks(api, 1);169    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});170    await waitNewBlocks(api, 1);171    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;172173    await transferBalanceToEth(api, alice, flipper.options.address);174    await waitNewBlocks(api, 2);175176    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);177    expect(originalFlipperBalance).to.be.not.equal('0');178179    await flipper.methods.flip().send({ from: caller });180    await waitNewBlocks(api, 1);181    expect(await flipper.methods.getValue().call()).to.be.true;182    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);183184    await flipper.methods.flip().send({ from: caller });185    await waitNewBlocks(api, 1);186    expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);187  });188189  itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {190    const owner = await createEthAccountWithBalance(api, web3);191    const flipper = await deployFlipper(web3, owner);192    await waitNewBlocks(api, 1);193    const helpers = contractHelpers(web3, owner);194    await waitNewBlocks(api, 1);195    expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');196  });197198  itWeb3('If whitelist mode is off and sponsorship is on, sponsorship does not work', async ({api, web3}) => {199    const alice = privateKey('//Alice');200201    const owner = await createEthAccountWithBalance(api, web3);202    const caller = await createEthAccountWithBalance(api, web3);203204    const flipper = await deployFlipper(web3, owner);205    await waitNewBlocks(api, 1);206    207    const helpers = contractHelpers(web3, owner);208209    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;210    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});211    await waitNewBlocks(api, 1);212    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});213    await waitNewBlocks(api, 1);214    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;215216    await transferBalanceToEth(api, alice, flipper.options.address);217    await waitNewBlocks(api, 2);218219    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);220    expect(originalFlipperBalance).to.be.not.equal('0');221222    await flipper.methods.flip().send({ from: caller });223    await waitNewBlocks(api, 1);224    expect(await flipper.methods.getValue().call()).to.be.true;225226    // Balance should be taken from flipper instead of caller227    const balanceAfter = await web3.eth.getBalance(flipper.options.address);228    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);229  });230231});