1234567891011121314151617import {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 35 expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;3637 38 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 42 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 53 await flipper.methods.flip().send({from: caller});54 expect(await flipper.methods.getValue().call()).to.be.true;5556 57 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 62 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 78 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();81 const crossUser = helper.ethCrossAccount.fromAddress(user);82 83 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');84 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);8586 expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.false;87 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});88 expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.true;8990 await collectionEvm.methods.removeFromCollectionAllowList(user).send({from: owner});91 expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.false;92 });9394 itEth('Collection allowlist can be added and removed by [cross] address', async ({helper}) => {95 const owner = (await helper.eth.createAccountWithBalance(donor)).toLowerCase();96 const [userSub] = await helper.arrange.createAccounts([10n], donor);97 const userEth = await helper.eth.createAccountWithBalance(donor);98 99 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');100 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);101 const userCrossSub = helper.ethCrossAccount.fromKeyringPair(userSub);102 const userCrossEth = helper.ethCrossAccount.fromAddress(userEth);103 const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner);104 105 106 expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.false;107 await collectionEvm.methods.addToCollectionAllowListCross(userCrossSub).send({from: owner});108 await collectionEvm.methods.addToCollectionAllowListCross(userCrossEth).send({from: owner});109 await collectionEvm.methods.addToCollectionAllowListCross(ownerCrossEth).send({from: owner});110 expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.true;111 expect(await helper.collection.allowed(collectionId, {Ethereum: userEth})).to.be.true;112 expect(await collectionEvm.methods.allowlistedCross(userCrossSub).call({from: owner})).to.be.true;113 expect(await collectionEvm.methods.allowlistedCross(userCrossEth).call({from: owner})).to.be.true;114115 await collectionEvm.methods.mint(userEth).send(); 116 await collectionEvm.methods.mint(userEth).send(); 117 await collectionEvm.methods.setCollectionAccess(1).send();118 119 120 await collectionEvm.methods.transfer(owner, 1).send({from: userEth});121 await collectionEvm.methods.transferCross(userCrossSub, 2).send({from: userEth});122 expect(await helper.nft.getTokenOwner(collectionId, 1)).to.deep.eq({Ethereum: owner});123 expect(await helper.nft.getTokenOwner(collectionId, 2)).to.deep.eq({Substrate: userSub.address});124 125 126 await collectionEvm.methods.removeFromCollectionAllowListCross(userCrossSub).send({from: owner});127 await collectionEvm.methods.removeFromCollectionAllowListCross(userCrossEth).send({from: owner});128 expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.false;129 expect(await helper.collection.allowed(collectionId, {Ethereum: userEth})).to.be.false;130 expect(await collectionEvm.methods.allowlistedCross(userCrossSub).call({from: owner})).to.be.false;131 expect(await collectionEvm.methods.allowlistedCross(userCrossEth).call({from: owner})).to.be.false;132133 134 await collectionEvm.methods.mint(userEth).send();135 await expect(collectionEvm.methods.transfer(owner, 2).send({from: userEth})).to.be.rejectedWith(/Transaction has been reverted/);136 });137138 139 itEth('Collection allowlist can not be add and remove [eth] address by not owner', async ({helper}) => {140 const owner = await helper.eth.createAccountWithBalance(donor);141 const notOwner = await helper.eth.createAccountWithBalance(donor);142 const user = helper.eth.createAccount();143 const crossUser = helper.ethCrossAccount.fromAddress(user);144145 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');146 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);147148 expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.false;149 await expect(collectionEvm.methods.addToCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');150 expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.false;151 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});152 expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.true;153 await expect(collectionEvm.methods.removeFromCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');154 expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.true;155 });156157 itEth('Collection allowlist can not be add and remove [cross] address by not owner', async ({helper}) => {158 const owner = await helper.eth.createAccountWithBalance(donor);159 const notOwner = await helper.eth.createAccountWithBalance(donor);160 const user = donor;161 162 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');163 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);164 165 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;166 const userCross = helper.ethCrossAccount.fromKeyringPair(user);167 await expect(collectionEvm.methods.addToCollectionAllowListCross(userCross).call({from: notOwner})).to.be.rejectedWith('NoPermission');168 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;169 await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});170 171 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;172 await expect(collectionEvm.methods.removeFromCollectionAllowListCross(userCross).call({from: notOwner})).to.be.rejectedWith('NoPermission');173 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;174 });175});