From 6e334734553591bec0e16240f1be892a77e4297f Mon Sep 17 00:00:00 2001 From: jerrybaoo Date: Wed, 12 Jul 2023 15:27:09 +0800 Subject: [PATCH] fix rpc --- zenlink-protocol/rpc/src/lib.rs | 26 ++++++------ zenlink-stable-amm/rpc/src/lib.rs | 69 +++++++++++++++---------------- 2 files changed, 46 insertions(+), 49 deletions(-) diff --git a/zenlink-protocol/rpc/src/lib.rs b/zenlink-protocol/rpc/src/lib.rs index daafdeb..5bab4c5 100644 --- a/zenlink-protocol/rpc/src/lib.rs +++ b/zenlink-protocol/rpc/src/lib.rs @@ -15,7 +15,7 @@ use jsonrpsee::{ use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_rpc::number::NumberOrHex; -use sp_runtime::{generic::BlockId, traits::Block as BlockT}; +use sp_runtime::traits::Block as BlockT; use std::sync::Arc; use zenlink_protocol::{AssetBalance, PairInfo}; @@ -107,9 +107,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_amount_in_price(&at, supply, path) + api.get_amount_in_price(at, supply, path) .map(|price| price.into()) .map_err(runtime_error_into_rpc_err) } @@ -122,9 +122,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_amount_out_price(&at, supply, path) + api.get_amount_out_price(at, supply, path) .map(|price| price.into()) .map_err(runtime_error_into_rpc_err) } @@ -140,10 +140,10 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); api.get_estimate_lptoken( - &at, + at, asset_0, asset_1, amount_0_desired, @@ -162,9 +162,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_balance(&at, asset_id, account) + api.get_balance(at, asset_id, account) .map(|asset_balance| asset_balance.into()) .map_err(runtime_error_into_rpc_err) } @@ -176,9 +176,9 @@ where at: Option<::Hash>, ) -> RpcResult>> { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_pair_by_asset_id(&at, asset_0, asset_1) + api.get_pair_by_asset_id(at, asset_0, asset_1) .map(|pairs| { pairs.map(|pair| PairInfo { asset_0: pair.asset_0, @@ -203,9 +203,9 @@ where at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.calculate_remove_liquidity(&at, asset_0, asset_1, amount) + api.calculate_remove_liquidity(at, asset_0, asset_1, amount) .map_err(runtime_error_into_rpc_err) } } diff --git a/zenlink-stable-amm/rpc/src/lib.rs b/zenlink-stable-amm/rpc/src/lib.rs index fed612d..8aeb662 100644 --- a/zenlink-stable-amm/rpc/src/lib.rs +++ b/zenlink-stable-amm/rpc/src/lib.rs @@ -15,10 +15,7 @@ use jsonrpsee::{ use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_rpc::number::NumberOrHex; -use sp_runtime::{ - generic::BlockId, - traits::{Block as BlockT, MaybeDisplay}, -}; +use sp_runtime::traits::{Block as BlockT, MaybeDisplay}; use std::sync::Arc; use zenlink_stable_amm_runtime_api::StableAmmApi as StableAmmRuntimeApi; @@ -156,9 +153,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - let price = api.get_virtual_price(&at, pool_id).map_err(runtime_error_into_rpc_err)?; + let price = api.get_virtual_price(at, pool_id).map_err(runtime_error_into_rpc_err)?; try_into_rpc_balance(price) } @@ -169,9 +166,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - let price = api.get_a(&at, pool_id).map_err(runtime_error_into_rpc_err)?; + let price = api.get_a(at, pool_id).map_err(runtime_error_into_rpc_err)?; try_into_rpc_balance(price) } @@ -182,9 +179,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - let price = api.get_a_precise(&at, pool_id).map_err(runtime_error_into_rpc_err)?; + let price = api.get_a_precise(at, pool_id).map_err(runtime_error_into_rpc_err)?; try_into_rpc_balance(price) } @@ -195,9 +192,9 @@ where at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_currencies(&at, pool_id).map_err(runtime_error_into_rpc_err) + api.get_currencies(at, pool_id).map_err(runtime_error_into_rpc_err) } fn get_currency( @@ -207,9 +204,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_currency(&at, pool_id, index).map_or_else( + api.get_currency(at, pool_id, index).map_or_else( |e| Err(runtime_error_into_rpc_err(e)), |v| v.ok_or(runtime_error_into_rpc_err("not found")), ) @@ -221,9 +218,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_lp_currency(&at, pool_id).map_or_else( + api.get_lp_currency(at, pool_id).map_or_else( |e| Err(runtime_error_into_rpc_err(e)), |v| v.ok_or(runtime_error_into_rpc_err("not found")), ) @@ -236,9 +233,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - let currencies = api.get_currencies(&at, pool_id).map_err(runtime_error_into_rpc_err)?; + let currencies = api.get_currencies(at, pool_id).map_err(runtime_error_into_rpc_err)?; for (i, c) in currencies.iter().enumerate() { if *c == currency { @@ -254,9 +251,9 @@ where at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_currency_precision_multipliers(&at, pool_id) + api.get_currency_precision_multipliers(at, pool_id) .map_err(runtime_error_into_rpc_err)? .iter() .map(|b| try_into_rpc_balance(*b)) @@ -269,9 +266,9 @@ where at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_currency_balances(&at, pool_id) + api.get_currency_balances(at, pool_id) .map_err(runtime_error_into_rpc_err)? .iter() .map(|b| try_into_rpc_balance(*b)) @@ -284,9 +281,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_number_of_currencies(&at, pool_id).map_err(runtime_error_into_rpc_err) + api.get_number_of_currencies(at, pool_id).map_err(runtime_error_into_rpc_err) } fn get_admin_balances( @@ -295,9 +292,9 @@ where at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_admin_balances(&at, pool_id) + api.get_admin_balances(at, pool_id) .map_err(runtime_error_into_rpc_err)? .iter() .map(|b| try_into_rpc_balance(*b)) @@ -311,9 +308,9 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - let balances = api.get_admin_balances(&at, pool_id).map_err(runtime_error_into_rpc_err)?; + let balances = api.get_admin_balances(at, pool_id).map_err(runtime_error_into_rpc_err)?; for (i, balance) in balances.iter().enumerate() { if i as u32 == index { @@ -332,10 +329,10 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); let amount = api - .calculate_currency_amount(&at, pool_id, amounts, deposit) + .calculate_currency_amount(at, pool_id, amounts, deposit) .map_err(runtime_error_into_rpc_err)?; try_into_rpc_balance(amount) @@ -350,10 +347,10 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); let amount = api - .calculate_swap(&at, pool_id, in_index, out_index, in_amount) + .calculate_swap(at, pool_id, in_index, out_index, in_amount) .map_err(runtime_error_into_rpc_err)?; try_into_rpc_balance(amount) @@ -366,9 +363,9 @@ where at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.calculate_remove_liquidity(&at, pool_id, amount) + api.calculate_remove_liquidity(at, pool_id, amount) .map_err(runtime_error_into_rpc_err)? .iter() .map(|b| try_into_rpc_balance(*b)) @@ -383,10 +380,10 @@ where at: Option<::Hash>, ) -> RpcResult { let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + let at = at.unwrap_or_else(|| self.client.info().best_hash); let amount = api - .calculate_remove_liquidity_one_currency(&at, pool_id, amount, index) + .calculate_remove_liquidity_one_currency(at, pool_id, amount, index) .map_err(runtime_error_into_rpc_err)?; try_into_rpc_balance(amount)