git.delta.rocks / unique-network / refs/commits / 352886154808

difftreelog

feat(rpc) adminlist/allowlist/last_token_id calls

Yaroslav Bolyukin2021-10-22parent: #9c7fc18.patch.diff
in: master

10 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -1,5 +1,6 @@
 use std::sync::Arc;
 
+use codec::Decode;
 use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
 use jsonrpc_derive::rpc;
 use nft_data_structs::{CollectionId, TokenId};
@@ -72,6 +73,13 @@
 		token: TokenId,
 		at: Option<BlockHash>,
 	) -> Result<u128>;
+
+	#[rpc(name = "nft_adminlist")]
+	fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;
+	#[rpc(name = "nft_allowlist")]
+	fn allowlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;
+	#[rpc(name = "nft_lastTokenId")]
+	fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;
 }
 
 pub struct Nft<C, P> {
@@ -125,6 +133,7 @@
 	for Nft<C, Block>
 where
 	Block: BlockT,
+	AccountId: Decode,
 	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
 	C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,
 	CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
@@ -138,4 +147,8 @@
 	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);
 	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);
 	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);
+
+	pass_method!(adminlist(collection: CollectionId) -> Vec<AccountId>);
+	pass_method!(allowlist(collection: CollectionId) -> Vec<AccountId>);
+	pass_method!(last_token_id(collection: CollectionId) -> TokenId);
 }
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -109,12 +109,12 @@
 	pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> Result<bool, DispatchError> {
 		Ok(self.limits.owner_can_transfer && self.is_owner_or_admin(user)?)
 	}
-	pub fn check_whitelist(&self, user: &T::CrossAccountId) -> DispatchResult {
+	pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {
 		self.consume_sload()?;
 
 		ensure!(
-			<WhiteList<T>>::get((self.id, user.as_sub())),
-			<Error<T>>::AddressNotInWhiteList
+			<Allowlist<T>>::get((self.id, user.as_sub())),
+			<Error<T>>::AddressNotInAllowlist
 		);
 		Ok(())
 	}
@@ -252,7 +252,7 @@
 		/// Collection is not in mint mode.
 		PublicMintingNotAllowed,
 		/// Address is not in white list.
-		AddressNotInWhiteList,
+		AddressNotInAllowlist,
 
 		/// Collection name can not be longer than 63 char.
 		CollectionNameLimitExceeded,
@@ -315,9 +315,9 @@
 		QueryKind = ValueQuery,
 	>;
 
-	/// Whitelisted collection users
+	/// Allowlisted collection users
 	#[pallet::storage]
-	pub type WhiteList<T: Config> = StorageNMap<
+	pub type Allowlist<T: Config> = StorageNMap<
 		Key = (
 			Key<Blake2_128Concat, CollectionId>,
 			Key<Blake2_128Concat, T::AccountId>,
@@ -398,6 +398,7 @@
 		<CollectionById<T>>::insert(id, data);
 		Ok(id)
 	}
+
 	pub fn destroy_collection(
 		collection: CollectionHandle<T>,
 		sender: &T::CrossAccountId,
@@ -417,11 +418,11 @@
 		<DestroyedCollectionCount<T>>::put(destroyed_collections);
 		<CollectionById<T>>::remove(collection.id);
 		<IsAdmin<T>>::remove_prefix((collection.id,), None);
-		<WhiteList<T>>::remove_prefix((collection.id,), None);
+		<Allowlist<T>>::remove_prefix((collection.id,), None);
 		Ok(())
 	}
 
-	pub fn toggle_whitelist(
+	pub fn toggle_allowlist(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
 		user: &T::CrossAccountId,
@@ -432,9 +433,9 @@
 		// =========
 
 		if allowed {
-			<WhiteList<T>>::insert((collection.id, user.as_sub()), true);
+			<Allowlist<T>>::insert((collection.id, user.as_sub()), true);
 		} else {
-			<WhiteList<T>>::remove((collection.id, user.as_sub()));
+			<Allowlist<T>>::remove((collection.id, user.as_sub()));
 		}
 
 		Ok(())
@@ -511,6 +512,7 @@
 
 	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;
 	fn token_exists(&self, token: TokenId) -> bool;
+	fn last_token_id(&self) -> TokenId;
 
 	fn token_owner(&self, token: TokenId) -> T::CrossAccountId;
 	fn const_metadata(&self, token: TokenId) -> Vec<u8>;
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -177,6 +177,10 @@
 		token == TokenId::default()
 	}
 
+	fn last_token_id(&self) -> TokenId {
+		TokenId::default()
+	}
+
 	fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {
 		T::CrossAccountId::default()
 	}
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -123,7 +123,7 @@
 			.ok_or(<CommonError<T>>::TokenValueTooLow)?;
 
 		if collection.access == AccessMode::WhiteList {
-			collection.check_whitelist(owner)?;
+			collection.check_allowlist(owner)?;
 		}
 
 		// =========
@@ -161,8 +161,8 @@
 		);
 
 		if collection.access == AccessMode::WhiteList {
-			collection.check_whitelist(from)?;
-			collection.check_whitelist(to)?;
+			collection.check_allowlist(from)?;
+			collection.check_allowlist(to)?;
 		}
 		<PalletCommon<T>>::ensure_correct_receiver(to)?;
 
@@ -222,10 +222,10 @@
 				collection.mint_mode,
 				<CommonError<T>>::PublicMintingNotAllowed
 			);
-			collection.check_whitelist(sender)?;
+			collection.check_allowlist(sender)?;
 
 			for (owner, _) in data.iter() {
-				collection.check_whitelist(owner)?;
+				collection.check_allowlist(owner)?;
 			}
 		}
 
