-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
b3282e3
commit cc48076
Showing
53 changed files
with
1,256 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use alloc::borrow::Cow; | ||
use anyhow::Result; | ||
|
||
use crate::{ | ||
core::addresscodec::{is_valid_xaddress, xaddress_to_classic_address}, | ||
models::{ledger::AccountRoot, requests::AccountInfo, results}, | ||
Err, | ||
}; | ||
|
||
use super::clients::AsyncClient; | ||
|
||
pub async fn get_next_valid_seq_number( | ||
address: Cow<'_, str>, | ||
client: &impl AsyncClient, | ||
ledger_index: Option<Cow<'_, str>>, | ||
) -> Result<u32> { | ||
let account_info = | ||
get_account_root(address, client, ledger_index.unwrap_or("current".into())).await?; | ||
Ok(account_info.sequence) | ||
} | ||
|
||
pub async fn get_account_root<'a>( | ||
address: Cow<'a, str>, | ||
client: &impl AsyncClient, | ||
ledger_index: Cow<'a, str>, | ||
) -> Result<AccountRoot<'a>> { | ||
let mut classic_address = address; | ||
if is_valid_xaddress(&classic_address) { | ||
classic_address = match xaddress_to_classic_address(&classic_address) { | ||
Ok(addr) => addr.0.into(), | ||
Err(e) => return Err!(e), | ||
}; | ||
} | ||
let account_info = client | ||
.request( | ||
AccountInfo::new( | ||
None, | ||
classic_address, | ||
None, | ||
Some(ledger_index), | ||
None, | ||
None, | ||
None, | ||
) | ||
.into(), | ||
) | ||
.await?; | ||
|
||
Ok(account_info | ||
.try_into_result::<results::AccountInfo<'_>>()? | ||
.account_data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
use super::client::Client; | ||
use crate::models::{requests::XRPLRequest, results::XRPLResponse}; | ||
use super::{client::Client, CommonFields}; | ||
use crate::models::{ | ||
requests::{ServerState, XRPLRequest}, | ||
results::{ServerState as ServerStateResult, XRPLResponse}, | ||
}; | ||
use anyhow::Result; | ||
|
||
#[allow(async_fn_in_trait)] | ||
pub trait AsyncClient: Client { | ||
async fn request<'a: 'b, 'b>(&self, request: XRPLRequest<'a>) -> Result<XRPLResponse<'b>> { | ||
self.request_impl(request).await | ||
} | ||
|
||
async fn get_common_fields(&self) -> Result<CommonFields<'_>> { | ||
let server_state = self.request(ServerState::new(None).into()).await?; | ||
let state = server_state | ||
.try_into_result::<ServerStateResult<'_>>()? | ||
.state; | ||
let common_fields = CommonFields { | ||
network_id: state.network_id, | ||
build_version: Some(state.build_version), | ||
}; | ||
|
||
Ok(common_fields) | ||
} | ||
} | ||
|
||
impl<T: Client> AsyncClient for T {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use core::{cmp::min, convert::TryInto}; | ||
|
||
use alloc::string::ToString; | ||
use anyhow::Result; | ||
|
||
use crate::models::{ | ||
amount::XRPAmount, | ||
requests::{Fee, Ledger}, | ||
results::{Drops, Fee as FeeResult, Ledger as LedgerResult}, | ||
}; | ||
|
||
use super::clients::AsyncClient; | ||
|
||
pub async fn get_latest_validated_ledger_sequence(client: &impl AsyncClient) -> Result<u32> { | ||
let ledger_response = client | ||
.request( | ||
Ledger::new( | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some("validated".into()), | ||
None, | ||
None, | ||
None, | ||
) | ||
.into(), | ||
) | ||
.await?; | ||
|
||
Ok(ledger_response | ||
.try_into_result::<LedgerResult<'_>>()? | ||
.ledger_index) | ||
} | ||
|
||
pub enum FeeType { | ||
Open, | ||
Minimum, | ||
Dynamic, | ||
} | ||
|
||
pub async fn get_fee( | ||
client: &impl AsyncClient, | ||
max_fee: Option<u32>, | ||
fee_type: Option<FeeType>, | ||
) -> Result<XRPAmount<'_>> { | ||
let fee_request = Fee::new(None); | ||
match client.request(fee_request.into()).await { | ||
Ok(response) => { | ||
let drops = response.try_into_result::<FeeResult<'_>>()?.drops; | ||
let fee = match_fee_type(fee_type, drops)?; | ||
|
||
if let Some(max_fee) = max_fee { | ||
Ok(XRPAmount::from(min(max_fee, fee).to_string())) | ||
} else { | ||
Ok(XRPAmount::from(fee.to_string())) | ||
} | ||
} | ||
Err(err) => Err(err), | ||
} | ||
} | ||
|
||
fn match_fee_type(fee_type: Option<FeeType>, drops: Drops<'_>) -> Result<u32> { | ||
match fee_type { | ||
None | Some(FeeType::Open) => Ok(drops.open_ledger_fee.try_into()?), | ||
Some(FeeType::Minimum) => Ok(drops.minimum_fee.try_into()?), | ||
Some(FeeType::Dynamic) => unimplemented!("Dynamic fee calculation not yet implemented"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
#[cfg(feature = "account-helpers")] | ||
pub mod account; | ||
#[cfg(any( | ||
feature = "websocket-std", | ||
feature = "websocket", | ||
feature = "json-rpc-std", | ||
feature = "json-rpc" | ||
))] | ||
pub mod clients; | ||
#[cfg(feature = "ledger-helpers")] | ||
pub mod ledger; | ||
#[cfg(feature = "transaction-helpers")] | ||
pub mod transaction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use core::num::ParseIntError; | ||
|
||
use alloc::borrow::Cow; | ||
use thiserror_no_std::Error; | ||
|
||
use crate::models::amount::XRPAmount; | ||
|
||
#[derive(Error, Debug, PartialEq)] | ||
pub enum XRPLTransactionException<'a> { | ||
#[error("Fee of {0:?} Drops is much higher than a typical XRP transaction fee. This may be a mistake. If intentional, please use `check_fee = false`")] | ||
FeeUnusuallyHigh(XRPAmount<'a>), | ||
#[error("Unable to parse rippled version: {0}")] | ||
ParseRippledVersionError(ParseIntError), | ||
#[error("Invalid rippled version: {0}")] | ||
InvalidRippledVersion(Cow<'a, str>), | ||
} |
Oops, something went wrong.