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

difftreelog

test Add test for "sef sponsoring" and "remove sponsor".

Trubnikov Sergey2022-08-05parent: #f1e64f2.patch.diff
in: master

4 files changed

modifiedpallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
+++ b/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
@@ -27,8 +27,13 @@
 	}
 }
 
-// Selector: 06fc42e9
+// Selector: 6073d917
 contract ContractHelpers is Dummy, ERC165 {
+	// Get contract ovner
+	//
+	// @param Contract_address contract for which the owner is being determined.
+	// @return Contract owner.
+	//
 	// Selector: contractOwner(address) 5152b14c
 	function contractOwner(address contractAddress)
 		public
@@ -41,6 +46,11 @@
 		return 0x0000000000000000000000000000000000000000;
 	}
 
+	// Set sponsor.
+	//
+	// @param contract_address Contract for which a sponsor is being established.
+	// @param sponsor User address who set as pending sponsor.
+	//
 	// Selector: setSponsor(address,address) f01fba93
 	function setSponsor(address contractAddress, address sponsor) public {
 		require(false, stub_error);
@@ -49,6 +59,34 @@
 		dummy = 0;
 	}
 
+	// Set contract as self sponsored.
+	//
+	// @param contract_address Contract for which a self sponsoring is being enabled.
+	//
+	// Selector: selfSponsoredEnable(address) 89f7d9ae
+	function selfSponsoredEnable(address contractAddress) public {
+		require(false, stub_error);
+		contractAddress;
+		dummy = 0;
+	}
+
+	// Remove sponsor.
+	//
+	// @param contract_address Contract for which a sponsorship is being removed.
+	//
+	// Selector: removeSponsor(address) ef784250
+	function removeSponsor(address contractAddress) public {
+		require(false, stub_error);
+		contractAddress;
+		dummy = 0;
+	}
+
+	// Confirm sponsorship.
+	//
+	// @dev Caller must be same that set via [`set_sponsor`].
+	//
+	// @param contract_address Сontract for which need to confirm sponsorship.
+	//
 	// Selector: confirmSponsorship(address) abc00001
 	function confirmSponsorship(address contractAddress) public {
 		require(false, stub_error);
@@ -56,6 +94,11 @@
 		dummy = 0;
 	}
 
+	// Get current sponsor.
+	//
+	// @param contract_address The contract for which a sponsor is requested.
+	// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
+	//
 	// Selector: getSponsor(address) 743fc745
 	function getSponsor(address contractAddress)
 		public
@@ -68,6 +111,11 @@
 		return Tuple0(0x0000000000000000000000000000000000000000, 0);
 	}
 
+	// Check tat contract has confirmed sponsor.
+	//
+	// @param contract_address The contract for which the presence of a confirmed sponsor is checked.
+	// @return **true** if contract has confirmed sponsor.
+	//
 	// Selector: hasSponsor(address) 97418603
 	function hasSponsor(address contractAddress) public view returns (bool) {
 		require(false, stub_error);
@@ -76,6 +124,11 @@
 		return false;
 	}
 
+	// Check tat contract has pending sponsor.
+	//
+	// @param contract_address The contract for which the presence of a pending sponsor is checked.
+	// @return **true** if contract has pending sponsor.
+	//
 	// Selector: hasPendingSponsor(address) 39b9b242
 	function hasPendingSponsor(address contractAddress)
 		public
modifiedtests/src/eth/api/ContractHelpers.soldiffbeforeafterboth
--- a/tests/src/eth/api/ContractHelpers.sol
+++ b/tests/src/eth/api/ContractHelpers.sol
@@ -18,29 +18,74 @@
 	function supportsInterface(bytes4 interfaceID) external view returns (bool);
 }
 
