git.delta.rocks / unique-network / refs/commits / acef2251004a

difftreelog

source

tests/src/eth/contractSponsoring.test.ts10.7 KiBsourcehistory
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('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  // TODO: Find a way to calculate default rate limit 190  itWeb3('Default rate limit equals 7200', async ({api, web3}) => {191    const owner = await createEthAccountWithBalance(api, web3);192    const flipper = await deployFlipper(web3, owner);193    await waitNewBlocks(api, 1);194    const helpers = contractHelpers(web3, owner);195    await waitNewBlocks(api, 1);196    expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');197  });198199  itWeb3('If whitelist mode is off and sponsorship is on, sponsorship does not work', async ({api, web3}) => {200    const alice = privateKey('//Alice');201202    const owner = await createEthAccountWithBalance(api, web3);203    const caller = await createEthAccountWithBalance(api, web3);204205    const flipper = await deployFlipper(web3, owner);206    await waitNewBlocks(api, 1);207    208    const helpers = contractHelpers(web3, owner);209210    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;211    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});212    await waitNewBlocks(api, 1);213    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});214    await waitNewBlocks(api, 1);215    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;216217    await transferBalanceToEth(api, alice, flipper.options.address);218    await waitNewBlocks(api, 2);219220    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);221    expect(originalFlipperBalance).to.be.not.equal('0');222223    await flipper.methods.flip().send({ from: caller });224    await waitNewBlocks(api, 1);225    expect(await flipper.methods.getValue().call()).to.be.true;226227    // Balance should be taken from flipper instead of caller228    const balanceAfter = await web3.eth.getBalance(flipper.options.address);229    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);230  });231232});