From 2ea8e6dfd7e9dba7ac4fa9cdd23a8cdd0d190715 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Sat, 30 Dec 2023 17:57:44 +0100 Subject: [PATCH] Add missing view functions --- chain-extensions/pallet-uniques/src/lib.rs | 45 ++++++++++++++++--- .../pallet-uniques/src/weights.rs | 10 +++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/chain-extensions/pallet-uniques/src/lib.rs b/chain-extensions/pallet-uniques/src/lib.rs index ebf285853b..745998df88 100644 --- a/chain-extensions/pallet-uniques/src/lib.rs +++ b/chain-extensions/pallet-uniques/src/lib.rs @@ -20,10 +20,14 @@ pub mod weights; -use frame_support::traits::nonfungibles::{Inspect, InspectEnumerable}; +use frame_support::traits::{ + nonfungibles::{Inspect, InspectEnumerable}, + Currency, +}; use pallet_contracts::chain_extension::{ ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, }; +use pallet_uniques::{CollectionDetails, ItemDetails}; use parity_scale_codec::Encode; use sp_runtime::traits::StaticLookup; use sp_runtime::BoundedVec; @@ -33,6 +37,8 @@ use sp_std::vec::Vec; use uniques_chain_extension_types::Outcome; type AccountIdLookup = <::Lookup as StaticLookup>::Source; +type DepositBalanceOf = + <::Currency as Currency<::AccountId>>::Balance; enum UniquesFunc { Owner, @@ -40,6 +46,8 @@ enum UniquesFunc { Attribute, CollectionAttribute, CanTransfer, + Collection, + Item, Collections, Items, Owned, @@ -56,10 +64,12 @@ impl TryFrom for UniquesFunc { 3 => Ok(UniquesFunc::Attribute), 4 => Ok(UniquesFunc::CollectionAttribute), 5 => Ok(UniquesFunc::CanTransfer), - 6 => Ok(UniquesFunc::Collections), - 7 => Ok(UniquesFunc::Items), - 8 => Ok(UniquesFunc::Owned), - 9 => Ok(UniquesFunc::OwnedInCollection), + 6 => Ok(UniquesFunc::Collection), + 7 => Ok(UniquesFunc::Item), + 8 => Ok(UniquesFunc::Collections), + 9 => Ok(UniquesFunc::Items), + 10 => Ok(UniquesFunc::Owned), + 11 => Ok(UniquesFunc::OwnedInCollection), _ => Err(DispatchError::Other( "Unimplemented func_id for UniquesFunc", )), @@ -150,6 +160,31 @@ where let can_transfer = pallet_uniques::Pallet::::can_transfer(&collection_id, &item); env.write(&can_transfer.encode(), false, None)?; } + UniquesFunc::Collection => { + let collection_id: ::CollectionId = env.read_as()?; + + let collection: Option>> = + pallet_uniques::Collection::::get(&collection_id); + + let base_weight = ::collection(); + env.charge_weight(base_weight)?; + + env.write(&collection.encode(), false, None)?; + } + UniquesFunc::Item => { + let (collection_id, item_id): ( + ::CollectionId, + ::ItemId, + ) = env.read_as()?; + + let item: Option>> = + pallet_uniques::Item::::get(&collection_id, &item_id); + + let base_weight = ::item(); + env.charge_weight(base_weight)?; + + env.write(&item.encode(), false, None)?; + } UniquesFunc::Collections => { let read_bound: u32 = env.read_as()?; diff --git a/chain-extensions/pallet-uniques/src/weights.rs b/chain-extensions/pallet-uniques/src/weights.rs index fb847b3abb..69d1328e63 100644 --- a/chain-extensions/pallet-uniques/src/weights.rs +++ b/chain-extensions/pallet-uniques/src/weights.rs @@ -30,6 +30,8 @@ pub trait WeightInfo { fn attribute() -> Weight; fn collection_attribute() -> Weight; fn can_transfer() -> Weight; + fn collection() -> Weight; + fn item() -> Weight; fn collections(n: u32) -> Weight; fn items(n: u32) -> Weight; fn owned(n: u32) -> Weight; @@ -59,6 +61,14 @@ impl WeightInfo for SubstrateWeight { T::DbWeight::get().reads(2 as u64) } + fn collection() -> Weight { + T::DbWeight::get().reads(1 as u64) + } + + fn item() -> Weight { + T::DbWeight::get().reads(1 as u64) + } + fn collections(n: u32) -> Weight { T::DbWeight::get().reads(1 as u64).saturating_mul(n.into()) }