git.delta.rocks / unique-network / refs/commits / 667f02faac6f

difftreelog

source

js-packages/tests/eth/sponsoring.test.ts4.8 KiBsourcehistory
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 type {IKeyringPair} from '@polkadot/types/types';18import {itEth, expect, SponsoringMode} from './util/index.js';19import {usingPlaygrounds} from '@unique/test-utils/util.js';2021describe('EVM sponsoring', () => {22  let donor: IKeyringPair;2324  before(async () => {25    await usingPlaygrounds(async (_helper, privateKey) => {26      donor = await privateKey({url: import.meta.url});27    });28  });2930  itEth('Fee is deducted from contract if sponsoring is enabled', async ({helper}) => {31    const owner = await helper.eth.createAccountWithBalance(donor);32    const sponsor = await helper.eth.createAccountWithBalance(donor);33    const caller = helper.eth.createAccount();34    const originalCallerBalance = await helper.balance.getEthereum(caller);3536    expect(originalCallerBalance).to.be.equal(0n);3738    const flipper = await helper.eth.deployFlipper(owner);3940    const helpers = await helper.ethNativeContract.contractHelpers(owner);4142    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});43    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});4445    await helpers.methods.setSponsor(flipper.options.address, sponsor).send({from: owner});46    await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});4748    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;49    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});50    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});51    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;5253    const originalSponsorBalance = await helper.balance.getEthereum(sponsor);54    expect(originalSponsorBalance).to.be.not.equal(0n);5556    await flipper.methods.flip().send({from: caller});57    expect(await flipper.methods.getValue().call()).to.be.true;5859    // Balance should be taken from flipper instead of caller60    expect(await helper.balance.getEthereum(caller)).to.be.equal(originalCallerBalance);61    expect(await helper.balance.getEthereum(sponsor)).to.be.not.equal(originalSponsorBalance);62  });6364  itEth('...but this doesn\'t applies to payable value', async ({helper}) => {65    const owner = await helper.eth.createAccountWithBalance(donor);66    const sponsor = await helper.eth.createAccountWithBalance(donor);67    const caller = await helper.eth.createAccountWithBalance(donor);68    const originalCallerBalance = await helper.balance.getEthereum(caller);6970    expect(originalCallerBalance).to.be.not.equal(0n);7172    const collector = await helper.eth.deployCollectorContract(owner);7374    const helpers = await helper.ethNativeContract.contractHelpers(owner);7576    await helpers.methods.toggleAllowlist(collector.options.address, true).send({from: owner});77    await helpers.methods.toggleAllowed(collector.options.address, caller, true).send({from: owner});7879    expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.false;80    await helpers.methods.setSponsoringMode(collector.options.address, SponsoringMode.Allowlisted).send({from: owner});81    await helpers.methods.setSponsoringRateLimit(collector.options.address, 0).send({from: owner});82    expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.true;8384    await helpers.methods.setSponsor(collector.options.address, sponsor).send({from: owner});85    await helpers.methods.confirmSponsorship(collector.options.address).send({from: sponsor});8687    const originalSponsorBalance = await helper.balance.getEthereum(sponsor);88    expect(originalSponsorBalance).to.be.not.equal(0n);8990    await collector.methods.giveMoney().send({from: caller, value: '10000'});9192    // Balance will be taken from both caller (value) and from collector (fee)93    expect(await helper.balance.getEthereum(caller)).to.be.equals((originalCallerBalance - 10000n));94    expect(await helper.balance.getEthereum(sponsor)).to.be.not.equals(originalSponsorBalance);95    expect(await collector.methods.getCollected().call()).to.be.equal('10000');96  });97});