-// Selector: 06fc42e9
+// Selector: 6073d917
 interface ContractHelpers is Dummy, ERC165 {
+	// Get contract ovner
+	//
+	// @param Contract_address contract for which the owner is being determined.
+	// @return Contract owner.
+	//
 	// Selector: contractOwner(address) 5152b14c
 	function contractOwner(address contractAddress)
 		external
 		view
 		returns (address);
 
+	// Set sponsor.
+	//
+	// @param contract_address Contract for which a sponsor is being established.
+	// @param sponsor User address who set as pending sponsor.
+	//
 	// Selector: setSponsor(address,address) f01fba93
 	function setSponsor(address contractAddress, address sponsor) external;
 
+	// Set contract as self sponsored.
+	//
+	// @param contract_address Contract for which a self sponsoring is being enabled.
+	//
+	// Selector: selfSponsoredEnable(address) 89f7d9ae
+	function selfSponsoredEnable(address contractAddress) external;
+
+	// Remove sponsor.
+	//
+	// @param contract_address Contract for which a sponsorship is being removed.
+	//
+	// Selector: removeSponsor(address) ef784250
+	function removeSponsor(address contractAddress) external;
+
+	// Confirm sponsorship.
+	//
+	// @dev Caller must be same that set via [`set_sponsor`].
+	//
+	// @param contract_address Сontract for which need to confirm sponsorship.
+	//
 	// Selector: confirmSponsorship(address) abc00001
 	function confirmSponsorship(address contractAddress) external;
 
+	// Get current sponsor.
+	//
+	// @param contract_address The contract for which a sponsor is requested.
+	// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
+	//
 	// Selector: getSponsor(address) 743fc745
 	function getSponsor(address contractAddress)
 		external
 		view
 		returns (Tuple0 memory);
 
+	// Check tat contract has confirmed sponsor.
+	//
+	// @param contract_address The contract for which the presence of a confirmed sponsor is checked.
+	// @return **true** if contract has confirmed sponsor.
+	//
 	// Selector: hasSponsor(address) 97418603
 	function hasSponsor(address contractAddress) external view returns (bool);
 
+	// Check tat contract has pending sponsor.
+	//
+	// @param contract_address The contract for which the presence of a pending sponsor is checked.
+	// @return **true** if contract has pending sponsor.
+	//
 	// Selector: hasPendingSponsor(address) 39b9b242
 	function hasPendingSponsor(address contractAddress)
 		external
modifiedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
--- a/tests/src/eth/contractSponsoring.test.ts
+++ b/tests/src/eth/contractSponsoring.test.ts
@@ -28,12 +28,31 @@
 import {evmToAddress} from '@polkadot/util-crypto';
 
 describe('Sponsoring EVM contracts', () => {
+  itWeb3('Self sponsored can be set by the address that deployed the contract', async ({api, web3, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const flipper = await deployFlipper(web3, owner);
+    const helpers = contractHelpers(web3, owner);
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
+    await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
+  });
+
+  itWeb3('Self sponsored can not be set by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const flipper = await deployFlipper(web3, owner);
+    const helpers = contractHelpers(web3, owner);
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
+    await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
+  });
+
   itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3, privateKeyWrapper}) => {
     const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
     const flipper = await deployFlipper(web3, owner);
     const helpers = contractHelpers(web3, owner);
     expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
+    await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;
     expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
   });
 
@@ -43,7 +62,7 @@
     const flipper = await deployFlipper(web3, owner);
     const helpers = contractHelpers(web3, owner);
     expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
-    await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).send({from: notOwner})).to.rejected;
+    await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');
     expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
   });
   
@@ -91,6 +110,19 @@
     expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
   });
 
+  itWeb3('Get self sponsored sponsor', async ({api, web3, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const flipper = await deployFlipper(web3, owner);
+    const helpers = contractHelpers(web3, owner);
+    await helpers.methods.selfSponsoredEnable(flipper.options.address).send();
+    
+    const result = await helpers.methods.getSponsor(flipper.options.address).call();
+
+    expect(result[0]).to.be.eq(flipper.options.address);
+    const sponsorSub = api.registry.createType('AccountId', '0x' + BigInt(result[1]).toString(16).padStart(64, '0')).toJSON();
+    expect(sponsorSub).to.be.eq(evmToAddress(flipper.options.address));
+  });
+
   itWeb3('Get confirmed sponsor', async ({api, web3, privateKeyWrapper}) => {
     const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
     const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
@@ -106,6 +138,37 @@
     expect(sponsorSub).to.be.eq(evmToAddress(sponsor));
   });
 
+  itWeb3('Sponsor can be removed by the address that deployed the contract', async ({api, web3, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const flipper = await deployFlipper(web3, owner);
+    const helpers = contractHelpers(web3, owner);
+
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
+    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
+    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
+    
+    await helpers.methods.removeSponsor(flipper.options.address).send();
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
+  });
+
+  itWeb3('Sponsor can not be removed by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const flipper = await deployFlipper(web3, owner);
+    const helpers = contractHelpers(web3, owner);
+
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
+    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
+    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
+    
+    await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');
+    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
+  });
+
   itWeb3('In generous mode, non-allowlisted user transaction will be sponsored', async ({api, web3, privateKeyWrapper}) => {
     const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
     const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
@@ -115,10 +178,8 @@
 
     const helpers = contractHelpers(web3, owner);
 
-    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
     await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
     await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
-    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
 
     await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});
     await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
@@ -136,6 +197,36 @@
     expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);
   });
 
