1234567891011121314151617import chai from 'chai';18import chaiAsPromised from 'chai-as-promised';19import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from '../substrate/substrate-api';20import {21 deployFlipper,22 getFlipValue,23} from '../deprecated-helpers/contracthelpers';24import {25 getGenericResult,26} from '../deprecated-helpers/helpers';2728chai.use(chaiAsPromised);29const expect = chai.expect;3031const value = 0;32const gasLimit = 3000n * 1000000n;333435describe.skip('Integration Test toggleContractAllowList', () => {3637 it('Enable allow list contract mode', async () => {38 await usingApi(async (api, privateKeyWrapper) => {39 const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);4041 const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();42 const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);43 const enableEvents = await submitTransactionAsync(deployer, enableAllowListTx);44 const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();4546 expect(getGenericResult(enableEvents).success).to.be.true;47 expect(enabledBefore).to.be.false;48 expect(enabled).to.be.true;49 });50 });5152 it('Only allowlisted account can call contract', async () => {53 await usingApi(async (api, privateKeyWrapper) => {54 const bob = privateKeyWrapper('//Bob');5556 const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);5758 let flipValueBefore = await getFlipValue(contract, deployer);59 const flip = contract.tx.flip({value, gasLimit});60 await submitTransactionAsync(bob, flip);61 const flipValueAfter = await getFlipValue(contract,deployer);62 expect(flipValueAfter).to.be.eq(!flipValueBefore, 'Anyone can call new contract.');6364 const deployerCanFlip = async () => {65 const flipValueBefore = await getFlipValue(contract, deployer);66 const deployerFlip = contract.tx.flip({value, gasLimit});67 await submitTransactionAsync(deployer, deployerFlip);68 const aliceFlip1Response = await getFlipValue(contract, deployer);69 expect(aliceFlip1Response).to.be.eq(!flipValueBefore, 'Deployer always can flip.');70 };71 await deployerCanFlip();7273 flipValueBefore = await getFlipValue(contract, deployer);74 const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);75 await submitTransactionAsync(deployer, enableAllowListTx);76 const flipWithEnabledAllowList = contract.tx.flip({value, gasLimit});77 await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledAllowList)).to.be.rejected;78 const flipValueAfterEnableAllowList = await getFlipValue(contract, deployer);79 expect(flipValueAfterEnableAllowList).to.be.eq(flipValueBefore, 'Enabling allowlist doesn\'t make it possible to call contract for everyone.');8081 await deployerCanFlip();8283 flipValueBefore = await getFlipValue(contract, deployer);84 const addBobToAllowListTx = api.tx.unique.addToContractAllowList(contract.address, bob.address);85 await submitTransactionAsync(deployer, addBobToAllowListTx);86 const flipWithAllowlistedBob = contract.tx.flip({value, gasLimit});87 await submitTransactionAsync(bob, flipWithAllowlistedBob);88 const flipAfterAllowListed = await getFlipValue(contract,deployer);89 expect(flipAfterAllowListed).to.be.eq(!flipValueBefore, 'Bob was allowlisted, now he can flip.');9091 await deployerCanFlip();9293 flipValueBefore = await getFlipValue(contract, deployer);94 const removeBobFromAllowListTx = api.tx.unique.removeFromContractAllowList(contract.address, bob.address);95 await submitTransactionAsync(deployer, removeBobFromAllowListTx);96 const bobRemoved = contract.tx.flip({value, gasLimit});97 await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;98 const afterBobRemoved = await getFlipValue(contract, deployer);99 expect(afterBobRemoved).to.be.eq(flipValueBefore, 'Bob can\'t call contract, now when he is removeed from allow list.');100101 await deployerCanFlip();102103 flipValueBefore = await getFlipValue(contract, deployer);104 const disableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, false);105 await submitTransactionAsync(deployer, disableAllowListTx);106 const allowListDisabledFlip = contract.tx.flip({value, gasLimit});107 await submitTransactionAsync(bob, allowListDisabledFlip);108 const afterAllowListDisabled = await getFlipValue(contract,deployer);109 expect(afterAllowListDisabled).to.be.eq(!flipValueBefore, 'Anyone can call contract with disabled allowlist.');110111 });112 });113114 it('Enabling allow list repeatedly should not produce errors', async () => {115 await usingApi(async (api, privateKeyWrapper) => {116 const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);117118 const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();119 const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);120 const enableEvents = await submitTransactionAsync(deployer, enableAllowListTx);121 const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();122 const enableAgainEvents = await submitTransactionAsync(deployer, enableAllowListTx);123 const enabledAgain = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();124125 expect(getGenericResult(enableEvents).success).to.be.true;126 expect(enabledBefore).to.be.false;127 expect(enabled).to.be.true;128 expect(getGenericResult(enableAgainEvents).success).to.be.true;129 expect(enabledAgain).to.be.true;130 });131 });132133});134135describe.skip('Negative Integration Test toggleContractAllowList', () => {136137 it('Enable allow list for a non-contract', async () => {138 await usingApi(async (api, privateKeyWrapper) => {139 const alice = privateKeyWrapper('//Alice');140 const bobGuineaPig = privateKeyWrapper('//Bob');141142 const enabledBefore = (await api.query.unique.contractAllowListEnabled(bobGuineaPig.address)).toJSON();143 const enableAllowListTx = api.tx.unique.toggleContractAllowList(bobGuineaPig.address, true);144 await expect(submitTransactionExpectFailAsync(alice, enableAllowListTx)).to.be.rejected;145 const enabled = (await api.query.unique.contractAllowListEnabled(bobGuineaPig.address)).toJSON();146147 expect(enabledBefore).to.be.false;148 expect(enabled).to.be.false;149 });150 });151152 it('Enable allow list using a non-owner address', async () => {153 await usingApi(async (api, privateKeyWrapper) => {154 const bob = privateKeyWrapper('//Bob');155 const [contract] = await deployFlipper(api, privateKeyWrapper);156157 const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();158 const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);159 await expect(submitTransactionExpectFailAsync(bob, enableAllowListTx)).to.be.rejected;160 const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();161162 expect(enabledBefore).to.be.false;163 expect(enabled).to.be.false;164 });165 });166167});