r402-hedera 0.17.1

Hedera chain support for the x402 payment protocol.
Documentation
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
//! Mirror Node REST client used for preflight and signature verification.

use std::fmt::{Debug, Formatter};

use base64::Engine;
use hedera::{AnyTransaction, PublicKey};
use serde::Deserialize;

use super::types::is_hbar_asset;

/// Errors from Mirror Node REST reads.
#[derive(Debug, thiserror::Error)]
pub enum HederaMirrorError {
    /// HTTP or transport failure.
    #[error("hedera mirror error: {0}")]
    Http(String),
    /// Response JSON could not be parsed.
    #[error("hedera mirror parse error: {0}")]
    Parse(String),
}

/// Account resolution used by alias policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HederaAccountResolution {
    /// Whether the identifier resolved to an on-chain account.
    pub exists: bool,
    /// Whether the identifier is an alias rather than a numeric entity id.
    pub is_alias: bool,
}

/// Result of a preflight balance / association check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HederaPreflightResult {
    /// Whether the transfer is expected to succeed.
    pub ok: bool,
    /// Machine-readable failure reason.
    pub reason: Option<String>,
    /// Human-readable detail.
    pub message: Option<String>,
}

impl HederaPreflightResult {
    /// Successful preflight.
    #[must_use]
    pub const fn ok() -> Self {
        Self {
            ok: true,
            reason: None,
            message: None,
        }
    }

    /// Failed preflight.
    #[must_use]
    pub fn fail(reason: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            ok: false,
            reason: Some(reason.into()),
            message: Some(message.into()),
        }
    }

    /// Formats `reason: message` for the verify `invalidMessage` field.
    #[must_use]
    pub fn invalid_message(&self) -> Option<String> {
        match (&self.reason, &self.message) {
            (Some(reason), Some(message)) => Some(format!("{reason}: {message}")),
            (Some(reason), None) => Some(reason.clone()),
            (None, Some(message)) => Some(message.clone()),
            (None, None) => None,
        }
    }
}

/// Result of payer-signature verification.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HederaSignatureResult {
    /// Whether the payer key signed the frozen body.
    pub ok: bool,
    /// Machine-readable failure reason.
    pub reason: Option<String>,
    /// Human-readable detail.
    pub message: Option<String>,
}

impl HederaSignatureResult {
    /// Valid signature.
    #[must_use]
    pub const fn ok() -> Self {
        Self {
            ok: true,
            reason: None,
            message: None,
        }
    }

    /// Invalid or unverifiable signature.
    #[must_use]
    pub fn fail(reason: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            ok: false,
            reason: Some(reason.into()),
            message: Some(message.into()),
        }
    }

    /// Formats `reason: message` for the verify `invalidMessage` field.
    #[must_use]
    pub fn invalid_message(&self) -> Option<String> {
        match (&self.reason, &self.message) {
            (Some(reason), Some(message)) => Some(format!("{reason}: {message}")),
            (Some(reason), None) => Some(reason.clone()),
            (None, Some(message)) => Some(message.clone()),
            (None, None) => None,
        }
    }
}

/// Mirror Node REST client.
#[derive(Clone)]
pub struct HederaMirrorClient {
    http: reqwest::Client,
    base_url: String,
}

impl Debug for HederaMirrorClient {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HederaMirrorClient")
            .field("base_url", &self.base_url)
            .finish_non_exhaustive()
    }
}

impl HederaMirrorClient {
    /// Connects to the given Mirror Node base URL (no trailing slash).
    #[must_use]
    pub fn connect(base_url: impl Into<String>) -> Self {
        Self {
            http: reqwest::Client::new(),
            base_url: trim_slash(base_url.into()),
        }
    }

    /// Fetches `/api/v1/accounts/{id}`.
    ///
    /// # Errors
    ///
    /// Returns [`HederaMirrorError`] on HTTP or JSON failure.
    pub async fn account(&self, account_id: &str) -> Result<MirrorAccount, HederaMirrorError> {
        let url = format!(
            "{}/api/v1/accounts/{}",
            self.base_url,
            urlencoding(account_id)
        );
        self.get_json(&url).await
    }

