git.delta.rocks / unique-network / refs/commits / 1d12e73b766b

difftreelog

source

client/rpc/src/lib.rs4.4 KiBsourcehistory
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<u128>;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<u128>;7677	#[rpc(name = "nft_adminlist")]78	fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;79	#[rpc(name = "nft_allowlist")]80	fn allowlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;81	#[rpc(name = "nft_lastTokenId")]82	fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;83}8485pub struct Nft<C, P> {86	client: Arc<C>,87	_marker: std::marker::PhantomData<P>,88}8990impl<C, P> Nft<C, P> {91	pub fn new(client: Arc<C>) -> Self {92		Self {93			client,94			_marker: Default::default(),95		}96	}97}9899pub enum Error {100	RuntimeError,101}102103impl From<Error> for i64 {104	fn from(e: Error) -> i64 {105		match e {106			Error::RuntimeError => 1,107		}108	}109}110111macro_rules! pass_method {112	($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty) => {113		fn $method_name(114			&self,115			$(116				$name: $ty,117			)*118			at: Option<<Block as BlockT>::Hash>,119		) -> Result<$result> {120			let api = self.client.runtime_api();121			let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));122123			api.$method_name(&at, $($name),*).map_err(|e| RpcError {124				code: ErrorCode::ServerError(Error::RuntimeError.into()),125				message: "Unable to query".into(),126				data: Some(format!("{:?}", e).into()),127			})128		}129	};130}131132impl<C, Block, CrossAccountId, AccountId> NftApi<<Block as BlockT>::Hash, CrossAccountId, AccountId>133	for Nft<C, Block>134where135	Block: BlockT,136	AccountId: Decode,137	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,138	C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,139	CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,140{141	pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);142	pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);143	pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);144	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);145	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);146	pass_method!(collection_tokens(collection: CollectionId) -> u32);147	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);148	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);149	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);150151	pass_method!(adminlist(collection: CollectionId) -> Vec<AccountId>);152	pass_method!(allowlist(collection: CollectionId) -> Vec<AccountId>);153	pass_method!(last_token_id(collection: CollectionId) -> TokenId);154}