1234567891011121314151617use std::sync::Arc;1819use codec::Decode;20use jsonrpc_core::{Error as RpcError, ErrorCode, Result};21use jsonrpc_derive::rpc;22use up_data_structs::{RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId};23use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};24use sp_blockchain::HeaderBackend;25use up_rpc::UniqueApi as UniqueRuntimeApi;2627#[rpc]28pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {29 #[rpc(name = "unique_accountTokens")]30 fn account_tokens(31 &self,32 collection: CollectionId,33 account: CrossAccountId,34 at: Option<BlockHash>,35 ) -> Result<Vec<TokenId>>;36 #[rpc(name = "unique_collectionTokens")]37 fn collection_tokens(38 &self,39 collection: CollectionId,40 at: Option<BlockHash>,41 ) -> Result<Vec<TokenId>>;42 #[rpc(name = "unique_tokenExists")]43 fn token_exists(44 &self,45 collection: CollectionId,46 token: TokenId,47 at: Option<BlockHash>,48 ) -> Result<bool>;4950 #[rpc(name = "unique_tokenOwner")]51 fn token_owner(52 &self,53 collection: CollectionId,54 token: TokenId,55 at: Option<BlockHash>,56 ) -> Result<Option<CrossAccountId>>;57 #[rpc(name = "unique_topmostTokenOwner")]58 fn topmost_token_owner(59 &self,60 collection: CollectionId,61 token: TokenId,62 at: Option<BlockHash>,63 ) -> Result<Option<CrossAccountId>>;64 #[rpc(name = "unique_constMetadata")]65 fn const_metadata(66 &self,67 collection: CollectionId,68 token: TokenId,69 at: Option<BlockHash>,70 ) -> Result<Vec<u8>>;71 #[rpc(name = "unique_variableMetadata")]72 fn variable_metadata(73 &self,74 collection: CollectionId,75 token: TokenId,76 at: Option<BlockHash>,77 ) -> Result<Vec<u8>>;7879 #[rpc(name = "unique_totalSupply")]80 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;81 #[rpc(name = "unique_accountBalance")]82 fn account_balance(83 &self,84 collection: CollectionId,85 account: CrossAccountId,86 at: Option<BlockHash>,87 ) -> Result<u32>;88 #[rpc(name = "unique_balance")]89 fn balance(90 &self,91 collection: CollectionId,92 account: CrossAccountId,93 token: TokenId,94 at: Option<BlockHash>,95 ) -> Result<String>;96 #[rpc(name = "unique_allowance")]97 fn allowance(98 &self,99 collection: CollectionId,100 sender: CrossAccountId,101 spender: CrossAccountId,102 token: TokenId,103 at: Option<BlockHash>,104 ) -> Result<String>;105106 #[rpc(name = "unique_adminlist")]107 fn adminlist(108 &self,109 collection: CollectionId,110 at: Option<BlockHash>,111 ) -> Result<Vec<CrossAccountId>>;112 #[rpc(name = "unique_allowlist")]113 fn allowlist(114 &self,115 collection: CollectionId,116 at: Option<BlockHash>,117 ) -> Result<Vec<CrossAccountId>>;118 #[rpc(name = "unique_allowed")]119 fn allowed(120 &self,121 collection: CollectionId,122 user: CrossAccountId,123 at: Option<BlockHash>,124 ) -> Result<bool>;125 #[rpc(name = "unique_lastTokenId")]126 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;127 #[rpc(name = "unique_collectionById")]128 fn collection_by_id(129 &self,130 collection: CollectionId,131 at: Option<BlockHash>,132 ) -> Result<Option<RpcCollection<AccountId>>>;133 #[rpc(name = "unique_collectionStats")]134 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;135136 #[rpc(name = "unique_nextSponsored")]137 fn next_sponsored(138 &self,139 collection: CollectionId,140 account: CrossAccountId,141 token: TokenId,142 at: Option<BlockHash>,143 ) -> Result<Option<u64>>;144 #[rpc(name = "unique_effectiveCollectionLimits")]145 fn effective_collection_limits(146 &self,147 collection_id: CollectionId,148 at: Option<BlockHash>,149 ) -> Result<Option<CollectionLimits>>;150}151152pub struct Unique<C, P> {153 client: Arc<C>,154 _marker: std::marker::PhantomData<P>,155}156157impl<C, P> Unique<C, P> {158 pub fn new(client: Arc<C>) -> Self {159 Self {160 client,161 _marker: Default::default(),162 }163 }164}165166pub enum Error {167 RuntimeError,168}169170impl From<Error> for i64 {171 fn from(e: Error) -> i64 {172 match e {173 Error::RuntimeError => 1,174 }175 }176}177178macro_rules! pass_method {179 (180 $method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?181 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*182 ) => {183 fn $method_name(184 &self,185 $(186 $name: $ty,187 )*188 at: Option<<Block as BlockT>::Hash>,189 ) -> Result<$result> {190 let api = self.client.runtime_api();191 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));192 let _api_version = if let Ok(Some(api_version)) =193 api.api_version::<dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>>(&at)194 {195 api_version196 } else {197 198 return Err(RpcError {199 code: ErrorCode::InvalidParams,200 message: "Api is not available".into(),201 data: None,202 })203 };204205 let result = $(if _api_version < $ver {206 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))207 } else)*208 { api.$method_name(&at, $($name),*) };209210 let result = result.map_err(|e| RpcError {211 code: ErrorCode::ServerError(Error::RuntimeError.into()),212 message: "Unable to query".into(),213 data: Some(format!("{:?}", e).into()),214 })?;215 result.map_err(|e| RpcError {216 code: ErrorCode::InvalidParams,217 message: "Runtime returned error".into(),218 data: Some(format!("{:?}", e).into()),219 })$(.map($mapper))?220 }221 };222}223224#[allow(deprecated)]225impl<C, Block, CrossAccountId, AccountId>226 UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>227where228 Block: BlockT,229 AccountId: Decode,230 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,231 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,232 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,233{234 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);235 pass_method!(collection_tokens(collection: CollectionId) -> Vec<TokenId>);236 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);237 pass_method!(238 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>;239 changed_in 2, token_owner_before_version_2(collection, token) => |u| Some(u)240 );241 pass_method!(topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>);242 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);243 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);244245 pass_method!(total_supply(collection: CollectionId) -> u32);246 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);247 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());248 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());249250 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);251 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);252 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);253 pass_method!(last_token_id(collection: CollectionId) -> TokenId);254 pass_method!(collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>);255 pass_method!(collection_stats() -> CollectionStats);256 pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>);257 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>);258}