Skip to content

Commit

Permalink
refactor(custom-rpc): simplify return types (#4059)
Browse files Browse the repository at this point in the history
* refactor(custom-rpc): simplify return types

* simplify simplifications

* chore: convert to str instead of using Debug

---------

Co-authored-by: Daniel <daniel@chainflip.io>
  • Loading branch information
acdibble and dandanlen authored Sep 28, 2023
1 parent 0168147 commit f54fedf
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub trait CustomApi {
pair_asset: Asset,
tick_range: Range<cf_amm::common::Tick>,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Option<Result<AssetsMap<Amount>, DispatchError>>>;
) -> RpcResult<Option<AssetsMap<Amount>>>;
#[method(name = "pool_info")]
fn cf_pool_info(
&self,
Expand All @@ -227,7 +227,7 @@ pub trait CustomApi {
pair_asset: Asset,
tick_range: Range<cf_amm::common::Tick>,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Option<Result<AssetsMap<Depth>, DispatchError>>>;
) -> RpcResult<Option<AssetsMap<Depth>>>;
#[method(name = "pool_liquidity")]
fn cf_pool_liquidity(
&self,
Expand All @@ -251,7 +251,7 @@ pub trait CustomApi {
tick_range: Range<Tick>,
liquidity: Liquidity,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Option<Result<AssetsMap<Amount>, DispatchError>>>;
) -> RpcResult<Option<AssetsMap<Amount>>>;
#[method(name = "environment")]
fn cf_environment(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<RpcEnvironment>;
#[method(name = "current_compatibility_version")]
Expand Down Expand Up @@ -289,6 +289,10 @@ fn to_rpc_error<E: std::error::Error + Send + Sync + 'static>(e: E) -> jsonrpsee
CallError::from_std_error(e).into()
}

fn map_dispatch_error(e: DispatchError) -> jsonrpsee::core::Error {
jsonrpsee::core::Error::from(anyhow::anyhow!("Dispatch error: {}", <&'static str>::from(e)))
}

impl<C, B> CustomApiServer for CustomRpc<C, B>
where
B: BlockT<Hash = state_chain_runtime::Hash>,
Expand Down Expand Up @@ -586,11 +590,13 @@ where
pair_asset: Asset,
tick_range: Range<Tick>,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Option<Result<AssetsMap<Depth>, DispatchError>>> {
) -> RpcResult<Option<AssetsMap<Depth>>> {
self.client
.runtime_api()
.cf_pool_depth(self.unwrap_or_best(at), base_asset, pair_asset, tick_range)
.map_err(to_rpc_error)
.map_err(to_rpc_error)?
.transpose()
.map_err(map_dispatch_error)
}

fn cf_pool_liquidity(
Expand All @@ -611,7 +617,7 @@ where
pair_asset: Asset,
tick_range: Range<cf_amm::common::Tick>,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Option<Result<AssetsMap<Amount>, DispatchError>>> {
) -> RpcResult<Option<AssetsMap<Amount>>> {
self.client
.runtime_api()
.cf_required_asset_ratio_for_range_order(
Expand All @@ -620,7 +626,9 @@ where
pair_asset,
tick_range,
)
.map_err(to_rpc_error)
.map_err(to_rpc_error)?
.transpose()
.map_err(map_dispatch_error)
}

fn cf_pool_orders(
Expand All @@ -643,7 +651,7 @@ where
tick_range: Range<Tick>,
liquidity: Liquidity,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Option<Result<AssetsMap<Amount>, DispatchError>>> {
) -> RpcResult<Option<AssetsMap<Amount>>> {
self.client
.runtime_api()
.cf_pool_range_order_liquidity_value(
Expand All @@ -653,7 +661,9 @@ where
tick_range,
liquidity,
)
.map_err(to_rpc_error)
.map_err(to_rpc_error)?
.transpose()
.map_err(map_dispatch_error)
}

fn cf_environment(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<RpcEnvironment> {
Expand Down

0 comments on commit f54fedf

Please sign in to comment.