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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::rpc::{GetTransactionResponse, GetTransactionResponseRaw, SimulateTransactionResponse};
use directories::ProjectDirs;
use http::Uri;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

use crate::xdr::{self, WriteXdr};

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Failed to find project directories")]
    FailedToFindProjectDirs,
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    SerdeJson(#[from] serde_json::Error),
    #[error(transparent)]
    Http(#[from] http::uri::InvalidUri),
    #[error(transparent)]
    Ulid(#[from] ulid::DecodeError),
    #[error(transparent)]
    Xdr(#[from] xdr::Error),
}

pub const XDG_DATA_HOME: &str = "XDG_DATA_HOME";

pub fn project_dir() -> Result<directories::ProjectDirs, Error> {
    std::env::var(XDG_DATA_HOME)
        .map_or_else(
            |_| ProjectDirs::from("org", "stellar", "stellar-cli"),
            |data_home| ProjectDirs::from_path(std::path::PathBuf::from(data_home)),
        )
        .ok_or(Error::FailedToFindProjectDirs)
}

#[allow(clippy::module_name_repetitions)]
pub fn data_local_dir() -> Result<std::path::PathBuf, Error> {
    Ok(project_dir()?.data_local_dir().to_path_buf())
}

pub fn actions_dir() -> Result<std::path::PathBuf, Error> {
    let dir = data_local_dir()?.join("actions");
    std::fs::create_dir_all(&dir)?;
    Ok(dir)
}

pub fn spec_dir() -> Result<std::path::PathBuf, Error> {
    let dir = data_local_dir()?.join("spec");
    std::fs::create_dir_all(&dir)?;
    Ok(dir)
}

pub fn write(action: Action, rpc_url: &Uri) -> Result<ulid::Ulid, Error> {
    let data = Data {
        action,
        rpc_url: rpc_url.to_string(),
    };
    let id = ulid::Ulid::new();
    let file = actions_dir()?.join(id.to_string()).with_extension("json");
    std::fs::write(file, serde_json::to_string(&data)?)?;
    Ok(id)
}

pub fn read(id: &ulid::Ulid) -> Result<(Action, Uri), Error> {
    let file = actions_dir()?.join(id.to_string()).with_extension("json");
    let data: Data = serde_json::from_str(&std::fs::read_to_string(file)?)?;
    Ok((data.action, http::Uri::from_str(&data.rpc_url)?))
}

pub fn write_spec(hash: &str, spec_entries: &[xdr::ScSpecEntry]) -> Result<(), Error> {
    let file = spec_dir()?.join(hash);
    tracing::trace!("writing spec to {:?}", file);
    let mut contents: Vec<u8> = Vec::new();
    for entry in spec_entries {
        contents.extend(entry.to_xdr(xdr::Limits::none())?);
    }
    std::fs::write(file, contents)?;
    Ok(())
}

pub fn read_spec(hash: &str) -> Result<Vec<xdr::ScSpecEntry>, Error> {
    let file = spec_dir()?.join(hash);
    tracing::trace!("reading spec from {:?}", file);
    Ok(soroban_spec::read::parse_raw(&std::fs::read(file)?)?)
}

pub fn list_ulids() -> Result<Vec<ulid::Ulid>, Error> {
    let dir = actions_dir()?;
    let mut list = std::fs::read_dir(dir)?
        .map(|entry| {
            entry
                .map(|e| e.file_name().into_string().unwrap())
                .map_err(Error::from)
        })
        .collect::<Result<Vec<String>, Error>>()?;
    list.sort();
    Ok(list
        .iter()
        .map(|s| ulid::Ulid::from_str(s.trim_end_matches(".json")))
        .collect::<Result<Vec<_>, _>>()?)
}

pub fn list_actions() -> Result<Vec<DatedAction>, Error> {
    list_ulids()?
        .into_iter()
        .rev()
        .map(|id| {
            let (action, uri) = read(&id)?;
            Ok(DatedAction(id, action, uri))
        })
        .collect::<Result<Vec<_>, Error>>()
}

pub struct DatedAction(ulid::Ulid, Action, Uri);

impl std::fmt::Display for DatedAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (id, a, uri) = (&self.0, &self.1, &self.2);
        let datetime = to_datatime(id).format("%b %d %H:%M");
        let status = match a {
            Action::Simulate { response } => response
                .error
                .as_ref()
                .map_or_else(|| "SUCCESS".to_string(), |_| "ERROR".to_string()),
            Action::Send { response } => response.status.to_string(),
        };
        write!(f, "{id} {} {status} {datetime} {uri} ", a.type_str(),)
    }
}

impl DatedAction {}

fn to_datatime(id: &ulid::Ulid) -> chrono::DateTime<chrono::Utc> {
    chrono::DateTime::from_timestamp_millis(id.timestamp_ms().try_into().unwrap()).unwrap()
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
struct Data {
    action: Action,
    rpc_url: String,
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum Action {
    Simulate {
        response: SimulateTransactionResponse,
    },
    Send {
        response: GetTransactionResponseRaw,
    },
}

impl Action {
    pub fn type_str(&self) -> String {
        match self {
            Action::Simulate { .. } => "Simulate",
            Action::Send { .. } => "Send    ",
        }
        .to_string()
    }
}

impl From<SimulateTransactionResponse> for Action {
    fn from(response: SimulateTransactionResponse) -> Self {
        Self::Simulate { response }
    }
}

impl TryFrom<GetTransactionResponse> for Action {
    type Error = xdr::Error;
    fn try_from(res: GetTransactionResponse) -> Result<Self, Self::Error> {
        Ok(Self::Send {
            response: GetTransactionResponseRaw {
                status: res.status,
                envelope_xdr: res.envelope.as_ref().map(to_xdr).transpose()?,
                result_xdr: res.result.as_ref().map(to_xdr).transpose()?,
                result_meta_xdr: res.result_meta.as_ref().map(to_xdr).transpose()?,
            },
        })
    }
}

fn to_xdr(data: &impl WriteXdr) -> Result<String, xdr::Error> {
    data.to_xdr_base64(xdr::Limits::none())
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_write_read() {
        let t = assert_fs::TempDir::new().unwrap();
        std::env::set_var(XDG_DATA_HOME, t.path().to_str().unwrap());
        let rpc_uri = http::uri::Uri::from_str("http://localhost:8000").unwrap();
        let sim = SimulateTransactionResponse::default();
        let original_action: Action = sim.into();

        let id = write(original_action.clone(), &rpc_uri.clone()).unwrap();
        let (action, new_rpc_uri) = read(&id).unwrap();
        assert_eq!(rpc_uri, new_rpc_uri);
        match (action, original_action) {
            (Action::Simulate { response: a }, Action::Simulate { response: b }) => {
                assert_eq!(a.cost.cpu_insns, b.cost.cpu_insns);
            }
            _ => panic!("Action mismatch"),
        }
    }
}