    /// Fetches token relationships, optionally filtered by token id.
    ///
    /// # Errors
    ///
    /// Returns [`HederaMirrorError`] on HTTP or JSON failure.
    pub async fn account_tokens(
        &self,
        account_id: &str,
        token_id: Option<&str>,
    ) -> Result<MirrorTokensResponse, HederaMirrorError> {
        let mut url = format!(
            "{}/api/v1/accounts/{}/tokens",
            self.base_url,
            urlencoding(account_id)
        );
        if let Some(token_id) = token_id {
            url.push_str("?token.id=");
            url.push_str(&urlencoding(token_id));
        }
        self.get_json(&url).await
    }

    /// Fetches a Mirror Node path returned in `links.next`.
    ///
    /// # Errors
    ///
    /// Returns [`HederaMirrorError`] on HTTP or JSON failure.
    pub async fn get_path<T: for<'de> Deserialize<'de>>(
        &self,
        path: &str,
    ) -> Result<T, HederaMirrorError> {
        let url = if path.starts_with("http://") || path.starts_with("https://") {
            path.to_owned()
        } else {
            format!("{}{path}", self.base_url)
        };
        self.get_json(&url).await
    }

    async fn get_json<T: for<'de> Deserialize<'de>>(
        &self,
        url: &str,
    ) -> Result<T, HederaMirrorError> {
        let response = self
            .http
            .get(url)
            .send()
            .await
            .map_err(|e| HederaMirrorError::Http(e.to_string()))?;
        let status = response.status();
        if !status.is_success() {
            return Err(HederaMirrorError::Http(format!(
                "Mirror Node request failed with status {status}"
            )));
        }
        response
            .json()
            .await
            .map_err(|e| HederaMirrorError::Parse(e.to_string()))
    }
}

/// Account fields read from `/accounts/{id}`.
#[derive(Debug, Clone, Deserialize)]
pub struct MirrorAccount {
    /// Tinybar balance wrapper.
    #[serde(default)]
    pub balance: MirrorBalance,
    /// Auto-association slot cap (`-1` is unlimited).
    #[serde(default)]
    pub max_automatic_token_associations: i64,
    /// On-chain account key.
    #[serde(default)]
    pub key: Option<MirrorAccountKey>,
    /// Alias bytes or evm address when present.
    #[serde(default)]
    pub alias: Option<String>,
}

/// Balance object from the Mirror Node account endpoint.
#[derive(Debug, Clone, Copy, Default, Deserialize)]
pub struct MirrorBalance {
    /// Tinybars held by the account.
    #[serde(default)]
    pub balance: i64,
}

/// Account key as returned by the Mirror Node.
#[derive(Debug, Clone, Deserialize)]
pub struct MirrorAccountKey {
    /// Key encoding: `ED25519`, `ECDSA_SECP256K1`, or `ProtobufEncoded`.
    #[serde(rename = "_type")]
    pub key_type: String,
    /// Hex-encoded key bytes.
    pub key: String,
}

/// A token relationship entry from `/accounts/{id}/tokens`.
#[derive(Debug, Clone, Deserialize)]
pub struct MirrorTokenRelationship {
    /// Token entity id.
    pub token_id: String,
    /// Token balance in atomic units.
    #[serde(default)]
    pub balance: i64,
    /// Whether this relationship used an auto-association slot.
    #[serde(default)]
    pub automatic_association: bool,
}

/// Paged response from `/accounts/{id}/tokens`.
#[derive(Debug, Clone, Deserialize)]
pub struct MirrorTokensResponse {
    /// Token relationships on this page.
    #[serde(default)]
    pub tokens: Vec<MirrorTokenRelationship>,
    /// Pagination links.
    #[serde(default)]
    pub links: MirrorLinks,
}

/// Pagination links from the Mirror Node.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct MirrorLinks {
    /// Relative path of the next page, if any.
    #[serde(default)]
    pub next: Option<String>,
}

