1234567891011121314151617import * 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 72 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 153 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 212 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 321 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 369 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 397 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 428 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 452 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 522 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 676 677 678 679 680 681 682 683 684 685 686 687 688 689 await helper.eth.sendEVM(690 alice,691 testContract.options.address,692 testContract.methods.test(100).encodeABI(),693 '0',694 );695 696 expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 await helper.eth.sendEVM(713 alice,714 testContract.options.address,715 testContract.methods.test().encodeABI(),716 '0',717 );718 719 expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);720 });721});