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
3 "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"