|
| 1 | +use crate::AccountData; |
| 2 | +use ::serde::{Deserialize, Serialize, de::DeserializeOwned}; |
| 3 | +use anyhow::{Context, Error, ensure}; |
| 4 | +use reqwest::Client; |
| 5 | +use serde_json::json; |
| 6 | +use starknet_types_core::felt::Felt; |
| 7 | +use url::Url; |
| 8 | + |
| 9 | +/// A Devnet-RPC client. |
| 10 | +#[derive(Debug, Clone)] |
| 11 | +pub struct DevnetProvider { |
| 12 | + client: Client, |
| 13 | + url: Url, |
| 14 | +} |
| 15 | + |
| 16 | +/// All Devnet-RPC methods as listed in the official docs. |
| 17 | +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] |
| 18 | +pub enum DevnetProviderMethod { |
| 19 | + #[serde(rename = "devnet_getConfig")] |
| 20 | + GetConfig, |
| 21 | + |
| 22 | + #[serde(rename = "devnet_getPredeployedAccounts")] |
| 23 | + GetPredeployedAccounts, |
| 24 | +} |
| 25 | + |
| 26 | +impl DevnetProvider { |
| 27 | + #[must_use] |
| 28 | + pub fn new(url: &str) -> Self { |
| 29 | + let url = Url::parse(url).expect("Invalid URL"); |
| 30 | + Self { |
| 31 | + client: Client::new(), |
| 32 | + url, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl DevnetProvider { |
| 38 | + async fn send_request<P, R>(&self, method: DevnetProviderMethod, params: P) -> anyhow::Result<R> |
| 39 | + where |
| 40 | + P: Serialize + Send + Sync, |
| 41 | + R: DeserializeOwned, |
| 42 | + { |
| 43 | + let res = self |
| 44 | + .client |
| 45 | + .post(self.url.clone()) |
| 46 | + .header("Content-Type", "application/json") |
| 47 | + .json(&json!({ |
| 48 | + "jsonrpc": "2.0", |
| 49 | + "method": method, |
| 50 | + "params": params, |
| 51 | + "id": 1, |
| 52 | + })) |
| 53 | + .send() |
| 54 | + .await |
| 55 | + .context("Failed to send request")? |
| 56 | + .json::<serde_json::Value>() |
| 57 | + .await |
| 58 | + .context("Failed to parse response")?; |
| 59 | + |
| 60 | + if let Some(error) = res.get("error") { |
| 61 | + Err(anyhow::anyhow!(error.to_string())) |
| 62 | + } else if let Some(result) = res.get("result") { |
| 63 | + serde_json::from_value(result.clone()).map_err(anyhow::Error::from) |
| 64 | + } else { |
| 65 | + panic!("Malformed RPC response: {res}") |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Fetches the current Devnet configuration. |
| 70 | + pub async fn get_config(&self) -> Result<Config, Error> { |
| 71 | + self.send_request(DevnetProviderMethod::GetConfig, json!({})) |
| 72 | + .await |
| 73 | + } |
| 74 | + |
| 75 | + /// Fetches the list of predeployed accounts in Devnet. |
| 76 | + pub async fn get_predeployed_accounts(&self) -> Result<Vec<PredeployedAccount>, Error> { |
| 77 | + self.send_request(DevnetProviderMethod::GetPredeployedAccounts, json!({})) |
| 78 | + .await |
| 79 | + } |
| 80 | + |
| 81 | + /// Ensures the Devnet instance is alive. |
| 82 | + pub async fn ensure_alive(&self) -> Result<(), Error> { |
| 83 | + let is_alive = self |
| 84 | + .client |
| 85 | + .get(format!( |
| 86 | + "{}/is_alive", |
| 87 | + self.url.to_string().replace("/rpc", "") |
| 88 | + )) |
| 89 | + .send() |
| 90 | + .await |
| 91 | + .map(|res| res.status().is_success()) |
| 92 | + .unwrap_or(false); |
| 93 | + |
| 94 | + ensure!( |
| 95 | + is_alive, |
| 96 | + "Node at {} is not responding to the Devnet health check (GET `/is_alive`). It may not be a Devnet instance or it may be down.", |
| 97 | + self.url |
| 98 | + ); |
| 99 | + Ok(()) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[derive(Debug, Serialize, Deserialize)] |
| 104 | +pub struct Config { |
| 105 | + pub seed: u32, |
| 106 | + pub account_contract_class_hash: Felt, |
| 107 | + pub total_accounts: u8, |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(Debug, Serialize, Deserialize)] |
| 111 | +pub struct PredeployedAccount { |
| 112 | + pub address: Felt, |
| 113 | + pub private_key: Felt, |
| 114 | + pub public_key: Felt, |
| 115 | +} |
| 116 | + |
| 117 | +impl From<&PredeployedAccount> for AccountData { |
| 118 | + fn from(predeployed_account: &PredeployedAccount) -> Self { |
| 119 | + Self { |
| 120 | + address: Some(predeployed_account.address), |
| 121 | + private_key: predeployed_account.private_key, |
| 122 | + public_key: predeployed_account.public_key, |
| 123 | + class_hash: None, |
| 124 | + salt: None, |
| 125 | + deployed: None, |
| 126 | + legacy: None, |
| 127 | + account_type: None, |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments