123456789101112131415161718use std::sync::Arc;1920use codec::Decode;21use jsonrpc_core::{Error as RpcError, ErrorCode, Result};22use jsonrpc_derive::rpc;23use up_data_structs::{Collection, CollectionId, CollectionStats, TokenId};24use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};25use sp_blockchain::HeaderBackend;26use up_rpc::UniqueApi as UniqueRuntimeApi;2728#[rpc]29pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {30 #[rpc(name = "unique_accountTokens")]31 fn account_tokens(32 &self,33 collection: CollectionId,34 account: CrossAccountId,35 at: Option<BlockHash>,36 ) -> Result<Vec<TokenId>>;37 #[rpc(name = "unique_tokenExists")]38 fn token_exists(39 &self,40 collection: CollectionId,41 token: TokenId,42 at: Option<BlockHash>,43 ) -> Result<bool>;4445 #[rpc(name = "unique_tokenOwner")]46 fn token_owner(47 &self,48 collection: CollectionId,49 token: TokenId,50 at: Option<BlockHash>,51 ) -> Result<Option<CrossAccountId>>;52 #[rpc(name = "unique_constMetadata")]53 fn const_metadata(54 &self,55 collection: CollectionId,56 token: TokenId,57 at: Option<BlockHash>,58 ) -> Result<Vec<u8>>;59 #[rpc(name = "unique_variableMetadata")]60 fn variable_metadata(61 &self,62 collection: CollectionId,63 token: TokenId,64 at: Option<BlockHash>,65 ) -> Result<Vec<u8>>;6667 #[rpc(name = "unique_collectionTokens")]68 fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;69 #[rpc(name = "unique_accountBalance")]70 fn account_balance(71 &self,72 collection: CollectionId,73 account: CrossAccountId,74 at: Option<BlockHash>,75 ) -> Result<u32>;76 #[rpc(name = "unique_balance")]77 fn balance(78 &self,79 collection: CollectionId,80 account: CrossAccountId,81 token: TokenId,82 at: Option<BlockHash>,83 ) -> Result<String>;84 #[rpc(name = "unique_allowance")]85 fn allowance(86 &self,87 collection: CollectionId,88 sender: CrossAccountId,89 spender: CrossAccountId,90 token: TokenId,91 at: Option<BlockHash>,92 ) -> Result<String>;9394 #[rpc(name = "unique_adminlist")]95 fn adminlist(96 &self,97 collection: CollectionId,98 at: Option<BlockHash>,99 ) -> Result<Vec<CrossAccountId>>;100 #[rpc(name = "unique_allowlist")]101 fn allowlist(102 &self,103 collection: CollectionId,104 at: Option<BlockHash>,105 ) -> Result<Vec<CrossAccountId>>;106 #[rpc(name = "unique_allowed")]107 fn allowed(108 &self,109 collection: CollectionId,110 user: CrossAccountId,111 at: Option<BlockHash>,112 ) -> Result<bool>;113 #[rpc(name = "unique_lastTokenId")]114 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;115 #[rpc(name = "unique_collectionById")]116 fn collection_by_id(117 &self,118 collection: CollectionId,119 at: Option<BlockHash>,120 ) -> Result<Option<Collection<AccountId>>>;121 #[rpc(name = "unique_collectionStats")]122 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;123}124125pub struct Unique<C, P> {126 client: Arc<C>,127 _marker: std::marker::PhantomData<P>,128}129130impl<C, P> Unique<C, P> {131 pub fn new(client: Arc<C>) -> Self {132 Self {133 client,134 _marker: Default::default(),135 }136 }137}138139pub enum Error {140 RuntimeError,141}142143impl From<Error> for i64 {144 fn from(e: Error) -> i64 {145 match e {146 Error::RuntimeError => 1,147 }148 }149}150151macro_rules! pass_method {152 (153 $method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?154 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*155 ) => {156 fn $method_name(157 &self,158 $(159 $name: $ty,160 )*161 at: Option<<Block as BlockT>::Hash>,162 ) -> Result<$result> {163 let api = self.client.runtime_api();164 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));165 let _api_version = if let Ok(Some(api_version)) =166 api.api_version::<dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>>(&at)167 {168 api_version169 } else {170 171 return Err(RpcError {172 code: ErrorCode::InvalidParams,173 message: "Api is not available".into(),174 data: None,175 })176 };177178 let result = $(if _api_version < $ver {179 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))180 } else)*181 { api.$method_name(&at, $($name),*) };182183 let result = result.map_err(|e| RpcError {184 code: ErrorCode::ServerError(Error::RuntimeError.into()),185 message: "Unable to query".into(),186 data: Some(format!("{:?}", e).into()),187 })?;188 result.map_err(|e| RpcError {189 code: ErrorCode::InvalidParams,190 message: "Runtime returned error".into(),191 data: Some(format!("{:?}", e).into()),192 })$(.map($mapper))?193 }194 };195}196197#[allow(deprecated)]198impl<C, Block, CrossAccountId, AccountId>199 UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>200where201 Block: BlockT,202 AccountId: Decode,203 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,204 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,205 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,206{207 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);208 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);209 pass_method!(210 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>;211 changed_in 2, token_owner_before_version_2(collection, token) => |u| Some(u)212 );213 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);214 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);215 pass_method!(collection_tokens(collection: CollectionId) -> u32);216 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);217 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());218 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());219220 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);221 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);222 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);223 pass_method!(last_token_id(collection: CollectionId) -> TokenId);224 pass_method!(collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>);225 pass_method!(collection_stats() -> CollectionStats);226}