v0.2.7-pre.010
This commit is contained in:
70
crates/ksp-onchain-transport-lib/src/ws_cluster.rs
Normal file
70
crates/ksp-onchain-transport-lib/src/ws_cluster.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
// file: crates/ksp-onchain-transport-lib/src/ws_cluster.rs
|
||||
// version: 1
|
||||
|
||||
/// Slot relationship reported by the standard Solana `slotNotification` WebSocket method.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct SolanaSlotNotification {
|
||||
slot: u64,
|
||||
parent: u64,
|
||||
root: u64,
|
||||
}
|
||||
|
||||
impl SolanaSlotNotification {
|
||||
/// Returns the newly processed slot.
|
||||
#[must_use]
|
||||
pub const fn slot(&self) -> u64 {
|
||||
return self.slot;
|
||||
}
|
||||
|
||||
/// Returns the parent slot reported by the validator.
|
||||
#[must_use]
|
||||
pub const fn parent(&self) -> u64 {
|
||||
return self.parent;
|
||||
}
|
||||
|
||||
/// Returns the current root slot reported alongside this slot update.
|
||||
#[must_use]
|
||||
pub const fn root(&self) -> u64 {
|
||||
return self.root;
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::WsSession {
|
||||
/// Subscribes to standard Solana slot-processing notifications through `slotSubscribe`.
|
||||
pub async fn slot_subscribe(&self) -> ksp_core_lib::Result<crate::WsSubscription<crate::SolanaSlotNotification>> {
|
||||
return self
|
||||
.subscribe_typed(crate::WsSubscriptionKind::Slot, std::vec::Vec::new(), |value| {
|
||||
return decode_slot_notification("slotSubscribe", value);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Subscribes to standard Solana root-slot notifications through `rootSubscribe`.
|
||||
pub async fn root_subscribe(&self) -> ksp_core_lib::Result<crate::WsSubscription<u64>> {
|
||||
return self
|
||||
.subscribe_typed(crate::WsSubscriptionKind::Root, std::vec::Vec::new(), |value| {
|
||||
return crate::decode_wire_json::<u64>("rootSubscribe", value);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct WireSlotNotification {
|
||||
slot: u64,
|
||||
parent: u64,
|
||||
root: u64,
|
||||
}
|
||||
|
||||
fn decode_slot_notification(method: &str, value: serde_json::Value) -> ksp_core_lib::Result<crate::SolanaSlotNotification> {
|
||||
let decoded = crate::decode_wire_json::<WireSlotNotification>(method, value);
|
||||
let wire = match decoded {
|
||||
std::result::Result::Ok(wire) => wire,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
return std::result::Result::Ok(crate::SolanaSlotNotification { slot: wire.slot, parent: wire.parent, root: wire.root });
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../unit_tests/ws_cluster.rs"]
|
||||
mod tests;
|
||||
Reference in New Issue
Block a user