Skip to content

Commit 12ed42a

Browse files
authored
Merge branch 'dev' into utility-functions
2 parents a230d87 + 89e68e1 commit 12ed42a

File tree

89 files changed

+2027
-377
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2027
-377
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "playground"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
xrpl-rust = { path = "../.." }
8+
tokio = { version = "1", features = ["full"] }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[tokio::main]
2+
async fn main() {
3+
println!("Hello, world!");
4+
}

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Generated by Cargo
22
# will have compiled files and executables
3-
/target/
3+
**/target/
44

55
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
66
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
7-
Cargo.lock
7+
**/Cargo.lock
88

99
# These are backup files generated by rustfmt
1010
**/*.rs.bk
@@ -17,7 +17,7 @@ Cargo.lock
1717
.idea
1818

1919
# Additional
20-
src/main.rs
20+
**/src/main.rs
2121

2222
**/.DS_Store
2323

src/account/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use crate::{
1313
clients::XRPLClient,
1414
exceptions::XRPLHelperResult,
1515
},
16-
models::{ledger::objects::AccountRoot, results::account_tx::AccountTx, XRPAmount},
16+
models::{
17+
ledger::objects::account_root::AccountRoot, results::account_tx::AccountTxVersionMap,
18+
XRPAmount,
19+
},
1720
};
1821

1922
pub fn does_account_exist<C>(
@@ -44,7 +47,7 @@ where
4447

4548
pub fn get_xrp_balance<'a: 'b, 'b, C>(
4649
address: Cow<'a, str>,
47-
client: &C,
50+
client: &'a C,
4851
ledger_index: Option<Cow<'a, str>>,
4952
) -> XRPLHelperResult<XRPAmount<'b>>
5053
where
@@ -55,7 +58,7 @@ where
5558

5659
pub fn get_account_root<'a: 'b, 'b, C>(
5760
address: Cow<'a, str>,
58-
client: &C,
61+
client: &'a C,
5962
ledger_index: Cow<'a, str>,
6063
) -> XRPLHelperResult<AccountRoot<'b>>
6164
where
@@ -67,7 +70,7 @@ where
6770
pub fn get_latest_transaction<'a: 'b, 'b, C>(
6871
address: Cow<'a, str>,
6972
client: &C,
70-
) -> XRPLHelperResult<AccountTx<'b>>
73+
) -> XRPLHelperResult<AccountTxVersionMap<'b>>
7174
where
7275
C: XRPLClient,
7376
{

src/asynch/account/mod.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use alloc::borrow::Cow;
1+
use alloc::borrow::{Cow, ToOwned};
22

33
use crate::{
44
core::addresscodec::{is_valid_xaddress, xaddress_to_classic_address},
55
models::{
6-
ledger::objects::AccountRoot,
6+
ledger::objects::account_root::AccountRoot,
77
requests::{account_info::AccountInfo, account_tx::AccountTx},
88
results::{self},
99
XRPAmount,
@@ -38,7 +38,7 @@ pub async fn get_next_valid_seq_number(
3838

3939
pub async fn get_xrp_balance<'a: 'b, 'b, C>(
4040
address: Cow<'a, str>,
41-
client: &C,
41+
client: &'a C,
4242
ledger_index: Option<Cow<'a, str>>,
4343
) -> XRPLHelperResult<XRPAmount<'b>>
4444
where
@@ -54,7 +54,7 @@ where
5454

5555
pub async fn get_account_root<'a: 'b, 'b, C>(
5656
address: Cow<'a, str>,
57-
client: &C,
57+
client: &'a C,
5858
ledger_index: Cow<'a, str>,
5959
) -> XRPLHelperResult<AccountRoot<'b>>
6060
where
@@ -74,17 +74,17 @@ where
7474
None,
7575
)
7676
.into();
77-
let account_info = client.request(request).await?;
77+
let response = client.request(request).await?;
78+
let account_info = results::account_info::AccountInfoVersionMap::try_from(response)?;
79+
let account_root = account_info.get_account_root().to_owned();
7880

79-
Ok(account_info
80-
.try_into_result::<results::account_info::AccountInfo<'_>>()?
81-
.account_data)
81+
Ok(account_root)
8282
}
8383

8484
pub async fn get_latest_transaction<'a: 'b, 'b, C>(
8585
mut address: Cow<'a, str>,
8686
client: &C,
87-
) -> XRPLHelperResult<crate::models::results::account_tx::AccountTx<'b>>
87+
) -> XRPLHelperResult<crate::models::results::account_tx::AccountTxVersionMap<'b>>
8888
where
8989
C: XRPLAsyncClient,
9090
{
@@ -103,7 +103,8 @@ where
103103
Some(1),
104104
None,
105105
);
106-
let response = client.request(account_tx.into()).await?;
106+
let response: results::account_tx::AccountTxVersionMap =
107+
client.request(account_tx.into()).await?.try_into()?;
107108

108-
Ok(response.try_into_result::<results::account_tx::AccountTx<'_>>()?)
109+
Ok(response)
109110
}

