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

difftreelog

Sponsoring tests

str-mv2021-09-01parent: #54811ce.patch.diff
in: master

3 files changed

modifiedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
--- a/tests/src/eth/contractSponsoring.test.ts
+++ b/tests/src/eth/contractSponsoring.test.ts
@@ -5,92 +5,166 @@
 
 import privateKey from '../substrate/privateKey';
 import { expect } from 'chai';
-import { createCollectionExpectSuccess, 
-  createFungibleItemExpectSuccess, 
-  transferExpectSuccess, 
-  transferFromExpectSuccess, 
-  createItemExpectSuccess, 
-  setContractSponsoringRateLimitExpectSuccess,
-  enableContractSponsoringExpectSuccess,
-  setCollectionSponsorExpectSuccess,
-  confirmSponsorshipExpectSuccess,
-  enableContractSponsoringExpectFailure} from '../util/helpers';
-import { collectionIdToAddress, 
+import {  
   contractHelpers,
   createEthAccountWithBalance,
   createEthAccount,
   transferBalanceToEth,
-  subToEth,
   deployFlipper,
   usingWeb3Http,
-  deployFungibleContract,
-  GAS_ARGS, itWeb3 } from './util/helpers';
+  itWeb3 } from './util/helpers';
 import waitNewBlocks from '../substrate/wait-new-blocks';
-import fungibleAbi from './fungibleAbi.json';
-import nonFungibleAbi from './nonFungibleAbi.json';
 
 describe.only('Sponsoring EVM contracts', () => {
-  itWeb3.skip('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {
+  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 fungible = await deployFungibleContract(web3Http, owner);
+      const flipper = await deployFlipper(web3Http, owner);
       await waitNewBlocks(api, 1);
       const helpers = contractHelpers(web3Http, owner);
       await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;
+      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
       await waitNewBlocks(api, 1);
-      await helpers.methods.toggleSponsoring(fungible.options.address, true).send({from: owner});
+      await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});
       await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.true;
+      expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
     });
   });
 
-  itWeb3.skip('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {
+  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 fungible = await deployFungibleContract(web3Http, owner);
+      const flipper = await deployFlipper(web3Http, owner);
       await waitNewBlocks(api, 1);
       const helpers = contractHelpers(web3Http, owner);
       await waitNewBlocks(api, 1);
-      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;
+      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(fungible.options.address).call()).to.be.false;
+      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');
+
+      const owner = await createEthAccountWithBalance(api, web3Http);
+      const caller = createEthAccount(web3Http);
+      const originalCallerBalance = await web3.eth.getBalance(caller);
+      expect(originalCallerBalance).to.be.equal('0');
+
+      const flipper = await deployFlipper(web3Http, 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);
+
+      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;
+
+      // 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', async ({api, web3}) => {
+  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 bob = privateKey('//Bob');
-      const collection = await createCollectionExpectSuccess({
-        mode: { type: 'Fungible', decimalPoints: 0 },
-      });
-      await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, alice.address);
+
       const owner = await createEthAccountWithBalance(api, web3Http);
-      const notOwner = await createEthAccountWithBalance(api, web3Http);
-      await transferExpectSuccess(collection, 0, alice, { ethereum: notOwner } , 200, 'Fungible');
-      const address = collectionIdToAddress(collection);
-      const fungible = await deployFungibleContract(web3Http, address);
-      await transferBalanceToEth(api, alice, fungible.options.address);
+      const caller = createEthAccount(web3Http);
+      const originalCallerBalance = await web3.eth.getBalance(caller);
+      expect(originalCallerBalance).to.be.equal('0');
+
+      const flipper = await deployFlipper(web3Http, 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);
-      const balanceBefore = await web3.eth.getBalance(fungible.options.address);
+      await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });
+      await waitNewBlocks(api, 1);
 
-      const helpers = contractHelpers(web3Http, address);
+      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);
-      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;
+      await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
       await waitNewBlocks(api, 1);
-      await helpers.methods.toggleSponsoring(fungible.options.address, true).send({from: owner});
+      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 helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.true;
+      expect(await flipper.methods.getValue().call()).to.be.true;
 
-//      await fungible.methods.approve(owner, 50).send({ from: owner });
-      await fungible.methods.transferFrom(notOwner, owner, 50).send({ from: notOwner });
-      const balanceAfter = await web3.eth.getBalance(fungible.options.address);
-      expect(+balanceAfter).to.lessThan(+balanceBefore);
+      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 owner = await createEthAccountWithBalance(api, web3Http);
+      const caller = await createEthAccountWithBalance(api, web3Http);
+      const originalCallerBalance = await web3.eth.getBalance(caller);
+
+      const flipper = await deployFlipper(web3Http, 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);
+
+      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);
+    });
+  });
 });
deletedtests/src/eth/util/ERC721.soldiffbeforeafterboth

no changes

modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -17,7 +17,6 @@
 import config from '../../config';
 import privateKey from '../../substrate/privateKey';
 import contractHelpersAbi from './contractHelpersAbi.json';
-import fs from 'fs';
 
 export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };
 
@@ -176,20 +175,20 @@
   };
 }
 
-export async function deployFungibleContract(web3: Web3 & Web3HttpMarker, deployer: string) {
+// export async function deployFungibleContract(web3: Web3 & Web3HttpMarker, deployer: string) {
 
-  const sol = fs.readFileSync(__dirname + '/ERC721.sol').toString();
+//   const sol = fs.readFileSync(__dirname + '/ERC721.sol').toString();
 
-  const compiled = compileContract('ERC721', sol);
-  const ERC721 = new web3.eth.Contract(compiled.abi, undefined, {
-    data: compiled.object,
-    from: deployer,
-    ...GAS_ARGS,
-  });
-  const fungible = await ERC721.deploy({ data: compiled.object }).send({from: deployer});
+//   const compiled = compileContract('ERC721', sol);
+//   const ERC721 = new web3.eth.Contract(compiled.abi, undefined, {
+//     data: compiled.object,
+//     from: deployer,
+//     ...GAS_ARGS,
+//   });
+//   const fungible = await ERC721.deploy({ data: compiled.object }).send({from: deployer});
 
-  return fungible;
-}
+//   return fungible;
+// }
 
 export async function deployFlipper(web3: Web3 & Web3HttpMarker, deployer: string) {
   const compiled = compileContract('Flipper', `