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
18 function supportsInterface(bytes4 interfaceID) external view returns (bool);18 function supportsInterface(bytes4 interfaceID) external view returns (bool);
19}19}
2020
21// Selector: 06fc42e921// Selector: 6073d917
22interface ContractHelpers is Dummy, ERC165 {22interface ContractHelpers is Dummy, ERC165 {
23 // Get contract ovner
24 //
25 // @param Contract_address contract for which the owner is being determined.
26 // @return Contract owner.
27 //
23 // Selector: contractOwner(address) 5152b14c28 // Selector: contractOwner(address) 5152b14c
24 function contractOwner(address contractAddress)29 function contractOwner(address contractAddress)
25 external30 external
26 view31 view
27 returns (address);32 returns (address);
2833
34 // Set sponsor.
35 //
36 // @param contract_address Contract for which a sponsor is being established.
37 // @param sponsor User address who set as pending sponsor.
38 //
29 // Selector: setSponsor(address,address) f01fba9339 // Selector: setSponsor(address,address) f01fba93
30 function setSponsor(address contractAddress, address sponsor) external;40 function setSponsor(address contractAddress, address sponsor) external;
3141
42 // Set contract as self sponsored.
43 //
44 // @param contract_address Contract for which a self sponsoring is being enabled.
45 //
46 // Selector: selfSponsoredEnable(address) 89f7d9ae
47 function selfSponsoredEnable(address contractAddress) external;
48
49 // Remove sponsor.
50 //
51 // @param contract_address Contract for which a sponsorship is being removed.
52 //
53 // Selector: removeSponsor(address) ef784250
54 function removeSponsor(address contractAddress) external;
55
56 // Confirm sponsorship.
57 //
58 // @dev Caller must be same that set via [`set_sponsor`].
59 //
60 // @param contract_address Сontract for which need to confirm sponsorship.
61 //
32 // Selector: confirmSponsorship(address) abc0000162 // Selector: confirmSponsorship(address) abc00001
33 function confirmSponsorship(address contractAddress) external;63 function confirmSponsorship(address contractAddress) external;
3464
65 // Get current sponsor.
66 //
67 // @param contract_address The contract for which a sponsor is requested.
68 // @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
69 //
35 // Selector: getSponsor(address) 743fc74570 // Selector: getSponsor(address) 743fc745
36 function getSponsor(address contractAddress)71 function getSponsor(address contractAddress)
37 external72 external
38 view73 view
39 returns (Tuple0 memory);74 returns (Tuple0 memory);
4075
76 // Check tat contract has confirmed sponsor.
77 //
78 // @param contract_address The contract for which the presence of a confirmed sponsor is checked.
79 // @return **true** if contract has confirmed sponsor.
80 //
41 // Selector: hasSponsor(address) 9741860381 // Selector: hasSponsor(address) 97418603
42 function hasSponsor(address contractAddress) external view returns (bool);82 function hasSponsor(address contractAddress) external view returns (bool);
4383
84 // Check tat contract has pending sponsor.
85 //
86 // @param contract_address The contract for which the presence of a pending sponsor is checked.
87 // @return **true** if contract has pending sponsor.
88 //
44 // Selector: hasPendingSponsor(address) 39b9b24289 // Selector: hasPendingSponsor(address) 39b9b242
45 function hasPendingSponsor(address contractAddress)90 function hasPendingSponsor(address contractAddress)
46 external91 external
modifiedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
28import {evmToAddress} from '@polkadot/util-crypto';28import {evmToAddress} from '@polkadot/util-crypto';
2929
30describe('Sponsoring EVM contracts', () => {30describe('Sponsoring EVM contracts', () => {
31 itWeb3('Self sponsored can be set by the address that deployed the contract', async ({api, web3, privateKeyWrapper}) => {
32 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
33 const flipper = await deployFlipper(web3, owner);
34 const helpers = contractHelpers(web3, owner);
35 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
36 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;
37 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
38 });
39
40 itWeb3('Self sponsored can not be set by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => {
41 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
42 const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
43 const flipper = await deployFlipper(web3, owner);
44 const helpers = contractHelpers(web3, owner);
45 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
46 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');
47 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
48 });
49
31 itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3, privateKeyWrapper}) => {50 itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3, privateKeyWrapper}) => {
32 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);51 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
33 const flipper = await deployFlipper(web3, owner);52 const flipper = await deployFlipper(web3, owner);
34 const helpers = contractHelpers(web3, owner);53 const helpers = contractHelpers(web3, owner);
35 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;54 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
36 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});55 await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;
37 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;56 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
38 });57 });
3958
43 const flipper = await deployFlipper(web3, owner);62 const flipper = await deployFlipper(web3, owner);
44 const helpers = contractHelpers(web3, owner);63 const helpers = contractHelpers(web3, owner);
45 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;64 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
46 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).send({from: notOwner})).to.rejected;65 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');
47 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;66 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
48 });67 });
49 68
91 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;110 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
92 });111 });
112
113 itWeb3('Get self sponsored sponsor', async ({api, web3, privateKeyWrapper}) => {
114 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
115 const flipper = await deployFlipper(web3, owner);
116 const helpers = contractHelpers(web3, owner);
117 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();
118
119 const result = await helpers.methods.getSponsor(flipper.options.address).call();
120
121 expect(result[0]).to.be.eq(flipper.options.address);
122 const sponsorSub = api.registry.createType('AccountId', '0x' + BigInt(result[1]).toString(16).padStart(64, '0')).toJSON();
123 expect(sponsorSub).to.be.eq(evmToAddress(flipper.options.address));
124 });
93125
94 itWeb3('Get confirmed sponsor', async ({api, web3, privateKeyWrapper}) => {126 itWeb3('Get confirmed sponsor', async ({api, web3, privateKeyWrapper}) => {
95 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);127 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
106 expect(sponsorSub).to.be.eq(evmToAddress(sponsor));138 expect(sponsorSub).to.be.eq(evmToAddress(sponsor));
107 });139 });
140
141 itWeb3('Sponsor can be removed by the address that deployed the contract', async ({api, web3, privateKeyWrapper}) => {
142 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
143 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
144 const flipper = await deployFlipper(web3, owner);
145 const helpers = contractHelpers(web3, owner);
146
147 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
148 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
149 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
150 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
151
152 await helpers.methods.removeSponsor(flipper.options.address).send();
153 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
154 });
155
156 itWeb3('Sponsor can not be removed by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => {
157 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
158 const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
159 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
160 const flipper = await deployFlipper(web3, owner);
161 const helpers = contractHelpers(web3, owner);
162
163 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
164 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
165 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
166 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
167
168 await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');
169 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
170 });
108171
109 itWeb3('In generous mode, non-allowlisted user transaction will be sponsored', async ({api, web3, privateKeyWrapper}) => {172 itWeb3('In generous mode, non-allowlisted user transaction will be sponsored', async ({api, web3, privateKeyWrapper}) => {
110 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);173 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
115178
116 const helpers = contractHelpers(web3, owner);179 const helpers = contractHelpers(web3, owner);
117180
118 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
119 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();181 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
120 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});182 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
121 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
122183
123 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});184 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});
124 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});185 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
136 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);197 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);
137 });198 });
199
200 itWeb3('In generous mode, non-allowlisted user transaction will be self sponsored', async ({api, web3, privateKeyWrapper}) => {
201 const alice = privateKeyWrapper('//Alice');
202
203 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
204 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
205
206 const flipper = await deployFlipper(web3, owner);
207
208 const helpers = contractHelpers(web3, owner);
209
210 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();
211
212 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});
213 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
214
215 await transferBalanceToEth(api, alice, flipper.options.address);
216
217 const contractBalanceBefore = await ethBalanceViaSub(api, flipper.options.address);
218 const callerBalanceBefore = await ethBalanceViaSub(api, caller);
219
220 await flipper.methods.flip().send({from: caller});
221 expect(await flipper.methods.getValue().call()).to.be.true;
222
223 // Balance should be taken from sponsor instead of caller
224 const contractBalanceAfter = await ethBalanceViaSub(api, flipper.options.address);
225 const callerBalanceAfter = await ethBalanceViaSub(api, caller);
226 expect(contractBalanceAfter < contractBalanceBefore).to.be.true;
227 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);
228 });
138229
139 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}) => {230 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}) => {
140 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);231 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
modifiedtests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth
114 "stateMutability": "view",114 "stateMutability": "view",
115 "type": "function"115 "type": "function"
116 },116 },
117 {
118 "inputs": [
119 {
120 "internalType": "address",
121 "name": "contractAddress",
122 "type": "address"
123 }
124 ],
125 "name": "removeSponsor",
126 "outputs": [],
127 "stateMutability": "nonpayable",
128 "type": "function"
129 },
130 {
131 "inputs": [
132 {
133 "internalType": "address",
134 "name": "contractAddress",
135 "type": "address"
136 }
137 ],
138 "name": "selfSponsoredEnable",
139 "outputs": [],
140 "stateMutability": "nonpayable",
141 "type": "function"
142 },
117 {143 {
118 "inputs": [144 "inputs": [
119 {145 {