soroban_cli/commands/ledger/entry/fetch/
claimable_balance.rs

1use super::args::Args;
2use crate::xdr::{
3    ClaimableBalanceId::ClaimableBalanceIdTypeV0, Hash, LedgerKey, LedgerKeyClaimableBalance,
4};
5use clap::{command, Parser};
6use hex::FromHexError;
7use soroban_spec_tools::utils::padded_hex_from_str;
8
9#[derive(Parser, Debug, Clone)]
10#[group(skip)]
11pub struct Cmd {
12    /// Claimable Balance Ids to fetch an entry for
13    #[arg(long)]
14    pub id: Vec<String>,
15
16    #[command(flatten)]
17    pub args: Args,
18}
19
20#[derive(thiserror::Error, Debug)]
21pub enum Error {
22    #[error(transparent)]
23    FromHexError(#[from] FromHexError),
24    #[error("provided hash value is invalid: {0}")]
25    InvalidHash(String),
26    #[error(transparent)]
27    Run(#[from] super::args::Error),
28}
29
30impl Cmd {
31    pub async fn run(&self) -> Result<(), Error> {
32        let mut ledger_keys = vec![];
33        self.insert_keys(&mut ledger_keys)?;
34        Ok(self.args.run(ledger_keys).await?)
35    }
36
37    fn insert_keys(&self, ledger_keys: &mut Vec<LedgerKey>) -> Result<(), Error> {
38        for x in &self.id {
39            let padded_hex = padded_hex_from_str(x, 32)?;
40            let hash_bytes: [u8; 32] = padded_hex
41                .try_into()
42                .map_err(|_| Error::InvalidHash(x.clone()))?;
43            let hash = Hash(hash_bytes);
44            let key = LedgerKey::ClaimableBalance(LedgerKeyClaimableBalance {
45                balance_id: ClaimableBalanceIdTypeV0(hash),
46            });
47            ledger_keys.push(key);
48        }
49        Ok(())
50    }
51}