Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fixes
  • Loading branch information
gpestana committed Jan 9, 2023
commit d8ea2643b097429e5ad348b61d1a2df043ce614d
7 changes: 3 additions & 4 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_nfts::PalletFeatures;
use pallet_nis::WithMaximumOf;
use pallet_session::historical::{self as pallet_session_historical};
use pallet_staking::Nominators;
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use sp_api::impl_runtime_apis;
Expand Down Expand Up @@ -2138,9 +2137,9 @@ impl_runtime_apis! {
Staking::query_nominations_quota()
}

fn query_points_to_balance(pool_id: u32) -> Result<u32, ()> {
NominationPools::query_points_to_balance(pool_id)
}
fn query_points_to_balance(points: Balance, pool_id: u32) -> Result<Balance, ()> {
NominationPools::query_points_to_balance(points, pool_id)
}
}

impl pallet_mmr::primitives::MmrApi<
Expand Down
13 changes: 8 additions & 5 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,11 +2140,14 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
// Query points to balance for a given pool. Used for custom RPC call.
pub fn query_points_to_balance(pool_id: u32) -> Result<u32, ()> {
// TODO(gpestana): finish
Ok(0)
}
// Query points to balance for a given pool. Used for custom RPC call.
pub fn query_points_to_balance(points: BalanceOf<T>, pool_id: u32) -> Result<BalanceOf<T>, ()> {
if let Some(pool) = BondedPool::<T>::get(pool_id).defensive() {
Ok(pool.points_to_balance(points))
} else {
Err(())
}
}

/// Returns the pending rewards for the specified `member_account`.
///
Expand Down
2 changes: 1 addition & 1 deletion frame/staking/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ sp_api::decl_runtime_apis! {
Balance: Codec + MaybeDisplay,
{
fn query_nominations_quota() -> u32;
fn query_points_to_balance(pool_id: u32) -> Result<u32, ()>;
fn query_points_to_balance(points: Balance, pool_id: u32) -> Result<Balance, ()>;
}
}
42 changes: 21 additions & 21 deletions frame/staking/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub use pallet_staking_rpc_runtime_api::StakingApi as StakingRuntimeApi;
pub trait StakingApi<Balance> {
#[method(name = "staking_nominationsQuota")]
fn query_nominations_quota(&self) -> RpcResult<u32>;
#[method(name = "nominationPools_pointsToBalance")]
fn query_points_to_balance(&self, pool_id: u32) -> RpcResult<u32>;
#[method(name = "nominationPools_pointsToBalance")]
fn query_points_to_balance(&self, balance: Balance, pool_id: u32) -> RpcResult<Balance>;
}

pub struct Staking<C, P> {
Expand All @@ -55,15 +55,15 @@ impl<C, P> Staking<C, P> {
}

/// Error type of this RPC api.
pub enum Error {
pub enum ApiError {
/// The call to runtime failed.
RuntimeError,
}

impl From<Error> for i32 {
fn from(e: Error) -> i32 {
impl From<ApiError> for i32 {
fn from(e: ApiError) -> i32 {
match e {
Error::RuntimeError => 1,
ApiError::RuntimeError => 1,
}
}
}
Expand All @@ -83,31 +83,31 @@ where

Ok(runtime_api_result.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
ApiError::RuntimeError.into(),
"Unable to query the nominations quota.",
Some(e.to_string()),
))
})?)
}

fn query_points_to_balance(&self, pool_id: u32) -> RpcResult<u32> {
let api = self.client.runtime_api();
fn query_points_to_balance(&self, balance: Balance, pool_id: u32) -> RpcResult<Balance> {
let api = self.client.runtime_api();
let at = BlockId::hash(self.client.info().best_hash);

api.query_points_to_balance(&at, pool_id).map_err(|e| {
let result = api.query_points_to_balance(&at, balance, pool_id).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
ApiError::RuntimeError.into(),
"Unable to query the points to balance conversion for pool.",
Some(e.to_string()),
))
}).and_then(|r| {
r.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query the points to balance conversion for pool.",
Some(e.to_string()),
))
})?
})
}
})?;

Ok(result.map_err(|_| {
CallError::Custom(ErrorObject::owned(
ApiError::RuntimeError.into(),
"Pool ID not found",
None::<String>,
))
})?)
}
}