1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {EthUniqueHelper} from './util/playgrounds/unique.dev';19import {itEth, expect, SponsoringMode, usingEthPlaygrounds} from './util';20import {usingPlaygrounds} from '../util';21import {CompiledContract} from './util/playgrounds/types';2223describe('Sponsoring EVM contracts', () => {24 let donor: IKeyringPair;2526 before(async () => {27 await usingPlaygrounds(async (_helper, privateKey) => {28 donor = await privateKey({filename: __filename});29 });30 });3132 itEth('Self sponsored can be set by the address that deployed the contract', async ({helper}) => {33 const owner = await helper.eth.createAccountWithBalance(donor);34 const flipper = await helper.eth.deployFlipper(owner);35 const helpers = helper.ethNativeContract.contractHelpers(owner);3637 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;38 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;39 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;40 });4142 itEth('Set self sponsored events', async ({helper}) => {43 const owner = await helper.eth.createAccountWithBalance(donor);44 const flipper = await helper.eth.deployFlipper(owner);45 const helpers = helper.ethNativeContract.contractHelpers(owner);46 47 const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();48 const ethEvents = helper.eth.helper.eth.normalizeEvents(result.events);49 expect(ethEvents).to.be.deep.equal([50 {51 address: flipper.options.address,52 event: 'ContractSponsorSet',53 args: {54 contractAddress: flipper.options.address,55 sponsor: flipper.options.address,56 },57 },58 {59 address: flipper.options.address,60 event: 'ContractSponsorshipConfirmed',61 args: {62 contractAddress: flipper.options.address,63 sponsor: flipper.options.address,64 },65 },66 ]);67 });6869 itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper}) => {70 const owner = await helper.eth.createAccountWithBalance(donor);71 const notOwner = await helper.eth.createAccountWithBalance(donor);72 const helpers = helper.ethNativeContract.contractHelpers(owner);73 const flipper = await helper.eth.deployFlipper(owner);7475 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;76 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');77 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;78 });7980 itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper}) => {81 const owner = await helper.eth.createAccountWithBalance(donor);82 const helpers = helper.ethNativeContract.contractHelpers(owner);83 const flipper = await helper.eth.deployFlipper(owner);8485 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;86 await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;87 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;88 });8990 itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {91 const owner = await helper.eth.createAccountWithBalance(donor);92 const notOwner = await helper.eth.createAccountWithBalance(donor);93 const helpers = helper.ethNativeContract.contractHelpers(owner);94 const flipper = await helper.eth.deployFlipper(owner);9596 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;97 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');98 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;99 });100 101 itEth('Sponsor can be set by the address that deployed the contract', async ({helper}) => {102 const owner = await helper.eth.createAccountWithBalance(donor);103 const sponsor = await helper.eth.createAccountWithBalance(donor);104 const helpers = helper.ethNativeContract.contractHelpers(owner);105 const flipper = await helper.eth.deployFlipper(owner);106107 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;108 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;109 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;110 });111 112 itEth('Set sponsor event', async ({helper}) => {113 const owner = await helper.eth.createAccountWithBalance(donor);114 const sponsor = await helper.eth.createAccountWithBalance(donor);115 const helpers = helper.ethNativeContract.contractHelpers(owner);116 const flipper = await helper.eth.deployFlipper(owner);117 118 const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();119 const events = helper.eth.normalizeEvents(result.events);120 expect(events).to.be.deep.equal([121 {122 address: flipper.options.address,123 event: 'ContractSponsorSet',124 args: {125 contractAddress: flipper.options.address,126 sponsor: sponsor,127 },128 },129 ]);130 });131 132 itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper}) => {133 const owner = await helper.eth.createAccountWithBalance(donor);134 const sponsor = await helper.eth.createAccountWithBalance(donor);135 const notOwner = await helper.eth.createAccountWithBalance(donor);136 const helpers = helper.ethNativeContract.contractHelpers(owner);137 const flipper = await helper.eth.deployFlipper(owner);138139 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;140 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');141 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;142 });143144 itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper}) => {145 const owner = await helper.eth.createAccountWithBalance(donor);146 const sponsor = await helper.eth.createAccountWithBalance(donor);147 const helpers = helper.ethNativeContract.contractHelpers(owner);148 const flipper = await helper.eth.deployFlipper(owner);149150 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;151 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;152 await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;153 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;154 });155156 itEth('Confirm sponsorship event', async ({helper}) => {157 const owner = await helper.eth.createAccountWithBalance(donor);158 const sponsor = await helper.eth.createAccountWithBalance(donor);159 const helpers = helper.ethNativeContract.contractHelpers(owner);160 const flipper = await helper.eth.deployFlipper(owner);161162 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;163 const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});164 const events = helper.eth.normalizeEvents(result.events);165 expect(events).to.be.deep.equal([166 {167 address: flipper.options.address,168 event: 'ContractSponsorshipConfirmed',169 args: {170 contractAddress: flipper.options.address,171 sponsor: sponsor,172 },173 },174 ]);175 });176177 itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper}) => {178 const owner = await helper.eth.createAccountWithBalance(donor);179 const sponsor = await helper.eth.createAccountWithBalance(donor);180 const notSponsor = await helper.eth.createAccountWithBalance(donor);181 const helpers = helper.ethNativeContract.contractHelpers(owner);182 const flipper = await helper.eth.deployFlipper(owner);183184 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;185 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;186 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');187 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;188 });189190 itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper}) => {191 const owner = await helper.eth.createAccountWithBalance(donor);192 const notSponsor = await helper.eth.createAccountWithBalance(donor);193 const helpers = helper.ethNativeContract.contractHelpers(owner);194 const flipper = await helper.eth.deployFlipper(owner);195196 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;197 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');198 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;199 });200201 itEth('Get self sponsored sponsor', async ({helper}) => {202 const owner = await helper.eth.createAccountWithBalance(donor);203 const helpers = helper.ethNativeContract.contractHelpers(owner);204 const flipper = await helper.eth.deployFlipper(owner);205206 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();207 208 const result = await helpers.methods.sponsor(flipper.options.address).call();209210 expect(result[0]).to.be.eq(flipper.options.address);211 expect(result[1]).to.be.eq('0');212 });213214 itEth('Get confirmed sponsor', async ({helper}) => {215 const owner = await helper.eth.createAccountWithBalance(donor);216 const sponsor = await helper.eth.createAccountWithBalance(donor);217 const helpers = helper.ethNativeContract.contractHelpers(owner);218 const flipper = await helper.eth.deployFlipper(owner);219220 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();221 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});222 223 const result = await helpers.methods.sponsor(flipper.options.address).call();224225 expect(result[0]).to.be.eq(sponsor);226 expect(result[1]).to.be.eq('0');227 });228229 itEth('Sponsor can be removed by the address that deployed the contract', async ({helper}) => {230 const owner = await helper.eth.createAccountWithBalance(donor);231 const sponsor = await helper.eth.createAccountWithBalance(donor);232 const helpers = helper.ethNativeContract.contractHelpers(owner);233 const flipper = await helper.eth.deployFlipper(owner);234235 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;236 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();237 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});238 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;239 240 await helpers.methods.removeSponsor(flipper.options.address).send();241 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;242 });243244 itEth('Remove sponsor event', async ({helper}) => {245 const owner = await helper.eth.createAccountWithBalance(donor);246 const sponsor = await helper.eth.createAccountWithBalance(donor);247 const helpers = helper.ethNativeContract.contractHelpers(owner);248 const flipper = await helper.eth.deployFlipper(owner);249250 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();251 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});252 253 const result = await helpers.methods.removeSponsor(flipper.options.address).send();254 const events = helper.eth.normalizeEvents(result.events);255 expect(events).to.be.deep.equal([256 {257 address: flipper.options.address,258 event: 'ContractSponsorRemoved',259 args: {260 contractAddress: flipper.options.address,261 },262 },263 ]);264 });265266 itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper}) => {267 const owner = await helper.eth.createAccountWithBalance(donor);268 const notOwner = await helper.eth.createAccountWithBalance(donor);269 const sponsor = await helper.eth.createAccountWithBalance(donor);270 const helpers = helper.ethNativeContract.contractHelpers(owner);271 const flipper = await helper.eth.deployFlipper(owner);272273 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;274 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();275 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});276 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;277 278 await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');279 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;280 });281282 itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper}) => {283 const owner = await helper.eth.createAccountWithBalance(donor);284 const sponsor = await helper.eth.createAccountWithBalance(donor);285 const caller = await helper.eth.createAccountWithBalance(donor);286 const helpers = helper.ethNativeContract.contractHelpers(owner);287 const flipper = await helper.eth.deployFlipper(owner);288289 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();290 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});291292 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});293 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});294295 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));296 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));297298 await flipper.methods.flip().send({from: caller});299 expect(await flipper.methods.getValue().call()).to.be.true;300301 302 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));303 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));304 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;305 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);306 });307308 itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper}) => {309 const owner = await helper.eth.createAccountWithBalance(donor);310 const caller = await helper.eth.createAccountWithBalance(donor);311 const helpers = helper.ethNativeContract.contractHelpers(owner);312 const flipper = await helper.eth.deployFlipper(owner);313314 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();315316 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});317 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});318319 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);320321 const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));322 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));323324 await flipper.methods.flip().send({from: caller});325 expect(await flipper.methods.getValue().call()).to.be.true;326327 328 const contractBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));329 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));330 expect(contractBalanceAfter < contractBalanceBefore).to.be.true;331 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);332 });333334 itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({helper}) => {335 const owner = await helper.eth.createAccountWithBalance(donor);336 const sponsor = await helper.eth.createAccountWithBalance(donor);337 const caller = helper.eth.createAccount();338 const helpers = helper.ethNativeContract.contractHelpers(owner);339 const flipper = await helper.eth.deployFlipper(owner);340341 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});342 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});343344 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});345 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});346347 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();348 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});349350 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));351 expect(sponsorBalanceBefore).to.be.not.equal('0');352353 await flipper.methods.flip().send({from: caller});354 expect(await flipper.methods.getValue().call()).to.be.true;355356 357 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));358 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;359 });360361 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}) => {362 const owner = await helper.eth.createAccountWithBalance(donor);363 const caller = await helper.eth.createAccount();364 const helpers = helper.ethNativeContract.contractHelpers(owner);365 const flipper = await helper.eth.deployFlipper(owner);366367 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});368 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});369370 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);371372 const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);373 expect(originalFlipperBalance).to.be.not.equal('0');374375 await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/Returned error: insufficient funds for gas \* price \+ value/);376 expect(await flipper.methods.getValue().call()).to.be.false;377378 379 380 const balanceAfter = await helper.balance.getEthereum(flipper.options.address);381 expect(balanceAfter).to.be.equal(originalFlipperBalance);382 });383384 itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper}) => {385 const owner = await helper.eth.createAccountWithBalance(donor);386 const sponsor = await helper.eth.createAccountWithBalance(donor);387 const caller = await helper.eth.createAccountWithBalance(donor);388 const helpers = helper.ethNativeContract.contractHelpers(owner);389 const flipper = await helper.eth.deployFlipper(owner);390391 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});392 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});393394 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});395 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});396397 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();398 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});399400 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));401 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));402403 await flipper.methods.flip().send({from: caller});404 expect(await flipper.methods.getValue().call()).to.be.true;405406 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));407 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));408 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;409 expect(callerBalanceAfter).to.be.equal(callerBalanceBefore);410 });411412 itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper}) => {413 const owner = await helper.eth.createAccountWithBalance(donor);414 const sponsor = await helper.eth.createAccountWithBalance(donor);415 const caller = await helper.eth.createAccountWithBalance(donor);416 const helpers = helper.ethNativeContract.contractHelpers(owner);417 const flipper = await helper.eth.deployFlipper(owner);418 419 const originalCallerBalance = await helper.balance.getEthereum(caller);420 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});421 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});422423 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});424 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});425426 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();427 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});428429 const originalFlipperBalance = await helper.balance.getEthereum(sponsor);430 expect(originalFlipperBalance).to.be.not.equal('0');431432 await flipper.methods.flip().send({from: caller});433 expect(await flipper.methods.getValue().call()).to.be.true;434 expect(await helper.balance.getEthereum(caller)).to.be.equal(originalCallerBalance);435436 const newFlipperBalance = await helper.balance.getEthereum(sponsor);437 expect(newFlipperBalance).to.be.not.equal(originalFlipperBalance);438439 await flipper.methods.flip().send({from: caller});440 441 expect(await helper.balance.getEthereum(sponsor)).to.be.equal(newFlipperBalance);442 expect(await helper.balance.getEthereum(caller)).to.be.not.equal(originalCallerBalance);443 });444445 446 itEth('Default rate limit equal 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.equal('7200');452 });453});454455describe('Sponsoring Fee Limit', () => {456 let donor: IKeyringPair;457 let alice: IKeyringPair;458 let testContract: CompiledContract;459460 async function compileTestContract(helper: EthUniqueHelper) {461 if (!testContract) {462 testContract = await helper.ethContract.compile(463 'TestContract',464 `465 // SPDX-License-Identifier: MIT466 pragma solidity ^0.8.0;467 468 contract TestContract {469 event Result(bool);470471 function test(uint32 cycles) public {472 uint256 counter = 0;473 while(true) {474 counter ++;475 if (counter > cycles){476 break;477 }478 }479 emit Result(true);480 }481 }482 `,483 );484 }485 return testContract;486 }487 488 async function deployTestContract(helper: EthUniqueHelper, owner: string) {489 const compiled = await compileTestContract(helper);490 return await helper.ethContract.deployByAbi(owner, compiled.abi, compiled.object);491 }492493 before(async () => {494 await usingEthPlaygrounds(async (helper, privateKey) => {495 donor = await privateKey({filename: __filename});496 [alice] = await helper.arrange.createAccounts([100n], donor);497 });498 });499500 itEth('Default fee limit', async ({helper}) => {501 const owner = await helper.eth.createAccountWithBalance(donor);502 const helpers = helper.ethNativeContract.contractHelpers(owner);503 const flipper = await helper.eth.deployFlipper(owner);504505 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equal('115792089237316195423570985008687907853269984665640564039457584007913129639935');506 });507508 itEth('Set fee limit', async ({helper}) => {509 const owner = await helper.eth.createAccountWithBalance(donor);510 const helpers = helper.ethNativeContract.contractHelpers(owner);511 const flipper = await helper.eth.deployFlipper(owner);512513 await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();514 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equal('100');515 });516517 itEth('Negative test - set fee limit by non-owner', async ({helper}) => {518 const owner = await helper.eth.createAccountWithBalance(donor);519 const stranger = await helper.eth.createAccountWithBalance(donor);520 const helpers = helper.ethNativeContract.contractHelpers(owner);521 const flipper = await helper.eth.deployFlipper(owner);522523 await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;524 });525526 itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper}) => {527 const owner = await helper.eth.createAccountWithBalance(donor);528 const sponsor = await helper.eth.createAccountWithBalance(donor);529 const user = await helper.eth.createAccountWithBalance(donor);530 const helpers = helper.ethNativeContract.contractHelpers(owner);531532 const testContract = await deployTestContract(helper, owner);533 534 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});535 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});536 537 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();538 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});539540 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());541542 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();543544 const originalUserBalance = await helper.balance.getEthereum(user);545 await testContract.methods.test(100).send({from: user, gas: 2_000_000});546 expect(await helper.balance.getEthereum(user)).to.be.equal(originalUserBalance);547548 await testContract.methods.test(100).send({from: user, gas: 2_100_000});549 expect(await helper.balance.getEthereum(user)).to.not.be.equal(originalUserBalance);550 });551552 itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper}) => {553 const owner = await helper.eth.createAccountWithBalance(donor);554 const sponsor = await helper.eth.createAccountWithBalance(donor);555 const helpers = helper.ethNativeContract.contractHelpers(owner);556557 const testContract = await deployTestContract(helper, owner);558 559 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});560 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});561 562 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();563 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});564565 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());566567 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();568569 const originalAliceBalance = await helper.balance.getSubstrate(alice.address);570571 await helper.eth.sendEVM(572 alice,573 testContract.options.address,574 testContract.methods.test(100).encodeABI(),575 '0',576 2_000_000,577 );578 579 expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);580 581 await helper.eth.sendEVM(582 alice,583 testContract.options.address,584 testContract.methods.test(100).encodeABI(),585 '0',586 2_100_000,587 );588 expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);589 });590});