forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.rs
More file actions
356 lines (304 loc) · 11.2 KB
/
Copy pathstellar.rs
File metadata and controls
356 lines (304 loc) · 11.2 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use crate::{
error::{AppError, AppResult},
models::payment::{Asset, PathHop},
};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Thin async wrapper around the Stellar Horizon REST API.
#[derive(Debug, Clone)]
pub struct StellarService {
client: Client,
horizon_url: String,
}
// ─── Horizon response types ───────────────────────────────────────────────────
#[derive(Debug, Deserialize, Serialize)]
pub struct HorizonAccount {
pub id: String,
pub account_id: String,
pub sequence: String,
pub subentry_count: u32,
pub balances: Vec<HorizonBalance>,
pub thresholds: HorizonThresholds,
pub flags: HorizonFlags,
pub last_modified_ledger: u64,
pub last_modified_time: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct HorizonBalance {
pub balance: String,
pub asset_type: String,
#[serde(default)]
pub asset_code: String,
#[serde(default)]
pub asset_issuer: String,
pub limit: Option<String>,
pub buying_liabilities: Option<String>,
pub selling_liabilities: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct HorizonThresholds {
pub low_threshold: u32,
pub med_threshold: u32,
pub high_threshold: u32,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct HorizonFlags {
pub auth_required: bool,
pub auth_revocable: bool,
pub auth_immutable: bool,
}
#[derive(Debug, Deserialize)]
struct PathPaymentResponse {
#[serde(rename = "_embedded")]
embedded: PathPaymentEmbedded,
}
#[derive(Debug, Deserialize)]
struct PathPaymentEmbedded {
records: Vec<PathRecord>,
}
#[derive(Debug, Deserialize)]
pub struct PathRecord {
pub source_asset_type: String,
#[serde(default)]
pub source_asset_code: String,
#[serde(default)]
pub source_asset_issuer: String,
pub source_amount: String,
pub destination_asset_type: String,
#[serde(default)]
pub destination_asset_code: String,
#[serde(default)]
pub destination_asset_issuer: String,
pub destination_amount: String,
pub path: Vec<HorizonPathHop>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct HorizonPathHop {
pub asset_type: String,
#[serde(default)]
pub asset_code: String,
#[serde(default)]
pub asset_issuer: String,
}
#[derive(Debug, Deserialize)]
pub struct SubmitTransactionResponse {
pub hash: String,
pub ledger: Option<u64>,
pub fee_charged: Option<String>,
pub successful: bool,
}
#[derive(Debug, Deserialize)]
struct HorizonErrorResponse {
title: String,
detail: Option<String>,
extras: Option<Value>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct HorizonTransaction {
pub id: String,
pub hash: String,
pub ledger: u64,
pub created_at: String,
pub source_account: String,
pub fee_charged: String,
pub successful: bool,
}
#[derive(Debug, Deserialize)]
struct FeeStatsResponse {
last_ledger_base_fee: String,
fee_charged: FeeCharged,
}
#[derive(Debug, Deserialize)]
struct FeeCharged {
p50: String,
p90: String,
}
impl StellarService {
pub fn new(horizon_url: impl Into<String>) -> Self {
let client = Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()
.expect("Failed to build reqwest client");
Self {
client,
horizon_url: horizon_url.into().trim_end_matches('/').to_string(),
}
}
// ── Account ──────────────────────────────────────────────────────────────
/// Fetch a Stellar account from Horizon.
pub async fn get_account(&self, address: &str) -> AppResult<HorizonAccount> {
let url = format!("{}/accounts/{}", self.horizon_url, address);
let resp = self
.client
.get(&url)
.send()
.await
.map_err(AppError::HttpClient)?;
if resp.status() == reqwest::StatusCode::NOT_FOUND {
return Err(AppError::NotFound(format!(
"Stellar account '{address}'"
)));
}
if !resp.status().is_success() {
return Err(self.parse_horizon_error(resp).await);
}
resp.json::<HorizonAccount>()
.await
.map_err(AppError::HttpClient)
}
/// Return the balance list for a Stellar account.
pub async fn get_balances(&self, address: &str) -> AppResult<Vec<HorizonBalance>> {
let account = self.get_account(address).await?;
Ok(account.balances)
}
// ── Path payments ────────────────────────────────────────────────────────
/// Call Horizon `/paths/strict-send` to find all available payment paths.
pub async fn get_path_payment_paths(
&self,
from_asset: &Asset,
to_asset: &Asset,
send_amount: &str,
destination: &str,
) -> AppResult<Vec<PathRecord>> {
let url = format!("{}/paths/strict-send", self.horizon_url);
// Build query string manually to handle native vs issued assets.
let mut params: Vec<(&str, String)> = vec![
("source_amount", send_amount.to_string()),
("destination_account", destination.to_string()),
];
if from_asset.is_native() {
params.push(("source_asset_type", "native".to_string()));
} else {
params.push(("source_asset_type", "credit_alphanum4".to_string()));
params.push(("source_asset_code", from_asset.code.clone()));
if let Some(issuer) = &from_asset.issuer {
params.push(("source_asset_issuer", issuer.clone()));
}
}
if to_asset.is_native() {
params.push(("destination_asset_type", "native".to_string()));
} else {
params.push(("destination_asset_type", "credit_alphanum4".to_string()));
params.push(("destination_asset_code", to_asset.code.clone()));
if let Some(issuer) = &to_asset.issuer {
params.push(("destination_asset_issuer", issuer.clone()));
}
}
let resp = self
.client
.get(&url)
.query(¶ms)
.send()
.await
.map_err(AppError::HttpClient)?;
if !resp.status().is_success() {
return Err(self.parse_horizon_error(resp).await);
}
let path_resp = resp
.json::<PathPaymentResponse>()
.await
.map_err(AppError::HttpClient)?;
Ok(path_resp.embedded.records)
}
// ── Transaction submission ────────────────────────────────────────────────
/// Submit a signed TransactionEnvelope XDR to Horizon.
pub async fn submit_transaction(
&self,
signed_xdr: &str,
) -> AppResult<SubmitTransactionResponse> {
let url = format!("{}/transactions", self.horizon_url);
let resp = self
.client
.post(&url)
.form(&[("tx", signed_xdr)])
.send()
.await
.map_err(AppError::HttpClient)?;
if !resp.status().is_success() {
return Err(self.parse_horizon_error(resp).await);
}
resp.json::<SubmitTransactionResponse>()
.await
.map_err(AppError::HttpClient)
}
// ── Fee estimation ────────────────────────────────────────────────────────
/// Estimate the current network fee in XLM (using p90 of recent fee stats).
pub async fn estimate_fee(&self) -> AppResult<String> {
let url = format!("{}/fee_stats", self.horizon_url);
let resp = self
.client
.get(&url)
.send()
.await
.map_err(AppError::HttpClient)?;
if !resp.status().is_success() {
// Fall back to base fee if fee_stats is unavailable.
return Ok("0.00001".to_string());
}
let stats: FeeStatsResponse = resp.json().await.map_err(AppError::HttpClient)?;
// Convert stroops to XLM (1 XLM = 10_000_000 stroops).
let fee_stroops: f64 = stats.fee_charged.p90.parse().unwrap_or(100.0);
let fee_xlm = fee_stroops / 10_000_000.0;
Ok(format!("{:.7}", fee_xlm))
}
// ── Transaction fetching ──────────────────────────────────────────────────
/// Fetch a single transaction by hash from Horizon.
pub async fn fetch_transaction(&self, hash: &str) -> AppResult<HorizonTransaction> {
let url = format!("{}/transactions/{}", self.horizon_url, hash);
let resp = self
.client
.get(&url)
.send()
.await
.map_err(AppError::HttpClient)?;
if resp.status() == reqwest::StatusCode::NOT_FOUND {
return Err(AppError::NotFound(format!(
"Stellar transaction '{hash}'"
)));
}
if !resp.status().is_success() {
return Err(self.parse_horizon_error(resp).await);
}
resp.json::<HorizonTransaction>()
.await
.map_err(AppError::HttpClient)
}
// ── Helpers ───────────────────────────────────────────────────────────────
async fn parse_horizon_error(&self, resp: reqwest::Response) -> AppError {
let status = resp.status();
match resp.json::<HorizonErrorResponse>().await {
Ok(e) => {
let detail = e.detail.unwrap_or_else(|| e.title.clone());
// If extras contain result_codes, surface them.
let msg = if let Some(extras) = e.extras {
if let Some(codes) = extras.get("result_codes") {
format!("{detail} — result_codes: {codes}")
} else {
detail
}
} else {
detail
};
AppError::HorizonError(msg)
}
Err(_) => AppError::HorizonError(format!("HTTP {status}")),
}
}
/// Convert a `HorizonPathHop` to our model `PathHop`.
pub fn convert_path_hop(hop: &HorizonPathHop) -> PathHop {
PathHop {
asset_code: if hop.asset_type == "native" {
"XLM".to_string()
} else {
hop.asset_code.clone()
},
asset_issuer: if hop.asset_issuer.is_empty() {
None
} else {
Some(hop.asset_issuer.clone())
},
asset_type: hop.asset_type.clone(),
}
}
}