git.delta.rocks / unique-network / refs/commits / 7781ab982a4b

difftreelog

source

tests/src/toggleContractAllowList.test.ts7.1 KiBsourcehistory
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import chai from 'chai';7import chaiAsPromised from 'chai-as-promised';8import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';9import privateKey from './substrate/privateKey';10import {11  deployFlipper,12  getFlipValue,13} from './util/contracthelpers';14import {15  getGenericResult,16} from './util/helpers';1718chai.use(chaiAsPromised);19const expect = chai.expect;2021const value = 0;22const gasLimit = 3000n * 1000000n;2324describe.skip('Integration Test toggleContractAllowList', () => {2526  it('Enable allow list contract mode', async () => {27    await usingApi(async api => {28      const [contract, deployer] = await deployFlipper(api);2930      const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();31      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);32      const enableEvents = await submitTransactionAsync(deployer, enableAllowListTx);33      const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();3435      expect(getGenericResult(enableEvents).success).to.be.true;36      expect(enabledBefore).to.be.false;37      expect(enabled).to.be.true;38    });39  });4041  it('Only allowlisted account can call contract', async () => {42    await usingApi(async api => {43      const bob = privateKey('//Bob');4445      const [contract, deployer] = await deployFlipper(api);4647      let flipValueBefore = await getFlipValue(contract, deployer);48      const flip = contract.tx.flip(value, gasLimit);49      await submitTransactionAsync(bob, flip);50      const flipValueAfter = await getFlipValue(contract,deployer);51      expect(flipValueAfter).to.be.eq(!flipValueBefore, 'Anyone can call new contract.');5253      const deployerCanFlip = async () => {54        const flipValueBefore = await getFlipValue(contract, deployer);55        const deployerFlip = contract.tx.flip(value, gasLimit);56        await submitTransactionAsync(deployer, deployerFlip);57        const aliceFlip1Response = await getFlipValue(contract, deployer);58        expect(aliceFlip1Response).to.be.eq(!flipValueBefore, 'Deployer always can flip.');59      };60      await deployerCanFlip();6162      flipValueBefore = await getFlipValue(contract, deployer);63      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);64      await submitTransactionAsync(deployer, enableAllowListTx);65      const flipWithEnabledAllowList = contract.tx.flip(value, gasLimit);66      await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledAllowList)).to.be.rejected;67      const flipValueAfterEnableAllowList = await getFlipValue(contract, deployer);68      expect(flipValueAfterEnableAllowList).to.be.eq(flipValueBefore, 'Enabling allowlist doesn\'t make it possible to call contract for everyone.');6970      await deployerCanFlip();7172      flipValueBefore = await getFlipValue(contract, deployer);73      const addBobToAllowListTx = api.tx.unique.addToContractAllowList(contract.address, bob.address);74      await submitTransactionAsync(deployer, addBobToAllowListTx);75      const flipWithAllowlistedBob = contract.tx.flip(value, gasLimit);76      await submitTransactionAsync(bob, flipWithAllowlistedBob);77      const flipAfterAllowListed = await getFlipValue(contract,deployer);78      expect(flipAfterAllowListed).to.be.eq(!flipValueBefore, 'Bob was allowlisted, now he can flip.');7980      await deployerCanFlip();8182      flipValueBefore = await getFlipValue(contract, deployer);83      const removeBobFromAllowListTx = api.tx.unique.removeFromContractAllowList(contract.address, bob.address);84      await submitTransactionAsync(deployer, removeBobFromAllowListTx);85      const bobRemoved = contract.tx.flip(value, gasLimit);86      await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;87      const afterBobRemoved = await getFlipValue(contract, deployer);88      expect(afterBobRemoved).to.be.eq(flipValueBefore, 'Bob can\'t call contract, now when he is removeed from allow list.');8990      await deployerCanFlip();9192      flipValueBefore = await getFlipValue(contract, deployer);93      const disableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, false);94      await submitTransactionAsync(deployer, disableAllowListTx);95      const allowListDisabledFlip = contract.tx.flip(value, gasLimit);96      await submitTransactionAsync(bob, allowListDisabledFlip);97      const afterAllowListDisabled = await getFlipValue(contract,deployer);98      expect(afterAllowListDisabled).to.be.eq(!flipValueBefore, 'Anyone can call contract with disabled allowlist.');99100    });101  });102103  it('Enabling allow list repeatedly should not produce errors', async () => {104    await usingApi(async api => {105      const [contract, deployer] = await deployFlipper(api);106107      const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();108      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);109      const enableEvents = await submitTransactionAsync(deployer, enableAllowListTx);110      const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();111      const enableAgainEvents = await submitTransactionAsync(deployer, enableAllowListTx);112      const enabledAgain = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();113114      expect(getGenericResult(enableEvents).success).to.be.true;115      expect(enabledBefore).to.be.false;116      expect(enabled).to.be.true;117      expect(getGenericResult(enableAgainEvents).success).to.be.true;118      expect(enabledAgain).to.be.true;119    });120  });121122});123124describe.skip('Negative Integration Test toggleContractAllowList', () => {125126  it('Enable allow list for a non-contract', async () => {127    await usingApi(async api => {128      const alice = privateKey('//Alice');129      const bobGuineaPig = privateKey('//Bob');130131      const enabledBefore = (await api.query.unique.contractAllowListEnabled(bobGuineaPig.address)).toJSON();132      const enableAllowListTx = api.tx.unique.toggleContractAllowList(bobGuineaPig.address, true);133      await expect(submitTransactionExpectFailAsync(alice, enableAllowListTx)).to.be.rejected;134      const enabled = (await api.query.unique.contractAllowListEnabled(bobGuineaPig.address)).toJSON();135136      expect(enabledBefore).to.be.false;137      expect(enabled).to.be.false;138    });139  });140141  it('Enable allow list using a non-owner address', async () => {142    await usingApi(async api => {143      const bob = privateKey('//Bob');144      const [contract] = await deployFlipper(api);145146      const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();147      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);148      await expect(submitTransactionExpectFailAsync(bob, enableAllowListTx)).to.be.rejected;149      const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();150151      expect(enabledBefore).to.be.false;152      expect(enabled).to.be.false;153    });154  });155156});