123456import privateKey from '../substrate/privateKey';7import { expect } from 'chai';8import { 9 contractHelpers,10 createEthAccountWithBalance,11 createEthAccount,12 transferBalanceToEth,13 deployFlipper,14 usingWeb3Http,15 itWeb3 } from './util/helpers';16import waitNewBlocks from '../substrate/wait-new-blocks';1718describe.only('Sponsoring EVM contracts', () => {19 itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api}) => {20 await usingWeb3Http(async web3Http => {21 const owner = await createEthAccountWithBalance(api, web3Http);22 const flipper = await deployFlipper(web3Http, owner);23 await waitNewBlocks(api, 1);24 const helpers = contractHelpers(web3Http, owner);25 await waitNewBlocks(api, 1);26 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;27 await waitNewBlocks(api, 1);28 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});29 await waitNewBlocks(api, 1);30 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;31 });32 });3334 itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api}) => {35 await usingWeb3Http(async web3Http => {36 const owner = await createEthAccountWithBalance(api, web3Http);37 const notOwner = await createEthAccountWithBalance(api, web3Http);38 const flipper = await deployFlipper(web3Http, owner);39 await waitNewBlocks(api, 1);40 const helpers = contractHelpers(web3Http, owner);41 await waitNewBlocks(api, 1);42 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;43 await waitNewBlocks(api, 1);44 await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;45 await waitNewBlocks(api, 1);46 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;47 });48 });4950 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease', async ({api, web3}) => {51 await usingWeb3Http(async web3Http => {52 const alice = privateKey('//Alice');5354 const owner = await createEthAccountWithBalance(api, web3Http);55 const caller = createEthAccount(web3Http);56 const originalCallerBalance = await web3.eth.getBalance(caller);57 expect(originalCallerBalance).to.be.equal('0');5859 const flipper = await deployFlipper(web3Http, owner);60 await waitNewBlocks(api, 1);61 62 const helpers = contractHelpers(web3Http, owner);63 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });64 await waitNewBlocks(api, 1);65 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });66 await waitNewBlocks(api, 1);6768 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;69 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});70 await waitNewBlocks(api, 1);71 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});72 await waitNewBlocks(api, 1);73 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;7475 await transferBalanceToEth(api, alice, flipper.options.address);76 await waitNewBlocks(api, 2);7778 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);79 expect(originalFlipperBalance).to.be.not.equal('0');8081 await flipper.methods.flip().send({ from: caller });82 await waitNewBlocks(api, 1);83 expect(await flipper.methods.getValue().call()).to.be.true;8485 86 const balanceAfter = await web3.eth.getBalance(flipper.options.address);87 expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);88 });89 });9091 itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3}) => {92 await usingWeb3Http(async web3Http => {93 const alice = privateKey('//Alice');9495 const owner = await createEthAccountWithBalance(api, web3Http);96 const caller = createEthAccount(web3Http);97 const originalCallerBalance = await web3.eth.getBalance(caller);98 expect(originalCallerBalance).to.be.equal('0');99100 const flipper = await deployFlipper(web3Http, owner);101 await waitNewBlocks(api, 1);102 103 const helpers = contractHelpers(web3Http, owner);104 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });105 await waitNewBlocks(api, 1);106 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });107 await waitNewBlocks(api, 1);108109 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;110 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});111 await waitNewBlocks(api, 1);112 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});113 await waitNewBlocks(api, 1);114 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;115116 await transferBalanceToEth(api, alice, flipper.options.address);117 await waitNewBlocks(api, 2);118119 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);120 expect(originalFlipperBalance).to.be.not.equal('0');121122 await flipper.methods.flip().send({ from: caller });123 await waitNewBlocks(api, 1);124 expect(await flipper.methods.getValue().call()).to.be.true;125126 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);127 });128 });129130 itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3}) => {131 await usingWeb3Http(async web3Http => {132 const alice = privateKey('//Alice');133134 const owner = await createEthAccountWithBalance(api, web3Http);135 const caller = await createEthAccountWithBalance(api, web3Http);136 const originalCallerBalance = await web3.eth.getBalance(caller);137138 const flipper = await deployFlipper(web3Http, owner);139 await waitNewBlocks(api, 1);140 141 const helpers = contractHelpers(web3Http, owner);142 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });143 await waitNewBlocks(api, 1);144 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });145 await waitNewBlocks(api, 1);146147 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;148 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});149 await waitNewBlocks(api, 1);150 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});151 await waitNewBlocks(api, 1);152 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;153154 await transferBalanceToEth(api, alice, flipper.options.address);155 await waitNewBlocks(api, 2);156157 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);158 expect(originalFlipperBalance).to.be.not.equal('0');159160 await flipper.methods.flip().send({ from: caller });161 await waitNewBlocks(api, 1);162 expect(await flipper.methods.getValue().call()).to.be.true;163 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);164165 await flipper.methods.flip().send({ from: caller });166 await waitNewBlocks(api, 1);167 expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);168 });169 });170});