1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect} from 'chai';19import {isAllowlisted, normalizeAccountId} from '../util/helpers';20import {21 contractHelpers,22 createEthAccountWithBalance,23 deployFlipper,24 itWeb3,25} from './util/helpers';26import {itEth, usingEthPlaygrounds} from './util/playgrounds';2728describe('EVM contract allowlist', () => {29 itWeb3('Contract allowlist can be toggled', async ({api, web3, privateKeyWrapper}) => {30 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);31 const flipper = await deployFlipper(web3, owner);3233 const helpers = contractHelpers(web3, owner);3435 36 expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;3738 39 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});40 expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.true;4142 43 await helpers.methods.toggleAllowlist(flipper.options.address, false).send({from: owner});44 expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;45 });4647 itWeb3('Non-allowlisted user can\'t call contract with allowlist enabled', async ({api, web3, privateKeyWrapper}) => {48 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);49 const flipper = await deployFlipper(web3, owner);50 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);5152 const helpers = contractHelpers(web3, owner);5354 55 await flipper.methods.flip().send({from: caller});56 expect(await flipper.methods.getValue().call()).to.be.true;5758 59 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});60 await expect(flipper.methods.flip().send({from: caller})).to.rejected;61 expect(await flipper.methods.getValue().call()).to.be.true;6263 64 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});65 await flipper.methods.flip().send({from: caller});66 expect(await flipper.methods.getValue().call()).to.be.false;67 });68});6970describe('EVM collection allowlist', () => {71 let donor: IKeyringPair;7273 before(async function() {74 await usingEthPlaygrounds(async (_helper, privateKey) => {75 donor = privateKey('//Alice');76 });77 });78 79 itEth('Collection allowlist can be added and removed by [eth] address', async ({helper}) => {80 const owner = await helper.eth.createAccountWithBalance(donor);81 const user = helper.eth.createAccount();82 83 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');84 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);85 86 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;87 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});88 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;89 90 await collectionEvm.methods.removeFromCollectionAllowList(user).send({from: owner});91 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;92 });9394 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109110 itEth('Collection allowlist can not be add and remove [eth] address by not owner', async ({helper}) => {111 const owner = await helper.eth.createAccountWithBalance(donor);112 const notOwner = await helper.eth.createAccountWithBalance(donor);113 const user = helper.eth.createAccount();114 115 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');116 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);117 118 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;119 await expect(collectionEvm.methods.addToCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');120 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;121 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});122 123 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;124 await expect(collectionEvm.methods.removeFromCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');125 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;126 });127128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146});