Skip to content

Commit

Permalink
Migrate all client calls to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Apr 5, 2024
1 parent 44518a8 commit aeb2d4a
Showing 1 changed file with 51 additions and 11 deletions.
62 changes: 51 additions & 11 deletions cmd/crates/stellar-rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use http::{uri::Authority, Uri};
use itertools::Itertools;
use jsonrpsee_core::params::ObjectParams;
use jsonrpsee_core::{self, client::ClientT, rpc_params};
use jsonrpsee_core::{self, client::ClientT};
use jsonrpsee_http_client::{HeaderMap, HttpClient, HttpClientBuilder};
use serde_aux::prelude::{
deserialize_default_from_null, deserialize_number_from_string,
Expand Down Expand Up @@ -279,6 +279,24 @@ pub struct SimulateHostFunctionResult {
pub xdr: xdr::ScVal,
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, PartialEq)]
pub enum LedgerEntryChangeType {
#[serde(rename = "created")]
Created,
#[serde(rename = "deleted")]
Deleted,
#[serde(rename = "updated")]
Updated,
}
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub struct LedgerEntryChange {
#[serde(rename = "type")]
pub type_: LedgerEntryChangeType,
pub key: String,
pub before: Option<String>,
pub after: Option<String>,
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Default, Clone)]
pub struct SimulateTransactionResponse {
#[serde(
Expand All @@ -305,6 +323,12 @@ pub struct SimulateTransactionResponse {
default
)]
pub restore_preamble: Option<RestorePreamble>,
#[serde(
rename = "stateChanges",
skip_serializing_if = "Option::is_none",
default
)]
pub state_changes: Option<Vec<LedgerEntryChange>>,
#[serde(rename = "latestLedger")]
pub latest_ledger: u32,
#[serde(skip_serializing_if = "Option::is_none", default)]
Expand Down Expand Up @@ -637,7 +661,7 @@ impl Client {
/// # Errors
pub async fn get_network(&self) -> Result<GetNetworkResponse, Error> {
tracing::trace!("Getting network");
Ok(self.client()?.request("getNetwork", rpc_params![]).await?)
Ok(self.client()?.request("getNetwork", ObjectParams::new()).await?)
}

///
Expand All @@ -646,7 +670,7 @@ impl Client {
tracing::trace!("Getting latest ledger");
Ok(self
.client()?
.request("getLatestLedger", rpc_params![])
.request("getLatestLedger", ObjectParams::new())
.await?)
}

Expand Down Expand Up @@ -691,6 +715,8 @@ soroban config identity fund {address} --helper-url <url>"#
) -> Result<GetTransactionResponse, Error> {
let client = self.client()?;
tracing::trace!("Sending:\n{tx:#?}");
let mut oparams = ObjectParams::new();
oparams.insert("transaction", tx.to_xdr_base64(Limits::none())?)?;
let SendTransactionResponse {
hash,
error_result_xdr,
Expand All @@ -699,7 +725,7 @@ soroban config identity fund {address} --helper-url <url>"#
} = client
.request(
"sendTransaction",
rpc_params![tx.to_xdr_base64(Limits::none())?],
oparams,
)
.await
.map_err(|err| {
Expand Down Expand Up @@ -761,11 +787,11 @@ soroban config identity fund {address} --helper-url <url>"#
) -> Result<SimulateTransactionResponse, Error> {
tracing::trace!("Simulating:\n{tx:#?}");
let base64_tx = tx.to_xdr_base64(Limits::none())?;
let mut builder = ObjectParams::new();
builder.insert("transaction", base64_tx)?;
let mut oparams = ObjectParams::new();
oparams.insert("transaction", base64_tx)?;
let response: SimulateTransactionResponse = self
.client()?
.request("simulateTransaction", builder)
.request("simulateTransaction", oparams)
.await?;
tracing::trace!("Simulation response:\n {response:#?}");
match response.error {
Expand Down Expand Up @@ -835,9 +861,11 @@ soroban config identity fund {address} --helper-url <url>"#
///
/// # Errors
pub async fn get_transaction(&self, tx_id: &str) -> Result<GetTransactionResponseRaw, Error> {
let mut oparams = ObjectParams::new();
oparams.insert("hash", tx_id)?;
Ok(self
.client()?
.request("getTransaction", rpc_params![tx_id])
.request("getTransaction", oparams)
.await?)
}

Expand All @@ -855,9 +883,11 @@ soroban config identity fund {address} --helper-url <url>"#
}
base64_keys.push(k.to_xdr_base64(Limits::none())?);
}
let mut oparams = ObjectParams::new();
oparams.insert("keys", base64_keys)?;
Ok(self
.client()?
.request("getLedgerEntries", rpc_params![base64_keys])
.request("getLedgerEntries", oparams)
.await?)
}

Expand Down Expand Up @@ -1070,10 +1100,20 @@ mod tests {
"minResourceFee": "100000000",
"cost": { "cpuInsns": "1000", "memBytes": "1000" },
"transactionData": "",
"latestLedger": 1234
}"#;
"latestLedger": 1234,
"stateChanges": [{
"type": "created",
"key": "AAAAAAAAAABuaCbVXZ2DlXWarV6UxwbW3GNJgpn3ASChIFp5bxSIWg==",
"before": null,
"after": "AAAAZAAAAAAAAAAAbmgm1V2dg5V1mq1elMcG1txjSYKZ9wEgoSBaeW8UiFoAAAAAAAAAZAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
}]
}"#;

let resp: SimulateTransactionResponse = serde_json::from_str(s).unwrap();
assert_eq!(
resp.state_changes.unwrap()[0].type_,
LedgerEntryChangeType::Created
);
assert_eq!(resp.min_resource_fee, 100_000_000);
}

Expand Down

0 comments on commit aeb2d4a

Please sign in to comment.