@@ -305,8 +305,8 @@
 		amount: u128,
 	) -> DispatchResult {
 		if collection.access == AccessMode::WhiteList {
-			collection.check_whitelist(&owner)?;
-			collection.check_whitelist(&spender)?;
+			collection.check_allowlist(&owner)?;
+			collection.check_allowlist(&spender)?;
 		}
 
 		if <Balance<T>>::get((collection.id, owner.as_sub())) < amount {
@@ -334,7 +334,7 @@
 		}
 		if collection.access == AccessMode::WhiteList {
 			// `from`, `to` checked in [`transfer`]
-			collection.check_whitelist(spender)?;
+			collection.check_allowlist(spender)?;
 		}
 
 		let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -41,7 +41,7 @@
 };
 use pallet_common::{
 	account::CrossAccountId, CollectionHandle, IsAdmin, Pallet as PalletCommon,
-	Error as CommonError, CommonWeightInfo,
+	Error as CommonError, CommonWeightInfo, Allowlist,
 };
 use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle};
 use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};
@@ -311,7 +311,7 @@
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let collection = <CollectionHandle<T>>::try_get(collection_id)?;
 
-			<PalletCommon<T>>::toggle_whitelist(
+			<PalletCommon<T>>::toggle_allowlist(
 				&collection,
 				&sender,
 				&address,
@@ -340,7 +340,7 @@
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let collection = <CollectionHandle<T>>::try_get(collection_id)?;
 
-			<PalletCommon<T>>::toggle_whitelist(
+			<PalletCommon<T>>::toggle_allowlist(
 				&collection,
 				&sender,
 				&address,
@@ -923,3 +923,17 @@
 		}
 	}
 }
+
+// TODO: limit returned entries?
+impl<T: Config> Pallet<T> {
+	pub fn adminlist(collection: CollectionId) -> Vec<T::AccountId> {
+		<IsAdmin<T>>::iter_prefix((collection,))
+			.map(|(a, _)| a)
+			.collect()
+	}
+	pub fn allowlist(collection: CollectionId) -> Vec<T::AccountId> {
+		<Allowlist<T>>::iter_prefix((collection,))
+			.map(|(a, _)| a)
+			.collect()
+	}
+}
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -185,6 +185,10 @@
 		<Pallet<T>>::token_exists(self, token)
 	}
 
+	fn last_token_id(&self) -> TokenId {
+		TokenId(<TokensMinted<T>>::get(self.id))
+	}
+
 	fn token_owner(&self, token: TokenId) -> T::CrossAccountId {
 		<Owner<T>>::get((self.id, token))
 	}
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -198,7 +198,7 @@
 		);
 
 		if collection.access == AccessMode::WhiteList {
-			collection.check_whitelist(sender)?;
+			collection.check_allowlist(sender)?;
 		}
 
 		let burnt = <TokensBurnt<T>>::get(collection.id)
@@ -246,8 +246,8 @@
 		);
 
 		if collection.access == AccessMode::WhiteList {
-			collection.check_whitelist(from)?;
-			collection.check_whitelist(to)?;
+			collection.check_allowlist(from)?;
+			collection.check_allowlist(to)?;
 		}
 		<PalletCommon<T>>::ensure_correct_receiver(to)?;
 
@@ -314,10 +314,10 @@
 				collection.mint_mode,
 				<CommonError<T>>::PublicMintingNotAllowed
 			);
-			collection.check_whitelist(sender)?;
+			collection.check_allowlist(sender)?;
 
 			for item in data.iter() {
-				collection.check_whitelist(&item.owner)?;
+				collection.check_allowlist(&item.owner)?;
 			}
 		}
 
@@ -453,9 +453,9 @@
 		spender: Option<&T::CrossAccountId>,
 	) -> DispatchResult {
 		if collection.access == AccessMode::WhiteList {
-			collection.check_whitelist(&sender)?;
+			collection.check_allowlist(&sender)?;
 			if let Some(spender) = spender {
-				collection.check_whitelist(&spender)?;
+				collection.check_allowlist(&spender)?;
 			}
 		}
 
@@ -488,7 +488,7 @@
 		}
 		if collection.access == AccessMode::WhiteList {
 			// `from`, `to` checked in [`transfer`]
-			collection.check_whitelist(spender)?;
+			collection.check_allowlist(spender)?;
 		}
 
 		if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
