git.delta.rocks / unique-network / refs/commits / 0e237807a727

difftreelog

Merge branch 'develop' into tests/feed-alices

Fahrrader2022-10-07parents: #a5bd6fa #ba7ab8a.patch.diff
in: master

5 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -40,6 +40,7 @@
 
     "testEthPayable": "mocha --timeout 9999999 -r ts-node/register './**/eth/payable.test.ts'",
     "testEthTokenProperties": "mocha --timeout 9999999 -r ts-node/register ./**/eth/tokenProperties.test.ts",
+    "testEvmCoder": "mocha --timeout 9999999 -r ts-node/register './**/eth/evmCoder.test.ts'",
     "testNesting": "mocha --timeout 9999999 -r ts-node/register ./**/nest.test.ts",
     "testUnnesting": "mocha --timeout 9999999 -r ts-node/register ./**/unnest.test.ts",
     "testProperties": "mocha --timeout 9999999 -r ts-node/register ./**/properties.test.ts ./**/getPropertiesRpc.test.ts",
@@ -68,7 +69,7 @@
     "testAdminTransferAndBurn": "mocha --timeout 9999999 -r ts-node/register ./**/adminTransferAndBurn.test.ts",
     "testSetPermissions": "mocha --timeout 9999999 -r ts-node/register ./**/setPermissions.test.ts",
     "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
-    "testContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/contractSponsoring.test.ts",
+    "testContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/eth/contractSponsoring.test.ts",
     "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
     "testRemoveFromContractAllowList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromContractAllowList.test.ts",
     "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",
modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
--- a/tests/src/app-promotion.test.ts
+++ b/tests/src/app-promotion.test.ts
@@ -557,6 +557,7 @@
   
       // caller payed for call
       expect(1000n * nominal > callerBalance).to.be.true;
+      // todo:playgrounds look into. why did it work before with 1000n, and is now 100n?
       expect(contractBalanceAfter).to.be.equal(100n * nominal);
     });
   
modifiedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
before · tests/src/eth/contractSponsoring.test.ts
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 {IKeyringPair} from '@polkadot/types/types';18import * as solc from 'solc';19import {EthUniqueHelper} from './util/playgrounds/unique.dev';20import {itEth, expect, SponsoringMode, usingEthPlaygrounds} from '../eth/util/playgrounds';21import {usingPlaygrounds} from '../util/playgrounds';22import {CompiledContract} from './util/playgrounds/types';2324describe('Sponsoring EVM contracts', () => {25  let donor: IKeyringPair;2627  before(async () => {28    await usingPlaygrounds(async (_helper, privateKey) => {29      donor = await privateKey({filename: __filename});30    });31  });3233  itEth('Self sponsored can be set by the address that deployed the contract', async ({helper}) => {34    const owner = await helper.eth.createAccountWithBalance(donor);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}) => {44    const owner = await helper.eth.createAccountWithBalance(donor);45    const flipper = await helper.eth.deployFlipper(owner);46    const helpers = helper.ethNativeContract.contractHelpers(owner);47    48    const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();49    const ethEvents = helper.eth.helper.eth.normalizeEvents(result.events);50    expect(ethEvents).to.be.deep.equal([51      {52        address: flipper.options.address,53        event: 'ContractSponsorSet',54        args: {55          contractAddress: flipper.options.address,56          sponsor: flipper.options.address,57        },58      },59      {60        address: flipper.options.address,61        event: 'ContractSponsorshipConfirmed',62        args: {63          contractAddress: flipper.options.address,64          sponsor: flipper.options.address,65        },66      },67    ]);68  });6970  itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper}) => {71    const owner = await helper.eth.createAccountWithBalance(donor);72    const notOwner = await helper.eth.createAccountWithBalance(donor);73    const helpers = helper.ethNativeContract.contractHelpers(owner);74    const flipper = await helper.eth.deployFlipper(owner);7576    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;77    await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');78    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;79  });8081  itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper}) => {82    const owner = await helper.eth.createAccountWithBalance(donor);83    const helpers = helper.ethNativeContract.contractHelpers(owner);84    const flipper = await helper.eth.deployFlipper(owner);8586    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;87    await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;88    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;89  });9091  itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {92    const owner = await helper.eth.createAccountWithBalance(donor);93    const notOwner = await helper.eth.createAccountWithBalance(donor);94    const helpers = helper.ethNativeContract.contractHelpers(owner);95    const flipper = await helper.eth.deployFlipper(owner);9697    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;98    await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');99    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;100  });101  102  itEth('Sponsor can be set by the address that deployed the contract', async ({helper}) => {103    const owner = await helper.eth.createAccountWithBalance(donor);104    const sponsor = await helper.eth.createAccountWithBalance(donor);105    const helpers = helper.ethNativeContract.contractHelpers(owner);106    const flipper = await helper.eth.deployFlipper(owner);107108    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;109    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;110    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;111  });112  113  itEth('Set sponsor event', async ({helper}) => {114    const owner = await helper.eth.createAccountWithBalance(donor);115    const sponsor = await helper.eth.createAccountWithBalance(donor);116    const helpers = helper.ethNativeContract.contractHelpers(owner);117    const flipper = await helper.eth.deployFlipper(owner);118    119    const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();120    const events = helper.eth.normalizeEvents(result.events);121    expect(events).to.be.deep.equal([122      {123        address: flipper.options.address,124        event: 'ContractSponsorSet',125        args: {126          contractAddress: flipper.options.address,127          sponsor: sponsor,128        },129      },130    ]);131  });132  133  itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper}) => {134    const owner = await helper.eth.createAccountWithBalance(donor);135    const sponsor = await helper.eth.createAccountWithBalance(donor);136    const notOwner = await helper.eth.createAccountWithBalance(donor);137    const helpers = helper.ethNativeContract.contractHelpers(owner);138    const flipper = await helper.eth.deployFlipper(owner);139140    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;141    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');142    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;143  });144145  itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper}) => {146    const owner = await helper.eth.createAccountWithBalance(donor);147    const sponsor = await helper.eth.createAccountWithBalance(donor);148    const helpers = helper.ethNativeContract.contractHelpers(owner);149    const flipper = await helper.eth.deployFlipper(owner);150151    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;152    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;153    await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;154    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;155  });156157  itEth('Confirm sponsorship event', async ({helper}) => {158    const owner = await helper.eth.createAccountWithBalance(donor);159    const sponsor = await helper.eth.createAccountWithBalance(donor);160    const helpers = helper.ethNativeContract.contractHelpers(owner);161    const flipper = await helper.eth.deployFlipper(owner);162163    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;164    const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});165    const events = helper.eth.normalizeEvents(result.events);166    expect(events).to.be.deep.equal([167      {168        address: flipper.options.address,169        event: 'ContractSponsorshipConfirmed',170        args: {171          contractAddress: flipper.options.address,172          sponsor: sponsor,173        },174      },175    ]);176  });177178  itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper}) => {179    const owner = await helper.eth.createAccountWithBalance(donor);180    const sponsor = await helper.eth.createAccountWithBalance(donor);181    const notSponsor = await helper.eth.createAccountWithBalance(donor);182    const helpers = helper.ethNativeContract.contractHelpers(owner);183    const flipper = await helper.eth.deployFlipper(owner);184185    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;186    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;187    await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');188    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;189  });190191  itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper}) => {192    const owner = await helper.eth.createAccountWithBalance(donor);193    const notSponsor = await helper.eth.createAccountWithBalance(donor);194    const helpers = helper.ethNativeContract.contractHelpers(owner);195    const flipper = await helper.eth.deployFlipper(owner);196197    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;198    await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');199    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;200  });201202  itEth('Get self sponsored sponsor', async ({helper}) => {203    const owner = await helper.eth.createAccountWithBalance(donor);204    const helpers = helper.ethNativeContract.contractHelpers(owner);205    const flipper = await helper.eth.deployFlipper(owner);206207    await helpers.methods.selfSponsoredEnable(flipper.options.address).send();208    209    const result = await helpers.methods.sponsor(flipper.options.address).call();210211    expect(result[0]).to.be.eq(flipper.options.address);212    expect(result[1]).to.be.eq('0');213  });214215  itEth('Get confirmed sponsor', async ({helper}) => {216    const owner = await helper.eth.createAccountWithBalance(donor);217    const sponsor = await helper.eth.createAccountWithBalance(donor);218    const helpers = helper.ethNativeContract.contractHelpers(owner);219    const flipper = await helper.eth.deployFlipper(owner);220221    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();222    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});223    224    const result = await helpers.methods.sponsor(flipper.options.address).call();225226    expect(result[0]).to.be.eq(sponsor);227    expect(result[1]).to.be.eq('0');228  });229230  itEth('Sponsor can be removed by the address that deployed the contract', async ({helper}) => {231    const owner = await helper.eth.createAccountWithBalance(donor);232    const sponsor = await helper.eth.createAccountWithBalance(donor);233    const helpers = helper.ethNativeContract.contractHelpers(owner);234    const flipper = await helper.eth.deployFlipper(owner);235236    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;237    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();238    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});239    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;240    241    await helpers.methods.removeSponsor(flipper.options.address).send();242    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;243  });244245  itEth('Remove sponsor event', async ({helper}) => {246    const owner = await helper.eth.createAccountWithBalance(donor);247    const sponsor = await helper.eth.createAccountWithBalance(donor);248    const helpers = helper.ethNativeContract.contractHelpers(owner);249    const flipper = await helper.eth.deployFlipper(owner);250251    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();252    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});253    254    const result = await helpers.methods.removeSponsor(flipper.options.address).send();255    const events = helper.eth.normalizeEvents(result.events);256    expect(events).to.be.deep.equal([257      {258        address: flipper.options.address,259        event: 'ContractSponsorRemoved',260        args: {261          contractAddress: flipper.options.address,262        },263      },264    ]);265  });266267  itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper}) => {268    const owner = await helper.eth.createAccountWithBalance(donor);269    const notOwner = await helper.eth.createAccountWithBalance(donor);270    const sponsor = await helper.eth.createAccountWithBalance(donor);271    const helpers = helper.ethNativeContract.contractHelpers(owner);272    const flipper = await helper.eth.deployFlipper(owner);273274    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;275    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();276    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});277    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;278    279    await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');280    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;281  });282283  itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper}) => {284    const owner = await helper.eth.createAccountWithBalance(donor);285    const sponsor = await helper.eth.createAccountWithBalance(donor);286    const caller = await helper.eth.createAccountWithBalance(donor);287    const helpers = helper.ethNativeContract.contractHelpers(owner);288    const flipper = await helper.eth.deployFlipper(owner);289290    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();291    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});292293    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});294    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});295296    const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));297    const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));298299    await flipper.methods.flip().send({from: caller});300    expect(await flipper.methods.getValue().call()).to.be.true;301302    // Balance should be taken from sponsor instead of caller303    const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));304    const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));305    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;306    expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);307  });308309  itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper}) => {310    const owner = await helper.eth.createAccountWithBalance(donor);311    const caller = await helper.eth.createAccountWithBalance(donor);312    const helpers = helper.ethNativeContract.contractHelpers(owner);313    const flipper = await helper.eth.deployFlipper(owner);314315    await helpers.methods.selfSponsoredEnable(flipper.options.address).send();316317    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});318    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});319320    await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);321322    const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));323    const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));324325    await flipper.methods.flip().send({from: caller});326    expect(await flipper.methods.getValue().call()).to.be.true;327328    // Balance should be taken from sponsor instead of caller329    const contractBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));330    const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));331    expect(contractBalanceAfter < contractBalanceBefore).to.be.true;332    expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);333  });334335  itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({helper}) => {336    const owner = await helper.eth.createAccountWithBalance(donor);337    const sponsor = await helper.eth.createAccountWithBalance(donor);338    const caller = helper.eth.createAccount();339    const helpers = helper.ethNativeContract.contractHelpers(owner);340    const flipper = await helper.eth.deployFlipper(owner);341342    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});343    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});344345    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});346    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});347348    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();349    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});350351    const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));352    expect(sponsorBalanceBefore).to.be.not.equal('0');353354    await flipper.methods.flip().send({from: caller});355    expect(await flipper.methods.getValue().call()).to.be.true;356357    // Balance should be taken from flipper instead of caller358    const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));359    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;360  });361362  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}) => {363    const owner = await helper.eth.createAccountWithBalance(donor);364    const caller = await helper.eth.createAccount();365    const helpers = helper.ethNativeContract.contractHelpers(owner);366    const flipper = await helper.eth.deployFlipper(owner);367368    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});369    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});370371    await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);372373    const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);374    expect(originalFlipperBalance).to.be.not.equal('0');375376    await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/InvalidTransaction::Payment/);377    expect(await flipper.methods.getValue().call()).to.be.false;378379    // Balance should be taken from flipper instead of caller380    // FIXME the comment is wrong! What check should be here?381    const balanceAfter = await helper.balance.getEthereum(flipper.options.address);382    expect(balanceAfter).to.be.equals(originalFlipperBalance);383  });384385  itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper}) => {386    const owner = await helper.eth.createAccountWithBalance(donor);387    const sponsor = await helper.eth.createAccountWithBalance(donor);388    const caller = await helper.eth.createAccountWithBalance(donor);389    const helpers = helper.ethNativeContract.contractHelpers(owner);390    const flipper = await helper.eth.deployFlipper(owner);391392    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});393    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});394395    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});396    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});397398    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();399    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});400401    const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));402    const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));403404    await flipper.methods.flip().send({from: caller});405    expect(await flipper.methods.getValue().call()).to.be.true;406407    const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));408    const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));409    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;410    expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);411  });412413  itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper}) => {414    const owner = await helper.eth.createAccountWithBalance(donor);415    const sponsor = await helper.eth.createAccountWithBalance(donor);416    const caller = await helper.eth.createAccountWithBalance(donor);417    const helpers = helper.ethNativeContract.contractHelpers(owner);418    const flipper = await helper.eth.deployFlipper(owner);419    420    const originalCallerBalance = await helper.balance.getEthereum(caller);421    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});422    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});423424    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});425    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});426427    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();428    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});429430    const originalFlipperBalance = await helper.balance.getEthereum(sponsor);431    expect(originalFlipperBalance).to.be.not.equal('0');432433    await flipper.methods.flip().send({from: caller});434    expect(await flipper.methods.getValue().call()).to.be.true;435    expect(await helper.balance.getEthereum(caller)).to.be.equals(originalCallerBalance);436437    const newFlipperBalance = await helper.balance.getEthereum(sponsor);438    expect(newFlipperBalance).to.be.not.equals(originalFlipperBalance);439440    await flipper.methods.flip().send({from: caller});441    expect(await helper.balance.getEthereum(sponsor)).to.be.equal(newFlipperBalance);442    expect(await helper.balance.getEthereum(caller)).to.be.not.equals(originalCallerBalance);443  });444445  // TODO: Find a way to calculate default rate limit446  itEth('Default rate limit equals 7200', async ({helper}) => {447    const owner = await helper.eth.createAccountWithBalance(donor);448    const helpers = helper.ethNativeContract.contractHelpers(owner);449    const flipper = await helper.eth.deployFlipper(owner);450451    expect(await helpers.methods.sponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');452  });453});454455describe('Sponsoring Fee Limit', () => {456  let donor: IKeyringPair;457  let alice: IKeyringPair;458  let DEFAULT_GAS: number;459460  function compileTestContract() {461    if (!testContract) {462      const input = {463        language: 'Solidity',464        sources: {465          ['TestContract.sol']: {466            content:467            `468            // SPDX-License-Identifier: MIT469            pragma solidity ^0.8.0;470            471            contract TestContract {472              event Result(bool);473474              function test(uint32 cycles) public {475                uint256 counter = 0;476                while(true) {477                  counter ++;478                  if (counter > cycles){479                    break;480                  }481                }482                emit Result(true);483              }484            }485            `,486          },487        },488        settings: {489          outputSelection: {490            '*': {491              '*': ['*'],492            },493          },494        },495      };496      const json = JSON.parse(solc.compile(JSON.stringify(input)));497      const out = json.contracts['TestContract.sol']['TestContract'];498  499      testContract = {500        abi: out.abi,501        object: '0x' + out.evm.bytecode.object,502      };503    }504    return testContract;505  }506  507  async function deployTestContract(helper: EthUniqueHelper, owner: string) {508    const web3 = helper.getWeb3();509    const compiled = compileTestContract();510    const testContract = new web3.eth.Contract(compiled.abi, undefined, {511      data: compiled.object,512      from: owner,513      gas: DEFAULT_GAS,514    });515    return await testContract.deploy({data: compiled.object}).send({from: owner});516  }517518  before(async () => {519    await usingEthPlaygrounds(async (helper, privateKey) => {520      donor = await privateKey({filename: __filename});521      [alice] = await helper.arrange.createAccounts([100n], donor);522      DEFAULT_GAS = helper.eth.DEFAULT_GAS;523    });524  });525526  let testContract: CompiledContract;527528  itEth('Default fee limit', async ({helper}) => {529    const owner = await helper.eth.createAccountWithBalance(donor);530    const helpers = helper.ethNativeContract.contractHelpers(owner);531    const flipper = await helper.eth.deployFlipper(owner);532533    expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('115792089237316195423570985008687907853269984665640564039457584007913129639935');534  });535536  itEth('Set fee limit', async ({helper}) => {537    const owner = await helper.eth.createAccountWithBalance(donor);538    const helpers = helper.ethNativeContract.contractHelpers(owner);539    const flipper = await helper.eth.deployFlipper(owner);540541    await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();542    expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('100');543  });544545  itEth('Negative test - set fee limit by non-owner', async ({helper}) => {546    const owner = await helper.eth.createAccountWithBalance(donor);547    const stranger = await helper.eth.createAccountWithBalance(donor);548    const helpers = helper.ethNativeContract.contractHelpers(owner);549    const flipper = await helper.eth.deployFlipper(owner);550551    await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;552  });553554  itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper}) => {555    const owner = await helper.eth.createAccountWithBalance(donor);556    const sponsor = await helper.eth.createAccountWithBalance(donor);557    const user = await helper.eth.createAccountWithBalance(donor);558    const helpers = helper.ethNativeContract.contractHelpers(owner);559560    const testContract = await deployTestContract(helper, owner);561    562    await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});563    await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});564    565    await helpers.methods.setSponsor(testContract.options.address, sponsor).send();566    await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});567568    const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());569570    await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();571572    const originalUserBalance = await helper.balance.getEthereum(user);573    await testContract.methods.test(100).send({from: user, gas: 2_000_000});574    expect(await helper.balance.getEthereum(user)).to.be.equal(originalUserBalance);575576    await testContract.methods.test(100).send({from: user, gas: 2_100_000});577    expect(await helper.balance.getEthereum(user)).to.not.be.equal(originalUserBalance);578  });579580  itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper}) => {581    const owner = await helper.eth.createAccountWithBalance(donor);582    const sponsor = await helper.eth.createAccountWithBalance(donor);583    const helpers = helper.ethNativeContract.contractHelpers(owner);584585    const testContract = await deployTestContract(helper, owner);586    587    await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});588    await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});589    590    await helpers.methods.setSponsor(testContract.options.address, sponsor).send();591    await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});592593    const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());594595    await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();596597    const originalAliceBalance = await helper.balance.getSubstrate(alice.address);598599    await helper.eth.sendEVM(600      alice,601      testContract.options.address,602      testContract.methods.test(100).encodeABI(),603      '0',604      2_000_000,605    );606    // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.be.equal(originalAliceBalance);607    expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);608    609    await helper.eth.sendEVM(610      alice,611      testContract.options.address,612      testContract.methods.test(100).encodeABI(),613      '0',614      2_100_000,615    );616    expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);617  });618});
after · tests/src/eth/contractSponsoring.test.ts
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 {IKeyringPair} from '@polkadot/types/types';18import {EthUniqueHelper} from './util/playgrounds/unique.dev';19import {itEth, expect, SponsoringMode, usingEthPlaygrounds} from '../eth/util/playgrounds';20import {usingPlaygrounds} from '../util/playgrounds';21import {CompiledContract} from './util/playgrounds/types';2223describe('Sponsoring EVM contracts', () => {24  let donor: IKeyringPair;2526  before(async () => {27    await usingPlaygrounds(async (_helper, privateKey) => {28      donor = await privateKey({filename: __filename});29    });30  });3132  itEth('Self sponsored can be set by the address that deployed the contract', async ({helper}) => {33    const owner = await helper.eth.createAccountWithBalance(donor);34    const flipper = await helper.eth.deployFlipper(owner);35    const helpers = helper.ethNativeContract.contractHelpers(owner);3637    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;38    await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;39    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;40  });4142  itEth('Set self sponsored events', async ({helper}) => {43    const owner = await helper.eth.createAccountWithBalance(donor);44    const flipper = await helper.eth.deployFlipper(owner);45    const helpers = helper.ethNativeContract.contractHelpers(owner);46    47    const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();48    const ethEvents = helper.eth.helper.eth.normalizeEvents(result.events);49    expect(ethEvents).to.be.deep.equal([50      {51        address: flipper.options.address,52        event: 'ContractSponsorSet',53        args: {54          contractAddress: flipper.options.address,55          sponsor: flipper.options.address,56        },57      },58      {59        address: flipper.options.address,60        event: 'ContractSponsorshipConfirmed',61        args: {62          contractAddress: flipper.options.address,63          sponsor: flipper.options.address,64        },65      },66    ]);67  });6869  itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper}) => {70    const owner = await helper.eth.createAccountWithBalance(donor);71    const notOwner = await helper.eth.createAccountWithBalance(donor);72    const helpers = helper.ethNativeContract.contractHelpers(owner);73    const flipper = await helper.eth.deployFlipper(owner);7475    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;76    await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');77    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;78  });7980  itEth('Sponsoring can be set by the address that has deployed the contract', async ({helper}) => {81    const owner = await helper.eth.createAccountWithBalance(donor);82    const helpers = helper.ethNativeContract.contractHelpers(owner);83    const flipper = await helper.eth.deployFlipper(owner);8485    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;86    await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;87    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;88  });8990  itEth('Sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {91    const owner = await helper.eth.createAccountWithBalance(donor);92    const notOwner = await helper.eth.createAccountWithBalance(donor);93    const helpers = helper.ethNativeContract.contractHelpers(owner);94    const flipper = await helper.eth.deployFlipper(owner);9596    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;97    await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).call({from: notOwner})).to.be.rejectedWith('NoPermission');98    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;99  });100  101  itEth('Sponsor can be set by the address that deployed the contract', async ({helper}) => {102    const owner = await helper.eth.createAccountWithBalance(donor);103    const sponsor = await helper.eth.createAccountWithBalance(donor);104    const helpers = helper.ethNativeContract.contractHelpers(owner);105    const flipper = await helper.eth.deployFlipper(owner);106107    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;108    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;109    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;110  });111  112  itEth('Set sponsor event', async ({helper}) => {113    const owner = await helper.eth.createAccountWithBalance(donor);114    const sponsor = await helper.eth.createAccountWithBalance(donor);115    const helpers = helper.ethNativeContract.contractHelpers(owner);116    const flipper = await helper.eth.deployFlipper(owner);117    118    const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();119    const events = helper.eth.normalizeEvents(result.events);120    expect(events).to.be.deep.equal([121      {122        address: flipper.options.address,123        event: 'ContractSponsorSet',124        args: {125          contractAddress: flipper.options.address,126          sponsor: sponsor,127        },128      },129    ]);130  });131  132  itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper}) => {133    const owner = await helper.eth.createAccountWithBalance(donor);134    const sponsor = await helper.eth.createAccountWithBalance(donor);135    const notOwner = await helper.eth.createAccountWithBalance(donor);136    const helpers = helper.ethNativeContract.contractHelpers(owner);137    const flipper = await helper.eth.deployFlipper(owner);138139    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;140    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');141    expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;142  });143144  itEth('Sponsorship can be confirmed by the address that pending as sponsor', async ({helper}) => {145    const owner = await helper.eth.createAccountWithBalance(donor);146    const sponsor = await helper.eth.createAccountWithBalance(donor);147    const helpers = helper.ethNativeContract.contractHelpers(owner);148    const flipper = await helper.eth.deployFlipper(owner);149150    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;151    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;152    await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;153    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;154  });155156  itEth('Confirm sponsorship event', async ({helper}) => {157    const owner = await helper.eth.createAccountWithBalance(donor);158    const sponsor = await helper.eth.createAccountWithBalance(donor);159    const helpers = helper.ethNativeContract.contractHelpers(owner);160    const flipper = await helper.eth.deployFlipper(owner);161162    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;163    const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});164    const events = helper.eth.normalizeEvents(result.events);165    expect(events).to.be.deep.equal([166      {167        address: flipper.options.address,168        event: 'ContractSponsorshipConfirmed',169        args: {170          contractAddress: flipper.options.address,171          sponsor: sponsor,172        },173      },174    ]);175  });176177  itEth('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({helper}) => {178    const owner = await helper.eth.createAccountWithBalance(donor);179    const sponsor = await helper.eth.createAccountWithBalance(donor);180    const notSponsor = await helper.eth.createAccountWithBalance(donor);181    const helpers = helper.ethNativeContract.contractHelpers(owner);182    const flipper = await helper.eth.deployFlipper(owner);183184    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;185    await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;186    await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');187    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;188  });189190  itEth('Sponsorship can not be confirmed by the address that not set as sponsor', async ({helper}) => {191    const owner = await helper.eth.createAccountWithBalance(donor);192    const notSponsor = await helper.eth.createAccountWithBalance(donor);193    const helpers = helper.ethNativeContract.contractHelpers(owner);194    const flipper = await helper.eth.deployFlipper(owner);195196    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;197    await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');198    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;199  });200201  itEth('Get self sponsored sponsor', async ({helper}) => {202    const owner = await helper.eth.createAccountWithBalance(donor);203    const helpers = helper.ethNativeContract.contractHelpers(owner);204    const flipper = await helper.eth.deployFlipper(owner);205206    await helpers.methods.selfSponsoredEnable(flipper.options.address).send();207    208    const result = await helpers.methods.sponsor(flipper.options.address).call();209210    expect(result[0]).to.be.eq(flipper.options.address);211    expect(result[1]).to.be.eq('0');212  });213214  itEth('Get confirmed sponsor', async ({helper}) => {215    const owner = await helper.eth.createAccountWithBalance(donor);216    const sponsor = await helper.eth.createAccountWithBalance(donor);217    const helpers = helper.ethNativeContract.contractHelpers(owner);218    const flipper = await helper.eth.deployFlipper(owner);219220    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();221    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});222    223    const result = await helpers.methods.sponsor(flipper.options.address).call();224225    expect(result[0]).to.be.eq(sponsor);226    expect(result[1]).to.be.eq('0');227  });228229  itEth('Sponsor can be removed by the address that deployed the contract', async ({helper}) => {230    const owner = await helper.eth.createAccountWithBalance(donor);231    const sponsor = await helper.eth.createAccountWithBalance(donor);232    const helpers = helper.ethNativeContract.contractHelpers(owner);233    const flipper = await helper.eth.deployFlipper(owner);234235    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;236    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();237    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});238    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;239    240    await helpers.methods.removeSponsor(flipper.options.address).send();241    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;242  });243244  itEth('Remove sponsor event', async ({helper}) => {245    const owner = await helper.eth.createAccountWithBalance(donor);246    const sponsor = await helper.eth.createAccountWithBalance(donor);247    const helpers = helper.ethNativeContract.contractHelpers(owner);248    const flipper = await helper.eth.deployFlipper(owner);249250    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();251    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});252    253    const result = await helpers.methods.removeSponsor(flipper.options.address).send();254    const events = helper.eth.normalizeEvents(result.events);255    expect(events).to.be.deep.equal([256      {257        address: flipper.options.address,258        event: 'ContractSponsorRemoved',259        args: {260          contractAddress: flipper.options.address,261        },262      },263    ]);264  });265266  itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper}) => {267    const owner = await helper.eth.createAccountWithBalance(donor);268    const notOwner = await helper.eth.createAccountWithBalance(donor);269    const sponsor = await helper.eth.createAccountWithBalance(donor);270    const helpers = helper.ethNativeContract.contractHelpers(owner);271    const flipper = await helper.eth.deployFlipper(owner);272273    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;274    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();275    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});276    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;277    278    await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');279    expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;280  });281282  itEth('In generous mode, non-allowlisted user transaction will be sponsored', async ({helper}) => {283    const owner = await helper.eth.createAccountWithBalance(donor);284    const sponsor = await helper.eth.createAccountWithBalance(donor);285    const caller = await helper.eth.createAccountWithBalance(donor);286    const helpers = helper.ethNativeContract.contractHelpers(owner);287    const flipper = await helper.eth.deployFlipper(owner);288289    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();290    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});291292    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});293    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});294295    const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));296    const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));297298    await flipper.methods.flip().send({from: caller});299    expect(await flipper.methods.getValue().call()).to.be.true;300301    // Balance should be taken from sponsor instead of caller302    const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));303    const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));304    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;305    expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);306  });307308  itEth('In generous mode, non-allowlisted user transaction will be self sponsored', async ({helper}) => {309    const owner = await helper.eth.createAccountWithBalance(donor);310    const caller = await helper.eth.createAccountWithBalance(donor);311    const helpers = helper.ethNativeContract.contractHelpers(owner);312    const flipper = await helper.eth.deployFlipper(owner);313314    await helpers.methods.selfSponsoredEnable(flipper.options.address).send();315316    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});317    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});318319    await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);320321    const contractBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));322    const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));323324    await flipper.methods.flip().send({from: caller});325    expect(await flipper.methods.getValue().call()).to.be.true;326327    // Balance should be taken from sponsor instead of caller328    const contractBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(flipper.options.address));329    const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));330    expect(contractBalanceAfter < contractBalanceBefore).to.be.true;331    expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);332  });333334  itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({helper}) => {335    const owner = await helper.eth.createAccountWithBalance(donor);336    const sponsor = await helper.eth.createAccountWithBalance(donor);337    const caller = helper.eth.createAccount();338    const helpers = helper.ethNativeContract.contractHelpers(owner);339    const flipper = await helper.eth.deployFlipper(owner);340341    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});342    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});343344    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});345    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});346347    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();348    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});349350    const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));351    expect(sponsorBalanceBefore).to.be.not.equal('0');352353    await flipper.methods.flip().send({from: caller});354    expect(await flipper.methods.getValue().call()).to.be.true;355356    // Balance should be taken from flipper instead of caller357    const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));358    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;359  });360361  itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({helper}) => {362    const owner = await helper.eth.createAccountWithBalance(donor);363    const caller = await helper.eth.createAccount();364    const helpers = helper.ethNativeContract.contractHelpers(owner);365    const flipper = await helper.eth.deployFlipper(owner);366367    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});368    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});369370    await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);371372    const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);373    expect(originalFlipperBalance).to.be.not.equal('0');374375    await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/InvalidTransaction::Payment/);376    expect(await flipper.methods.getValue().call()).to.be.false;377378    // Balance should be taken from flipper instead of caller379    // FIXME the comment is wrong! What check should be here?380    const balanceAfter = await helper.balance.getEthereum(flipper.options.address);381    expect(balanceAfter).to.be.equals(originalFlipperBalance);382  });383384  itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper}) => {385    const owner = await helper.eth.createAccountWithBalance(donor);386    const sponsor = await helper.eth.createAccountWithBalance(donor);387    const caller = await helper.eth.createAccountWithBalance(donor);388    const helpers = helper.ethNativeContract.contractHelpers(owner);389    const flipper = await helper.eth.deployFlipper(owner);390391    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});392    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});393394    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});395    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});396397    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();398    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});399400    const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));401    const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));402403    await flipper.methods.flip().send({from: caller});404    expect(await flipper.methods.getValue().call()).to.be.true;405406    const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));407    const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));408    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;409    expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);410  });411412  itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper}) => {413    const owner = await helper.eth.createAccountWithBalance(donor);414    const sponsor = await helper.eth.createAccountWithBalance(donor);415    const caller = await helper.eth.createAccountWithBalance(donor);416    const helpers = helper.ethNativeContract.contractHelpers(owner);417    const flipper = await helper.eth.deployFlipper(owner);418    419    const originalCallerBalance = await helper.balance.getEthereum(caller);420    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});421    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});422423    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});424    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});425426    await helpers.methods.setSponsor(flipper.options.address, sponsor).send();427    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});428429    const originalFlipperBalance = await helper.balance.getEthereum(sponsor);430    expect(originalFlipperBalance).to.be.not.equal('0');431432    await flipper.methods.flip().send({from: caller});433    expect(await flipper.methods.getValue().call()).to.be.true;434    expect(await helper.balance.getEthereum(caller)).to.be.equals(originalCallerBalance);435436    const newFlipperBalance = await helper.balance.getEthereum(sponsor);437    expect(newFlipperBalance).to.be.not.equals(originalFlipperBalance);438439    await flipper.methods.flip().send({from: caller});440    expect(await helper.balance.getEthereum(sponsor)).to.be.equal(newFlipperBalance);441    expect(await helper.balance.getEthereum(caller)).to.be.not.equals(originalCallerBalance);442  });443444  // TODO: Find a way to calculate default rate limit445  itEth('Default rate limit equals 7200', async ({helper}) => {446    const owner = await helper.eth.createAccountWithBalance(donor);447    const helpers = helper.ethNativeContract.contractHelpers(owner);448    const flipper = await helper.eth.deployFlipper(owner);449450    expect(await helpers.methods.sponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');451  });452});453454describe('Sponsoring Fee Limit', () => {455  let donor: IKeyringPair;456  let alice: IKeyringPair;457  let testContract: CompiledContract;458459  async function compileTestContract(helper: EthUniqueHelper) {460    if (!testContract) {461      testContract = await helper.ethContract.compile(462        'TestContract',463        `464        // SPDX-License-Identifier: MIT465        pragma solidity ^0.8.0;466        467        contract TestContract {468          event Result(bool);469470          function test(uint32 cycles) public {471            uint256 counter = 0;472            while(true) {473              counter ++;474              if (counter > cycles){475                break;476              }477            }478            emit Result(true);479          }480        }481      `,482      );483    }484    return testContract;485  }486  487  async function deployTestContract(helper: EthUniqueHelper, owner: string) {488    const compiled = await compileTestContract(helper);489    return await helper.ethContract.deployByAbi(owner, compiled.abi, compiled.object);490  }491492  before(async () => {493    await usingEthPlaygrounds(async (helper, privateKey) => {494      donor = await privateKey({filename: __filename});495      [alice] = await helper.arrange.createAccounts([100n], donor);496    });497  });498499  itEth('Default fee limit', async ({helper}) => {500    const owner = await helper.eth.createAccountWithBalance(donor);501    const helpers = helper.ethNativeContract.contractHelpers(owner);502    const flipper = await helper.eth.deployFlipper(owner);503504    expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('115792089237316195423570985008687907853269984665640564039457584007913129639935');505  });506507  itEth('Set fee limit', async ({helper}) => {508    const owner = await helper.eth.createAccountWithBalance(donor);509    const helpers = helper.ethNativeContract.contractHelpers(owner);510    const flipper = await helper.eth.deployFlipper(owner);511512    await helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send();513    expect(await helpers.methods.sponsoringFeeLimit(flipper.options.address).call()).to.be.equals('100');514  });515516  itEth('Negative test - set fee limit by non-owner', async ({helper}) => {517    const owner = await helper.eth.createAccountWithBalance(donor);518    const stranger = await helper.eth.createAccountWithBalance(donor);519    const helpers = helper.ethNativeContract.contractHelpers(owner);520    const flipper = await helper.eth.deployFlipper(owner);521522    await expect(helpers.methods.setSponsoringFeeLimit(flipper.options.address, 100).send({from: stranger})).to.be.rejected;523  });524525  itEth('Negative test - check that eth transactions exceeding fee limit are not executed', async ({helper}) => {526    const owner = await helper.eth.createAccountWithBalance(donor);527    const sponsor = await helper.eth.createAccountWithBalance(donor);528    const user = await helper.eth.createAccountWithBalance(donor);529    const helpers = helper.ethNativeContract.contractHelpers(owner);530531    const testContract = await deployTestContract(helper, owner);532    533    await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});534    await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});535    536    await helpers.methods.setSponsor(testContract.options.address, sponsor).send();537    await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});538539    const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());540541    await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();542543    const originalUserBalance = await helper.balance.getEthereum(user);544    await testContract.methods.test(100).send({from: user, gas: 2_000_000});545    expect(await helper.balance.getEthereum(user)).to.be.equal(originalUserBalance);546547    await testContract.methods.test(100).send({from: user, gas: 2_100_000});548    expect(await helper.balance.getEthereum(user)).to.not.be.equal(originalUserBalance);549  });550551  itEth('Negative test - check that evm.call transactions exceeding fee limit are not executed', async ({helper}) => {552    const owner = await helper.eth.createAccountWithBalance(donor);553    const sponsor = await helper.eth.createAccountWithBalance(donor);554    const helpers = helper.ethNativeContract.contractHelpers(owner);555556    const testContract = await deployTestContract(helper, owner);557    558    await helpers.methods.setSponsoringMode(testContract.options.address, SponsoringMode.Generous).send({from: owner});559    await helpers.methods.setSponsoringRateLimit(testContract.options.address, 0).send({from: owner});560    561    await helpers.methods.setSponsor(testContract.options.address, sponsor).send();562    await helpers.methods.confirmSponsorship(testContract.options.address).send({from: sponsor});563564    const gasPrice = BigInt(await helper.getWeb3().eth.getGasPrice());565566    await helpers.methods.setSponsoringFeeLimit(testContract.options.address, 2_000_000n * gasPrice).send();567568    const originalAliceBalance = await helper.balance.getSubstrate(alice.address);569570    await helper.eth.sendEVM(571      alice,572      testContract.options.address,573      testContract.methods.test(100).encodeABI(),574      '0',575      2_000_000,576    );577    // expect((await api.query.system.account(alice.address)).data.free.toBigInt()).to.be.equal(originalAliceBalance);578    expect(await helper.balance.getSubstrate(alice.address)).to.be.equal(originalAliceBalance);579    580    await helper.eth.sendEVM(581      alice,582      testContract.options.address,583      testContract.methods.test(100).encodeABI(),584      '0',585      2_100_000,586    );587    expect(await helper.balance.getSubstrate(alice.address)).to.not.be.equal(originalAliceBalance);588  });589});
addedtests/src/eth/evmCoder.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/eth/evmCoder.test.ts
@@ -0,0 +1,90 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {itEth, expect, usingEthPlaygrounds} from './util/playgrounds';
+
+const getContractSource = (collectionAddress: string, contractAddress: string): string => {
+  return `
+  // SPDX-License-Identifier: MIT
+  pragma solidity ^0.8.0;
+  interface ITest {
+    function ztestzzzzzzz() external returns (uint256 n);
+  }
+  contract Test {
+    event Result(bool, uint256);
+    function test1() public {
+        try
+            ITest(${collectionAddress}).ztestzzzzzzz()
+        returns (uint256 n) {
+            // enters
+            emit Result(true, n); // => [true, BigNumber { value: "43648854190028290368124427828690944273759144372138548774646036134290060795932" }]
+        } catch {
+            emit Result(false, 0);
+        }
+    }
+    function test2() public {
+        try
+            ITest(${contractAddress}).ztestzzzzzzz()
+        returns (uint256 n) {
+            emit Result(true, n);
+        } catch {
+            // enters
+            emit Result(false, 0); // => [ false, BigNumber { value: "0" } ]
+        }
+    }
+    function test3() public {
+      ITest(${collectionAddress}).ztestzzzzzzz();
+    }
+  }
+  `;
+};
+
+
+describe('Evm Coder tests', () => {
+  let donor: IKeyringPair;
+
+  before(async function() {
+    await usingEthPlaygrounds(async (_helper, privateKey) => {
+      donor = await privateKey({filename: __filename});
+    });
+  });
+  
+  itEth('Call non-existing function', async ({helper}) => {
+    const owner = await helper.eth.createAccountWithBalance(donor);
+    const collection = await helper.eth.createNonfungibleCollection(owner, 'EVMCODER', '', 'TEST');
+    const contract = await helper.ethContract.deployByCode(owner, 'Test', getContractSource(collection.collectionAddress, '0x1bfed5D614b886b9Ab2eA4CBAc22A96B7EC29c9c'));
+    const testContract = await helper.ethContract.deployByCode(owner, 'Test', getContractSource(collection.collectionAddress, contract.options.address));
+    {
+      const result = await testContract.methods.test1().send();
+      expect(result.events.Result.returnValues).to.deep.equal({
+        '0': false,
+        '1': '0',
+      });
+    }
+    {
+      const result = await testContract.methods.test2().send();
+      expect(result.events.Result.returnValues).to.deep.equal({
+        '0': false,
+        '1': '0',
+      });
+    }
+    {
+      await expect(testContract.methods.test3().call())
+        .to.be.rejectedWith(/unrecognized selector: 0xd9f02b36$/g);
+    }
+  });
+});
deletedtests/src/evmCoder.test.tsdiffbeforeafterboth
--- a/tests/src/evmCoder.test.ts
+++ /dev/null
@@ -1,123 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// Unique Network is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-import {IKeyringPair} from '@polkadot/types/types';
-import Web3 from 'web3';
-import {itEth, expect, usingEthPlaygrounds} from './eth/util/playgrounds';
-import * as solc from 'solc';
-
-async function compileTestContract(collectionAddress: string, contractAddress: string) {
-  const input = {
-    language: 'Solidity',
-    sources: {
-      ['Test.sol']: {
-        content: 
-        `
-        // SPDX-License-Identifier: MIT
-        pragma solidity ^0.8.0;
-        interface ITest {
-          function ztestzzzzzzz() external returns (uint256 n);
-        }
-        contract Test {
-          event Result(bool, uint256);
-          function test1() public {
-              try
-                  ITest(${collectionAddress}).ztestzzzzzzz()
-              returns (uint256 n) {
-                  // enters
-                  emit Result(true, n); // => [true, BigNumber { value: "43648854190028290368124427828690944273759144372138548774646036134290060795932" }]
-              } catch {
-                  emit Result(false, 0);
-              }
-          }
-          function test2() public {
-              try
-                  ITest(${contractAddress}).ztestzzzzzzz()
-              returns (uint256 n) {
-                  emit Result(true, n);
-              } catch {
-                  // enters
-                  emit Result(false, 0); // => [ false, BigNumber { value: "0" } ]
-              }
-          }
-          function test3() public {
-            ITest(${collectionAddress}).ztestzzzzzzz();
-          }
-        }
-        `,
-      },
-    },
-    settings: {
-      outputSelection: {
-        '*': {
-          '*': ['*'],
-        },
-      },
-    },
-  };
-  const json = JSON.parse(solc.compile(JSON.stringify(input)));
-  const out = json.contracts['Test.sol']['Test'];
-
-  return  {
-    abi: out.abi,
-    object: '0x' + out.evm.bytecode.object,
-  };
-}
-
-async function deployTestContract(web3: Web3, owner: string, collectionAddress: string, contractAddress: string, gas: number) {
-  const compiled = await compileTestContract(collectionAddress, contractAddress);
-  const fractionalizerContract = new web3.eth.Contract(compiled.abi, undefined, {
-    data: compiled.object,
-    from: owner,
-    gas,
-  });
-  return await fractionalizerContract.deploy({data: compiled.object}).send({from: owner});
-}
-
-describe('Evm Coder tests', () => {
-  let donor: IKeyringPair;
-
-  before(async function() {
-    await usingEthPlaygrounds(async (_helper, privateKey) => {
-      donor = await privateKey({filename: __filename});
-    });
-  });
-  
-  itEth('Call non-existing function', async ({helper}) => {
-    const owner = await helper.eth.createAccountWithBalance(donor);
-    const collection = await helper.eth.createNonfungibleCollection(owner, 'EVMCODER', '', 'TEST');
-    const contract = await deployTestContract(helper.getWeb3(), owner, collection.collectionAddress, '0x1bfed5D614b886b9Ab2eA4CBAc22A96B7EC29c9c', helper.eth.DEFAULT_GAS);
-    const testContract = await deployTestContract(helper.getWeb3(), owner, collection.collectionAddress, contract.options.address, helper.eth.DEFAULT_GAS);
-    {
-      const result = await testContract.methods.test1().send();
-      expect(result.events.Result.returnValues).to.deep.equal({
-        '0': false,
-        '1': '0',
-      });
-    }
-    {
-      const result = await testContract.methods.test2().send();
-      expect(result.events.Result.returnValues).to.deep.equal({
-        '0': false,
-        '1': '0',
-      });
-    }
-    {
-      await expect(testContract.methods.test3().call())
-        .to.be.rejectedWith(/unrecognized selector: 0xd9f02b36$/g);
-    }
-  });
-});