git.delta.rocks / unique-network / refs/commits / 4d2c47e3a0db

difftreelog

Missing contract sponsoring tests added

str-mv2021-09-07parent: #44bd99a.patch.diff
in: master

2 files 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  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      // Balance should be taken from flipper instead of caller86      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});
after · 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});
modifiedtests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth
--- a/tests/src/eth/util/contractHelpersAbi.json
+++ b/tests/src/eth/util/contractHelpersAbi.json
@@ -3,11 +3,16 @@
         "inputs": [
             {
                 "internalType": "address",
-                "name": "contract",
+                "name": "contractAddress",
                 "type": "address"
+            },
+            {
+                "internalType": "address",
+                "name": "user",
+                "type": "address"
             }
         ],
-        "name": "allowlistEnabled",
+        "name": "allowed",
         "outputs": [
             {
                 "internalType": "bool",
@@ -22,16 +27,11 @@
         "inputs": [
             {
                 "internalType": "address",
-                "name": "target",
-                "type": "address"
-            },
-            {
-                "internalType": "address",
-                "name": "caller",
+                "name": "contractAddress",
                 "type": "address"
             }
         ],
-        "name": "allowed",
+        "name": "allowlistEnabled",
         "outputs": [
             {
                 "internalType": "bool",
@@ -46,7 +46,7 @@
         "inputs": [
             {
                 "internalType": "address",
-                "name": "target",
+                "name": "contractAddress",
                 "type": "address"
             }
         ],
@@ -65,16 +65,16 @@
         "inputs": [
             {
                 "internalType": "address",
-                "name": "target",
+                "name": "contractAddress",
                 "type": "address"
             }
         ],
-        "name": "sponsoringEnabled",
+        "name": "getSponsoringRateLimit",
         "outputs": [
             {
-                "internalType": "bool",
+                "internalType": "uint32",
                 "name": "",
-                "type": "bool"
+                "type": "uint32"
             }
         ],
         "stateMutability": "view",
@@ -84,39 +84,58 @@
         "inputs": [
             {
                 "internalType": "address",
-                "name": "target",
+                "name": "contractAddress",
                 "type": "address"
             },
             {
+                "internalType": "uint32",
+                "name": "rateLimit",
+                "type": "uint32"
+            }
+        ],
+        "name": "setSponsoringRateLimit",
+        "outputs": [],
+        "stateMutability": "nonpayable",
+        "type": "function"
+    },
+    {
+        "inputs": [
+            {
                 "internalType": "address",
-                "name": "user",
+                "name": "contractAddress",
                 "type": "address"
-            },
+            }
+        ],
+        "name": "sponsoringEnabled",
+        "outputs": [
             {
                 "internalType": "bool",
-                "name": "isAllowed",
+                "name": "",
                 "type": "bool"
             }
         ],
-        "name": "toggleAllowed",
-        "outputs": [],
-        "stateMutability": "nonpayable",
+        "stateMutability": "view",
         "type": "function"
     },
     {
         "inputs": [
             {
                 "internalType": "address",
-                "name": "target",
+                "name": "contractAddress",
+                "type": "address"
+            },
+            {
+                "internalType": "address",
+                "name": "user",
                 "type": "address"
             },
             {
                 "internalType": "bool",
-                "name": "enabled",
+                "name": "allowed",
                 "type": "bool"
             }
         ],
-        "name": "toggleAllowlist",
+        "name": "toggleAllowed",
         "outputs": [],
         "stateMutability": "nonpayable",
         "type": "function"
@@ -125,7 +144,7 @@
         "inputs": [
             {
                 "internalType": "address",
-                "name": "target",
+                "name": "contractAddress",
                 "type": "address"
             },
             {
@@ -134,7 +153,7 @@
                 "type": "bool"
             }
         ],
-        "name": "toggleSponsoring",
+        "name": "toggleAllowlist",
         "outputs": [],
         "stateMutability": "nonpayable",
         "type": "function"
@@ -143,16 +162,16 @@
         "inputs": [
             {
                 "internalType": "address",
-                "name": "target",
+                "name": "contractAddress",
                 "type": "address"
             },
             {
-                "internalType": "uint32",
-                "name": "limit",
-                "type": "uint32"
+                "internalType": "bool",
+                "name": "enabled",
+                "type": "bool"
             }
         ],
-        "name": "setSponsoringRateLimit",
+        "name": "toggleSponsoring",
         "outputs": [],
         "stateMutability": "nonpayable",
         "type": "function"