src/asynch/clients/async_client.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ pub trait XRPLAsyncClient: XRPLClient {
1515

1616
async fn get_common_fields(&self) -> XRPLClientResult<CommonFields<'_>> {
1717
let server_state = self.request(ServerState::new(None).into()).await?;
18-
let state = server_state
19-
.try_into_result::<ServerStateResult<'_>>()?
20-
.state;
18+
let server_state: ServerStateResult = server_state.try_into()?;
2119
let common_fields = CommonFields {
22-
network_id: state.network_id,
23-
build_version: Some(state.build_version),
20+
network_id: server_state.state.network_id,
21+
build_version: Some(server_state.state.build_version),
2422
};
2523

2624
Ok(common_fields)

src/asynch/clients/client.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ pub trait XRPLClient {
1818

1919
fn set_request_id(&self, request: &mut XRPLRequest<'_>) {
2020
let common_fields = request.get_common_fields_mut();
21-
common_fields.id = match &common_fields.id {
22-
Some(id) => Some(id.clone()),
23-
None => {
24-
#[cfg(feature = "std")]
25-
{
26-
Some(self.get_random_id())
27-
}
28-
#[cfg(not(feature = "std"))]
29-
unimplemented!(
30-
"Random ID generation is not supported in no_std. Please provide an ID."
31-
)
21+
if common_fields.id.is_none() {
22+
#[cfg(feature = "std")]
23+
{
24+
common_fields.id = Some(self.get_random_id());
3225
}
33-
};
26+
#[cfg(not(feature = "std"))]
27+
unimplemented!(
28+
"Random ID generation is not supported in no_std. Please provide an ID."
29+
);
30+
}
3431
}
3532

3633
/// Generate a random id.

src/asynch/clients/json_rpc/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
use alloc::{string::ToString, vec};
23
use serde::Serialize;
34
use serde_json::{Map, Value};
@@ -12,20 +13,12 @@ use super::{client::XRPLClient, exceptions::XRPLClientResult};
1213
/// Renames the requests field `command` to `method` for JSON-RPC.
1314
fn request_to_json_rpc(request: &impl Serialize) -> XRPLClientResult<Value> {
1415
let mut json_rpc_request = Map::new();
15-
// let mut request = match serde_json::to_value(request) {
16-
// Ok(request) => match request.as_object().cloned() {
17-
// Some(request) => request,
18-
// None => todo!("Handle non-object requests"),
19-
// },
20-
// Err(error) => return Err!(error),
21-
// };
2216
let request_value = serde_json::to_value(request)?;
2317
let mut request = request_value
24-
.clone()
2518
.as_object()
2619
.ok_or(XRPLSerdeJsonError::UnexpectedValueType {
2720
expected: "Object".to_string(),
28-
found: request_value,
21+
found: request_value.clone(),
2922
})?
3023
.clone();
3124
if let Some(command) = request.remove("command") {

src/asynch/clients/websocket/websocket_base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ where
7979
};
8080
sender
8181
.send(message)
82-
.map_err(|e| XRPLWebSocketException::MessageChannelError(e))?;
82+
.map_err(XRPLWebSocketException::MessageChannelError)?;
8383
} else {
8484
self.messages.send(message).await;
8585
}

src/asynch/ledger/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloc::string::ToString;
44

55
use crate::models::{
66
requests::{fee::Fee, ledger::Ledger},
7-
results::{fee::Drops, fee::Fee as FeeResult, ledger::Ledger as LedgerResult},
7+
results::{self},
88
XRPAmount,
99
};
1010

@@ -30,10 +30,9 @@ pub async fn get_latest_validated_ledger_sequence(
3030
.into(),
3131
)
3232
.await?;
33+
let ledger_result: results::ledger::Ledger = ledger_response.try_into()?;
3334

34-
Ok(ledger_response
35-
.try_into_result::<LedgerResult<'_>>()?
36-
.ledger_index)
35+
Ok(ledger_result.ledger_index)
3736
}
3837

3938
pub async fn get_latest_open_ledger_sequence(
@@ -56,10 +55,9 @@ pub async fn get_latest_open_ledger_sequence(
5655
.into(),
5756
)
5857
.await?;
58+
let ledger_result: results::ledger::Ledger = ledger_response.try_into()?;
5959

60-
Ok(ledger_response
61-
.try_into_result::<LedgerResult<'_>>()?
62-
.ledger_index)
60+
Ok(ledger_result.ledger_index)
6361
}
6462

6563
pub enum FeeType {
@@ -75,7 +73,8 @@ pub async fn get_fee(
7573
) -> XRPLHelperResult<XRPAmount<'_>> {
7674
let fee_request = Fee::new(None);
7775
let response = client.request(fee_request.into()).await?;
78-
let drops = response.try_into_result::<FeeResult<'_>>()?.drops;
76+
let result: results::fee::Fee = response.try_into()?;
77+
let drops = result.drops;
7978
let fee = match_fee_type(fee_type, drops)?;
8079

8180
if let Some(max_fee) = max_fee {
@@ -85,7 +84,10 @@ pub async fn get_fee(
8584
}
8685
}
8786

88-
fn match_fee_type(fee_type: Option<FeeType>, drops: Drops<'_>) -> XRPLHelperResult<u32> {
87+
fn match_fee_type(
88+
fee_type: Option<FeeType>,
89+
drops: results::fee::Drops<'_>,
90+
) -> XRPLHelperResult<u32> {
8991
match fee_type {
9092
None | Some(FeeType::Open) => Ok(drops.open_ledger_fee.try_into()?),
9193
Some(FeeType::Minimum) => Ok(drops.minimum_fee.try_into()?),

0 commit comments

Comments
 (0)