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

difftreelog

source

tests/src/eth/collectionAdmin.test.ts19.4 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.3// Unique Network is free software: you can redistribute it and/or modify4// it under the terms of the GNU General Public License as published by5// the Free Software Foundation, either version 3 of the License, or6// (at your option) any later version.7//8// Unique Network is distributed in the hope that it will be useful,9// but WITHOUT ANY WARRANTY; without even the implied warranty of10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the11// GNU General Public License for more details.1213// You should have received a copy of the GNU General Public License14// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1516import {IKeyringPair} from '@polkadot/types/types';17import privateKey from '../substrate/privateKey';18import {UNIQUE} from '../util/helpers';19import {20  createEthAccount,21  createEthAccountWithBalance,22  evmCollection,23  evmCollectionHelpers,24  getCollectionAddressFromResult,25  itWeb3,26  recordEthFee,27} from './util/helpers';28import {usingEthPlaygrounds, itEth, expect} from './util/playgrounds';2930describe('Add collection admins', () => {31  let donor: IKeyringPair;3233  before(async function() {34    await usingEthPlaygrounds(async (_helper, privateKey) => {35      donor = privateKey('//Alice');36    });37  });3839  itEth('Add admin by owner', async ({helper}) => {40    const owner = await helper.eth.createAccountWithBalance(donor);41    const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');42    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);4344    const newAdmin = helper.eth.createAccount();4546    await collectionEvm.methods.addCollectionAdmin(newAdmin).send();47    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);48    expect(adminList[0].asEthereum.toString().toLocaleLowerCase())49      .to.be.eq(newAdmin.toLocaleLowerCase());50  });5152  itEth('Add substrate admin by owner', async ({helper}) => {53    const owner = await helper.eth.createAccountWithBalance(donor);54    const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');55    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);5657    const [newAdmin] = await helper.arrange.createAccounts([10n], donor);58    await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();5960    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);61    expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())62      .to.be.eq(newAdmin.address.toLocaleLowerCase());63  });6465  itWeb3('Verify owner or admin', async ({api, web3, privateKeyWrapper}) => {66    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);67    const collectionHelper = evmCollectionHelpers(web3, owner);6869    const result = await collectionHelper.methods70      .createNonfungibleCollection('A', 'B', 'C')71      .send();72    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);7374    const newAdmin = createEthAccount(web3);75    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);76    expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.false;77    await collectionEvm.methods.addCollectionAdmin(newAdmin).send();78    expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;79  });8081  itWeb3('(!negative tests!) Add admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {82    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);83    const collectionHelper = evmCollectionHelpers(web3, owner);8485    const result = await collectionHelper.methods86      .createNonfungibleCollection('A', 'B', 'C')87      .send();88    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);8990    const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);91    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);92    await collectionEvm.methods.addCollectionAdmin(admin).send();9394    const user = createEthAccount(web3);95    await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: admin}))96      .to.be.rejectedWith('NoPermission');9798    const adminList = await api.rpc.unique.adminlist(collectionId);99    expect(adminList.length).to.be.eq(1);100    expect(adminList[0].asEthereum.toString().toLocaleLowerCase())101      .to.be.eq(admin.toLocaleLowerCase());102  });103104  itWeb3('(!negative tests!) Add admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {105    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);106    const collectionHelper = evmCollectionHelpers(web3, owner);107108    const result = await collectionHelper.methods109      .createNonfungibleCollection('A', 'B', 'C')110      .send();111    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);112113    const notAdmin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);114    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);115116    const user = createEthAccount(web3);117    await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: notAdmin}))118      .to.be.rejectedWith('NoPermission');119120    const adminList = await api.rpc.unique.adminlist(collectionId);121    expect(adminList.length).to.be.eq(0);122  });123124  itWeb3('(!negative tests!) Add substrate admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {125    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);126    const collectionHelper = evmCollectionHelpers(web3, owner);127128    const result = await collectionHelper.methods129      .createNonfungibleCollection('A', 'B', 'C')130      .send();131    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);132133    const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);134    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);135    await collectionEvm.methods.addCollectionAdmin(admin).send();136137    const notAdmin = privateKey('//Alice');138    await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin.addressRaw).call({from: admin}))139      .to.be.rejectedWith('NoPermission');140141    const adminList = await api.rpc.unique.adminlist(collectionId);142    expect(adminList.length).to.be.eq(1);143    expect(adminList[0].asEthereum.toString().toLocaleLowerCase())144      .to.be.eq(admin.toLocaleLowerCase());145  });146147  itWeb3('(!negative tests!) Add substrate admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {148    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);149    const collectionHelper = evmCollectionHelpers(web3, owner);150151    const result = await collectionHelper.methods152      .createNonfungibleCollection('A', 'B', 'C')153      .send();154    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);155156    const notAdmin0 = await createEthAccountWithBalance(api, web3, privateKeyWrapper);157    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);158    const notAdmin1 = privateKey('//Alice');159    await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin1.addressRaw).call({from: notAdmin0}))160      .to.be.rejectedWith('NoPermission');161162    const adminList = await api.rpc.unique.adminlist(collectionId);163    expect(adminList.length).to.be.eq(0);164  });165});166167describe('Remove collection admins', () => {168  itWeb3('Remove admin by owner', async ({api, web3, privateKeyWrapper}) => {169    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);170    const collectionHelper = evmCollectionHelpers(web3, owner);171172    const result = await collectionHelper.methods173      .createNonfungibleCollection('A', 'B', 'C')174      .send();175    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);176177    const newAdmin = createEthAccount(web3);178    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);179    await collectionEvm.methods.addCollectionAdmin(newAdmin).send();180    {181      const adminList = await api.rpc.unique.adminlist(collectionId);182      expect(adminList.length).to.be.eq(1);183      expect(adminList[0].asEthereum.toString().toLocaleLowerCase())184        .to.be.eq(newAdmin.toLocaleLowerCase());185    }186187    await collectionEvm.methods.removeCollectionAdmin(newAdmin).send();188    const adminList = await api.rpc.unique.adminlist(collectionId);189    expect(adminList.length).to.be.eq(0);190  });191192  itWeb3('Remove substrate admin by owner', async ({api, web3, privateKeyWrapper}) => {193    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);194    const collectionHelper = evmCollectionHelpers(web3, owner);195196    const result = await collectionHelper.methods197      .createNonfungibleCollection('A', 'B', 'C')198      .send();199    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);200201    const newAdmin = privateKeyWrapper('//Alice');202    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);203    await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();204    {205      const adminList = await api.rpc.unique.adminlist(collectionId);206      expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())207        .to.be.eq(newAdmin.address.toLocaleLowerCase());208    }209210    await collectionEvm.methods.removeCollectionAdminSubstrate(newAdmin.addressRaw).send();211    const adminList = await api.rpc.unique.adminlist(collectionId);212    expect(adminList.length).to.be.eq(0);213  });214215  itWeb3('(!negative tests!) Remove admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {216    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);217    const collectionHelper = evmCollectionHelpers(web3, owner);218219    const result = await collectionHelper.methods220      .createNonfungibleCollection('A', 'B', 'C')221      .send();222    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);223224    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);225226    const admin0 = await createEthAccountWithBalance(api, web3, privateKeyWrapper);227    await collectionEvm.methods.addCollectionAdmin(admin0).send();228    const admin1 = createEthAccount(web3);229    await collectionEvm.methods.addCollectionAdmin(admin1).send();230231    await expect(collectionEvm.methods.removeCollectionAdmin(admin1).call({from: admin0}))232      .to.be.rejectedWith('NoPermission');233    {234      const adminList = await api.rpc.unique.adminlist(collectionId);235      expect(adminList.length).to.be.eq(2);236      expect(adminList.toString().toLocaleLowerCase())237        .to.be.deep.contains(admin0.toLocaleLowerCase())238        .to.be.deep.contains(admin1.toLocaleLowerCase());239    }240  });241242  itWeb3('(!negative tests!) Remove admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {243    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);244    const collectionHelper = evmCollectionHelpers(web3, owner);245246    const result = await collectionHelper.methods247      .createNonfungibleCollection('A', 'B', 'C')248      .send();249    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);250251    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);252253    const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);254    await collectionEvm.methods.addCollectionAdmin(admin).send();255    const notAdmin = createEthAccount(web3);256257    await expect(collectionEvm.methods.removeCollectionAdmin(admin).call({from: notAdmin}))258      .to.be.rejectedWith('NoPermission');259    {260      const adminList = await api.rpc.unique.adminlist(collectionId);261      expect(adminList[0].asEthereum.toString().toLocaleLowerCase())262        .to.be.eq(admin.toLocaleLowerCase());263      expect(adminList.length).to.be.eq(1);264    }265  });266267  itWeb3('(!negative tests!) Remove substrate admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {268    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);269    const collectionHelper = evmCollectionHelpers(web3, owner);270271    const result = await collectionHelper.methods272      .createNonfungibleCollection('A', 'B', 'C')273      .send();274    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);275276    const adminSub = privateKeyWrapper('//Alice');277    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);278    await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();279    const adminEth = await createEthAccountWithBalance(api, web3, privateKeyWrapper);280    await collectionEvm.methods.addCollectionAdmin(adminEth).send();281282    await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: adminEth}))283      .to.be.rejectedWith('NoPermission');284285    const adminList = await api.rpc.unique.adminlist(collectionId);286    expect(adminList.length).to.be.eq(2);287    expect(adminList.toString().toLocaleLowerCase())288      .to.be.deep.contains(adminSub.address.toLocaleLowerCase())289      .to.be.deep.contains(adminEth.toLocaleLowerCase());290  });291292  itWeb3('(!negative tests!) Remove substrate admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {293    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);294    const collectionHelper = evmCollectionHelpers(web3, owner);295296    const result = await collectionHelper.methods297      .createNonfungibleCollection('A', 'B', 'C')298      .send();299    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);300301    const adminSub = privateKeyWrapper('//Alice');302    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);303    await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();304    const notAdminEth = await createEthAccountWithBalance(api, web3, privateKeyWrapper);305306    await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: notAdminEth}))307      .to.be.rejectedWith('NoPermission');308309    const adminList = await api.rpc.unique.adminlist(collectionId);310    expect(adminList.length).to.be.eq(1);311    expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())312      .to.be.eq(adminSub.address.toLocaleLowerCase());313  });314});315316describe('Change owner tests', () => {317  itWeb3('Change owner', async ({api, web3, privateKeyWrapper}) => {318    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);319    const newOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);320    const collectionHelper = evmCollectionHelpers(web3, owner);321    const result = await collectionHelper.methods322      .createNonfungibleCollection('A', 'B', 'C')323      .send();324    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);325    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);326327    await collectionEvm.methods.setOwner(newOwner).send();328329    expect(await collectionEvm.methods.isOwnerOrAdmin(owner).call()).to.be.false;330    expect(await collectionEvm.methods.isOwnerOrAdmin(newOwner).call()).to.be.true;331  });332333  itWeb3('change owner call fee', async ({web3, api, privateKeyWrapper}) => {334    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);335    const newOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);336    const collectionHelper = evmCollectionHelpers(web3, owner);337    const result = await collectionHelper.methods338      .createNonfungibleCollection('A', 'B', 'C')339      .send();340    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);341    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);342343    const cost = await recordEthFee(api, owner, () => collectionEvm.methods.setOwner(newOwner).send());344    expect(cost < BigInt(0.2 * Number(UNIQUE)));345    expect(cost > 0);346  });347348  itWeb3('(!negative tests!) call setOwner by non owner', async ({api, web3, privateKeyWrapper}) => {349    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);350    const newOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);351    const collectionHelper = evmCollectionHelpers(web3, owner);352    const result = await collectionHelper.methods353      .createNonfungibleCollection('A', 'B', 'C')354      .send();355    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);356    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);357358    await expect(collectionEvm.methods.setOwner(newOwner).send({from: newOwner})).to.be.rejected;359    expect(await collectionEvm.methods.isOwnerOrAdmin(newOwner).call()).to.be.false;360  });361});362363describe('Change substrate owner tests', () => {364  itWeb3('Change owner', async ({api, web3, privateKeyWrapper}) => {365    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);366    const newOwner = privateKeyWrapper('//Alice');367    const collectionHelper = evmCollectionHelpers(web3, owner);368    const result = await collectionHelper.methods369      .createNonfungibleCollection('A', 'B', 'C')370      .send();371    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);372    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);373374    expect(await collectionEvm.methods.isOwnerOrAdmin(owner).call()).to.be.true;375    expect(await collectionEvm.methods.isOwnerOrAdminSubstrate(newOwner.addressRaw).call()).to.be.false;376377    await collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send();378379    expect(await collectionEvm.methods.isOwnerOrAdmin(owner).call()).to.be.false;380    expect(await collectionEvm.methods.isOwnerOrAdminSubstrate(newOwner.addressRaw).call()).to.be.true;381  });382383  itWeb3('change owner call fee', async ({web3, api, privateKeyWrapper}) => {384    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);385    const newOwner = privateKeyWrapper('//Alice');386    const collectionHelper = evmCollectionHelpers(web3, owner);387    const result = await collectionHelper.methods388      .createNonfungibleCollection('A', 'B', 'C')389      .send();390    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);391    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);392393    const cost = await recordEthFee(api, owner, () => collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send());394    expect(cost < BigInt(0.2 * Number(UNIQUE)));395    expect(cost > 0);396  });397398  itWeb3('(!negative tests!) call setOwner by non owner', async ({api, web3, privateKeyWrapper}) => {399    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);400    const otherReceiver = await createEthAccountWithBalance(api, web3, privateKeyWrapper);401    const newOwner = privateKeyWrapper('//Alice');402    const collectionHelper = evmCollectionHelpers(web3, owner);403    const result = await collectionHelper.methods404      .createNonfungibleCollection('A', 'B', 'C')405      .send();406    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);407    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);408409    await expect(collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send({from: otherReceiver})).to.be.rejected;410    expect(await collectionEvm.methods.isOwnerOrAdminSubstrate(newOwner.addressRaw).call()).to.be.false;411  });412});