before · pallets/refungible/src/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]23use frame_support::{ensure, BoundedVec};4use nft_data_structs::{5	AccessMode, CUSTOM_DATA_LIMIT, Collection, CollectionId, CustomDataLimit,6	MAX_REFUNGIBLE_PIECES, TokenId,7};8use pallet_common::{9	Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,10};11use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};12use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};13use core::ops::Deref;1415pub use pallet::*;16pub mod benchmarking;17pub mod common;18pub mod erc;19pub mod weights;20pub struct CreateItemData<T: Config> {21	pub const_data: BoundedVec<u8, CustomDataLimit>,22	pub variable_data: BoundedVec<u8, CustomDataLimit>,23	pub users: BTreeMap<T::CrossAccountId, u128>,24}25pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;2627#[frame_support::pallet]28pub mod pallet {29	use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};30	use sp_std::vec::Vec;31	use nft_data_structs::{CollectionId, TokenId};32	use super::weights::WeightInfo;3334	#[pallet::error]35	pub enum Error<T> {36		/// Not Refungible item data used to mint in Refungible collection.37		NotRefungibleDataUsedToMintFungibleCollectionToken,38		/// Maximum refungibility exceeded39		WrongRefungiblePieces,40	}4142	#[pallet::config]43	pub trait Config: frame_system::Config + pallet_common::Config {44		type WeightInfo: WeightInfo;45	}4647	#[pallet::pallet]48	#[pallet::generate_store(pub(super) trait Store)]49	pub struct Pallet<T>(_);5051	#[pallet::storage]52	pub(super) type TokensMinted<T: Config> =53		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;54	#[pallet::storage]55	pub(super) type TokensBurnt<T: Config> =56		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;5758	#[derive(Encode, Decode)]59	pub enum DataKind {60		Constant,61		Variable,62	}6364	#[pallet::storage]65	pub(super) type TokenData<T: Config> = StorageNMap<66		Key = (67			Key<Twox64Concat, CollectionId>,68			Key<Twox64Concat, TokenId>,69			Key<Identity, DataKind>,70		),71		Value = Vec<u8>,72		QueryKind = ValueQuery,73	>;7475	#[pallet::storage]76	pub(super) type TotalSupply<T: Config> = StorageNMap<77		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),78		Value = u128,79		QueryKind = ValueQuery,80	>;8182	/// Used to enumerate tokens owned by account83	#[pallet::storage]84	pub(super) type Owned<T: Config> = StorageNMap<85		Key = (86			Key<Twox64Concat, CollectionId>,87			Key<Blake2_128Concat, T::AccountId>,88			Key<Twox64Concat, TokenId>,89		),90		Value = bool,91		QueryKind = ValueQuery,92	>;9394	#[pallet::storage]95	pub(super) type AccountBalance<T: Config> = StorageNMap<96		Key = (97			Key<Twox64Concat, CollectionId>,98			// Owner99			Key<Blake2_128Concat, T::AccountId>,100		),101		Value = u32,102		QueryKind = ValueQuery,103	>;104105	#[pallet::storage]106	pub(super) type Balance<T: Config> = StorageNMap<107		Key = (108			Key<Twox64Concat, CollectionId>,109			Key<Twox64Concat, TokenId>,110			// Owner111			Key<Blake2_128Concat, T::AccountId>,112		),113		Value = u128,114		QueryKind = ValueQuery,115	>;116117	#[pallet::storage]118	pub(super) type Allowance<T: Config> = StorageNMap<119		Key = (120			Key<Twox64Concat, CollectionId>,121			Key<Twox64Concat, TokenId>,122			// Owner123			Key<Blake2_128, T::AccountId>,124			// Spender125			Key<Blake2_128Concat, T::CrossAccountId>,126		),127		Value = u128,128		QueryKind = ValueQuery,129	>;130}131132pub struct RefungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);133impl<T: Config> RefungibleHandle<T> {134	pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {135		Self(inner)136	}137	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {138		self.0139	}140}141impl<T: Config> Deref for RefungibleHandle<T> {142	type Target = pallet_common::CollectionHandle<T>;143144	fn deref(&self) -> &Self::Target {145		&self.0146	}147}148149impl<T: Config> Pallet<T> {150	pub fn total_supply(collection: &RefungibleHandle<T>) -> u32 {151		<TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)152	}153	pub fn token_exists(collection: &RefungibleHandle<T>, token: TokenId) -> bool {154		<TotalSupply<T>>::contains_key((collection.id, token))155	}156}157158// unchecked calls skips any permission checks159impl<T: Config> Pallet<T> {160	pub fn init_collection(data: Collection<T>) -> Result<CollectionId, DispatchError> {161		PalletCommon::init_collection(data)162	}163	pub fn destroy_collection(164		collection: RefungibleHandle<T>,165		sender: &T::CrossAccountId,166	) -> DispatchResult {167		let id = collection.id;168169		// =========170171		PalletCommon::destroy_collection(collection.0, sender)?;172173		<TokensMinted<T>>::remove(id);174		<TokensBurnt<T>>::remove(id);175		<TokenData<T>>::remove_prefix((id,), None);176		<TotalSupply<T>>::remove_prefix((id,), None);177		<Balance<T>>::remove_prefix((id,), None);178		<Allowance<T>>::remove_prefix((id,), None);179		Ok(())180	}181182	pub fn burn_token(collection: &RefungibleHandle<T>, token_id: TokenId) -> DispatchResult {183		let burnt = <TokensBurnt<T>>::get(collection.id)184			.checked_add(1)185			.ok_or(ArithmeticError::Overflow)?;186187		<TokensBurnt<T>>::insert(collection.id, burnt);188		<TokenData<T>>::remove_prefix((collection.id, token_id), None);189		<TotalSupply<T>>::remove((collection.id, token_id));190		<Balance<T>>::remove_prefix((collection.id, token_id), None);191		<Allowance<T>>::remove_prefix((collection.id, token_id), None);192		// TODO: ERC721 transfer event193		return Ok(());194	}195196	pub fn burn(197		collection: &RefungibleHandle<T>,198		owner: &T::CrossAccountId,199		token: TokenId,200		amount: u128,201	) -> DispatchResult {202		let total_supply = <TotalSupply<T>>::get((collection.id, token))203			.checked_sub(amount)204			.ok_or(<CommonError<T>>::TokenValueTooLow)?;205206		// This was probally last owner of this token?207		if total_supply == 0 {208			// Ensure user actually owns this amount209			ensure!(210				<Balance<T>>::get((collection.id, token, owner.as_sub())) == amount,211				<CommonError<T>>::TokenValueTooLow212			);213214			// =========215216			<Owned<T>>::remove((collection.id, owner.as_sub(), token));217			Self::burn_token(collection, token)?;218			<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(219				collection.id,220				token,221				owner.clone(),222				amount,223			));224			return Ok(());225		}226227		let balance = <Balance<T>>::get((collection.id, token, owner.as_sub()))228			.checked_sub(amount)229			.ok_or(<CommonError<T>>::TokenValueTooLow)?;230		let account_balance = if balance == 0 {231			<AccountBalance<T>>::get((collection.id, owner.as_sub()))232				.checked_sub(1)233				// Should not occur234				.ok_or(ArithmeticError::Underflow)?235		} else {236			0237		};238239		// =========240241		if balance == 0 {242			<Balance<T>>::remove((collection.id, token, owner.as_sub()));243			<AccountBalance<T>>::insert((collection.id, owner.as_sub()), account_balance);244		} else {245			<Balance<T>>::insert((collection.id, token, owner.as_sub()), balance);246		}247		<TotalSupply<T>>::insert((collection.id, token), total_supply);248		// TODO: ERC20 transfer event249		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(250			collection.id,251			token,252			owner.clone(),253			amount,254		));255		Ok(())256	}257258	pub fn transfer(259		collection: &RefungibleHandle<T>,260		from: &T::CrossAccountId,261		to: &T::CrossAccountId,262		token: TokenId,263		amount: u128,264	) -> DispatchResult {265		ensure!(266			collection.transfers_enabled,267			<CommonError<T>>::TransferNotAllowed268		);269270		if collection.access == AccessMode::WhiteList {271			collection.check_whitelist(from)?;272			collection.check_whitelist(to)?;273		}274		<PalletCommon<T>>::ensure_correct_receiver(to)?;275276		let balance_from = <Balance<T>>::get((collection.id, token, from.as_sub()))277			.checked_sub(amount)278			.ok_or(<CommonError<T>>::TokenValueTooLow)?;279		let mut create_target = false;280		let from_to_differ = from != to;281		let balance_to = if from != to {282			let old_balance = <Balance<T>>::get((collection.id, token, to.as_sub()));283			if old_balance == 0 {284				create_target = true;285			}286			Some(287				old_balance288					.checked_add(amount)289					.ok_or(ArithmeticError::Overflow)?,290			)291		} else {292			None293		};294295		let account_balance_from = if balance_from == 0 {296			Some(297				<AccountBalance<T>>::get((collection.id, from.as_sub()))298					.checked_sub(1)299					// Should not occur300					.ok_or(ArithmeticError::Underflow)?,301			)302		} else {303			None304		};305		// Account data is created in token, AccountBalance should be increased306		// But only if from != to as we shouldn't check overflow in this case307		let account_balance_to = if create_target && from_to_differ {308			let account_balance_to = <AccountBalance<T>>::get((collection.id, to.as_sub()))309				.checked_add(1)310				.ok_or(ArithmeticError::Overflow)?;311			ensure!(312				account_balance_to < collection.limits.account_token_ownership_limit(),313				<CommonError<T>>::AccountTokenLimitExceeded,314			);315316			Some(account_balance_to)317		} else {318			None319		};320321		// =========322323		if let Some(balance_to) = balance_to {324			// from != to325			if balance_from == 0 {326				<Balance<T>>::remove((collection.id, token, from.as_sub()));327			} else {328				<Balance<T>>::insert((collection.id, token, from.as_sub()), balance_from);329			}330			<Balance<T>>::insert((collection.id, token, to.as_sub()), balance_to);331			if let Some(account_balance_from) = account_balance_from {332				<AccountBalance<T>>::insert((collection.id, from.as_sub()), account_balance_from);333			}334			if let Some(account_balance_to) = account_balance_to {335				<AccountBalance<T>>::insert((collection.id, to.as_sub()), account_balance_to);336			}337338			<Owned<T>>::remove((collection.id, from.as_sub(), token));339			<Owned<T>>::insert((collection.id, to.as_sub(), token), true);340		}341342		// TODO: ERC20 transfer event343		<PalletCommon<T>>::deposit_event(CommonEvent::Transfer(344			collection.id,345			token,346			from.clone(),347			to.clone(),348			amount,349		));350		Ok(())351	}352353	pub fn create_multiple_items(354		collection: &RefungibleHandle<T>,355		sender: &T::CrossAccountId,356		data: Vec<CreateItemData<T>>,357	) -> DispatchResult {358		let unrestricted_minting = collection.is_owner_or_admin(sender)?;359		if !unrestricted_minting {360			ensure!(361				collection.mint_mode,362				<CommonError<T>>::PublicMintingNotAllowed363			);364			collection.check_whitelist(sender)?;365366			for item in data.iter() {367				for (user, _) in &item.users {368					collection.check_whitelist(&user)?;369				}370			}371		}372373		for item in data.iter() {374			for (owner, _) in item.users.iter() {375				<PalletCommon<T>>::ensure_correct_receiver(owner)?;376			}377		}378379		// Total pieces per tokens380		let totals = data381			.iter()382			.map(|data| {383				Ok(data384					.users385					.iter()386					.map(|u| u.1)387					.try_fold(0u128, |acc, v| acc.checked_add(*v))388					.ok_or(ArithmeticError::Overflow)?)389			})390			.collect::<Result<Vec<_>, DispatchError>>()?;391		for total in &totals {392			ensure!(393				*total <= MAX_REFUNGIBLE_PIECES,394				<Error<T>>::WrongRefungiblePieces395			);396		}397398		let first_token_id = <TokensMinted<T>>::get(collection.id);399		let tokens_minted = first_token_id400			.checked_add(data.len() as u32)401			.ok_or(ArithmeticError::Overflow)?;402		ensure!(403			tokens_minted < collection.limits.token_limit,404			<CommonError<T>>::CollectionTokenLimitExceeded405		);406407		let mut balances = BTreeMap::new();408		for data in &data {409			for (owner, _) in &data.users {410				let balance = balances411					.entry(owner.as_sub())412					.or_insert_with(|| <AccountBalance<T>>::get((collection.id, owner.as_sub())));413				*balance = balance.checked_add(1).ok_or(ArithmeticError::Overflow)?;414415				ensure!(416					*balance <= collection.limits.account_token_ownership_limit(),417					<CommonError<T>>::AccountTokenLimitExceeded,418				);419			}420		}421422		// =========423424		<TokensMinted<T>>::insert(collection.id, tokens_minted);425		for (account, balance) in balances {426			<AccountBalance<T>>::insert((collection.id, account), balance);427		}428		for (i, token) in data.into_iter().enumerate() {429			let token_id = first_token_id + i as u32;430			<TotalSupply<T>>::insert((collection.id, token_id), totals[i]);431432			if !token.const_data.is_empty() {433				<TokenData<T>>::insert(434					(collection.id, token_id, DataKind::Constant),435					token.const_data,436				);437			}438			if !token.variable_data.is_empty() {439				<TokenData<T>>::insert(440					(collection.id, token_id, DataKind::Variable),441					token.variable_data,442				);443			}444			for (user, amount) in token.users.into_iter() {445				if amount == 0 {446					continue;447				}448				<Balance<T>>::insert((collection.id, token_id, user.as_sub()), amount);449				<Owned<T>>::insert((collection.id, user.as_sub(), TokenId(token_id)), true);450				// TODO: ERC20 transfer event451				<PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(452					collection.id,453					TokenId(token_id),454					user,455					amount,456				));457			}458		}459		Ok(())460	}461462	pub fn set_allowance_unchecked(463		collection: &RefungibleHandle<T>,464		sender: &T::CrossAccountId,465		spender: &T::CrossAccountId,466		token: TokenId,467		amount: u128,468	) {469		<Allowance<T>>::insert((collection.id, token, sender.as_sub(), spender), amount);470		// TODO: ERC20 approval event471		<PalletCommon<T>>::deposit_event(CommonEvent::Approved(472			collection.id,473			token,474			sender.clone(),475			spender.clone(),476			amount,477		))478	}479480	pub fn set_allowance(481		collection: &RefungibleHandle<T>,482		sender: &T::CrossAccountId,483		spender: &T::CrossAccountId,484		token: TokenId,485		amount: u128,486	) -> DispatchResult {487		if collection.access == AccessMode::WhiteList {488			collection.check_whitelist(&sender)?;489			collection.check_whitelist(&spender)?;490		}491492		<PalletCommon<T>>::ensure_correct_receiver(spender)?;493494		if <Balance<T>>::get((collection.id, token, sender.as_sub())) < amount {495			ensure!(496				collection.ignores_owned_amount(sender)?,497				<CommonError<T>>::CantApproveMoreThanOwned498			);499		}500501		// =========502503		Self::set_allowance_unchecked(collection, sender, spender, token, amount);504		Ok(())505	}506507	pub fn transfer_from(508		collection: &RefungibleHandle<T>,509		spender: &T::CrossAccountId,510		from: &T::CrossAccountId,511		to: &T::CrossAccountId,512		token: TokenId,513		amount: u128,514	) -> DispatchResult {515		if spender == from {516			return Self::transfer(collection, from, to, token, amount);517		}518		if collection.access == AccessMode::WhiteList {519			// `from`, `to` checked in [`transfer`]520			collection.check_whitelist(spender)?;521		}522523		let allowance = <Allowance<T>>::get((collection.id, token, from.as_sub(), &spender))524			.checked_sub(amount);525		if allowance.is_none() {526			ensure!(527				collection.ignores_allowance(spender)?,528				<CommonError<T>>::TokenValueNotEnough529			);530		}531532		// =========533534		Self::transfer(collection, from, to, token, amount)?;535		if let Some(allowance) = allowance {536			Self::set_allowance_unchecked(collection, from, spender, token, allowance);537		}538		Ok(())539	}540541	pub fn set_variable_metadata(542		collection: &RefungibleHandle<T>,543		sender: &T::CrossAccountId,544		token: TokenId,545		data: Vec<u8>,546	) -> DispatchResult {547		ensure!(548			data.len() as u32 <= CUSTOM_DATA_LIMIT,549			<CommonError<T>>::TokenVariableDataLimitExceeded550		);551		collection.check_can_update_meta(552			sender,553			&T::CrossAccountId::from_sub(collection.owner.clone()),554		)?;555556		collection.consume_sstore()?;557558		// =========559560		<TokenData<T>>::insert((collection.id, token, DataKind::Variable), data);561		Ok(())562	}563564	/// Delegated to `create_multiple_items`565	pub fn create_item(566		collection: &RefungibleHandle<T>,567		sender: &T::CrossAccountId,568		data: CreateItemData<T>,569	) -> DispatchResult {570		Self::create_multiple_items(collection, sender, vec![data])571	}572}
after · pallets/refungible/src/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]23use frame_support::{ensure, BoundedVec};4use nft_data_structs::{5	AccessMode, CUSTOM_DATA_LIMIT, Collection, CollectionId, CustomDataLimit,6	MAX_REFUNGIBLE_PIECES, TokenId,7};8use pallet_common::{9	Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,10};11use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};12use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};13use core::ops::Deref;1415pub use pallet::*;16pub mod benchmarking;17pub mod common;18pub mod erc;19pub mod weights;20pub struct CreateItemData<T: Config> {21	pub const_data: BoundedVec<u8, CustomDataLimit>,22	pub variable_data: BoundedVec<u8, CustomDataLimit>,23	pub users: BTreeMap<T::CrossAccountId, u128>,24}25pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;2627#[frame_support::pallet]28pub mod pallet {29	use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};30	use sp_std::vec::Vec;31	use nft_data_structs::{CollectionId, TokenId};32	use super::weights::WeightInfo;3334	#[pallet::error]35	pub enum Error<T> {36		/// Not Refungible item data used to mint in Refungible collection.37		NotRefungibleDataUsedToMintFungibleCollectionToken,38		/// Maximum refungibility exceeded39		WrongRefungiblePieces,40	}4142	#[pallet::config]43	pub trait Config: frame_system::Config + pallet_common::Config {44		type WeightInfo: WeightInfo;45	}4647	#[pallet::pallet]48	#[pallet::generate_store(pub(super) trait Store)]49	pub struct Pallet<T>(_);5051	#[pallet::storage]52	pub(super) type TokensMinted<T: Config> =53		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;54	#[pallet::storage]55	pub(super) type TokensBurnt<T: Config> =56		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;5758	#[derive(Encode, Decode)]59	pub enum DataKind {60		Constant,61		Variable,62	}6364	#[pallet::storage]65	pub(super) type TokenData<T: Config> = StorageNMap<66		Key = (67			Key<Twox64Concat, CollectionId>,68			Key<Twox64Concat, TokenId>,69			Key<Identity, DataKind>,70		),71		Value = Vec<u8>,72		QueryKind = ValueQuery,73	>;7475	#[pallet::storage]76	pub(super) type TotalSupply<T: Config> = StorageNMap<77		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),78		Value = u128,79		QueryKind = ValueQuery,80	>;8182	/// Used to enumerate tokens owned by account83	#[pallet::storage]84	pub(super) type Owned<T: Config> = StorageNMap<85		Key = (86			Key<Twox64Concat, CollectionId>,87			Key<Blake2_128Concat, T::AccountId>,88			Key<Twox64Concat, TokenId>,89		),90		Value = bool,91		QueryKind = ValueQuery,92	>;9394	#[pallet::storage]95	pub(super) type AccountBalance<T: Config> = StorageNMap<96		Key = (97			Key<Twox64Concat, CollectionId>,98			// Owner99			Key<Blake2_128Concat, T::AccountId>,100		),101		Value = u32,102		QueryKind = ValueQuery,103	>;104105	#[pallet::storage]106	pub(super) type Balance<T: Config> = StorageNMap<107		Key = (108			Key<Twox64Concat, CollectionId>,109			Key<Twox64Concat, TokenId>,110			// Owner111			Key<Blake2_128Concat, T::AccountId>,112		),113		Value = u128,114		QueryKind = ValueQuery,115	>;116117	#[pallet::storage]118	pub(super) type Allowance<T: Config> = StorageNMap<119		Key = (120			Key<Twox64Concat, CollectionId>,121			Key<Twox64Concat, TokenId>,122			// Owner123			Key<Blake2_128, T::AccountId>,124			// Spender125			Key<Blake2_128Concat, T::CrossAccountId>,126		),127		Value = u128,128		QueryKind = ValueQuery,129	>;130}131132pub struct RefungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);133impl<T: Config> RefungibleHandle<T> {134	pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {135		Self(inner)136	}137	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {138		self.0139	}140}141impl<T: Config> Deref for RefungibleHandle<T> {142	type Target = pallet_common::CollectionHandle<T>;143144	fn deref(&self) -> &Self::Target {145		&self.0146	}147}148149impl<T: Config> Pallet<T> {150	pub fn total_supply(collection: &RefungibleHandle<T>) -> u32 {151		<TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)152	}153	pub fn token_exists(collection: &RefungibleHandle<T>, token: TokenId) -> bool {154		<TotalSupply<T>>::contains_key((collection.id, token))155	}156}157158// unchecked calls skips any permission checks159impl<T: Config> Pallet<T> {160	pub fn init_collection(data: Collection<T>) -> Result<CollectionId, DispatchError> {161		PalletCommon::init_collection(data)162	}163	pub fn destroy_collection(164		collection: RefungibleHandle<T>,165		sender: &T::CrossAccountId,166	) -> DispatchResult {167		let id = collection.id;168169		// =========170171		PalletCommon::destroy_collection(collection.0, sender)?;172173		<TokensMinted<T>>::remove(id);174		<TokensBurnt<T>>::remove(id);175		<TokenData<T>>::remove_prefix((id,), None);176		<TotalSupply<T>>::remove_prefix((id,), None);177		<Balance<T>>::remove_prefix((id,), None);178		<Allowance<T>>::remove_prefix((id,), None);179		Ok(())180	}181182	pub fn burn_token(collection: &RefungibleHandle<T>, token_id: TokenId) -> DispatchResult {183		let burnt = <TokensBurnt<T>>::get(collection.id)184			.checked_add(1)185			.ok_or(ArithmeticError::Overflow)?;186187		<TokensBurnt<T>>::insert(collection.id, burnt);188		<TokenData<T>>::remove_prefix((collection.id, token_id), None);189		<TotalSupply<T>>::remove((collection.id, token_id));190		<Balance<T>>::remove_prefix((collection.id, token_id), None);191		<Allowance<T>>::remove_prefix((collection.id, token_id), None);192		// TODO: ERC721 transfer event193		return Ok(());194	}195196	pub fn burn(197		collection: &RefungibleHandle<T>,198		owner: &T::CrossAccountId,199		token: TokenId,200		amount: u128,201	) -> DispatchResult {202		let total_supply = <TotalSupply<T>>::get((collection.id, token))203			.checked_sub(amount)204			.ok_or(<CommonError<T>>::TokenValueTooLow)?;205206		// This was probally last owner of this token?207		if total_supply == 0 {208			// Ensure user actually owns this amount209			ensure!(210				<Balance<T>>::get((collection.id, token, owner.as_sub())) == amount,211				<CommonError<T>>::TokenValueTooLow212			);213214			// =========215216			<Owned<T>>::remove((collection.id, owner.as_sub(), token));217			Self::burn_token(collection, token)?;218			<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(219				collection.id,220				token,221				owner.clone(),222				amount,223			));224			return Ok(());225		}226227		let balance = <Balance<T>>::get((collection.id, token, owner.as_sub()))228			.checked_sub(amount)229			.ok_or(<CommonError<T>>::TokenValueTooLow)?;230		let account_balance = if balance == 0 {231			<AccountBalance<T>>::get((collection.id, owner.as_sub()))232				.checked_sub(1)233				// Should not occur234				.ok_or(ArithmeticError::Underflow)?235		} else {236			0237		};238239		// =========240241		if balance == 0 {242			<Balance<T>>::remove((collection.id, token, owner.as_sub()));243			<AccountBalance<T>>::insert((collection.id, owner.as_sub()), account_balance);244		} else {245			<Balance<T>>::insert((collection.id, token, owner.as_sub()), balance);246		}247		<TotalSupply<T>>::insert((collection.id, token), total_supply);248		// TODO: ERC20 transfer event249		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(250			collection.id,251			token,252			owner.clone(),253			amount,254		));255		Ok(())256	}257258	pub fn transfer(259		collection: &RefungibleHandle<T>,260		from: &T::CrossAccountId,261		to: &T::CrossAccountId,262		token: TokenId,263		amount: u128,264	) -> DispatchResult {265		ensure!(266			collection.transfers_enabled,267			<CommonError<T>>::TransferNotAllowed268		);269270		if collection.access == AccessMode::WhiteList {271			collection.check_allowlist(from)?;272			collection.check_allowlist(to)?;273		}274		<PalletCommon<T>>::ensure_correct_receiver(to)?;275276		let balance_from = <Balance<T>>::get((collection.id, token, from.as_sub()))277			.checked_sub(amount)278			.ok_or(<CommonError<T>>::TokenValueTooLow)?;279		let mut create_target = false;280		let from_to_differ = from != to;281		let balance_to = if from != to {282			let old_balance = <Balance<T>>::get((collection.id, token, to.as_sub()));283			if old_balance == 0 {284				create_target = true;285			}286			Some(287				old_balance288					.checked_add(amount)289					.ok_or(ArithmeticError::Overflow)?,290			)291		} else {292			None293		};294295		let account_balance_from = if balance_from == 0 {296			Some(297				<AccountBalance<T>>::get((collection.id, from.as_sub()))298					.checked_sub(1)299					// Should not occur300					.ok_or(ArithmeticError::Underflow)?,301			)302		} else {303			None304		};305		// Account data is created in token, AccountBalance should be increased306		// But only if from != to as we shouldn't check overflow in this case307		let account_balance_to = if create_target && from_to_differ {308			let account_balance_to = <AccountBalance<T>>::get((collection.id, to.as_sub()))309				.checked_add(1)310				.ok_or(ArithmeticError::Overflow)?;311			ensure!(312				account_balance_to < collection.limits.account_token_ownership_limit(),313				<CommonError<T>>::AccountTokenLimitExceeded,314			);315316			Some(account_balance_to)317		} else {318			None319		};320321		// =========322323		if let Some(balance_to) = balance_to {324			// from != to325			if balance_from == 0 {326				<Balance<T>>::remove((collection.id, token, from.as_sub()));327			} else {328				<Balance<T>>::insert((collection.id, token, from.as_sub()), balance_from);329			}330			<Balance<T>>::insert((collection.id, token, to.as_sub()), balance_to);331			if let Some(account_balance_from) = account_balance_from {332				<AccountBalance<T>>::insert((collection.id, from.as_sub()), account_balance_from);333			}334			if let Some(account_balance_to) = account_balance_to {335				<AccountBalance<T>>::insert((collection.id, to.as_sub()), account_balance_to);336			}337338			<Owned<T>>::remove((collection.id, from.as_sub(), token));339			<Owned<T>>::insert((collection.id, to.as_sub(), token), true);340		}341342		// TODO: ERC20 transfer event343		<PalletCommon<T>>::deposit_event(CommonEvent::Transfer(344			collection.id,345			token,346			from.clone(),347			to.clone(),348			amount,349		));350		Ok(())351	}352353	pub fn create_multiple_items(354		collection: &RefungibleHandle<T>,355		sender: &T::CrossAccountId,356		data: Vec<CreateItemData<T>>,357	) -> DispatchResult {358		let unrestricted_minting = collection.is_owner_or_admin(sender)?;359		if !unrestricted_minting {360			ensure!(361				collection.mint_mode,362				<CommonError<T>>::PublicMintingNotAllowed363			);364			collection.check_allowlist(sender)?;365366			for item in data.iter() {367				for (user, _) in &item.users {368					collection.check_allowlist(&user)?;369				}370			}371		}372373		for item in data.iter() {374			for (owner, _) in item.users.iter() {375				<PalletCommon<T>>::ensure_correct_receiver(owner)?;376			}377		}378379		// Total pieces per tokens380		let totals = data381			.iter()382			.map(|data| {383				Ok(data384					.users385					.iter()386					.map(|u| u.1)387					.try_fold(0u128, |acc, v| acc.checked_add(*v))388					.ok_or(ArithmeticError::Overflow)?)389			})390			.collect::<Result<Vec<_>, DispatchError>>()?;391		for total in &totals {392			ensure!(393				*total <= MAX_REFUNGIBLE_PIECES,394				<Error<T>>::WrongRefungiblePieces395			);396		}397398		let first_token_id = <TokensMinted<T>>::get(collection.id);399		let tokens_minted = first_token_id400			.checked_add(data.len() as u32)401			.ok_or(ArithmeticError::Overflow)?;402		ensure!(403			tokens_minted < collection.limits.token_limit,404			<CommonError<T>>::CollectionTokenLimitExceeded405		);406407		let mut balances = BTreeMap::new();408		for data in &data {409			for (owner, _) in &data.users {410				let balance = balances411					.entry(owner.as_sub())412					.or_insert_with(|| <AccountBalance<T>>::get((collection.id, owner.as_sub())));413				*balance = balance.checked_add(1).ok_or(ArithmeticError::Overflow)?;414415				ensure!(416					*balance <= collection.limits.account_token_ownership_limit(),417					<CommonError<T>>::AccountTokenLimitExceeded,418				);419			}420		}421422		// =========423424		<TokensMinted<T>>::insert(collection.id, tokens_minted);425		for (account, balance) in balances {426			<AccountBalance<T>>::insert((collection.id, account), balance);427		}428		for (i, token) in data.into_iter().enumerate() {429			let token_id = first_token_id + i as u32;430			<TotalSupply<T>>::insert((collection.id, token_id), totals[i]);431432			if !token.const_data.is_empty() {433				<TokenData<T>>::insert(434					(collection.id, token_id, DataKind::Constant),435					token.const_data,436				);437			}438			if !token.variable_data.is_empty() {439				<TokenData<T>>::insert(440					(collection.id, token_id, DataKind::Variable),441					token.variable_data,442				);443			}444			for (user, amount) in token.users.into_iter() {445				if amount == 0 {446					continue;447				}448				<Balance<T>>::insert((collection.id, token_id, user.as_sub()), amount);449				<Owned<T>>::insert((collection.id, user.as_sub(), TokenId(token_id)), true);450				// TODO: ERC20 transfer event451				<PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(452					collection.id,453					TokenId(token_id),454					user,455					amount,456				));457			}458		}459		Ok(())460	}461462	pub fn set_allowance_unchecked(463		collection: &RefungibleHandle<T>,464		sender: &T::CrossAccountId,465		spender: &T::CrossAccountId,466		token: TokenId,467		amount: u128,468	) {469		<Allowance<T>>::insert((collection.id, token, sender.as_sub(), spender), amount);470		// TODO: ERC20 approval event471		<PalletCommon<T>>::deposit_event(CommonEvent::Approved(472			collection.id,473			token,474			sender.clone(),475			spender.clone(),476			amount,477		))478	}479480	pub fn set_allowance(481		collection: &RefungibleHandle<T>,482		sender: &T::CrossAccountId,483		spender: &T::CrossAccountId,484		token: TokenId,485		amount: u128,486	) -> DispatchResult {487		if collection.access == AccessMode::WhiteList {488			collection.check_allowlist(&sender)?;489			collection.check_allowlist(&spender)?;490		}491492		<PalletCommon<T>>::ensure_correct_receiver(spender)?;493494		if <Balance<T>>::get((collection.id, token, sender.as_sub())) < amount {495			ensure!(496				collection.ignores_owned_amount(sender)?,497				<CommonError<T>>::CantApproveMoreThanOwned498			);499		}500501		// =========502503		Self::set_allowance_unchecked(collection, sender, spender, token, amount);504		Ok(())505	}506507	pub fn transfer_from(508		collection: &RefungibleHandle<T>,509		spender: &T::CrossAccountId,510		from: &T::CrossAccountId,511		to: &T::CrossAccountId,512		token: TokenId,513		amount: u128,514	) -> DispatchResult {515		if spender == from {516			return Self::transfer(collection, from, to, token, amount);517		}518		if collection.access == AccessMode::WhiteList {519			// `from`, `to` checked in [`transfer`]520			collection.check_allowlist(spender)?;521		}522523		let allowance = <Allowance<T>>::get((collection.id, token, from.as_sub(), &spender))524			.checked_sub(amount);525		if allowance.is_none() {526			ensure!(527				collection.ignores_allowance(spender)?,528				<CommonError<T>>::TokenValueNotEnough529			);530		}531532		// =========533534		Self::transfer(collection, from, to, token, amount)?;535		if let Some(allowance) = allowance {536			Self::set_allowance_unchecked(collection, from, spender, token, allowance);537		}538		Ok(())539	}540541	pub fn set_variable_metadata(542		collection: &RefungibleHandle<T>,543		sender: &T::CrossAccountId,544		token: TokenId,545		data: Vec<u8>,546	) -> DispatchResult {547		ensure!(548			data.len() as u32 <= CUSTOM_DATA_LIMIT,549			<CommonError<T>>::TokenVariableDataLimitExceeded550		);551		collection.check_can_update_meta(552			sender,553			&T::CrossAccountId::from_sub(collection.owner.clone()),554		)?;555556		collection.consume_sstore()?;557558		// =========559560		<TokenData<T>>::insert((collection.id, token, DataKind::Variable), data);561		Ok(())562	}563564	/// Delegated to `create_multiple_items`565	pub fn create_item(566		collection: &RefungibleHandle<T>,567		sender: &T::CrossAccountId,568		data: CreateItemData<T>,569	) -> DispatchResult {570		Self::create_multiple_items(collection, sender, vec![data])571	}572}
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -3,9 +3,11 @@
 use nft_data_structs::{CollectionId, TokenId};
 use sp_std::vec::Vec;
 use sp_core::H160;
