From 3f38e5baad710814896b3704df1ad4e5ac9aee9e Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Fri, 28 Jul 2023 16:17:49 +0200 Subject: [PATCH 01/12] added collection counter --- pallets/living-assets-ownership/src/lib.rs | 9 +++++++-- pallets/living-assets-ownership/src/tests.rs | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pallets/living-assets-ownership/src/lib.rs b/pallets/living-assets-ownership/src/lib.rs index d5219cfb..8e7b149e 100644 --- a/pallets/living-assets-ownership/src/lib.rs +++ b/pallets/living-assets-ownership/src/lib.rs @@ -15,7 +15,7 @@ mod tests; #[frame_support::pallet] pub mod pallet { - use frame_support::pallet_prelude::{OptionQuery, *}; + use frame_support::pallet_prelude::{OptionQuery, ValueQuery, *}; use frame_system::pallet_prelude::*; #[pallet::pallet] @@ -27,7 +27,7 @@ pub mod pallet { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Collection id type - type CollectionId: Member + Parameter + MaxEncodedLen + Copy; + type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default; } /// Mapping from collection id to owner @@ -36,6 +36,11 @@ pub mod pallet { pub(super) type OwnerOfCollection = StorageMap<_, Blake2_128Concat, T::CollectionId, T::AccountId, OptionQuery>; + /// Collection counter + #[pallet::storage] + #[pallet::getter(fn collection_counter)] + pub(super) type CollectionCounter = StorageValue<_, T::CollectionId, ValueQuery>; + /// Pallet events #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] diff --git a/pallets/living-assets-ownership/src/tests.rs b/pallets/living-assets-ownership/src/tests.rs index 0251eb82..df62c905 100644 --- a/pallets/living-assets-ownership/src/tests.rs +++ b/pallets/living-assets-ownership/src/tests.rs @@ -46,6 +46,14 @@ mod test { }); } + #[test] + fn initially_collection_counter_is_0() { + new_test_ext().execute_with(|| { + assert_eq!(LivingAssetsModule::collection_counter(), 0); + }); + } + + // Test LivingAssetsOwnership trait #[test] fn living_assets_ownership_trait_create_new_collection_by_living() { new_test_ext().execute_with(|| { From 65136fbb4d1babf0e7c8dfa22acc3b16b64185c5 Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Fri, 28 Jul 2023 16:27:42 +0200 Subject: [PATCH 02/12] test to check the counter increase on creation --- pallets/living-assets-ownership/src/tests.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pallets/living-assets-ownership/src/tests.rs b/pallets/living-assets-ownership/src/tests.rs index df62c905..bdfd1a3f 100644 --- a/pallets/living-assets-ownership/src/tests.rs +++ b/pallets/living-assets-ownership/src/tests.rs @@ -53,6 +53,14 @@ mod test { }); } + #[test] + fn create_collection_and_check_counter() { + new_test_ext().execute_with(|| { + assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0)); + assert_eq!(LivingAssetsModule::collection_counter(), 1); + }); + } + // Test LivingAssetsOwnership trait #[test] fn living_assets_ownership_trait_create_new_collection_by_living() { From 5f67a28d21e90c484c9e452cf574cecb65aecd66 Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Fri, 28 Jul 2023 18:14:20 +0200 Subject: [PATCH 03/12] counter works --- pallets/living-assets-ownership/src/functions.rs | 16 +++++++++++++++- pallets/living-assets-ownership/src/lib.rs | 9 +++++++-- pallets/living-assets-ownership/src/tests.rs | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pallets/living-assets-ownership/src/functions.rs b/pallets/living-assets-ownership/src/functions.rs index ab3dce53..fd7d3d3d 100644 --- a/pallets/living-assets-ownership/src/functions.rs +++ b/pallets/living-assets-ownership/src/functions.rs @@ -1,6 +1,12 @@ //! Contains helper and utility functions of the pallet use super::*; -use frame_support::{ensure, sp_runtime::DispatchResult}; +use frame_support::{ + ensure, + sp_runtime::{ + traits::{CheckedAdd, One}, + DispatchResult, + }, +}; impl Pallet { /// See [Self::create_collection] @@ -13,8 +19,16 @@ impl Pallet { Error::::CollectionAlreadyExists ); + // Retrieve the current collection count + let collection_id = Self::collection_counter(); + OwnerOfCollection::::insert(collection_id, &who); + // Increment collection counter by 1 + let counter = + collection_id.checked_add(&One::one()).ok_or(Error::::CollectionIdOverflow)?; + CollectionCounter::::put(counter); + Self::deposit_event(Event::CollectionCreated { collection_id, who }); Ok(()) diff --git a/pallets/living-assets-ownership/src/lib.rs b/pallets/living-assets-ownership/src/lib.rs index 8e7b149e..b5d4b167 100644 --- a/pallets/living-assets-ownership/src/lib.rs +++ b/pallets/living-assets-ownership/src/lib.rs @@ -15,7 +15,10 @@ mod tests; #[frame_support::pallet] pub mod pallet { - use frame_support::pallet_prelude::{OptionQuery, ValueQuery, *}; + use frame_support::{ + pallet_prelude::{OptionQuery, ValueQuery, *}, + sp_runtime::traits::{CheckedAdd, One}, + }; use frame_system::pallet_prelude::*; #[pallet::pallet] @@ -27,7 +30,7 @@ pub mod pallet { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Collection id type - type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default; + type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default + CheckedAdd + One; } /// Mapping from collection id to owner @@ -55,6 +58,8 @@ pub mod pallet { pub enum Error { /// Collection already exists CollectionAlreadyExists, + /// Collection id overflow + CollectionIdOverflow, } // Dispatchable functions allows users to interact with the pallet and invoke state changes. diff --git a/pallets/living-assets-ownership/src/tests.rs b/pallets/living-assets-ownership/src/tests.rs index bdfd1a3f..08178015 100644 --- a/pallets/living-assets-ownership/src/tests.rs +++ b/pallets/living-assets-ownership/src/tests.rs @@ -56,7 +56,7 @@ mod test { #[test] fn create_collection_and_check_counter() { new_test_ext().execute_with(|| { - assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0)); + assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0)); assert_eq!(LivingAssetsModule::collection_counter(), 1); }); } From bea92725d7f7cecf9e7e4e35d465944f70216009 Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Fri, 28 Jul 2023 18:22:19 +0200 Subject: [PATCH 04/12] create_collection generate the collectionId --- .../living-assets-ownership/src/functions.rs | 54 ++++++++++++------- pallets/living-assets-ownership/src/lib.rs | 13 ++--- pallets/living-assets-ownership/src/tests.rs | 36 +++---------- 3 files changed, 48 insertions(+), 55 deletions(-) diff --git a/pallets/living-assets-ownership/src/functions.rs b/pallets/living-assets-ownership/src/functions.rs index fd7d3d3d..1e5beb4c 100644 --- a/pallets/living-assets-ownership/src/functions.rs +++ b/pallets/living-assets-ownership/src/functions.rs @@ -1,36 +1,54 @@ //! Contains helper and utility functions of the pallet use super::*; -use frame_support::{ - ensure, - sp_runtime::{ - traits::{CheckedAdd, One}, - DispatchResult, - }, +use frame_support::sp_runtime::{ + traits::{CheckedAdd, One}, + DispatchResult, }; impl Pallet { - /// See [Self::create_collection] - pub fn do_create_collection( - collection_id: T::CollectionId, - who: T::AccountId, - ) -> DispatchResult { - ensure!( - !OwnerOfCollection::::contains_key(collection_id), - Error::::CollectionAlreadyExists - ); - - // Retrieve the current collection count + /// Attempts to create a new collection + /// + /// This function is intended to be used as the implementation for the + /// [Self::create_collection] public interface. It first retrieves the current + /// collection count to use as the new collection's ID. It then inserts a new + /// entry into the `OwnerOfCollection` map, mapping the new collection's ID to + /// the owner's account ID. + /// + /// After this, the function attempts to increment the collection counter by 1. + /// If this operation would result in an overflow, the function returns early + /// with an [Error::CollectionIdOverflow]. + /// + /// Finally, if all operations were successful, the function emits a + /// [Event::CollectionCreated] event and returns `Ok(())`. + /// + /// # Arguments + /// + /// * `who` - The account ID of the new collection's owner. + /// + /// # Return + /// + /// Returns a [DispatchResult] indicating the outcome of the operation. If the + /// operation was successful, the function returns `Ok(())`. If the operation + /// was not successful, the function returns `Err(e)`, where `e` is the error + /// that occurred. + pub fn do_create_collection(who: T::AccountId) -> DispatchResult { + // Retrieve the current collection count to use as the new collection's ID let collection_id = Self::collection_counter(); + // Insert a new entry into the OwnerOfCollection map, mapping the new + // collection's ID to the owner's account ID OwnerOfCollection::::insert(collection_id, &who); - // Increment collection counter by 1 + // Attempt to increment the collection counter by 1. If this operation + // would result in an overflow, return early with an error let counter = collection_id.checked_add(&One::one()).ok_or(Error::::CollectionIdOverflow)?; CollectionCounter::::put(counter); + // Emit an event indicating that a new collection was created Self::deposit_event(Event::CollectionCreated { collection_id, who }); + // Return Ok to indicate that the operation was successful Ok(()) } } diff --git a/pallets/living-assets-ownership/src/lib.rs b/pallets/living-assets-ownership/src/lib.rs index b5d4b167..e06e564e 100644 --- a/pallets/living-assets-ownership/src/lib.rs +++ b/pallets/living-assets-ownership/src/lib.rs @@ -69,12 +69,9 @@ pub mod pallet { impl Pallet { #[pallet::call_index(0)] #[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())] // TODO set proper weight - pub fn create_collection( - origin: OriginFor, - collection_id: T::CollectionId, - ) -> DispatchResult { + pub fn create_collection(origin: OriginFor) -> DispatchResult { let who = ensure_signed(origin)?; - Self::do_create_collection(collection_id, who) + Self::do_create_collection(who) } } @@ -106,7 +103,7 @@ pub mod pallet { fn owner_of_collection(collection_id: CollectionId) -> Option; /// Create collection - fn create_collection(collection_id: CollectionId, who: AccountId) -> DispatchResult; + fn create_collection(who: AccountId) -> DispatchResult; } impl LivingAssetsOwnership for Pallet { @@ -114,8 +111,8 @@ pub mod pallet { OwnerOfCollection::::get(collection_id) } - fn create_collection(collection_id: T::CollectionId, who: T::AccountId) -> DispatchResult { - Self::do_create_collection(collection_id, who) + fn create_collection(who: T::AccountId) -> DispatchResult { + Self::do_create_collection(who) } } } diff --git a/pallets/living-assets-ownership/src/tests.rs b/pallets/living-assets-ownership/src/tests.rs index 08178015..318fe389 100644 --- a/pallets/living-assets-ownership/src/tests.rs +++ b/pallets/living-assets-ownership/src/tests.rs @@ -1,5 +1,5 @@ -use crate::{mock::*, Error, Event, LivingAssetsOwnership}; -use frame_support::{assert_noop, assert_ok}; +use crate::{mock::*, Event, LivingAssetsOwnership}; +use frame_support::assert_ok; #[cfg(test)] mod test { @@ -19,29 +19,18 @@ mod test { #[test] fn create_new_collection() { new_test_ext().execute_with(|| { - assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0)); + assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1))); assert_eq!(LivingAssetsModule::owner_of_collection(0), Some(1)); }); } - #[test] - fn create_an_existing_collection_should_fail() { - new_test_ext().execute_with(|| { - assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0)); - assert_noop!( - LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0), - Error::::CollectionAlreadyExists - ); - }); - } - #[test] fn create_new_collection_should_emit_an_event() { new_test_ext().execute_with(|| { // Go past genesis block so events get deposited System::set_block_number(1); - assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0)); + assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1))); System::assert_last_event(Event::CollectionCreated { collection_id: 0, who: 1 }.into()); }); } @@ -56,7 +45,7 @@ mod test { #[test] fn create_collection_and_check_counter() { new_test_ext().execute_with(|| { - assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0)); + assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1))); assert_eq!(LivingAssetsModule::collection_counter(), 1); }); } @@ -65,7 +54,7 @@ mod test { #[test] fn living_assets_ownership_trait_create_new_collection_by_living() { new_test_ext().execute_with(|| { - let result = >::create_collection(0, 1); + let result = >::create_collection(1); assert_ok!(result); assert_eq!(LivingAssetsModule::owner_of_collection(0), Some(1)); }); @@ -79,24 +68,13 @@ mod test { }); } - #[test] - fn living_assets_ownership_trait_create_an_existing_collection_should_fail() { - new_test_ext().execute_with(|| { - assert_ok!(>::create_collection(0, 1)); - assert_noop!( - >::create_collection(0, 1), - Error::::CollectionAlreadyExists - ); - }); - } - #[test] fn living_assets_ownership_trait_create_new_collection_should_emit_an_event() { new_test_ext().execute_with(|| { // Go past genesis block so events get deposited System::set_block_number(1); - assert_ok!(>::create_collection(0, 1)); + assert_ok!(>::create_collection( 1)); System::assert_last_event(Event::CollectionCreated { collection_id: 0, who: 1 }.into()); }); } From 460066cf01cdf657e77dacd9385c38c8b602ab84 Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Fri, 28 Jul 2023 19:25:07 +0200 Subject: [PATCH 05/12] fix prevcompiled comntract --- .../LivingAssetsOwnershipWrapper.sol | 23 ------------------- precompile/living-assets/src/lib.rs | 7 +++--- 2 files changed, 3 insertions(+), 27 deletions(-) delete mode 100644 precompile/living-assets/contracts/LivingAssetsOwnershipWrapper.sol diff --git a/precompile/living-assets/contracts/LivingAssetsOwnershipWrapper.sol b/precompile/living-assets/contracts/LivingAssetsOwnershipWrapper.sol deleted file mode 100644 index eab17502..00000000 --- a/precompile/living-assets/contracts/LivingAssetsOwnershipWrapper.sol +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-only -pragma solidity >=0.8.3; -import "./LivingAssetsOwnership.sol"; - -contract A { - LivingAssets public constant LIVING_ASSETS = - LivingAssets(0x0000000000000000000000000000000000000402); - - /// @notice Get owner of collection - function ownerOfCollection( - uint64 collection_id - ) public view returns (bytes32) { - return LIVING_ASSETS.ownerOfCollection(collection_id); - } - - /// @notice Create collection - function createCollection( - uint64 collection_id, - address who - ) public payable { - LIVING_ASSETS.createCollection(collection_id, who); - } -} diff --git a/precompile/living-assets/src/lib.rs b/precompile/living-assets/src/lib.rs index 91509c38..78f1cd70 100644 --- a/precompile/living-assets/src/lib.rs +++ b/precompile/living-assets/src/lib.rs @@ -17,7 +17,7 @@ use sp_std::{fmt::Debug, marker::PhantomData}; #[derive(Debug, PartialEq)] pub enum Action { /// Create a new collection - CreateCollection = "createCollection(uint64,address)", + CreateCollection = "createCollection(address)", /// Get owner of the collection OwnerOfCollection = "ownerOfCollection(uint64)", } @@ -85,12 +85,11 @@ where // write storage Action::CreateCollection => { let mut input = handle.read_input()?; - input.expect_arguments(2)?; + input.expect_arguments(1)?; - let collection_id = input.read::()?.saturated_into(); let owner = AddressMapping::into_account_id(input.read::
()?.0); - if LivingAssets::create_collection(collection_id, owner).is_err() { + if LivingAssets::create_collection(owner).is_err() { return Err(PrecompileFailure::Error { exit_status: ExitError::Other(sp_std::borrow::Cow::Borrowed( "Could net create collection", From bcb3329360520cb9bc4d923f802e340e681b3e8b Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Sat, 29 Jul 2023 10:13:00 +0200 Subject: [PATCH 06/12] collectio_id is not payable --- precompile/living-assets/contracts/LivingAssetsOwnership.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/precompile/living-assets/contracts/LivingAssetsOwnership.sol b/precompile/living-assets/contracts/LivingAssetsOwnership.sol index fb8f65ef..8bac6610 100644 --- a/precompile/living-assets/contracts/LivingAssetsOwnership.sol +++ b/precompile/living-assets/contracts/LivingAssetsOwnership.sol @@ -13,7 +13,7 @@ interface LivingAssets { function createCollection( uint64 collection_id, address who - ) external payable; + ) external; /// @dev Get collection owner /// @custom:selector 0xfb34ae53 From 0fc56da5d37f613ba9796dddaf58d19a784103d4 Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Sat, 29 Jul 2023 14:36:18 +0200 Subject: [PATCH 07/12] refactoring --- pallets/living-assets-ownership/src/lib.rs | 37 +++---------------- pallets/living-assets-ownership/src/tests.rs | 12 +++--- pallets/living-assets-ownership/src/traits.rs | 32 ++++++++++++++++ .../contracts/LivingAssetsOwnership.sol | 2 +- precompile/living-assets/src/lib.rs | 14 +++---- runtime/src/precompiles.rs | 4 +- 6 files changed, 53 insertions(+), 48 deletions(-) create mode 100644 pallets/living-assets-ownership/src/traits.rs diff --git a/pallets/living-assets-ownership/src/lib.rs b/pallets/living-assets-ownership/src/lib.rs index e06e564e..393f140f 100644 --- a/pallets/living-assets-ownership/src/lib.rs +++ b/pallets/living-assets-ownership/src/lib.rs @@ -4,8 +4,10 @@ /// Learn more about FRAME and the core library of Substrate FRAME pallets: /// pub use pallet::*; +use frame_support::dispatch::DispatchResult; mod functions; +pub mod traits; #[cfg(test)] mod mock; @@ -75,38 +77,10 @@ pub mod pallet { } } - /// The `LivingAssetsOwnership` trait provides an interface for managing collections in a - /// decentralized and non-fungible asset management system. This system allows for the creation of - /// collections, each of which can be owned by a unique `AccountId`. - /// - /// A collection in this context can be thought of as a container for non-fungible assets. - /// Each collection has an associated `collection_id` which is a unique identifier for the collection - /// and can be used to retrieve the owner of the collection. - /// - /// # Methods - /// - /// - `owner_of_collection(collection_id: T::CollectionId) -> Option`: This method retrieves the owner - /// of a collection given its `collection_id`. If no collection exists with the provided `collection_id`, - /// the method returns `None`. - /// - /// - `create_collection(collection_id: T::CollectionId, who: AccountId) -> DispatchResult`: This method creates a - /// new collection with the specified `collection_id` and assigns ownership to the provided `AccountId`. - /// If a collection already exists with the provided `collection_id`, the method will return an error. - /// - /// # Errors - /// - /// - `CollectionAlreadyExists`: This error is returned by the `create_collection` method when a collection - /// with the provided `collection_id` already exists. - /// - pub trait LivingAssetsOwnership { - /// Get owner of collection - fn owner_of_collection(collection_id: CollectionId) -> Option; - - /// Create collection - fn create_collection(who: AccountId) -> DispatchResult; - } - impl LivingAssetsOwnership for Pallet { +} + + impl traits::CollectionManager for Pallet { fn owner_of_collection(collection_id: T::CollectionId) -> Option { OwnerOfCollection::::get(collection_id) } @@ -115,4 +89,3 @@ pub mod pallet { Self::do_create_collection(who) } } -} diff --git a/pallets/living-assets-ownership/src/tests.rs b/pallets/living-assets-ownership/src/tests.rs index 318fe389..af17cb7f 100644 --- a/pallets/living-assets-ownership/src/tests.rs +++ b/pallets/living-assets-ownership/src/tests.rs @@ -1,4 +1,4 @@ -use crate::{mock::*, Event, LivingAssetsOwnership}; +use crate::{mock::*, traits::CollectionManager, Event}; use frame_support::assert_ok; #[cfg(test)] @@ -50,11 +50,11 @@ mod test { }); } - // Test LivingAssetsOwnership trait + // Test CollectionManager trait #[test] fn living_assets_ownership_trait_create_new_collection_by_living() { new_test_ext().execute_with(|| { - let result = >::create_collection(1); + let result = >::create_collection(1); assert_ok!(result); assert_eq!(LivingAssetsModule::owner_of_collection(0), Some(1)); }); @@ -63,8 +63,8 @@ mod test { #[test] fn living_assets_ownership_trait_owner_of_unexistent_collection_is_none() { new_test_ext().execute_with(|| { - assert_eq!(>::owner_of_collection(0), None); - assert_eq!(>::owner_of_collection(1), None); + assert_eq!(>::owner_of_collection(0), None); + assert_eq!(>::owner_of_collection(1), None); }); } @@ -74,7 +74,7 @@ mod test { // Go past genesis block so events get deposited System::set_block_number(1); - assert_ok!(>::create_collection( 1)); + assert_ok!(>::create_collection( 1)); System::assert_last_event(Event::CollectionCreated { collection_id: 0, who: 1 }.into()); }); } diff --git a/pallets/living-assets-ownership/src/traits.rs b/pallets/living-assets-ownership/src/traits.rs new file mode 100644 index 00000000..edd9a59a --- /dev/null +++ b/pallets/living-assets-ownership/src/traits.rs @@ -0,0 +1,32 @@ +use frame_support::dispatch::DispatchResult; + +/// The `CollectionManager` trait provides an interface for managing collections in a +/// decentralized and non-fungible asset management system. This system allows for the creation of +/// collections, each of which can be owned by a unique `AccountId`. +/// +/// A collection in this context can be thought of as a container for non-fungible assets. +/// Each collection has an associated `collection_id` which is a unique identifier for the collection +/// and can be used to retrieve the owner of the collection. +/// +/// # Methods +/// +/// - `owner_of_collection(collection_id: T::CollectionId) -> Option`: This method retrieves the owner +/// of a collection given its `collection_id`. If no collection exists with the provided `collection_id`, +/// the method returns `None`. +/// +/// - `create_collection(collection_id: T::CollectionId, who: AccountId) -> DispatchResult`: This method creates a +/// new collection with the specified `collection_id` and assigns ownership to the provided `AccountId`. +/// If a collection already exists with the provided `collection_id`, the method will return an error. +/// +/// # Errors +/// +/// - `CollectionAlreadyExists`: This error is returned by the `create_collection` method when a collection +/// with the provided `collection_id` already exists. +/// +pub trait CollectionManager { + /// Get owner of collection + fn owner_of_collection(collection_id: CollectionId) -> Option; + + /// Create collection + fn create_collection(who: AccountId) -> DispatchResult; +} diff --git a/precompile/living-assets/contracts/LivingAssetsOwnership.sol b/precompile/living-assets/contracts/LivingAssetsOwnership.sol index 8bac6610..85b2f7f7 100644 --- a/precompile/living-assets/contracts/LivingAssetsOwnership.sol +++ b/precompile/living-assets/contracts/LivingAssetsOwnership.sol @@ -2,7 +2,7 @@ pragma solidity >=0.8.3; /// @author Freeverse team -/// @title Pallet LivingAssetsOwnership Interface +/// @title Pallet CollectionManager Interface /// @dev The interface through which solidity contracts will interact with pallet-living-assets /// @custom:address 0x0000000000000000000000000000000000000402 interface LivingAssets { diff --git a/precompile/living-assets/src/lib.rs b/precompile/living-assets/src/lib.rs index 78f1cd70..cad40c24 100644 --- a/precompile/living-assets/src/lib.rs +++ b/precompile/living-assets/src/lib.rs @@ -3,7 +3,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(test, feature(assert_matches))] use fp_evm::{ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput}; -use pallet_living_assets_ownership::LivingAssetsOwnership; +use pallet_living_assets_ownership::traits::CollectionManager; use parity_scale_codec::Encode; use precompile_utils::{ succeed, Address, EvmDataWriter, EvmResult, FunctionModifier, PrecompileHandleExt, @@ -23,22 +23,22 @@ pub enum Action { } /// Wrapper for the precompile function. -pub struct LivingAssetsOwnershipPrecompile( +pub struct CollectionManagerPrecompile( PhantomData<(AddressMapping, AccountId, CollectionId, LivingAssets)>, ) where AddressMapping: pallet_evm::AddressMapping, AccountId: Encode + Debug, CollectionId: BaseArithmetic + Debug, - LivingAssets: LivingAssetsOwnership; + LivingAssets: CollectionManager; impl - LivingAssetsOwnershipPrecompile + CollectionManagerPrecompile where AddressMapping: pallet_evm::AddressMapping, AccountId: Encode + Debug, CollectionId: BaseArithmetic + Debug, - LivingAssets: LivingAssetsOwnership, + LivingAssets: CollectionManager, { #[allow(clippy::new_without_default)] pub fn new() -> Self { @@ -47,12 +47,12 @@ where } impl fp_evm::Precompile - for LivingAssetsOwnershipPrecompile + for CollectionManagerPrecompile where AddressMapping: pallet_evm::AddressMapping, AccountId: Encode + Debug, CollectionId: BaseArithmetic + Debug, - LivingAssets: LivingAssetsOwnership, + LivingAssets: CollectionManager, { fn execute(handle: &mut impl PrecompileHandle) -> EvmResult { let selector = handle.read_selector()?; diff --git a/runtime/src/precompiles.rs b/runtime/src/precompiles.rs index 8aaa6e01..b80c9536 100644 --- a/runtime/src/precompiles.rs +++ b/runtime/src/precompiles.rs @@ -7,7 +7,7 @@ use polkadot_primitives::BlakeTwo256; use sp_core::H160; use sp_std::marker::PhantomData; -use pallet_evm_living_assets_ownership::LivingAssetsOwnershipPrecompile; +use pallet_evm_living_assets_ownership::CollectionManagerPrecompile; use pallet_evm_precompile_modexp::Modexp; use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256}; @@ -27,7 +27,7 @@ where } } -type LivingAssetsPrecompile = LivingAssetsOwnershipPrecompile< +type LivingAssetsPrecompile = CollectionManagerPrecompile< pallet_evm::HashedAddressMapping, AccountId, CollectionId, From 49c425b24bb026252504766a1b75f5c35832092e Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Sat, 29 Jul 2023 14:41:49 +0200 Subject: [PATCH 08/12] fmt --- pallets/living-assets-ownership/src/lib.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pallets/living-assets-ownership/src/lib.rs b/pallets/living-assets-ownership/src/lib.rs index 393f140f..16c3a3b7 100644 --- a/pallets/living-assets-ownership/src/lib.rs +++ b/pallets/living-assets-ownership/src/lib.rs @@ -1,10 +1,10 @@ #![cfg_attr(not(feature = "std"), no_std)] +use frame_support::dispatch::DispatchResult; /// Edit this file to define custom logic or remove it if it is not needed. /// Learn more about FRAME and the core library of Substrate FRAME pallets: /// pub use pallet::*; -use frame_support::dispatch::DispatchResult; mod functions; pub mod traits; @@ -76,16 +76,14 @@ pub mod pallet { Self::do_create_collection(who) } } - - } - impl traits::CollectionManager for Pallet { - fn owner_of_collection(collection_id: T::CollectionId) -> Option { - OwnerOfCollection::::get(collection_id) - } +impl traits::CollectionManager for Pallet { + fn owner_of_collection(collection_id: T::CollectionId) -> Option { + OwnerOfCollection::::get(collection_id) + } - fn create_collection(who: T::AccountId) -> DispatchResult { - Self::do_create_collection(who) - } + fn create_collection(who: T::AccountId) -> DispatchResult { + Self::do_create_collection(who) } +} From 021de5ac922f942d7eae092572da1f03555c21a7 Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Sat, 29 Jul 2023 15:12:09 +0200 Subject: [PATCH 09/12] CollectionId is Unsigned --- Cargo.lock | 1 + pallets/living-assets-ownership/Cargo.toml | 1 + pallets/living-assets-ownership/src/lib.rs | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2009360d..d13c4c9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6719,6 +6719,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", + "sp-arithmetic", "sp-core", "sp-io", "sp-runtime", diff --git a/pallets/living-assets-ownership/Cargo.toml b/pallets/living-assets-ownership/Cargo.toml index 9b4a4f32..85ff1af9 100644 --- a/pallets/living-assets-ownership/Cargo.toml +++ b/pallets/living-assets-ownership/Cargo.toml @@ -19,6 +19,7 @@ scale-info = { workspace = true, features = ["derive"] } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } frame-system = { workspace = true } +sp-arithmetic = { workspace = true } [dev-dependencies] serde = { workspace = true } diff --git a/pallets/living-assets-ownership/src/lib.rs b/pallets/living-assets-ownership/src/lib.rs index 16c3a3b7..1c5b3517 100644 --- a/pallets/living-assets-ownership/src/lib.rs +++ b/pallets/living-assets-ownership/src/lib.rs @@ -17,6 +17,7 @@ mod tests; #[frame_support::pallet] pub mod pallet { + use sp_arithmetic::traits::Unsigned; use frame_support::{ pallet_prelude::{OptionQuery, ValueQuery, *}, sp_runtime::traits::{CheckedAdd, One}, @@ -32,7 +33,7 @@ pub mod pallet { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Collection id type - type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default + CheckedAdd + One; + type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default + CheckedAdd + One + Unsigned; } /// Mapping from collection id to owner From 5dd8c0e984a495a67b069049e2e987b8e3fcebfa Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Sat, 29 Jul 2023 15:30:40 +0200 Subject: [PATCH 10/12] fmt --- pallets/living-assets-ownership/src/lib.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pallets/living-assets-ownership/src/lib.rs b/pallets/living-assets-ownership/src/lib.rs index 1c5b3517..bc0c7d5c 100644 --- a/pallets/living-assets-ownership/src/lib.rs +++ b/pallets/living-assets-ownership/src/lib.rs @@ -17,12 +17,12 @@ mod tests; #[frame_support::pallet] pub mod pallet { - use sp_arithmetic::traits::Unsigned; use frame_support::{ pallet_prelude::{OptionQuery, ValueQuery, *}, sp_runtime::traits::{CheckedAdd, One}, }; use frame_system::pallet_prelude::*; + use sp_arithmetic::traits::Unsigned; #[pallet::pallet] pub struct Pallet(_); @@ -33,7 +33,14 @@ pub mod pallet { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Collection id type - type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default + CheckedAdd + One + Unsigned; + type CollectionId: Member + + Parameter + + MaxEncodedLen + + Copy + + Default + + CheckedAdd + + One + + Unsigned; } /// Mapping from collection id to owner From d2279f5b8abb273b1475fa5fde8d201d73b1688f Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Sat, 29 Jul 2023 15:56:13 +0200 Subject: [PATCH 11/12] rollback name refactring for precompile --- precompile/living-assets/src/lib.rs | 6 +++--- runtime/src/precompiles.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/precompile/living-assets/src/lib.rs b/precompile/living-assets/src/lib.rs index cad40c24..6148da90 100644 --- a/precompile/living-assets/src/lib.rs +++ b/precompile/living-assets/src/lib.rs @@ -23,7 +23,7 @@ pub enum Action { } /// Wrapper for the precompile function. -pub struct CollectionManagerPrecompile( +pub struct LivingAssetsOwnershipPrecompile( PhantomData<(AddressMapping, AccountId, CollectionId, LivingAssets)>, ) where @@ -33,7 +33,7 @@ where LivingAssets: CollectionManager; impl - CollectionManagerPrecompile + LivingAssetsOwnershipPrecompile where AddressMapping: pallet_evm::AddressMapping, AccountId: Encode + Debug, @@ -47,7 +47,7 @@ where } impl fp_evm::Precompile - for CollectionManagerPrecompile + for LivingAssetsOwnershipPrecompile where AddressMapping: pallet_evm::AddressMapping, AccountId: Encode + Debug, diff --git a/runtime/src/precompiles.rs b/runtime/src/precompiles.rs index b80c9536..8aaa6e01 100644 --- a/runtime/src/precompiles.rs +++ b/runtime/src/precompiles.rs @@ -7,7 +7,7 @@ use polkadot_primitives::BlakeTwo256; use sp_core::H160; use sp_std::marker::PhantomData; -use pallet_evm_living_assets_ownership::CollectionManagerPrecompile; +use pallet_evm_living_assets_ownership::LivingAssetsOwnershipPrecompile; use pallet_evm_precompile_modexp::Modexp; use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256}; @@ -27,7 +27,7 @@ where } } -type LivingAssetsPrecompile = CollectionManagerPrecompile< +type LivingAssetsPrecompile = LivingAssetsOwnershipPrecompile< pallet_evm::HashedAddressMapping, AccountId, CollectionId, From e46f0dd1ca776089cce0658c95bb757da82c19ab Mon Sep 17 00:00:00 2001 From: Alessandro Siniscalchi Date: Sat, 29 Jul 2023 15:57:22 +0200 Subject: [PATCH 12/12] rollback name refactring for precompile --- precompile/living-assets/contracts/LivingAssetsOwnership.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/precompile/living-assets/contracts/LivingAssetsOwnership.sol b/precompile/living-assets/contracts/LivingAssetsOwnership.sol index 85b2f7f7..8bac6610 100644 --- a/precompile/living-assets/contracts/LivingAssetsOwnership.sol +++ b/precompile/living-assets/contracts/LivingAssetsOwnership.sol @@ -2,7 +2,7 @@ pragma solidity >=0.8.3; /// @author Freeverse team -/// @title Pallet CollectionManager Interface +/// @title Pallet LivingAssetsOwnership Interface /// @dev The interface through which solidity contracts will interact with pallet-living-assets /// @custom:address 0x0000000000000000000000000000000000000402 interface LivingAssets {