difftreelog
Tests: fix eth contractsSponsoing
in: master
1 file changed
tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.161617import {IKeyringPair} from '@polkadot/types/types';17import * as solc from 'solc';18import * as solc from 'solc';18import {expectSubstrateEventsAtBlock} from '../util/helpers';19import {EthUniqueHelper} from './util/playgrounds/unique.dev';20import {itEth, expect, SponsoringMode, usingEthPlaygrounds} from '../eth/util/playgrounds';19import Web3 from 'web3';21import {usingPlaygrounds} from '../util/playgrounds';22import {CompiledContract} from './util/playgrounds/types';202321import {24describe('Sponsoring EVM contracts', () => {22 SponsoringMode,23 normalizeEvents,24 CompiledContract,25 GAS_ARGS,25 let donor: IKeyringPair;26} from './util/helpers';27import {itEth, expect} from '../eth/util/playgrounds';282627 before(async () => {28 await usingPlaygrounds(async (_helper, privateKey) => {29 donor = privateKey('//Alice');30 });31 });293230describe('Sponsoring EVM contracts', () => {31 itEth('Self sponsored can be set by the address that deployed the contract', async ({helper, privateKey}) => {33 itEth('Self sponsored can be set by the address that deployed the contract', async ({helper, privateKey}) => {32 const alice = privateKey('//Alice');34 const owner = await helper.eth.createAccountWithBalance(donor);3334 const owner = await helper.eth.createAccountWithBalance(alice);35 const flipper = await helper.eth.deployFlipper(owner);35 const flipper = await helper.eth.deployFlipper(owner);36 const helpers = helper.ethNativeContract.contractHelpers(owner);36 const helpers = helper.ethNativeContract.contractHelpers(owner);373740 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;40 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;41 });41 });424243 itEth('Set self sponsored events', async ({helper, privateKey}) => {43 itEth('Set self sponsored events', async ({helper}) => {44 const alice = privateKey('//Alice');4546 const owner = await helper.eth.createAccountWithBalance(alice);44 const owner = await helper.eth.createAccountWithBalance(donor);47 const flipper = await helper.eth.deployFlipper(owner);45 const flipper = await helper.eth.deployFlipper(owner);48 const helpers = helper.ethNativeContract.contractHelpers(owner);46 const helpers = helper.ethNativeContract.contractHelpers(owner);49 47 50 const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();48 const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();51 const ethEvents = normalizeEvents(result.events);49 const ethEvents = helper.eth.helper.eth.normalizeEvents(result.events);52 expect(ethEvents).to.be.deep.equal([50 expect(ethEvents).to.be.deep.equal([53 {51 {54 address: flipper.options.address,52 address: flipper.options.address,67 },65 },68 },66 },69 ]);67 ]);7071 // TODO use helper.getApi() from the sceduler PR72 await expectSubstrateEventsAtBlock(73 helper.api!,74 result.blockNumber,75 'evmContractHelpers',76 ['ContractSponsorSet','ContractSponsorshipConfirmed'],77 );78 });68 });796980 itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper, privateKey}) => {70 itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper}) => {81 const alice = privateKey('//Alice');8283 const owner = await helper.eth.createAccountWithBalance(alice);71 const owner = await helper.eth.createAccountWithBalance(donor);84 const notOwner = await helper.eth.createAccountWithBalance(alice);72 const notOwner = await helper.eth.createAccountWithBalance(donor);85 const helpers = helper.ethNativeContract.contractHelpers(owner);73 const helpers = helper.ethNativeContract.contractHelpers(owner);86 const flipper = await helper.eth.deployFlipper(owner);74 const flipper = await helper.eth.deployFlipper(owner);877590 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;78 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;91 });79 });928093 itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper, privateKey}) => {81 itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper}) => {94 const alice = privateKey('//Alice');9596 const owner = await helper.eth.createAccountWithBalance(alice);82 const owner = await helper.eth.createAccountWithBalance(donor);97 const helpers = helper.ethNativeContract.contractHelpers(owner);83 const helpers = helper.ethNativeContract.contractHelpers(owner);98 const flipper = await helper.eth.deployFlipper(owner);84 const flipper = await helper.eth.deployFlipper(owner);9985102 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;88 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;103 });89 });10490105 itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper, privateKey}) => {91 itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {106 const alice = privateKey('//Alice');107108 const owner = await helper.eth.createAccountWithBalance(alice);92 const owner = await helper.eth.createAccountWithBalance(donor);109 const notOwner = await helper.eth.createAccountWithBalance(alice);93 const notOwner = await helper.eth.createAccountWithBalance(donor);110 const helpers = helper.ethNativeContract.contractHelpers(owner);94 const helpers = helper.ethNativeContract.contractHelpers(owner);111 const flipper = await helper.eth.deployFlipper(owner);95 const flipper = await helper.eth.deployFlipper(owner);11296115 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;99 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;116 });100 });117 101 118 itEth('Sponsor can be set by the address that deployed the contract', async ({helper, privateKey}) => {102 itEth('Sponsor can be set by the address that deployed the contract', async ({helper}) => {119 const alice = privateKey('//Alice');120121 const owner = await helper.eth.createAccountWithBalance(alice);103 const owner = await helper.eth.createAccountWithBalance(donor);122 const sponsor = await helper.eth.createAccountWithBalance(alice);104 const sponsor = await helper.eth.createAccountWithBalance(donor);123 const helpers = helper.ethNativeContract.contractHelpers(owner);105 const helpers = helper.ethNativeContract.contractHelpers(owner);124 const flipper = await helper.eth.deployFlipper(owner);106 const flipper = await helper.eth.deployFlipper(owner);125107128 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;110 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;129 });111 });130 112 131 itEth('Set sponsor event', async ({helper, privateKey}) => {113 itEth('Set sponsor event', async ({helper}) => {132 const alice = privateKey('//Alice');133134 const owner = await helper.eth.createAccountWithBalance(alice);114 const owner = await helper.eth.createAccountWithBalance(donor);135 const sponsor = await helper.eth.createAccountWithBalance(alice);115 const sponsor = await helper.eth.createAccountWithBalance(donor);136 const helpers = helper.ethNativeContract.contractHelpers(owner);116 const helpers = helper.ethNativeContract.contractHelpers(owner);137 const flipper = await helper.eth.deployFlipper(owner);117 const flipper = await helper.eth.deployFlipper(owner);138 118 139 const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();119 const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();140 const events = normalizeEvents(result.events);120 const events = helper.eth.normalizeEvents(result.events);141 expect(events).to.be.deep.equal([121 expect(events).to.be.deep.equal([142 {122 {143 address: flipper.options.address,123 address: flipper.options.address,148 },128 },149 },129 },150 ]);130 ]);151152 // TODO use helper.getApi() from the sceduler PR153 await expectSubstrateEventsAtBlock(154 helper.api!, 155 result.blockNumber,156 'evmContractHelpers',157 ['ContractSponsorSet'],158 );159 });131 });160 132 161 itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper, privateKey}) => {133 itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper}) => {162 const alice = privateKey('//Alice');163164 const owner = await helper.eth.createAccountWithBalance(alice);134 const owner = await helper.eth.createAccountWithBalance(donor);165 const sponsor = await helper.eth.createAccountWithBalance(alice);135 const sponsor = await helper.eth.createAccountWithBalance(donor);166 const notOwner = await helper.eth.createAccountWithBalance(alice);136 const notOwner = await helper.eth.createAccountWithBalance(donor);167 const helpers = helper.ethNativeContract.contractHelpers(owner);137 const helpers = helper.ethNativeContract.contractHelpers(owner);168 const flipper = await helper.eth.deployFlipper(owner);138 const flipper = await helper.eth.deployFlipper(owner);169139172 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;142 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;173 });143 });174144175 itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper, privateKey}) => {145 itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper}) => {176 const alice = privateKey('//Alice');177178 const owner = await helper.eth.createAccountWithBalance(alice);146 const owner = await helper.eth.createAccountWithBalance(donor);179 const sponsor = await helper.eth.createAccountWithBalance(alice);147 const sponsor = await helper.eth.createAccountWithBalance(donor);180 const helpers = helper.ethNativeContract.contractHelpers(owner);148 const helpers = helper.ethNativeContract.contractHelpers(owner);181 const flipper = await helper.eth.deployFlipper(owner);149 const flipper = await helper.eth.deployFlipper(owner);182150186 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;154 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;187 });155 });188156189 itEth('Confirm sponsorship event', async ({helper, privateKey}) => {157 itEth('Confirm sponsorship event', async ({helper}) => {190 const alice = privateKey('//Alice');191192 const owner = await helper.eth.createAccountWithBalance(alice);158 const owner = await helper.eth.createAccountWithBalance(donor);193 const sponsor = await helper.eth.createAccountWithBalance(alice);159 const sponsor = await helper.eth.createAccountWithBalance(donor);194 const helpers = helper.ethNativeContract.contractHelpers(owner);160 const helpers = helper.ethNativeContract.contractHelpers(owner);195 const flipper = await helper.eth.deployFlipper(owner);161 const flipper = await helper.eth.deployFlipper(owner);196162197 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;163 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});164 const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});199 const events = normalizeEvents(result.events);165 const events = helper.eth.normalizeEvents(result.events);200 expect(events).to.be.deep.equal([166 expect(events).to.be.deep.equal([201 {167 {202 address: flipper.options.address,168 address: flipper.options.address,207 },173 },208 },174 },209 ]);175 ]);210211 // TODO use helper.getApi() from the sceduler PR212 await expectSubstrateEventsAtBlock(213 helper.api!, 214 result.blockNumber,215 'evmContractHelpers',216 ['ContractSponsorshipConfirmed'],217 );218 });176 });219177220 itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper, privateKey}) => {178 itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper}) => {221 const alice = privateKey('//Alice');222223 const owner = await helper.eth.createAccountWithBalance(alice);179 const owner = await helper.eth.createAccountWithBalance(donor);224 const sponsor = await helper.eth.createAccountWithBalance(alice);180 const sponsor = await helper.eth.createAccountWithBalance(donor);225 const notSponsor = await helper.eth.createAccountWithBalance(alice);181 const notSponsor = await helper.eth.createAccountWithBalance(donor);226 const helpers = helper.ethNativeContract.contractHelpers(owner);182 const helpers = helper.ethNativeContract.contractHelpers(owner);227 const flipper = await helper.eth.deployFlipper(owner);183 const flipper = await helper.eth.deployFlipper(owner);228184232 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;188 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;233 });189 });234190235 itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper, privateKey}) => {191 itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper}) => {236 const alice = privateKey('//Alice');237238 const owner = await helper.eth.createAccountWithBalance(alice);192 const owner = await helper.eth.createAccountWithBalance(donor);239 const notSponsor = await helper.eth.createAccountWithBalance(alice);193 const notSponsor = await helper.eth.createAccountWithBalance(donor);240 const helpers = helper.ethNativeContract.contractHelpers(owner);194 const helpers = helper.ethNativeContract.contractHelpers(owner);241 const flipper = await helper.eth.deployFlipper(owner);195 const flipper = await helper.eth.deployFlipper(owner);242196245 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;199 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;246 });200 });247201248 itEth('Get self sponsored sponsor', async ({helper, privateKey}) => {202 itEth('Get self sponsored sponsor', async ({helper}) => {249 const alice = privateKey('//Alice');250251 const owner = await helper.eth.createAccountWithBalance(alice);203 const owner = await helper.eth.createAccountWithBalance(donor);252 const helpers = helper.ethNativeContract.contractHelpers(owner);204 const helpers = helper.ethNativeContract.contractHelpers(owner);253 const flipper = await helper.eth.deployFlipper(owner);205 const flipper = await helper.eth.deployFlipper(owner);254206260 expect(result[1]).to.be.eq('0');212 expect(result[1]).to.be.eq('0');261 });213 });262214263 itEth('Get confirmed sponsor', async ({helper, privateKey}) => {215 itEth('Get confirmed sponsor', async ({helper}) => {264 const alice = privateKey('//Alice');265266 const owner = await helper.eth.createAccountWithBalance(alice);216 const owner = await helper.eth.createAccountWithBalance(donor);267 const sponsor = await helper.eth.createAccountWithBalance(alice);217 const sponsor = await helper.eth.createAccountWithBalance(donor);268 const helpers = helper.ethNativeContract.contractHelpers(owner);218 const helpers = helper.ethNativeContract.contractHelpers(owner);269 const flipper = await helper.eth.deployFlipper(owner);219 const flipper = await helper.eth.deployFlipper(owner);270220277 expect(result[1]).to.be.eq('0');227 expect(result[1]).to.be.eq('0');278 });228 });279229280 itEth('Sponsor can be removed by the address that deployed the contract', async ({helper, privateKey}) => {230 itEth('Sponsor can be removed by the address that deployed the contract', async ({helper}) => {281 const alice = privateKey('//Alice');282283 const owner = await helper.eth.createAccountWithBalance(alice);231 const owner = await helper.eth.createAccountWithBalance(donor);284 const sponsor = await helper.eth.createAccountWithBalance(alice);232 const sponsor = await helper.eth.createAccountWithBalance(donor);285 const helpers = helper.ethNativeContract.contractHelpers(owner);233 const helpers = helper.ethNativeContract.contractHelpers(owner);286 const flipper = await helper.eth.deployFlipper(owner);234 const flipper = await helper.eth.deployFlipper(owner);287235294 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;242 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;295 });243 });296244297 itEth('Remove sponsor event', async ({helper, privateKey}) => {245 itEth('Remove sponsor event', async ({helper}) => {298 const alice = privateKey('//Alice');299300 const owner = await helper.eth.createAccountWithBalance(alice);246 const owner = await helper.eth.createAccountWithBalance(donor);301 const sponsor = await helper.eth.createAccountWithBalance(alice);247 const sponsor = await helper.eth.createAccountWithBalance(donor);302 const helpers = helper.ethNativeContract.contractHelpers(owner);248 const helpers = helper.ethNativeContract.contractHelpers(owner);303 const flipper = await helper.eth.deployFlipper(owner);249 const flipper = await helper.eth.deployFlipper(owner);304250305 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();251 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();306 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});252 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});307 253 308 const result = await helpers.methods.removeSponsor(flipper.options.address).send();254 const result = await helpers.methods.removeSponsor(flipper.options.address).send();309 const events = normalizeEvents(result.events);255 const events = helper.eth.normalizeEvents(result.events);310 expect(events).to.be.deep.equal([256 expect(events).to.be.deep.equal([311 {257 {312 address: flipper.options.address,258 address: flipper.options.address,316 },262 },317 },263 },318 ]);264 ]);319320 // TODO use helper.getApi() from the sceduler PR321 await expectSubstrateEventsAtBlock(322 helper.api!, 323 result.blockNumber,324 'evmContractHelpers',325 ['ContractSponsorRemoved'],326 );327 });265 });328266329 itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper, privateKey}) => {267 itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper}) => {330 const alice = privateKey('//Alice');331332 const owner = await helper.eth.createAccountWithBalance(alice);268 const owner = await helper.eth.createAccountWithBalance(donor);333 const notOwner = await helper.eth.createAccountWithBalance(alice);269 const notOwner = await helper.eth.createAccountWithBalance(donor);334 const sponsor = await helper.eth.createAccountWithBalance(alice);270 const sponsor = await helper.eth.createAccountWithBalance(donor);335 const helpers = helper.ethNativeContract.contractHelpers(owner);271 const helpers = helper.ethNativeContract.contractHelpers(owner);336 const flipper = await helper.eth.deployFlipper(owner);272 const flipper = await helper.eth.deployFlipper(owner);337273344 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;280 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;345 });281 });346282347 itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper, privateKey}) => {283 itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper}) => {348 const alice = privateKey('//Alice');349350 const owner = await helper.eth.createAccountWithBalance(alice);284 const owner = await helper.eth.createAccountWithBalance(donor);351 const sponsor = await helper.eth.createAccountWithBalance(alice);285 const sponsor = await helper.eth.createAccountWithBalance(donor);352 const caller = await helper.eth.createAccountWithBalance(alice);286 const caller = await helper.eth.createAccountWithBalance(donor);353 const helpers = helper.ethNativeContract.contractHelpers(owner);287 const helpers = helper.ethNativeContract.contractHelpers(owner);354 const flipper = await helper.eth.deployFlipper(owner);288 const flipper = await helper.eth.deployFlipper(owner);355289372 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);306 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);373 });307 });374308375 itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper, privateKey}) => {309 itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper}) => {376 const alice = privateKey('//Alice');377378 const owner = await helper.eth.createAccountWithBalance(alice);310 const owner = await helper.eth.createAccountWithBalance(donor);379 const caller = await helper.eth.createAccountWithBalance(alice);311 const caller = await helper.eth.createAccountWithBalance(donor);380 const helpers = helper.ethNativeContract.contractHelpers(owner);312 const helpers = helper.ethNativeContract.contractHelpers(owner);381 const flipper = await helper.eth.deployFlipper(owner);313 const flipper = await helper.eth.deployFlipper(owner);382314385 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});317 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});386 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});318 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});387319388 await helper.eth.transferBalanceFromSubstrate(alice, flipper.options.address);320 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);389321390 const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));322 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));323 const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));400 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);332 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);401 });333 });402334403 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}) => {335 itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({helper}) => {404 const alice = privateKey('//Alice');336 const owner = await helper.eth.createAccountWithBalance(donor);405406 const owner = await helper.eth.createAccountWithBalance(alice);407 const sponsor = await helper.eth.createAccountWithBalance(alice);337 const sponsor = await helper.eth.createAccountWithBalance(donor);408 const caller = await helper.eth.createAccount();338 const caller = helper.eth.createAccount();409 const helpers = helper.ethNativeContract.contractHelpers(owner);339 const helpers = helper.ethNativeContract.contractHelpers(owner);410 const flipper = await helper.eth.deployFlipper(owner);340 const flipper = await helper.eth.deployFlipper(owner);411341429 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;359 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;430 });360 });431361432 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}) => {362 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}) => {433 const alice = privateKey('//Alice');434435 const owner = await helper.eth.createAccountWithBalance(alice);363 const owner = await helper.eth.createAccountWithBalance(donor);436 const caller = await helper.eth.createAccount();364 const caller = await helper.eth.createAccount();437 const helpers = helper.ethNativeContract.contractHelpers(owner);365 const helpers = helper.ethNativeContract.contractHelpers(owner);438 const flipper = await helper.eth.deployFlipper(owner);366 const flipper = await helper.eth.deployFlipper(owner);439367440 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});368 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});441 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});369 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});442370443 await helper.eth.transferBalanceFromSubstrate(alice, flipper.options.address);371 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);444372445 const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);373 const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);446 expect(originalFlipperBalance).to.be.not.equal('0');374 expect(originalFlipperBalance).to.be.not.equal('0');454 expect(balanceAfter).to.be.equals(originalFlipperBalance);382 expect(balanceAfter).to.be.equals(originalFlipperBalance);455 });383 });456384457 itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper, privateKey}) => {385 itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper}) => {458 const alice = privateKey('//Alice');459460 const owner = await helper.eth.createAccountWithBalance(alice);386 const owner = await helper.eth.createAccountWithBalance(donor);461 const sponsor = await helper.eth.createAccountWithBalance(alice);387 const sponsor = await helper.eth.createAccountWithBalance(donor);462 const caller = await helper.eth.createAccountWithBalance(alice);388 const caller = await helper.eth.createAccountWithBalance(donor);463 const helpers = helper.ethNativeContract.contractHelpers(owner);389 const helpers = helper.ethNativeContract.contractHelpers(owner);464 const flipper = await helper.eth.deployFlipper(owner);390 const flipper = await helper.eth.deployFlipper(owner);465391484 expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);410 expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);485 });411 });486412487 itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper, privateKey}) => {413 itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper}) => {488 const alice = privateKey('//Alice');489490 const owner = await helper.eth.createAccountWithBalance(alice);414 const owner = await helper.eth.createAccountWithBalance(donor);491 const sponsor = await helper.eth.createAccountWithBalance(alice);415 const sponsor = await helper.eth.createAccountWithBalance(donor);492 const caller = await helper.eth.createAccountWithBalance(alice);416 const caller = await helper.eth.createAccountWithBalance(donor);493 const helpers = helper.ethNativeContract.contractHelpers(owner);417 const helpers = helper.ethNativeContract.contractHelpers(owner);494 const flipper = await helper.eth.deployFlipper(owner);418 const flipper = await helper.eth.deployFlipper(owner);495 419 519 });443 });520444521 // TODO: Find a way to calculate default rate limit445 // TODO: Find a way to calculate default rate limit522 itEth('Default rate limit equals 7200', async ({helper, privateKey}) => {446 itEth('Default rate limit equals 7200', async ({helper}) => {523 const alice = privateKey('//Alice');524525 const owner = await helper.eth.createAccountWithBalance(alice);447 const owner = await helper.eth.createAccountWithBalance(donor);526 const helpers = helper.ethNativeContract.contractHelpers(owner);448 const helpers = helper.ethNativeContract.contractHelpers(owner);527 const flipper = await helper.eth.deployFlipper(owner);449 const flipper = await helper.eth.deployFlipper(owner);528450531});453});532454533describe('Sponsoring Fee Limit', () => {455describe('Sponsoring Fee Limit', () => {456 let donor: IKeyringPair;457 let alice: IKeyringPair;458 let DEFAULT_GAS: number;534459535 let testContract: CompiledContract;536 537 function compileTestContract() {460 function compileTestContract() {538 if (!testContract) {461 if (!testContract) {539 const input = {462 const input = {581 return testContract;504 return testContract;582 }505 }583 506 584 async function deployTestContract(web3: Web3, owner: string) {507 async function deployTestContract(helper: EthUniqueHelper, owner: string) {508 const web3 = helper.getWeb3();585 const compiled = compileTestContract();509 const compiled = compileTestContract();586 const testContract = new web3.eth.Contract(compiled.abi, undefined, {510 const testContract = new web3.eth.Contract(compiled.abi, undefined, {587 data: compiled.object,511 data: compiled.object,588 from: owner,512 from: owner,589 ...GAS_ARGS,513 gas: DEFAULT_GAS,590 });514 });591 return await testContract.deploy({data: compiled.object}).send({from: owner});515 return await testContract.deploy({data: compiled.object}).send({from: owner});592 }516 }593517594 itEth('Default fee limit', async ({helper, privateKey}) => {518 before(async () => {519 await usingEthPlaygrounds(async (helper, privateKey) => {595 const alice = privateKey('//Alice');520 donor = privateKey('//Alice');521 DEFAULT_GAS = helper.eth.DEFAULT_GAS;522 });523 });596524597 const owner = await helper.eth.createAccountWithBalance(alice);525 beforeEach(async () => {526 await usingPlaygrounds(async (helper) => {527 [alice] = await helper.arrange.createAccounts([1000n], donor);528 });529 });530531 let testContract: CompiledContract;532 533534535 itEth('Default fee limit', async ({helper}) => {536 const owner = await helper.eth.createAccountWithBalance(donor);598 const helpers = helper.ethNativeContract.contractHelpers(owner);537 const helpers = helper.ethNativeContract.contractHelpers(owner);599 const flipper = await helper.eth.deployFlipper(owner);538 const flipper = await helper.eth.deployFlipper(owner);600539601 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('115792089237316195423570985008687907853269984665640564039457584007913129639935');540 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('115792089237316195423570985008687907853269984665640564039457584007913129639935');602 });541 });603542604 itEth('Set fee limit', async ({helper, privateKey}) => {543 itEth('Set fee limit', async ({helper}) => {605 const alice = privateKey('//Alice');606607 const owner = await helper.eth.createAccountWithBalance(alice);544 const owner = await helper.eth.createAccountWithBalance(donor);608 const helpers = helper.ethNativeContract.contractHelpers(owner);545 const helpers = helper.ethNativeContract.contractHelpers(owner);609 const flipper = await helper.eth.deployFlipper(owner);546 const flipper = await helper.eth.deployFlipper(owner);610547611 await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();548 await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();612 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('100');549 expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('100');613 });550 });614551615 itEth('Negative test - set fee limit by non-owner', async ({helper, privateKey}) => {552 itEth('Negative test - set fee limit by non-owner', async ({helper}) => {616 const alice = privateKey('//Alice');617618 const owner = await helper.eth.createAccountWithBalance(alice);553 const owner = await helper.eth.createAccountWithBalance(donor);619 const stranger = await helper.eth.createAccountWithBalance(alice);554 const stranger = await helper.eth.createAccountWithBalance(donor);620 const helpers = helper.ethNativeContract.contractHelpers(owner);555 const helpers = helper.ethNativeContract.contractHelpers(owner);621 const flipper = await helper.eth.deployFlipper(owner);556 const flipper = await helper.eth.deployFlipper(owner);622557623 await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;558 await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;624 });559 });625560626 itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {561 itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper}) => {627 const alice = privateKey('//Alice');628629 const owner = await helper.eth.createAccountWithBalance(alice);562 const owner = await helper.eth.createAccountWithBalance(donor);630 const sponsor = await helper.eth.createAccountWithBalance(alice);563 const sponsor = await helper.eth.createAccountWithBalance(donor);631 const user = await helper.eth.createAccountWithBalance(alice);564 const user = await helper.eth.createAccountWithBalance(donor);632 const helpers = helper.ethNativeContract.contractHelpers(owner);565 const helpers = helper.ethNativeContract.contractHelpers(owner);633566634 const testContract = await deployTestContract(helper.getWeb3(), owner);567 const testContract = await deployTestContract(helper, owner);635 568 636 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});569 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});637 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});570 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});652 });585 });653586654 itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {587 itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper, privateKey}) => {655 const alice = privateKey('//Alice');588 const owner = await helper.eth.createAccountWithBalance(donor);656657 const owner = await helper.eth.createAccountWithBalance(alice);658 const sponsor = await helper.eth.createAccountWithBalance(alice);589 const sponsor = await helper.eth.createAccountWithBalance(donor);659 const helpers = helper.ethNativeContract.contractHelpers(owner);590 const helpers = helper.ethNativeContract.contractHelpers(owner);660591661 const testContract = await deployTestContract(helper.getWeb3(), owner);592 const testContract = await deployTestContract(helper, owner);662 593 663 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});594 await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});664 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});595 await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});672603673 const originalAliceBalance = await helper.balance.getSubstrate(alice.address);604 const originalAliceBalance = await helper.balance.getSubstrate(alice.address);674605675 // 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(606 await helper.eth.sendEVM(690 alice,607 alice,691 testContract.options.address,608 testContract.options.address,692 testContract.methods.test(100).encodeABI(),609 testContract.methods.test(100).encodeABI(),693 '0',610 '0',611 2_000_000,694 );612 );695 // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.be.equal(originalAliceBalance);613 // 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);614 expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);697 615 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(616 await helper.eth.sendEVM(713 alice,617 alice,714 testContract.options.address,618 testContract.options.address,715 testContract.methods.test().encodeABI(),619 testContract.methods.test(100).encodeABI(),716 '0',620 '0',621 2_100_000,717 );622 );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);623 expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);720 });624 });721});625});