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
--- a/tests/src/eth/contractSponsoring.test.ts
+++ b/tests/src/eth/contractSponsoring.test.ts
@@ -8,163 +8,224 @@
 import {  
   contractHelpers,
   createEthAccountWithBalance,
-  createEthAccount,
   transferBalanceToEth,
   deployFlipper,
-  usingWeb3Http,
   itWeb3 } from './util/helpers';
 import waitNewBlocks from '../substrate/wait-new-blocks';
 
 describe.only('Sponsoring EVM contracts', () => {
-  itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api}) => {
-    await usingWeb3Http(async web3Http => {
-      const owner = await createEthAccountWithBalance(api, web3Http);
-      const flipper = await deployFlipper(web3Http, owner);
-      await waitNewBlocks(api, 1);
-      const helpers = contractHelpers(web3Http, owner);
-      await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-      await waitNewBlocks(api, 1);
-      await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
-      await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
-    });
+  itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {
+    const owner = await createEthAccountWithBalance(api, web3);
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
+    const helpers = contractHelpers(web3, owner);
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
+    await waitNewBlocks(api, 1);
+    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
   });
 
-  itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api}) => {
-    await usingWeb3Http(async web3Http => {
-      const owner = await createEthAccountWithBalance(api, web3Http);
-      const notOwner = await createEthAccountWithBalance(api, web3Http);
-      const flipper = await deployFlipper(web3Http, owner);
-      await waitNewBlocks(api, 1);
-      const helpers = contractHelpers(web3Http, owner);
-      await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-      await waitNewBlocks(api, 1);
-      await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;
-      await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-    });
+  itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {
+    const owner = await createEthAccountWithBalance(api, web3);
+    const notOwner = await createEthAccountWithBalance(api, web3);
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
+    const helpers = contractHelpers(web3, owner);
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
+    await waitNewBlocks(api, 1);
+    await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
   });
 
-  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease', async ({api, web3}) => {
-    await usingWeb3Http(async web3Http => {
-      const alice = privateKey('//Alice');
+  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}) => {
+    const alice = privateKey('//Alice');
 
-      const owner = await createEthAccountWithBalance(api, web3Http);
-      const caller = createEthAccount(web3Http);
-      const originalCallerBalance = await web3.eth.getBalance(caller);
-      expect(originalCallerBalance).to.be.equal('0');
+    const owner = await createEthAccountWithBalance(api, web3);
+    const caller = await createEthAccountWithBalance(api, web3);
 
-      const flipper = await deployFlipper(web3Http, owner);
-      await waitNewBlocks(api, 1);
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
       
-      const helpers = contractHelpers(web3Http, owner);
-      await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });
-      await waitNewBlocks(api, 1);
-      await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });
-      await waitNewBlocks(api, 1);
+    const helpers = contractHelpers(web3, owner);
+    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });
+    await waitNewBlocks(api, 1);
+    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });
+    await waitNewBlocks(api, 1);
+
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
+    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
+    await waitNewBlocks(api, 1);
+    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
+
+    await transferBalanceToEth(api, alice, flipper.options.address);
+    await waitNewBlocks(api, 2);
+
+    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
+    expect(originalFlipperBalance).to.be.not.equal('0');
+
+    await flipper.methods.flip().send({ from: caller });
+    await waitNewBlocks(api, 1);
+    expect(await flipper.methods.getValue().call()).to.be.true;
 
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-      await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
-      await waitNewBlocks(api, 1);
-      await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
-      await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
+    // Balance should be taken from flipper instead of caller
+    const balanceAfter = await web3.eth.getBalance(flipper.options.address);
+    expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);
+  });
+
+  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}) => {
+    const alice = privateKey('//Alice');
+
+    const owner = await createEthAccountWithBalance(api, web3);
+    const caller = await createEthAccountWithBalance(api, web3);
+
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
+    
+    const helpers = contractHelpers(web3, owner);
+
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
+    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
+    await waitNewBlocks(api, 1);
+    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
 
-      await transferBalanceToEth(api, alice, flipper.options.address);
-      await waitNewBlocks(api, 2);
+    await transferBalanceToEth(api, alice, flipper.options.address);
+    await waitNewBlocks(api, 2);
 
-      const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
-      expect(originalFlipperBalance).to.be.not.equal('0');
+    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
+    expect(originalFlipperBalance).to.be.not.equal('0');
 
-      await flipper.methods.flip().send({ from: caller });
-      await waitNewBlocks(api, 1);
-      expect(await flipper.methods.getValue().call()).to.be.true;
+    await flipper.methods.flip().send({ from: caller });
+    await waitNewBlocks(api, 1);
+    expect(await flipper.methods.getValue().call()).to.be.true;
 
-      // Balance should be taken from flipper instead of caller
-      const balanceAfter = await web3.eth.getBalance(flipper.options.address);
-      expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);
-    });
+    // Balance should be taken from flipper instead of caller
+    const balanceAfter = await web3.eth.getBalance(flipper.options.address);
+    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);
   });
 
   itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3}) => {
-    await usingWeb3Http(async web3Http => {
-      const alice = privateKey('//Alice');
+    const alice = privateKey('//Alice');
 
-      const owner = await createEthAccountWithBalance(api, web3Http);
-      const caller = createEthAccount(web3Http);
-      const originalCallerBalance = await web3.eth.getBalance(caller);
-      expect(originalCallerBalance).to.be.equal('0');
+    const owner = await createEthAccountWithBalance(api, web3);
+    const caller = await createEthAccountWithBalance(api, web3);
+    const originalCallerBalance = await web3.eth.getBalance(caller);
 
-      const flipper = await deployFlipper(web3Http, owner);
-      await waitNewBlocks(api, 1);
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
       
-      const helpers = contractHelpers(web3Http, owner);
-      await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });
-      await waitNewBlocks(api, 1);
-      await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });
-      await waitNewBlocks(api, 1);
+    const helpers = contractHelpers(web3, owner);
+    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });
+    await waitNewBlocks(api, 1);
+    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });
+    await waitNewBlocks(api, 1);
 
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-      await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
-      await waitNewBlocks(api, 1);
-      await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
-      await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
+    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
+    await waitNewBlocks(api, 1);
+    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
 
