git.delta.rocks / unique-network / refs/commits / a11cc833f140

difftreelog

test(contract-helpers) generous sponsoring mode

Yaroslav Bolyukin2022-01-19parent: #a5d7692.patch.diff
in: master

4 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  transferBalanceToEth,12  deployFlipper,13  itWeb3} from './util/helpers';1415describe('Sponsoring EVM contracts', () => {16  itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {17    const owner = await createEthAccountWithBalance(api, web3);18    const flipper = await deployFlipper(web3, owner);19    const helpers = contractHelpers(web3, owner);20    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;21    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});22    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;23  });2425  itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {26    const owner = await createEthAccountWithBalance(api, web3);27    const notOwner = await createEthAccountWithBalance(api, web3);28    const flipper = await deployFlipper(web3, owner);29    const helpers = contractHelpers(web3, owner);30    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;31    await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;32    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;33  });3435  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({api, web3}) => {36    const alice = privateKey('//Alice');3738    const owner = await createEthAccountWithBalance(api, web3);39    const caller = await createEthAccountWithBalance(api, web3);4041    const flipper = await deployFlipper(web3, owner);4243    const helpers = contractHelpers(web3, owner);44    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});45    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});4647    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;48    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});49    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});50    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;5152    await transferBalanceToEth(api, alice, flipper.options.address);5354    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);55    expect(originalFlipperBalance).to.be.not.equal('0');5657    await flipper.methods.flip().send({from: caller});58    expect(await flipper.methods.getValue().call()).to.be.true;5960    // Balance should be taken from flipper instead of caller61    const balanceAfter = await web3.eth.getBalance(flipper.options.address);62    expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);63  });6465  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({api, web3}) => {66    const alice = privateKey('//Alice');6768    const owner = await createEthAccountWithBalance(api, web3);69    const caller = await createEthAccountWithBalance(api, web3);7071    const flipper = await deployFlipper(web3, owner);7273    const helpers = contractHelpers(web3, owner);7475    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;76    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});77    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});78    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;7980    await transferBalanceToEth(api, alice, flipper.options.address);8182    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);83    expect(originalFlipperBalance).to.be.not.equal('0');8485    await flipper.methods.flip().send({from: caller});86    expect(await flipper.methods.getValue().call()).to.be.true;8788    // Balance should be taken from flipper instead of caller89    const balanceAfter = await web3.eth.getBalance(flipper.options.address);90    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);91  });9293  itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3}) => {94    const alice = privateKey('//Alice');9596    const owner = await createEthAccountWithBalance(api, web3);97    const caller = await createEthAccountWithBalance(api, web3);98    const originalCallerBalance = await web3.eth.getBalance(caller);99100    const flipper = await deployFlipper(web3, owner);101102    const helpers = contractHelpers(web3, owner);103    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});104    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});105106    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;107    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});108    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});109    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;110111    await transferBalanceToEth(api, alice, flipper.options.address);112113    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);114    expect(originalFlipperBalance).to.be.not.equal('0');115116    await flipper.methods.flip().send({from: caller});117    expect(await flipper.methods.getValue().call()).to.be.true;118119    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);120  });121122  itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3}) => {123    const alice = privateKey('//Alice');124125    const owner = await createEthAccountWithBalance(api, web3);126    const caller = await createEthAccountWithBalance(api, web3);127    const originalCallerBalance = await web3.eth.getBalance(caller);128129    const flipper = await deployFlipper(web3, owner);130131    const helpers = contractHelpers(web3, owner);132    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});133    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});134135    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;136    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});137    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});138    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;139140    await transferBalanceToEth(api, alice, flipper.options.address);141142    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);143    expect(originalFlipperBalance).to.be.not.equal('0');144145    await flipper.methods.flip().send({from: caller});146    expect(await flipper.methods.getValue().call()).to.be.true;147    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);148149    await flipper.methods.flip().send({from: caller});150    expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);151  });152153  // TODO: Find a way to calculate default rate limit154  itWeb3('Default rate limit equals 7200', async ({api, web3}) => {155    const owner = await createEthAccountWithBalance(api, web3);156    const flipper = await deployFlipper(web3, owner);157    const helpers = contractHelpers(web3, owner);158    expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');159  });160161  itWeb3('If allowlist mode is off and sponsorship is on, sponsorship does not work', async ({api, web3}) => {162    const alice = privateKey('//Alice');163164    const owner = await createEthAccountWithBalance(api, web3);165    const caller = await createEthAccountWithBalance(api, web3);166167    const flipper = await deployFlipper(web3, owner);168169    const helpers = contractHelpers(web3, owner);170171    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;172    await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});173    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});174    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;175176    await transferBalanceToEth(api, alice, flipper.options.address);177178    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);179    expect(originalFlipperBalance).to.be.not.equal('0');180181    await flipper.methods.flip().send({from: caller});182    expect(await flipper.methods.getValue().call()).to.be.true;183184    // Balance should be taken from flipper instead of caller185    const balanceAfter = await web3.eth.getBalance(flipper.options.address);186    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);187  });188189});
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,14  SponsoringMode,15  createEthAccount,16} from './util/helpers';1718describe('Sponsoring EVM contracts', () => {19  itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {20    const owner = await createEthAccountWithBalance(api, web3);21    const flipper = await deployFlipper(web3, owner);22    const helpers = contractHelpers(web3, owner);23    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;24    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});25    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;26  });2728  itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {29    const owner = await createEthAccountWithBalance(api, web3);30    const notOwner = await createEthAccountWithBalance(api, web3);31    const flipper = await deployFlipper(web3, owner);32    const helpers = contractHelpers(web3, owner);33    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;34    await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).send({from: notOwner})).to.rejected;35    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;36  });3738  itWeb3('In generous mode, non-allowlisted user transaction will be sponsored', async ({ api, web3 }) => {39    const alice = privateKey('//Alice');4041    const owner = await createEthAccountWithBalance(api, web3);42    const caller = await createEthAccountWithBalance(api, web3);4344    const flipper = await deployFlipper(web3, owner);4546    const helpers = contractHelpers(web3, owner);4748    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;49    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});50    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});51    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;5253    await transferBalanceToEth(api, alice, flipper.options.address);5455    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);56    expect(originalFlipperBalance).to.be.not.equal('0');5758    await flipper.methods.flip().send({from: caller});59    expect(await flipper.methods.getValue().call()).to.be.true;6061    // Balance should be taken from flipper instead of caller62    const balanceAfter = await web3.eth.getBalance(flipper.options.address);63    expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);64  });6566  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({api, web3}) => {67    const alice = privateKey('//Alice');6869    const owner = await createEthAccountWithBalance(api, web3);70    const caller = await createEthAccount(api);7172    const flipper = await deployFlipper(web3, owner);7374    const helpers = contractHelpers(web3, owner);75    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});76    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});7778    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;79    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});80    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});81    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;8283    await transferBalanceToEth(api, alice, flipper.options.address);8485    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);86    expect(originalFlipperBalance).to.be.not.equal('0');8788    await flipper.methods.flip().send({from: caller});89    expect(await flipper.methods.getValue().call()).to.be.true;9091    // Balance should be taken from flipper instead of caller92    const balanceAfter = await web3.eth.getBalance(flipper.options.address);93    expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);94  });9596  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({api, web3}) => {97    const alice = privateKey('//Alice');9899    const owner = await createEthAccountWithBalance(api, web3);100    const caller = await createEthAccount(api);101102    const flipper = await deployFlipper(web3, owner);103104    const helpers = contractHelpers(web3, owner);105106    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;107    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});108    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});109    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;110111    await transferBalanceToEth(api, alice, flipper.options.address);112113    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);114    expect(originalFlipperBalance).to.be.not.equal('0');115116    await flipper.methods.flip().send({from: caller});117    expect(await flipper.methods.getValue().call()).to.be.true;118119    // Balance should be taken from flipper instead of caller120    const balanceAfter = await web3.eth.getBalance(flipper.options.address);121    expect(+balanceAfter).to.be.equals(+originalFlipperBalance);122  });123124  itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3}) => {125    const alice = privateKey('//Alice');126127    const owner = await createEthAccountWithBalance(api, web3);128    const caller = await createEthAccountWithBalance(api, web3);129    const originalCallerBalance = await web3.eth.getBalance(caller);130131    const flipper = await deployFlipper(web3, owner);132133    const helpers = contractHelpers(web3, owner);134    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});135    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});136137    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;138    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});139    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});140    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;141142    await transferBalanceToEth(api, alice, flipper.options.address);143144    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);145    expect(originalFlipperBalance).to.be.not.equal('0');146147    await flipper.methods.flip().send({from: caller});148    expect(await flipper.methods.getValue().call()).to.be.true;149150    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);151  });152153  itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3}) => {154    const alice = privateKey('//Alice');155156    const owner = await createEthAccountWithBalance(api, web3);157    const caller = await createEthAccountWithBalance(api, web3);158    const originalCallerBalance = await web3.eth.getBalance(caller);159160    const flipper = await deployFlipper(web3, owner);161162    const helpers = contractHelpers(web3, owner);163    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});164    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});165166    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;167    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});168    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});169    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;170171    await transferBalanceToEth(api, alice, flipper.options.address);172173    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);174    expect(originalFlipperBalance).to.be.not.equal('0');175176    await flipper.methods.flip().send({from: caller});177    expect(await flipper.methods.getValue().call()).to.be.true;178    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);179180    await flipper.methods.flip().send({from: caller});181    expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);182  });183184  // TODO: Find a way to calculate default rate limit185  itWeb3('Default rate limit equals 7200', async ({api, web3}) => {186    const owner = await createEthAccountWithBalance(api, web3);187    const flipper = await deployFlipper(web3, owner);188    const helpers = contractHelpers(web3, owner);189    expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');190  });191});
modifiedtests/src/eth/marketplace/marketplace.test.tsdiffbeforeafterboth
--- a/tests/src/eth/marketplace/marketplace.test.ts
+++ b/tests/src/eth/marketplace/marketplace.test.ts
@@ -2,7 +2,7 @@
 import {getBalanceSingle, transferBalanceExpectSuccess} from '../../substrate/get-balance';
 import privateKey from '../../substrate/privateKey';
 import {addToAllowListExpectSuccess, confirmSponsorshipExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, createItemExpectSuccess, getTokenOwner, setCollectionLimitsExpectSuccess, setCollectionSponsorExpectSuccess, transferExpectSuccess, transferFromExpectSuccess} from '../../util/helpers';
