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

difftreelog

source

client/rpc/src/lib.rs14.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use std::sync::Arc;1819use codec::{Decode, Encode};20use jsonrpsee::{21	core::{RpcResult as Result},22	proc_macros::rpc,23};24use anyhow::anyhow;25use up_data_structs::{26	RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property,27	PropertyKeyPermission, TokenData,28};29use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};30use sp_blockchain::HeaderBackend;31use up_rpc::UniqueApi as UniqueRuntimeApi;3233// RMRK34use rmrk_rpc::RmrkApi as RmrkRuntimeApi;35use up_data_structs::{36	RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName, RmrkResourceId,37};3839pub use rmrk_unique_rpc::RmrkApiServer;4041#[rpc(server)]42#[async_trait]43pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {44	#[method(name = "unique_accountTokens")]45	fn account_tokens(46		&self,47		collection: CollectionId,48		account: CrossAccountId,49		at: Option<BlockHash>,50	) -> Result<Vec<TokenId>>;51	#[method(name = "unique_collectionTokens")]52	fn collection_tokens(53		&self,54		collection: CollectionId,55		at: Option<BlockHash>,56	) -> Result<Vec<TokenId>>;57	#[method(name = "unique_tokenExists")]58	fn token_exists(59		&self,60		collection: CollectionId,61		token: TokenId,62		at: Option<BlockHash>,63	) -> Result<bool>;6465	#[method(name = "unique_tokenOwner")]66	fn token_owner(67		&self,68		collection: CollectionId,69		token: TokenId,70		at: Option<BlockHash>,71	) -> Result<Option<CrossAccountId>>;72	#[method(name = "unique_topmostTokenOwner")]73	fn topmost_token_owner(74		&self,75		collection: CollectionId,76		token: TokenId,77		at: Option<BlockHash>,78	) -> Result<Option<CrossAccountId>>;7980	#[method(name = "unique_collectionProperties")]81	fn collection_properties(82		&self,83		collection: CollectionId,84		keys: Option<Vec<String>>,85		at: Option<BlockHash>,86	) -> Result<Vec<Property>>;8788	#[method(name = "unique_tokenProperties")]89	fn token_properties(90		&self,91		collection: CollectionId,92		token_id: TokenId,93		keys: Option<Vec<String>>,94		at: Option<BlockHash>,95	) -> Result<Vec<Property>>;9697	#[method(name = "unique_propertyPermissions")]98	fn property_permissions(99		&self,100		collection: CollectionId,101		keys: Option<Vec<String>>,102		at: Option<BlockHash>,103	) -> Result<Vec<PropertyKeyPermission>>;104105	#[method(name = "unique_tokenData")]106	fn token_data(107		&self,108		collection: CollectionId,109		token_id: TokenId,110		keys: Option<Vec<String>>,111		at: Option<BlockHash>,112	) -> Result<TokenData<CrossAccountId>>;113114	#[method(name = "unique_totalSupply")]115	fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;116	#[method(name = "unique_accountBalance")]117	fn account_balance(118		&self,119		collection: CollectionId,120		account: CrossAccountId,121		at: Option<BlockHash>,122	) -> Result<u32>;123	#[method(name = "unique_balance")]124	fn balance(125		&self,126		collection: CollectionId,127		account: CrossAccountId,128		token: TokenId,129		at: Option<BlockHash>,130	) -> Result<String>;131	#[method(name = "unique_allowance")]132	fn allowance(133		&self,134		collection: CollectionId,135		sender: CrossAccountId,136		spender: CrossAccountId,137		token: TokenId,138		at: Option<BlockHash>,139	) -> Result<String>;140141	#[method(name = "unique_adminlist")]142	fn adminlist(143		&self,144		collection: CollectionId,145		at: Option<BlockHash>,146	) -> Result<Vec<CrossAccountId>>;147	#[method(name = "unique_allowlist")]148	fn allowlist(149		&self,150		collection: CollectionId,151		at: Option<BlockHash>,152	) -> Result<Vec<CrossAccountId>>;153	#[method(name = "unique_allowed")]154	fn allowed(155		&self,156		collection: CollectionId,157		user: CrossAccountId,158		at: Option<BlockHash>,159	) -> Result<bool>;160	#[method(name = "unique_lastTokenId")]161	fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;162	#[method(name = "unique_collectionById")]163	fn collection_by_id(164		&self,165		collection: CollectionId,166		at: Option<BlockHash>,167	) -> Result<Option<RpcCollection<AccountId>>>;168	#[method(name = "unique_collectionStats")]169	fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;170171	#[method(name = "unique_nextSponsored")]172	fn next_sponsored(173		&self,174		collection: CollectionId,175		account: CrossAccountId,176		token: TokenId,177		at: Option<BlockHash>,178	) -> Result<Option<u64>>;179	#[method(name = "unique_effectiveCollectionLimits")]180	fn effective_collection_limits(181		&self,182		collection_id: CollectionId,183		at: Option<BlockHash>,184	) -> Result<Option<CollectionLimits>>;185}186187mod rmrk_unique_rpc {188	use super::*;189190	#[rpc(server)]191	#[async_trait]192	pub trait RmrkApi<193		BlockHash,194		AccountId,195		CollectionInfo,196		NftInfo,197		ResourceInfo,198		PropertyInfo,199		BaseInfo,200		PartType,201		Theme,202	>203	{204		#[method(name = "rmrk_lastCollectionIdx")]205		/// Get the latest created collection id206		fn last_collection_idx(&self, at: Option<BlockHash>) -> Result<RmrkCollectionId>;207208		#[method(name = "rmrk_collectionById")]209		/// Get collection by id210		fn collection_by_id(211			&self,212			id: RmrkCollectionId,213			at: Option<BlockHash>,214		) -> Result<Option<CollectionInfo>>;215216		#[method(name = "rmrk_nftById")]217		/// Get NFT by collection id and NFT id218		fn nft_by_id(219			&self,220			collection_id: RmrkCollectionId,221			nft_id: RmrkNftId,222			at: Option<BlockHash>,223		) -> Result<Option<NftInfo>>;224225		#[method(name = "rmrk_accountTokens")]226		/// Get tokens owned by an account in a collection227		fn account_tokens(228			&self,229			account_id: AccountId,230			collection_id: RmrkCollectionId,231			at: Option<BlockHash>,232		) -> Result<Vec<RmrkNftId>>;233234		#[method(name = "rmrk_nftChildren")]235		/// Get NFT children236		fn nft_children(237			&self,238			collection_id: RmrkCollectionId,239			nft_id: RmrkNftId,240			at: Option<BlockHash>,241		) -> Result<Vec<RmrkNftChild>>;242243		#[method(name = "rmrk_collectionProperties")]244		/// Get collection properties245		fn collection_properties(246			&self,247			collection_id: RmrkCollectionId,248			filter_keys: Option<Vec<String>>,249			at: Option<BlockHash>,250		) -> Result<Vec<PropertyInfo>>;251252		#[method(name = "rmrk_nftProperties")]253		/// Get NFT properties254		fn nft_properties(255			&self,256			collection_id: RmrkCollectionId,257			nft_id: RmrkNftId,258			filter_keys: Option<Vec<String>>,259			at: Option<BlockHash>,260		) -> Result<Vec<PropertyInfo>>;261262		#[method(name = "rmrk_nftResources")]263		/// Get NFT resources264		fn nft_resources(265			&self,266			collection_id: RmrkCollectionId,267			nft_id: RmrkNftId,268			at: Option<BlockHash>,269		) -> Result<Vec<ResourceInfo>>;270271		#[method(name = "rmrk_nftResourcePriorities")]272		/// Get NFT resource priorities273		fn nft_resource_priorities(274			&self,275			collection_id: RmrkCollectionId,276			nft_id: RmrkNftId,277			at: Option<BlockHash>,278		) -> Result<Vec<RmrkResourceId>>;279280		#[method(name = "rmrk_base")]281		/// Get base info282		fn base(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Option<BaseInfo>>;283284		#[method(name = "rmrk_baseParts")]285		/// Get all Base's parts286		fn base_parts(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Vec<PartType>>;287288		#[method(name = "rmrk_themeNames")]289		fn theme_names(290			&self,291			base_id: RmrkBaseId,292			at: Option<BlockHash>,293		) -> Result<Vec<RmrkThemeName>>;294295		#[method(name = "rmrk_themes")]296		fn theme(297			&self,298			base_id: RmrkBaseId,299			theme_name: String,300			filter_keys: Option<Vec<String>>,301			at: Option<BlockHash>,302		) -> Result<Option<Theme>>;303	}304}305306pub struct Unique<C, P> {307	client: Arc<C>,308	_marker: std::marker::PhantomData<P>,309}310311impl<C, P> Unique<C, P> {312	pub fn new(client: Arc<C>) -> Self {313		Self {314			client,315			_marker: Default::default(),316		}317	}318}319320macro_rules! pass_method {321	(322		$method_name:ident(323			$($(#[map(|$map_arg:ident| $map:expr)])? $name:ident: $ty:ty),* $(,)?324		) -> $result:ty $(=> $mapper:expr)?,325		//$runtime_name:ident $(<$($lt: tt),+>)*326		$runtime_api_macro:ident327		$(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*328	) => {329		fn $method_name(330			&self,331			$(332				$name: $ty,333			)*334			at: Option<<Block as BlockT>::Hash>,335		) -> Result<$result> {336			let api = self.client.runtime_api();337			let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));338			let _api_version = if let Ok(Some(api_version)) =339				api.api_version::<$runtime_api_macro!()>(&at)340			{341				api_version342			} else {343				// unreachable for our runtime344				return Err(anyhow!("api is not available").into())345			};346347			let result = $(if _api_version < $ver {348				api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))349			} else)*350			{ api.$method_name(&at, $($((|$map_arg: $ty| $map))? ($name)),*) };351352			Ok(result353				.map_err(|e| anyhow!("unable to query: {e}"))?354				.map_err(|e| anyhow!("runtime error: {e:?}"))$(.map($mapper))??)355		}356	};357}358359macro_rules! unique_api {360	() => {361		dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>362	};363}364365macro_rules! rmrk_api {366	() => {367		dyn RmrkRuntimeApi<Block, AccountId, CollectionInfo, NftInfo, ResourceInfo, PropertyInfo, BaseInfo, PartType, Theme>368	};369}370371#[allow(deprecated)]372impl<C, Block, CrossAccountId, AccountId>373	UniqueApiServer<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>374where375	Block: BlockT,376	AccountId: Decode,377	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,378	C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,379	CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,380{381	pass_method!(382		account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>, unique_api383	);384	pass_method!(385		collection_tokens(collection: CollectionId) -> Vec<TokenId>, unique_api386	);387	pass_method!(388		token_exists(collection: CollectionId, token: TokenId) -> bool, unique_api389	);390	pass_method!(391		token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api392	);393	pass_method!(394		topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api395	);396	pass_method!(total_supply(collection: CollectionId) -> u32, unique_api);397	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32, unique_api);398	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string(), unique_api);399	pass_method!(400		allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string(),401		unique_api402	);403404	pass_method!(collection_properties(405		collection: CollectionId,406407		#[map(|keys| string_keys_to_bytes_keys(keys))]408		keys: Option<Vec<String>>409	) -> Vec<Property>, unique_api);410411	pass_method!(token_properties(412		collection: CollectionId,413		token_id: TokenId,414415		#[map(|keys| string_keys_to_bytes_keys(keys))]416		keys: Option<Vec<String>>417	) -> Vec<Property>, unique_api);418419	pass_method!(property_permissions(420		collection: CollectionId,421422		#[map(|keys| string_keys_to_bytes_keys(keys))]423		keys: Option<Vec<String>>424	) -> Vec<PropertyKeyPermission>, unique_api);425426	pass_method!(token_data(427		collection: CollectionId,428		token_id: TokenId,429430		#[map(|keys| string_keys_to_bytes_keys(keys))]431		keys: Option<Vec<String>>,432	) -> TokenData<CrossAccountId>, unique_api);433434	pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);435	pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);436	pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool, unique_api);437	pass_method!(last_token_id(collection: CollectionId) -> TokenId, unique_api);438	pass_method!(collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>, unique_api);439	pass_method!(collection_stats() -> CollectionStats, unique_api);440	pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>, unique_api);441	pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>, unique_api);442}443444#[allow(deprecated)]445impl<446		C,447		Block,448		AccountId,449		CollectionInfo,450		NftInfo,451		ResourceInfo,452		PropertyInfo,453		BaseInfo,454		PartType,455		Theme,456	>457	rmrk_unique_rpc::RmrkApiServer<458		<Block as BlockT>::Hash,459		AccountId,460		CollectionInfo,461		NftInfo,462		ResourceInfo,463		PropertyInfo,464		BaseInfo,465		PartType,466		Theme,467	> for Unique<C, Block>468where469	C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,470	C::Api: RmrkRuntimeApi<471		Block,472		AccountId,473		CollectionInfo,474		NftInfo,475		ResourceInfo,476		PropertyInfo,477		BaseInfo,478		PartType,479		Theme,480	>,481	AccountId: Decode + Encode,482	CollectionInfo: Decode,483	NftInfo: Decode,484	ResourceInfo: Decode,485	PropertyInfo: Decode,486	BaseInfo: Decode,487	PartType: Decode,488	Theme: Decode,489	Block: BlockT,490{491	pass_method!(last_collection_idx() -> RmrkCollectionId, rmrk_api);492	pass_method!(collection_by_id(id: RmrkCollectionId) -> Option<CollectionInfo>, rmrk_api);493	pass_method!(nft_by_id(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Option<NftInfo>, rmrk_api);494	pass_method!(account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Vec<RmrkNftId>, rmrk_api);495	pass_method!(nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkNftChild>, rmrk_api);496	pass_method!(497		collection_properties(498			collection_id: RmrkCollectionId,499500			#[map(|keys| string_keys_to_bytes_keys(keys))]501			filter_keys: Option<Vec<String>>502		) -> Vec<PropertyInfo>,503		rmrk_api504	);505	pass_method!(506		nft_properties(507			collection_id: RmrkCollectionId,508			nft_id: RmrkNftId,509510			#[map(|keys| string_keys_to_bytes_keys(keys))]511			filter_keys: Option<Vec<String>>512		) -> Vec<PropertyInfo>,513		rmrk_api514	);515	pass_method!(nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<ResourceInfo>, rmrk_api);516	pass_method!(nft_resource_priorities(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkResourceId>, rmrk_api);517	pass_method!(base(base_id: RmrkBaseId) -> Option<BaseInfo>, rmrk_api);518	pass_method!(base_parts(base_id: RmrkBaseId) -> Vec<PartType>, rmrk_api);519	pass_method!(theme_names(base_id: RmrkBaseId) -> Vec<RmrkThemeName>, rmrk_api);520	pass_method!(521		theme(522			base_id: RmrkBaseId,523524			#[map(|n| n.into_bytes())]525			theme_name: String,526527			#[map(|keys| string_keys_to_bytes_keys(keys))]528			filter_keys: Option<Vec<String>>529		) -> Option<Theme>, rmrk_api);530}531532fn string_keys_to_bytes_keys(keys: Option<Vec<String>>) -> Option<Vec<Vec<u8>>> {533	keys.map(|keys| keys.into_iter().map(|key| key.into_bytes()).collect())534}