-      await transferBalanceToEth(api, alice, flipper.options.address);
-      await waitNewBlocks(api, 2);
+    await transferBalanceToEth(api, alice, flipper.options.address);
+    await waitNewBlocks(api, 2);
 
-      const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
-      expect(originalFlipperBalance).to.be.not.equal('0');
+    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
+    expect(originalFlipperBalance).to.be.not.equal('0');
 
-      await flipper.methods.flip().send({ from: caller });
-      await waitNewBlocks(api, 1);
-      expect(await flipper.methods.getValue().call()).to.be.true;
+    await flipper.methods.flip().send({ from: caller });
+    await waitNewBlocks(api, 1);
+    expect(await flipper.methods.getValue().call()).to.be.true;
 
-      expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);
-    });
+    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);
   });
 
   itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3}) => {
-    await usingWeb3Http(async web3Http => {
-      const alice = privateKey('//Alice');
+    const alice = privateKey('//Alice');
 
-      const owner = await createEthAccountWithBalance(api, web3Http);
-      const caller = await createEthAccountWithBalance(api, web3Http);
-      const originalCallerBalance = await web3.eth.getBalance(caller);
+    const owner = await createEthAccountWithBalance(api, web3);
+    const caller = await createEthAccountWithBalance(api, web3);
+    const originalCallerBalance = await web3.eth.getBalance(caller);
 
-      const flipper = await deployFlipper(web3Http, owner);
-      await waitNewBlocks(api, 1);
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
       
-      const helpers = contractHelpers(web3Http, owner);
-      await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });
-      await waitNewBlocks(api, 1);
-      await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });
-      await waitNewBlocks(api, 1);
+    const helpers = contractHelpers(web3, owner);
+    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });
+    await waitNewBlocks(api, 1);
+    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });
+    await waitNewBlocks(api, 1);
+
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
+    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
+    await waitNewBlocks(api, 1);
+    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
+
+    await transferBalanceToEth(api, alice, flipper.options.address);
+    await waitNewBlocks(api, 2);
+
+    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
+    expect(originalFlipperBalance).to.be.not.equal('0');
+
+    await flipper.methods.flip().send({ from: caller });
+    await waitNewBlocks(api, 1);
+    expect(await flipper.methods.getValue().call()).to.be.true;
+    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);
+
+    await flipper.methods.flip().send({ from: caller });
+    await waitNewBlocks(api, 1);
+    expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);
+  });
+
+  itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {
+    const owner = await createEthAccountWithBalance(api, web3);
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
+    const helpers = contractHelpers(web3, owner);
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');
+  });
+
+  itWeb3('If whitelist mode is off and sponsorship is on, sponsorship does not work', async ({api, web3}) => {
+    const alice = privateKey('//Alice');
+
+    const owner = await createEthAccountWithBalance(api, web3);
+    const caller = await createEthAccountWithBalance(api, web3);
+
+    const flipper = await deployFlipper(web3, owner);
+    await waitNewBlocks(api, 1);
+    
+    const helpers = contractHelpers(web3, owner);
 
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-      await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
-      await waitNewBlocks(api, 1);
-      await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});
-      await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
+    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
+    await waitNewBlocks(api, 1);
+    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+    await waitNewBlocks(api, 1);
+    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
 
