git.delta.rocks / unique-network / refs/commits / 78a9ca040ab8

difftreelog

source

tests/src/eth/allowlist.test.ts7.5 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 {itEth, usingEthPlaygrounds, expect} from './util';1920describe('EVM contract allowlist', () => {21  let donor: IKeyringPair;2223  before(async function() {24    await usingEthPlaygrounds(async (_helper, privateKey) => {25      donor = await privateKey({filename: __filename});26    });27  });2829  itEth('Contract allowlist can be toggled', async ({helper}) => {30    const owner = await helper.eth.createAccountWithBalance(donor);31    const flipper = await helper.eth.deployFlipper(owner);32    const helpers = helper.ethNativeContract.contractHelpers(owner);3334    // Any user is allowed by default35    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;3637    // Enable38    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});39    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.true;4041    // Disable42    await helpers.methods.toggleAllowlist(flipper.options.address, false).send({from: owner});43    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;44  });4546  itEth('Non-allowlisted user can\'t call contract with allowlist enabled', async ({helper}) => {47    const owner = await helper.eth.createAccountWithBalance(donor);48    const caller = await helper.eth.createAccountWithBalance(donor);49    const flipper = await helper.eth.deployFlipper(owner);50    const helpers = helper.ethNativeContract.contractHelpers(owner);5152    // User can flip with allowlist disabled53    await flipper.methods.flip().send({from: caller});54    expect(await flipper.methods.getValue().call()).to.be.true;5556    // Tx will be reverted if user is not in allowlist57    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});58    await expect(flipper.methods.flip().send({from: caller})).to.rejected;59    expect(await flipper.methods.getValue().call()).to.be.true;6061    // Adding caller to allowlist will make contract callable again62    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});63    await flipper.methods.flip().send({from: caller});64    expect(await flipper.methods.getValue().call()).to.be.false;65  });66});6768describe('EVM collection allowlist', () => {69  let donor: IKeyringPair;7071  before(async function() {72    await usingEthPlaygrounds(async (_helper, privateKey) => {73      donor = await privateKey({filename: __filename});74    });75  });7677  // Soft-deprecated78  itEth('Collection allowlist can be added and removed by [eth] address', async ({helper}) => {79    const owner = await helper.eth.createAccountWithBalance(donor);80    const user = helper.eth.createAccount();8182    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');83    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);8485    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;86    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});87    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;8889    await collectionEvm.methods.removeFromCollectionAllowList(user).send({from: owner});90    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;91  });9293  itEth('Collection allowlist can be added and removed by [cross] address', async ({helper}) => {94    const owner = await helper.eth.createAccountWithBalance(donor);95    const user = donor;96    97    const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');98    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);99    const userCross = helper.ethCrossAccount.fromKeyringPair(user);100    101    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;102    await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});103    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;104    105    await collectionEvm.methods.removeFromCollectionAllowListCross(userCross).send({from: owner});106    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;107  });108109  // Soft-deprecated110  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();114115    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');116    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);117118    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    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;123    await expect(collectionEvm.methods.removeFromCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');124    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;125  });126127  itEth('Collection allowlist can not be add and remove [cross] address by not owner', async ({helper}) => {128    const owner = await helper.eth.createAccountWithBalance(donor);129    const notOwner = await helper.eth.createAccountWithBalance(donor);130    const user = donor;131    132    const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');133    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);134    135    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;136    const userCross = helper.ethCrossAccount.fromKeyringPair(user);137    await expect(collectionEvm.methods.addToCollectionAllowListCross(userCross).call({from: notOwner})).to.be.rejectedWith('NoPermission');138    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;139    await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});140    141    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;142    await expect(collectionEvm.methods.removeFromCollectionAllowListCross(userCross).call({from: notOwner})).to.be.rejectedWith('NoPermission');143    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;144  });145});