simulator-client 0.7.1

Async WebSocket client for the Solana simulator backtest API
Documentation
use simulator_api::AccountModifications;

use crate::{BacktestClientError, BacktestClientResult};

/// Modify accounts on a session RPC endpoint via the custom `modifyAccounts` JSON-RPC method.
///
/// Returns the number of accounts modified on success.
pub async fn modify_accounts(
    rpc_url: &str,
    modifications: &AccountModifications,
) -> BacktestClientResult<usize> {
    let count = modifications.0.len();
    let body = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "modifyAccounts",
        "params": [modifications],
    });

    let resp: serde_json::Value = reqwest::Client::new()
        .post(rpc_url)
        .json(&body)
        .send()
        .await
        .map_err(|e| BacktestClientError::Http {
            url: rpc_url.to_string(),
            source: Box::new(e),
        })?
        .json()
        .await
        .map_err(|e| BacktestClientError::Http {
            url: rpc_url.to_string(),
            source: Box::new(e),
        })?;

    if let Some(error) = resp.get("error") {
        return Err(BacktestClientError::Http {
            url: rpc_url.to_string(),
            source: format!("modifyAccounts RPC error: {error}").into(),
        });
    }

    Ok(count)
}