forked from SO4-Markets/so4-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar_rpc.rs
More file actions
371 lines (317 loc) · 12 KB
/
Copy pathstellar_rpc.rs
File metadata and controls
371 lines (317 loc) · 12 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Clone)]
pub enum RpcError {
NetworkError(String),
HttpError { status: u16, body: String },
JsonError(String),
RpcFault { code: i64, message: String },
BalanceBelowMinimum { balance_xlm: f64, min_xlm: f64 },
}
impl Eq for RpcError {}
/// Network errors and server-side/rate-limit HTTP statuses are transient and
/// worth retrying; parse errors, RPC faults, and balance-precondition
/// failures are not going to change on a bare retry, so they fail fast.
impl crate::retry::Retryable for RpcError {
fn is_retryable(&self) -> bool {
match self {
RpcError::NetworkError(_) => true,
RpcError::HttpError { status, .. } => *status >= 500 || *status == 429,
RpcError::JsonError(_) => false,
RpcError::RpcFault { .. } => false,
RpcError::BalanceBelowMinimum { .. } => false,
}
}
}
impl std::fmt::Display for RpcError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RpcError::NetworkError(msg) => write!(f, "network error: {msg}"),
RpcError::HttpError { status, body } => {
if body.is_empty() {
write!(f, "HTTP {status}")
} else {
write!(f, "HTTP {status}: {body}")
}
}
RpcError::JsonError(msg) => write!(f, "JSON parse error: {msg}"),
RpcError::RpcFault { code, message } => {
write!(f, "RPC fault {code}: {message}")
}
RpcError::BalanceBelowMinimum {
balance_xlm,
min_xlm,
} => {
write!(
f,
"balance {balance_xlm} XLM is below minimum {min_xlm} XLM"
)
}
}
}
}
// ── JSON-RPC wire types ──────────────────────────────────────────────────────
#[derive(Serialize)]
struct JsonRpcRequest<'a> {
jsonrpc: &'a str,
id: u32,
method: &'a str,
params: serde_json::Value,
}
#[derive(Deserialize)]
struct JsonRpcResponse<T> {
result: Option<T>,
error: Option<JsonRpcFault>,
}
#[derive(Deserialize)]
struct JsonRpcFault {
code: i64,
message: String,
}
// ── getLatestLedger ──────────────────────────────────────────────────────────
#[derive(Debug, Clone, Deserialize)]
struct GetLatestLedgerResult {
sequence: u32,
#[allow(dead_code)]
id: String,
#[serde(rename = "protocolVersion")]
#[allow(dead_code)]
protocol_version: serde_json::Value,
}
/// Parse the raw JSON body returned by a `getLatestLedger` RPC call.
///
/// Kept separate from the HTTP layer so it can be unit-tested without
/// mocking the network.
pub fn parse_latest_ledger_response(body: &str) -> Result<u32, RpcError> {
let resp: JsonRpcResponse<GetLatestLedgerResult> =
serde_json::from_str(body).map_err(|e| RpcError::JsonError(e.to_string()))?;
if let Some(fault) = resp.error {
return Err(RpcError::RpcFault {
code: fault.code,
message: fault.message,
});
}
resp.result
.ok_or_else(|| RpcError::JsonError("missing 'result' field".to_string()))
.map(|r| r.sequence)
}
/// Call `getLatestLedger` on the Stellar RPC endpoint and return the current
/// ledger sequence number.
///
/// **Caching note:** call this once per price-update cycle and pass the
/// returned value to any downstream function that needs `ledger_seq`. This
/// avoids redundant round-trips within a single scheduled invocation.
pub async fn get_latest_ledger_sequence(rpc_url: &str) -> Result<u32, RpcError> {
let payload = serde_json::to_string(&JsonRpcRequest {
jsonrpc: "2.0",
id: 1,
method: "getLatestLedger",
params: serde_json::Value::Object(serde_json::Map::new()),
})
.map_err(|e| RpcError::JsonError(e.to_string()))?;
let body = rpc_post(rpc_url, payload).await?;
parse_latest_ledger_response(&body)
}
/// Low-level helper: POST a JSON string to the RPC URL, return the response body.
pub(crate) async fn rpc_post(rpc_url: &str, payload: String) -> Result<String, RpcError> {
let response = crate::http::client()
.post(rpc_url)
.header("Content-Type", "application/json")
.body(payload)
.send()
.await
.map_err(|e| RpcError::NetworkError(e.to_string()))?;
let status = response.status().as_u16();
let body = response
.text()
.await
.map_err(|e| RpcError::NetworkError(e.to_string()))?;
if status != 200 {
return Err(RpcError::HttpError {
status,
body: crate::http::truncate_error_body(&body),
});
}
Ok(body)
}
// ── Account balance (Horizon REST) ──────────────────────────────────────────
#[derive(Debug, Deserialize)]
struct HorizonBalanceEntry {
asset_type: String,
balance: String,
}
#[derive(Debug, Deserialize)]
struct HorizonAccountResponse {
balances: Vec<HorizonBalanceEntry>,
}
/// Parse the JSON body returned by `GET /accounts/{id}` on a Horizon server.
///
/// Returns the native (XLM) balance in stroops (1 XLM = 10_000_000 stroops).
pub fn parse_account_balance_response(body: &str) -> Result<i64, RpcError> {
let resp: HorizonAccountResponse =
serde_json::from_str(body).map_err(|e| RpcError::JsonError(e.to_string()))?;
let native = resp
.balances
.iter()
.find(|b| b.asset_type == "native")
.ok_or_else(|| RpcError::JsonError("no native balance entry".to_string()))?;
// Horizon returns XLM as a decimal string "100.0000000" (7 decimal places).
let xlm: f64 = native
.balance
.parse()
.map_err(|_| RpcError::JsonError(format!("unparseable balance: {}", native.balance)))?;
Ok((xlm * 10_000_000.0) as i64)
}
/// Fetch the XLM balance for `account_id` from the Horizon server at
/// `horizon_url`. Returns the balance in stroops.
pub async fn get_account_balance_stroops(
horizon_url: &str,
account_id: &str,
) -> Result<i64, RpcError> {
let url = format!("{horizon_url}/accounts/{account_id}");
let response = crate::http::client()
.get(&url)
.send()
.await
.map_err(|e| RpcError::NetworkError(e.to_string()))?;
let status = response.status().as_u16();
let body = response
.text()
.await
.map_err(|e| RpcError::NetworkError(e.to_string()))?;
if status != 200 {
return Err(RpcError::HttpError {
status,
body: crate::http::truncate_error_body(&body),
});
}
parse_account_balance_response(&body)
}
#[cfg(test)]
mod tests {
use super::*;
/// Verifies that a valid `getLatestLedger` RPC response is parsed correctly
/// and the sequence number is extracted. Closes #409.
#[test]
fn parse_valid_latest_ledger_response() {
let body = r#"{
"jsonrpc":"2.0","id":1,
"result":{"id":"abc123","sequence":12345,"protocolVersion":22}
}"#;
assert_eq!(parse_latest_ledger_response(body).unwrap(), 12345u32);
}
/// Verifies that an RPC fault in the `getLatestLedger` response is
/// propagated as `RpcError::RpcFault` with the correct code and message.
/// Closes #410.
#[test]
fn parse_rpc_fault_response() {
let body = r#"{
"jsonrpc":"2.0","id":1,
"error":{"code":-32000,"message":"start height out of range"}
}"#;
let err = parse_latest_ledger_response(body).unwrap_err();
assert_eq!(
err,
RpcError::RpcFault {
code: -32000,
message: "start height out of range".to_string(),
}
);
}
#[test]
fn parse_malformed_json_returns_error() {
let err = parse_latest_ledger_response("not json").unwrap_err();
assert!(matches!(err, RpcError::JsonError(_)));
}
#[test]
fn parse_missing_result_field() {
let body = r#"{"jsonrpc":"2.0","id":1}"#;
let err = parse_latest_ledger_response(body).unwrap_err();
assert!(matches!(err, RpcError::JsonError(_)));
}
// ── account balance ──────────────────────────────────────────────────────
#[test]
fn parse_account_balance_native_xlm() {
let body = r#"{
"id": "GABC",
"balances": [
{"asset_type":"credit_alphanum4","asset_code":"USDC","balance":"50.0000000"},
{"asset_type":"native","balance":"100.5000000"}
]
}"#;
let stroops = parse_account_balance_response(body).unwrap();
assert_eq!(stroops, 1_005_000_000);
}
#[test]
fn parse_account_balance_low_balance() {
let body = r#"{
"id": "GABC",
"balances": [{"asset_type":"native","balance":"0.5000000"}]
}"#;
let stroops = parse_account_balance_response(body).unwrap();
assert_eq!(stroops, 5_000_000);
}
#[test]
fn parse_account_balance_no_native_entry() {
let body = r#"{
"id": "GABC",
"balances": [{"asset_type":"credit_alphanum4","asset_code":"USDC","balance":"10.0"}]
}"#;
let err = parse_account_balance_response(body).unwrap_err();
assert!(matches!(err, RpcError::JsonError(_)));
}
#[test]
fn parse_account_balance_malformed_json() {
let err = parse_account_balance_response("not json").unwrap_err();
assert!(matches!(err, RpcError::JsonError(_)));
}
// ── get_account_balance_stroops — HTTP-level tests (#404) ────────────────
#[tokio::test]
async fn get_account_balance_stroops_200_returns_stroops() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
let body = r#"{
"id": "GABC",
"balances": [
{"asset_type":"native","balance":"50.0000000"}
]
}"#;
Mock::given(method("GET"))
.and(path("/accounts/GABC"))
.respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/json"))
.mount(&server)
.await;
let result = get_account_balance_stroops(&server.uri(), "GABC").await;
assert_eq!(result.unwrap(), 500_000_000); // 50 XLM in stroops
}
#[tokio::test]
async fn get_account_balance_stroops_404_returns_http_error() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/accounts/GNOT_FOUND"))
.respond_with(ResponseTemplate::new(404))
.mount(&server)
.await;
let err = get_account_balance_stroops(&server.uri(), "GNOT_FOUND")
.await
.unwrap_err();
assert!(matches!(err, RpcError::HttpError { status: 404, .. }));
}
#[tokio::test]
async fn get_account_balance_stroops_500_returns_http_error() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/accounts/GABC"))
.respond_with(ResponseTemplate::new(500))
.mount(&server)
.await;
let err = get_account_balance_stroops(&server.uri(), "GABC")
.await
.unwrap_err();
assert!(matches!(err, RpcError::HttpError { status: 500, .. }));
}
}