difftreelog
fix backwards compatibility for token_owner rpc
in: master
2 files changed
client/rpc/src/lib.rsdiffbeforeafterboth4use jsonrpc_core::{Error as RpcError, ErrorCode, Result};4use jsonrpc_core::{Error as RpcError, ErrorCode, Result};5use jsonrpc_derive::rpc;5use jsonrpc_derive::rpc;6use up_data_structs::{Collection, CollectionId, CollectionStats, TokenId};6use up_data_structs::{Collection, CollectionId, CollectionStats, TokenId};7use sp_api::{BlockId, BlockT, ProvideRuntimeApi};7use sp_api::{BlockId, BlockT, ProvideRuntimeApi, ApiExt};8use sp_blockchain::HeaderBackend;8use sp_blockchain::HeaderBackend;9use up_rpc::UniqueApi as UniqueRuntimeApi;9use up_rpc::UniqueApi as UniqueRuntimeApi;1010134macro_rules! pass_method {134macro_rules! pass_method {135 ($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?) => {135 (136 $method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?137 $(; changed_in $ver:expr, $changed_method_name:ident ($($changed_name:expr), * $(,)?) => $fixer:expr)*138 ) => {136 fn $method_name(139 fn $method_name(137 &self,140 &self,142 ) -> Result<$result> {145 ) -> Result<$result> {143 let api = self.client.runtime_api();146 let api = self.client.runtime_api();144 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));147 let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));148 let _api_version = if let Ok(Some(api_version)) =149 api.api_version::<dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>>(&at)150 {151 api_version152 } else {153 // unreachable for our runtime154 return Err(RpcError {155 code: ErrorCode::InvalidParams,156 message: "Api is not available".into(),157 data: None,158 })159 };145160146 let result = api.$method_name(&at, $($name),*).map_err(|e| RpcError {161 let result = $(if _api_version < $ver {162 api.$changed_method_name(&at, $($changed_name),*).map(|r| r.map($fixer))163 } else)*164 { api.$method_name(&at, $($name),*) };165166 let result = result.map_err(|e| RpcError {147 code: ErrorCode::ServerError(Error::RuntimeError.into()),167 code: ErrorCode::ServerError(Error::RuntimeError.into()),148 message: "Unable to query".into(),168 message: "Unable to query".into(),149 data: Some(format!("{:?}", e).into()),169 data: Some(format!("{:?}", e).into()),169 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);189 pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);170 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);190 pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);171 pass_method!(token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>);191 pass_method!(192 token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>;193 changed_in 2, token_owner_before_version_2(collection, token) => |u| Some(u)194 );172 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);195 pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);173 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);196 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);primitives/rpc/src/lib.rsdiffbeforeafterboth9type Result<T> = core::result::Result<T, DispatchError>;9type Result<T> = core::result::Result<T, DispatchError>;101011sp_api::decl_runtime_apis! {11sp_api::decl_runtime_apis! {12 #[api_version(2)]12 pub trait UniqueApi<CrossAccountId, AccountId> where13 pub trait UniqueApi<CrossAccountId, AccountId> where13 AccountId: Decode,14 AccountId: Decode,14 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,15 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,15 {16 {17 #[changed_in(2)]18 fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;1916 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;20 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;17 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;21 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;