Ergonomic async client for the simulator backtest WebSocket API.
This wraps the simulator-api request/response protocol with helpers for common
workflows (create session, wait ready, continue/advance, close), while still allowing full
access to raw BacktestRequest / BacktestResponse when needed.
Quickstart
use std::time::Duration;
use simulator_client::{BacktestClient, Continue, CreateSession};
# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = BacktestClient::builder()
.url("ws://localhost:8900/backtest")
.api_key("local-dev-key")
.build();
let mut session = client
.create_session(
CreateSession::builder()
.start_slot(100)
.slot_count(10)
.build(),
)
.await?;
session.ensure_ready(Some(Duration::from_secs(30))).await?;
session
.continue_until_ready(
Continue::builder().advance_count(1).build(),
None,
|_| {},
)
.await?;
session.close(Some(Duration::from_secs(10))).await?;
# Ok(()) }
Lifecycle notes
BacktestClient::create_sessionsendsCreateBacktestSessionand waits forSessionCreated, buffering other responses until the session is ready.BacktestSession::closesendsCloseBacktestSessionand then closes the WebSocket.- Dropping
BacktestSessionsends a best-effort WebSocket close frame if a Tokio runtime is available. Callcloseexplicitly to ensure server-side session cleanup. connect_timeoutgoverns the initial handshake;request_timeoutapplies to send/receive operations and can be overridden per call.- For streaming workflows, use
BacktestSession::responses(consumes the session) ornext_eventto keep readiness state in sync while consuming responses.
Subscribe options
[SubscribeConfig] carries the cross-cutting options every subscribe accepts
(replayFromSlot, compression, intraSlotUpdates); merge it into a raw
subscribe's params with [SubscribeConfig::apply_to]. This crate leaves every
field it doesn't itself need unset, so those follow the server's defaults —
notably accountSubscribe / programSubscribe deliver Agave-parity one
notification per account per slot unless you set intra_slot_updates.
use simulator_client::SubscribeConfig;
let mut params = serde_json::json!([ "<pubkey>", { "encoding": "base64" } ]);
SubscribeConfig {
intra_slot_updates: true,
..SubscribeConfig::default()
}
.apply_to(&mut params);