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 {expect} from 'chai';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';2829describe('Add collection admins', () => {30 itWeb3('Add admin by owner', async ({api, web3, privateKeyWrapper}) => {31 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);32 const collectionHelper = evmCollectionHelpers(web3, owner);3334 const result = await collectionHelper.methods35 .createNonfungibleCollection('A', 'B', 'C')36 .send({value: Number(2n * UNIQUE)});37 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);3839 const newAdmin = createEthAccount(web3);40 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);41 await collectionEvm.methods.addCollectionAdmin(newAdmin).send();42 const adminList = await api.rpc.unique.adminlist(collectionId);43 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())44 .to.be.eq(newAdmin.toLocaleLowerCase());45 });4647 // TODO: Temprorary off. Need refactor48 // itWeb3('Add substrate admin by owner', async ({api, web3, privateKeyWrapper}) => {49 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);50 // const collectionHelper = evmCollectionHelpers(web3, owner);51 52 // const result = await collectionHelper.methods53 // .createNonfungibleCollection('A', 'B', 'C')54 // .send();55 // const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);5657 // const newAdmin = privateKeyWrapper('//Alice');58 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);59 // await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();6061 // const adminList = await api.rpc.unique.adminlist(collectionId);62 // expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())63 // .to.be.eq(newAdmin.address.toLocaleLowerCase());64 // });65 66 itWeb3('Verify owner or admin', async ({api, web3, privateKeyWrapper}) => {67 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);68 const collectionHelper = evmCollectionHelpers(web3, owner);69 70 const result = await collectionHelper.methods71 .createNonfungibleCollection('A', 'B', 'C')72 .send({value: Number(2n * UNIQUE)});73 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);7475 const newAdmin = createEthAccount(web3);76 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);77 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.false;78 await collectionEvm.methods.addCollectionAdmin(newAdmin).send();79 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;80 });8182 itWeb3('(!negative tests!) Add admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {83 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);84 const collectionHelper = evmCollectionHelpers(web3, owner);85 86 const result = await collectionHelper.methods87 .createNonfungibleCollection('A', 'B', 'C')88 .send({value: Number(2n * UNIQUE)});89 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);9091 const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);92 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);93 await collectionEvm.methods.addCollectionAdmin(admin).send();94 95 const user = createEthAccount(web3);96 await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: admin}))97 .to.be.rejectedWith('NoPermission');9899 const adminList = await api.rpc.unique.adminlist(collectionId);100 expect(adminList.length).to.be.eq(1);101 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())102 .to.be.eq(admin.toLocaleLowerCase());103 });104105 itWeb3('(!negative tests!) Add admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {106 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);107 const collectionHelper = evmCollectionHelpers(web3, owner);108 109 const result = await collectionHelper.methods110 .createNonfungibleCollection('A', 'B', 'C')111 .send({value: Number(2n * UNIQUE)});112 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);113114 const notAdmin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);115 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);116 117 const user = createEthAccount(web3);118 await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: notAdmin}))119 .to.be.rejectedWith('NoPermission');120121 const adminList = await api.rpc.unique.adminlist(collectionId);122 expect(adminList.length).to.be.eq(0);123 });124125 // TODO: Temprorary off. Need refactor126 // itWeb3('(!negative tests!) Add substrate admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {127 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);128 // const collectionHelper = evmCollectionHelpers(web3, owner);129 130 // const result = await collectionHelper.methods131 // .createNonfungibleCollection('A', 'B', 'C')132 // .send();133 // const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);134135 // const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);136 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);137 // await collectionEvm.methods.addCollectionAdmin(admin).send();138139 // const notAdmin = privateKey('//Alice');140 // await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin.addressRaw).call({from: admin}))141 // .to.be.rejectedWith('NoPermission');142143 // const adminList = await api.rpc.unique.adminlist(collectionId);144 // expect(adminList.length).to.be.eq(1);145 // expect(adminList[0].asEthereum.toString().toLocaleLowerCase())146 // .to.be.eq(admin.toLocaleLowerCase());147 // });148 149 // TODO: Temprorary off. Need refactor150 // itWeb3('(!negative tests!) Add substrate admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {151 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);152 // const collectionHelper = evmCollectionHelpers(web3, owner);153 154 // const result = await collectionHelper.methods155 // .createNonfungibleCollection('A', 'B', 'C')156 // .send();157 // const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);158159 // const notAdmin0 = await createEthAccountWithBalance(api, web3, privateKeyWrapper);160 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);161 // const notAdmin1 = privateKey('//Alice');162 // await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin1.addressRaw).call({from: notAdmin0}))163 // .to.be.rejectedWith('NoPermission');164165 // const adminList = await api.rpc.unique.adminlist(collectionId);166 // expect(adminList.length).to.be.eq(0);167 // });168});169170describe('Remove collection admins', () => {171 itWeb3('Remove admin by owner', async ({api, web3, privateKeyWrapper}) => {172 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);173 const collectionHelper = evmCollectionHelpers(web3, owner);174 175 const result = await collectionHelper.methods176 .createNonfungibleCollection('A', 'B', 'C')177 .send({value: Number(2n * UNIQUE)});178 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);179180 const newAdmin = createEthAccount(web3);181 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);182 await collectionEvm.methods.addCollectionAdmin(newAdmin).send();183 {184 const adminList = await api.rpc.unique.adminlist(collectionId);185 expect(adminList.length).to.be.eq(1);186 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())187 .to.be.eq(newAdmin.toLocaleLowerCase());188 }189190 await collectionEvm.methods.removeCollectionAdmin(newAdmin).send();191 const adminList = await api.rpc.unique.adminlist(collectionId);192 expect(adminList.length).to.be.eq(0);193 });194195 // TODO: Temprorary off. Need refactor196 // itWeb3('Remove substrate admin by owner', async ({api, web3, privateKeyWrapper}) => {197 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);198 // const collectionHelper = evmCollectionHelpers(web3, owner);199 200 // const result = await collectionHelper.methods201 // .createNonfungibleCollection('A', 'B', 'C')202 // .send();203 // const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);204205 // const newAdmin = privateKeyWrapper('//Alice');206 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);207 // await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();208 // {209 // const adminList = await api.rpc.unique.adminlist(collectionId);210 // expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())211 // .to.be.eq(newAdmin.address.toLocaleLowerCase());212 // }213 214 // await collectionEvm.methods.removeCollectionAdminSubstrate(newAdmin.addressRaw).send();215 // const adminList = await api.rpc.unique.adminlist(collectionId);216 // expect(adminList.length).to.be.eq(0);217 // });218219 itWeb3('(!negative tests!) Remove admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {220 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);221 const collectionHelper = evmCollectionHelpers(web3, owner);222 223 const result = await collectionHelper.methods224 .createNonfungibleCollection('A', 'B', 'C')225 .send({value: Number(2n * UNIQUE)});226 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);227228 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);229230 const admin0 = await createEthAccountWithBalance(api, web3, privateKeyWrapper);231 await collectionEvm.methods.addCollectionAdmin(admin0).send();232 const admin1 = createEthAccount(web3);233 await collectionEvm.methods.addCollectionAdmin(admin1).send();234235 await expect(collectionEvm.methods.removeCollectionAdmin(admin1).call({from: admin0}))236 .to.be.rejectedWith('NoPermission');237 {238 const adminList = await api.rpc.unique.adminlist(collectionId);239 expect(adminList.length).to.be.eq(2);240 expect(adminList.toString().toLocaleLowerCase())241 .to.be.deep.contains(admin0.toLocaleLowerCase())242 .to.be.deep.contains(admin1.toLocaleLowerCase());243 }244 });245246 itWeb3('(!negative tests!) Remove admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {247 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);248 const collectionHelper = evmCollectionHelpers(web3, owner);249 250 const result = await collectionHelper.methods251 .createNonfungibleCollection('A', 'B', 'C')252 .send({value: Number(2n * UNIQUE)});253 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);254255 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);256257 const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);258 await collectionEvm.methods.addCollectionAdmin(admin).send();259 const notAdmin = createEthAccount(web3);260261 await expect(collectionEvm.methods.removeCollectionAdmin(admin).call({from: notAdmin}))262 .to.be.rejectedWith('NoPermission');263 {264 const adminList = await api.rpc.unique.adminlist(collectionId);265 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())266 .to.be.eq(admin.toLocaleLowerCase());267 expect(adminList.length).to.be.eq(1);268 }269 });270271 // TODO: Temprorary off. Need refactor272 // itWeb3('(!negative tests!) Remove substrate admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {273 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);274 // const collectionHelper = evmCollectionHelpers(web3, owner);275 276 // const result = await collectionHelper.methods277 // .createNonfungibleCollection('A', 'B', 'C')278 // .send();279 // const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);280281 // const adminSub = privateKeyWrapper('//Alice');282 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);283 // await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();284 // const adminEth = await createEthAccountWithBalance(api, web3, privateKeyWrapper);285 // await collectionEvm.methods.addCollectionAdmin(adminEth).send();286287 // await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: adminEth}))288 // .to.be.rejectedWith('NoPermission');289290 // const adminList = await api.rpc.unique.adminlist(collectionId);291 // expect(adminList.length).to.be.eq(2);292 // expect(adminList.toString().toLocaleLowerCase())293 // .to.be.deep.contains(adminSub.address.toLocaleLowerCase())294 // .to.be.deep.contains(adminEth.toLocaleLowerCase());295 // });296297 // TODO: Temprorary off. Need refactor298 // itWeb3('(!negative tests!) Remove substrate admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {299 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);300 // const collectionHelper = evmCollectionHelpers(web3, owner);301 302 // const result = await collectionHelper.methods303 // .createNonfungibleCollection('A', 'B', 'C')304 // .send();305 // const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);306307 // const adminSub = privateKeyWrapper('//Alice');308 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);309 // await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();310 // const notAdminEth = await createEthAccountWithBalance(api, web3, privateKeyWrapper);311312 // await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: notAdminEth}))313 // .to.be.rejectedWith('NoPermission');314315 // const adminList = await api.rpc.unique.adminlist(collectionId);316 // expect(adminList.length).to.be.eq(1);317 // expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())318 // .to.be.eq(adminSub.address.toLocaleLowerCase());319 // });320});321322describe('Change owner tests', () => {323 itWeb3('Change owner', async ({api, web3, privateKeyWrapper}) => {324 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);325 const newOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);326 const collectionHelper = evmCollectionHelpers(web3, owner);327 const result = await collectionHelper.methods328 .createNonfungibleCollection('A', 'B', 'C')329 .send({value: Number(2n * UNIQUE)});330 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);331 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);332 333 await collectionEvm.methods.setOwner(newOwner).send();334 335 expect(await collectionEvm.methods.isOwnerOrAdmin(owner).call()).to.be.false;336 expect(await collectionEvm.methods.isOwnerOrAdmin(newOwner).call()).to.be.true;337 });338339 itWeb3('change owner call fee', async ({web3, api, privateKeyWrapper}) => {340 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);341 const newOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);342 const collectionHelper = evmCollectionHelpers(web3, owner);343 const result = await collectionHelper.methods344 .createNonfungibleCollection('A', 'B', 'C')345 .send({value: Number(2n * UNIQUE)});346 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);347 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);348349 const cost = await recordEthFee(api, owner, () => collectionEvm.methods.setOwner(newOwner).send());350 expect(cost < BigInt(0.2 * Number(UNIQUE)));351 expect(cost > 0);352 });353354 itWeb3('(!negative tests!) call setOwner by non owner', async ({api, web3, privateKeyWrapper}) => {355 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);356 const newOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);357 const collectionHelper = evmCollectionHelpers(web3, owner);358 const result = await collectionHelper.methods359 .createNonfungibleCollection('A', 'B', 'C')360 .send({value: Number(2n * UNIQUE)});361 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);362 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);363 364 await expect(collectionEvm.methods.setOwner(newOwner).send({from: newOwner})).to.be.rejected;365 expect(await collectionEvm.methods.isOwnerOrAdmin(newOwner).call()).to.be.false;366 });367});368369describe('Change substrate owner tests', () => {370 // TODO: Temprorary off. Need refactor371 // itWeb3('Change owner', async ({api, web3, privateKeyWrapper}) => {372 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);373 // const newOwner = privateKeyWrapper('//Alice');374 // const collectionHelper = evmCollectionHelpers(web3, owner);375 // const result = await collectionHelper.methods376 // .createNonfungibleCollection('A', 'B', 'C')377 // .send();378 // const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);379 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);380 381 // expect(await collectionEvm.methods.isOwnerOrAdmin(owner).call()).to.be.true;382 // expect(await collectionEvm.methods.isOwnerOrAdminSubstrate(newOwner.addressRaw).call()).to.be.false;383 384 // await collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send();385 386 // expect(await collectionEvm.methods.isOwnerOrAdmin(owner).call()).to.be.false;387 // expect(await collectionEvm.methods.isOwnerOrAdminSubstrate(newOwner.addressRaw).call()).to.be.true;388 // });389390 // TODO: Temprorary off. Need refactor391 // itWeb3('change owner call fee', async ({web3, api, privateKeyWrapper}) => {392 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);393 // const newOwner = privateKeyWrapper('//Alice');394 // const collectionHelper = evmCollectionHelpers(web3, owner);395 // const result = await collectionHelper.methods396 // .createNonfungibleCollection('A', 'B', 'C')397 // .send();398 // const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);399 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);400401 // const cost = await recordEthFee(api, owner, () => collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send());402 // expect(cost < BigInt(0.2 * Number(UNIQUE)));403 // expect(cost > 0);404 // });405406 // TODO: Temprorary off. Need refactor407 // itWeb3('(!negative tests!) call setOwner by non owner', async ({api, web3, privateKeyWrapper}) => {408 // const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);409 // const otherReceiver = await createEthAccountWithBalance(api, web3, privateKeyWrapper);410 // const newOwner = privateKeyWrapper('//Alice');411 // const collectionHelper = evmCollectionHelpers(web3, owner);412 // const result = await collectionHelper.methods413 // .createNonfungibleCollection('A', 'B', 'C')414 // .send();415 // const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);416 // const collectionEvm = evmCollection(web3, owner, collectionIdAddress);417 418 // await expect(collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send({from: otherReceiver})).to.be.rejected;419 // expect(await collectionEvm.methods.isOwnerOrAdminSubstrate(newOwner.addressRaw).call()).to.be.false;420 // });421});difftreelog
source
tests/src/eth/collectionAdmin.test.ts20.6 KiBsourcehistory