/// Runs the HBAR / HTS association preflight against a Mirror Node.
///
/// # Errors
///
/// Returns [`HederaMirrorError`] when Mirror Node requests fail.
pub async fn preflight_transfer(
    mirror: &HederaMirrorClient,
    payer: &str,
    pay_to: &str,
    asset: &str,
    amount: &str,
) -> Result<HederaPreflightResult, HederaMirrorError> {
    let required = amount
        .parse::<i128>()
        .map_err(|e| HederaMirrorError::Parse(e.to_string()))?;

    if is_hbar_asset(asset) {
        let account = mirror.account(payer).await?;
        let held = i128::from(account.balance.balance);
        if held < required {
            return Ok(HederaPreflightResult::fail(
                "insufficient_balance",
                format!("payer has {held} tinybars, needs {required}"),
            ));
        }
        return Ok(HederaPreflightResult::ok());
    }

    let payer_tokens = mirror.account_tokens(payer, Some(asset)).await?;
    let held = payer_tokens
        .tokens
        .first()
        .map_or(0, |t| i128::from(t.balance));
    if held < required {
        return Ok(HederaPreflightResult::fail(
            "insufficient_balance",
            format!("payer holds {held} of {asset}, needs {required}"),
        ));
    }

    if is_pay_to_associated(mirror, pay_to, asset).await? {
        return Ok(HederaPreflightResult::ok());
    }
    Ok(HederaPreflightResult::fail(
        "pay_to_not_associated",
        format!("payTo {pay_to} is not associated with {asset} and has no auto-association slots"),
    ))
}

async fn is_pay_to_associated(
    mirror: &HederaMirrorClient,
    pay_to: &str,
    asset: &str,
) -> Result<bool, HederaMirrorError> {
    let direct = mirror.account_tokens(pay_to, Some(asset)).await?;
    if !direct.tokens.is_empty() {
        return Ok(true);
    }

    let account = mirror.account(pay_to).await?;
    let max_auto = account.max_automatic_token_associations;
    if max_auto == -1 {
        return Ok(true);
    }
    if max_auto == 0 {
        return Ok(false);
    }

    let mut consumed = 0i64;
    let mut next = Some(format!("/api/v1/accounts/{pay_to}/tokens"));
    while let Some(path) = next {
        let page: MirrorTokensResponse = mirror.get_path(&path).await?;
        let auto_count = page
            .tokens
            .iter()
            .filter(|t| t.automatic_association)
            .count();
        let auto_count = i64::try_from(auto_count).unwrap_or(i64::MAX);
        consumed = consumed.saturating_add(auto_count);
        if consumed >= max_auto {
            return Ok(false);
        }
        next = page.links.next;
    }
    Ok(consumed < max_auto)
}

/// Resolves `payTo` against the Mirror Node for alias policy.
///
/// # Errors
///
/// Returns [`HederaMirrorError`] when the account lookup fails for a reason
/// other than not-found.
pub async fn resolve_account(
    mirror: &HederaMirrorClient,
    account_id_or_alias: &str,
) -> Result<HederaAccountResolution, HederaMirrorError> {
    match mirror.account(account_id_or_alias).await {
        Ok(_) => Ok(HederaAccountResolution {
            exists: true,
            is_alias: !super::types::is_entity_id(account_id_or_alias),
        }),
        Err(HederaMirrorError::Http(msg)) if msg.contains("status 404") => {
            Ok(HederaAccountResolution {
                exists: false,
                is_alias: !super::types::is_entity_id(account_id_or_alias),
            })
        }
        Err(err) => Err(err),
    }
}

/// Verifies that `payer` signed `transaction_base64` using the on-chain key.
///
/// # Errors
///
/// Returns [`HederaMirrorError`] when the account or key cannot be loaded.
pub async fn verify_payer_signature(
    mirror: &HederaMirrorClient,
    payer: &str,
    transaction_base64: &str,
) -> Result<HederaSignatureResult, HederaMirrorError> {
    let bytes = Engine::decode(
        &base64::engine::general_purpose::STANDARD,
        transaction_base64,
    )
    .map_err(|e| HederaMirrorError::Parse(e.to_string()))?;
    let mut tx =
        AnyTransaction::from_bytes(&bytes).map_err(|e| HederaMirrorError::Parse(e.to_string()))?;
    let account = mirror.account(payer).await?;
    let Some(mirror_key) = account.key.as_ref() else {
        return Ok(HederaSignatureResult::fail(
            "signature_invalid",
            "could not resolve payer key",
        ));
    };
    let key = match parse_mirror_key(mirror_key) {
        Ok(key) => key,
        Err(err) => {
            return Ok(HederaSignatureResult::fail("signature_unverifiable", err));
        }
    };
    if key.signs(&mut tx) {
        Ok(HederaSignatureResult::ok())
    } else {
        Ok(HederaSignatureResult::fail(
            "signature_invalid",
            format!("payer {payer} did not sign the transaction"),
        ))
    }
}

