Skip to content

Commit

Permalink
fix rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrybaoo committed Jul 12, 2023
1 parent a7358c3 commit 6e33473
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
26 changes: 13 additions & 13 deletions zenlink-protocol/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -107,9 +107,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
}
Expand All @@ -122,9 +122,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
}
Expand All @@ -140,10 +140,10 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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,
Expand All @@ -162,9 +162,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
}
Expand All @@ -176,9 +176,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<PairInfo<AccountId, NumberOrHex, AssetId>>> {
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,
Expand All @@ -203,9 +203,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<(AssetBalance, AssetBalance)>> {
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)
}
}
Expand Down
69 changes: 33 additions & 36 deletions zenlink-stable-amm/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -156,9 +153,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
}
Expand All @@ -169,9 +166,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
}
Expand All @@ -182,9 +179,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
}
Expand All @@ -195,9 +192,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<CurrencyId>> {
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(
Expand All @@ -207,9 +204,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<CurrencyId> {
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")),
)
Expand All @@ -221,9 +218,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<CurrencyId> {
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")),
)
Expand All @@ -236,9 +233,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<u32> {
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 {
Expand All @@ -254,9 +251,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<NumberOrHex>> {
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))
Expand All @@ -269,9 +266,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<NumberOrHex>> {
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))
Expand All @@ -284,9 +281,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<u32> {
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(
Expand All @@ -295,9 +292,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<NumberOrHex>> {
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))
Expand All @@ -311,9 +308,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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 {
Expand All @@ -332,10 +329,10 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
Expand All @@ -350,10 +347,10 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
Expand All @@ -366,9 +363,9 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<NumberOrHex>> {
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))
Expand All @@ -383,10 +380,10 @@ where
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
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)
Expand Down

0 comments on commit 6e33473

Please sign in to comment.