1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::{
    fmt::Debug,
    io::{self, stdout},
};

use clap::{command, Parser, ValueEnum};
use soroban_env_host::{
    xdr::{
        ContractDataEntry, Error as XdrError, LedgerEntryData, LedgerKey, LedgerKeyContractData,
        ScVal, WriteXdr,
    },
    HostError,
};
use soroban_sdk::xdr::Limits;

use crate::{
    commands::config,
    key,
    rpc::{self, Client, FullLedgerEntries, FullLedgerEntry},
};

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
    /// Type of output to generate
    #[arg(long, value_enum, default_value("string"))]
    pub output: Output,
    #[command(flatten)]
    pub key: key::Args,
    #[command(flatten)]
    config: config::Args,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
pub enum Output {
    /// String
    String,
    /// Json
    Json,
    /// XDR
    Xdr,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("parsing key {key}: {error}")]
    CannotParseKey {
        key: String,
        error: soroban_spec_tools::Error,
    },
    #[error("parsing XDR key {key}: {error}")]
    CannotParseXdrKey { key: String, error: XdrError },
    #[error("cannot parse contract ID {contract_id}: {error}")]
    CannotParseContractId {
        contract_id: String,
        error: stellar_strkey::DecodeError,
    },
    #[error("cannot print result {result:?}: {error}")]
    CannotPrintResult {
        result: ScVal,
        error: soroban_spec_tools::Error,
    },
    #[error("cannot print result {result:?}: {error}")]
    CannotPrintJsonResult {
        result: ScVal,
        error: serde_json::Error,
    },
    #[error("cannot print as csv: {error}")]
    CannotPrintAsCsv { error: csv::Error },
    #[error("cannot print: {error}")]
    CannotPrintFlush { error: io::Error },
    #[error(transparent)]
    Config(#[from] config::Error),
    #[error("either `--key` or `--key-xdr` are required when querying a network")]
    KeyIsRequired,
    #[error(transparent)]
    Rpc(#[from] rpc::Error),
    #[error(transparent)]
    Xdr(#[from] XdrError),
    #[error(transparent)]
    // TODO: the Display impl of host errors is pretty user-unfriendly
    //       (it just calls Debug). I think we can do better than that
    Host(#[from] HostError),
    #[error("no matching contract data entries were found for the specified contract id")]
    NoContractDataEntryFoundForContractID,
    #[error(transparent)]
    Key(#[from] key::Error),
    #[error("Only contract data and code keys are allowed")]
    OnlyDataAllowed,
}

impl Cmd {
    pub async fn run(&self) -> Result<(), Error> {
        let entries = self.run_against_rpc_server().await?;
        self.output_entries(&entries)
    }

    async fn run_against_rpc_server(&self) -> Result<FullLedgerEntries, Error> {
        let network = self.config.get_network()?;
        tracing::trace!(?network);
        let network = &self.config.get_network()?;
        let client = Client::new(&network.rpc_url)?;
        let keys = self.key.parse_keys()?;
        Ok(client.get_full_ledger_entries(&keys).await?)
    }

    fn output_entries(&self, entries: &FullLedgerEntries) -> Result<(), Error> {
        if entries.entries.is_empty() {
            return Err(Error::NoContractDataEntryFoundForContractID);
        }
        tracing::trace!("{entries:#?}");
        let mut out = csv::Writer::from_writer(stdout());
        for FullLedgerEntry {
            key,
            val,
            live_until_ledger_seq,
            last_modified_ledger,
        } in &entries.entries
        {
            let (
                LedgerKey::ContractData(LedgerKeyContractData { key, .. }),
                LedgerEntryData::ContractData(ContractDataEntry { val, .. }),
            ) = (key, val)
            else {
                return Err(Error::OnlyDataAllowed);
            };
            let output = match self.output {
                Output::String => [
                    soroban_spec_tools::to_string(key).map_err(|e| Error::CannotPrintResult {
                        result: key.clone(),
                        error: e,
                    })?,
                    soroban_spec_tools::to_string(val).map_err(|e| Error::CannotPrintResult {
                        result: val.clone(),
                        error: e,
                    })?,
                    last_modified_ledger.to_string(),
                    live_until_ledger_seq.to_string(),
                ],
                Output::Json => [
                    serde_json::to_string_pretty(&key).map_err(|error| {
                        Error::CannotPrintJsonResult {
                            result: key.clone(),
                            error,
                        }
                    })?,
                    serde_json::to_string_pretty(&val).map_err(|error| {
                        Error::CannotPrintJsonResult {
                            result: val.clone(),
                            error,
                        }
                    })?,
                    serde_json::to_string_pretty(&last_modified_ledger).map_err(|error| {
                        Error::CannotPrintJsonResult {
                            result: val.clone(),
                            error,
                        }
                    })?,
                    serde_json::to_string_pretty(&live_until_ledger_seq).map_err(|error| {
                        Error::CannotPrintJsonResult {
                            result: val.clone(),
                            error,
                        }
                    })?,
                ],
                Output::Xdr => [
                    key.to_xdr_base64(Limits::none())?,
                    val.to_xdr_base64(Limits::none())?,
                    last_modified_ledger.to_xdr_base64(Limits::none())?,
                    live_until_ledger_seq.to_xdr_base64(Limits::none())?,
                ],
            };
            out.write_record(output)
                .map_err(|e| Error::CannotPrintAsCsv { error: e })?;
        }
        out.flush()
            .map_err(|e| Error::CannotPrintFlush { error: e })?;
        Ok(())
    }
}