difftreelog
Missing contract sponsoring tests added
in: master
2 files changed
tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth8import { 8import { 9 contractHelpers,9 contractHelpers,10 createEthAccountWithBalance,10 createEthAccountWithBalance,11 createEthAccount,12 transferBalanceToEth,11 transferBalanceToEth,13 deployFlipper,12 deployFlipper,14 usingWeb3Http,15 itWeb3 } from './util/helpers';13 itWeb3 } from './util/helpers';16import waitNewBlocks from '../substrate/wait-new-blocks';14import waitNewBlocks from '../substrate/wait-new-blocks';171518describe.only('Sponsoring EVM contracts', () => {16describe.only('Sponsoring EVM contracts', () => {19 itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api}) => {17 itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {20 await usingWeb3Http(async web3Http => {18 const owner = await createEthAccountWithBalance(api, web3);21 const owner = await createEthAccountWithBalance(api, web3Http);22 const flipper = await deployFlipper(web3Http, owner);19 const flipper = await deployFlipper(web3, owner);23 await waitNewBlocks(api, 1);20 await waitNewBlocks(api, 1);24 const helpers = contractHelpers(web3Http, owner);21 const helpers = contractHelpers(web3, owner);25 await waitNewBlocks(api, 1);22 await waitNewBlocks(api, 1);26 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;23 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;27 await waitNewBlocks(api, 1);24 await waitNewBlocks(api, 1);28 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});25 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});29 await waitNewBlocks(api, 1);26 await waitNewBlocks(api, 1);30 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;27 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;31 });32 });28 });332934 itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api}) => {30 itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {35 await usingWeb3Http(async web3Http => {31 const owner = await createEthAccountWithBalance(api, web3);36 const owner = await createEthAccountWithBalance(api, web3Http);37 const notOwner = await createEthAccountWithBalance(api, web3Http);32 const notOwner = await createEthAccountWithBalance(api, web3);38 const flipper = await deployFlipper(web3Http, owner);33 const flipper = await deployFlipper(web3, owner);39 await waitNewBlocks(api, 1);34 await waitNewBlocks(api, 1);40 const helpers = contractHelpers(web3Http, owner);35 const helpers = contractHelpers(web3, owner);41 await waitNewBlocks(api, 1);36 await waitNewBlocks(api, 1);42 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;37 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;43 await waitNewBlocks(api, 1);38 await waitNewBlocks(api, 1);44 await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;39 await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;45 await waitNewBlocks(api, 1);40 await waitNewBlocks(api, 1);46 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;41 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;47 });48 });42 });494350 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease', async ({api, web3}) => {44 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}) => {51 await usingWeb3Http(async web3Http => {52 const alice = privateKey('//Alice');45 const alice = privateKey('//Alice');534654 const owner = await createEthAccountWithBalance(api, web3Http);47 const owner = await createEthAccountWithBalance(api, web3);55 const caller = createEthAccount(web3Http);48 const caller = await createEthAccountWithBalance(api, web3);56 const originalCallerBalance = await web3.eth.getBalance(caller);57 expect(originalCallerBalance).to.be.equal('0');584959 const flipper = await deployFlipper(web3Http, owner);50 const flipper = await deployFlipper(web3, owner);60 await waitNewBlocks(api, 1);51 await waitNewBlocks(api, 1);61 52 62 const helpers = contractHelpers(web3Http, owner);53 const helpers = contractHelpers(web3, owner);63 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });54 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });64 await waitNewBlocks(api, 1);55 await waitNewBlocks(api, 1);65 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });56 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });66 await waitNewBlocks(api, 1);57 await waitNewBlocks(api, 1);675868 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;59 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;69 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});60 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});70 await waitNewBlocks(api, 1);61 await waitNewBlocks(api, 1);71 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});62 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});72 await waitNewBlocks(api, 1);63 await waitNewBlocks(api, 1);73 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;64 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;746575 await transferBalanceToEth(api, alice, flipper.options.address);66 await transferBalanceToEth(api, alice, flipper.options.address);76 await waitNewBlocks(api, 2);67 await waitNewBlocks(api, 2);776878 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);69 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);79 expect(originalFlipperBalance).to.be.not.equal('0');70 expect(originalFlipperBalance).to.be.not.equal('0');807181 await flipper.methods.flip().send({ from: caller });72 await flipper.methods.flip().send({ from: caller });82 await waitNewBlocks(api, 1);73 await waitNewBlocks(api, 1);83 expect(await flipper.methods.getValue().call()).to.be.true;74 expect(await flipper.methods.getValue().call()).to.be.true;847585 // Balance should be taken from flipper instead of caller76 // Balance should be taken from flipper instead of caller86 const balanceAfter = await web3.eth.getBalance(flipper.options.address);77 const balanceAfter = await web3.eth.getBalance(flipper.options.address);87 expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);78 expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);88 });89 });79 });908081 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 });11391 itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3}) => {114 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 => {115 const alice = privateKey('//Alice');93 const alice = privateKey('//Alice');9411695 const owner = await createEthAccountWithBalance(api, web3Http);117 const owner = await createEthAccountWithBalance(api, web3);96 const caller = createEthAccount(web3Http);118 const caller = await createEthAccountWithBalance(api, web3);97 const originalCallerBalance = await web3.eth.getBalance(caller);119 const originalCallerBalance = await web3.eth.getBalance(caller);98 expect(originalCallerBalance).to.be.equal('0');99120100 const flipper = await deployFlipper(web3Http, owner);121 const flipper = await deployFlipper(web3, owner);101 await waitNewBlocks(api, 1);122 await waitNewBlocks(api, 1);102 123 103 const helpers = contractHelpers(web3Http, owner);124 const helpers = contractHelpers(web3, owner);104 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });125 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });105 await waitNewBlocks(api, 1);126 await waitNewBlocks(api, 1);106 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });127 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });107 await waitNewBlocks(api, 1);128 await waitNewBlocks(api, 1);108129109 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;130 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;110 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});131 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});111 await waitNewBlocks(api, 1);132 await waitNewBlocks(api, 1);112 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});133 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});113 await waitNewBlocks(api, 1);134 await waitNewBlocks(api, 1);114 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;135 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;115136116 await transferBalanceToEth(api, alice, flipper.options.address);137 await transferBalanceToEth(api, alice, flipper.options.address);117 await waitNewBlocks(api, 2);138 await waitNewBlocks(api, 2);118139119 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);140 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);120 expect(originalFlipperBalance).to.be.not.equal('0');141 expect(originalFlipperBalance).to.be.not.equal('0');121142122 await flipper.methods.flip().send({ from: caller });143 await flipper.methods.flip().send({ from: caller });123 await waitNewBlocks(api, 1);144 await waitNewBlocks(api, 1);124 expect(await flipper.methods.getValue().call()).to.be.true;145 expect(await flipper.methods.getValue().call()).to.be.true;125146126 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);147 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);127 });128 });148 });129149130 itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3}) => {150 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 => {151 const alice = privateKey('//Alice');132 const alice = privateKey('//Alice');133152134 const owner = await createEthAccountWithBalance(api, web3Http);153 const owner = await createEthAccountWithBalance(api, web3);135 const caller = await createEthAccountWithBalance(api, web3Http);154 const caller = await createEthAccountWithBalance(api, web3);136 const originalCallerBalance = await web3.eth.getBalance(caller);155 const originalCallerBalance = await web3.eth.getBalance(caller);137156138 const flipper = await deployFlipper(web3Http, owner);157 const flipper = await deployFlipper(web3, owner);139 await waitNewBlocks(api, 1);158 await waitNewBlocks(api, 1);140 159 141 const helpers = contractHelpers(web3Http, owner);160 const helpers = contractHelpers(web3, owner);142 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });161 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });143 await waitNewBlocks(api, 1);162 await waitNewBlocks(api, 1);144 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });163 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });145 await waitNewBlocks(api, 1);164 await waitNewBlocks(api, 1);146165147 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;166 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;148 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});167 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});149 await waitNewBlocks(api, 1);168 await waitNewBlocks(api, 1);150 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});169 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});151 await waitNewBlocks(api, 1);170 await waitNewBlocks(api, 1);152 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;171 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;153172154 await transferBalanceToEth(api, alice, flipper.options.address);173 await transferBalanceToEth(api, alice, flipper.options.address);155 await waitNewBlocks(api, 2);174 await waitNewBlocks(api, 2);156175157 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);176 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);158 expect(originalFlipperBalance).to.be.not.equal('0');177 expect(originalFlipperBalance).to.be.not.equal('0');159178160 await flipper.methods.flip().send({ from: caller });179 await flipper.methods.flip().send({ from: caller });161 await waitNewBlocks(api, 1);180 await waitNewBlocks(api, 1);162 expect(await flipper.methods.getValue().call()).to.be.true;181 expect(await flipper.methods.getValue().call()).to.be.true;163 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);182 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);164183165 await flipper.methods.flip().send({ from: caller });184 await flipper.methods.flip().send({ from: caller });166 await waitNewBlocks(api, 1);185 await waitNewBlocks(api, 1);167 expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);186 expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);168 });169 });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 });230170});231});171232tests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth3 "inputs": [3 "inputs": [4 {4 {5 "internalType": "address",5 "internalType": "address",6 "name": "contract",6 "name": "contractAddress",7 "type": "address"7 "type": "address"8 }8 },9 {10 "internalType": "address",11 "name": "user",12 "type": "address"13 }9 ],14 ],10 "name": "allowlistEnabled",15 "name": "allowed",11 "outputs": [16 "outputs": [12 {17 {13 "internalType": "bool",18 "internalType": "bool",22 "inputs": [27 "inputs": [23 {28 {24 "internalType": "address",29 "internalType": "address",25 "name": "target",30 "name": "contractAddress",26 "type": "address"31 "type": "address"27 },32 }28 {29 "internalType": "address",30 "name": "caller",31 "type": "address"32 }33 ],33 ],34 "name": "allowed",34 "name": "allowlistEnabled",35 "outputs": [35 "outputs": [36 {36 {37 "internalType": "bool",37 "internalType": "bool",46 "inputs": [46 "inputs": [47 {47 {48 "internalType": "address",48 "internalType": "address",49 "name": "target",49 "name": "contractAddress",50 "type": "address"50 "type": "address"51 }51 }52 ],52 ],65 "inputs": [65 "inputs": [66 {66 {67 "internalType": "address",67 "internalType": "address",68 "name": "target",68 "name": "contractAddress",69 "type": "address"69 "type": "address"70 }70 }71 ],71 ],72 "name": "sponsoringEnabled",72 "name": "getSponsoringRateLimit",73 "outputs": [73 "outputs": [74 {74 {75 "internalType": "bool",75 "internalType": "uint32",76 "name": "",76 "name": "",77 "type": "bool"77 "type": "uint32"78 }78 }79 ],79 ],80 "stateMutability": "view",80 "stateMutability": "view",84 "inputs": [84 "inputs": [85 {85 {86 "internalType": "address",86 "internalType": "address",87 "name": "target",87 "name": "contractAddress",88 "type": "address"88 "type": "address"89 },89 },90 {90 {91 "internalType": "address",91 "internalType": "uint32",92 "name": "user",92 "name": "rateLimit",93 "type": "address"93 "type": "uint32"94 },94 }95 {96 "internalType": "bool",97 "name": "isAllowed",98 "type": "bool"99 }100 ],95 ],101 "name": "toggleAllowed",96 "name": "setSponsoringRateLimit",102 "outputs": [],97 "outputs": [],103 "stateMutability": "nonpayable",98 "stateMutability": "nonpayable",104 "type": "function"99 "type": "function"105 },100 },101 {102 "inputs": [103 {104 "internalType": "address",105 "name": "contractAddress",106 "type": "address"107 }108 ],109 "name": "sponsoringEnabled",110 "outputs": [111 {112 "internalType": "bool",113 "name": "",114 "type": "bool"115 }116 ],117 "stateMutability": "view",118 "type": "function"119 },106 {120 {107 "inputs": [121 "inputs": [122 {123 "internalType": "address",124 "name": "contractAddress",125 "type": "address"126 },108 {127 {109 "internalType": "address",128 "internalType": "address",110 "name": "target",129 "name": "user",111 "type": "address"130 "type": "address"112 },131 },113 {132 {114 "internalType": "bool",133 "internalType": "bool",115 "name": "enabled",134 "name": "allowed",116 "type": "bool"135 "type": "bool"117 }136 }118 ],137 ],119 "name": "toggleAllowlist",138 "name": "toggleAllowed",120 "outputs": [],139 "outputs": [],121 "stateMutability": "nonpayable",140 "stateMutability": "nonpayable",122 "type": "function"141 "type": "function"125 "inputs": [144 "inputs": [126 {145 {127 "internalType": "address",146 "internalType": "address",128 "name": "target",147 "name": "contractAddress",129 "type": "address"148 "type": "address"130 },149 },131 {150 {134 "type": "bool"153 "type": "bool"135 }154 }136 ],155 ],137 "name": "toggleSponsoring",156 "name": "toggleAllowlist",138 "outputs": [],157 "outputs": [],139 "stateMutability": "nonpayable",158 "stateMutability": "nonpayable",140 "type": "function"159 "type": "function"143 "inputs": [162 "inputs": [144 {163 {145 "internalType": "address",164 "internalType": "address",146 "name": "target",165 "name": "contractAddress",147 "type": "address"166 "type": "address"148 },167 },149 {168 {150 "internalType": "uint32",169 "internalType": "bool",151 "name": "limit",170 "name": "enabled",152 "type": "uint32"171 "type": "bool"153 }172 }154 ],173 ],155 "name": "setSponsoringRateLimit",174 "name": "toggleSponsoring",156 "outputs": [],175 "outputs": [],157 "stateMutability": "nonpayable",176 "stateMutability": "nonpayable",158 "type": "function"177 "type": "function"