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
before · tests/src/eth/util/ERC721.sol
1contract ERC721 {2    uint8 _dummy = 0;3    address _dummy_addr = 0x0000000000000000000000000000000000000000;4    string _dummy_string = "";5    string stub_error =6        "this contract does not exists, code for collections is implemented at pallet side";78    event Transfer(9        address indexed from,10        address indexed to,11        uint256 indexed tokenId12    );1314    event Approval(15        address indexed owner,16        address indexed approved,17        uint256 indexed tokenId18    );1920    event ApprovalForAll(21        address indexed owner,22        address indexed operator,23        bool approved24    );2526	// 0x18160ddd27    function totalSupply() external view returns (uint256) {28        require(false, stub_error);29        return 0;30    }3132    function name() external view returns (string memory res_name) {33        require(false, stub_error);34        res_name = _dummy_string;35    }3637    function symbol() external view returns (string memory res_symbol) {38        require(false, stub_error);39        res_symbol = _dummy_string;40    }4142    function tokenURI(uint256 tokenId) external view returns (string memory) {43        require(false, stub_error);44        tokenId;45        return _dummy_string;46    }4748    function tokenByIndex(uint256 index) external view returns (uint256) {49        require(false, stub_error);50        index;51		return 0;52    }5354    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256) {55        require(false, stub_error);56		owner;57		index;58		return 0;59    }6061    // 0x70a0823162    function balanceOf(address owner) external view returns (uint256) {63        require(false, stub_error);64        owner;65        return 0;66    }6768    // 0x6352211e69    function ownerOf(uint256 tokenId) external view returns (address) {70        require(false, stub_error);71        tokenId;72        return _dummy_addr;73    }7475    // 0xb88d4fde76    function safeTransferFrom(77        address from,78        address to,79        uint256 tokenId,80        bytes calldata data81    ) external payable {82        require(false, stub_error);83        from;84        to;85        tokenId;86        data;87    }8889    // 0x42842e0e90    function safeTransferFrom(91        address from,92        address to,93        uint256 tokenId94    ) external payable {95        require(false, stub_error);96        from;97        to;98        tokenId;99    }100101    // 0x23b872dd102    function transferFrom(103        address from,104        address to,105        uint256 tokenId106    ) external payable {107        require(false, stub_error);108        from;109        to;110        tokenId;111    }112113    // 0x095ea7b3114    function approve(address approved, uint256 tokenId) external payable {115        require(false, stub_error);116        approved;117        tokenId;118    }119120    // 0xa22cb465121    function setApprovalForAll(address operator, bool approved) external {122        require(false, stub_error);123        operator;124        approved;125        _dummy = 0;126    }127128    // 0x081812fc129    function getApproved(uint256 tokenId) external view returns (address) {130        require(false, stub_error);131        tokenId;132        return _dummy_addr;133    }134135    // 0xe985e9c5136    function isApprovedForAll(address owner, address operator)137        external138        view139        returns (bool)140    {141        require(false, stub_error);142        owner;143        operator;144        return false;145    }146147    // 0x01ffc9a7148    function supportsInterface(bytes4 interfaceID) public pure returns (bool) {149        return150            // ERC721151            interfaceID == 0x80ac58cd ||152            // ERC721Metadata153            interfaceID == 0x5b5e139f ||154            // ERC721Enumerable155            interfaceID == 0x780e9d63 ||156            // ERC165157            interfaceID == 0x01ffc9a7;158    }159}
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', `