Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET /api/core/v3/validators routes #1080

Merged
merged 19 commits into from
Sep 6, 2023
Merged
31 changes: 28 additions & 3 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use crate::{
},
types::{
api::core::response::{
BlockMetadataResponse, CommitteeResponse, CongestionResponse, InfoResponse, IssuanceBlockHeaderResponse,
ManaRewardsResponse, PeerResponse, RoutesResponse, SubmitBlockResponse, UtxoChangesResponse,
AccountStakingResponse, BlockMetadataResponse, CommitteeResponse, CongestionResponse, InfoResponse,
IssuanceBlockHeaderResponse, ManaRewardsResponse, PeerResponse, RoutesResponse, SubmitBlockResponse,
UtxoChangesResponse, ValidatorsResponse,
},
block::{
output::{dto::OutputDto, AccountId, Output, OutputId, OutputMetadata},
Expand Down Expand Up @@ -129,14 +130,38 @@ impl ClientInner {
const PATH: &str = "api/core/v3/committee";

let epoch_index = epoch_index.into().map(|i| format!("epochIndex={i}"));

self.node_manager
.read()
.await
.get_request(PATH, epoch_index.as_deref(), self.get_timeout().await, false, false)
.await
}

/// Returns information of all registered validators and if they are active.
/// GET JSON to /api/core/v3/validators
pub async fn get_validators(&self, page_size: Option<u32>) -> Result<ValidatorsResponse> {
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
const PATH: &str = "api/core/v3/validators";

let page_size = page_size.map(|i| format!("pageSize={i}"));
self.node_manager
.read()
.await
.get_request(PATH, page_size.as_deref(), self.get_timeout().await, false, false)
.await
}

/// Return the information of requested staker.
/// GET /api/core/v3/validators/{accountId}
pub async fn get_validator(&self, account_id: &AccountId) -> Result<AccountStakingResponse> {
Alex6323 marked this conversation as resolved.
Show resolved Hide resolved
let path = &format!("api/core/v3/validators/{account_id}");

self.node_manager
.read()
.await
.get_request(path, None, self.get_timeout().await, false, false)
.await
}

// Blocks routes.

/// Returns information that is ideal for attaching a block in the network.
Expand Down
136 changes: 94 additions & 42 deletions sdk/src/types/api/core/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,45 @@ pub struct BaseTokenResponse {
pub use_metric_prefix: bool,
}

/// Response of
/// - GET /api/core/v3/blocks/validators
/// A paginated list of all registered validators ready for the next epoch and indicates if they were active recently
/// (are eligible for committee selection).
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct ValidatorsResponse {
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
/// List of registered validators ready for the next epoch
validators: Vec<Validator>,
/// The number of validators returned per one API request with pagination.
page_size: u32,
/// The cursor that needs to be provided as cursor query parameter to request the next page. If empty, this was the
/// last page.
cursor: String,
}

/// Response of GET /api/core/v3/rewards/{outputId}.
/// Returns the mana rewards of an account or delegation output.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct ManaRewardsResponse {
/// The starting epoch index for which the mana rewards are returned.
pub epoch_start: u64, // TODO: replace with `EpochIndex`
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
/// The ending epoch index for which the mana rewards are returned, the decay is applied up to this point
/// included.
pub epoch_end: u64, // TODO: replace with `EpochIndex`
/// The amount of totally available rewards the requested output may claim.
#[serde(with = "crate::utils::serde::string")]
pub rewards: u64,
}

/// Response of GET /api/core/v3/committee
/// The validator information of the committee.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -206,7 +245,7 @@ pub struct CommitteeResponse {
pub committee: Box<[CommitteeMember]>,
}

/// Validator information.
/// Returns information of a committee member.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
Expand All @@ -227,8 +266,40 @@ pub struct CommitteeMember {
pub fixed_cost: u64,
}

/// Information of a validator.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct Validator {
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
/// The account identifier of the validator
account_id: AccountId,
/// The epoch index until which the validator registered to stake.
staking_end_epoch: EpochIndex,
/// The total stake of the pool, including delegators.
#[serde(with = "crate::utils::serde::string")]
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
pool_stake: u64,
/// The stake of a validator.
#[serde(with = "crate::utils::serde::string")]
validator_stake: u64,
/// The fixed cost of the validator, which it receives as part of its Mana rewards.
#[serde(with = "crate::utils::serde::string")]
fixed_cost: u64,
/// Shows whether validator was active recently.
active: bool,
/// The latest protocol version the validator supported.
latest_supported_protocol_version: u8,
}

/// Response of
/// - GET /api/core/v3/blocks/validators/{accountId}
/// The requested staking information of the account.
pub type AccountStakingResponse = Validator;

/// Response of GET /api/core/v3/blocks/issuance
/// Information that is ideal for attaching a block in the network.
/// Return information that is used to attach a block in the network.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
Expand All @@ -248,6 +319,27 @@ pub struct IssuanceBlockHeaderResponse {
pub commitment: SlotCommitment,
}

/// Response of GET /api/core/v3/accounts/{accountId}/congestion.
/// Provides the cost and readiness to issue estimates.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct CongestionResponse {
/// The slot index for which the congestion estimate is provided.
pub slot_index: SlotIndex,
/// Indicates if a node is ready to issue a block in a current congestion or should wait.
pub ready: bool,
/// The cost in mana for issuing a block in a current congestion estimated based on RMC and slot index.
#[serde(with = "crate::utils::serde::string")]
pub reference_mana_cost: u64,
/// The Block Issuance Credits of the requested account.
#[serde(with = "crate::utils::serde::string")]
pub block_issuance_credits: u64,
}

/// Response of POST /api/core/v3/blocks.
/// Returns the block identifier of the submitted block.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -474,43 +566,3 @@ pub struct UtxoChangesResponse {
pub created_outputs: Vec<OutputId>,
pub consumed_outputs: Vec<OutputId>,
}

/// Response of GET /api/core/v3/accounts/{accountId}/congestion.
/// Provides the cost and readiness to issue estimates.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct CongestionResponse {
/// The slot index for which the congestion estimate is provided.
pub slot_index: SlotIndex,
/// Indicates if a node is ready to issue a block in a current congestion or should wait.
pub ready: bool,
/// The cost in mana for issuing a block in a current congestion estimated based on RMC and slot index.
#[serde(with = "crate::utils::serde::string")]
pub reference_mana_cost: u64,
/// The Block Issuance Credits of the requested account.
#[serde(with = "crate::utils::serde::string")]
pub block_issuance_credits: u64,
}

/// Response of GET /api/core/v3/rewards/{outputId}.
/// Returns the mana rewards of an account or delegation output.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct ManaRewardsResponse {
/// The starting epoch index for which the mana rewards are returned.
pub epoch_start: u64, // TODO: replace with `EpochIndex`
/// The ending epoch index for which the mana rewards are returned, the decay is applied up to this point
/// included.
pub epoch_end: u64, // TODO: replace with `EpochIndex`
/// The amount of totally available rewards the requested output may claim.
#[serde(with = "crate::utils::serde::string")]
pub rewards: u64,
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/slot/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl EpochIndex {
/// Gets the epoch index given a [`SlotIndex`].
pub fn from_slot_index(
slot_index: SlotIndex,
slots_per_epoch_exponent_iter: impl Iterator<Item = (EpochIndex, u32)>,
slots_per_epoch_exponent_iter: impl Iterator<Item = (Self, u32)>,
) -> Result<Self, Error> {
let mut slot_index = *slot_index;
let mut res = 0;
Expand Down
Loading