Skip to content

Commit

Permalink
Account helpers (#81)
Browse files Browse the repository at this point in the history
* add remaining account helpers

---------

Co-authored-by: LimpidCrypto <[email protected]>
  • Loading branch information
LimpidCrypto and LimpidCrypto authored Sep 1, 2024
1 parent 6cbf998 commit 3e2b3cb
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
75 changes: 71 additions & 4 deletions src/asynch/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@ use anyhow::Result;

use crate::{
core::addresscodec::{is_valid_xaddress, xaddress_to_classic_address},
models::{ledger::AccountRoot, requests::AccountInfo, results},
models::{
amount::XRPAmount,
ledger::AccountRoot,
requests::{AccountInfo, AccountTx},
results,
},
Err,
};

use super::clients::AsyncClient;

pub async fn does_account_exist<C>(
address: Cow<'_, str>,
client: &C,
ledger_index: Option<Cow<'_, str>>,
) -> Result<bool>
where
C: AsyncClient,
{
match get_account_root(address, client, ledger_index.unwrap_or("validated".into())).await {
Ok(_) => Ok(true),
Err(_) => Ok(false),
}
}

pub async fn get_next_valid_seq_number(
address: Cow<'_, str>,
client: &impl AsyncClient,
Expand All @@ -19,11 +38,30 @@ pub async fn get_next_valid_seq_number(
Ok(account_info.sequence)
}

pub async fn get_account_root<'a>(
pub async fn get_xrp_balance<'a: 'b, 'b, C>(
address: Cow<'a, str>,
client: &impl AsyncClient,
client: &C,
ledger_index: Option<Cow<'a, str>>,
) -> Result<XRPAmount<'b>>
where
C: AsyncClient,
{
let account_info =
get_account_root(address, client, ledger_index.unwrap_or("validated".into())).await?;
match account_info.balance {
Some(balance) => Ok(balance),
None => Ok(0.into()),
}
}

pub async fn get_account_root<'a: 'b, 'b, C>(
address: Cow<'a, str>,
client: &C,
ledger_index: Cow<'a, str>,
) -> Result<AccountRoot<'a>> {
) -> Result<AccountRoot<'b>>
where
C: AsyncClient,
{
let mut classic_address = address;
if is_valid_xaddress(&classic_address) {
classic_address = match xaddress_to_classic_address(&classic_address) {
Expand All @@ -50,3 +88,32 @@ pub async fn get_account_root<'a>(
.try_into_result::<results::AccountInfo<'_>>()?
.account_data)
}

pub async fn get_latest_transaction<'a: 'b, 'b, C>(
mut address: Cow<'a, str>,
client: &C,
) -> Result<results::AccountTx<'b>>
where
C: AsyncClient,
{
if is_valid_xaddress(&address) {
address = match xaddress_to_classic_address(&address) {
Ok((address, _, _)) => address.into(),
Err(e) => return Err!(e),
};
}
let account_tx = AccountTx::new(
None,
address,
None,
Some("validated".into()),
None,
None,
None,
None,
Some(1),
None,
);
let response = client.request(account_tx.into()).await?;
response.try_into_result::<results::AccountTx<'_>>()
}
35 changes: 35 additions & 0 deletions src/models/results/account_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use core::convert::TryFrom;

use alloc::{borrow::Cow, vec::Vec};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{models::results::exceptions::XRPLResultException, Err};

use super::XRPLResult;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AccountTx<'a> {
pub account: Cow<'a, str>,
pub ledger_index_min: Option<u32>,
pub ledger_index_max: Option<u32>,
pub limit: Option<u16>,
pub marker: Option<Value>,
pub transactions: Vec<Value>,
pub validated: Option<bool>,
}

impl<'a> TryFrom<XRPLResult<'a>> for AccountTx<'a> {
type Error = anyhow::Error;

fn try_from(result: XRPLResult<'a>) -> Result<Self> {
match result {
XRPLResult::AccountTx(account_tx) => Ok(account_tx),
res => Err!(XRPLResultException::UnexpectedResultType(
"AccountTx".to_string(),
res.get_name()
)),
}
}
}
26 changes: 18 additions & 8 deletions src/models/results/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

pub mod account_info;
pub mod account_tx;
pub mod exceptions;
pub mod fee;
pub mod ledger;
pub mod server_state;
pub mod submit;

pub use account_info::*;
pub use account_tx::*;
pub use fee::*;
pub use ledger::*;
pub use server_state::*;
Expand All @@ -30,26 +32,33 @@ use super::requests::XRPLRequest;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum XRPLResult<'a> {
Fee(Fee<'a>),
AccountInfo(AccountInfo<'a>),
AccountTx(AccountTx<'a>),
Fee(Fee<'a>),
Ledger(Ledger<'a>),
ServerState(ServerState<'a>),
Submit(Submit<'a>),
Other(Value),
}

impl<'a> From<Fee<'a>> for XRPLResult<'a> {
fn from(fee: Fee<'a>) -> Self {
XRPLResult::Fee(fee)
}
}

impl<'a> From<AccountInfo<'a>> for XRPLResult<'a> {
fn from(account_info: AccountInfo<'a>) -> Self {
XRPLResult::AccountInfo(account_info)
}
}

impl<'a> From<AccountTx<'a>> for XRPLResult<'a> {
fn from(account_tx: AccountTx<'a>) -> Self {
XRPLResult::AccountTx(account_tx)
}
}

impl<'a> From<Fee<'a>> for XRPLResult<'a> {
fn from(fee: Fee<'a>) -> Self {
XRPLResult::Fee(fee)
}
}

impl<'a> From<Ledger<'a>> for XRPLResult<'a> {
fn from(ledger: Ledger<'a>) -> Self {
XRPLResult::Ledger(ledger)
Expand Down Expand Up @@ -91,8 +100,9 @@ impl<'a> TryInto<Value> for XRPLResult<'a> {
impl XRPLResult<'_> {
pub(crate) fn get_name(&self) -> String {
match self {
XRPLResult::Fee(_) => "Fee".to_string(),
XRPLResult::AccountInfo(_) => "AccountInfo".to_string(),
XRPLResult::AccountTx(_) => "AccountTx".to_string(),
XRPLResult::Fee(_) => "Fee".to_string(),
XRPLResult::Ledger(_) => "Ledger".to_string(),
XRPLResult::ServerState(_) => "ServerState".to_string(),
XRPLResult::Submit(_) => "Submit".to_string(),
Expand Down

0 comments on commit 3e2b3cb

Please sign in to comment.