xrpl_wasm_stdlib/core/ledger_objects/
account_root.rs

1use crate::core::ledger_objects::traits::{AccountFields, LedgerObjectCommonFields};
2use crate::core::types::account_id::AccountID;
3use crate::core::types::amount::Amount;
4use crate::core::types::keylets::account_keylet;
5use crate::host;
6use host::Error;
7
8#[derive(Debug, Clone, Copy, Eq, PartialEq)]
9#[repr(C)]
10pub struct AccountRoot {
11    pub slot_num: i32,
12}
13
14impl LedgerObjectCommonFields for AccountRoot {
15    fn get_slot_num(&self) -> i32 {
16        self.slot_num
17    }
18}
19
20impl AccountFields for AccountRoot {}
21
22pub fn get_account_balance(account_id: &AccountID) -> host::Result<Option<Amount>> {
23    // Construct the account keylet. This calls a host function, so propagate the error via `?`
24    let account_keylet = match account_keylet(account_id) {
25        host::Result::Ok(keylet) => keylet,
26        host::Result::Err(e) => return host::Result::Err(e),
27    };
28
29    // Try to cache the ledger object inside rippled
30    let slot = unsafe { host::cache_ledger_obj(account_keylet.as_ptr(), account_keylet.len(), 0) };
31    if slot < 0 {
32        return host::Result::Err(Error::from_code(slot));
33    }
34
35    // Get the balance.
36    // We use the trait-bound implementation so as not to duplicate accessor logic.
37    let account = AccountRoot { slot_num: slot };
38    account.balance()
39}