v0.2.4-pre.008
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-onchain-transport-lib/src/rpc_economics.rs
|
||||
// version: 3
|
||||
// version: 4
|
||||
|
||||
/// Inflation-governor values returned by `getInflationGovernor`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
@@ -142,14 +142,12 @@ impl SolanaInflationRewardConfig {
|
||||
}
|
||||
|
||||
/// Returns whether this config would serialize to an empty object.
|
||||
#[cfg(test)]
|
||||
pub(crate) const fn is_empty(&self) -> bool {
|
||||
return self.epoch.is_none() && self.commitment.is_none() && self.min_context_slot.is_none();
|
||||
}
|
||||
|
||||
/// Serializes this config to the exact Solana JSON-RPC object.
|
||||
#[must_use]
|
||||
#[cfg(test)]
|
||||
pub(crate) fn to_json_value(self) -> serde_json::Value {
|
||||
let mut object = serde_json::Map::new();
|
||||
if let std::option::Option::Some(epoch) = self.epoch {
|
||||
@@ -214,7 +212,6 @@ impl SolanaInflationReward {
|
||||
}
|
||||
|
||||
/// Decodes one non-null inflation reward from its Solana JSON wire shape.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn decode_wire(method: &str, value: serde_json::Value) -> ksp_core_lib::Result<Self> {
|
||||
let decoded = crate::decode_wire_json::<WireInflationReward>(method, value);
|
||||
return match decoded {
|
||||
@@ -359,6 +356,40 @@ impl crate::HttpTransportPool {
|
||||
};
|
||||
}
|
||||
|
||||
/// Executes typed `getInflationReward`, preserving input order, positional nulls and runtime commission extensions.
|
||||
pub async fn get_inflation_reward(
|
||||
&self,
|
||||
role: &crate::HttpRoleName,
|
||||
addresses: &[ksp_core_lib::Pubkey],
|
||||
config: std::option::Option<&crate::SolanaInflationRewardConfig>,
|
||||
) -> ksp_core_lib::Result<std::vec::Vec<std::option::Option<crate::SolanaInflationReward>>> {
|
||||
if let std::option::Option::Some(config) = config
|
||||
&& config.commitment() == std::option::Option::Some(crate::SolanaCommitment::Processed)
|
||||
{
|
||||
return std::result::Result::Err(
|
||||
ksp_core_lib::Error::new(
|
||||
crate::ERROR_CODE_INVALID_RPC_PARAMETERS,
|
||||
"getInflationReward commitment must be confirmed or finalized when explicitly provided",
|
||||
)
|
||||
.with_context("rpc_method", "getInflationReward")
|
||||
.with_context("commitment", "processed"),
|
||||
);
|
||||
}
|
||||
let address_values = addresses.iter().map(|address| serde_json::Value::String(address.to_string())).collect::<std::vec::Vec<_>>();
|
||||
let mut params = std::vec![serde_json::Value::Array(address_values)];
|
||||
if let std::option::Option::Some(config) = config
|
||||
&& !config.is_empty()
|
||||
{
|
||||
params.push((*config).to_json_value());
|
||||
}
|
||||
let value = self.execute_economics_rpc("getInflationReward", role, params).await;
|
||||
let value = match value {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
return decode_inflation_rewards("getInflationReward", value, addresses.len());
|
||||
}
|
||||
|
||||
/// Executes typed `getStakeMinimumDelegation` and preserves the contextual runtime value in lamports.
|
||||
pub async fn get_stake_minimum_delegation(
|
||||
&self,
|
||||
@@ -428,6 +459,40 @@ fn push_economics_context_config(params: &mut std::vec::Vec<serde_json::Value>,
|
||||
return;
|
||||
}
|
||||
|
||||
fn decode_inflation_rewards(
|
||||
method: &str,
|
||||
value: serde_json::Value,
|
||||
expected_count: usize,
|
||||
) -> ksp_core_lib::Result<std::vec::Vec<std::option::Option<crate::SolanaInflationReward>>> {
|
||||
let decoded = crate::decode_wire_json::<std::vec::Vec<std::option::Option<serde_json::Value>>>(method, value);
|
||||
let values = match decoded {
|
||||
std::result::Result::Ok(values) => values,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
if values.len() != expected_count {
|
||||
return std::result::Result::Err(
|
||||
ksp_core_lib::Error::new(crate::ERROR_CODE_INVALID_RESPONSE, "getInflationReward result count does not match the requested address count")
|
||||
.with_context("rpc_method", method)
|
||||
.with_context("expected_count", expected_count.to_string())
|
||||
.with_context("actual_count", values.len().to_string()),
|
||||
);
|
||||
}
|
||||
let mut rewards = std::vec::Vec::with_capacity(values.len());
|
||||
for value in values {
|
||||
match value {
|
||||
std::option::Option::Some(value) => {
|
||||
let reward = crate::SolanaInflationReward::decode_wire(method, value);
|
||||
match reward {
|
||||
std::result::Result::Ok(reward) => rewards.push(std::option::Option::Some(reward)),
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
}
|
||||
},
|
||||
std::option::Option::None => rewards.push(std::option::Option::None),
|
||||
}
|
||||
}
|
||||
return std::result::Result::Ok(rewards);
|
||||
}
|
||||
|
||||
fn decode_economics_u64_response(method: &str, value: serde_json::Value) -> ksp_core_lib::Result<crate::SolanaRpcResponse<u64>> {
|
||||
let decoded = crate::decode_wire_json::<WireEconomicsRpcResponse<u64>>(method, value);
|
||||
let wire = match decoded {
|
||||
@@ -499,7 +564,6 @@ struct WireInflationRate {
|
||||
epoch: u64,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct WireInflationReward {
|
||||
|
||||
Reference in New Issue
Block a user