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<String>;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<String>;7677 #[rpc(name = "nft_adminlist")]78 fn adminlist(79 &self,80 collection: CollectionId,81 at: Option<BlockHash>,82 ) -> Result<Vec<CrossAccountId>>;83 #[rpc(name = "nft_allowlist")]84 fn allowlist(85 &self,86 collection: CollectionId,87 at: Option<BlockHash>,88 ) -> Result<Vec<CrossAccountId>>;89 #[rpc(name = "nft_lastTokenId")]90 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;91}9293pub struct Nft<C, P> {94 client: Arc<C>,95 _marker: std::marker::PhantomData<P>,96}9798impl<C, P> Nft<C, P> {99 pub fn new(client: Arc<C>) -> Self {100 Self {101 client,102 _marker: Default::default(),103 }104 }105}106107pub enum Error {108 RuntimeError,109}110111impl From<Error> for i64 {112 fn from(e: Error) -> i64 {113 match e {114 Error::RuntimeError => 1,115 }116 }117}118119macro_rules! pass_method {120 ($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?) => {121 fn $method_name(122 &self,123 $(124 $name: $ty,125 )*126 at: Option<<Block as BlockT>::Hash>,127 ) -> Result<$result> {128 let api = self.client.runtime_api();129 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));130131 api.$method_name(&at, $($name),*).map_err(|e| RpcError {132 code: ErrorCode::ServerError(Error::RuntimeError.into()),133 message: "Unable to query".into(),134 data: Some(format!("{:?}", e).into()),135 }) $(136 .map($mapper)137 )?138 }139 };140}141142impl<C, Block, CrossAccountId, AccountId> NftApi<<Block as BlockT>::Hash, CrossAccountId, AccountId>143 for Nft<C, Block>144where145 Block: BlockT,146 AccountId: Decode,147 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,148 C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,149 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,150{151 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);152 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);153 pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);154 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);155 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);156 pass_method!(collection_tokens(collection: CollectionId) -> u32);157 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);158 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());159 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());160161 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>);162 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>);163 pass_method!(last_token_id(collection: CollectionId) -> TokenId);164}