difftreelog
fix contractSponsoring deplyFlipper, remove unneeded comments
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 contractHelpers,27 createEthAccountWithBalance,28} from './util/helpers';29import {itEth, expect} from '../eth/util/playgrounds';303132describe.only('Sponsoring EVM contracts', () => {33 itEth('Self sponsored can be set by the address that deployed the contract', async ({helper, privateKey}) => {34 const alice = privateKey('//Alice');3536 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);37 // const flipper = await deployFlipper(web3, owner);38 // const helpers = contractHelpers(web3, owner);39 const owner = await helper.eth.createAccountWithBalance(alice);40 const flipper = await helper.eth.deployFlipper(owner);41 const helpers = helper.ethNativeContract.contractHelpers(owner);4243 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;44 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;45 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;46 });4748 itEth('Set self sponsored events', async ({helper, privateKey}) => {49 const alice = privateKey('//Alice');5051 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);52 // const flipper = await deployFlipper(web3, owner);53 // const helpers = contractHelpers(web3, owner);54 const owner = await helper.eth.createAccountWithBalance(alice);55 const flipper = await helper.eth.deployFlipper(owner);56 const helpers = helper.ethNativeContract.contractHelpers(owner);57 58 const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();59 const ethEvents = normalizeEvents(result.events);60 expect(ethEvents).to.be.deep.equal([61 {62 address: flipper.options.address,63 event: 'ContractSponsorSet',64 args: {65 contractAddress: flipper.options.address,66 sponsor: flipper.options.address,67 },68 },69 {70 address: flipper.options.address,71 event: 'ContractSponsorshipConfirmed',72 args: {73 contractAddress: flipper.options.address,74 sponsor: flipper.options.address,75 },76 },77 ]);7879 // TODO use helper.getApi() from the sceduler PR80 await expectSubstrateEventsAtBlock(81 helper.api!,82 result.blockNumber,83 'evmContractHelpers',84 ['ContractSponsorSet','ContractSponsorshipConfirmed'],85 );86 });8788 itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper, privateKey}) => {89 const alice = privateKey('//Alice');9091 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);92 // const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);93 // const helpers = contractHelpers(web3, owner);94 // const flipper = await deployFlipper(web3, owner);95 const owner = await helper.eth.createAccountWithBalance(alice);96 const notOwner = await helper.eth.createAccountWithBalance(alice);97 const helpers = helper.ethNativeContract.contractHelpers(owner);98 const flipper = helper.ethNativeContract.contractHelpers(owner);99100 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;101 await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');102 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;103 });104105 itEth.only('Sponsoring can be set by the address that has deployed the contract', async ({helper, privateKey}) => {106 const alice = privateKey('//Alice');107108 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);109 // const flipper = await deployFlipper(web3, owner);110 // const helpers = contractHelpers(web3, owner);111 const owner = await helper.eth.createAccountWithBalance(alice);112 const helpers = helper.ethNativeContract.contractHelpers(owner);113 const flipper = await helper.eth.deployFlipper(owner);114115 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;116 await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;117 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;118 });119120 itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper, privateKey}) => {121 const alice = privateKey('//Alice');122123 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);124 // const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);125 // const flipper = await deployFlipper(web3, owner);126 // const helpers = contractHelpers(web3, owner);127 const owner = await helper.eth.createAccountWithBalance(alice);128 const notOwner = await helper.eth.createAccountWithBalance(alice);129 const helpers = helper.ethNativeContract.contractHelpers(owner);130 const flipper = helper.ethNativeContract.contractHelpers(owner);131132 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;133 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');134 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;135 });136 137 itEth('Sponsor can be set by the address that deployed the contract', async ({helper, privateKey}) => {138 const alice = privateKey('//Alice');139140 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);141 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);142 // const flipper = await deployFlipper(web3, owner);143 // const helpers = contractHelpers(web3, owner);144 const owner = await helper.eth.createAccountWithBalance(alice);145 const sponsor = await helper.eth.createAccountWithBalance(alice);146 const helpers = helper.ethNativeContract.contractHelpers(owner);147 const flipper = helper.ethNativeContract.contractHelpers(owner);148149 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;150 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;151 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;152 });153 154 itEth('Set sponsor event', async ({helper, privateKey}) => {155 const alice = privateKey('//Alice');156157 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);158 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);159 // const flipper = await deployFlipper(web3, owner);160 // const helpers = contractHelpers(web3, owner);161 const owner = await helper.eth.createAccountWithBalance(alice);162 const sponsor = await helper.eth.createAccountWithBalance(alice);163 const helpers = helper.ethNativeContract.contractHelpers(owner);164 const flipper = helper.ethNativeContract.contractHelpers(owner);165 166 const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();167 const events = normalizeEvents(result.events);168 expect(events).to.be.deep.equal([169 {170 address: flipper.options.address,171 event: 'ContractSponsorSet',172 args: {173 contractAddress: flipper.options.address,174 sponsor: sponsor,175 },176 },177 ]);178179 // TODO use helper.getApi() from the sceduler PR180 await expectSubstrateEventsAtBlock(181 helper.api!, 182 result.blockNumber,183 'evmContractHelpers',184 ['ContractSponsorSet'],185 );186 });187 188 itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper, privateKey}) => {189 const alice = privateKey('//Alice');190191 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);192 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);193 // const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);194 // const flipper = await deployFlipper(web3, owner);195 // const helpers = contractHelpers(web3, owner);196 const owner = await helper.eth.createAccountWithBalance(alice);197 const sponsor = await helper.eth.createAccountWithBalance(alice);198 const notOwner = await helper.eth.createAccountWithBalance(alice);199 const helpers = helper.ethNativeContract.contractHelpers(owner);200 const flipper = helper.ethNativeContract.contractHelpers(owner);201202 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;203 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');204 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;205 });206207 itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper, privateKey}) => {208 const alice = privateKey('//Alice');209210 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);211 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);212 // const flipper = await deployFlipper(web3, owner);213 // const helpers = contractHelpers(web3, owner);214 const owner = await helper.eth.createAccountWithBalance(alice);215 const sponsor = await helper.eth.createAccountWithBalance(alice);216 const helpers = helper.ethNativeContract.contractHelpers(owner);217 const flipper = helper.ethNativeContract.contractHelpers(owner);218219 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;220 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;221 await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;222 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;223 });224225 itEth('Confirm sponsorship event', async ({helper, privateKey}) => {226 const alice = privateKey('//Alice');227228 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);229 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);230 // const flipper = await deployFlipper(web3, owner);231 // const helpers = contractHelpers(web3, owner);232 const owner = await helper.eth.createAccountWithBalance(alice);233 const sponsor = await helper.eth.createAccountWithBalance(alice);234 const helpers = helper.ethNativeContract.contractHelpers(owner);235 const flipper = helper.ethNativeContract.contractHelpers(owner);236237 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;238 const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});239 const events = normalizeEvents(result.events);240 expect(events).to.be.deep.equal([241 {242 address: flipper.options.address,243 event: 'ContractSponsorshipConfirmed',244 args: {245 contractAddress: flipper.options.address,246 sponsor: sponsor,247 },248 },249 ]);250251 // TODO use helper.getApi() from the sceduler PR252 await expectSubstrateEventsAtBlock(253 helper.api!, 254 result.blockNumber,255 'evmContractHelpers',256 ['ContractSponsorshipConfirmed'],257 );258 });259260 itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper, privateKey}) => {261 const alice = privateKey('//Alice');262263 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);264 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);265 // const notSponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);266 // const flipper = await deployFlipper(web3, owner);267 // const helpers = contractHelpers(web3, owner);268 const owner = await helper.eth.createAccountWithBalance(alice);269 const sponsor = await helper.eth.createAccountWithBalance(alice);270 const notSponsor = await helper.eth.createAccountWithBalance(alice);271 const helpers = helper.ethNativeContract.contractHelpers(owner);272 const flipper = helper.ethNativeContract.contractHelpers(owner);273274 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;275 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;276 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');277 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;278 });279280 itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper, privateKey}) => {281 const alice = privateKey('//Alice');282283 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);284 // const notSponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);285 // const flipper = await deployFlipper(web3, owner);286 // const helpers = contractHelpers(web3, owner);287 const owner = await helper.eth.createAccountWithBalance(alice);288 const notSponsor = await helper.eth.createAccountWithBalance(alice);289 const helpers = helper.ethNativeContract.contractHelpers(owner);290 const flipper = helper.ethNativeContract.contractHelpers(owner);291292 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;293 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');294 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;295 });296297 itEth('Get self sponsored sponsor', async ({helper, privateKey}) => {298 const alice = privateKey('//Alice');299300 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);301 // const flipper = await deployFlipper(web3, owner);302 // const helpers = contractHelpers(web3, owner);303 const owner = await helper.eth.createAccountWithBalance(alice);304 const flipper = helper.ethNativeContract.contractHelpers(owner);305 const helpers = helper.ethNativeContract.contractHelpers(owner);306307 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();308 309 const result = await helpers.methods.sponsor(flipper.options.address).call();310311 expect(result[0]).to.be.eq(flipper.options.address);312 expect(result[1]).to.be.eq('0');313 });314315 itEth('Get confirmed sponsor', async ({helper, privateKey}) => {316 const alice = privateKey('//Alice');317318 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);319 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);320 // const flipper = await deployFlipper(web3, owner);321 // const helpers = contractHelpers(web3, owner);322 const owner = await helper.eth.createAccountWithBalance(alice);323 const sponsor = await helper.eth.createAccountWithBalance(alice);324 const helpers = helper.ethNativeContract.contractHelpers(owner);325 const flipper = helper.ethNativeContract.contractHelpers(owner);326327 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();328 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});329 330 const result = await helpers.methods.sponsor(flipper.options.address).call();331332 expect(result[0]).to.be.eq(sponsor);333 expect(result[1]).to.be.eq('0');334 });335336 itEth('Sponsor can be removed by the address that deployed the contract', async ({helper, privateKey}) => {337 const alice = privateKey('//Alice');338339 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);340 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);341 // const flipper = await deployFlipper(web3, owner);342 // const helpers = contractHelpers(web3, owner);343 const owner = await helper.eth.createAccountWithBalance(alice);344 const sponsor = await helper.eth.createAccountWithBalance(alice);345 const helpers = helper.ethNativeContract.contractHelpers(owner);346 const flipper = helper.ethNativeContract.contractHelpers(owner);347348 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;349 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();350 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});351 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;352 353 await helpers.methods.removeSponsor(flipper.options.address).send();354 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;355 });356357 itEth('Remove sponsor event', async ({helper, privateKey}) => {358 const alice = privateKey('//Alice');359360 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);361 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);362 // const flipper = await deployFlipper(web3, owner);363 // const helpers = contractHelpers(web3, owner);364 const owner = await helper.eth.createAccountWithBalance(alice);365 const sponsor = await helper.eth.createAccountWithBalance(alice);366 const helpers = helper.ethNativeContract.contractHelpers(owner);367 const flipper = helper.ethNativeContract.contractHelpers(owner);368369 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();370 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});371 372 const result = await helpers.methods.removeSponsor(flipper.options.address).send();373 const events = normalizeEvents(result.events);374 expect(events).to.be.deep.equal([375 {376 address: flipper.options.address,377 event: 'ContractSponsorRemoved',378 args: {379 contractAddress: flipper.options.address,380 },381 },382 ]);383384 // TODO use helper.getApi() from the sceduler PR385 await expectSubstrateEventsAtBlock(386 helper.api!, 387 result.blockNumber,388 'evmContractHelpers',389 ['ContractSponsorRemoved'],390 );391 });392393 itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper, privateKey}) => {394 const alice = privateKey('//Alice');395396 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);397 // const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);398 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);399 // const flipper = await deployFlipper(web3, owner);400 // const helpers = contractHelpers(web3, owner);401 const owner = await helper.eth.createAccountWithBalance(alice);402 const notOwner = await helper.eth.createAccountWithBalance(alice);403 const sponsor = await helper.eth.createAccountWithBalance(alice);404 const helpers = helper.ethNativeContract.contractHelpers(owner);405 const flipper = helper.ethNativeContract.contractHelpers(owner);406407 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;408 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();409 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});410 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;411 412 await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');413 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;414 });415416 itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper, privateKey}) => {417 const alice = privateKey('//Alice');418419 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);420 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);421 // const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);422 // const flipper = await deployFlipper(web3, owner);423 // const helpers = contractHelpers(web3, owner);424 const owner = await helper.eth.createAccountWithBalance(alice);425 const sponsor = await helper.eth.createAccountWithBalance(alice);426 const caller = await helper.eth.createAccountWithBalance(alice);427 const helpers = helper.ethNativeContract.contractHelpers(owner);428 const flipper = helper.ethNativeContract.contractHelpers(owner);429430 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();431 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});432433 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});434 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});435436 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));437 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));438439 await flipper.methods.flip().send({from: caller});440 expect(await flipper.methods.getValue().call()).to.be.true;441442 // Balance should be taken from sponsor instead of caller443 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));444 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));445 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;446 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);447 });448449 itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper, privateKey}) => {450 const alice = privateKey('//Alice');451452 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);453 // const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);454 // const flipper = await deployFlipper(web3, owner);455 // const helpers = contractHelpers(web3, owner);456 const owner = await helper.eth.createAccountWithBalance(alice);457 const caller = await helper.eth.createAccountWithBalance(alice);458 const helpers = helper.ethNativeContract.contractHelpers(owner);459 const flipper = helper.ethNativeContract.contractHelpers(owner);460461 await helpers.methods.selfSponsoredEnable(flipper.options.address).send();462463 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});464 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});465466 // await transferBalanceToEth(api, alice, flipper.options.address);467 await helper.eth.transferBalanceFromSubstrate(alice, flipper.options.address);468469 const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));470 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));471472 await flipper.methods.flip().send({from: caller});473 expect(await flipper.methods.getValue().call()).to.be.true;474475 // Balance should be taken from sponsor instead of caller476 const contractBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));477 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));478 expect(contractBalanceAfter < contractBalanceBefore).to.be.true;479 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);480 });481482 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}) => {483 const alice = privateKey('//Alice');484485 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);486 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);487 // const caller = createEthAccount(web3);488 // const flipper = await deployFlipper(web3, owner);489 // const helpers = contractHelpers(web3, owner);490 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 = helper.ethNativeContract.contractHelpers(owner);495496 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});497 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});498499 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});500 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});501502 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();503 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});504505 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));506 expect(sponsorBalanceBefore).to.be.not.equal('0');507508 await flipper.methods.flip().send({from: caller});509 expect(await flipper.methods.getValue().call()).to.be.true;510511 // Balance should be taken from flipper instead of caller512 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));513 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;514 });515516 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}) => {517 const alice = privateKey('//Alice');518519 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);520 // const caller = createEthAccount(web3);521 // const flipper = await deployFlipper(web3, owner);522 // const helpers = contractHelpers(web3, owner);523 const owner = await helper.eth.createAccountWithBalance(alice);524 const caller = await helper.eth.createAccountWithBalance(alice);525 const helpers = helper.ethNativeContract.contractHelpers(owner);526 const flipper = helper.ethNativeContract.contractHelpers(owner);527528 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});529 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});530531 // await transferBalanceToEth(api, alice, flipper.options.address);532 await helper.eth.transferBalanceFromSubstrate(alice, flipper.options.address);533534 const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address); // await web3.eth.getBalance(flipper.options.address);535 expect(originalFlipperBalance).to.be.not.equal('0');536537 await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/InvalidTransaction::Payment/);538 expect(await flipper.methods.getValue().call()).to.be.false;539540 // Balance should be taken from flipper instead of caller541 // FIXME the comment is wrong! What check should be here?542 const balanceAfter = await helper.balance.getEthereum(flipper.options.address); // await web3.eth.getBalance(flipper.options.address);543 expect(balanceAfter).to.be.equals(originalFlipperBalance);544 });545546 itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper, privateKey}) => {547 const alice = privateKey('//Alice');548549 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);550 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);551 // const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);552 // const flipper = await deployFlipper(web3, owner);553 // const helpers = contractHelpers(web3, owner);554 const owner = await helper.eth.createAccountWithBalance(alice);555 const sponsor = await helper.eth.createAccountWithBalance(alice);556 const caller = await helper.eth.createAccountWithBalance(alice);557 const helpers = helper.ethNativeContract.contractHelpers(owner);558 const flipper = helper.ethNativeContract.contractHelpers(owner);559560 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});561 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});562563 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});564 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});565566 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();567 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});568569 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));570 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));571572 await flipper.methods.flip().send({from: caller});573 expect(await flipper.methods.getValue().call()).to.be.true;574575 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));576 const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));577 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;578 expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);579 });580581 itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper, privateKey}) => {582 const alice = privateKey('//Alice');583584 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);585 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);586 // const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);587 // const flipper = await deployFlipper(web3, owner);588 // const helpers = contractHelpers(web3, owner);589 const owner = await helper.eth.createAccountWithBalance(alice);590 const sponsor = await helper.eth.createAccountWithBalance(alice);591 const caller = await helper.eth.createAccountWithBalance(alice);592 const helpers = helper.ethNativeContract.contractHelpers(owner);593 const flipper = helper.ethNativeContract.contractHelpers(owner);594 595 const originalCallerBalance = await helper.balance.getEthereum(caller); // await web3.eth.getBalance(caller);596 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});597 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});598599 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});600 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});601602 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();603 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});604605 const originalFlipperBalance = await helper.balance.getEthereum(sponsor); // await web3.eth.getBalance(sponsor);606 expect(originalFlipperBalance).to.be.not.equal('0');607608 await flipper.methods.flip().send({from: caller});609 expect(await flipper.methods.getValue().call()).to.be.true;610 expect(await helper.balance.getEthereum(caller)).to.be.equals(originalCallerBalance);611612 const newFlipperBalance = await helper.balance.getEthereum(sponsor);613 expect(newFlipperBalance).to.be.not.equals(originalFlipperBalance);614615 await flipper.methods.flip().send({from: caller});616 expect(await helper.balance.getEthereum(sponsor)).to.be.equal(newFlipperBalance);617 expect(await helper.balance.getEthereum(caller)).to.be.not.equals(originalCallerBalance);618 });619620 // TODO: Find a way to calculate default rate limit621 itEth('Default rate limit equals 7200', async ({helper, privateKey}) => {622 const alice = privateKey('//Alice');623624 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);625 // const flipper = await deployFlipper(web3, owner);626 // const helpers = contractHelpers(web3, owner);627 const owner = await helper.eth.createAccountWithBalance(alice);628 const helpers = helper.ethNativeContract.contractHelpers(owner);629 const flipper = helper.ethNativeContract.contractHelpers(owner);630631 expect(await helpers.methods.sponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');632 });633});634635describe('Sponsoring Fee Limit', () => {636637 let testContract: CompiledContract;638 639 function compileTestContract() {640 if (!testContract) {641 const input = {642 language: 'Solidity',643 sources: {644 ['TestContract.sol']: {645 content:646 `647 // SPDX-License-Identifier: MIT648 pragma solidity ^0.8.0;649 650 contract TestContract {651 event Result(bool);652653 function test(uint32 cycles) public {654 uint256 counter = 0;655 while(true) {656 counter ++;657 if (counter > cycles){658 break;659 }660 }661 emit Result(true);662 }663 }664 `,665 },666 },667 settings: {668 outputSelection: {669 '*': {670 '*': ['*'],671 },672 },673 },674 };675 const json = JSON.parse(solc.compile(JSON.stringify(input)));676 const out = json.contracts['TestContract.sol']['TestContract'];677 678 testContract = {679 abi: out.abi,680 object: '0x' + out.evm.bytecode.object,681 };682 }683 return testContract;684 }685 686 async function deployTestContract(web3: Web3, owner: string) {687 const compiled = compileTestContract();688 const testContract = new web3.eth.Contract(compiled.abi, undefined, {689 data: compiled.object,690 from: owner,691 ...GAS_ARGS,692 });693 return await testContract.deploy({data: compiled.object}).send({from: owner});694 }695696 itEth('Default fee limit', async ({helper, privateKey}) => {697 const alice = privateKey('//Alice');698699 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);700 // const flipper = await deployFlipper(web3, owner);701 // const helpers = contractHelpers(web3, owner);702 const owner = await helper.eth.createAccountWithBalance(alice);703 const helpers = helper.ethNativeContract.contractHelpers(owner);704 const flipper = helper.ethNativeContract.contractHelpers(owner);705706 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('115792089237316195423570985008687907853269984665640564039457584007913129639935');707 });708709 itEth('Set fee limit', async ({helper, privateKey}) => {710 const alice = privateKey('//Alice');711712 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);713 // const flipper = await deployFlipper(web3, owner);714 // const helpers = contractHelpers(web3, owner);715 const owner = await helper.eth.createAccountWithBalance(alice);716 const helpers = helper.ethNativeContract.contractHelpers(owner);717 const flipper = helper.ethNativeContract.contractHelpers(owner);718719 await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();720 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('100');721 });722723 itEth('Negative test - set fee limit by non-owner', async ({helper, privateKey}) => {724 const alice = privateKey('//Alice');725726 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);727 // const stranger = await createEthAccountWithBalance(api, web3, privateKeyWrapper);728 // const flipper = await deployFlipper(web3, owner);729 // const helpers = contractHelpers(web3, owner);730 const owner = await helper.eth.createAccountWithBalance(alice);731 const stranger = await helper.eth.createAccountWithBalance(alice);732 const helpers = helper.ethNativeContract.contractHelpers(owner);733 const flipper = helper.ethNativeContract.contractHelpers(owner);734735 await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;736 });737738 itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {739 const alice = privateKey('//Alice');740741 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);742 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);743 // const user = await createEthAccountWithBalance(api, web3, privateKeyWrapper);744 // const helpers = contractHelpers(web3, owner);745 const owner = await helper.eth.createAccountWithBalance(alice);746 const sponsor = await helper.eth.createAccountWithBalance(alice);747 const user = await helper.eth.createAccountWithBalance(alice);748 const helpers = helper.ethNativeContract.contractHelpers(owner);749750 const testContract = await deployTestContract(helper.getWeb3(), owner);751 752 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});753 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});754 755 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();756 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});757758 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());759760 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();761762 const originalUserBalance = await helper.balance.getEthereum(user); // await web3.eth.getBalance(user);763 await testContract.methods.test(100).send({from: user, gas: 2_000_000});764 expect(await helper.balance.getEthereum(user)).to.be.equal(originalUserBalance);765766 await testContract.methods.test(100).send({from: user, gas: 2_100_000});767 expect(await helper.balance.getEthereum(user)).to.not.be.equal(originalUserBalance);768 });769770 itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {771 const alice = privateKey('//Alice');772773 // const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);774 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);775 // const helpers = contractHelpers(web3, owner);776 const owner = await helper.eth.createAccountWithBalance(alice);777 const sponsor = await helper.eth.createAccountWithBalance(alice);778 const helpers = helper.ethNativeContract.contractHelpers(owner);779780 const testContract = await deployTestContract(helper.getWeb3(), owner);781 782 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});783 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});784 785 await helpers.methods.setSponsor(testContract.options.address, sponsor).send();786 await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});787788 const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());789790 await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();791792 const originalAliceBalance = await helper.balance.getSubstrate(alice.address); // (await api.query.system.account(alice.address)).data.free.toBigInt();793794 // await submitTransactionAsync(795 // alice,796 // api.tx.evm.call(797 // subToEth(alice.address),798 // testContract.options.address,799 // testContract.methods.test(100).encodeABI(),800 // Uint8Array.from([]),801 // 2_000_000n,802 // gasPrice,803 // null,804 // null,805 // [],806 // ),807 // );808 await helper.eth.sendEVM(809 alice,810 testContract.options.address,811 testContract.methods.test().encodeABI(),812 '100',813 );814 // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.be.equal(originalAliceBalance);815 expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);816 817 // await submitTransactionAsync(818 // alice,819 // api.tx.evm.call(820 // subToEth(alice.address),821 // testContract.options.address,822 // testContract.methods.test(100).encodeABI(),823 // Uint8Array.from([]),824 // 2_100_000n,825 // gasPrice,826 // null,827 // null,828 // [],829 // ),830 // );831 await helper.eth.sendEVM(832 alice,833 testContract.options.address,834 testContract.methods.test().encodeABI(),835 '100',836 );837 // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.not.be.equal(originalAliceBalance);838 expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);839 });840});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 * 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});