difftreelog
added tests for `createRTCollection` , refactor `Unique` pallet code
in: master
9 files changed
pallets/unique/CHANGELOG.mddiffbeforeafterboth--- a/pallets/unique/CHANGELOG.md
+++ b/pallets/unique/CHANGELOG.md
@@ -8,7 +8,7 @@
### Changes
-- Addded **CollectionHelpers** method `destroyCollection`.
+- Added `destroyCollection` and `createFTCollection` methods to **CollectionHelpers**.
## [v0.2.0] 2022-09-13
pallets/unique/src/eth/mod.rsdiffbeforeafterboth--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -18,7 +18,7 @@
use core::marker::PhantomData;
use ethereum as _;
-use evm_coder::{execution::*, generate_stubgen, solidity_interface, solidity, weight, types::*};
+use evm_coder::{execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};
use frame_support::traits::Get;
use crate::Pallet;
@@ -27,23 +27,24 @@
CollectionById,
dispatch::CollectionDispatch,
erc::{
+ static_property::key,
CollectionHelpersEvents,
- static_property::{key},
},
Pallet as PalletCommon,
+
};
+use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};
use pallet_evm_coder_substrate::{dispatch_to_evm, SubstrateRecorder, WithRecorder};
-use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};
use sp_std::vec;
use up_data_structs::{
- CollectionName, CollectionDescription, CollectionTokenPrefix, CreateCollectionData,
- CollectionMode, PropertyValue,
+ CollectionDescription, CollectionMode, CollectionName, CollectionTokenPrefix,
+ CreateCollectionData, PropertyValue,
};
-use crate::{Config, SelfWeightOf, weights::WeightInfo};
+use crate::{weights::WeightInfo, Config, SelfWeightOf};
-use sp_std::vec::Vec;
use alloc::format;
+use sp_std::vec::Vec;
/// See [`CollectionHelpersCall`]
pub struct EvmCollectionHelpers<T: Config>(SubstrateRecorder<T>);
@@ -104,6 +105,7 @@
)
}
+#[inline(always)]
fn create_collection_internal<T: Config>(
caller: caller,
value: value,
@@ -212,7 +214,14 @@
description: string,
token_prefix: string,
) -> Result<address> {
- self.create_nft_collection(caller, value, name, description, token_prefix)
+ create_collection_internal::<T>(
+ caller,
+ value,
+ name,
+ CollectionMode::NFT,
+ description,
+ token_prefix,
+ )
}
#[weight(<SelfWeightOf<T>>::create_collection())]
@@ -225,11 +234,39 @@
description: string,
token_prefix: string,
) -> Result<address> {
- create_refungible_collection_internal::<T>(caller, value, name, description, token_prefix)
+ create_collection_internal::<T>(
+ caller,
+ value,
+ name,
+ CollectionMode::ReFungible,
+ description,
+ token_prefix,
+ )
+ }
+
+ #[weight(<SelfWeightOf<T>>::create_collection())]
+ #[solidity(rename_selector = "createERC721MetadataCompatibleRFTCollection")]
+ fn create_refungible_collection_with_properties(
+ &mut self,
+ caller: caller,
+ value: value,
+ name: string,
+ description: string,
+ token_prefix: string,
+ base_uri: string,
+ ) -> Result<address> {
+ create_collection_internal::<T>(
+ caller,
+ value,
+ name,
+ CollectionMode::ReFungible,
+ description,
+ token_prefix,
+ )
}
#[weight(<SelfWeightOf<T>>::create_collection())]
- #[solidity(rename_selector = "createRTCollection")]
+ #[solidity(rename_selector = "createFTCollection")]
fn create_fungible_collection(
&mut self,
caller: caller,
pallets/unique/src/eth/stubs/CollectionHelpers.rawdiffbeforeafterbothbinary blob — no preview
pallets/unique/src/eth/stubs/CollectionHelpers.soldiffbeforeafterboth--- a/pallets/unique/src/eth/stubs/CollectionHelpers.sol
+++ b/pallets/unique/src/eth/stubs/CollectionHelpers.sol
@@ -24,7 +24,7 @@
}
/// @title Contract, which allows users to operate with collections
-/// @dev the ERC-165 identifier for this interface is 0xa2c196ab
+/// @dev the ERC-165 identifier for this interface is 0xd8b36039
contract CollectionHelpers is Dummy, ERC165, CollectionHelpersEvents {
/// Create an NFT collection
/// @param name Name of the collection
@@ -77,9 +77,26 @@
return 0x0000000000000000000000000000000000000000;
}
- /// @dev EVM selector for this function is: 0xac1e2285,
- /// or in textual repr: createRTCollection(string,uint8,string,string)
- function createRTCollection(
+ /// @dev EVM selector for this function is: 0xa5596388,
+ /// or in textual repr: createERC721MetadataCompatibleRFTCollection(string,string,string,string)
+ function createERC721MetadataCompatibleRFTCollection(
+ string memory name,
+ string memory description,
+ string memory tokenPrefix,
+ string memory baseUri
+ ) public payable returns (address) {
+ require(false, stub_error);
+ name;
+ description;
+ tokenPrefix;
+ baseUri;
+ dummy = 0;
+ return 0x0000000000000000000000000000000000000000;
+ }
+
+ /// @dev EVM selector for this function is: 0x7335b79f,
+ /// or in textual repr: createFTCollection(string,uint8,string,string)
+ function createFTCollection(
string memory name,
uint8 decimals,
string memory description,
tests/src/eth/api/CollectionHelpers.soldiffbeforeafterboth--- a/tests/src/eth/api/CollectionHelpers.sol
+++ b/tests/src/eth/api/CollectionHelpers.sol
@@ -19,7 +19,7 @@
}
/// @title Contract, which allows users to operate with collections
-/// @dev the ERC-165 identifier for this interface is 0xa2c196ab
+/// @dev the ERC-165 identifier for this interface is 0xd8b36039
interface CollectionHelpers is Dummy, ERC165, CollectionHelpersEvents {
/// Create an NFT collection
/// @param name Name of the collection
@@ -51,9 +51,18 @@
string memory tokenPrefix
) external payable returns (address);
- /// @dev EVM selector for this function is: 0xac1e2285,
- /// or in textual repr: createRTCollection(string,uint8,string,string)
- function createRTCollection(
+ /// @dev EVM selector for this function is: 0xa5596388,
+ /// or in textual repr: createERC721MetadataCompatibleRFTCollection(string,string,string,string)
+ function createERC721MetadataCompatibleRFTCollection(
+ string memory name,
+ string memory description,
+ string memory tokenPrefix,
+ string memory baseUri
+ ) external payable returns (address);
+
+ /// @dev EVM selector for this function is: 0x7335b79f,
+ /// or in textual repr: createFTCollection(string,uint8,string,string)
+ function createFTCollection(
string memory name,
uint8 decimals,
string memory description,
tests/src/eth/collectionHelpersAbi.jsondiffbeforeafterboth--- a/tests/src/eth/collectionHelpersAbi.json
+++ b/tests/src/eth/collectionHelpersAbi.json
@@ -42,9 +42,22 @@
"inputs": [
{ "internalType": "string", "name": "name", "type": "string" },
{ "internalType": "string", "name": "description", "type": "string" },
+ { "internalType": "string", "name": "tokenPrefix", "type": "string" },
+ { "internalType": "string", "name": "baseUri", "type": "string" }
+ ],
+ "name": "createERC721MetadataCompatibleRFTCollection",
+ "outputs": [{ "internalType": "address", "name": "", "type": "address" }],
+ "stateMutability": "payable",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ { "internalType": "string", "name": "name", "type": "string" },
+ { "internalType": "uint8", "name": "decimals", "type": "uint8" },
+ { "internalType": "string", "name": "description", "type": "string" },
{ "internalType": "string", "name": "tokenPrefix", "type": "string" }
],
- "name": "createNFTCollection",
+ "name": "createFTCollection",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "payable",
"type": "function"
@@ -55,7 +68,7 @@
{ "internalType": "string", "name": "description", "type": "string" },
{ "internalType": "string", "name": "tokenPrefix", "type": "string" }
],
- "name": "createRFTCollection",
+ "name": "createNFTCollection",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "payable",
"type": "function"
@@ -63,11 +76,10 @@
{
"inputs": [
{ "internalType": "string", "name": "name", "type": "string" },
- { "internalType": "uint8", "name": "decimals", "type": "uint8" },
{ "internalType": "string", "name": "description", "type": "string" },
{ "internalType": "string", "name": "tokenPrefix", "type": "string" }
],
- "name": "createRTCollection",
+ "name": "createRFTCollection",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "payable",
"type": "function"
tests/src/eth/createFTCollection.test.tsdiffbeforeafterboth1// 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 {IKeyringPair} from '@polkadot/types/types';18import {Pallets, requirePalletsOrSkip} from '../util';19import {expect, itEth, usingEthPlaygrounds} from './util';2021const DECIMALS = 18;2223describe('Create FT collection from EVM', () => {24 let donor: IKeyringPair;2526 before(async function() {27 await usingEthPlaygrounds(async (helper, privateKey) => {28 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);29 donor = await privateKey('//Alice');30 });31 });3233 itEth('Create collection', async ({helper}) => {34 const owner = await helper.eth.createAccountWithBalance(donor);35 36 const name = 'CollectionEVM';37 const description = 'Some description';38 const prefix = 'token prefix';39 40 // todo:playgrounds this might fail when in async environment.41 const collectionCountBefore = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;42 43 const collectionCreationPrice = helper.balance.getCollectionCreationPrice();44 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);45 46 const result = await collectionHelper.methods.createRTCollection(name, DECIMALS, description, prefix).call({value: Number(collectionCreationPrice)});47 console.log(result);48 const {collectionId} = await helper.eth.createFungibleCollection(owner, name, DECIMALS, description, prefix);49 const collectionCountAfter = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;50 51 const data = (await helper.ft.getData(collectionId))!;5253 expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);54 expect(collectionId).to.be.eq(collectionCountAfter);55 expect(data.name).to.be.eq(name);56 expect(data.description).to.be.eq(description);57 expect(data.raw.tokenPrefix).to.be.eq(prefix);58 expect(data.raw.mode).to.be.deep.eq({Fungible: DECIMALS.toString()});59 });6061 // // todo:playgrounds this test will fail when in async environment.62 // itEth('Check collection address exist', async ({helper}) => {63 // const owner = await helper.eth.createAccountWithBalance(donor);6465 // const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;66 // const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);67 // const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);6869 // expect(await collectionHelpers.methods70 // .isCollectionExist(expectedCollectionAddress)71 // .call()).to.be.false;7273 // await collectionHelpers.methods74 // .createRFTCollection('A', 'A', 'A')75 // .send({value: Number(2n * helper.balance.getOneTokenNominal())});76 77 // expect(await collectionHelpers.methods78 // .isCollectionExist(expectedCollectionAddress)79 // .call()).to.be.true;80 // });81 82 // itEth('Set sponsorship', async ({helper}) => {83 // const owner = await helper.eth.createAccountWithBalance(donor);84 // const sponsor = await helper.eth.createAccountWithBalance(donor);85 // const ss58Format = helper.chain.getChainProperties().ss58Format;86 // const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');8788 // const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);89 // await collection.methods.setCollectionSponsor(sponsor).send();9091 // let data = (await helper.rft.getData(collectionId))!;92 // expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));9394 // await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');9596 // const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);97 // await sponsorCollection.methods.confirmCollectionSponsorship().send();9899 // data = (await helper.rft.getData(collectionId))!;100 // expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));101 // });102103 // itEth('Set limits', async ({helper}) => {104 // const owner = await helper.eth.createAccountWithBalance(donor);105 // const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Limits', 'absolutely anything', 'INSI');106 // const limits = {107 // accountTokenOwnershipLimit: 1000,108 // sponsoredDataSize: 1024,109 // sponsoredDataRateLimit: 30,110 // tokenLimit: 1000000,111 // sponsorTransferTimeout: 6,112 // sponsorApproveTimeout: 6,113 // ownerCanTransfer: false,114 // ownerCanDestroy: false,115 // transfersEnabled: false,116 // };117118 // const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);119 // await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();120 // await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();121 // await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();122 // await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();123 // await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();124 // await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();125 // await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();126 // await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();127 // await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();128 129 // const data = (await helper.rft.getData(collectionId))!;130 // expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);131 // expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);132 // expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);133 // expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);134 // expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);135 // expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);136 // expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);137 // expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);138 // expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);139 // });140141 // itEth('Collection address exist', async ({helper}) => {142 // const owner = await helper.eth.createAccountWithBalance(donor);143 // const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';144 // expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)145 // .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())146 // .to.be.false;147 148 // const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Exister', 'absolutely anything', 'WIWT');149 // expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)150 // .methods.isCollectionExist(collectionAddress).call())151 // .to.be.true;152 // });153});154155// describe('(!negative tests!) Create RFT collection from EVM', () => {156// let donor: IKeyringPair;157// let nominal: bigint;158159// before(async function() {160// await usingEthPlaygrounds(async (helper, privateKey) => {161// requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);162// donor = privateKey('//Alice');163// nominal = helper.balance.getOneTokenNominal();164// });165// });166167// itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {168// const owner = await helper.eth.createAccountWithBalance(donor);169// const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);170// {171// const MAX_NAME_LENGTH = 64;172// const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);173// const description = 'A';174// const tokenPrefix = 'A';175176// await expect(collectionHelper.methods177// .createRFTCollection(collectionName, description, tokenPrefix)178// .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);179// }180// {181// const MAX_DESCRIPTION_LENGTH = 256;182// const collectionName = 'A';183// const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);184// const tokenPrefix = 'A';185// await expect(collectionHelper.methods186// .createRFTCollection(collectionName, description, tokenPrefix)187// .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);188// }189// {190// const MAX_TOKEN_PREFIX_LENGTH = 16;191// const collectionName = 'A';192// const description = 'A';193// const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);194// await expect(collectionHelper.methods195// .createRFTCollection(collectionName, description, tokenPrefix)196// .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);197// }198// });199 200// itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {201// const owner = await helper.eth.createAccountWithBalance(donor);202// const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);203// await expect(collectionHelper.methods204// .createRFTCollection('Peasantry', 'absolutely anything', 'TWIW')205// .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');206// });207208// itEth('(!negative test!) Check owner', async ({helper}) => {209// const owner = await helper.eth.createAccountWithBalance(donor);210// const peasant = helper.eth.createAccount();211// const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');212// const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', peasant);213// const EXPECTED_ERROR = 'NoPermission';214// {215// const sponsor = await helper.eth.createAccountWithBalance(donor);216// await expect(peasantCollection.methods217// .setCollectionSponsor(sponsor)218// .call()).to.be.rejectedWith(EXPECTED_ERROR);219 220// const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);221// await expect(sponsorCollection.methods222// .confirmCollectionSponsorship()223// .call()).to.be.rejectedWith('caller is not set as sponsor');224// }225// {226// await expect(peasantCollection.methods227// .setCollectionLimit('account_token_ownership_limit', '1000')228// .call()).to.be.rejectedWith(EXPECTED_ERROR);229// }230// });231232// itEth('(!negative test!) Set limits', async ({helper}) => {233// const owner = await helper.eth.createAccountWithBalance(donor);234// const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Limits', 'absolutely anything', 'ISNI');235// const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);236// await expect(collectionEvm.methods237// .setCollectionLimit('badLimit', 'true')238// .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');239// });240 241// itEth('destroyCollection test', async ({helper}) => {242// const owner = await helper.eth.createAccountWithBalance(donor);243// const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Limits', 'absolutely anything', 'OLF');244// const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);245 246// await expect(collectionHelper.methods247// .destroyCollection(collectionAddress)248// .send({from: owner})).to.be.fulfilled;249 250// expect(await collectionHelper.methods251// .isCollectionExist(collectionAddress)252// .call()).to.be.false; 253// });254// });tests/src/eth/createRFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createRFTCollection.test.ts
+++ b/tests/src/eth/createRFTCollection.test.ts
@@ -264,7 +264,7 @@
.call()).to.be.rejectedWith('unknown boolean limit "badLimit"');
});
- itEth('destroyCollection test', async ({helper}) => {
+ itEth('destroyCollection', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
tests/src/eth/util/playgrounds/unique.dev.tsdiffbeforeafterboth--- a/tests/src/eth/util/playgrounds/unique.dev.ts
+++ b/tests/src/eth/util/playgrounds/unique.dev.ts
@@ -210,8 +210,7 @@
const collectionCreationPrice = this.helper.balance.getCollectionCreationPrice();
const collectionHelper = this.helper.ethNativeContract.collectionHelpers(signer);
- const result = await collectionHelper.methods.createRTCollection(name, decimals, description, tokenPrefix).send({value: Number(collectionCreationPrice)});
-
+ const result = await collectionHelper.methods.createFTCollection(name, decimals, description, tokenPrefix).send({value: Number(collectionCreationPrice)});
const collectionAddress = this.helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);
const collectionId = this.helper.ethAddress.extractCollectionId(collectionAddress);