/// Account key reconstructed from Mirror Node JSON.
#[derive(Debug, Clone)]
enum AccountKey {
    /// A single public key.
    Single(PublicKey),
    /// A key list, optionally thresholded.
    List {
        /// Nested keys.
        keys: Vec<Self>,
        /// Number of keys that must sign.
        threshold: usize,
    },
}

impl AccountKey {
    fn signs(&self, tx: &mut AnyTransaction) -> bool {
        match self {
            Self::Single(pk) => pk.verify_transaction(tx).is_ok(),
            Self::List { keys, threshold } => {
                keys.iter().filter(|k| k.signs(tx)).count() >= *threshold
            }
        }
    }
}

fn parse_mirror_key(mirror_key: &MirrorAccountKey) -> Result<AccountKey, String> {
    if mirror_key.key.is_empty() {
        return Err("could not resolve payer key".to_owned());
    }
    match mirror_key.key_type.as_str() {
        "ED25519" => {
            let bytes = decode_hex(&mirror_key.key)?;
            PublicKey::from_bytes_ed25519(&bytes)
                .map(AccountKey::Single)
                .map_err(|e| e.to_string())
        }
        "ECDSA_SECP256K1" => {
            let bytes = decode_hex(&mirror_key.key)?;
            PublicKey::from_bytes_ecdsa(&bytes)
                .map(AccountKey::Single)
                .map_err(|e| e.to_string())
        }
        "ProtobufEncoded" => {
            let bytes = decode_hex(&mirror_key.key)?;
            parse_proto_key(&bytes)
        }
        other => Err(format!("unrecognized mirror key type {other}")),
    }
}

fn decode_hex(s: &str) -> Result<Vec<u8>, String> {
    let s = s.strip_prefix("0x").unwrap_or(s);
    hex::decode(s).map_err(|_| "invalid hex key".to_owned())
}

// `hedera::protobuf::FromProtobuf` is `pub(crate)`; `Key` has no public decode.
fn parse_proto_key(bytes: &[u8]) -> Result<AccountKey, String> {
    let mut rest = bytes;
    let mut found = None;
    while !rest.is_empty() {
        let (tag, after_tag) = read_varint(rest)?;
        rest = after_tag;
        let field = tag >> 3;
        let wire = tag & 7;
        if wire != 2 {
            return Err("unsupported protobuf key wire type".to_owned());
        }
        let (len, after_len) = read_varint(rest)?;
        rest = after_len;
        let len = usize::try_from(len).map_err(|_| "invalid protobuf key length".to_owned())?;
        if rest.len() < len {
            return Err("truncated protobuf key".to_owned());
        }
        let (payload, after_payload) = rest.split_at(len);
        rest = after_payload;
        found = Some(match field {
            2 => AccountKey::Single(
                PublicKey::from_bytes_ed25519(payload).map_err(|e| e.to_string())?,
            ),
            5 => parse_threshold_key(payload)?,
            6 => parse_key_list(payload, None)?,
            7 => {
                AccountKey::Single(PublicKey::from_bytes_ecdsa(payload).map_err(|e| e.to_string())?)
            }
            1 | 3 | 4 | 8 => return Err("unsupported account key kind".to_owned()),
            _ => continue,
        });
    }
    found.ok_or_else(|| "empty protobuf key".to_owned())
}