-import {collectionIdToAddress, contractHelpers, createEthAccountWithBalance, executeEthTxOnSub, GAS_ARGS, itWeb3, subToEth, subToEthLowercase, transferBalanceToEth} from '../util/helpers';
+import {collectionIdToAddress, contractHelpers, createEthAccountWithBalance, executeEthTxOnSub, GAS_ARGS, itWeb3, SponsoringMode, subToEth, subToEthLowercase, transferBalanceToEth} from '../util/helpers';
 import {evmToAddress} from '@polkadot/util-crypto';
 import nonFungibleAbi from '../nonFungibleAbi.json';
 import fungibleAbi from '../fungibleAbi.json';
@@ -20,7 +20,7 @@
     });
     const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});
     const helpers = contractHelpers(web3, matcherOwner);
-    await helpers.methods.toggleSponsoring(matcher.options.address, true).send({from: matcherOwner});
+    await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});
     await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});
     await transferBalanceToEth(api, alice, matcher.options.address);
 
@@ -147,7 +147,7 @@
     const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments: [matcherOwner]}).send({from: matcherOwner, gas: 10000000});
     await matcher.methods.setEscrow(escrow).send({from: matcherOwner});
     const helpers = contractHelpers(web3, matcherOwner);
-    await helpers.methods.toggleSponsoring(matcher.options.address, true).send({from: matcherOwner});
+    await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});
     await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});
     await transferBalanceToEth(api, alice, matcher.options.address);
 
