git.delta.rocks / unique-network / refs/commits / 73ad88465834

difftreelog

CORE-302 Clean code

Trubnikov Sergey2022-04-26parent: #3912bc1.patch.diff
in: master

7 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -153,7 +153,7 @@
 	pub fn set_sponsor(&mut self, sponsor: T::AccountId) {
 		self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor);
 	}
-	
+
 	pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> bool {
 		if self.collection.sponsorship.pending_sponsor() != Some(sender) {
 			return false;
modifiedpallets/evm-collection/src/eth.rsdiffbeforeafterboth
--- a/pallets/evm-collection/src/eth.rs
+++ b/pallets/evm-collection/src/eth.rs
@@ -18,6 +18,7 @@
 use evm_coder::{abi::AbiWriter, execution::*, generate_stubgen, solidity_interface, types::*, ToLog};
 use ethereum as _;
 use pallet_common::CollectionById;
+use pallet_common::CollectionHandle;
 use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
 use pallet_evm::{
 	ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure,
modifiedpallets/evm-collection/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-collection/src/lib.rs
+++ b/pallets/evm-collection/src/lib.rs
@@ -18,22 +18,22 @@
 
 extern crate alloc;
 
-use codec::{Decode, Encode, MaxEncodedLen};
 pub use pallet::*;
 pub use eth::*;
-use scale_info::TypeInfo;
 pub mod eth;
 
 #[frame_support::pallet]
 pub mod pallet {
 	pub use super::*;
-	use evm_coder::execution::Result;
 	use frame_support::pallet_prelude::*;
 	use sp_core::H160;
 
 	#[pallet::config]
 	pub trait Config:
-		frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::account::Config + pallet_nonfungible::Config
+		frame_system::Config
+		+ pallet_evm_coder_substrate::Config
+		+ pallet_evm::account::Config
+		+ pallet_nonfungible::Config
 	{
 		type ContractAddress: Get<H160>;
 	}
@@ -47,7 +47,6 @@
 	#[pallet::pallet]
 	// #[pallet::generate_store(pub(super) trait Store)]
 	pub struct Pallet<T>(_);
-
 
 	impl<T: Config> Pallet<T> {}
 }
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -31,7 +31,10 @@
 
 	#[pallet::config]
 	pub trait Config:
-		frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::account::Config + pallet_nonfungible::Config
+		frame_system::Config
+		+ pallet_evm_coder_substrate::Config
+		+ pallet_evm::account::Config
+		+ pallet_nonfungible::Config
 	{
 		type ContractAddress: Get<H160>;
 		type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -368,30 +368,30 @@
 #[derive(Encode, Decode, Debug, Default, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
 pub struct CollectionLimits {
-	#[serde(alias="accountTokenOwnershipLimit")]
+	#[serde(alias = "accountTokenOwnershipLimit")]
 	pub account_token_ownership_limit: Option<u32>,
-	#[serde(alias="sponsoredDataSize")]
+	#[serde(alias = "sponsoredDataSize")]
 	pub sponsored_data_size: Option<u32>,
 
 	/// FIXME should we delete this or repurpose it?
 	/// None - setVariableMetadata is not sponsored
 	/// Some(v) - setVariableMetadata is sponsored
 	///           if there is v block between txs
-	#[serde(alias="sponsoredDataRateLimit")]
+	#[serde(alias = "sponsoredDataRateLimit")]
 	pub sponsored_data_rate_limit: Option<SponsoringRateLimit>,
-	#[serde(alias="tokenLimit")]
+	#[serde(alias = "tokenLimit")]
 	pub token_limit: Option<u32>,
-	
+
 	// Timeouts for item types in passed blocks
-	#[serde(alias="sponsorTransferTimeout")]
+	#[serde(alias = "sponsorTransferTimeout")]
 	pub sponsor_transfer_timeout: Option<u32>,
-	#[serde(alias="sponsorApproveTimeout")]
+	#[serde(alias = "sponsorApproveTimeout")]
 	pub sponsor_approve_timeout: Option<u32>,
-	#[serde(alias="ownerCanTransfer")]
+	#[serde(alias = "ownerCanTransfer")]
 	pub owner_can_transfer: Option<bool>,
-	#[serde(alias="ownerCanDestroy")]
+	#[serde(alias = "ownerCanDestroy")]
 	pub owner_can_destroy: Option<bool>,
-	#[serde(alias="transfersEnabled")]
+	#[serde(alias = "transfersEnabled")]
 	pub transfers_enabled: Option<bool>,
 }
 
modifiedruntime/opal/src/lib.rsdiffbeforeafterboth
--- a/runtime/opal/src/lib.rs
+++ b/runtime/opal/src/lib.rs
@@ -975,7 +975,7 @@
 	pub const HelpersContractAddress: H160 = H160([
 		0x84, 0x28, 0x99, 0xec, 0xf3, 0x80, 0x55, 0x3e, 0x8a, 0x4d, 0xe7, 0x5b, 0xf5, 0x34, 0xcd, 0xf6, 0xfb, 0xf6, 0x40, 0x49,
 	]);
-	
+
 	// 0x6c4e9fe1ae37a41e93cee429e8e1881abdcbb54f
 	pub const EvmCollectionAddress: H160 = H160([
 		0x6c, 0x4e, 0x9f, 0xe1, 0xae, 0x37, 0xa4, 0x1e, 0x93, 0xce, 0xe4, 0x29, 0xe8, 0xe1, 0x88, 0x1a, 0xbd, 0xcb, 0xb5, 0x4f,
modifiedtests/src/eth/createCollection.test.tsdiffbeforeafterboth
before · tests/src/eth/createCollection.test.ts
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.8//9// 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 {ApiPromise} from '@polkadot/api';18import {evmToAddress} from '@polkadot/util-crypto';19import {expect} from 'chai';20import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';21import {collectionHelper, collectionIdFromAddress, createEthAccount, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers';2223async function getCollectionAddressFromResult(api: ApiPromise, result: any) {24  const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);25  const collectionId = collectionIdFromAddress(collectionIdAddress);  26  const collection = (await getDetailedCollectionInfo(api, collectionId))!;27  return {collectionIdAddress, collectionId, collection};28}2930describe('Create collection from EVM', () => {31  itWeb3('Create collection', async ({api, web3}) => {32    const owner = await createEthAccountWithBalance(api, web3);33    const helper = collectionHelper(web3, owner);34    const collectionName = 'CollectionEVM';35    const description = 'Some description';36    const tokenPrefix = 'token prefix';37  38    const collectionCountBefore = await getCreatedCollectionCount(api);39    const result = await helper.methods40      .create721Collection(collectionName, description, tokenPrefix)41      .send();42    const collectionCountAfter = await getCreatedCollectionCount(api);43  44    const {collectionId, collection} = await getCollectionAddressFromResult(api, result);45    expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);46    expect(collectionId).to.be.eq(collectionCountAfter);47    expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);48    expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);49    expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);50    expect(collection.schemaVersion.type).to.be.eq('ImageURL');51  });52  53  itWeb3('Set sponsorship', async ({api, web3}) => {54    const owner = await createEthAccountWithBalance(api, web3);55    const helper = collectionHelper(web3, owner);56    let result = await helper.methods.create721Collection('Sponsor collection', '1', '1').send();57    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);58    const sponsor = await createEthAccountWithBalance(api, web3);59    result = await helper.methods.setSponsor(collectionIdAddress, sponsor).send();60    let collection = (await getDetailedCollectionInfo(api, collectionId))!;61    expect(collection.sponsorship.isUnconfirmed).to.be.true;62    expect(collection.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));63    await expect(helper.methods.confirmSponsorship(collectionIdAddress).call()).to.be.rejectedWith('Caller is not set as sponsor');64    const sponsorHelper = collectionHelper(web3, sponsor);65    await sponsorHelper.methods.confirmSponsorship(collectionIdAddress).send();66    collection = (await getDetailedCollectionInfo(api, collectionId))!;67    expect(collection.sponsorship.isConfirmed).to.be.true;68    expect(collection.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));69  });70  71  itWeb3('Set offchain shema', async ({api, web3}) => {72    const owner = await createEthAccountWithBalance(api, web3);73    const helper = collectionHelper(web3, owner);74    let result = await helper.methods.create721Collection('Shema collection', '2', '2').send();75    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);76    const shema = 'Some shema';77    result = await helper.methods.setOffchainShema(collectionIdAddress, shema).send();78    const collection = (await getDetailedCollectionInfo(api, collectionId))!;79    expect(collection.offchainSchema.toHuman()).to.be.eq(shema);80  });81  82  itWeb3('Set variable on chain schema', async ({api, web3}) => {83    const owner = await createEthAccountWithBalance(api, web3);84    const helper = collectionHelper(web3, owner);85    let result = await helper.methods.create721Collection('Variable collection', '3', '3').send();86    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);87    const variable = 'Some variable';88    result = await helper.methods.setVariableOnChainSchema(collectionIdAddress, variable).send();89    const collection = (await getDetailedCollectionInfo(api, collectionId))!;90    expect(collection.variableOnChainSchema.toHuman()).to.be.eq(variable);91  });92  93  itWeb3('Set const on chain schema', async ({api, web3}) => {94    const owner = await createEthAccountWithBalance(api, web3);95    const helper = collectionHelper(web3, owner);96    let result = await helper.methods.create721Collection('Const collection', '4', '4').send();97    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);98    const constShema = 'Some const';99    result = await helper.methods.setConstOnChainSchema(collectionIdAddress, constShema).send();100    const collection = (await getDetailedCollectionInfo(api, collectionId))!;101    expect(collection.constOnChainSchema.toHuman()).to.be.eq(constShema);102  });103104  itWeb3.only('Set limits', async ({api, web3}) => {105    const owner = await createEthAccountWithBalance(api, web3);106    const helper = collectionHelper(web3, owner);107    const result = await helper.methods.create721Collection('Const collection', '4', '4').send();108    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);109    const limits = {110      accountTokenOwnershipLimit: 1000,111      sponsoredDataSize: 1024,112      sponsoredDataRateLimit: {Blocks: 30},113      tokenLimit: 1000000,114      sponsorTransferTimeout: 6,115      sponsorApproveTimeout: 6,116      ownerCanTransfer: false,117      ownerCanDestroy: false,118      transfersEnabled: false,119    };120121    const limitsJson = JSON.stringify(limits, null, 1);122    await helper.methods.setLimits(collectionIdAddress, limitsJson).send();123    124    const collection = (await getDetailedCollectionInfo(api, collectionId))!;125    expect(collection.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);126    expect(collection.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);127    expect(collection.limits.sponsoredDataRateLimit.unwrap().asBlocks.toNumber()).to.be.eq(limits.sponsoredDataRateLimit.Blocks);128    expect(collection.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);129    expect(collection.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);130    expect(collection.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);131    expect(collection.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);132    expect(collection.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);133    expect(collection.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);134  });135});136137describe('(!negative tests!) Create collection from EVM', () => {138  itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => {139    const owner = await createEthAccountWithBalance(api, web3);140    const helper = collectionHelper(web3, owner);141    {142      const MAX_NAME_LENGHT = 64;143      const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);144      const description = 'A';145      const tokenPrefix = 'A';146    147      await expect(helper.methods148        .create721Collection(collectionName, description, tokenPrefix)149        .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);150      151    }152    {  153      const MAX_DESCRIPTION_LENGHT = 256;154      const collectionName = 'A';155      const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);156      const tokenPrefix = 'A';157      await expect(helper.methods158        .create721Collection(collectionName, description, tokenPrefix)159        .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);160    }161    {  162      const MAX_TOKEN_PREFIX_LENGHT = 16;163      const collectionName = 'A';164      const description = 'A';165      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);166      await expect(helper.methods167        .create721Collection(collectionName, description, tokenPrefix)168        .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);169    }170  });171  172  itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {173    const owner = await createEthAccount(web3);174    const helper = collectionHelper(web3, owner);175    const collectionName = 'A';176    const description = 'A';177    const tokenPrefix = 'A';178    179    await expect(helper.methods180      .create721Collection(collectionName, description, tokenPrefix)181      .call()).to.be.rejectedWith('NotSufficientFounds');182  });183184  itWeb3('(!negative test!) Collection address (Bad ETH prefix)', async ({api, web3}) => {185    const owner = await createEthAccountWithBalance(api, web3);186    const helper = collectionHelper(web3, owner);187    const collectionAddressWithBadPrefix = '0x00112233445566778899AABBCCDDEEFF00112233';188    const EXPECTED_ERROR = 'Bad ETH prefix';189    {190      const sponsor = await createEthAccountWithBalance(api, web3);191      await expect(helper.methods192        .setSponsor(collectionAddressWithBadPrefix, sponsor)193        .call()).to.be.rejectedWith(EXPECTED_ERROR);194      195      const sponsorHelper = collectionHelper(web3, sponsor);196      await expect(sponsorHelper.methods197        .confirmSponsorship(collectionAddressWithBadPrefix)198        .call()).to.be.rejectedWith(EXPECTED_ERROR);199    }200    {201      const shema = 'Some shema';202      await expect(helper.methods203        .setOffchainShema(collectionAddressWithBadPrefix, shema)204        .call()).to.be.rejectedWith(EXPECTED_ERROR);205    }206    {207      const variable = 'Some variable';208      await expect(helper.methods209        .setVariableOnChainSchema(collectionAddressWithBadPrefix, variable)210        .call()).to.be.rejectedWith(EXPECTED_ERROR);211    }212    {213      const constData = 'Some const';214      await expect(helper.methods215        .setConstOnChainSchema(collectionAddressWithBadPrefix, constData)216        .call()).to.be.rejectedWith(EXPECTED_ERROR);217    }218    {219      const limits = '{"account_token_ownership_limit":1000}';220      await expect(helper.methods221        .setLimits(collectionAddressWithBadPrefix, limits)222        .call()).to.be.rejectedWith(EXPECTED_ERROR);223    }224  });225226  itWeb3('(!negative test!) Check owner', async ({api, web3}) => {227    const owner = await createEthAccountWithBalance(api, web3);228    const notOwner = await createEthAccount(web3);229    const helperFromOwner = collectionHelper(web3, owner);230    const helperFromNotOwner = collectionHelper(web3, notOwner);231    const result = await helperFromOwner.methods.create721Collection('A', 'A', 'A').send();232    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);233    const EXPECTED_ERROR = 'NoPermission';234    {235      const sponsor = await createEthAccountWithBalance(api, web3);236      await expect(helperFromNotOwner.methods237        .setSponsor(collectionIdAddress, sponsor)238        .call()).to.be.rejectedWith(EXPECTED_ERROR);239      240      const sponsorHelper = collectionHelper(web3, sponsor);241      await expect(sponsorHelper.methods242        .confirmSponsorship(collectionIdAddress)243        .call()).to.be.rejectedWith('Caller is not set as sponsor');244    }245    {246      const shema = 'Some shema';247      await expect(helperFromNotOwner.methods248        .setOffchainShema(collectionIdAddress, shema)249        .call()).to.be.rejectedWith(EXPECTED_ERROR);250    }251    {252      const variable = 'Some variable';253      await expect(helperFromNotOwner.methods254        .setVariableOnChainSchema(collectionIdAddress, variable)255        .call()).to.be.rejectedWith(EXPECTED_ERROR);256    }257    {258      const constData = 'Some const';259      await expect(helperFromNotOwner.methods260        .setConstOnChainSchema(collectionIdAddress, constData)261        .call()).to.be.rejectedWith(EXPECTED_ERROR);262    }263    {264      const limits = '{"account_token_ownership_limit":1000}';265      await expect(helperFromNotOwner.methods266        .setLimits(collectionIdAddress, limits)267        .call()).to.be.rejectedWith(EXPECTED_ERROR);268    }269  });270271  itWeb3('(!negative test!) Set offchain shema (length limit)', async ({api, web3}) => {272    const owner = await createEthAccountWithBalance(api, web3);273    const helper = collectionHelper(web3, owner);274    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();275    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);276    const OFFCHAIN_SCHEMA_LIMIT = 8192;277    const shema = 'A'.repeat(OFFCHAIN_SCHEMA_LIMIT + 1);278    await expect(helper.methods279      .setOffchainShema(collectionIdAddress, shema)280      .call()).to.be.rejectedWith('shema is too long. Max length is ' + OFFCHAIN_SCHEMA_LIMIT);281  });282283  itWeb3('(!negative test!) Set variable on chain schema (length limit)', async ({api, web3}) => {284    const owner = await createEthAccountWithBalance(api, web3);285    const helper = collectionHelper(web3, owner);286    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();287    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);288    const VARIABLE_ON_CHAIN_SCHEMA_LIMIT = 8192;289    const variable = 'A'.repeat(VARIABLE_ON_CHAIN_SCHEMA_LIMIT + 1);290    await expect(helper.methods291      .setVariableOnChainSchema(collectionIdAddress, variable)292      .call()).to.be.rejectedWith('variable is too long. Max length is ' + VARIABLE_ON_CHAIN_SCHEMA_LIMIT);293  });294295  itWeb3('(!negative test!) Set const on chain schema (length limit)', async ({api, web3}) => {296    const owner = await createEthAccountWithBalance(api, web3);297    const helper = collectionHelper(web3, owner);298    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();299    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);300    const CONST_ON_CHAIN_SCHEMA_LIMIT = 32768;301    const constData = 'A'.repeat(CONST_ON_CHAIN_SCHEMA_LIMIT + 1);302    await expect(helper.methods303      .setConstOnChainSchema(collectionIdAddress, constData)304      .call()).to.be.rejectedWith('const_on_chain is too long. Max length is ' + CONST_ON_CHAIN_SCHEMA_LIMIT);305  });306307  itWeb3('(!negative test!) Set limits', async ({api, web3}) => {308    const owner = await createEthAccountWithBalance(api, web3);309    const helper = collectionHelper(web3, owner);310    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();311    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);312    const badJson = '{accountTokenOwnershipLimit: 1000}';313    await expect(helper.methods314      .setLimits(collectionIdAddress, badJson)315      .call()).to.be.rejectedWith('Parse JSON error:');316  });317});
after · tests/src/eth/createCollection.test.ts
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.8//9// 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 {ApiPromise} from '@polkadot/api';18import {evmToAddress} from '@polkadot/util-crypto';19import {expect} from 'chai';20import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';21import {collectionHelper, collectionIdFromAddress, createEthAccount, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers';2223async function getCollectionAddressFromResult(api: ApiPromise, result: any) {24  const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);25  const collectionId = collectionIdFromAddress(collectionIdAddress);  26  const collection = (await getDetailedCollectionInfo(api, collectionId))!;27  return {collectionIdAddress, collectionId, collection};28}2930describe('Create collection from EVM', () => {31  itWeb3('Create collection', async ({api, web3}) => {32    const owner = await createEthAccountWithBalance(api, web3);33    const helper = collectionHelper(web3, owner);34    const collectionName = 'CollectionEVM';35    const description = 'Some description';36    const tokenPrefix = 'token prefix';37  38    const collectionCountBefore = await getCreatedCollectionCount(api);39    const result = await helper.methods40      .create721Collection(collectionName, description, tokenPrefix)41      .send();42    const collectionCountAfter = await getCreatedCollectionCount(api);43  44    const {collectionId, collection} = await getCollectionAddressFromResult(api, result);45    expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);46    expect(collectionId).to.be.eq(collectionCountAfter);47    expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);48    expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);49    expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);50    expect(collection.schemaVersion.type).to.be.eq('ImageURL');51  });52  53  itWeb3('Set sponsorship', async ({api, web3}) => {54    const owner = await createEthAccountWithBalance(api, web3);55    const helper = collectionHelper(web3, owner);56    let result = await helper.methods.create721Collection('Sponsor collection', '1', '1').send();57    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);58    const sponsor = await createEthAccountWithBalance(api, web3);59    result = await helper.methods.setSponsor(collectionIdAddress, sponsor).send();60    let collection = (await getDetailedCollectionInfo(api, collectionId))!;61    expect(collection.sponsorship.isUnconfirmed).to.be.true;62    expect(collection.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));63    await expect(helper.methods.confirmSponsorship(collectionIdAddress).call()).to.be.rejectedWith('Caller is not set as sponsor');64    const sponsorHelper = collectionHelper(web3, sponsor);65    await sponsorHelper.methods.confirmSponsorship(collectionIdAddress).send();66    collection = (await getDetailedCollectionInfo(api, collectionId))!;67    expect(collection.sponsorship.isConfirmed).to.be.true;68    expect(collection.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));69  });70  71  itWeb3('Set offchain shema', async ({api, web3}) => {72    const owner = await createEthAccountWithBalance(api, web3);73    const helper = collectionHelper(web3, owner);74    let result = await helper.methods.create721Collection('Shema collection', '2', '2').send();75    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);76    const shema = 'Some shema';77    result = await helper.methods.setOffchainShema(collectionIdAddress, shema).send();78    const collection = (await getDetailedCollectionInfo(api, collectionId))!;79    expect(collection.offchainSchema.toHuman()).to.be.eq(shema);80  });81  82  itWeb3('Set variable on chain schema', async ({api, web3}) => {83    const owner = await createEthAccountWithBalance(api, web3);84    const helper = collectionHelper(web3, owner);85    let result = await helper.methods.create721Collection('Variable collection', '3', '3').send();86    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);87    const variable = 'Some variable';88    result = await helper.methods.setVariableOnChainSchema(collectionIdAddress, variable).send();89    const collection = (await getDetailedCollectionInfo(api, collectionId))!;90    expect(collection.variableOnChainSchema.toHuman()).to.be.eq(variable);91  });92  93  itWeb3('Set const on chain schema', async ({api, web3}) => {94    const owner = await createEthAccountWithBalance(api, web3);95    const helper = collectionHelper(web3, owner);96    let result = await helper.methods.create721Collection('Const collection', '4', '4').send();97    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);98    const constShema = 'Some const';99    result = await helper.methods.setConstOnChainSchema(collectionIdAddress, constShema).send();100    const collection = (await getDetailedCollectionInfo(api, collectionId))!;101    expect(collection.constOnChainSchema.toHuman()).to.be.eq(constShema);102  });103104  itWeb3('Set limits', async ({api, web3}) => {105    const owner = await createEthAccountWithBalance(api, web3);106    const helper = collectionHelper(web3, owner);107    const result = await helper.methods.create721Collection('Const collection', '4', '4').send();108    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);109    const limits = {110      accountTokenOwnershipLimit: 1000,111      sponsoredDataSize: 1024,112      sponsoredDataRateLimit: {Blocks: 30},113      tokenLimit: 1000000,114      sponsorTransferTimeout: 6,115      sponsorApproveTimeout: 6,116      ownerCanTransfer: false,117      ownerCanDestroy: false,118      transfersEnabled: false,119    };120121    const limitsJson = JSON.stringify(limits, null, 1);122    await helper.methods.setLimits(collectionIdAddress, limitsJson).send();123    124    const collection = (await getDetailedCollectionInfo(api, collectionId))!;125    expect(collection.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);126    expect(collection.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);127    expect(collection.limits.sponsoredDataRateLimit.unwrap().asBlocks.toNumber()).to.be.eq(limits.sponsoredDataRateLimit.Blocks);128    expect(collection.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);129    expect(collection.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);130    expect(collection.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);131    expect(collection.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);132    expect(collection.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);133    expect(collection.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);134  });135});136137describe('(!negative tests!) Create collection from EVM', () => {138  itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => {139    const owner = await createEthAccountWithBalance(api, web3);140    const helper = collectionHelper(web3, owner);141    {142      const MAX_NAME_LENGHT = 64;143      const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);144      const description = 'A';145      const tokenPrefix = 'A';146    147      await expect(helper.methods148        .create721Collection(collectionName, description, tokenPrefix)149        .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);150      151    }152    {  153      const MAX_DESCRIPTION_LENGHT = 256;154      const collectionName = 'A';155      const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);156      const tokenPrefix = 'A';157      await expect(helper.methods158        .create721Collection(collectionName, description, tokenPrefix)159        .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);160    }161    {  162      const MAX_TOKEN_PREFIX_LENGHT = 16;163      const collectionName = 'A';164      const description = 'A';165      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);166      await expect(helper.methods167        .create721Collection(collectionName, description, tokenPrefix)168        .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);169    }170  });171  172  itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {173    const owner = await createEthAccount(web3);174    const helper = collectionHelper(web3, owner);175    const collectionName = 'A';176    const description = 'A';177    const tokenPrefix = 'A';178    179    await expect(helper.methods180      .create721Collection(collectionName, description, tokenPrefix)181      .call()).to.be.rejectedWith('NotSufficientFounds');182  });183184  itWeb3('(!negative test!) Collection address (Bad ETH prefix)', async ({api, web3}) => {185    const owner = await createEthAccountWithBalance(api, web3);186    const helper = collectionHelper(web3, owner);187    const collectionAddressWithBadPrefix = '0x00112233445566778899AABBCCDDEEFF00112233';188    const EXPECTED_ERROR = 'Bad ETH prefix';189    {190      const sponsor = await createEthAccountWithBalance(api, web3);191      await expect(helper.methods192        .setSponsor(collectionAddressWithBadPrefix, sponsor)193        .call()).to.be.rejectedWith(EXPECTED_ERROR);194      195      const sponsorHelper = collectionHelper(web3, sponsor);196      await expect(sponsorHelper.methods197        .confirmSponsorship(collectionAddressWithBadPrefix)198        .call()).to.be.rejectedWith(EXPECTED_ERROR);199    }200    {201      const shema = 'Some shema';202      await expect(helper.methods203        .setOffchainShema(collectionAddressWithBadPrefix, shema)204        .call()).to.be.rejectedWith(EXPECTED_ERROR);205    }206    {207      const variable = 'Some variable';208      await expect(helper.methods209        .setVariableOnChainSchema(collectionAddressWithBadPrefix, variable)210        .call()).to.be.rejectedWith(EXPECTED_ERROR);211    }212    {213      const constData = 'Some const';214      await expect(helper.methods215        .setConstOnChainSchema(collectionAddressWithBadPrefix, constData)216        .call()).to.be.rejectedWith(EXPECTED_ERROR);217    }218    {219      const limits = '{"account_token_ownership_limit":1000}';220      await expect(helper.methods221        .setLimits(collectionAddressWithBadPrefix, limits)222        .call()).to.be.rejectedWith(EXPECTED_ERROR);223    }224  });225226  itWeb3('(!negative test!) Check owner', async ({api, web3}) => {227    const owner = await createEthAccountWithBalance(api, web3);228    const notOwner = await createEthAccount(web3);229    const helperFromOwner = collectionHelper(web3, owner);230    const helperFromNotOwner = collectionHelper(web3, notOwner);231    const result = await helperFromOwner.methods.create721Collection('A', 'A', 'A').send();232    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);233    const EXPECTED_ERROR = 'NoPermission';234    {235      const sponsor = await createEthAccountWithBalance(api, web3);236      await expect(helperFromNotOwner.methods237        .setSponsor(collectionIdAddress, sponsor)238        .call()).to.be.rejectedWith(EXPECTED_ERROR);239      240      const sponsorHelper = collectionHelper(web3, sponsor);241      await expect(sponsorHelper.methods242        .confirmSponsorship(collectionIdAddress)243        .call()).to.be.rejectedWith('Caller is not set as sponsor');244    }245    {246      const shema = 'Some shema';247      await expect(helperFromNotOwner.methods248        .setOffchainShema(collectionIdAddress, shema)249        .call()).to.be.rejectedWith(EXPECTED_ERROR);250    }251    {252      const variable = 'Some variable';253      await expect(helperFromNotOwner.methods254        .setVariableOnChainSchema(collectionIdAddress, variable)255        .call()).to.be.rejectedWith(EXPECTED_ERROR);256    }257    {258      const constData = 'Some const';259      await expect(helperFromNotOwner.methods260        .setConstOnChainSchema(collectionIdAddress, constData)261        .call()).to.be.rejectedWith(EXPECTED_ERROR);262    }263    {264      const limits = '{"account_token_ownership_limit":1000}';265      await expect(helperFromNotOwner.methods266        .setLimits(collectionIdAddress, limits)267        .call()).to.be.rejectedWith(EXPECTED_ERROR);268    }269  });270271  itWeb3('(!negative test!) Set offchain shema (length limit)', async ({api, web3}) => {272    const owner = await createEthAccountWithBalance(api, web3);273    const helper = collectionHelper(web3, owner);274    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();275    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);276    const OFFCHAIN_SCHEMA_LIMIT = 8192;277    const shema = 'A'.repeat(OFFCHAIN_SCHEMA_LIMIT + 1);278    await expect(helper.methods279      .setOffchainShema(collectionIdAddress, shema)280      .call()).to.be.rejectedWith('shema is too long. Max length is ' + OFFCHAIN_SCHEMA_LIMIT);281  });282283  itWeb3('(!negative test!) Set variable on chain schema (length limit)', async ({api, web3}) => {284    const owner = await createEthAccountWithBalance(api, web3);285    const helper = collectionHelper(web3, owner);286    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();287    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);288    const VARIABLE_ON_CHAIN_SCHEMA_LIMIT = 8192;289    const variable = 'A'.repeat(VARIABLE_ON_CHAIN_SCHEMA_LIMIT + 1);290    await expect(helper.methods291      .setVariableOnChainSchema(collectionIdAddress, variable)292      .call()).to.be.rejectedWith('variable is too long. Max length is ' + VARIABLE_ON_CHAIN_SCHEMA_LIMIT);293  });294295  itWeb3('(!negative test!) Set const on chain schema (length limit)', async ({api, web3}) => {296    const owner = await createEthAccountWithBalance(api, web3);297    const helper = collectionHelper(web3, owner);298    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();299    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);300    const CONST_ON_CHAIN_SCHEMA_LIMIT = 32768;301    const constData = 'A'.repeat(CONST_ON_CHAIN_SCHEMA_LIMIT + 1);302    await expect(helper.methods303      .setConstOnChainSchema(collectionIdAddress, constData)304      .call()).to.be.rejectedWith('const_on_chain is too long. Max length is ' + CONST_ON_CHAIN_SCHEMA_LIMIT);305  });306307  itWeb3('(!negative test!) Set limits', async ({api, web3}) => {308    const owner = await createEthAccountWithBalance(api, web3);309    const helper = collectionHelper(web3, owner);310    const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();311    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);312    const badJson = '{accountTokenOwnershipLimit: 1000}';313    await expect(helper.methods314      .setLimits(collectionIdAddress, badJson)315      .call()).to.be.rejectedWith('Parse JSON error:');316  });317});