forked from StellarRouter/StellarRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.rs
More file actions
1326 lines (1185 loc) · 46.4 KB
/
Copy pathrpc.rs
File metadata and controls
1326 lines (1185 loc) · 46.4 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Soroban RPC client helpers.
//!
//! Wraps the JSON-RPC calls needed to read on-chain contract state.
//! We use raw `reqwest` + `serde_json` rather than the `stellar-rpc-client`
//! crate so that this binary has no dependency on the Soroban SDK (which
//! requires `wasm32` toolchain features and complicates native builds).
use anyhow::{anyhow, Context, Result};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::time::Duration;
use tracing::{debug, warn};
/// Maximum number of pages to fetch in a single `get_events` call.
/// Prevents unbounded loops if the RPC server misbehaves.
const GET_EVENTS_MAX_PAGES: usize = 20;
// ── JSON-RPC request / response types ────────────────────────────────────────
#[derive(Debug, Serialize)]
struct RpcRequest<'a> {
jsonrpc: &'a str,
id: u64,
method: &'a str,
params: Value,
}
#[derive(Debug, Deserialize)]
struct RpcResponse {
result: Option<Value>,
error: Option<RpcError>,
}
#[derive(Debug, Deserialize)]
struct RpcError {
code: i64,
message: String,
}
// ── Decoded ledger entry types ────────────────────────────────────────────────
/// A single ledger entry returned by `getLedgerEntries`.
#[derive(Debug, Deserialize, Clone)]
pub struct LedgerEntry {
/// Base64-encoded XDR of the entry key.
pub key: String,
/// Base64-encoded XDR of the entry value.
pub xdr: String,
}
/// Response from `getLedgerEntries`.
#[derive(Debug, Deserialize)]
struct GetLedgerEntriesResult {
entries: Option<Vec<LedgerEntry>>,
}
/// A single event returned by `getEvents`.
#[derive(Debug, Deserialize, Clone)]
pub struct ContractEvent {
/// The contract that emitted the event.
#[serde(rename = "contractId")]
#[allow(dead_code)]
pub contract_id: String,
/// Ledger sequence number in which this event was emitted.
#[serde(default)]
pub ledger: u32,
/// Event topic symbols (decoded from XDR).
#[allow(dead_code)]
pub topic: Vec<serde_json::Value>,
/// Event value (decoded from XDR).
pub value: serde_json::Value,
}
/// Response from `getEvents`.
#[derive(Debug, Deserialize)]
struct GetEventsResult {
events: Option<Vec<ContractEvent>>,
#[serde(rename = "latestLedger")]
#[allow(dead_code)]
latest_ledger: Option<u64>,
}
// ── Client ────────────────────────────────────────────────────────────────────
/// Thin async wrapper around the Soroban JSON-RPC endpoint.
#[derive(Clone)]
pub struct SorobanRpcClient {
http: Client,
rpc_url: String,
/// Maximum number of retry attempts for retryable errors (default: 3).
max_retries: u32,
/// Base backoff duration before first retry (doubles on each attempt).
base_backoff: Duration,
}
impl SorobanRpcClient {
/// Create a new client.
///
/// `timeout_secs` is applied to every individual HTTP request.
/// Retry behaviour is configured via environment variables:
///
/// | Variable | Default | Description |
/// |---|---|---|
/// | `ROUTER_RPC_MAX_RETRIES` | `3` | Max retry attempts for transient errors. |
/// | `ROUTER_RPC_BACKOFF_MS` | `200` | Base backoff in milliseconds (doubles each retry). |
pub fn new(rpc_url: impl Into<String>, timeout_secs: u64) -> Result<Self> {
let http = Client::builder()
.timeout(Duration::from_secs(timeout_secs))
.build()
.context("failed to build HTTP client")?;
let max_retries = std::env::var("ROUTER_RPC_MAX_RETRIES")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(3u32);
let base_backoff_ms = std::env::var("ROUTER_RPC_BACKOFF_MS")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(200);
Ok(Self {
http,
rpc_url: rpc_url.into(),
max_retries,
base_backoff: Duration::from_millis(base_backoff_ms),
})
}
/// Determine whether an HTTP status code or a reqwest error is retryable.
///
/// We retry on:
/// - Connection-level errors (timeout, connection reset, DNS failure)
/// - HTTP 429 (Too Many Requests) and 5xx server errors
fn is_retryable_error(err: &reqwest::Error) -> bool {
if err.is_timeout() || err.is_connect() {
return true;
}
if let Some(status) = err.status() {
return status.is_server_error() || status.as_u16() == 429;
}
false
}
/// Execute an HTTP POST and parse the JSON-RPC response, retrying up to
/// `self.max_retries` times on transient/retryable errors with exponential
/// backoff.
async fn post_with_retry(&self, req_body: &impl Serialize) -> Result<RpcResponse> {
let mut attempt = 0u32;
loop {
let result = self
.http
.post(&self.rpc_url)
.json(req_body)
.send()
.await;
match result {
Ok(response) => {
// Treat 5xx / 429 as retryable at the HTTP level.
let status = response.status();
if (status.is_server_error() || status.as_u16() == 429)
&& attempt < self.max_retries
{
let delay = self.base_backoff * 2u32.pow(attempt);
warn!(
attempt,
delay_ms = delay.as_millis(),
%status,
"RPC HTTP error — retrying"
);
attempt += 1;
tokio::time::sleep(delay).await;
continue;
}
return response
.json::<RpcResponse>()
.await
.context("failed to parse JSON-RPC response");
}
Err(err) => {
if Self::is_retryable_error(&err) && attempt < self.max_retries {
let delay = self.base_backoff * 2u32.pow(attempt);
warn!(
attempt,
delay_ms = delay.as_millis(),
error = %err,
"RPC request failed — retrying"
);
attempt += 1;
tokio::time::sleep(delay).await;
continue;
}
return Err(err).context("HTTP request failed");
}
}
}
}
/// Call `simulateTransaction` to invoke a read-only contract function and
/// return the raw JSON result value.
///
/// This is the standard way to call view functions on Soroban contracts
/// without submitting a real transaction.
pub async fn simulate_invoke(
&self,
contract_id: &str,
function_name: &str,
args_xdr: Vec<String>,
) -> Result<Value> {
// Build a minimal transaction envelope XDR for simulation.
// We use the `invokeHostFunction` operation type.
let invoke_params = json!({
"transaction": build_invoke_xdr(contract_id, function_name, &args_xdr)?,
});
let req = RpcRequest {
jsonrpc: "2.0",
id: 1,
method: "simulateTransaction",
params: invoke_params,
};
let resp = self
.post_with_retry(&req)
.await?;
if let Some(err) = resp.error {
return Err(anyhow!("RPC error {}: {}", err.code, err.message));
}
resp.result.ok_or_else(|| anyhow!("empty RPC result"))
}
/// Call `getLedgerEntries` for the given base64-encoded XDR keys.
pub async fn get_ledger_entries(&self, keys_xdr: Vec<String>) -> Result<Vec<LedgerEntry>> {
let req = RpcRequest {
jsonrpc: "2.0",
id: 1,
method: "getLedgerEntries",
params: json!({ "keys": keys_xdr }),
};
let resp = self
.post_with_retry(&req)
.await?;
if let Some(err) = resp.error {
return Err(anyhow!("RPC error {}: {}", err.code, err.message));
}
let result: GetLedgerEntriesResult =
serde_json::from_value(resp.result.ok_or_else(|| anyhow!("empty RPC result"))?)
.context("failed to deserialize getLedgerEntries result")?;
Ok(result.entries.unwrap_or_default())
}
/// Call `getEvents` to fetch contract events matching the given topic filters.
///
/// Paginates through all available pages until exhausted or until
/// [`GET_EVENTS_MAX_PAGES`] is reached (logged as a warning).
///
/// `contract_id` — the contract whose events to query.
/// `topic_filters` — list of topic symbol strings to match (e.g. `["quote_generated"]`).
/// `start_ledger` — earliest ledger to include (0 = let the RPC choose).
pub async fn get_events(
&self,
contract_id: &str,
topic_filters: &[&str],
start_ledger: u32,
) -> Result<Vec<ContractEvent>> {
let filters: Vec<serde_json::Value> = topic_filters
.iter()
.map(|t| json!({ "type": "contract", "contractIds": [contract_id], "topics": [[t]] }))
.collect();
let mut all_events: Vec<ContractEvent> = Vec::new();
let mut paging_token: Option<String> = None;
let mut pages_fetched: usize = 0;
loop {
let params = match (&paging_token, start_ledger) {
(Some(token), _) => json!({ "pagingToken": token, "filters": filters }),
(None, ledger) if ledger > 0 => {
json!({ "startLedger": ledger, "filters": filters })
}
(None, _) => json!({ "filters": filters }),
};
let req = RpcRequest {
jsonrpc: "2.0",
id: 1,
method: "getEvents",
params,
};
let resp = self
.post_with_retry(&req)
.await?;
if let Some(err) = resp.error {
return Err(anyhow!("RPC error {}: {}", err.code, err.message));
}
let raw_value = resp.result.ok_or_else(|| anyhow!("empty RPC result"))?;
// Deserialize the raw response to extract paging tokens.
let raw_result: GetEventsResult =
serde_json::from_value(raw_value.clone())
.context("failed to deserialize getEvents result")?;
let page_events = raw_result.events.unwrap_or_default();
if page_events.is_empty() {
break;
}
// Extract the paging token from the last event in this page
// by re-parsing the events array to find pagingToken fields.
let last_paging_token = extract_last_paging_token(&raw_value);
all_events.extend(page_events);
pages_fetched += 1;
if pages_fetched >= GET_EVENTS_MAX_PAGES {
warn!(
pages = pages_fetched,
total_events = all_events.len(),
"get_events hit max-pages cap ({}); remaining events may be missed",
GET_EVENTS_MAX_PAGES
);
break;
}
match last_paging_token {
Some(token) => paging_token = Some(token),
None => break, // No continuation cursor — done.
}
}
debug!(
pages = pages_fetched,
total_events = all_events.len(),
"get_events pagination complete"
);
Ok(all_events)
}
/// Convenience: call a view function and extract a `u64` from the result.
///
/// Soroban returns `u64` values as XDR `ScVal::U64`. The RPC simulation
/// result encodes the return value in `results[0].xdr` as base64 XDR.
/// We parse the JSON representation that the RPC server returns in the
/// `results` array.
pub async fn call_u64(&self, contract_id: &str, function_name: &str) -> Result<u64> {
debug!(contract_id, function_name, "calling view function → u64");
let result = self
.simulate_invoke(contract_id, function_name, vec![])
.await?;
// The simulation result has shape:
// { "results": [{ "xdr": "<base64 ScVal XDR>", ... }], ... }
// We look for a numeric value in the decoded JSON representation.
extract_u64_from_sim_result(&result)
.with_context(|| format!("parsing u64 from {function_name} on {contract_id}"))
}
/// Convenience: call a view function and extract a `bool` from the result.
#[allow(dead_code)]
pub async fn call_bool(&self, contract_id: &str, function_name: &str) -> Result<bool> {
debug!(contract_id, function_name, "calling view function → bool");
let result = self
.simulate_invoke(contract_id, function_name, vec![])
.await?;
extract_bool_from_sim_result(&result)
.with_context(|| format!("parsing bool from {function_name} on {contract_id}"))
}
/// Convenience: call a view function and extract a `Vec<String>` from the result.
pub async fn call_string_vec(
&self,
contract_id: &str,
function_name: &str,
) -> Result<Vec<String>> {
debug!(
contract_id,
function_name, "calling view function → Vec<String>"
);
let result = self
.simulate_invoke(contract_id, function_name, vec![])
.await?;
extract_string_vec_from_sim_result(&result)
.with_context(|| format!("parsing Vec<String> from {function_name} on {contract_id}"))
}
/// Convenience: call a view function with a single string arg and extract a `Vec<u32>` from the result.
pub async fn call_u32_vec(
&self,
contract_id: &str,
function_name: &str,
arg: &str,
) -> Result<Vec<u32>> {
debug!(
contract_id,
function_name, "calling view function → Vec<u32>"
);
let result = self
.simulate_invoke(contract_id, function_name, vec![hex_encode_arg(arg)])
.await?;
extract_u32_vec_from_sim_result(&result).with_context(|| {
format!("parsing Vec<u32> from {function_name}({arg}) on {contract_id}")
})
}
}
// ── XDR helpers ───────────────────────────────────────────────────────────────
// ── Strkey decode ─────────────────────────────────────────────────────────────
const BASE32_ALPHA: &[u8; 32] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
const VERSION_CONTRACT: u8 = 2 << 3; // 0x10 → first char 'C'
/// CRC-16/XModem used by Stellar strkey checksums.
fn crc16(data: &[u8]) -> u16 {
let mut crc: u16 = 0;
for &b in data {
crc ^= (b as u16) << 8;
for _ in 0..8 {
crc = if crc & 0x8000 != 0 {
(crc << 1) ^ 0x1021
} else {
crc << 1
};
}
}
crc
}
/// Decode a Stellar contract strkey (C…) into its 32-byte hash.
pub fn decode_contract_id(strkey: &str) -> Result<[u8; 32]> {
if strkey.len() != 56 {
return Err(anyhow!("strkey must be 56 chars, got {}", strkey.len()));
}
let mut lookup = [0xFFu8; 256];
for (i, &c) in BASE32_ALPHA.iter().enumerate() {
lookup[c as usize] = i as u8;
}
let mut bits: u64 = 0;
let mut bit_count: u32 = 0;
let mut decoded: Vec<u8> = Vec::with_capacity(35);
for &ch in strkey.as_bytes() {
let v = lookup[ch as usize];
if v == 0xFF {
return Err(anyhow!("invalid base32 character '{}'", ch as char));
}
bits = (bits << 5) | v as u64;
bit_count += 5;
if bit_count >= 8 {
bit_count -= 8;
decoded.push((bits >> bit_count) as u8);
}
}
if decoded.len() != 35 {
return Err(anyhow!(
"strkey decoded to {} bytes, expected 35",
decoded.len()
));
}
let version = decoded[0];
if version != VERSION_CONTRACT {
return Err(anyhow!(
"expected contract strkey (C…), got version 0x{:02x}",
version
));
}
let payload: [u8; 32] = decoded[1..33].try_into().unwrap();
let stored_crc = u16::from_le_bytes([decoded[33], decoded[34]]);
let actual_crc = crc16(&decoded[..33]);
if actual_crc != stored_crc {
return Err(anyhow!("strkey checksum mismatch"));
}
Ok(payload)
}
// ── Base64 ────────────────────────────────────────────────────────────────────
const B64: &[u8; 64] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
fn base64_encode(input: &[u8]) -> String {
let mut out = String::with_capacity(input.len().div_ceil(3) * 4);
for chunk in input.chunks(3) {
let b0 = chunk[0] as u32;
let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
let n = (b0 << 16) | (b1 << 8) | b2;
out.push(B64[(n >> 18 & 0x3F) as usize] as char);
out.push(B64[(n >> 12 & 0x3F) as usize] as char);
out.push(if chunk.len() > 1 {
B64[(n >> 6 & 0x3F) as usize] as char
} else {
'='
});
out.push(if chunk.len() > 2 {
B64[(n & 0x3F) as usize] as char
} else {
'='
});
}
out
}
// ── XDR writer ────────────────────────────────────────────────────────────────
struct XdrWriter(Vec<u8>);
impl XdrWriter {
fn new() -> Self {
Self(Vec::new())
}
fn u32(&mut self, v: u32) {
self.0.extend_from_slice(&v.to_be_bytes());
}
fn i64(&mut self, v: i64) {
self.0.extend_from_slice(&v.to_be_bytes());
}
/// Write `data` followed by zero-padding to the next 4-byte boundary.
fn opaque_fixed(&mut self, data: &[u8]) {
self.0.extend_from_slice(data);
let pad = data.len().wrapping_neg() & 3;
self.0.extend(std::iter::repeat_n(0u8, pad));
}
/// Write a 4-byte length prefix, then `opaque_fixed(data)`.
fn opaque_var(&mut self, data: &[u8]) {
self.u32(data.len() as u32);
self.opaque_fixed(data);
}
fn into_bytes(self) -> Vec<u8> {
self.0
}
}
// ScVal discriminants (Soroban protocol 21)
const SCV_STRING: u32 = 14;
const SC_ADDR_CONTRACT: u32 = 1;
/// Build a minimal base64-encoded `TransactionEnvelope` (v1) containing a
/// single `InvokeHostFunctionOp` for `simulateTransaction`.
///
/// The source account is the all-zero Ed25519 key, fee is 100 stroops, and
/// sequence number is 0. Signatures are omitted — `simulateTransaction` does
/// not validate them.
///
/// `args_xdr` items are treated as Soroban `ScVal::String` arguments
/// (the only type currently needed for registry/core view functions).
fn build_invoke_xdr(
contract_id: &str,
function_name: &str,
args_xdr: &[String],
) -> Result<String> {
let contract_hash = decode_contract_id(contract_id)
.with_context(|| format!("invalid contract id '{contract_id}'"))?;
let mut w = XdrWriter::new();
// TransactionEnvelope discriminant: ENVELOPE_TYPE_TX = 2
w.u32(2);
// sourceAccount: MuxedAccount::Ed25519([0u8;32])
w.u32(0); // KEY_TYPE_ED25519
w.opaque_fixed(&[0u8; 32]);
w.u32(100); // fee (stroops)
w.i64(0); // seqNum
// cond: Preconditions::None
w.u32(0);
// memo: Memo::None
w.u32(0);
// operations: count = 1
w.u32(1);
// Operation
w.u32(0); // sourceAccount: absent
w.u32(24); // OperationType::INVOKE_HOST_FUNCTION
// InvokeHostFunctionOp
// hostFunction: HOST_FUNCTION_TYPE_INVOKE_CONTRACT = 0
w.u32(0);
// InvokeContractArgs
// contractAddress: SC_ADDRESS_TYPE_CONTRACT
w.u32(SC_ADDR_CONTRACT);
w.opaque_fixed(&contract_hash); // Hash(32)
// functionName: SCSymbol
w.opaque_var(function_name.as_bytes());
// args: SCVal[]
w.u32(args_xdr.len() as u32);
for arg in args_xdr {
// Treat each arg string as SCV_STRING
w.u32(SCV_STRING);
w.opaque_var(arg.as_bytes());
}
// auth: SorobanAuthorizationEntry[] — empty for read-only calls
w.u32(0);
// Transaction.ext: v = 0
w.u32(0);
// TransactionV1Envelope.signatures: count = 0
w.u32(0);
Ok(base64_encode(&w.into_bytes()))
}
// ── Result extraction helpers ─────────────────────────────────────────────────
/// Extract a `u64` from a `simulateTransaction` result JSON value.
///
/// The Soroban RPC returns the return value as a base64-encoded `ScVal` XDR
/// in `result.results[0].xdr`. The RPC server also provides a JSON-decoded
/// representation in some versions. We try both paths.
pub fn extract_u64_from_sim_result(result: &Value) -> Result<u64> {
// Path 1: JSON-decoded ScVal in `results[0].retval` (newer RPC versions)
if let Some(v) = result
.get("results")
.and_then(|r| r.get(0))
.and_then(|r| r.get("retval"))
{
if let Some(n) = v.as_u64() {
return Ok(n);
}
// ScVal::U64 is encoded as `{"u64": <number>}` in JSON
if let Some(n) = v.get("u64").and_then(|n| n.as_u64()) {
return Ok(n);
}
}
// Path 2: Numeric value directly in `result`
if let Some(n) = result.as_u64() {
return Ok(n);
}
Err(anyhow!("could not find u64 in simulation result: {result}"))
}
/// Extract a `bool` from a `simulateTransaction` result JSON value.
#[allow(dead_code)]
pub fn extract_bool_from_sim_result(result: &Value) -> Result<bool> {
if let Some(v) = result
.get("results")
.and_then(|r| r.get(0))
.and_then(|r| r.get("retval"))
{
if let Some(b) = v.as_bool() {
return Ok(b);
}
if let Some(b) = v.get("bool").and_then(|b| b.as_bool()) {
return Ok(b);
}
}
if let Some(b) = result.as_bool() {
return Ok(b);
}
Err(anyhow!(
"could not find bool in simulation result: {result}"
))
}
/// Extract a `Vec<String>` from a `simulateTransaction` result JSON value.
pub fn extract_string_vec_from_sim_result(result: &Value) -> Result<Vec<String>> {
let retval = result
.get("results")
.and_then(|r| r.get(0))
.and_then(|r| r.get("retval"))
.unwrap_or(result);
if let Some(arr) = retval.as_array() {
let strings: Vec<String> = arr
.iter()
.filter_map(|v| {
v.as_str()
.map(|s| s.to_string())
.or_else(|| v.get("str").and_then(|s| s.as_str()).map(|s| s.to_string()))
})
.collect();
return Ok(strings);
}
// Empty vec is a valid return value
Ok(vec![])
}
/// Extract a `Vec<u32>` from a `simulateTransaction` result JSON value.
///
/// Used to parse the return value of `versions(name) -> Vec<u32>`.
pub fn extract_u32_vec_from_sim_result(result: &Value) -> Result<Vec<u32>> {
let retval = result
.get("results")
.and_then(|r| r.get(0))
.and_then(|r| r.get("retval"))
.unwrap_or(result);
if let Some(arr) = retval.as_array() {
let nums: Vec<u32> = arr
.iter()
.filter_map(|v| {
v.as_u64()
.map(|n| n as u32)
.or_else(|| v.get("u32").and_then(|n| n.as_u64()).map(|n| n as u32))
})
.collect();
return Ok(nums);
}
Ok(vec![])
}
/// Hex-encode a string as a placeholder XDR `ScVal::String` argument.
fn hex_encode_arg(s: &str) -> String {
use std::fmt::Write;
let mut out = String::new();
for b in s.as_bytes() {
write!(out, "{b:02x}").ok();
}
out
}
/// Extract the `pagingToken` from the last event in a `getEvents` response.
///
/// The Soroban RPC embeds a continuation cursor in each event's `pagingToken`
/// field. We only need the token from the final event to request the next page.
pub fn extract_last_paging_token(raw_result: &Value) -> Option<String> {
raw_result
.get("events")?
.as_array()?
.last()?
.get("pagingToken")?
.as_str()
.map(|s| s.to_string())
}
// ── Storage key XDR helpers ───────────────────────────────────────────────────
/// Build the base64-encoded XDR key for a `ContractData` ledger entry.
///
/// This is used with `getLedgerEntries` to read contract storage directly
/// without needing to simulate a transaction.
///
/// The key format is:
/// ```text
/// LedgerKey::ContractData {
/// contract: ScAddress::Contract(Hash(<contract_id_bytes>)),
/// key: <ScVal>,
/// durability: ContractDataDurability::Persistent | Instance,
/// }
/// ```
///
/// For the simple scalar keys used by router contracts (e.g. `DataKey::TotalRouted`,
/// `DataKey::Paused`) the `key` ScVal is a `ScVal::LedgerKeyContractInstance`
/// for instance storage.
///
/// # Stub — not yet implemented
///
/// This function is an **intentional stub**. Direct XDR construction requires the
/// [`stellar-xdr`](https://crates.io/crates/stellar-xdr) crate (or equivalent
/// hand-rolled encoding), which has not been integrated yet. Until that work is
/// complete the metrics collector falls back to the simulation path
/// (`simulateTransaction`) instead of calling `getLedgerEntries` directly.
///
/// This is tracked as a known limitation in `metrics/CHANGELOG.md`
/// ("XDR transaction building not implemented, uses simulation-based scraping
/// instead") and is planned for a future release once `stellar-xdr` encoding is
/// integrated (see issue #183).
///
/// **Do not remove `#[allow(dead_code)]`** until the stub is replaced with a
/// real implementation — the attribute documents the deliberate incompleteness.
///
/// # Errors
///
/// Always returns `Err` with the message:
/// `"Direct XDR key construction not implemented. Use the simulation path or integrate stellar-xdr."`
#[allow(dead_code)]
pub fn instance_storage_key_xdr(_contract_id: &str) -> Result<String> {
Err(anyhow!(
"Direct XDR key construction not implemented. \
Use the simulation path or integrate stellar-xdr."
))
}
// ── RpcClient trait ───────────────────────────────────────────────────────────
/// Trait abstracting the Soroban RPC calls used by the collector.
///
/// Implement this trait with [`MockRpcClient`] in tests to avoid live network
/// access, or use the real [`SorobanRpcClient`] in production.
#[async_trait::async_trait]
#[allow(dead_code)]
pub trait RpcClient: Send + Sync {
async fn call_u64(&self, contract_id: &str, function_name: &str) -> Result<u64>;
async fn call_bool(&self, contract_id: &str, function_name: &str) -> Result<bool>;
async fn call_string_vec(&self, contract_id: &str, function_name: &str) -> Result<Vec<String>>;
async fn call_u32_vec(
&self,
contract_id: &str,
function_name: &str,
arg: &str,
) -> Result<Vec<u32>>;
async fn simulate_invoke(
&self,
contract_id: &str,
function_name: &str,
args_xdr: Vec<String>,
) -> Result<serde_json::Value>;
async fn get_events(
&self,
contract_id: &str,
topic_filters: &[&str],
start_ledger: u32,
) -> Result<Vec<ContractEvent>>;
async fn get_ledger_entries(&self, keys_xdr: Vec<String>) -> Result<Vec<LedgerEntry>>;
}
#[async_trait::async_trait]
impl RpcClient for SorobanRpcClient {
async fn call_u64(&self, contract_id: &str, function_name: &str) -> Result<u64> {
self.call_u64(contract_id, function_name).await
}
async fn call_bool(&self, contract_id: &str, function_name: &str) -> Result<bool> {
self.call_bool(contract_id, function_name).await
}
async fn call_string_vec(&self, contract_id: &str, function_name: &str) -> Result<Vec<String>> {
self.call_string_vec(contract_id, function_name).await
}
async fn call_u32_vec(
&self,
contract_id: &str,
function_name: &str,
arg: &str,
) -> Result<Vec<u32>> {
self.call_u32_vec(contract_id, function_name, arg).await
}
async fn simulate_invoke(
&self,
contract_id: &str,
function_name: &str,
args_xdr: Vec<String>,
) -> Result<serde_json::Value> {
self.simulate_invoke(contract_id, function_name, args_xdr)
.await
}
async fn get_events(
&self,
contract_id: &str,
topic_filters: &[&str],
start_ledger: u32,
) -> Result<Vec<ContractEvent>> {
self.get_events(contract_id, topic_filters, start_ledger)
.await
}
async fn get_ledger_entries(&self, keys_xdr: Vec<String>) -> Result<Vec<LedgerEntry>> {
self.get_ledger_entries(keys_xdr).await
}
}
// ── MockRpcClient ─────────────────────────────────────────────────────────────
/// A deterministic mock RPC client for use in tests.
///
/// Pre-load responses via the builder methods; any call not explicitly
/// configured returns an error so tests fail loudly on unexpected calls.
///
/// # Example
/// ```rust
/// let mock = MockRpcClient::new()
/// .with_u64("CONTRACT", "total_routed", 42)
/// .with_string_vec("CONTRACT", "get_all_routes", vec![]);
/// ```
#[cfg(test)]
pub struct MockRpcClient {
u64_responses: std::collections::HashMap<(String, String), u64>,
bool_responses: std::collections::HashMap<(String, String), bool>,
string_vec_responses: std::collections::HashMap<(String, String), Vec<String>>,
u32_vec_responses: std::collections::HashMap<(String, String, String), Vec<u32>>,
simulate_responses: std::collections::HashMap<(String, String), serde_json::Value>,
events_responses: std::collections::HashMap<(String, String), Vec<ContractEvent>>,
ledger_entries_responses: std::collections::HashMap<String, Vec<LedgerEntry>>,
}
#[cfg(test)]
impl MockRpcClient {
pub fn new() -> Self {
Self {
u64_responses: Default::default(),
bool_responses: Default::default(),
string_vec_responses: Default::default(),
u32_vec_responses: Default::default(),
simulate_responses: Default::default(),
events_responses: Default::default(),
ledger_entries_responses: Default::default(),
}
}
pub fn with_u64(mut self, contract: &str, func: &str, val: u64) -> Self {
self.u64_responses
.insert((contract.to_string(), func.to_string()), val);
self
}
pub fn with_bool(mut self, contract: &str, func: &str, val: bool) -> Self {
self.bool_responses
.insert((contract.to_string(), func.to_string()), val);
self
}
pub fn with_string_vec(mut self, contract: &str, func: &str, val: Vec<String>) -> Self {
self.string_vec_responses
.insert((contract.to_string(), func.to_string()), val);
self
}
/// Pre-load a `call_u32_vec` response for a given contract + function + arg.
pub fn with_u32_vec(mut self, contract: &str, func: &str, arg: &str, val: Vec<u32>) -> Self {
self.u32_vec_responses.insert(
(contract.to_string(), func.to_string(), arg.to_string()),
val,
);
self
}
pub fn with_simulate(mut self, contract: &str, func: &str, val: serde_json::Value) -> Self {
self.simulate_responses
.insert((contract.to_string(), func.to_string()), val);
self
}
/// Pre-load a `getEvents` response for a given contract + topic.
pub fn with_events(mut self, contract: &str, topic: &str, val: Vec<ContractEvent>) -> Self {
self.events_responses
.insert((contract.to_string(), topic.to_string()), val);
self
}
/// Pre-load a `getLedgerEntries` response keyed by the first XDR key.
pub fn with_ledger_entries(mut self, key: &str, val: Vec<LedgerEntry>) -> Self {
self.ledger_entries_responses.insert(key.to_string(), val);
self
}
}
#[cfg(test)]
#[async_trait::async_trait]
impl RpcClient for MockRpcClient {
async fn call_u64(&self, contract_id: &str, function_name: &str) -> Result<u64> {
self.u64_responses
.get(&(contract_id.to_string(), function_name.to_string()))
.copied()
.ok_or_else(|| {
anyhow::anyhow!("MockRpcClient: no u64 response for {contract_id}::{function_name}")
})
}
async fn call_bool(&self, contract_id: &str, function_name: &str) -> Result<bool> {
self.bool_responses
.get(&(contract_id.to_string(), function_name.to_string()))
.copied()
.ok_or_else(|| {
anyhow::anyhow!(
"MockRpcClient: no bool response for {contract_id}::{function_name}"
)
})
}
async fn call_string_vec(&self, contract_id: &str, function_name: &str) -> Result<Vec<String>> {
self.string_vec_responses
.get(&(contract_id.to_string(), function_name.to_string()))
.cloned()
.ok_or_else(|| {
anyhow::anyhow!(
"MockRpcClient: no string_vec response for {contract_id}::{function_name}"
)
})
}
async fn call_u32_vec(
&self,
contract_id: &str,
function_name: &str,
arg: &str,
) -> Result<Vec<u32>> {
Ok(self
.u32_vec_responses
.get(&(
contract_id.to_string(),
function_name.to_string(),