soroban_cli/commands/ledger/entry/fetch/
offer.rs1use std::array::TryFromSliceError;
2use std::fmt::Debug;
3
4use super::args::Args;
5use crate::{
6 commands::config::{self, locator},
7 xdr::{LedgerKey, LedgerKeyOffer, MuxedAccount},
8};
9use clap::{command, Parser};
10
11#[derive(Parser, Debug, Clone)]
12#[group(skip)]
13pub struct Cmd {
14 #[command(flatten)]
15 pub args: Args,
16
17 #[arg(long)]
19 pub account: String,
20
21 #[arg(long, required = true)]
23 pub offer: Vec<i64>,
24
25 #[arg(long)]
27 pub hd_path: Option<usize>,
28}
29
30#[derive(thiserror::Error, Debug)]
31pub enum Error {
32 #[error(transparent)]
33 Config(#[from] config::key::Error),
34 #[error("provided asset is invalid: {0}")]
35 InvalidAsset(String),
36 #[error("provided data name is invalid: {0}")]
37 InvalidDataName(String),
38 #[error(transparent)]
39 Locator(#[from] locator::Error),
40 #[error(transparent)]
41 TryFromSliceError(#[from] TryFromSliceError),
42 #[error(transparent)]
43 Run(#[from] super::args::Error),
44}
45
46impl Cmd {
47 pub async fn run(&self) -> Result<(), Error> {
48 let mut ledger_keys = vec![];
49 self.insert_offer_keys(&mut ledger_keys)?;
50
51 Ok(self.args.run(ledger_keys).await?)
52 }
53
54 fn insert_offer_keys(&self, ledger_keys: &mut Vec<LedgerKey>) -> Result<(), Error> {
55 let acc = self.muxed_account(&self.account)?;
56 for offer in &self.offer {
57 let key = LedgerKey::Offer(LedgerKeyOffer {
58 seller_id: acc.clone().account_id(),
59 offer_id: *offer,
60 });
61 ledger_keys.push(key);
62 }
63
64 Ok(())
65 }
66
67 fn muxed_account(&self, account: &str) -> Result<MuxedAccount, Error> {
68 Ok(self
69 .args
70 .locator
71 .read_key(account)?
72 .muxed_account(self.hd_path)?)
73 }
74}