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
before · client/rpc/src/lib.rs
1use std::sync::Arc;23use jsonrpc_core::{Error as RpcError, ErrorCode, Result};4use jsonrpc_derive::rpc;5use nft_data_structs::{CollectionId, TokenId};6use sp_api::{BlockId, BlockT, ProvideRuntimeApi};7use sp_blockchain::HeaderBackend;8use up_rpc::NftApi as NftRuntimeApi;910#[rpc]11pub trait NftApi<BlockHash, CrossAccountId, AccountId> {12	#[rpc(name = "nft_accountTokens")]13	fn account_tokens(14		&self,15		collection: CollectionId,16		account: CrossAccountId,17		at: Option<BlockHash>,18	) -> Result<Vec<TokenId>>;19	#[rpc(name = "nft_tokenExists")]20	fn token_exists(21		&self,22		collection: CollectionId,23		token: TokenId,24		at: Option<BlockHash>,25	) -> Result<bool>;2627	#[rpc(name = "nft_tokenOwner")]28	fn token_owner(29		&self,30		collection: CollectionId,31		token: TokenId,32		at: Option<BlockHash>,33	) -> Result<CrossAccountId>;34	#[rpc(name = "nft_constMetadata")]35	fn const_metadata(36		&self,37		collection: CollectionId,38		token: TokenId,39		at: Option<BlockHash>,40	) -> Result<Vec<u8>>;41	#[rpc(name = "nft_variableMetadata")]42	fn variable_metadata(43		&self,44		collection: CollectionId,45		token: TokenId,46		at: Option<BlockHash>,47	) -> Result<Vec<u8>>;4849	#[rpc(name = "nft_collectionTokens")]50	fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;51	#[rpc(name = "nft_accountBalance")]52	fn account_balance(53		&self,54		collection: CollectionId,55		account: CrossAccountId,56		at: Option<BlockHash>,57	) -> Result<u32>;58	#[rpc(name = "nft_balance")]59	fn balance(60		&self,61		collection: CollectionId,62		account: CrossAccountId,63		token: TokenId,64		at: Option<BlockHash>,65	) -> Result<u128>;66	#[rpc(name = "nft_allowance")]67	fn allowance(68		&self,69		collection: CollectionId,70		sender: CrossAccountId,71		spender: CrossAccountId,72		token: TokenId,73		at: Option<BlockHash>,74	) -> Result<u128>;75}7677pub struct Nft<C, P> {78	client: Arc<C>,79	_marker: std::marker::PhantomData<P>,80}8182impl<C, P> Nft<C, P> {83	pub fn new(client: Arc<C>) -> Self {84		Self {85			client,86			_marker: Default::default(),87		}88	}89}9091pub enum Error {92	RuntimeError,93}9495impl From<Error> for i64 {96	fn from(e: Error) -> i64 {97		match e {98			Error::RuntimeError => 1,99		}100	}101}102103macro_rules! pass_method {104	($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty) => {105		fn $method_name(106			&self,107			$(108				$name: $ty,109			)*110			at: Option<<Block as BlockT>::Hash>,111		) -> Result<$result> {112			let api = self.client.runtime_api();113			let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));114115			api.$method_name(&at, $($name),*).map_err(|e| RpcError {116				code: ErrorCode::ServerError(Error::RuntimeError.into()),117				message: "Unable to query".into(),118				data: Some(format!("{:?}", e).into()),119			})120		}121	};122}123124impl<C, Block, CrossAccountId, AccountId> NftApi<<Block as BlockT>::Hash, CrossAccountId, AccountId>125	for Nft<C, Block>126where127	Block: BlockT,128	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,129	C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,130	CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,131{132	pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);133	pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);134	pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);135	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);136	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);137	pass_method!(collection_tokens(collection: CollectionId) -> u32);138	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);139	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);140	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);141}
after · client/rpc/src/lib.rs
1use std::sync::Arc;23use codec::Decode;4use jsonrpc_core::{Error as RpcError, ErrorCode, Result};5use jsonrpc_derive::rpc;6use nft_data_structs::{CollectionId, TokenId};7use sp_api::{BlockId, BlockT, ProvideRuntimeApi};8use sp_blockchain::HeaderBackend;9use up_rpc::NftApi as NftRuntimeApi;1011#[rpc]12pub trait NftApi<BlockHash, CrossAccountId, AccountId> {13	#[rpc(name = "nft_accountTokens")]14	fn account_tokens(15		&self,16		collection: CollectionId,17		account: CrossAccountId,18		at: Option<BlockHash>,19	) -> Result<Vec<TokenId>>;20	#[rpc(name = "nft_tokenExists")]21	fn token_exists(22		&self,23		collection: CollectionId,24		token: TokenId,25		at: Option<BlockHash>,26	) -> Result<bool>;2728	#[rpc(name = "nft_tokenOwner")]29	fn token_owner(30		&self,31		collection: CollectionId,32		token: TokenId,33		at: Option<BlockHash>,34	) -> Result<CrossAccountId>;35	#[rpc(name = "nft_constMetadata")]36	fn const_metadata(37		&self,38		collection: CollectionId,39		token: TokenId,40		at: Option<BlockHash>,41	) -> Result<Vec<u8>>;42	#[rpc(name = "nft_variableMetadata")]43	fn variable_metadata(44		&self,45		collection: CollectionId,46		token: TokenId,47		at: Option<BlockHash>,48	) -> Result<Vec<u8>>;4950	#[rpc(name = "nft_collectionTokens")]51	fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;52	#[rpc(name = "nft_accountBalance")]53	fn account_balance(54		&self,55		collection: CollectionId,56		account: CrossAccountId,57		at: Option<BlockHash>,58	) -> Result<u32>;59	#[rpc(name = "nft_balance")]60	fn balance(61		&self,62		collection: CollectionId,63		account: CrossAccountId,64		token: TokenId,65		at: Option<BlockHash>,66	) -> Result<u128>;67	#[rpc(name = "nft_allowance")]68	fn allowance(69		&self,70		collection: CollectionId,71		sender: CrossAccountId,72		spender: CrossAccountId,73		token: TokenId,74		at: Option<BlockHash>,75	) -> Result<u128>;7677	#[rpc(name = "nft_adminlist")]78	fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;79	#[rpc(name = "nft_allowlist")]80	fn allowlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;81	#[rpc(name = "nft_lastTokenId")]82	fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;83}8485pub struct Nft<C, P> {86	client: Arc<C>,87	_marker: std::marker::PhantomData<P>,88}8990impl<C, P> Nft<C, P> {91	pub fn new(client: Arc<C>) -> Self {92		Self {93			client,94			_marker: Default::default(),95		}96	}97}9899pub enum Error {100	RuntimeError,101}102103impl From<Error> for i64 {104	fn from(e: Error) -> i64 {105		match e {106			Error::RuntimeError => 1,107		}108	}109}110111macro_rules! pass_method {112	($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty) => {113		fn $method_name(114			&self,115			$(116				$name: $ty,117			)*118			at: Option<<Block as BlockT>::Hash>,119		) -> Result<$result> {120			let api = self.client.runtime_api();121			let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));122123			api.$method_name(&at, $($name),*).map_err(|e| RpcError {124				code: ErrorCode::ServerError(Error::RuntimeError.into()),125				message: "Unable to query".into(),126				data: Some(format!("{:?}", e).into()),127			})128		}129	};130}131132impl<C, Block, CrossAccountId, AccountId> NftApi<<Block as BlockT>::Hash, CrossAccountId, AccountId>133	for Nft<C, Block>134where135	Block: BlockT,136	AccountId: Decode,137	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,138	C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,139	CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,140{141	pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);142	pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);143	pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);144	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);145	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);146	pass_method!(collection_tokens(collection: CollectionId) -> u32);147	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);148	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);149	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);150151	pass_method!(adminlist(collection: CollectionId) -> Vec<AccountId>);152	pass_method!(allowlist(collection: CollectionId) -> Vec<AccountId>);153	pass_method!(last_token_id(collection: CollectionId) -> TokenId);154}
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
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -268,8 +268,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)?;
 
@@ -361,11 +361,11 @@
 				collection.mint_mode,
 				<CommonError<T>>::PublicMintingNotAllowed
 			);
-			collection.check_whitelist(sender)?;
+			collection.check_allowlist(sender)?;
 
 			for item in data.iter() {
 				for (user, _) in &item.users {
-					collection.check_whitelist(&user)?;
+					collection.check_allowlist(&user)?;
 				}
 			}
 		}
@@ -485,8 +485,8 @@
 		amount: u128,
 	) -> DispatchResult {
 		if collection.access == AccessMode::WhiteList {
-			collection.check_whitelist(&sender)?;
-			collection.check_whitelist(&spender)?;
+			collection.check_allowlist(&sender)?;
+			collection.check_allowlist(&spender)?;
 		}
 
 		<PalletCommon<T>>::ensure_correct_receiver(spender)?;
@@ -517,7 +517,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, token, from.as_sub(), &spender))
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 {