Skip to content

Commit

Permalink
Add missing view functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Dec 30, 2023
1 parent 96d191f commit 2ea8e6d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
45 changes: 40 additions & 5 deletions chain-extensions/pallet-uniques/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,13 +37,17 @@ use sp_std::vec::Vec;
use uniques_chain_extension_types::Outcome;

type AccountIdLookup<T> = <<T as SysConfig>::Lookup as StaticLookup>::Source;
type DepositBalanceOf<T> =
<<T as pallet_uniques::Config>::Currency as Currency<<T as SysConfig>::AccountId>>::Balance;

enum UniquesFunc {
Owner,
CollectionOwner,
Attribute,
CollectionAttribute,
CanTransfer,
Collection,
Item,
Collections,
Items,
Owned,
Expand All @@ -56,10 +64,12 @@ impl TryFrom<u16> 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",
)),
Expand Down Expand Up @@ -150,6 +160,31 @@ where
let can_transfer = pallet_uniques::Pallet::<T>::can_transfer(&collection_id, &item);
env.write(&can_transfer.encode(), false, None)?;
}
UniquesFunc::Collection => {
let collection_id: <T as pallet_uniques::Config>::CollectionId = env.read_as()?;

let collection: Option<CollectionDetails<T::AccountId, DepositBalanceOf<T>>> =
pallet_uniques::Collection::<T>::get(&collection_id);

let base_weight = <W as weights::WeightInfo>::collection();
env.charge_weight(base_weight)?;

env.write(&collection.encode(), false, None)?;
}
UniquesFunc::Item => {
let (collection_id, item_id): (
<T as pallet_uniques::Config>::CollectionId,
<T as pallet_uniques::Config>::ItemId,
) = env.read_as()?;

let item: Option<ItemDetails<T::AccountId, DepositBalanceOf<T>>> =
pallet_uniques::Item::<T>::get(&collection_id, &item_id);

let base_weight = <W as weights::WeightInfo>::item();
env.charge_weight(base_weight)?;

env.write(&item.encode(), false, None)?;
}
UniquesFunc::Collections => {
let read_bound: u32 = env.read_as()?;

Expand Down
10 changes: 10 additions & 0 deletions chain-extensions/pallet-uniques/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +61,14 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
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())
}
Expand Down

0 comments on commit 2ea8e6d

Please sign in to comment.