git.delta.rocks / unique-network / refs/commits / ea8fa39bcf35

difftreelog

source

tests/src/eth/allowlist.test.ts7.6 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 {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    // Any user is allowed by default36    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;3738    // Enable39    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    // Disable43    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    // User can flip with allowlist disabled55    await flipper.methods.flip().send({from: caller});56    expect(await flipper.methods.getValue().call()).to.be.true;5758    // Tx will be reverted if user is not in allowlist59    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    // Adding caller to allowlist will make contract callable again64    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  // TODO: Temprorary off. Need refactor95  // itEth('Collection allowlist can be added and removed by [sub] address', async ({helper}) => {96  //   const owner = await helper.eth.createAccountWithBalance(donor);97  //   const user = donor;98    99  //   const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');100  //   const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);101    102  //   expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;103  //   await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});104  //   expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;105    106  //   await collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).send({from: owner});107  //   expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;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  // TODO: Temprorary off. Need refactor129  // itEth('Collection allowlist can not be add and remove [sub] address by not owner', async ({helper}) => {130  //   const owner = await helper.eth.createAccountWithBalance(donor);131  //   const notOwner = await helper.eth.createAccountWithBalance(donor);132  //   const user = donor;133    134  //   const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');135  //   const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);136    137  //   expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;138  //   await expect(collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');139  //   expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;140  //   await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});141    142  //   expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;143  //   await expect(collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');144  //   expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;145  // });146});