1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect} from 'chai';19import {isAllowlisted, normalizeAccountId} from '../util/helpers';20import {21 contractHelpers,22 createEthAccount,23 createEthAccountWithBalance,24 deployFlipper,25 evmCollection,26 evmCollectionHelpers,27 getCollectionAddressFromResult,28 itWeb3,29} from './util/helpers';30import {itEth, usingEthPlaygrounds} from './util/playgrounds';3132describe('EVM contract allowlist', () => {33 itWeb3('Contract allowlist can be toggled', async ({api, web3, privateKeyWrapper}) => {34 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);35 const flipper = await deployFlipper(web3, owner);3637 const helpers = contractHelpers(web3, owner);3839 40 expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;4142 43 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});44 expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.true;4546 47 await helpers.methods.toggleAllowlist(flipper.options.address, false).send({from: owner});48 expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;49 });5051 itWeb3('Non-allowlisted user can\'t call contract with allowlist enabled', async ({api, web3, privateKeyWrapper}) => {52 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);53 const flipper = await deployFlipper(web3, owner);54 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);5556 const helpers = contractHelpers(web3, owner);5758 59 await flipper.methods.flip().send({from: caller});60 expect(await flipper.methods.getValue().call()).to.be.true;6162 63 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});64 await expect(flipper.methods.flip().send({from: caller})).to.rejected;65 expect(await flipper.methods.getValue().call()).to.be.true;6667 68 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});69 await flipper.methods.flip().send({from: caller});70 expect(await flipper.methods.getValue().call()).to.be.false;71 });72});7374describe('EVM collection allowlist', () => {75 let donor: IKeyringPair;7677 before(async function() {78 await usingEthPlaygrounds(async (_helper, privateKey) => {79 donor = privateKey('//Alice');80 });81 });82 83 itEth('Collection allowlist can be added and removed by [eth] address', async ({helper}) => {84 const owner = await helper.eth.createAccountWithBalance(donor);85 const user = helper.eth.createAccount();86 87 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');88 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);89 90 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;91 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});92 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;93 94 await collectionEvm.methods.removeFromCollectionAllowList(user).send({from: owner});95 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;96 });9798 itEth('Collection allowlist can be added and removed by [sub] address', async ({helper}) => {99 const owner = await helper.eth.createAccountWithBalance(donor);100 const user = donor;101 102 const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');103 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);104 105 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;106 await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});107 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;108 109 await collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).send({from: owner});110 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;111 });112113 itEth('Collection allowlist can not be add and remove [eth] address by not owner', async ({helper}) => {114 const owner = await helper.eth.createAccountWithBalance(donor);115 const notOwner = await helper.eth.createAccountWithBalance(donor);116 const user = helper.eth.createAccount();117 118 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');119 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);120 121 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;122 await expect(collectionEvm.methods.addToCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');123 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;124 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});125 126 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;127 await expect(collectionEvm.methods.removeFromCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');128 expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;129 });130131 itEth('Collection allowlist can not be add and remove [sub] address by not owner', async ({helper}) => {132 const owner = await helper.eth.createAccountWithBalance(donor);133 const notOwner = await helper.eth.createAccountWithBalance(donor);134 const user = donor;135 136 const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');137 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);138 139 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;140 await expect(collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');141 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;142 await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});143 144 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;145 await expect(collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');146 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;147 });148});