1234567891011121314151617use std::sync::Arc;1819use codec::{Decode, Encode};20use jsonrpc_core::{Error as RpcError, ErrorCode, Result};21use jsonrpc_derive::rpc;22use up_data_structs::{23 RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property,24 PropertyKeyPermission, TokenData,25};26use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};27use sp_blockchain::HeaderBackend;28use up_rpc::UniqueApi as UniqueRuntimeApi;293031use rmrk_rpc::RmrkApi as RmrkRuntimeApi;32use up_data_structs::{33 RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName,34 RmrkResourceId,35};3637pub use rmrk_unique_rpc::RmrkApi;3839#[rpc]40pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {41 #[rpc(name = "unique_accountTokens")]42 fn account_tokens(43 &self,44 collection: CollectionId,45 account: CrossAccountId,46 at: Option<BlockHash>,47 ) -> Result<Vec<TokenId>>;48 #[rpc(name = "unique_collectionTokens")]49 fn collection_tokens(50 &self,51 collection: CollectionId,52 at: Option<BlockHash>,53 ) -> Result<Vec<TokenId>>;54 #[rpc(name = "unique_tokenExists")]55 fn token_exists(56 &self,57 collection: CollectionId,58 token: TokenId,59 at: Option<BlockHash>,60 ) -> Result<bool>;6162 #[rpc(name = "unique_tokenOwner")]63 fn token_owner(64 &self,65 collection: CollectionId,66 token: TokenId,67 at: Option<BlockHash>,68 ) -> Result<Option<CrossAccountId>>;69 #[rpc(name = "unique_topmostTokenOwner")]70 fn topmost_token_owner(71 &self,72 collection: CollectionId,73 token: TokenId,74 at: Option<BlockHash>,75 ) -> Result<Option<CrossAccountId>>;76 #[rpc(name = "unique_constMetadata")]77 fn const_metadata(78 &self,79 collection: CollectionId,80 token: TokenId,81 at: Option<BlockHash>,82 ) -> Result<Vec<u8>>;8384 #[rpc(name = "unique_collectionProperties")]85 fn collection_properties(86 &self,87 collection: CollectionId,88 keys: Option<Vec<String>>,89 at: Option<BlockHash>,90 ) -> Result<Vec<Property>>;9192 #[rpc(name = "unique_tokenProperties")]93 fn token_properties(94 &self,95 collection: CollectionId,96 token_id: TokenId,97 keys: Option<Vec<String>>,98 at: Option<BlockHash>,99 ) -> Result<Vec<Property>>;100101 #[rpc(name = "unique_propertyPermissions")]102 fn property_permissions(103 &self,104 collection: CollectionId,105 keys: Option<Vec<String>>,106 at: Option<BlockHash>,107 ) -> Result<Vec<PropertyKeyPermission>>;108109 #[rpc(name = "unique_tokenData")]110 fn token_data(111 &self,112 collection: CollectionId,113 token_id: TokenId,114 keys: Option<Vec<String>>,115 at: Option<BlockHash>,116 ) -> Result<TokenData<CrossAccountId>>;117118 #[rpc(name = "unique_totalSupply")]119 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;120 #[rpc(name = "unique_accountBalance")]121 fn account_balance(122 &self,123 collection: CollectionId,124 account: CrossAccountId,125 at: Option<BlockHash>,126 ) -> Result<u32>;127 #[rpc(name = "unique_balance")]128 fn balance(129 &self,130 collection: CollectionId,131 account: CrossAccountId,132 token: TokenId,133 at: Option<BlockHash>,134 ) -> Result<String>;135 #[rpc(name = "unique_allowance")]136 fn allowance(137 &self,138 collection: CollectionId,139 sender: CrossAccountId,140 spender: CrossAccountId,141 token: TokenId,142 at: Option<BlockHash>,143 ) -> Result<String>;144145 #[rpc(name = "unique_adminlist")]146 fn adminlist(147 &self,148 collection: CollectionId,149 at: Option<BlockHash>,150 ) -> Result<Vec<CrossAccountId>>;151 #[rpc(name = "unique_allowlist")]152 fn allowlist(153 &self,154 collection: CollectionId,155 at: Option<BlockHash>,156 ) -> Result<Vec<CrossAccountId>>;157 #[rpc(name = "unique_allowed")]158 fn allowed(159 &self,160 collection: CollectionId,161 user: CrossAccountId,162 at: Option<BlockHash>,163 ) -> Result<bool>;164 #[rpc(name = "unique_lastTokenId")]165 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;166 #[rpc(name = "unique_collectionById")]167 fn collection_by_id(168 &self,169 collection: CollectionId,170 at: Option<BlockHash>,171 ) -> Result<Option<RpcCollection<AccountId>>>;172 #[rpc(name = "unique_collectionStats")]173 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;174175 #[rpc(name = "unique_nextSponsored")]176 fn next_sponsored(177 &self,178 collection: CollectionId,179 account: CrossAccountId,180 token: TokenId,181 at: Option<BlockHash>,182 ) -> Result<Option<u64>>;183 #[rpc(name = "unique_effectiveCollectionLimits")]184 fn effective_collection_limits(185 &self,186 collection_id: CollectionId,187 at: Option<BlockHash>,188 ) -> Result<Option<CollectionLimits>>;189}190191mod rmrk_unique_rpc {192 use super::*;193194 #[rpc(server)]195 pub trait RmrkApi<196 BlockHash,197 AccountId,198 CollectionInfo,199 NftInfo,200 ResourceInfo,201 PropertyInfo,202 BaseInfo,203 PartType,204 Theme,205 >206 {207 #[rpc(name = "rmrk_lastCollectionIdx")]208 209 fn last_collection_idx(&self, at: Option<BlockHash>) -> Result<RmrkCollectionId>;210211 #[rpc(name = "rmrk_collectionById")]212 213 fn collection_by_id(214 &self,215 id: RmrkCollectionId,216 at: Option<BlockHash>,217 ) -> Result<Option<CollectionInfo>>;218219 #[rpc(name = "rmrk_nftById")]220 221 fn nft_by_id(222 &self,223 collection_id: RmrkCollectionId,224 nft_id: RmrkNftId,225 at: Option<BlockHash>,226 ) -> Result<Option<NftInfo>>;227228 #[rpc(name = "rmrk_accountTokens")]229 230 fn account_tokens(231 &self,232 account_id: AccountId,233 collection_id: RmrkCollectionId,234 at: Option<BlockHash>,235 ) -> Result<Vec<RmrkNftId>>;236237 #[rpc(name = "rmrk_nftChildren")]238 239 fn nft_children(240 &self,241 collection_id: RmrkCollectionId,242 nft_id: RmrkNftId,243 at: Option<BlockHash>,244 ) -> Result<Vec<RmrkNftChild>>;245246 #[rpc(name = "rmrk_collectionProperties")]247 248 fn collection_properties(249 &self,250 collection_id: RmrkCollectionId,251 filter_keys: Option<Vec<String>>,252 at: Option<BlockHash>,253 ) -> Result<Vec<PropertyInfo>>;254255 #[rpc(name = "rmrk_nftProperties")]256 257 fn nft_properties(258 &self,259 collection_id: RmrkCollectionId,260 nft_id: RmrkNftId,261 filter_keys: Option<Vec<String>>,262 at: Option<BlockHash>,263 ) -> Result<Vec<PropertyInfo>>;264265 #[rpc(name = "rmrk_nftResources")]266 267 fn nft_resources(268 &self,269 collection_id: RmrkCollectionId,270 nft_id: RmrkNftId,271 at: Option<BlockHash>,272 ) -> Result<Vec<ResourceInfo>>;273274 #[rpc(name = "rmrk_nftResourcePriorities")]275 276 fn nft_resource_priorities(277 &self,278 collection_id: RmrkCollectionId,279 nft_id: RmrkNftId,280 at: Option<BlockHash>,281 ) -> Result<Vec<RmrkResourceId>>;282283 #[rpc(name = "rmrk_base")]284 285 fn base(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Option<BaseInfo>>;286287 #[rpc(name = "rmrk_baseParts")]288 289 fn base_parts(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Vec<PartType>>;290291 #[rpc(name = "rmrk_themeNames")]292 fn theme_names(293 &self,294 base_id: RmrkBaseId,295 at: Option<BlockHash>,296 ) -> Result<Vec<RmrkThemeName>>;297298 #[rpc(name = "rmrk_themes")]299 fn theme(300 &self,301 base_id: RmrkBaseId,302 theme_name: String,303 filter_keys: Option<Vec<String>>,304 at: Option<BlockHash>,305 ) -> Result<Option<Theme>>;306 }307}308309pub struct Unique<C, P> {310 client: Arc<C>,311 _marker: std::marker::PhantomData<P>,312}313314impl<C, P> Unique<C, P> {315 pub fn new(client: Arc<C>) -> Self {316 Self {317 client,318 _marker: Default::default(),319 }320 }321}322323pub enum Error {324 RuntimeError,325}326327impl From<Error> for i64 {328 fn from(e: Error) -> i64 {329 match e {330 Error::RuntimeError => 1,331 }332 }333}334335macro_rules! pass_method {336 (337 $method_name:ident(338 $($(#[map(|$map_arg:ident| $map:expr)])? $name:ident: $ty:ty),* $(,)?339 ) -> $result:ty $(=> $mapper:expr)?,340 341 $runtime_api_macro:ident342 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*343 ) => {344 fn $method_name(345 &self,346 $(347 $name: $ty,348 )*349 at: Option<<Block as BlockT>::Hash>,350 ) -> Result<$result> {351 let api = self.client.runtime_api();352 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));353 let _api_version = if let Ok(Some(api_version)) =354 api.api_version::<$runtime_api_macro!()>(&at)355 {356 api_version357 } else {358 359 return Err(RpcError {360 code: ErrorCode::InvalidParams,361 message: "Api is not available".into(),362 data: None,363 })364 };365366 let result = $(if _api_version < $ver {367 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))368 } else)*369 { api.$method_name(&at, $($((|$map_arg: $ty| $map))? ($name)),*) };370371 let result = result.map_err(|e| RpcError {372 code: ErrorCode::ServerError(Error::RuntimeError.into()),373 message: "Unable to query".into(),374 data: Some(format!("{:?}", e).into()),375 })?;376 result.map_err(|e| RpcError {377 code: ErrorCode::InvalidParams,378 message: "Runtime returned error".into(),379 data: Some(format!("{:?}", e).into()),380 })$(.map($mapper))?381 }382 };383}384385macro_rules! unique_api {386 () => {387 dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>388 };389}390391macro_rules! rmrk_api {392 () => {393 dyn RmrkRuntimeApi<Block, AccountId, CollectionInfo, NftInfo, ResourceInfo, PropertyInfo, BaseInfo, PartType, Theme>394 };395}396397#[allow(deprecated)]398impl<C, Block, CrossAccountId, AccountId>399 UniqueApi<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>400where401 Block: BlockT,402 AccountId: Decode,403 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,404 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,405 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,406{407 pass_method!(408 account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>, unique_api409 );410 pass_method!(411 collection_tokens(collection: CollectionId) -> Vec<TokenId>, unique_api412 );413 pass_method!(414 token_exists(collection: CollectionId, token: TokenId) -> bool, unique_api415 );416 pass_method!(417 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api418 );419 pass_method!(420 topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api421 );422 pass_method!(423 const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>, unique_api424 );425 pass_method!(total_supply(collection: CollectionId) -> u32, unique_api);426 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32, unique_api);427 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string(), unique_api);428 pass_method!(429 allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string(),430 unique_api431 );432433 pass_method!(collection_properties(434 collection: CollectionId,435436 #[map(|keys| string_keys_to_bytes_keys(keys))]437 keys: Option<Vec<String>>438 ) -> Vec<Property>, unique_api);439440 pass_method!(token_properties(441 collection: CollectionId,442 token_id: TokenId,443444 #[map(|keys| string_keys_to_bytes_keys(keys))]445 keys: Option<Vec<String>>446 ) -> Vec<Property>, unique_api);447448 pass_method!(property_permissions(449 collection: CollectionId,450451 #[map(|keys| string_keys_to_bytes_keys(keys))]452 keys: Option<Vec<String>>453 ) -> Vec<PropertyKeyPermission>, unique_api);454455 pass_method!(token_data(456 collection: CollectionId,457 token_id: TokenId,458459 #[map(|keys| string_keys_to_bytes_keys(keys))]460 keys: Option<Vec<String>>,461 ) -> TokenData<CrossAccountId>, unique_api);462463 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);464 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);465 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool, unique_api);466 pass_method!(last_token_id(collection: CollectionId) -> TokenId, unique_api);467 pass_method!(collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>, unique_api);468 pass_method!(collection_stats() -> CollectionStats, unique_api);469 pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>, unique_api);470 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>, unique_api);471}472473#[allow(deprecated)]474impl<475 C,476 Block,477 AccountId,478 CollectionInfo,479 NftInfo,480 ResourceInfo,481 PropertyInfo,482 BaseInfo,483 PartType,484 Theme,485 >486 rmrk_unique_rpc::RmrkApi<487 <Block as BlockT>::Hash,488 AccountId,489 CollectionInfo,490 NftInfo,491 ResourceInfo,492 PropertyInfo,493 BaseInfo,494 PartType,495 Theme,496 > for Unique<C, Block>497where498 C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,499 C::Api: RmrkRuntimeApi<500 Block,501 AccountId,502 CollectionInfo,503 NftInfo,504 ResourceInfo,505 PropertyInfo,506 BaseInfo,507 PartType,508 Theme,509 >,510 AccountId: Decode + Encode,511 CollectionInfo: Decode,512 NftInfo: Decode,513 ResourceInfo: Decode,514 PropertyInfo: Decode,515 BaseInfo: Decode,516 PartType: Decode,517 Theme: Decode,518 Block: BlockT,519{520 pass_method!(last_collection_idx() -> RmrkCollectionId, rmrk_api);521 pass_method!(collection_by_id(id: RmrkCollectionId) -> Option<CollectionInfo>, rmrk_api);522 pass_method!(nft_by_id(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Option<NftInfo>, rmrk_api);523 pass_method!(account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Vec<RmrkNftId>, rmrk_api);524 pass_method!(nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkNftChild>, rmrk_api);525 pass_method!(526 collection_properties(527 collection_id: RmrkCollectionId,528529 #[map(|keys| string_keys_to_bytes_keys(keys))]530 filter_keys: Option<Vec<String>>531 ) -> Vec<PropertyInfo>,532 rmrk_api533 );534 pass_method!(535 nft_properties(536 collection_id: RmrkCollectionId,537 nft_id: RmrkNftId,538539 #[map(|keys| string_keys_to_bytes_keys(keys))]540 filter_keys: Option<Vec<String>>541 ) -> Vec<PropertyInfo>,542 rmrk_api543 );544 pass_method!(nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<ResourceInfo>, rmrk_api);545 pass_method!(nft_resource_priorities(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkResourceId>, rmrk_api);546 pass_method!(base(base_id: RmrkBaseId) -> Option<BaseInfo>, rmrk_api);547 pass_method!(base_parts(base_id: RmrkBaseId) -> Vec<PartType>, rmrk_api);548 pass_method!(theme_names(base_id: RmrkBaseId) -> Vec<RmrkThemeName>, rmrk_api);549 pass_method!(550 theme(551 base_id: RmrkBaseId,552553 #[map(|n| n.into_bytes())]554 theme_name: String,555556 #[map(|keys| string_keys_to_bytes_keys(keys))]557 filter_keys: Option<Vec<String>>558 ) -> Option<Theme>, rmrk_api);559}560561fn string_keys_to_bytes_keys(keys: Option<Vec<String>>) -> Option<Vec<Vec<u8>>> {562 keys.map(|keys| keys.into_iter().map(|key| key.into_bytes()).collect())563}