1234567891011121314151617use std::sync::Arc;1819use codec::{Decode, Encode};20use jsonrpsee::{21 core::{RpcResult as Result},22 proc_macros::rpc,23};24use anyhow::anyhow;25use up_data_structs::{26 RpcCollection, CollectionId, CollectionStats, CollectionLimits, TokenId, Property,27 PropertyKeyPermission, TokenData, TokenChild,28};29use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};30use sp_blockchain::HeaderBackend;31use up_rpc::UniqueApi as UniqueRuntimeApi;323334use rmrk_rpc::RmrkApi as RmrkRuntimeApi;35use up_data_structs::{36 RmrkCollectionId, RmrkNftId, RmrkBaseId, RmrkNftChild, RmrkThemeName, RmrkResourceId,37};3839pub use rmrk_unique_rpc::RmrkApiServer;4041#[rpc(server)]42#[async_trait]43pub trait UniqueApi<BlockHash, CrossAccountId, AccountId> {44 #[method(name = "unique_accountTokens")]45 fn account_tokens(46 &self,47 collection: CollectionId,48 account: CrossAccountId,49 at: Option<BlockHash>,50 ) -> Result<Vec<TokenId>>;51 #[method(name = "unique_collectionTokens")]52 fn collection_tokens(53 &self,54 collection: CollectionId,55 at: Option<BlockHash>,56 ) -> Result<Vec<TokenId>>;57 #[method(name = "unique_tokenExists")]58 fn token_exists(59 &self,60 collection: CollectionId,61 token: TokenId,62 at: Option<BlockHash>,63 ) -> Result<bool>;6465 #[method(name = "unique_tokenOwner")]66 fn token_owner(67 &self,68 collection: CollectionId,69 token: TokenId,70 at: Option<BlockHash>,71 ) -> Result<Option<CrossAccountId>>;72 #[method(name = "unique_topmostTokenOwner")]73 fn topmost_token_owner(74 &self,75 collection: CollectionId,76 token: TokenId,77 at: Option<BlockHash>,78 ) -> Result<Option<CrossAccountId>>;79 #[method(name = "unique_tokenChildren")]80 fn token_children(81 &self,82 collection: CollectionId,83 token: TokenId,84 at: Option<BlockHash>,85 ) -> Result<Vec<TokenChild>>;8687 #[method(name = "unique_collectionProperties")]88 fn collection_properties(89 &self,90 collection: CollectionId,91 keys: Option<Vec<String>>,92 at: Option<BlockHash>,93 ) -> Result<Vec<Property>>;9495 #[method(name = "unique_tokenProperties")]96 fn token_properties(97 &self,98 collection: CollectionId,99 token_id: TokenId,100 keys: Option<Vec<String>>,101 at: Option<BlockHash>,102 ) -> Result<Vec<Property>>;103104 #[method(name = "unique_propertyPermissions")]105 fn property_permissions(106 &self,107 collection: CollectionId,108 keys: Option<Vec<String>>,109 at: Option<BlockHash>,110 ) -> Result<Vec<PropertyKeyPermission>>;111112 #[method(name = "unique_tokenData")]113 fn token_data(114 &self,115 collection: CollectionId,116 token_id: TokenId,117 keys: Option<Vec<String>>,118 at: Option<BlockHash>,119 ) -> Result<TokenData<CrossAccountId>>;120121 #[method(name = "unique_totalSupply")]122 fn total_supply(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;123 #[method(name = "unique_accountBalance")]124 fn account_balance(125 &self,126 collection: CollectionId,127 account: CrossAccountId,128 at: Option<BlockHash>,129 ) -> Result<u32>;130 #[method(name = "unique_balance")]131 fn balance(132 &self,133 collection: CollectionId,134 account: CrossAccountId,135 token: TokenId,136 at: Option<BlockHash>,137 ) -> Result<String>;138 #[method(name = "unique_allowance")]139 fn allowance(140 &self,141 collection: CollectionId,142 sender: CrossAccountId,143 spender: CrossAccountId,144 token: TokenId,145 at: Option<BlockHash>,146 ) -> Result<String>;147148 #[method(name = "unique_adminlist")]149 fn adminlist(150 &self,151 collection: CollectionId,152 at: Option<BlockHash>,153 ) -> Result<Vec<CrossAccountId>>;154 #[method(name = "unique_allowlist")]155 fn allowlist(156 &self,157 collection: CollectionId,158 at: Option<BlockHash>,159 ) -> Result<Vec<CrossAccountId>>;160 #[method(name = "unique_allowed")]161 fn allowed(162 &self,163 collection: CollectionId,164 user: CrossAccountId,165 at: Option<BlockHash>,166 ) -> Result<bool>;167 #[method(name = "unique_lastTokenId")]168 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;169 #[method(name = "unique_collectionById")]170 fn collection_by_id(171 &self,172 collection: CollectionId,173 at: Option<BlockHash>,174 ) -> Result<Option<RpcCollection<AccountId>>>;175 #[method(name = "unique_collectionStats")]176 fn collection_stats(&self, at: Option<BlockHash>) -> Result<CollectionStats>;177178 #[method(name = "unique_nextSponsored")]179 fn next_sponsored(180 &self,181 collection: CollectionId,182 account: CrossAccountId,183 token: TokenId,184 at: Option<BlockHash>,185 ) -> Result<Option<u64>>;186 #[method(name = "unique_effectiveCollectionLimits")]187 fn effective_collection_limits(188 &self,189 collection_id: CollectionId,190 at: Option<BlockHash>,191 ) -> Result<Option<CollectionLimits>>;192}193194mod rmrk_unique_rpc {195 use super::*;196197 #[rpc(server)]198 #[async_trait]199 pub trait RmrkApi<200 BlockHash,201 AccountId,202 CollectionInfo,203 NftInfo,204 ResourceInfo,205 PropertyInfo,206 BaseInfo,207 PartType,208 Theme,209 >210 {211 #[method(name = "rmrk_lastCollectionIdx")]212 213 fn last_collection_idx(&self, at: Option<BlockHash>) -> Result<RmrkCollectionId>;214215 #[method(name = "rmrk_collectionById")]216 217 fn collection_by_id(218 &self,219 id: RmrkCollectionId,220 at: Option<BlockHash>,221 ) -> Result<Option<CollectionInfo>>;222223 #[method(name = "rmrk_nftById")]224 225 fn nft_by_id(226 &self,227 collection_id: RmrkCollectionId,228 nft_id: RmrkNftId,229 at: Option<BlockHash>,230 ) -> Result<Option<NftInfo>>;231232 #[method(name = "rmrk_accountTokens")]233 234 fn account_tokens(235 &self,236 account_id: AccountId,237 collection_id: RmrkCollectionId,238 at: Option<BlockHash>,239 ) -> Result<Vec<RmrkNftId>>;240241 #[method(name = "rmrk_nftChildren")]242 243 fn nft_children(244 &self,245 collection_id: RmrkCollectionId,246 nft_id: RmrkNftId,247 at: Option<BlockHash>,248 ) -> Result<Vec<RmrkNftChild>>;249250 #[method(name = "rmrk_collectionProperties")]251 252 fn collection_properties(253 &self,254 collection_id: RmrkCollectionId,255 filter_keys: Option<Vec<String>>,256 at: Option<BlockHash>,257 ) -> Result<Vec<PropertyInfo>>;258259 #[method(name = "rmrk_nftProperties")]260 261 fn nft_properties(262 &self,263 collection_id: RmrkCollectionId,264 nft_id: RmrkNftId,265 filter_keys: Option<Vec<String>>,266 at: Option<BlockHash>,267 ) -> Result<Vec<PropertyInfo>>;268269 #[method(name = "rmrk_nftResources")]270 271 fn nft_resources(272 &self,273 collection_id: RmrkCollectionId,274 nft_id: RmrkNftId,275 at: Option<BlockHash>,276 ) -> Result<Vec<ResourceInfo>>;277278 #[method(name = "rmrk_nftResourcePriorities")]279 280 fn nft_resource_priorities(281 &self,282 collection_id: RmrkCollectionId,283 nft_id: RmrkNftId,284 at: Option<BlockHash>,285 ) -> Result<Vec<RmrkResourceId>>;286287 #[method(name = "rmrk_base")]288 289 fn base(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Option<BaseInfo>>;290291 #[method(name = "rmrk_baseParts")]292 293 fn base_parts(&self, base_id: RmrkBaseId, at: Option<BlockHash>) -> Result<Vec<PartType>>;294295 #[method(name = "rmrk_themeNames")]296 fn theme_names(297 &self,298 base_id: RmrkBaseId,299 at: Option<BlockHash>,300 ) -> Result<Vec<RmrkThemeName>>;301302 #[method(name = "rmrk_themes")]303 fn theme(304 &self,305 base_id: RmrkBaseId,306 theme_name: String,307 filter_keys: Option<Vec<String>>,308 at: Option<BlockHash>,309 ) -> Result<Option<Theme>>;310 }311}312313pub struct Unique<C, P> {314 client: Arc<C>,315 _marker: std::marker::PhantomData<P>,316}317318impl<C, P> Unique<C, P> {319 pub fn new(client: Arc<C>) -> Self {320 Self {321 client,322 _marker: Default::default(),323 }324 }325}326327macro_rules! pass_method {328 (329 $method_name:ident(330 $($(#[map(|$map_arg:ident| $map:expr)])? $name:ident: $ty:ty),* $(,)?331 ) -> $result:ty $(=> $mapper:expr)?,332 333 $runtime_api_macro:ident334 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*335 ) => {336 fn $method_name(337 &self,338 $(339 $name: $ty,340 )*341 at: Option<<Block as BlockT>::Hash>,342 ) -> Result<$result> {343 let api = self.client.runtime_api();344 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));345 let _api_version = if let Ok(Some(api_version)) =346 api.api_version::<$runtime_api_macro!()>(&at)347 {348 api_version349 } else {350 351 return Err(anyhow!("api is not available").into())352 };353354 let result = $(if _api_version < $ver {355 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))356 } else)*357 { api.$method_name(&at, $($((|$map_arg: $ty| $map))? ($name)),*) };358359 Ok(result360 .map_err(|e| anyhow!("unable to query: {e}"))?361 .map_err(|e| anyhow!("runtime error: {e:?}"))$(.map($mapper))??)362 }363 };364}365366macro_rules! unique_api {367 () => {368 dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>369 };370}371372macro_rules! rmrk_api {373 () => {374 dyn RmrkRuntimeApi<Block, AccountId, CollectionInfo, NftInfo, ResourceInfo, PropertyInfo, BaseInfo, PartType, Theme>375 };376}377378#[allow(deprecated)]379impl<C, Block, CrossAccountId, AccountId>380 UniqueApiServer<<Block as BlockT>::Hash, CrossAccountId, AccountId> for Unique<C, Block>381where382 Block: BlockT,383 AccountId: Decode,384 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,385 C::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,386 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,387{388 pass_method!(389 account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>, unique_api390 );391 pass_method!(392 collection_tokens(collection: CollectionId) -> Vec<TokenId>, unique_api393 );394 pass_method!(395 token_exists(collection: CollectionId, token: TokenId) -> bool, unique_api396 );397 pass_method!(398 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api399 );400 pass_method!(401 topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>, unique_api402 );403 pass_method!(token_children(collection: CollectionId, token: TokenId) -> Vec<TokenChild>, unique_api);404 pass_method!(total_supply(collection: CollectionId) -> u32, unique_api);405 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32, unique_api);406 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string(), unique_api);407 pass_method!(408 allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string(),409 unique_api410 );411412 pass_method!(collection_properties(413 collection: CollectionId,414415 #[map(|keys| string_keys_to_bytes_keys(keys))]416 keys: Option<Vec<String>>417 ) -> Vec<Property>, unique_api);418419 pass_method!(token_properties(420 collection: CollectionId,421 token_id: TokenId,422423 #[map(|keys| string_keys_to_bytes_keys(keys))]424 keys: Option<Vec<String>>425 ) -> Vec<Property>, unique_api);426427 pass_method!(property_permissions(428 collection: CollectionId,429430 #[map(|keys| string_keys_to_bytes_keys(keys))]431 keys: Option<Vec<String>>432 ) -> Vec<PropertyKeyPermission>, unique_api);433434 pass_method!(token_data(435 collection: CollectionId,436 token_id: TokenId,437438 #[map(|keys| string_keys_to_bytes_keys(keys))]439 keys: Option<Vec<String>>,440 ) -> TokenData<CrossAccountId>, unique_api);441442 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);443 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);444 pass_method!(allowed(collection: CollectionId, user: CrossAccountId) -> bool, unique_api);445 pass_method!(last_token_id(collection: CollectionId) -> TokenId, unique_api);446 pass_method!(collection_by_id(collection: CollectionId) -> Option<RpcCollection<AccountId>>, unique_api);447 pass_method!(collection_stats() -> CollectionStats, unique_api);448 pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>, unique_api);449 pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>, unique_api);450}451452#[allow(deprecated)]453impl<454 C,455 Block,456 AccountId,457 CollectionInfo,458 NftInfo,459 ResourceInfo,460 PropertyInfo,461 BaseInfo,462 PartType,463 Theme,464 >465 rmrk_unique_rpc::RmrkApiServer<466 <Block as BlockT>::Hash,467 AccountId,468 CollectionInfo,469 NftInfo,470 ResourceInfo,471 PropertyInfo,472 BaseInfo,473 PartType,474 Theme,475 > for Unique<C, Block>476where477 C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,478 C::Api: RmrkRuntimeApi<479 Block,480 AccountId,481 CollectionInfo,482 NftInfo,483 ResourceInfo,484 PropertyInfo,485 BaseInfo,486 PartType,487 Theme,488 >,489 AccountId: Decode + Encode,490 CollectionInfo: Decode,491 NftInfo: Decode,492 ResourceInfo: Decode,493 PropertyInfo: Decode,494 BaseInfo: Decode,495 PartType: Decode,496 Theme: Decode,497 Block: BlockT,498{499 pass_method!(last_collection_idx() -> RmrkCollectionId, rmrk_api);500 pass_method!(collection_by_id(id: RmrkCollectionId) -> Option<CollectionInfo>, rmrk_api);501 pass_method!(nft_by_id(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Option<NftInfo>, rmrk_api);502 pass_method!(account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Vec<RmrkNftId>, rmrk_api);503 pass_method!(nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkNftChild>, rmrk_api);504 pass_method!(505 collection_properties(506 collection_id: RmrkCollectionId,507508 #[map(|keys| string_keys_to_bytes_keys(keys))]509 filter_keys: Option<Vec<String>>510 ) -> Vec<PropertyInfo>,511 rmrk_api512 );513 pass_method!(514 nft_properties(515 collection_id: RmrkCollectionId,516 nft_id: RmrkNftId,517518 #[map(|keys| string_keys_to_bytes_keys(keys))]519 filter_keys: Option<Vec<String>>520 ) -> Vec<PropertyInfo>,521 rmrk_api522 );523 pass_method!(nft_resources(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<ResourceInfo>, rmrk_api);524 pass_method!(nft_resource_priorities(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Vec<RmrkResourceId>, rmrk_api);525 pass_method!(base(base_id: RmrkBaseId) -> Option<BaseInfo>, rmrk_api);526 pass_method!(base_parts(base_id: RmrkBaseId) -> Vec<PartType>, rmrk_api);527 pass_method!(theme_names(base_id: RmrkBaseId) -> Vec<RmrkThemeName>, rmrk_api);528 pass_method!(529 theme(530 base_id: RmrkBaseId,531532 #[map(|n| n.into_bytes())]533 theme_name: String,534535 #[map(|keys| string_keys_to_bytes_keys(keys))]536 filter_keys: Option<Vec<String>>537 ) -> Option<Theme>, rmrk_api);538}539540fn string_keys_to_bytes_keys(keys: Option<Vec<String>>) -> Option<Vec<Vec<u8>>> {541 keys.map(|keys| keys.into_iter().map(|key| key.into_bytes()).collect())542}