123456789101112131415161718use std::sync::Arc;1920use codec::{Decode, Encode};21use jsonrpsee::{22 core::{RpcResult as Result},23 proc_macros::rpc,24};25use anyhow::anyhow;26use sp_runtime::traits::{AtLeast32BitUnsigned, Member};27use up_data_structs::{28 RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property,29 PropertyKeyPermission, TokenData, TokenChild,30};31use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};32use sp_blockchain::HeaderBackend;33use up_rpc::UniqueApi as UniqueRuntimeApi;34use app_promotion_rpc::AppPromotionApi as AppPromotionRuntimeApi;353637use rmrk_rpc::RmrkApi as RmrkRuntimeApi;38use up_data_structs::{39 RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName, RmrkResourceId,40};4142pub use app_promotion_unique_rpc::AppPromotionApiServer;43pub use rmrk_unique_rpc::RmrkApiServer;4445#[rpc(server)]46#[async_trait]47pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {48 49 #[method(name = "unique_accountTokens")]50 fn account_tokens(51 &self,52 collection: CollectionId,53 account: CrossAccountId,54 at: Option<BlockHash>,55 ) -> Result<Vec<TokenId>>;5657 58 #[method(name = "unique_collectionTokens")]59 fn collection_tokens(60 &self,61 collection: CollectionId,62 at: Option<BlockHash>,63 ) -> Result<Vec<TokenId>>;6465 66 #[method(name = "unique_tokenExists")]67 fn token_exists(68 &self,69 collection: CollectionId,70 token: TokenId,71 at: Option<BlockHash>,72 ) -> Result<bool>;7374 75 #[method(name = "unique_tokenOwner")]76 fn token_owner(77 &self,78 collection: CollectionId,79 token: TokenId,80 at: Option<BlockHash>,81 ) -> Result<Option<CrossAccountId>>;8283 84 #[method(name = "unique_tokenOwners")]85 fn token_owners(86 &self,87 collection: CollectionId,88 token: TokenId,89 at: Option<BlockHash>,90 ) -> Result<Vec<CrossAccountId>>;9192 93 #[method(name = "unique_topmostTokenOwner")]94 fn topmost_token_owner(95 &self,96 collection: CollectionId,97 token: TokenId,98 at: Option<BlockHash>,99 ) -> Result<Option<CrossAccountId>>;100101 102 #[method(name = "unique_tokenChildren")]103 fn token_children(104 &self,105 collection: CollectionId,106 token: TokenId,107 at: Option<BlockHash>,108 ) -> Result<Vec<TokenChild>>;109110 111 #[method(name = "unique_collectionProperties")]112 fn collection_properties(113 &self,114 collection: CollectionId,115 keys: Option<Vec<String>>,116 at: Option<BlockHash>,117 ) -> Result<Vec<Property>>;118119 120 #[method(name = "unique_tokenProperties")]121 fn token_properties(122 &self,123 collection: CollectionId,124 token_id: TokenId,125 keys: Option<Vec<String>>,126 at: Option<BlockHash>,127 ) -> Result<Vec<Property>>;128129 130 #[method(name = "unique_propertyPermissions")]131 fn property_permissions(132 &self,133 collection: CollectionId,134 keys: Option<Vec<String>>,135 at: Option<BlockHash>,136 ) -> Result<Vec<PropertyKeyPermission>>;137138 139 #[method(name = "unique_tokenData")]140 fn token_data(141 &self,142 collection: CollectionId,143 token_id: TokenId,144 keys: Option<Vec<String>>,145 at: Option<BlockHash>,146 ) -> Result<TokenData<CrossAccountId>>;147148 149 #[method(name = "unique_totalSupply")]150 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;151152 153 #[method(name = "unique_accountBalance")]154 fn account_balance(155 &self,156 collection: CollectionId,157 account: CrossAccountId,158 at: Option<BlockHash>,159 ) -> Result<u32>;160161 162 #[method(name = "unique_balance")]163 fn balance(164 &self,165 collection: CollectionId,166 account: CrossAccountId,167 token: TokenId,168 at: Option<BlockHash>,169 ) -> Result<String>;170171 172 #[method(name = "unique_allowance")]173 fn allowance(174 &self,175 collection: CollectionId,176 sender: CrossAccountId,177 spender: CrossAccountId,178 token: TokenId,179 at: Option<BlockHash>,180 ) -> Result<String>;181182 183 #[method(name = "unique_adminlist")]184 fn adminlist(185 &self,186 collection: CollectionId,187 at: Option<BlockHash>,188 ) -> Result<Vec<CrossAccountId>>;189190 191 #[method(name = "unique_allowlist")]192 fn allowlist(193 &self,194 collection: CollectionId,195 at: Option<BlockHash>,196 ) -> Result<Vec<CrossAccountId>>;197198 199 #[method(name = "unique_allowed")]200 fn allowed(201 &self,202 collection: CollectionId,203 user: CrossAccountId,204 at: Option<BlockHash>,205 ) -> Result<bool>;206207 208 #[method(name = "unique_lastTokenId")]209 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;210211 212 #[method(name = "unique_collectionById")]213 fn collection_by_id(214 &self,215 collection: CollectionId,216 at: Option<BlockHash>,217 ) -> Result<Option<RpcCollection<AccountId>>>;218219 220 #[method(name = "unique_collectionStats")]221 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;222223 224 #[method(name = "unique_nextSponsored")]225 fn next_sponsored(226 &self,227 collection: CollectionId,228 account: CrossAccountId,229 token: TokenId,230 at: Option<BlockHash>,231 ) -> Result<Option<u64>>;232233 234 #[method(name = "unique_effectiveCollectionLimits")]235 fn effective_collection_limits(236 &self,237 collection_id: CollectionId,238 at: Option<BlockHash>,239 ) -> Result<Option<CollectionLimits>>;240241 242 #[method(name = "unique_totalPieces")]243 fn total_pieces(244 &self,245 collection_id: CollectionId,246 token_id: TokenId,247 at: Option<BlockHash>,248 ) -> Result<Option<String>>;249}250251mod app_promotion_unique_rpc {252 use super::*;253254 #[rpc(server)]255 #[async_trait]256 pub trait AppPromotionApi<BlockHash, BlockNumber, CrossAccountId, AccountId> {257 258 #[method(name = "appPromotion_totalStaked")]259 fn total_staked(260 &self,261 staker: Option<CrossAccountId>,262 at: Option<BlockHash>,263 ) -> Result<String>;264265 266 #[method(name = "appPromotion_totalStakedPerBlock")]267 fn total_staked_per_block(268 &self,269 staker: CrossAccountId,270 at: Option<BlockHash>,271 ) -> Result<Vec<(BlockNumber, String)>>;272273 274 #[method(name = "appPromotion_pendingUnstake")]275 fn pending_unstake(276 &self,277 staker: Option<CrossAccountId>,278 at: Option<BlockHash>,279 ) -> Result<String>;280281 282 #[method(name = "appPromotion_pendingUnstakePerBlock")]283 fn pending_unstake_per_block(284 &self,285 staker: CrossAccountId,286 at: Option<BlockHash>,287 ) -> Result<Vec<(BlockNumber, String)>>;288 }289}290291mod rmrk_unique_rpc {292 use super::*;293294 #[rpc(server)]295 #[async_trait]296 pub trait RmrkApi<297 BlockHash,298 AccountId,299 CollectionInfo,300 NftInfo,301 ResourceInfo,302 PropertyInfo,303 BaseInfo,304 PartType,305 Theme,306 >307 {308 309 #[method(name = "rmrk_lastCollectionIdx")]310 fn last_collection_idx(&self, at: Option<BlockHash>) -> Result<RmrkCollectionId>;311312 313 #[method(name = "rmrk_collectionById")]314 fn collection_by_id(315 &self,316 id: RmrkCollectionId,317 at: Option<BlockHash>,318 ) -> Result<Option<CollectionInfo>>;319320 321 #[method(name = "rmrk_nftById")]322 fn nft_by_id(323 &self,324 collection_id: RmrkCollectionId,325 nft_id: RmrkNftId,326 at: Option<BlockHash>,327 ) -> Result<Option<NftInfo>>;328329 330 #[method(name = "rmrk_accountTokens")]331 fn account_tokens(332 &self,333 account_id: AccountId,334 collection_id: RmrkCollectionId,335 at: Option<BlockHash>,336 ) -> Result<Vec<RmrkNftId>>;337338 339 #[method(name = "rmrk_nftChildren")]340 fn nft_children(341 &self,342 collection_id: RmrkCollectionId,343 nft_id: RmrkNftId,344 at: Option<BlockHash>,345 ) -> Result<Vec<RmrkNftChild>>;346347 348 #[method(name = "rmrk_collectionProperties")]349 fn collection_properties(350 &self,351 collection_id: RmrkCollectionId,352 filter_keys: Option<Vec<String>>,353 at: Option<BlockHash>,354 ) -> Result<Vec<PropertyInfo>>;355356 357 #[method(name = "rmrk_nftProperties")]358 fn nft_properties(359 &self,360 collection_id: RmrkCollectionId,361 nft_id: RmrkNftId,362 filter_keys: Option<Vec<String>>,363 at: Option<BlockHash>,364 ) -> Result<Vec<PropertyInfo>>;365366 367 #[method(name = "rmrk_nftResources")]368 fn nft_resources(369 &self,370 collection_id: RmrkCollectionId,371 nft_id: RmrkNftId,372 at: Option<BlockHash>,373 ) -> Result<Vec<ResourceInfo>>;374375 376 #[method(name = "rmrk_nftResourcePriority")]377 fn nft_resource_priority(378 &self,379 collection_id: RmrkCollectionId,380 nft_id: RmrkNftId,381 resource_id: RmrkResourceId,382 at: Option<BlockHash>,383 ) -> Result<Option<u32>>;384385 386 #[method(name = "rmrk_base")]387 fn base(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Option<BaseInfo>>;388389 390 #[method(name = "rmrk_baseParts")]391 fn base_parts(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Vec<PartType>>;392393 394 #[method(name = "rmrk_themeNames")]395 fn theme_names(396 &self,397 base_id: RmrkBaseId,398 at: Option<BlockHash>,399 ) -> Result<Vec<RmrkThemeName>>;400401 402 #[method(name = "rmrk_themes")]403 fn theme(404 &self,405 base_id: RmrkBaseId,406 theme_name: String,407 filter_keys: Option<Vec<String>>,408 at: Option<BlockHash>,409 ) -> Result<Option<Theme>>;410 }411}412413macro_rules! define_struct_for_server_api {414 ($name:ident) => {415 pub struct $name<C, P> {416 client: Arc<C>,417 _marker: std::marker::PhantomData<P>,418 }419420 impl<C, P> $name<C, P> {421 pub fn new(client: Arc<C>) -> Self {422 Self {423 client,424 _marker: Default::default(),425 }426 }427 }428 };429}430431define_struct_for_server_api!(Unique);432define_struct_for_server_api!(AppPromotion);433define_struct_for_server_api!(Rmrk);434435macro_rules! pass_method {436 (437 $method_name:ident(438 $($(#[map(|$map_arg:ident| $map:expr)])? $name:ident: $ty:ty),* $(,)?439 ) -> $result:ty $(=> $mapper:expr)?,440 441 $runtime_api_macro:ident442 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*443 ) => {444 fn $method_name(445 &self,446 $(447 $name: $ty,448 )*449 at: Option<<Block as BlockT>::Hash>,450 ) -> Result<$result> {451 let api = self.client.runtime_api();452 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));453 let _api_version = if let Ok(Some(api_version)) =454 api.api_version::<$runtime_api_macro!()>(&at)455 {456 api_version457 } else {458 459 return Err(anyhow!("api is not available").into())460 };461462 let result = $(if _api_version < $ver {463 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))464 } else)*465 { api.$method_name(&at, $($((|$map_arg: $ty| $map))? ($name)),*) };466467 Ok(result468 .map_err(|e| anyhow!("unable to query: {e}"))?469 .map_err(|e| anyhow!("runtime error: {e:?}"))$(.map($mapper))??)470 }471 };472}473474macro_rules! unique_api {475 () => {476 dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>477 };478}479480macro_rules! app_promotion_api {481 () => {482 dyn AppPromotionRuntimeApi<Block, BlockNumber, CrossAccountId, AccountId>483 };484}485486macro_rules! rmrk_api {487 () => {488 dyn RmrkRuntimeApi<Block, AccountId, CollectionInfo, NftInfo, ResourceInfo, PropertyInfo, BaseInfo, PartType, Theme>489 };490}491492#[allow(deprecated)]493impl<C, Block, CrossAccountId, AccountId>494 UniqueApiServer<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>495where496 Block: BlockT,497 AccountId: Decode,498 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,499 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,500 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,501{502 pass_method!(503 account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>, unique_api504 );505 pass_method!(506 collection_tokens(collection: CollectionId) -> Vec<TokenId>, unique_api507 );508 pass_method!(509 token_exists(collection: CollectionId, token: TokenId) -> bool, unique_api510 );511 pass_method!(512 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api513 );514 pass_method!(515 topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api516 );517 pass_method!(token_children(collection: CollectionId, token: TokenId) -> Vec<TokenChild>, unique_api);518 pass_method!(total_supply(collection: CollectionId) -> u32, unique_api);519 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32, unique_api);520 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string(), unique_api);521 pass_method!(522 allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string(),523 unique_api524 );525526 pass_method!(collection_properties(527 collection: CollectionId,528529 #[map(|keys| string_keys_to_bytes_keys(keys))]530 keys: Option<Vec<String>>531 ) -> Vec<Property>, unique_api);532533 pass_method!(token_properties(534 collection: CollectionId,535 token_id: TokenId,536537 #[map(|keys| string_keys_to_bytes_keys(keys))]538 keys: Option<Vec<String>>539 ) -> Vec<Property>, unique_api);540541 pass_method!(property_permissions(542 collection: CollectionId,543544 #[map(|keys| string_keys_to_bytes_keys(keys))]545 keys: Option<Vec<String>>546 ) -> Vec<PropertyKeyPermission>, unique_api);547548 pass_method!(549 token_data(550 collection: CollectionId,551 token_id: TokenId,552553 #[map(|keys| string_keys_to_bytes_keys(keys))]554 keys: Option<Vec<String>>,555 ) -> TokenData<CrossAccountId>, unique_api;556 changed_in 3, token_data_before_version_3(collection, token_id, string_keys_to_bytes_keys(keys)) => |value| value.into()557 );558559 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);560 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);561 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool, unique_api);562 pass_method!(last_token_id(collection: CollectionId) -> TokenId, unique_api);563 pass_method!(564 collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>, unique_api;565 changed_in 3, collection_by_id_before_version_3(collection) => |value| value.map(|coll| coll.into())566 );567 pass_method!(collection_stats() -> CollectionStats, unique_api);568 pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>, unique_api);569 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>, unique_api);570 pass_method!(total_pieces(collection_id: CollectionId, token_id: TokenId) -> Option<String> => |o| o.map(|number| number.to_string()) , unique_api);571 pass_method!(token_owners(collection: CollectionId, token: TokenId) -> Vec<CrossAccountId>, unique_api);572}573574impl<C, Block, BlockNumber, CrossAccountId, AccountId>575 app_promotion_unique_rpc::AppPromotionApiServer<576 <Block as BlockT>::Hash,577 BlockNumber,578 CrossAccountId,579 AccountId,580 > for AppPromotion<C, Block>581where582 Block: BlockT,583 BlockNumber: Decode + Member + AtLeast32BitUnsigned,584 AccountId: Decode,585 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,586 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,587 C::Api: AppPromotionRuntimeApi<Block, BlockNumber, CrossAccountId, AccountId>,588{589 pass_method!(total_staked(staker: Option<CrossAccountId>) -> String => |v| v.to_string(), app_promotion_api);590 pass_method!(total_staked_per_block(staker: CrossAccountId) -> Vec<(BlockNumber, String)> =>591 |v| v592 .into_iter()593 .map(|(b, a)| (b, a.to_string()))594 .collect::<Vec<_>>(), app_promotion_api);595 pass_method!(pending_unstake(staker: Option<CrossAccountId>) -> String => |v| v.to_string(), app_promotion_api);596 pass_method!(pending_unstake_per_block(staker: CrossAccountId) -> Vec<(BlockNumber, String)> =>597 |v| v598 .into_iter()599 .map(|(b, a)| (b, a.to_string()))600 .collect::<Vec<_>>(), app_promotion_api);601}602603#[allow(deprecated)]604impl<605 C,606 Block,607 AccountId,608 CollectionInfo,609 NftInfo,610 ResourceInfo,611 PropertyInfo,612 BaseInfo,613 PartType,614 Theme,615 >616 rmrk_unique_rpc::RmrkApiServer<617 <Block as BlockT>::Hash,618 AccountId,619 CollectionInfo,620 NftInfo,621 ResourceInfo,622 PropertyInfo,623 BaseInfo,624 PartType,625 Theme,626 > for Rmrk<C, Block>627where628 C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,629 C::Api: RmrkRuntimeApi<630 Block,631 AccountId,632 CollectionInfo,633 NftInfo,634 ResourceInfo,635 PropertyInfo,636 BaseInfo,637 PartType,638 Theme,639 >,640 AccountId: Decode + Encode,641 CollectionInfo: Decode,642 NftInfo: Decode,643 ResourceInfo: Decode,644 PropertyInfo: Decode,645 BaseInfo: Decode,646 PartType: Decode,647 Theme: Decode,648 Block: BlockT,649{650 pass_method!(last_collection_idx() -> RmrkCollectionId, rmrk_api);651 pass_method!(collection_by_id(id: RmrkCollectionId) -> Option<CollectionInfo>, rmrk_api);652 pass_method!(nft_by_id(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Option<NftInfo>, rmrk_api);653 pass_method!(account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Vec<RmrkNftId>, rmrk_api);654 pass_method!(nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkNftChild>, rmrk_api);655 pass_method!(656 collection_properties(657 collection_id: RmrkCollectionId,658659 #[map(|keys| string_keys_to_bytes_keys(keys))]660 filter_keys: Option<Vec<String>>661 ) -> Vec<PropertyInfo>,662 rmrk_api663 );664 pass_method!(665 nft_properties(666 collection_id: RmrkCollectionId,667 nft_id: RmrkNftId,668669 #[map(|keys| string_keys_to_bytes_keys(keys))]670 filter_keys: Option<Vec<String>>671 ) -> Vec<PropertyInfo>,672 rmrk_api673 );674 pass_method!(nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<ResourceInfo>, rmrk_api);675 pass_method!(nft_resource_priority(collection_id: RmrkCollectionId, nft_id: RmrkNftId, resource_id: RmrkResourceId) -> Option<u32>, rmrk_api);676 pass_method!(base(base_id: RmrkBaseId) -> Option<BaseInfo>, rmrk_api);677 pass_method!(base_parts(base_id: RmrkBaseId) -> Vec<PartType>, rmrk_api);678 pass_method!(theme_names(base_id: RmrkBaseId) -> Vec<RmrkThemeName>, rmrk_api);679 pass_method!(680 theme(681 base_id: RmrkBaseId,682683 #[map(|n| n.into_bytes())]684 theme_name: String,685686 #[map(|keys| string_keys_to_bytes_keys(keys))]687 filter_keys: Option<Vec<String>>688 ) -> Option<Theme>, rmrk_api);689}690691fn string_keys_to_bytes_keys(keys: Option<Vec<String>>) -> Option<Vec<Vec<u8>>> {692 keys.map(|keys| keys.into_iter().map(|key| key.into_bytes()).collect())693}