-      await transferBalanceToEth(api, alice, flipper.options.address);
-      await waitNewBlocks(api, 2);
+    await transferBalanceToEth(api, alice, flipper.options.address);
+    await waitNewBlocks(api, 2);
 
-      const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
-      expect(originalFlipperBalance).to.be.not.equal('0');
+    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
+    expect(originalFlipperBalance).to.be.not.equal('0');
 
-      await flipper.methods.flip().send({ from: caller });
-      await waitNewBlocks(api, 1);
-      expect(await flipper.methods.getValue().call()).to.be.true;
-      expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);
+    await flipper.methods.flip().send({ from: caller });
+    await waitNewBlocks(api, 1);
+    expect(await flipper.methods.getValue().call()).to.be.true;
 
-      await flipper.methods.flip().send({ from: caller });
-      await waitNewBlocks(api, 1);
-      expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);
-    });
+    // Balance should be taken from flipper instead of caller
+    const balanceAfter = await web3.eth.getBalance(flipper.options.address);
+    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);
   });
+
 });
modifiedtests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth
before · tests/src/eth/util/contractHelpersAbi.json
1[2    {3        "inputs": [4            {5                "internalType": "address",6                "name": "contract",7                "type": "address"8            }9        ],10        "name": "allowlistEnabled",11        "outputs": [12            {13                "internalType": "bool",14                "name": "",15                "type": "bool"16            }17        ],18        "stateMutability": "view",19        "type": "function"20    },21    {22        "inputs": [23            {24                "internalType": "address",25                "name": "target",26                "type": "address"27            },28            {29                "internalType": "address",30                "name": "caller",31                "type": "address"32            }33        ],34        "name": "allowed",35        "outputs": [36            {37                "internalType": "bool",38                "name": "",39                "type": "bool"40            }41        ],42        "stateMutability": "view",43        "type": "function"44    },45    {46        "inputs": [47            {48                "internalType": "address",49                "name": "target",50                "type": "address"51            }52        ],53        "name": "contractOwner",54        "outputs": [55            {56                "internalType": "address",57                "name": "",58                "type": "address"59            }60        ],61        "stateMutability": "view",62        "type": "function"63    },64    {65        "inputs": [66            {67                "internalType": "address",68                "name": "target",69                "type": "address"70            }71        ],72        "name": "sponsoringEnabled",73        "outputs": [74            {75                "internalType": "bool",76                "name": "",77                "type": "bool"78            }79        ],80        "stateMutability": "view",81        "type": "function"82    },83    {84        "inputs": [85            {86                "internalType": "address",87                "name": "target",88                "type": "address"89            },90            {91                "internalType": "address",92                "name": "user",93                "type": "address"94            },95            {96                "internalType": "bool",97                "name": "isAllowed",98                "type": "bool"99            }100        ],101        "name": "toggleAllowed",102        "outputs": [],103        "stateMutability": "nonpayable",104        "type": "function"105    },106    {107        "inputs": [108            {109                "internalType": "address",110                "name": "target",111                "type": "address"112            },113            {114                "internalType": "bool",115                "name": "enabled",116                "type": "bool"117            }118        ],119        "name": "toggleAllowlist",120        "outputs": [],121        "stateMutability": "nonpayable",122        "type": "function"123    },124    {125        "inputs": [126            {127                "internalType": "address",128                "name": "target",129                "type": "address"130            },131            {132                "internalType": "bool",133                "name": "enabled",134                "type": "bool"135            }136        ],137        "name": "toggleSponsoring",138        "outputs": [],139        "stateMutability": "nonpayable",140        "type": "function"141    },142    {143        "inputs": [144            {145                "internalType": "address",146                "name": "target",147                "type": "address"148            },149            {150                "internalType": "uint32",151                "name": "limit",152                "type": "uint32"153            }154        ],155        "name": "setSponsoringRateLimit",156        "outputs": [],157        "stateMutability": "nonpayable",158        "type": "function"159    }160]