Skip to main content

simulator_client/
rpc.rs

1use simulator_api::AccountModifications;
2
3use crate::{BacktestClientError, BacktestClientResult};
4
5/// Modify accounts on a session RPC endpoint via the custom `modifyAccounts` JSON-RPC method.
6///
7/// Returns the number of accounts modified on success.
8pub async fn modify_accounts(
9    rpc_url: &str,
10    modifications: &AccountModifications,
11) -> BacktestClientResult<usize> {
12    let count = modifications.0.len();
13    let body = serde_json::json!({
14        "jsonrpc": "2.0",
15        "id": 1,
16        "method": "modifyAccounts",
17        "params": [modifications],
18    });
19
20    let resp: serde_json::Value = reqwest::Client::new()
21        .post(rpc_url)
22        .json(&body)
23        .send()
24        .await
25        .map_err(|e| BacktestClientError::Http {
26            url: rpc_url.to_string(),
27            source: Box::new(e),
28        })?
29        .json()
30        .await
31        .map_err(|e| BacktestClientError::Http {
32            url: rpc_url.to_string(),
33            source: Box::new(e),
34        })?;
35
36    if let Some(error) = resp.get("error") {
37        return Err(BacktestClientError::Http {
38            url: rpc_url.to_string(),
39            source: format!("modifyAccounts RPC error: {error}").into(),
40        });
41    }
42
43    Ok(count)
44}