From e4de689ce26bea6ab57669915214d46d7d665bbd Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Wed, 26 Apr 2023 13:09:44 +0000 Subject: [PATCH] feat: impl dispatchable methods --- --- a/pallets/balances-adapter/src/common.rs +++ b/pallets/balances-adapter/src/common.rs @@ -1,7 +1,12 @@ -use alloc::vec::Vec; +use alloc::{vec, vec::Vec}; use core::marker::PhantomData; use crate::{Config, NativeFungibleHandle}; -use pallet_common::{CommonCollectionOperations, CommonWeightInfo}; +use frame_support::{ + traits::{Currency, ExistenceRequirement}, + fail, +}; +use pallet_common::{erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo, with_weight}; +use up_data_structs::TokenId; pub struct CommonWeights(PhantomData); impl CommonWeightInfo for CommonWeights { @@ -92,7 +97,7 @@ data: up_data_structs::CreateItemData, nesting_budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn create_multiple_items( @@ -102,7 +107,7 @@ data: Vec, nesting_budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn create_multiple_items_ex( @@ -111,26 +116,26 @@ data: up_data_structs::CreateItemExData<::CrossAccountId>, nesting_budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn burn_item( &self, sender: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, amount: u128, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn burn_item_recursively( &self, sender: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, self_budget: &dyn up_data_structs::budget::Budget, breadth_budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn set_collection_properties( @@ -138,7 +143,7 @@ sender: ::CrossAccountId, properties: Vec, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn delete_collection_properties( @@ -146,27 +151,27 @@ sender: &::CrossAccountId, property_keys: Vec, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn set_token_properties( &self, sender: ::CrossAccountId, - token_id: up_data_structs::TokenId, + token_id: TokenId, properties: Vec, budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn delete_token_properties( &self, sender: ::CrossAccountId, - token_id: up_data_structs::TokenId, + token_id: TokenId, property_keys: Vec, budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn set_token_property_permissions( @@ -174,28 +179,36 @@ sender: &::CrossAccountId, property_permissions: Vec, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn transfer( &self, sender: ::CrossAccountId, to: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, amount: u128, budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + with_weight( + ::Currency::transfer( + sender.as_sub(), + to.as_sub(), + amount.into(), + ExistenceRequirement::KeepAlive, + ), + Default::default(), + ) } fn approve( &self, sender: ::CrossAccountId, spender: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, amount: u128, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn approve_from( @@ -203,10 +216,10 @@ sender: ::CrossAccountId, from: ::CrossAccountId, to: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, amount: u128, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn transfer_from( @@ -214,120 +227,129 @@ sender: ::CrossAccountId, from: ::CrossAccountId, to: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, amount: u128, budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + with_weight( + ::Currency::transfer( + sender.as_sub(), + to.as_sub(), + amount.into(), + ExistenceRequirement::KeepAlive, + ), + Default::default(), + ) } fn burn_from( &self, sender: ::CrossAccountId, from: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, amount: u128, budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn check_nesting( &self, sender: ::CrossAccountId, - from: (up_data_structs::CollectionId, up_data_structs::TokenId), - under: up_data_structs::TokenId, + from: (up_data_structs::CollectionId, TokenId), + under: TokenId, budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::sp_runtime::DispatchResult { - todo!() + fail!(>::UnsupportedOperation); } - fn nest( - &self, - under: up_data_structs::TokenId, - to_nest: (up_data_structs::CollectionId, up_data_structs::TokenId), - ) { - todo!() - } + fn nest(&self, under: TokenId, to_nest: (up_data_structs::CollectionId, TokenId)) {} - fn unnest( - &self, - under: up_data_structs::TokenId, - to_nest: (up_data_structs::CollectionId, up_data_structs::TokenId), - ) { - todo!() - } + fn unnest(&self, under: TokenId, to_nest: (up_data_structs::CollectionId, TokenId)) {} - fn account_tokens(&self, account: ::CrossAccountId) -> Vec { - todo!() + fn account_tokens(&self, account: ::CrossAccountId) -> Vec { + let balance = ::Currency::total_balance(account.as_sub()); + if balance != 0 { + vec![TokenId::default()] + } else { + vec![] + } } - fn collection_tokens(&self) -> Vec { - todo!() + fn collection_tokens(&self) -> Vec { + vec![TokenId::default()] } - fn token_exists(&self, token: up_data_structs::TokenId) -> bool { - todo!() + fn token_exists(&self, token: TokenId) -> bool { + token == TokenId::default() } - fn last_token_id(&self) -> up_data_structs::TokenId { - todo!() + fn last_token_id(&self) -> TokenId { + TokenId::default() } fn token_owner( &self, - token: up_data_structs::TokenId, + token: TokenId, ) -> Result<::CrossAccountId, up_data_structs::TokenOwnerError> { - todo!() + Err(up_data_structs::TokenOwnerError::MultipleOwners) } - fn token_owners(&self, token: up_data_structs::TokenId) -> Vec<::CrossAccountId> { - todo!() + fn token_owners(&self, token: TokenId) -> Vec<::CrossAccountId> { + vec![] } fn token_property( &self, - token_id: up_data_structs::TokenId, + token_id: TokenId, key: &up_data_structs::PropertyKey, ) -> Option { - todo!() + None } fn token_properties( &self, - token: up_data_structs::TokenId, + token: TokenId, keys: Option>, ) -> Vec { - todo!() + vec![] } fn total_supply(&self) -> u32 { - todo!() + 1 } fn account_balance(&self, account: ::CrossAccountId) -> u32 { - todo!() + let balance: u128 = ::Currency::free_balance(account.as_sub()).into(); + (balance != 0).into() } - fn balance(&self, account: ::CrossAccountId, token: up_data_structs::TokenId) -> u128 { - todo!() + fn balance(&self, account: ::CrossAccountId, token: TokenId) -> u128 { + if token != TokenId::default() { + return 0; + } + ::Currency::free_balance(account.as_sub()).into() } - fn total_pieces(&self, token: up_data_structs::TokenId) -> Option { - todo!() + fn total_pieces(&self, token: TokenId) -> Option { + if token != TokenId::default() { + return None; + } + let total = ::Currency::total_issuance(); + Some(total.into()) } fn allowance( &self, sender: ::CrossAccountId, spender: ::CrossAccountId, - token: up_data_structs::TokenId, + token: TokenId, ) -> u128 { - todo!() + 0 } fn refungible_extensions(&self) -> Option<&dyn pallet_common::RefungibleExtensions> { - todo!() + None } fn set_allowance_for_all( @@ -336,17 +358,17 @@ operator: ::CrossAccountId, approve: bool, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } fn allowance_for_all(&self, owner: ::CrossAccountId, operator: ::CrossAccountId) -> bool { - todo!() + false } fn repair_item( &self, - token: up_data_structs::TokenId, + token: TokenId, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - todo!() + fail!(>::UnsupportedOperation); } } --- a/pallets/balances-adapter/src/lib.rs +++ b/pallets/balances-adapter/src/lib.rs @@ -44,7 +44,7 @@ Self::AccountId, Balance = Self::CurrencyBalance, >; - type CurrencyBalance: Into + TryFrom; + type CurrencyBalance: Into + TryFrom + PartialEq + From + Into; type Decimals: Get; type Name: Get; --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -462,6 +462,7 @@ } const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + pub const NATIVE_FINGIBLE_COLLECTION_ID: CollectionId = CollectionId(0); #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] --- a/pallets/unique/src/lib.rs +++ b/pallets/unique/src/lib.rs @@ -440,6 +440,10 @@ collection_id: CollectionId, address: T::CrossAccountId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } + let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let collection = >::try_get(collection_id)?; collection.check_is_internal()?; @@ -467,6 +471,10 @@ collection_id: CollectionId, address: T::CrossAccountId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } + let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let collection = >::try_get(collection_id)?; collection.check_is_internal()?; @@ -493,6 +501,9 @@ collection_id: CollectionId, new_owner: T::AccountId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let new_owner = T::CrossAccountId::from_sub(new_owner); let mut target_collection = >::try_get(collection_id)?; @@ -522,6 +533,9 @@ collection_id: CollectionId, new_admin_id: T::CrossAccountId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let collection = >::try_get(collection_id)?; >::toggle_admin(&collection, &sender, &new_admin_id, true) @@ -548,6 +562,9 @@ collection_id: CollectionId, account_id: T::CrossAccountId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let collection = >::try_get(collection_id)?; >::toggle_admin(&collection, &sender, &account_id, false) @@ -573,6 +590,9 @@ collection_id: CollectionId, new_sponsor: T::AccountId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let mut target_collection = >::try_get(collection_id)?; target_collection.set_sponsor(&sender, new_sponsor.clone()) @@ -597,6 +617,9 @@ origin: OriginFor, collection_id: CollectionId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = ensure_signed(origin)?; let mut target_collection = >::try_get(collection_id)?; target_collection.confirm_sponsorship(&sender) @@ -617,6 +640,9 @@ origin: OriginFor, collection_id: CollectionId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let mut target_collection = >::try_get(collection_id)?; target_collection.remove_sponsor(&sender) @@ -894,6 +920,9 @@ collection_id: CollectionId, value: bool, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let mut target_collection = >::try_get(collection_id)?; target_collection.check_is_internal()?; @@ -1146,6 +1175,9 @@ collection_id: CollectionId, new_limit: CollectionLimits, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let mut target_collection = >::try_get(collection_id)?; >::update_limits(&sender, &mut target_collection, new_limit) @@ -1170,6 +1202,9 @@ collection_id: CollectionId, new_permission: CollectionPermissions, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let mut target_collection = >::try_get(collection_id)?; >::update_permissions(&sender, &mut target_collection, new_permission) @@ -1238,6 +1273,9 @@ origin: OriginFor, collection_id: CollectionId, ) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } ensure_root(origin)?; >::repair_collection(collection_id) } --- a/runtime/common/dispatch.rs +++ b/runtime/common/dispatch.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . -use frame_support::{dispatch::DispatchResult, ensure}; +use frame_support::{dispatch::DispatchResult, ensure, fail}; use pallet_evm::{PrecompileHandle, PrecompileResult}; use sp_core::H160; use sp_runtime::DispatchError; @@ -100,6 +100,10 @@ } fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult { + if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID { + fail!(>::UnsupportedOperation); + } + let collection = >::try_get(collection_id)?; collection.check_is_internal()?; -- gitstuff