fn parse_threshold_key(bytes: &[u8]) -> Result<AccountKey, String> {
    let mut rest = bytes;
    let mut threshold = 0u32;
    let mut list = None;
    while !rest.is_empty() {
        let (tag, after_tag) = read_varint(rest)?;
        rest = after_tag;
        let field = tag >> 3;
        let wire = tag & 7;
        match (field, wire) {
            (1, 0) => {
                let (value, after) = read_varint(rest)?;
                rest = after;
                threshold = u32::try_from(value).map_err(|_| "invalid threshold".to_owned())?;
            }
            (2, 2) => {
                let (len, after_len) = read_varint(rest)?;
                rest = after_len;
                let len =
                    usize::try_from(len).map_err(|_| "invalid protobuf key length".to_owned())?;
                if rest.len() < len {
                    return Err("truncated threshold key list".to_owned());
                }
                let (payload, after_payload) = rest.split_at(len);
                rest = after_payload;
                list = Some(payload);
            }
            (_, 2) => {
                let (len, after_len) = read_varint(rest)?;
                rest = after_len;
                let len =
                    usize::try_from(len).map_err(|_| "invalid protobuf key length".to_owned())?;
                rest = rest
                    .get(len..)
                    .ok_or_else(|| "truncated protobuf key".to_owned())?;
            }
            (_, 0) => {
                let (_, after) = read_varint(rest)?;
                rest = after;
            }
            _ => return Err("unsupported threshold key field".to_owned()),
        }
    }
    let Some(list) = list else {
        return Err("threshold key missing key list".to_owned());
    };
    let threshold = if threshold == 0 {
        None
    } else {
        Some(threshold)
    };
    parse_key_list(list, threshold)
}

fn parse_key_list(bytes: &[u8], threshold: Option<u32>) -> Result<AccountKey, String> {
    let mut rest = bytes;
    let mut keys = Vec::new();
    while !rest.is_empty() {
        let (tag, after_tag) = read_varint(rest)?;
        rest = after_tag;
        let field = tag >> 3;
        let wire = tag & 7;
        if field != 1 || wire != 2 {
            return Err("unexpected key list field".to_owned());
        }
        let (len, after_len) = read_varint(rest)?;
        rest = after_len;
        let len = usize::try_from(len).map_err(|_| "invalid protobuf key length".to_owned())?;
        if rest.len() < len {
            return Err("truncated key list entry".to_owned());
        }
        let (payload, after_payload) = rest.split_at(len);
        rest = after_payload;
        keys.push(parse_proto_key(payload)?);
    }
    let threshold = threshold.map_or(keys.len(), |t| t as usize);
    Ok(AccountKey::List { keys, threshold })
}

fn read_varint(bytes: &[u8]) -> Result<(u64, &[u8]), String> {
    let mut value = 0u64;
    let mut shift = 0u32;
    for (i, b) in bytes.iter().copied().enumerate() {
        let bits = u64::from(b & 0x7f);
        value |= bits
            .checked_shl(shift)
            .ok_or_else(|| "varint overflow".to_owned())?;
        if b & 0x80 == 0 {
            let rest = bytes
                .get(i.saturating_add(1)..)
                .ok_or_else(|| "truncated varint".to_owned())?;
            return Ok((value, rest));
        }
        shift = shift.saturating_add(7);
        if shift > 63 {
            return Err("varint overflow".to_owned());
        }
    }
    Err("truncated varint".to_owned())
}

fn trim_slash(mut s: String) -> String {
    while s.ends_with('/') {
        s.pop();
    }
    s
}

fn urlencoding(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(char::from(b));
            }
            _ => {
                const HEX: &[u8; 16] = b"0123456789ABCDEF";
                out.push('%');
                let hi = HEX.get(usize::from(b >> 4)).copied().unwrap_or(b'0');
                let lo = HEX.get(usize::from(b & 0x0f)).copied().unwrap_or(b'0');
                out.push(char::from(hi));
                out.push(char::from(lo));
            }
        }
    }
    out
}

#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "test assertions")]
mod tests {
    use serde_json::json;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    use super::*;

    #[tokio::test]
    async fn hbar_preflight_ok() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/api/v1/accounts/0.0.9001"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "balance": { "balance": 5000 },
                "max_automatic_token_associations": 0
            })))
            .mount(&server)
            .await;
        let mirror = HederaMirrorClient::connect(server.uri());
        let result = preflight_transfer(&mirror, "0.0.9001", "0.0.7001", "0.0.0", "1000")
            .await
            .unwrap();
        assert!(result.ok);
    }

    #[tokio::test]
    async fn hbar_preflight_insufficient() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/api/v1/accounts/0.0.9001"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "balance": { "balance": 500 },
                "max_automatic_token_associations": 0
            })))
            .mount(&server)
            .await;
        let mirror = HederaMirrorClient::connect(server.uri());
        let result = preflight_transfer(&mirror, "0.0.9001", "0.0.7001", "0.0.0", "1000")
            .await
            .unwrap();
        assert!(!result.ok);
        assert_eq!(result.reason.as_deref(), Some("insufficient_balance"));
    }
}