git.delta.rocks / unique-network / refs/commits / 55c4d052d75c

difftreelog

source

tests/src/eth/sponsoring.test.ts4.4 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 {expect} from 'chai';18import privateKey from '../substrate/privateKey';19import {contractHelpers, createEthAccount, createEthAccountWithBalance, deployCollector, deployFlipper, itWeb3, SponsoringMode, transferBalanceToEth} from './util/helpers';2021describe('EVM sponsoring', () => {22  itWeb3('Fee is deducted from contract if sponsoring is enabled', async ({api, web3}) => {23    const alice = privateKey('//Alice');2425    const owner = await createEthAccountWithBalance(api, web3);26    const caller = createEthAccount(web3);27    const originalCallerBalance = await web3.eth.getBalance(caller);28    expect(originalCallerBalance).to.be.equal('0');2930    const flipper = await deployFlipper(web3, owner);3132    const helpers = contractHelpers(web3, owner);33    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});34    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});3536    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;37    await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});38    await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});39    expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;4041    await transferBalanceToEth(api, alice, flipper.options.address);4243    const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);44    expect(originalFlipperBalance).to.be.not.equal('0');4546    await flipper.methods.flip().send({from: caller});47    expect(await flipper.methods.getValue().call()).to.be.true;4849    // Balance should be taken from flipper instead of caller50    expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);51    expect(await web3.eth.getBalance(flipper.options.address)).to.be.not.equals(originalFlipperBalance);52  });53  itWeb3('...but this doesn\'t applies to payable value', async ({api, web3}) => {54    const alice = privateKey('//Alice');5556    const owner = await createEthAccountWithBalance(api, web3);57    const caller = await createEthAccountWithBalance(api, web3);58    const originalCallerBalance = await web3.eth.getBalance(caller);59    expect(originalCallerBalance).to.be.not.equal('0');6061    const collector = await deployCollector(web3, owner);6263    const helpers = contractHelpers(web3, owner);64    await helpers.methods.toggleAllowlist(collector.options.address, true).send({from: owner});65    await helpers.methods.toggleAllowed(collector.options.address, caller, true).send({from: owner});6667    expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.false;68    await helpers.methods.setSponsoringMode(collector.options.address, SponsoringMode.Allowlisted).send({from: owner});69    await helpers.methods.setSponsoringRateLimit(collector.options.address, 0).send({from: owner});70    expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.true;7172    await transferBalanceToEth(api, alice, collector.options.address);7374    const originalCollectorBalance = await web3.eth.getBalance(collector.options.address);75    expect(originalCollectorBalance).to.be.not.equal('0');7677    await collector.methods.giveMoney().send({from: caller, value: '10000'});7879    // Balance will be taken from both caller (value) and from collector (fee)80    expect(await web3.eth.getBalance(caller)).to.be.equals((BigInt(originalCallerBalance) - 10000n).toString());81    expect(await web3.eth.getBalance(collector.options.address)).to.be.not.equals(originalCollectorBalance);82    expect(await collector.methods.getCollected().call()).to.be.equal('10000');83  });84});