difftreelog
Tests: fix eth contractsSponsoing
in: master
1 file changed
tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import * as solc from 'solc';18import {expectSubstrateEventsAtBlock} from '../util/helpers';19import Web3 from 'web3';2021import {22 SponsoringMode,23 normalizeEvents,24 CompiledContract,25 GAS_ARGS,26} from './util/helpers';27import {itEth, expect} from '../eth/util/playgrounds';282930describe('Sponsoring EVM contracts', () => {31 itEth('Self sponsored can be set by the address that deployed the contract', async ({helper, privateKey}) => {32 const alice = privateKey('//Alice');3334 const owner = await helper.eth.createAccountWithBalance(alice);35 const flipper = await helper.eth.deployFlipper(owner);36 const helpers = helper.ethNativeContract.contractHelpers(owner);3738 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;39 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;40 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;41 });4243 itEth('Set self sponsored events', async ({helper, privateKey}) => {44 const alice = privateKey('//Alice');4546 const owner = await helper.eth.createAccountWithBalance(alice);47 const flipper = await helper.eth.deployFlipper(owner);48 const helpers = helper.ethNativeContract.contractHelpers(owner);49 50 const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();51 const ethEvents = normalizeEvents(result.events);52 expect(ethEvents).to.be.deep.equal([53 {54 address: flipper.options.address,55 event: 'ContractSponsorSet',56 args: {57 contractAddress: flipper.options.address,58 sponsor: flipper.options.address,59 },60 },61 {62 address: flipper.options.address,63 event: 'ContractSponsorshipConfirmed',64 args: {65 contractAddress: flipper.options.address,66 sponsor: flipper.options.address,67 },68 },69 ]);7071 // TODO use helper.getApi() from the sceduler PR72 await expectSubstrateEventsAtBlock(73 helper.api!,74 result.blockNumber,75 'evmContractHelpers',76 ['ContractSponsorSet','ContractSponsorshipConfirmed'],77 );78 });7980 itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper, privateKey}) => {81 const alice = privateKey('//Alice');8283 const owner = await helper.eth.createAccountWithBalance(alice);84 const notOwner = await helper.eth.createAccountWithBalance(alice);85 const helpers = helper.ethNativeContract.contractHelpers(owner);86 const flipper = await helper.eth.deployFlipper(owner);8788 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;89 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');90 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;91 });9293 itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper, privateKey}) => {94 const alice = privateKey('//Alice');9596 const owner = await helper.eth.createAccountWithBalance(alice);97 const helpers = helper.ethNativeContract.contractHelpers(owner);98 const flipper = await helper.eth.deployFlipper(owner);99100 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;101 await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;102 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;103 });104105 itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper, privateKey}) => {106 const alice = privateKey('//Alice');107108 const owner = await helper.eth.createAccountWithBalance(alice);109 const notOwner = await helper.eth.createAccountWithBalance(alice);110 const helpers = helper.ethNativeContract.contractHelpers(owner);111 const flipper = await helper.eth.deployFlipper(owner);112113 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;114 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');115 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;116 });117 118 itEth('Sponsor can be set by the address that deployed the contract', async ({helper, privateKey}) => {119 const alice = privateKey('//Alice');120121 const owner = await helper.eth.createAccountWithBalance(alice);122 const sponsor = await helper.eth.createAccountWithBalance(alice);123 const helpers = helper.ethNativeContract.contractHelpers(owner);124 const flipper = await helper.eth.deployFlipper(owner);125126 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;127 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;128 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;129 });130 131 itEth('Set sponsor event', async ({helper, privateKey}) => {132 const alice = privateKey('//Alice');133134 const owner = await helper.eth.createAccountWithBalance(alice);135 const sponsor = await helper.eth.createAccountWithBalance(alice);136 const helpers = helper.ethNativeContract.contractHelpers(owner);137 const flipper = await helper.eth.deployFlipper(owner);138 139 const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();140 const events = normalizeEvents(result.events);141 expect(events).to.be.deep.equal([142 {143 address: flipper.options.address,144 event: 'ContractSponsorSet',145 args: {146 contractAddress: flipper.options.address,147 sponsor: sponsor,148 },149 },150 ]);151152 // TODO use helper.getApi() from the sceduler PR153 await expectSubstrateEventsAtBlock(154 helper.api!, 155 result.blockNumber,156 'evmContractHelpers',157 ['ContractSponsorSet'],158 );159 });160 161 itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper, privateKey}) => {162 const alice = privateKey('//Alice');163164 const owner = await helper.eth.createAccountWithBalance(alice);165 const sponsor = await helper.eth.createAccountWithBalance(alice);166 const notOwner = await helper.eth.createAccountWithBalance(alice);167 const helpers = helper.ethNativeContract.contractHelpers(owner);168 const flipper = await helper.eth.deployFlipper(owner);169170 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;171 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');172 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;173 });174175 itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper, privateKey}) => {176 const alice = privateKey('//Alice');177178 const owner = await helper.eth.createAccountWithBalance(alice);179 const sponsor = await helper.eth.createAccountWithBalance(alice);180 const helpers = helper.ethNativeContract.contractHelpers(owner);181 const flipper = await helper.eth.deployFlipper(owner);182183 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;184 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;185 await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;186 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;187 });188189 itEth('Confirm sponsorship event', async ({helper, privateKey}) => {190 const alice = privateKey('//Alice');191192 const owner = await helper.eth.createAccountWithBalance(alice);193 const sponsor = await helper.eth.createAccountWithBalance(alice);194 const helpers = helper.ethNativeContract.contractHelpers(owner);195 const flipper = await helper.eth.deployFlipper(owner);196197 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;198 const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});199 const events = normalizeEvents(result.events);200 expect(events).to.be.deep.equal([201 {202 address: flipper.options.address,203 event: 'ContractSponsorshipConfirmed',204 args: {205 contractAddress: flipper.options.address,206 sponsor: sponsor,207 },208 },209 ]);210211 // TODO use helper.getApi() from the sceduler PR212 await expectSubstrateEventsAtBlock(213 helper.api!, 214 result.blockNumber,215 'evmContractHelpers',216 ['ContractSponsorshipConfirmed'],217 );218 });219220 itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper, privateKey}) => {221 const alice = privateKey('//Alice');222223 const owner = await helper.eth.createAccountWithBalance(alice);224 const sponsor = await helper.eth.createAccountWithBalance(alice);225 const notSponsor = await helper.eth.createAccountWithBalance(alice);226 const helpers = helper.ethNativeContract.contractHelpers(owner);227 const flipper = await helper.eth.deployFlipper(owner);228229 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;230 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;231 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');232 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;233 });234235 itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper, privateKey}) => {236 const alice = privateKey('//Alice');237238 const owner = await helper.eth.createAccountWithBalance(alice);239 const notSponsor = await helper.eth.createAccountWithBalance(alice);240 const helpers = helper.ethNativeContract.contractHelpers(owner);241 const flipper = await helper.eth.deployFlipper(owner);242243 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;244 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');245 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;246 });247248 itEth('Get self sponsored sponsor', async ({helper, privateKey}) => {249 const alice = privateKey('//Alice');250251 const owner = await helper.eth.createAccountWithBalance(alice);252 const helpers = helper.ethNativeContract.contractHelpers(owner);253 const flipper = await helper.eth.deployFlipper(owner);254255 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();256 257 const result = await helpers.methods.sponsor(flipper.options.address).call();258259 expect(result[0]).to.be.eq(flipper.options.address);260 expect(result[1]).to.be.eq('0');261 });262263 itEth('Get confirmed sponsor', async ({helper, privateKey}) => {264 const alice = privateKey('//Alice');265266 const owner = await helper.eth.createAccountWithBalance(alice);267 const sponsor = await helper.eth.createAccountWithBalance(alice);268 const helpers = helper.ethNativeContract.contractHelpers(owner);269 const flipper = await helper.eth.deployFlipper(owner);270271 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();272 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});273 274 const result = await helpers.methods.sponsor(flipper.options.address).call();275276 expect(result[0]).to.be.eq(sponsor);277 expect(result[1]).to.be.eq('0');278 });279280 itEth('Sponsor can be removed by the address that deployed the contract', async ({helper, privateKey}) => {281 const alice = privateKey('//Alice');282283 const owner = await helper.eth.createAccountWithBalance(alice);284 const sponsor = await helper.eth.createAccountWithBalance(alice);285 const helpers = helper.ethNativeContract.contractHelpers(owner);286 const flipper = await helper.eth.deployFlipper(owner);287288 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;289 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();290 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});291 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;292 293 await helpers.methods.removeSponsor(flipper.options.address).send();294 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;295 });296297 itEth('Remove sponsor event', async ({helper, privateKey}) => {298 const alice = privateKey('//Alice');299300 const owner = await helper.eth.createAccountWithBalance(alice);301 const sponsor = await helper.eth.createAccountWithBalance(alice);302 const helpers = helper.ethNativeContract.contractHelpers(owner);303 const flipper = await helper.eth.deployFlipper(owner);304305 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();306 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});307 308 const result = await helpers.methods.removeSponsor(flipper.options.address).send();309 const events = normalizeEvents(result.events);310 expect(events).to.be.deep.equal([311 {312 address: flipper.options.address,313 event: 'ContractSponsorRemoved',314 args: {315 contractAddress: flipper.options.address,316 },317 },318 ]);319320 // TODO use helper.getApi() from the sceduler PR321 await expectSubstrateEventsAtBlock(322 helper.api!, 323 result.blockNumber,324 'evmContractHelpers',325 ['ContractSponsorRemoved'],326 );327 });328329 itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper, privateKey}) => {330 const alice = privateKey('//Alice');331332 const owner = await helper.eth.createAccountWithBalance(alice);333 const notOwner = await helper.eth.createAccountWithBalance(alice);334 const sponsor = await helper.eth.createAccountWithBalance(alice);335 const helpers = helper.ethNativeContract.contractHelpers(owner);336 const flipper = await helper.eth.deployFlipper(owner);337338 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;339 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();340 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});341 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;342 343 await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');344 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;345 });346347 itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper, privateKey}) => {348 const alice = privateKey('//Alice');349350 const owner = await helper.eth.createAccountWithBalance(alice);351 const sponsor = await helper.eth.createAccountWithBalance(alice);352 const caller = await helper.eth.createAccountWithBalance(alice);353 const helpers = helper.ethNativeContract.contractHelpers(owner);354 const flipper = await helper.eth.deployFlipper(owner);355356 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();357 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});358359 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});360 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});361362 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));363 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));364365 await flipper.methods.flip().send({from: caller});366 expect(await flipper.methods.getValue().call()).to.be.true;367368 // Balance should be taken from sponsor instead of caller369 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));370 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));371 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;372 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);373 });374375 itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper, privateKey}) => {376 const alice = privateKey('//Alice');377378 const owner = await helper.eth.createAccountWithBalance(alice);379 const caller = await helper.eth.createAccountWithBalance(alice);380 const helpers = helper.ethNativeContract.contractHelpers(owner);381 const flipper = await helper.eth.deployFlipper(owner);382383 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();384385 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});386 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});387388 await helper.eth.transferBalanceFromSubstrate(alice, flipper.options.address);389390 const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));391 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));392393 await flipper.methods.flip().send({from: caller});394 expect(await flipper.methods.getValue().call()).to.be.true;395396 // Balance should be taken from sponsor instead of caller397 const contractBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));398 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));399 expect(contractBalanceAfter < contractBalanceBefore).to.be.true;400 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);401 });402403 itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({helper, privateKey}) => {404 const alice = privateKey('//Alice');405406 const owner = await helper.eth.createAccountWithBalance(alice);407 const sponsor = await helper.eth.createAccountWithBalance(alice);408 const caller = await helper.eth.createAccount();409 const helpers = helper.ethNativeContract.contractHelpers(owner);410 const flipper = await helper.eth.deployFlipper(owner);411412 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});413 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});414415 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});416 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});417418 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();419 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});420421 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));422 expect(sponsorBalanceBefore).to.be.not.equal('0');423424 await flipper.methods.flip().send({from: caller});425 expect(await flipper.methods.getValue().call()).to.be.true;426427 // Balance should be taken from flipper instead of caller428 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));429 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;430 });431432 itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({helper, privateKey}) => {433 const alice = privateKey('//Alice');434435 const owner = await helper.eth.createAccountWithBalance(alice);436 const caller = await helper.eth.createAccount();437 const helpers = helper.ethNativeContract.contractHelpers(owner);438 const flipper = await helper.eth.deployFlipper(owner);439440 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});441 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});442443 await helper.eth.transferBalanceFromSubstrate(alice, flipper.options.address);444445 const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);446 expect(originalFlipperBalance).to.be.not.equal('0');447448 await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/InvalidTransaction::Payment/);449 expect(await flipper.methods.getValue().call()).to.be.false;450451 // Balance should be taken from flipper instead of caller452 // FIXME the comment is wrong! What check should be here?453 const balanceAfter = await helper.balance.getEthereum(flipper.options.address);454 expect(balanceAfter).to.be.equals(originalFlipperBalance);455 });456457 itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper, privateKey}) => {458 const alice = privateKey('//Alice');459460 const owner = await helper.eth.createAccountWithBalance(alice);461 const sponsor = await helper.eth.createAccountWithBalance(alice);462 const caller = await helper.eth.createAccountWithBalance(alice);463 const helpers = helper.ethNativeContract.contractHelpers(owner);464 const flipper = await helper.eth.deployFlipper(owner);465466 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});467 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});468469 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});470 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});471472 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();473 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});474475 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));476 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));477478 await flipper.methods.flip().send({from: caller});479 expect(await flipper.methods.getValue().call()).to.be.true;480481 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));482 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));483 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;484 expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);485 });486487 itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper, privateKey}) => {488 const alice = privateKey('//Alice');489490 const owner = await helper.eth.createAccountWithBalance(alice);491 const sponsor = await helper.eth.createAccountWithBalance(alice);492 const caller = await helper.eth.createAccountWithBalance(alice);493 const helpers = helper.ethNativeContract.contractHelpers(owner);494 const flipper = await helper.eth.deployFlipper(owner);495 496 const originalCallerBalance = await helper.balance.getEthereum(caller);497 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});498 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});499500 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});501 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});502503 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();504 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});505506 const originalFlipperBalance = await helper.balance.getEthereum(sponsor);507 expect(originalFlipperBalance).to.be.not.equal('0');508509 await flipper.methods.flip().send({from: caller});510 expect(await flipper.methods.getValue().call()).to.be.true;511 expect(await helper.balance.getEthereum(caller)).to.be.equals(originalCallerBalance);512513 const newFlipperBalance = await helper.balance.getEthereum(sponsor);514 expect(newFlipperBalance).to.be.not.equals(originalFlipperBalance);515516 await flipper.methods.flip().send({from: caller});517 expect(await helper.balance.getEthereum(sponsor)).to.be.equal(newFlipperBalance);518 expect(await helper.balance.getEthereum(caller)).to.be.not.equals(originalCallerBalance);519 });520521 // TODO: Find a way to calculate default rate limit522 itEth('Default rate limit equals 7200', async ({helper, privateKey}) => {523 const alice = privateKey('//Alice');524525 const owner = await helper.eth.createAccountWithBalance(alice);526 const helpers = helper.ethNativeContract.contractHelpers(owner);527 const flipper = await helper.eth.deployFlipper(owner);528529 expect(await helpers.methods.sponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');530 });531});532533describe('Sponsoring Fee Limit', () => {534535 let testContract: CompiledContract;536 537 function compileTestContract() {538 if (!testContract) {539 const input = {540 language: 'Solidity',541 sources: {542 ['TestContract.sol']: {543 content:544 `545 // SPDX-License-Identifier: MIT546 pragma solidity ^0.8.0;547 548 contract TestContract {549 event Result(bool);550551 function test(uint32 cycles) public {552 uint256 counter = 0;553 while(true) {554 counter ++;555 if (counter > cycles){556 break;557 }558 }559 emit Result(true);560 }561 }562 `,563 },564 },565 settings: {566 outputSelection: {567 '*': {568 '*': ['*'],569 },570 },571 },572 };573 const json = JSON.parse(solc.compile(JSON.stringify(input)));574 const out = json.contracts['TestContract.sol']['TestContract'];575 576 testContract = {577 abi: out.abi,578 object: '0x' + out.evm.bytecode.object,579 };580 }581 return testContract;582 }583 584 async function deployTestContract(web3: Web3, owner: string) {585 const compiled = compileTestContract();586 const testContract = new web3.eth.Contract(compiled.abi, undefined, {587 data: compiled.object,588 from: owner,589 ...GAS_ARGS,590 });591 return await testContract.deploy({data: compiled.object}).send({from: owner});592 }593594 itEth('Default fee limit', async ({helper, privateKey}) => {595 const alice = privateKey('//Alice');596597 const owner = await helper.eth.createAccountWithBalance(alice);598 const helpers = helper.ethNativeContract.contractHelpers(owner);599 const flipper = await helper.eth.deployFlipper(owner);600601 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('115792089237316195423570985008687907853269984665640564039457584007913129639935');602 });603604 itEth('Set fee limit', async ({helper, privateKey}) => {605 const alice = privateKey('//Alice');606607 const owner = await helper.eth.createAccountWithBalance(alice);608 const helpers = helper.ethNativeContract.contractHelpers(owner);609 const flipper = await helper.eth.deployFlipper(owner);610611 await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();612 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('100');613 });614615 itEth('Negative test - set fee limit by non-owner', async ({helper, privateKey}) => {616 const alice = privateKey('//Alice');617618 const owner = await helper.eth.createAccountWithBalance(alice);619 const stranger = await helper.eth.createAccountWithBalance(alice);620 const helpers = helper.ethNativeContract.contractHelpers(owner);621 const flipper = await helper.eth.deployFlipper(owner);622623 await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;624 });625626 itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {627 const alice = privateKey('//Alice');628629 const owner = await helper.eth.createAccountWithBalance(alice);630 const sponsor = await helper.eth.createAccountWithBalance(alice);631 const user = await helper.eth.createAccountWithBalance(alice);632 const helpers = helper.ethNativeContract.contractHelpers(owner);633634 const testContract = await deployTestContract(helper.getWeb3(), owner);635 636 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});637 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});638 639 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();640 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});641642 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());643644 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();645646 const originalUserBalance = await helper.balance.getEthereum(user);647 await testContract.methods.test(100).send({from: user, gas: 2_000_000});648 expect(await helper.balance.getEthereum(user)).to.be.equal(originalUserBalance);649650 await testContract.methods.test(100).send({from: user, gas: 2_100_000});651 expect(await helper.balance.getEthereum(user)).to.not.be.equal(originalUserBalance);652 });653654 itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {655 const alice = privateKey('//Alice');656657 const owner = await helper.eth.createAccountWithBalance(alice);658 const sponsor = await helper.eth.createAccountWithBalance(alice);659 const helpers = helper.ethNativeContract.contractHelpers(owner);660661 const testContract = await deployTestContract(helper.getWeb3(), owner);662 663 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});664 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});665 666 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();667 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});668669 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());670671 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();672673 const originalAliceBalance = await helper.balance.getSubstrate(alice.address);674675 // await submitTransactionAsync(676 // alice,677 // api.tx.evm.call(678 // subToEth(alice.address),679 // testContract.options.address,680 // testContract.methods.test(100).encodeABI(),681 // Uint8Array.from([]),682 // 2_000_000n,683 // gasPrice,684 // null,685 // null,686 // [],687 // ),688 // );689 await helper.eth.sendEVM(690 alice,691 testContract.options.address,692 testContract.methods.test(100).encodeABI(),693 '0',694 );695 // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.be.equal(originalAliceBalance);696 expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);697 698 // await submitTransactionAsync(699 // alice,700 // api.tx.evm.call(701 // subToEth(alice.address),702 // testContract.options.address,703 // testContract.methods.test(100).encodeABI(),704 // Uint8Array.from([]),705 // 2_100_000n,706 // gasPrice,707 // null,708 // null,709 // [],710 // ),711 // );712 await helper.eth.sendEVM(713 alice,714 testContract.options.address,715 testContract.methods.test().encodeABI(),716 '0',717 );718 // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.not.be.equal(originalAliceBalance);719 expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);720 });721});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import * as solc from 'solc';19import {EthUniqueHelper} from './util/playgrounds/unique.dev';20import {itEth, expect, SponsoringMode, usingEthPlaygrounds} from '../eth/util/playgrounds';21import {usingPlaygrounds} from '../util/playgrounds';22import {CompiledContract} from './util/playgrounds/types';2324describe('Sponsoring EVM contracts', () => {25 let donor: IKeyringPair;2627 before(async () => {28 await usingPlaygrounds(async (_helper, privateKey) => {29 donor = privateKey('//Alice');30 });31 });3233 itEth('Self sponsored can be set by the address that deployed the contract', async ({helper, privateKey}) => {34 const owner = await helper.eth.createAccountWithBalance(donor);35 const flipper = await helper.eth.deployFlipper(owner);36 const helpers = helper.ethNativeContract.contractHelpers(owner);3738 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;39 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;40 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;41 });4243 itEth('Set self sponsored events', async ({helper}) => {44 const owner = await helper.eth.createAccountWithBalance(donor);45 const flipper = await helper.eth.deployFlipper(owner);46 const helpers = helper.ethNativeContract.contractHelpers(owner);47 48 const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();49 const ethEvents = helper.eth.helper.eth.normalizeEvents(result.events);50 expect(ethEvents).to.be.deep.equal([51 {52 address: flipper.options.address,53 event: 'ContractSponsorSet',54 args: {55 contractAddress: flipper.options.address,56 sponsor: flipper.options.address,57 },58 },59 {60 address: flipper.options.address,61 event: 'ContractSponsorshipConfirmed',62 args: {63 contractAddress: flipper.options.address,64 sponsor: flipper.options.address,65 },66 },67 ]);68 });6970 itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper}) => {71 const owner = await helper.eth.createAccountWithBalance(donor);72 const notOwner = await helper.eth.createAccountWithBalance(donor);73 const helpers = helper.ethNativeContract.contractHelpers(owner);74 const flipper = await helper.eth.deployFlipper(owner);7576 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;77 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');78 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;79 });8081 itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper}) => {82 const owner = await helper.eth.createAccountWithBalance(donor);83 const helpers = helper.ethNativeContract.contractHelpers(owner);84 const flipper = await helper.eth.deployFlipper(owner);8586 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;87 await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;88 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;89 });9091 itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {92 const owner = await helper.eth.createAccountWithBalance(donor);93 const notOwner = await helper.eth.createAccountWithBalance(donor);94 const helpers = helper.ethNativeContract.contractHelpers(owner);95 const flipper = await helper.eth.deployFlipper(owner);9697 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;98 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');99 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;100 });101 102 itEth('Sponsor can be set by the address that deployed the contract', async ({helper}) => {103 const owner = await helper.eth.createAccountWithBalance(donor);104 const sponsor = await helper.eth.createAccountWithBalance(donor);105 const helpers = helper.ethNativeContract.contractHelpers(owner);106 const flipper = await helper.eth.deployFlipper(owner);107108 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;109 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;110 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;111 });112 113 itEth('Set sponsor event', async ({helper}) => {114 const owner = await helper.eth.createAccountWithBalance(donor);115 const sponsor = await helper.eth.createAccountWithBalance(donor);116 const helpers = helper.ethNativeContract.contractHelpers(owner);117 const flipper = await helper.eth.deployFlipper(owner);118 119 const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();120 const events = helper.eth.normalizeEvents(result.events);121 expect(events).to.be.deep.equal([122 {123 address: flipper.options.address,124 event: 'ContractSponsorSet',125 args: {126 contractAddress: flipper.options.address,127 sponsor: sponsor,128 },129 },130 ]);131 });132 133 itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper}) => {134 const owner = await helper.eth.createAccountWithBalance(donor);135 const sponsor = await helper.eth.createAccountWithBalance(donor);136 const notOwner = await helper.eth.createAccountWithBalance(donor);137 const helpers = helper.ethNativeContract.contractHelpers(owner);138 const flipper = await helper.eth.deployFlipper(owner);139140 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;141 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');142 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;143 });144145 itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper}) => {146 const owner = await helper.eth.createAccountWithBalance(donor);147 const sponsor = await helper.eth.createAccountWithBalance(donor);148 const helpers = helper.ethNativeContract.contractHelpers(owner);149 const flipper = await helper.eth.deployFlipper(owner);150151 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;152 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;153 await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;154 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;155 });156157 itEth('Confirm sponsorship event', async ({helper}) => {158 const owner = await helper.eth.createAccountWithBalance(donor);159 const sponsor = await helper.eth.createAccountWithBalance(donor);160 const helpers = helper.ethNativeContract.contractHelpers(owner);161 const flipper = await helper.eth.deployFlipper(owner);162163 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;164 const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});165 const events = helper.eth.normalizeEvents(result.events);166 expect(events).to.be.deep.equal([167 {168 address: flipper.options.address,169 event: 'ContractSponsorshipConfirmed',170 args: {171 contractAddress: flipper.options.address,172 sponsor: sponsor,173 },174 },175 ]);176 });177178 itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper}) => {179 const owner = await helper.eth.createAccountWithBalance(donor);180 const sponsor = await helper.eth.createAccountWithBalance(donor);181 const notSponsor = await helper.eth.createAccountWithBalance(donor);182 const helpers = helper.ethNativeContract.contractHelpers(owner);183 const flipper = await helper.eth.deployFlipper(owner);184185 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;186 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;187 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');188 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;189 });190191 itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper}) => {192 const owner = await helper.eth.createAccountWithBalance(donor);193 const notSponsor = await helper.eth.createAccountWithBalance(donor);194 const helpers = helper.ethNativeContract.contractHelpers(owner);195 const flipper = await helper.eth.deployFlipper(owner);196197 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;198 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');199 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;200 });201202 itEth('Get self sponsored sponsor', async ({helper}) => {203 const owner = await helper.eth.createAccountWithBalance(donor);204 const helpers = helper.ethNativeContract.contractHelpers(owner);205 const flipper = await helper.eth.deployFlipper(owner);206207 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();208 209 const result = await helpers.methods.sponsor(flipper.options.address).call();210211 expect(result[0]).to.be.eq(flipper.options.address);212 expect(result[1]).to.be.eq('0');213 });214215 itEth('Get confirmed sponsor', async ({helper}) => {216 const owner = await helper.eth.createAccountWithBalance(donor);217 const sponsor = await helper.eth.createAccountWithBalance(donor);218 const helpers = helper.ethNativeContract.contractHelpers(owner);219 const flipper = await helper.eth.deployFlipper(owner);220221 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();222 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});223 224 const result = await helpers.methods.sponsor(flipper.options.address).call();225226 expect(result[0]).to.be.eq(sponsor);227 expect(result[1]).to.be.eq('0');228 });229230 itEth('Sponsor can be removed by the address that deployed the contract', async ({helper}) => {231 const owner = await helper.eth.createAccountWithBalance(donor);232 const sponsor = await helper.eth.createAccountWithBalance(donor);233 const helpers = helper.ethNativeContract.contractHelpers(owner);234 const flipper = await helper.eth.deployFlipper(owner);235236 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;237 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();238 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});239 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;240 241 await helpers.methods.removeSponsor(flipper.options.address).send();242 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;243 });244245 itEth('Remove sponsor event', async ({helper}) => {246 const owner = await helper.eth.createAccountWithBalance(donor);247 const sponsor = await helper.eth.createAccountWithBalance(donor);248 const helpers = helper.ethNativeContract.contractHelpers(owner);249 const flipper = await helper.eth.deployFlipper(owner);250251 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();252 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});253 254 const result = await helpers.methods.removeSponsor(flipper.options.address).send();255 const events = helper.eth.normalizeEvents(result.events);256 expect(events).to.be.deep.equal([257 {258 address: flipper.options.address,259 event: 'ContractSponsorRemoved',260 args: {261 contractAddress: flipper.options.address,262 },263 },264 ]);265 });266267 itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper}) => {268 const owner = await helper.eth.createAccountWithBalance(donor);269 const notOwner = await helper.eth.createAccountWithBalance(donor);270 const sponsor = await helper.eth.createAccountWithBalance(donor);271 const helpers = helper.ethNativeContract.contractHelpers(owner);272 const flipper = await helper.eth.deployFlipper(owner);273274 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;275 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();276 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});277 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;278 279 await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');280 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;281 });282283 itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper}) => {284 const owner = await helper.eth.createAccountWithBalance(donor);285 const sponsor = await helper.eth.createAccountWithBalance(donor);286 const caller = await helper.eth.createAccountWithBalance(donor);287 const helpers = helper.ethNativeContract.contractHelpers(owner);288 const flipper = await helper.eth.deployFlipper(owner);289290 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();291 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});292293 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});294 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});295296 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));297 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));298299 await flipper.methods.flip().send({from: caller});300 expect(await flipper.methods.getValue().call()).to.be.true;301302 // Balance should be taken from sponsor instead of caller303 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));304 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));305 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;306 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);307 });308309 itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper}) => {310 const owner = await helper.eth.createAccountWithBalance(donor);311 const caller = await helper.eth.createAccountWithBalance(donor);312 const helpers = helper.ethNativeContract.contractHelpers(owner);313 const flipper = await helper.eth.deployFlipper(owner);314315 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();316317 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});318 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});319320 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);321322 const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));323 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));324325 await flipper.methods.flip().send({from: caller});326 expect(await flipper.methods.getValue().call()).to.be.true;327328 // Balance should be taken from sponsor instead of caller329 const contractBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));330 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));331 expect(contractBalanceAfter < contractBalanceBefore).to.be.true;332 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);333 });334335 itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({helper}) => {336 const owner = await helper.eth.createAccountWithBalance(donor);337 const sponsor = await helper.eth.createAccountWithBalance(donor);338 const caller = helper.eth.createAccount();339 const helpers = helper.ethNativeContract.contractHelpers(owner);340 const flipper = await helper.eth.deployFlipper(owner);341342 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});343 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});344345 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});346 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});347348 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();349 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});350351 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));352 expect(sponsorBalanceBefore).to.be.not.equal('0');353354 await flipper.methods.flip().send({from: caller});355 expect(await flipper.methods.getValue().call()).to.be.true;356357 // Balance should be taken from flipper instead of caller358 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));359 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;360 });361362 itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({helper}) => {363 const owner = await helper.eth.createAccountWithBalance(donor);364 const caller = await helper.eth.createAccount();365 const helpers = helper.ethNativeContract.contractHelpers(owner);366 const flipper = await helper.eth.deployFlipper(owner);367368 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});369 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});370371 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);372373 const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);374 expect(originalFlipperBalance).to.be.not.equal('0');375376 await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/InvalidTransaction::Payment/);377 expect(await flipper.methods.getValue().call()).to.be.false;378379 // Balance should be taken from flipper instead of caller380 // FIXME the comment is wrong! What check should be here?381 const balanceAfter = await helper.balance.getEthereum(flipper.options.address);382 expect(balanceAfter).to.be.equals(originalFlipperBalance);383 });384385 itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper}) => {386 const owner = await helper.eth.createAccountWithBalance(donor);387 const sponsor = await helper.eth.createAccountWithBalance(donor);388 const caller = await helper.eth.createAccountWithBalance(donor);389 const helpers = helper.ethNativeContract.contractHelpers(owner);390 const flipper = await helper.eth.deployFlipper(owner);391392 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});393 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});394395 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});396 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});397398 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();399 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});400401 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));402 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));403404 await flipper.methods.flip().send({from: caller});405 expect(await flipper.methods.getValue().call()).to.be.true;406407 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));408 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));409 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;410 expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);411 });412413 itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper}) => {414 const owner = await helper.eth.createAccountWithBalance(donor);415 const sponsor = await helper.eth.createAccountWithBalance(donor);416 const caller = await helper.eth.createAccountWithBalance(donor);417 const helpers = helper.ethNativeContract.contractHelpers(owner);418 const flipper = await helper.eth.deployFlipper(owner);419 420 const originalCallerBalance = await helper.balance.getEthereum(caller);421 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});422 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});423424 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});425 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});426427 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();428 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});429430 const originalFlipperBalance = await helper.balance.getEthereum(sponsor);431 expect(originalFlipperBalance).to.be.not.equal('0');432433 await flipper.methods.flip().send({from: caller});434 expect(await flipper.methods.getValue().call()).to.be.true;435 expect(await helper.balance.getEthereum(caller)).to.be.equals(originalCallerBalance);436437 const newFlipperBalance = await helper.balance.getEthereum(sponsor);438 expect(newFlipperBalance).to.be.not.equals(originalFlipperBalance);439440 await flipper.methods.flip().send({from: caller});441 expect(await helper.balance.getEthereum(sponsor)).to.be.equal(newFlipperBalance);442 expect(await helper.balance.getEthereum(caller)).to.be.not.equals(originalCallerBalance);443 });444445 // TODO: Find a way to calculate default rate limit446 itEth('Default rate limit equals 7200', async ({helper}) => {447 const owner = await helper.eth.createAccountWithBalance(donor);448 const helpers = helper.ethNativeContract.contractHelpers(owner);449 const flipper = await helper.eth.deployFlipper(owner);450451 expect(await helpers.methods.sponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');452 });453});454455describe('Sponsoring Fee Limit', () => {456 let donor: IKeyringPair;457 let alice: IKeyringPair;458 let DEFAULT_GAS: number;459460 function compileTestContract() {461 if (!testContract) {462 const input = {463 language: 'Solidity',464 sources: {465 ['TestContract.sol']: {466 content:467 `468 // SPDX-License-Identifier: MIT469 pragma solidity ^0.8.0;470 471 contract TestContract {472 event Result(bool);473474 function test(uint32 cycles) public {475 uint256 counter = 0;476 while(true) {477 counter ++;478 if (counter > cycles){479 break;480 }481 }482 emit Result(true);483 }484 }485 `,486 },487 },488 settings: {489 outputSelection: {490 '*': {491 '*': ['*'],492 },493 },494 },495 };496 const json = JSON.parse(solc.compile(JSON.stringify(input)));497 const out = json.contracts['TestContract.sol']['TestContract'];498 499 testContract = {500 abi: out.abi,501 object: '0x' + out.evm.bytecode.object,502 };503 }504 return testContract;505 }506 507 async function deployTestContract(helper: EthUniqueHelper, owner: string) {508 const web3 = helper.getWeb3();509 const compiled = compileTestContract();510 const testContract = new web3.eth.Contract(compiled.abi, undefined, {511 data: compiled.object,512 from: owner,513 gas: DEFAULT_GAS,514 });515 return await testContract.deploy({data: compiled.object}).send({from: owner});516 }517518 before(async () => {519 await usingEthPlaygrounds(async (helper, privateKey) => {520 donor = privateKey('//Alice');521 DEFAULT_GAS = helper.eth.DEFAULT_GAS;522 });523 });524525 beforeEach(async () => {526 await usingPlaygrounds(async (helper) => {527 [alice] = await helper.arrange.createAccounts([1000n], donor);528 });529 });530531 let testContract: CompiledContract;532 533534535 itEth('Default fee limit', async ({helper}) => {536 const owner = await helper.eth.createAccountWithBalance(donor);537 const helpers = helper.ethNativeContract.contractHelpers(owner);538 const flipper = await helper.eth.deployFlipper(owner);539540 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('115792089237316195423570985008687907853269984665640564039457584007913129639935');541 });542543 itEth('Set fee limit', async ({helper}) => {544 const owner = await helper.eth.createAccountWithBalance(donor);545 const helpers = helper.ethNativeContract.contractHelpers(owner);546 const flipper = await helper.eth.deployFlipper(owner);547548 await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();549 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('100');550 });551552 itEth('Negative test - set fee limit by non-owner', async ({helper}) => {553 const owner = await helper.eth.createAccountWithBalance(donor);554 const stranger = await helper.eth.createAccountWithBalance(donor);555 const helpers = helper.ethNativeContract.contractHelpers(owner);556 const flipper = await helper.eth.deployFlipper(owner);557558 await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;559 });560561 itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper}) => {562 const owner = await helper.eth.createAccountWithBalance(donor);563 const sponsor = await helper.eth.createAccountWithBalance(donor);564 const user = await helper.eth.createAccountWithBalance(donor);565 const helpers = helper.ethNativeContract.contractHelpers(owner);566567 const testContract = await deployTestContract(helper, owner);568 569 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});570 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});571 572 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();573 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});574575 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());576577 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();578579 const originalUserBalance = await helper.balance.getEthereum(user);580 await testContract.methods.test(100).send({from: user, gas: 2_000_000});581 expect(await helper.balance.getEthereum(user)).to.be.equal(originalUserBalance);582583 await testContract.methods.test(100).send({from: user, gas: 2_100_000});584 expect(await helper.balance.getEthereum(user)).to.not.be.equal(originalUserBalance);585 });586587 itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {588 const owner = await helper.eth.createAccountWithBalance(donor);589 const sponsor = await helper.eth.createAccountWithBalance(donor);590 const helpers = helper.ethNativeContract.contractHelpers(owner);591592 const testContract = await deployTestContract(helper, owner);593 594 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});595 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});596 597 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();598 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});599600 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());601602 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();603604 const originalAliceBalance = await helper.balance.getSubstrate(alice.address);605606 await helper.eth.sendEVM(607 alice,608 testContract.options.address,609 testContract.methods.test(100).encodeABI(),610 '0',611 2_000_000,612 );613 // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.be.equal(originalAliceBalance);614 expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);615 616 await helper.eth.sendEVM(617 alice,618 testContract.options.address,619 testContract.methods.test(100).encodeABI(),620 '0',621 2_100_000,622 );623 expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);624 });625});