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;25 let nominal: bigint;2627 before(async () => {28 await usingPlaygrounds(async (helper, privateKey) => {29 donor = await privateKey({filename: __filename});30 nominal = helper.balance.getOneTokenNominal();31 });32 });3334 itEth('Self sponsoring can be set by the address that deployed the contract', async ({helper}) => {35 const owner = await helper.eth.createAccountWithBalance(donor);36 const flipper = await helper.eth.deployFlipper(owner);37 const helpers = helper.ethNativeContract.contractHelpers(owner);3839 40 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;41 const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send({from: owner});42 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;4344 45 const actualSponsor = await helpers.methods.sponsor(flipper.options.address).call();46 expect(actualSponsor.eth).to.eq(flipper.options.address);47 expect(actualSponsor.sub).to.eq('0');4849 50 const ethEvents = helper.eth.helper.eth.normalizeEvents(result.events);51 expect(ethEvents).to.be.deep.equal([52 {53 address: flipper.options.address,54 event: 'ContractSponsorSet',55 args: {56 contractAddress: flipper.options.address,57 sponsor: flipper.options.address,58 },59 },60 {61 address: flipper.options.address,62 event: 'ContractSponsorshipConfirmed',63 args: {64 contractAddress: flipper.options.address,65 sponsor: flipper.options.address,66 },67 },68 ]);69 });7071 itEth('Self sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {72 const owner = await helper.eth.createAccountWithBalance(donor);73 const notOwner = await helper.eth.createAccountWithBalance(donor);74 const helpers = helper.ethNativeContract.contractHelpers(owner);75 const flipper = await helper.eth.deployFlipper(owner);7677 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;78 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');79 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;80 });8182 itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper}) => {83 const owner = await helper.eth.createAccountWithBalance(donor);84 const helpers = helper.ethNativeContract.contractHelpers(owner);85 const flipper = await helper.eth.deployFlipper(owner);8687 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;88 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});89 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;90 });9192 itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {93 const owner = await helper.eth.createAccountWithBalance(donor);94 const notOwner = await helper.eth.createAccountWithBalance(donor);95 const helpers = helper.ethNativeContract.contractHelpers(owner);96 const flipper = await helper.eth.deployFlipper(owner);9798 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;99 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');100 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;101 });102 103 itEth('Sponsor can be set by the address that deployed the contract', async ({helper}) => {104 const owner = await helper.eth.createAccountWithBalance(donor);105 const sponsor = await helper.eth.createAccountWithBalance(donor);106 const helpers = helper.ethNativeContract.contractHelpers(owner);107 const flipper = await helper.eth.deployFlipper(owner);108109 110 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;111 const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();112 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;113114 115 const events = helper.eth.normalizeEvents(result.events);116 expect(events).to.be.deep.equal([117 {118 address: flipper.options.address,119 event: 'ContractSponsorSet',120 args: {121 contractAddress: flipper.options.address,122 sponsor: sponsor,123 },124 },125 ]);126 });127 128 itEth('Sponsor cannot be set by the address that did not deployed the contract', async ({helper}) => {129 const owner = await helper.eth.createAccountWithBalance(donor);130 const sponsor = await helper.eth.createAccountWithBalance(donor);131 const notOwner = await helper.eth.createAccountWithBalance(donor);132 const helpers = helper.ethNativeContract.contractHelpers(owner);133 const flipper = await helper.eth.deployFlipper(owner);134135 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;136 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');137 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;138 });139140 itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper}) => {141 const owner = await helper.eth.createAccountWithBalance(donor);142 const sponsor = await helper.eth.createAccountWithBalance(donor);143 const helpers = helper.ethNativeContract.contractHelpers(owner);144 const flipper = await helper.eth.deployFlipper(owner);145146 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;147 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();148149 150 const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});151 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;152153 154 const actualSponsor = await helpers.methods.sponsor(flipper.options.address).call();155 expect(actualSponsor.eth).to.eq(sponsor);156 expect(actualSponsor.sub).to.eq('0');157158 159 const events = helper.eth.normalizeEvents(result.events);160 expect(events).to.be.deep.equal([161 {162 address: flipper.options.address,163 event: 'ContractSponsorshipConfirmed',164 args: {165 contractAddress: flipper.options.address,166 sponsor: sponsor,167 },168 },169 ]);170 });171172 itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper}) => {173 const owner = await helper.eth.createAccountWithBalance(donor);174 const sponsor = await helper.eth.createAccountWithBalance(donor);175 const notSponsor = await helper.eth.createAccountWithBalance(donor);176 const helpers = helper.ethNativeContract.contractHelpers(owner);177 const flipper = await helper.eth.deployFlipper(owner);178179 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;180 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;181 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');182 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;183 });184185 itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper}) => {186 const owner = await helper.eth.createAccountWithBalance(donor);187 const notSponsor = await helper.eth.createAccountWithBalance(donor);188 const helpers = helper.ethNativeContract.contractHelpers(owner);189 const flipper = await helper.eth.deployFlipper(owner);190191 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;192 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');193 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;194 });195196 itEth('Sponsor can be removed by the address that deployed the contract', async ({helper}) => {197 const owner = await helper.eth.createAccountWithBalance(donor);198 const sponsor = await helper.eth.createAccountWithBalance(donor);199 const helpers = helper.ethNativeContract.contractHelpers(owner);200 const flipper = await helper.eth.deployFlipper(owner);201202 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;203 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();204 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});205 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;206 207 const result = await helpers.methods.removeSponsor(flipper.options.address).send();208 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;209210 211 const events = helper.eth.normalizeEvents(result.events);212 expect(events).to.be.deep.equal([213 {214 address: flipper.options.address,215 event: 'ContractSponsorRemoved',216 args: {217 contractAddress: flipper.options.address,218 },219 },220 ]);221222 223 224 225 226 });227228 itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper}) => {229 const owner = await helper.eth.createAccountWithBalance(donor);230 const notOwner = 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 expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');241 await expect(helpers.methods.removeSponsor(flipper.options.address).send({from: notOwner})).to.be.rejected;242 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;243 });244245 itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper}) => {246 const owner = await helper.eth.createAccountWithBalance(donor);247 const sponsor = await helper.eth.createAccountWithBalance(donor);248 const caller = await helper.eth.createAccountWithBalance(donor);249 const helpers = helper.ethNativeContract.contractHelpers(owner);250 const flipper = await helper.eth.deployFlipper(owner);251252 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();253 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});254255 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});256 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});257258 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));259 const callerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(caller));260261 await flipper.methods.flip().send({from: caller});262 expect(await flipper.methods.getValue().call()).to.be.true;263264 265 const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));266 const callerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(caller));267 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;268 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);269 });270271 itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper}) => {272 const owner = await helper.eth.createAccountWithBalance(donor);273 const caller = await helper.eth.createAccountWithBalance(donor);274 const helpers = helper.ethNativeContract.contractHelpers(owner);275 const flipper = await helper.eth.deployFlipper(owner);276277 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();278279 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});280 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});281282 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);283284 const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));285 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));286287 await flipper.methods.flip().send({from: caller});288 expect(await flipper.methods.getValue().call()).to.be.true;289290 291 const contractBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));292 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));293 expect(contractBalanceAfter < contractBalanceBefore).to.be.true;294 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);295 });296297 [298 {balance: 0n, label: '0'},299 {balance: 10n, label: '10'},300 ].map(testCase => {301 itEth(`Allow-listed address that has ${testCase.label} UNQ can call a contract. Sponsor balance should decrease`, async ({helper}) => {302 const owner = await helper.eth.createAccountWithBalance(donor);303 const sponsor = await helper.eth.createAccountWithBalance(donor);304 const caller = helper.eth.createAccount();305 await helper.eth.transferBalanceFromSubstrate(donor, caller, testCase.balance);306 const helpers = helper.ethNativeContract.contractHelpers(owner);307 const flipper = await helper.eth.deployFlipper(owner);308 309 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});310 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});311 312 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});313 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});314 315 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();316 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});317 318 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));319 expect(sponsorBalanceBefore > 0n).to.be.true;320 321 await flipper.methods.flip().send({from: caller});322 expect(await flipper.methods.getValue().call()).to.be.true;323 324 325 const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));326 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;327 328 const callerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(caller));329 expect(callerBalanceAfter).to.eq(testCase.balance * nominal);330 });331 });332333 itEth('Non-allow-listed address can call a contract. Sponsor balance should not decrease', async ({helper}) => {334 const owner = await helper.eth.createAccountWithBalance(donor);335 const caller = helper.eth.createAccount();336 const contractHelpers = helper.ethNativeContract.contractHelpers(owner);337338 339 const flipper = await helper.eth.deployFlipper(owner);340 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);341 expect(await flipper.methods.getValue().call()).to.be.false;342 343 const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);344 expect(originalFlipperBalance > 0n).to.be.true;345346 347 await contractHelpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});348 await contractHelpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});349 await contractHelpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});350351 352 await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/Returned error: insufficient funds for gas \* price \+ value/);353 expect(await flipper.methods.getValue().call()).to.be.false;354355 356 const balanceAfter = await helper.balance.getEthereum(flipper.options.address);357 expect(balanceAfter).to.be.equal(originalFlipperBalance);358 });359360 itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper}) => {361 const owner = await helper.eth.createAccountWithBalance(donor);362 const sponsor = await helper.eth.createAccountWithBalance(donor);363 const caller = await helper.eth.createAccountWithBalance(donor);364 const helpers = helper.ethNativeContract.contractHelpers(owner);365 const flipper = await helper.eth.deployFlipper(owner);366 367 const originalCallerBalance = await helper.balance.getEthereum(caller);368 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});369 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});370371 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});372 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});373374 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();375 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});376377 const originalFlipperBalance = await helper.balance.getEthereum(sponsor);378 expect(originalFlipperBalance > 0n).to.be.true;379380 await flipper.methods.flip().send({from: caller});381 expect(await flipper.methods.getValue().call()).to.be.true;382 expect(await helper.balance.getEthereum(caller)).to.be.equal(originalCallerBalance);383384 const newFlipperBalance = await helper.balance.getEthereum(sponsor);385 expect(newFlipperBalance).to.be.not.equal(originalFlipperBalance);386387 await flipper.methods.flip().send({from: caller});388 389 expect(await helper.balance.getEthereum(sponsor)).to.be.equal(newFlipperBalance);390 expect(await helper.balance.getEthereum(caller)).to.be.not.equal(originalCallerBalance);391 });392393 394 itEth('Default rate limit equal 7200', async ({helper}) => {395 const owner = await helper.eth.createAccountWithBalance(donor);396 const helpers = helper.ethNativeContract.contractHelpers(owner);397 const flipper = await helper.eth.deployFlipper(owner);398399 expect(await helpers.methods.sponsoringRateLimit(flipper.options.address).call()).to.be.equal('7200');400 });401});402403describe('Sponsoring Fee Limit', () => {404 let donor: IKeyringPair;405 let alice: IKeyringPair;406 let testContract: CompiledContract;407408 async function compileTestContract(helper: EthUniqueHelper) {409 if (!testContract) {410 testContract = await helper.ethContract.compile(411 'TestContract',412 `413 // SPDX-License-Identifier: MIT414 pragma solidity ^0.8.0;415 416 contract TestContract {417 event Result(bool);418419 function test(uint32 cycles) public {420 uint256 counter = 0;421 while(true) {422 counter ++;423 if (counter > cycles){424 break;425 }426 }427 emit Result(true);428 }429 }430 `,431 );432 }433 return testContract;434 }435 436 async function deployTestContract(helper: EthUniqueHelper, owner: string) {437 const compiled = await compileTestContract(helper);438 return await helper.ethContract.deployByAbi(owner, compiled.abi, compiled.object);439 }440441 before(async () => {442 await usingEthPlaygrounds(async (helper, privateKey) => {443 donor = await privateKey({filename: __filename});444 [alice] = await helper.arrange.createAccounts([100n], donor);445 });446 });447448 itEth('Default fee limit', async ({helper}) => {449 const owner = await helper.eth.createAccountWithBalance(donor);450 const helpers = helper.ethNativeContract.contractHelpers(owner);451 const flipper = await helper.eth.deployFlipper(owner);452453 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equal('115792089237316195423570985008687907853269984665640564039457584007913129639935');454 });455456 itEth('Set fee limit', async ({helper}) => {457 const owner = await helper.eth.createAccountWithBalance(donor);458 const helpers = helper.ethNativeContract.contractHelpers(owner);459 const flipper = await helper.eth.deployFlipper(owner);460461 await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();462 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equal('100');463 });464465 itEth('Negative test - set fee limit by non-owner', async ({helper}) => {466 const owner = await helper.eth.createAccountWithBalance(donor);467 const stranger = await helper.eth.createAccountWithBalance(donor);468 const helpers = helper.ethNativeContract.contractHelpers(owner);469 const flipper = await helper.eth.deployFlipper(owner);470471 await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;472 });473474 itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper}) => {475 const owner = await helper.eth.createAccountWithBalance(donor);476 const sponsor = await helper.eth.createAccountWithBalance(donor);477 const user = await helper.eth.createAccountWithBalance(donor);478 const helpers = helper.ethNativeContract.contractHelpers(owner);479480 const testContract = await deployTestContract(helper, owner);481 482 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});483 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});484 485 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();486 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});487488 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());489490 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();491492 const originalUserBalance = await helper.balance.getEthereum(user);493 await testContract.methods.test(100).send({from: user, gas: 2_000_000});494 expect(await helper.balance.getEthereum(user)).to.be.equal(originalUserBalance);495496 await testContract.methods.test(100).send({from: user, gas: 2_100_000});497 expect(await helper.balance.getEthereum(user)).to.not.be.equal(originalUserBalance);498 });499500 itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper}) => {501 const owner = await helper.eth.createAccountWithBalance(donor);502 const sponsor = await helper.eth.createAccountWithBalance(donor);503 const helpers = helper.ethNativeContract.contractHelpers(owner);504505 const testContract = await deployTestContract(helper, owner);506 507 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});508 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});509 510 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();511 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});512513 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());514515 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();516517 const originalAliceBalance = await helper.balance.getSubstrate(alice.address);518519 await helper.eth.sendEVM(520 alice,521 testContract.options.address,522 testContract.methods.test(100).encodeABI(),523 '0',524 2_000_000,525 );526 527 expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);528 529 await helper.eth.sendEVM(530 alice,531 testContract.options.address,532 testContract.methods.test(100).encodeABI(),533 '0',534 2_100_000,535 );536 expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);537 });538});