+use codec::Decode;
 
 sp_api::decl_runtime_apis! {
 	pub trait NftApi<CrossAccountId, AccountId> where
+		AccountId: Decode,
 		CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
 	{
 		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;
@@ -27,5 +29,9 @@
 
 		/// Used for ethereum integration
 		fn eth_contract_code(account: H160) -> Option<Vec<u8>>;
+
+		fn adminlist(collection: CollectionId) -> Vec<AccountId>;
+		fn allowlist(collection: CollectionId) -> Vec<AccountId>;
+		fn last_token_id(collection: CollectionId) -> TokenId;
 	}
 }
modifiedruntime/src/lib.rsdiffbeforeafterboth
--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -972,6 +972,15 @@
 		fn eth_contract_code(account: H160) -> Option<Vec<u8>> {
 			<pallet_nft::NftErcSupport<Runtime>>::get_code(&account)
 		}
+		fn adminlist(collection: CollectionId) -> Vec<AccountId> {
+			<pallet_nft::Pallet<Runtime>>::adminlist(collection)
+		}
+		fn allowlist(collection: CollectionId) -> Vec<AccountId> {
+			<pallet_nft::Pallet<Runtime>>::allowlist(collection)
+		}
+		fn last_token_id(collection: CollectionId) -> TokenId {
+			dispatch_nft_runtime!(collection.last_token_id())
+		}
 	}
 
 	impl sp_api::Core<Block> for Runtime {