modifiedtests/src/eth/sponsoring.test.tsdiffbeforeafterboth
--- a/tests/src/eth/sponsoring.test.ts
+++ b/tests/src/eth/sponsoring.test.ts
@@ -1,6 +1,6 @@
 import {expect} from 'chai';
 import privateKey from '../substrate/privateKey';
-import {contractHelpers, createEthAccount, createEthAccountWithBalance, deployCollector, deployFlipper, itWeb3, transferBalanceToEth} from './util/helpers';
+import {contractHelpers, createEthAccount, createEthAccountWithBalance, deployCollector, deployFlipper, itWeb3, SponsoringMode, transferBalanceToEth} from './util/helpers';
 
 describe('EVM sponsoring', () => {
   itWeb3('Fee is deducted from contract if sponsoring is enabled', async ({api, web3}) => {
@@ -18,7 +18,7 @@
     await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: 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 helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
     await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
     expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
 
@@ -49,7 +49,7 @@
     await helpers.methods.toggleAllowed(collector.options.address, caller, true).send({from: owner});
 
     expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.false;
-    await helpers.methods.toggleSponsoring(collector.options.address, true).send({from: owner});
+    await helpers.methods.setSponsoringMode(collector.options.address, SponsoringMode.Allowlisted).send({from: owner});
     await helpers.methods.setSponsoringRateLimit(collector.options.address, 0).send({from: owner});
     expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.true;
 
modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -21,6 +21,12 @@
 
 export const GAS_ARGS = {gas: 2500000};
 
+export enum SponsoringMode {
+  Disabled,
+  Allowlisted,
+  Generous,
+}
+
 let web3Connected = false;
 export async function usingWeb3<T>(cb: (web3: Web3) => Promise<T> | T): Promise<T> {
   if (web3Connected) throw new Error('do not nest usingWeb3 calls');