+  itWeb3('In generous mode, non-allowlisted user transaction will be self sponsored', async ({api, web3, privateKeyWrapper}) => {
+    const alice = privateKeyWrapper('//Alice');
+
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+
+    const flipper = await deployFlipper(web3, owner);
+
+    const helpers = contractHelpers(web3, owner);
+
+    await helpers.methods.selfSponsoredEnable(flipper.options.address).send();
+
+    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});
+    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+
+    await transferBalanceToEth(api, alice, flipper.options.address);
+
+    const contractBalanceBefore = await ethBalanceViaSub(api, flipper.options.address);
+    const callerBalanceBefore = await ethBalanceViaSub(api, caller);
+
+    await flipper.methods.flip().send({from: caller});
+    expect(await flipper.methods.getValue().call()).to.be.true;
+
+    // Balance should be taken from sponsor instead of caller
+    const contractBalanceAfter = await ethBalanceViaSub(api, flipper.options.address);
+    const callerBalanceAfter = await ethBalanceViaSub(api, caller);
+    expect(contractBalanceAfter < contractBalanceBefore).to.be.true;
+    expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);
+  });
+
   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, privateKeyWrapper}) => {
     const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
     const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
modifiedtests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth
before · tests/src/eth/util/contractHelpersAbi.json
1[2  {3    "inputs": [4      {5        "internalType": "address",6        "name": "contractAddress",7        "type": "address"8      },9      { "internalType": "address", "name": "user", "type": "address" }10    ],11    "name": "allowed",12    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],13    "stateMutability": "view",14    "type": "function"15  },16  {17    "inputs": [18      {19        "internalType": "address",20        "name": "contractAddress",21        "type": "address"22      }23    ],24    "name": "allowlistEnabled",25    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],26    "stateMutability": "view",27    "type": "function"28  },29  {30    "inputs": [31      {32        "internalType": "address",33        "name": "contractAddress",34        "type": "address"35      }36    ],37    "name": "confirmSponsorship",38    "outputs": [],39    "stateMutability": "nonpayable",40    "type": "function"41  },42  {43    "inputs": [44      {45        "internalType": "address",46        "name": "contractAddress",47        "type": "address"48      }49    ],50    "name": "contractOwner",51    "outputs": [{ "internalType": "address", "name": "", "type": "address" }],52    "stateMutability": "view",53    "type": "function"54  },55  {56    "inputs": [57      {58        "internalType": "address",59        "name": "contractAddress",60        "type": "address"61      }62    ],63    "name": "getSponsor",64    "outputs": [65      {66        "components": [67          { "internalType": "address", "name": "field_0", "type": "address" },68          { "internalType": "uint256", "name": "field_1", "type": "uint256" }69        ],70        "internalType": "struct Tuple0",71        "name": "",72        "type": "tuple"73      }74    ],75    "stateMutability": "view",76    "type": "function"77  },78  {79    "inputs": [80      {81        "internalType": "address",82        "name": "contractAddress",83        "type": "address"84      }85    ],86    "name": "getSponsoringRateLimit",87    "outputs": [{ "internalType": "uint32", "name": "", "type": "uint32" }],88    "stateMutability": "view",89    "type": "function"90  },91  {92    "inputs": [93      {94        "internalType": "address",95        "name": "contractAddress",96        "type": "address"97      }98    ],99    "name": "hasPendingSponsor",100    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],101    "stateMutability": "view",102    "type": "function"103  },104  {105    "inputs": [106      {107        "internalType": "address",108        "name": "contractAddress",109        "type": "address"110      }111    ],112    "name": "hasSponsor",113    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],114    "stateMutability": "view",115    "type": "function"116  },117  {118    "inputs": [119      {120        "internalType": "address",121        "name": "contractAddress",122        "type": "address"123      },124      { "internalType": "address", "name": "sponsor", "type": "address" }125    ],126    "name": "setSponsor",127    "outputs": [],128    "stateMutability": "nonpayable",129    "type": "function"130  },131  {132    "inputs": [133      {134        "internalType": "address",135        "name": "contractAddress",136        "type": "address"137      },138      { "internalType": "uint8", "name": "mode", "type": "uint8" }139    ],140    "name": "setSponsoringMode",141    "outputs": [],142    "stateMutability": "nonpayable",143    "type": "function"144  },145  {146    "inputs": [147      {148        "internalType": "address",149        "name": "contractAddress",150        "type": "address"151      },152      { "internalType": "uint32", "name": "rateLimit", "type": "uint32" }153    ],154    "name": "setSponsoringRateLimit",155    "outputs": [],156    "stateMutability": "nonpayable",157    "type": "function"158  },159  {160    "inputs": [161      {162        "internalType": "address",163        "name": "contractAddress",164        "type": "address"165      }166    ],167    "name": "sponsoringEnabled",168    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],169    "stateMutability": "view",170    "type": "function"171  },172  {173    "inputs": [174      {175        "internalType": "address",176        "name": "contractAddress",177        "type": "address"178      }179    ],180    "name": "sponsoringMode",181    "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],182    "stateMutability": "view",183    "type": "function"184  },185  {186    "inputs": [187      { "internalType": "bytes4", "name": "interfaceID", "type": "bytes4" }188    ],189    "name": "supportsInterface",190    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],191    "stateMutability": "view",192    "type": "function"193  },194  {195    "inputs": [196      {197        "internalType": "address",198        "name": "contractAddress",199        "type": "address"200      },201      { "internalType": "address", "name": "user", "type": "address" },202      { "internalType": "bool", "name": "allowed", "type": "bool" }203    ],204    "name": "toggleAllowed",205    "outputs": [],206    "stateMutability": "nonpayable",207    "type": "function"208  },209  {210    "inputs": [211      {212        "internalType": "address",213        "name": "contractAddress",214        "type": "address"215      },216      { "internalType": "bool", "name": "enabled", "type": "bool" }217    ],218    "name": "toggleAllowlist",219    "outputs": [],220    "stateMutability": "nonpayable",221    "type": "function"222  }223]