1use std::sync::Arc;23use codec::Decode;4use jsonrpc_core::{Error as RpcError, ErrorCode, Result};5use jsonrpc_derive::rpc;6use up_data_structs::{Collection, CollectionId, CollectionStats, TokenId};7use sp_api::{BlockId, BlockT, ProvideRuntimeApi};8use sp_blockchain::HeaderBackend;9use up_rpc::UniqueApi as UniqueRuntimeApi;1011#[rpc]12pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {13 #[rpc(name = "unique_accountTokens")]14 fn account_tokens(15 &self,16 collection: CollectionId,17 account: CrossAccountId,18 at: Option<BlockHash>,19 ) -> Result<Vec<TokenId>>;20 #[rpc(name = "unique_tokenExists")]21 fn token_exists(22 &self,23 collection: CollectionId,24 token: TokenId,25 at: Option<BlockHash>,26 ) -> Result<bool>;2728 #[rpc(name = "unique_tokenOwner")]29 fn token_owner(30 &self,31 collection: CollectionId,32 token: TokenId,33 at: Option<BlockHash>,34 ) -> Result<CrossAccountId>;35 #[rpc(name = "unique_constMetadata")]36 fn const_metadata(37 &self,38 collection: CollectionId,39 token: TokenId,40 at: Option<BlockHash>,41 ) -> Result<Vec<u8>>;42 #[rpc(name = "unique_variableMetadata")]43 fn variable_metadata(44 &self,45 collection: CollectionId,46 token: TokenId,47 at: Option<BlockHash>,48 ) -> Result<Vec<u8>>;4950 #[rpc(name = "unique_collectionTokens")]51 fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;52 #[rpc(name = "unique_accountBalance")]53 fn account_balance(54 &self,55 collection: CollectionId,56 account: CrossAccountId,57 at: Option<BlockHash>,58 ) -> Result<u32>;59 #[rpc(name = "unique_balance")]60 fn balance(61 &self,62 collection: CollectionId,63 account: CrossAccountId,64 token: TokenId,65 at: Option<BlockHash>,66 ) -> Result<String>;67 #[rpc(name = "unique_allowance")]68 fn allowance(69 &self,70 collection: CollectionId,71 sender: CrossAccountId,72 spender: CrossAccountId,73 token: TokenId,74 at: Option<BlockHash>,75 ) -> Result<String>;7677 #[rpc(name = "unique_adminlist")]78 fn adminlist(79 &self,80 collection: CollectionId,81 at: Option<BlockHash>,82 ) -> Result<Vec<CrossAccountId>>;83 #[rpc(name = "unique_allowlist")]84 fn allowlist(85 &self,86 collection: CollectionId,87 at: Option<BlockHash>,88 ) -> Result<Vec<CrossAccountId>>;89 #[rpc(name = "unique_allowed")]90 fn allowed(91 &self,92 collection: CollectionId,93 user: CrossAccountId,94 at: Option<BlockHash>,95 ) -> Result<bool>;96 #[rpc(name = "unique_lastTokenId")]97 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;98 #[rpc(name = "unique_collectionById")]99 fn collection_by_id(100 &self,101 collection: CollectionId,102 at: Option<BlockHash>,103 ) -> Result<Option<Collection<AccountId>>>;104 #[rpc(name = "unique_collectionStats")]105 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;106}107108pub struct Unique<C, P> {109 client: Arc<C>,110 _marker: std::marker::PhantomData<P>,111}112113impl<C, P> Unique<C, P> {114 pub fn new(client: Arc<C>) -> Self {115 Self {116 client,117 _marker: Default::default(),118 }119 }120}121122pub enum Error {123 RuntimeError,124}125126impl From<Error> for i64 {127 fn from(e: Error) -> i64 {128 match e {129 Error::RuntimeError => 1,130 }131 }132}133134macro_rules! pass_method {135 ($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?) => {136 fn $method_name(137 &self,138 $(139 $name: $ty,140 )*141 at: Option<<Block as BlockT>::Hash>,142 ) -> Result<$result> {143 let api = self.client.runtime_api();144 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));145146 api.$method_name(&at, $($name),*).map_err(|e| RpcError {147 code: ErrorCode::ServerError(Error::RuntimeError.into()),148 message: "Unable to query".into(),149 data: Some(format!("{:?}", e).into()),150 }) $(151 .map($mapper)152 )?153 }154 };155}156157impl<C, Block, CrossAccountId, AccountId>158 UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>159where160 Block: BlockT,161 AccountId: Decode,162 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,163 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,164 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,165{166 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);167 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);168 pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);169 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);170 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);171 pass_method!(collection_tokens(collection: CollectionId) -> u32);172 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);173 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());174 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());175176 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);177 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);178 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool);179 pass_method!(last_token_id(collection: CollectionId) -> TokenId);180 pass_method!(collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>);181 pass_method!(collection_stats() -> CollectionStats);182}