Skip to content

Commit e69b807

Browse files
authored
num or hex serde for u128 rpc. (#1682)
1 parent 72737c6 commit e69b807

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

pallets/loans/rpc/src/lib.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ use sp_rpc::number::NumberOrHex;
2626
use sp_runtime::{generic::BlockId, traits::Block as BlockT, FixedU128};
2727

2828
#[rpc]
29-
pub trait LoansApi<BlockHash, AccountId, Balance> {
29+
pub trait LoansApi<BlockHash, AccountId, Balance>
30+
where
31+
Balance: Codec + Copy + TryFrom<NumberOrHex>,
32+
{
3033
#[rpc(name = "loans_getCollateralLiquidity")]
3134
fn get_account_liquidity(
3235
&self,
@@ -38,7 +41,7 @@ pub trait LoansApi<BlockHash, AccountId, Balance> {
3841
&self,
3942
asset_id: CurrencyId,
4043
at: Option<BlockHash>,
41-
) -> Result<(Rate, Rate, Rate, Ratio, Balance, Balance, FixedU128)>;
44+
) -> Result<(Rate, Rate, Rate, Ratio, NumberOrHex, NumberOrHex, FixedU128)>;
4245
#[rpc(name = "loans_getLiquidationThresholdLiquidity")]
4346
fn get_liquidation_threshold_liquidity(
4447
&self,
@@ -109,15 +112,33 @@ where
109112
&self,
110113
asset_id: CurrencyId,
111114
at: Option<<Block as BlockT>::Hash>,
112-
) -> Result<(Rate, Rate, Rate, Ratio, Balance, Balance, FixedU128)> {
115+
) -> Result<(Rate, Rate, Rate, Ratio, NumberOrHex, NumberOrHex, FixedU128)> {
113116
let api = self.client.runtime_api();
114117
let at = BlockId::hash(at.unwrap_or(
115118
// If the block hash is not supplied assume the best block.
116119
self.client.info().best_hash,
117120
));
118-
api.get_market_status(&at, asset_id)
121+
let (
122+
borrow_rate,
123+
supply_rate,
124+
exchange_rate,
125+
util,
126+
total_borrows,
127+
total_reserves,
128+
borrow_index,
129+
) = api
130+
.get_market_status(&at, asset_id)
119131
.map_err(runtime_error_into_rpc_error)?
120-
.map_err(market_status_error_into_rpc_error)
132+
.map_err(market_status_error_into_rpc_error)?;
133+
Ok((
134+
borrow_rate,
135+
supply_rate,
136+
exchange_rate,
137+
util,
138+
try_into_rpc_balance(total_borrows)?,
139+
try_into_rpc_balance(total_reserves)?,
140+
borrow_index,
141+
))
121142
}
122143

123144
fn get_liquidation_threshold_liquidity(
@@ -162,3 +183,13 @@ fn market_status_error_into_rpc_error(err: impl std::fmt::Debug) -> RpcError {
162183
data: Some(format!("{:?}", err).into()),
163184
}
164185
}
186+
187+
fn try_into_rpc_balance<T: std::fmt::Display + Copy + TryInto<NumberOrHex>>(
188+
value: T,
189+
) -> Result<NumberOrHex> {
190+
value.try_into().map_err(|_| RpcError {
191+
code: ErrorCode::InvalidParams,
192+
message: format!("{} doesn't fit in NumberOrHex representation", value),
193+
data: None,
194+
})
195+
}

0 commit comments

Comments
 (0)