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
27 }27 }
28}28}
2929
30// Selector: 06fc42e930// Selector: 6073d917
31contract ContractHelpers is Dummy, ERC165 {31contract ContractHelpers is Dummy, ERC165 {
32 // Get contract ovner
33 //
34 // @param Contract_address contract for which the owner is being determined.
35 // @return Contract owner.
36 //
32 // Selector: contractOwner(address) 5152b14c37 // Selector: contractOwner(address) 5152b14c
33 function contractOwner(address contractAddress)38 function contractOwner(address contractAddress)
34 public39 public
41 return 0x0000000000000000000000000000000000000000;46 return 0x0000000000000000000000000000000000000000;
42 }47 }
4348
49 // Set sponsor.
50 //
51 // @param contract_address Contract for which a sponsor is being established.
52 // @param sponsor User address who set as pending sponsor.
53 //
44 // Selector: setSponsor(address,address) f01fba9354 // Selector: setSponsor(address,address) f01fba93
45 function setSponsor(address contractAddress, address sponsor) public {55 function setSponsor(address contractAddress, address sponsor) public {
46 require(false, stub_error);56 require(false, stub_error);
49 dummy = 0;59 dummy = 0;
50 }60 }
5161
62 // Set contract as self sponsored.
63 //
64 // @param contract_address Contract for which a self sponsoring is being enabled.
65 //
66 // Selector: selfSponsoredEnable(address) 89f7d9ae
67 function selfSponsoredEnable(address contractAddress) public {
68 require(false, stub_error);
69 contractAddress;
70 dummy = 0;
71 }
72
73 // Remove sponsor.
74 //
75 // @param contract_address Contract for which a sponsorship is being removed.
76 //
77 // Selector: removeSponsor(address) ef784250
78 function removeSponsor(address contractAddress) public {
79 require(false, stub_error);
80 contractAddress;
81 dummy = 0;
82 }
83
84 // Confirm sponsorship.
85 //
86 // @dev Caller must be same that set via [`set_sponsor`].
87 //
88 // @param contract_address Сontract for which need to confirm sponsorship.
89 //
52 // Selector: confirmSponsorship(address) abc0000190 // Selector: confirmSponsorship(address) abc00001
53 function confirmSponsorship(address contractAddress) public {91 function confirmSponsorship(address contractAddress) public {
54 require(false, stub_error);92 require(false, stub_error);
55 contractAddress;93 contractAddress;
56 dummy = 0;94 dummy = 0;
57 }95 }
5896
97 // Get current sponsor.
98 //
99 // @param contract_address The contract for which a sponsor is requested.
100 // @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
101 //
59 // Selector: getSponsor(address) 743fc745102 // Selector: getSponsor(address) 743fc745
60 function getSponsor(address contractAddress)103 function getSponsor(address contractAddress)
61 public104 public
68 return Tuple0(0x0000000000000000000000000000000000000000, 0);111 return Tuple0(0x0000000000000000000000000000000000000000, 0);
69 }112 }
70113
114 // Check tat contract has confirmed sponsor.
115 //
116 // @param contract_address The contract for which the presence of a confirmed sponsor is checked.
117 // @return **true** if contract has confirmed sponsor.
118 //
71 // Selector: hasSponsor(address) 97418603119 // Selector: hasSponsor(address) 97418603
72 function hasSponsor(address contractAddress) public view returns (bool) {120 function hasSponsor(address contractAddress) public view returns (bool) {
73 require(false, stub_error);121 require(false, stub_error);
76 return false;124 return false;
77 }125 }
78126
127 // Check tat contract has pending sponsor.
128 //
129 // @param contract_address The contract for which the presence of a pending sponsor is checked.
130 // @return **true** if contract has pending sponsor.
131 //
79 // Selector: hasPendingSponsor(address) 39b9b242132 // Selector: hasPendingSponsor(address) 39b9b242
80 function hasPendingSponsor(address contractAddress)133 function hasPendingSponsor(address contractAddress)
81 public134 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
--- a/tests/src/eth/util/contractHelpersAbi.json
+++ b/tests/src/eth/util/contractHelpersAbi.json
@@ -120,6 +120,32 @@
         "internalType": "address",
         "name": "contractAddress",
         "type": "address"
+      }
+    ],
+    "name": "removeSponsor",
+    "outputs": [],
+    "stateMutability": "nonpayable",
+    "type": "function"
+  },
+  {
+    "inputs": [
+      {
+        "internalType": "address",
+        "name": "contractAddress",
+        "type": "address"
+      }
+    ],
+    "name": "selfSponsoredEnable",
+    "outputs": [],
+    "stateMutability": "nonpayable",
+    "type": "function"
+  },
+  {
+    "inputs": [
+      {
+        "internalType": "address",
+        "name": "contractAddress",
+        "type": "address"
       },
       { "internalType": "address", "name": "sponsor", "type": "address" }
     ],