aspens 0.4.2

Aspens crosschain trading SDK
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
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
use alloy::primitives::{Address, Uint};
use alloy::providers::{Provider, ProviderBuilder};
use alloy::signers::local::PrivateKeySigner;
use alloy_chains::NamedChain;
use comfy_table::{presets::UTF8_BORDERS_ONLY, Table};
use eyre::Result;
use std::collections::HashMap;
use tracing::{info, warn};
use url::Url;

use super::{MidribV2, IERC20};
use crate::chain_client::{ChainClient, ARCH_SOLANA};
use crate::commands::config::config_pb::{Chain, Configuration, GetConfigResponse};
use crate::wallet::{load_trader_wallet, CurveType, Wallet};

/// Represents a unique token across all chains
#[derive(Debug, Clone)]
struct TokenInfo {
    symbol: String,
    decimals: u32,
}

/// Balance information for a token on a specific chain
#[derive(Debug)]
struct ChainBalance {
    chain_network: String,
    wallet_balance: String,
    available_balance: String,
    locked_balance: String,
}

/// Native gas token balance for a chain
#[derive(Debug)]
struct NativeBalance {
    chain_network: String,
    balance: String,
}

/// Aggregated balance for a single token across all chains
#[derive(Debug)]
struct TokenBalance {
    token_info: TokenInfo,
    chain_balances: Vec<ChainBalance>,
}

/// Extract all unique tokens from configuration chains
fn extract_all_tokens_from_config(config: &Configuration) -> HashMap<String, TokenInfo> {
    let mut tokens: HashMap<String, TokenInfo> = HashMap::new();

    for chain in &config.chains {
        for (symbol, token) in &chain.tokens {
            tokens.entry(symbol.clone()).or_insert_with(|| TokenInfo {
                symbol: symbol.clone(),
                decimals: token.decimals,
            });
        }
    }

    tokens
}

/// Format balance with decimals for human-readable display
fn format_balance_with_decimals(balance_str: &str, decimals: u32) -> String {
    if balance_str == "error" || balance_str == "not deployed" {
        return balance_str.to_string();
    }

    match balance_str.parse::<u128>() {
        Ok(balance) => {
            let divisor = 10_u128.pow(decimals);
            let integer_part = balance / divisor;
            let fractional_part = balance % divisor;

            // Format with proper decimal places
            format!(
                "{}.{:0width$}",
                integer_part,
                fractional_part,
                width = decimals as usize
            )
        }
        Err(_) => balance_str.to_string(),
    }
}

/// Display all token balances in a single table grouped by chain
fn display_all_token_balances(
    all_token_balances: &[TokenBalance],
    native_balances: &[NativeBalance],
) -> String {
    if all_token_balances.is_empty() {
        return String::new();
    }

    // Collect all unique chain networks across all tokens and sort them
    let mut all_chains: Vec<String> = Vec::new();
    for token_balance in all_token_balances {
        for chain_balance in &token_balance.chain_balances {
            if !all_chains.contains(&chain_balance.chain_network) {
                all_chains.push(chain_balance.chain_network.clone());
            }
        }
    }
    all_chains.sort();

    let mut output = String::new();
    output.push('\n');

    output
        .push_str("═══════════════════════════════════════════════════════════════════════════\n");
    output.push_str("                                 BALANCES\n");
    output.push_str(
        "═══════════════════════════════════════════════════════════════════════════\n\n",
    );

    let mut table = Table::new();
    table.load_preset(UTF8_BORDERS_ONLY);

    // Set header: Token, Wallet, Deposited, Locked
    table.set_header(vec!["Token", "Wallet", "Deposited", "Locked"]);

    // Group by chain - add rows for each chain section
    for (chain_idx, chain) in all_chains.iter().enumerate() {
        // Add chain header row (spans conceptually as a section header)
        if chain_idx > 0 {
            // Add separator row between chains
            table.add_row(vec!["", "", "", ""]);
        }

        // Add chain name as a section header
        table.add_row(vec![
            format!("── {} ──", chain),
            String::new(),
            String::new(),
            String::new(),
        ]);

        // Add native gas balance for this chain
        if let Some(native) = native_balances.iter().find(|nb| nb.chain_network == *chain) {
            let gas_balance = format_balance_with_decimals(&native.balance, 18);
            table.add_row(vec![
                "GAS".to_string(),
                gas_balance,
                String::new(),
                String::new(),
            ]);
        }

        // Add tokens for this chain
        for token_balance in all_token_balances {
            // Find if this token exists on this chain
            if let Some(chain_balance) = token_balance
                .chain_balances
                .iter()
                .find(|cb| cb.chain_network == *chain)
            {
                let decimals = token_balance.token_info.decimals;
                let symbol = &token_balance.token_info.symbol;

                let wallet = format_balance_with_decimals(&chain_balance.wallet_balance, decimals);
                let deposited =
                    format_balance_with_decimals(&chain_balance.available_balance, decimals);
                let locked = format_balance_with_decimals(&chain_balance.locked_balance, decimals);

                table.add_row(vec![symbol.clone(), wallet, deposited, locked]);
            }
        }
    }

    output.push_str(&table.to_string());
    output.push('\n');

    output
}

/// Query a token balance on a specific chain using a curve-aware client.
///
/// Used by `balance_from_config_with_wallet`. For Solana chains, locked/deposited
/// balances come from the trade program; for Solana it queries the on-chain
/// `UserBalance` PDA via the Midrib program.
#[cfg(feature = "solana")]
async fn solana_user_balance(
    chain: &Chain,
    token: &crate::commands::config::config_pb::Token,
    owner_address: &str,
) -> (String, String) {
    use solana_sdk::pubkey::Pubkey;
    use std::str::FromStr;

    let (program_id, instance) = match crate::solana::client::resolve_program_and_instance(chain) {
        Ok(v) => v,
        Err(_) => return ("not deployed".to_string(), "not deployed".to_string()),
    };
    let user = match Pubkey::from_str(owner_address) {
        Ok(p) => p,
        Err(_) => return ("bad address".to_string(), "bad address".to_string()),
    };
    let mint = match Pubkey::from_str(&token.address) {
        Ok(p) => p,
        Err(_) => return ("bad mint".to_string(), "bad mint".to_string()),
    };
    match crate::solana::client::fetch_user_balance(
        &chain.rpc_url,
        &instance,
        &user,
        &mint,
        &program_id,
    )
    .await
    {
        Ok((deposited, locked)) => {
            let available = deposited.saturating_sub(locked);
            (available.to_string(), locked.to_string())
        }
        Err(e) => {
            warn!(
                "Solana UserBalance fetch failed on {}: {}",
                chain.network, e
            );
            ("error".to_string(), "error".to_string())
        }
    }
}

#[cfg(not(feature = "solana"))]
async fn solana_user_balance(
    _chain: &Chain,
    _token: &crate::commands::config::config_pb::Token,
    _owner_address: &str,
) -> (String, String) {
    (
        "solana feature disabled".to_string(),
        "solana feature disabled".to_string(),
    )
}

async fn query_token_balance_via_client(
    chain: &Chain,
    token_symbol: &str,
    owner_address: &str,
) -> ChainBalance {
    let chain_network = chain.network.clone();
    let token = match chain.tokens.get(token_symbol) {
        Some(t) => t,
        None => {
            return ChainBalance {
                chain_network,
                wallet_balance: "missing token".to_string(),
                available_balance: "missing token".to_string(),
                locked_balance: "missing token".to_string(),
            };
        }
    };

    let client = match ChainClient::from_chain_config(chain) {
        Ok(c) => c,
        Err(e) => {
            warn!("Failed to build client for {}: {}", chain_network, e);
            return ChainBalance {
                chain_network,
                wallet_balance: "error".to_string(),
                available_balance: "error".to_string(),
                locked_balance: "error".to_string(),
            };
        }
    };

    let wallet_balance = client
        .token_balance(token, owner_address)
        .await
        .map_or_else(
            |e| {
                warn!("Failed to get wallet balance on {}: {}", chain_network, e);
                "error".to_string()
            },
            |v| v.to_string(),
        );

    // Locked/deposited balances come from the trade contract.
    // Solana side is not yet wired up; EVM uses MidribV2.
    let (available_balance, locked_balance) =
        if chain.architecture.eq_ignore_ascii_case(ARCH_SOLANA) {
            solana_user_balance(chain, token, owner_address).await
        } else {
            let contract_address = chain
                .trade_contract
                .as_ref()
                .map(|tc| tc.address.clone())
                .unwrap_or_default();

            if contract_address.is_empty() {
                ("not deployed".to_string(), "not deployed".to_string())
            } else {
                let named_chain =
                    NamedChain::try_from(chain.chain_id as u64).unwrap_or(NamedChain::BaseSepolia);
                // Reuse existing EVM helpers — they need a privkey to derive the address,
                // but we already have the address. Use *_for_address variants.
                let owner: Address = match owner_address.parse() {
                    Ok(a) => a,
                    Err(_) => {
                        return ChainBalance {
                            chain_network,
                            wallet_balance,
                            available_balance: "bad address".to_string(),
                            locked_balance: "bad address".to_string(),
                        };
                    }
                };
                let available = call_get_balance_for_address(
                    named_chain,
                    &chain.rpc_url,
                    &token.address,
                    &contract_address,
                    owner,
                )
                .await
                .map_or_else(
                    |e| {
                        warn!(
                            "Failed to get available balance on {}: {}",
                            chain_network, e
                        );
                        "error".to_string()
                    },
                    |v| v.to_string(),
                );
                let locked = call_get_locked_balance_for_address(
                    &chain.rpc_url,
                    &token.address,
                    &contract_address,
                    owner,
                )
                .await
                .map_or_else(
                    |e| {
                        warn!("Failed to get locked balance on {}: {}", chain_network, e);
                        "error".to_string()
                    },
                    |v| v.to_string(),
                );
                (available, locked)
            }
        };

    ChainBalance {
        chain_network,
        wallet_balance,
        available_balance,
        locked_balance,
    }
}

/// Curve-agnostic config-driven balance function.
///
/// Dispatches per-chain based on `chain.architecture`:
/// - EVM chains query via Alloy + MidribV2
/// - Solana chains query via solana-client (SOL + SPL); deposited/locked
///   balances are scaffolded as "not deployed" until the on-chain program lands
pub async fn balance_from_config_with_wallet(
    config: GetConfigResponse,
    wallet: &Wallet,
) -> Result<()> {
    balance_from_config_with_wallets(config, &[wallet]).await
}

/// Pick the first wallet matching `chain`'s curve. Returns `None` when no
/// caller-supplied wallet matches — the chain's rows are reported as
/// "no wallet" rather than erroring out.
fn select_wallet_for_chain<'a>(chain: &Chain, wallets: &'a [&'a Wallet]) -> Option<&'a Wallet> {
    let wanted = crate::wallet::chain_curve(chain);
    wallets.iter().copied().find(|w| w.curve() == wanted)
}

/// Curve-aware, multi-wallet config-driven balance function.
///
/// Each chain in the config gets matched to a wallet with the same curve
/// (EVM chains → `Wallet::Evm`, Solana chains → `Wallet::Solana`). A chain
/// with no matching wallet is reported as "no wallet" rather than failing
/// the whole call.
pub async fn balance_from_config_with_wallets(
    config: GetConfigResponse,
    wallets: &[&Wallet],
) -> Result<()> {
    let configuration = config
        .config
        .ok_or_else(|| eyre::eyre!("No configuration found in response"))?;

    let tokens = extract_all_tokens_from_config(&configuration);

    if tokens.is_empty() {
        info!("No tokens found in configuration");
        return Ok(());
    }

    info!("Found {} unique token(s) across all chains", tokens.len());

    let mut all_token_balances: Vec<TokenBalance> = Vec::new();

    for (symbol, token_info) in tokens {
        let mut chain_balances = Vec::new();

        for chain in &configuration.chains {
            if !chain.tokens.contains_key(&symbol) {
                continue;
            }
            let cb = match select_wallet_for_chain(chain, wallets) {
                Some(w) => query_token_balance_via_client(chain, &symbol, &w.address()).await,
                None => ChainBalance {
                    chain_network: chain.network.clone(),
                    wallet_balance: "no wallet".to_string(),
                    available_balance: "no wallet".to_string(),
                    locked_balance: "no wallet".to_string(),
                },
            };
            chain_balances.push(cb);
        }

        all_token_balances.push(TokenBalance {
            token_info: token_info.clone(),
            chain_balances,
        });
    }

    all_token_balances.sort_by(|a, b| a.token_info.symbol.cmp(&b.token_info.symbol));

    // Native gas balances per chain
    let mut native_balances: Vec<NativeBalance> = Vec::new();
    let mut seen: HashMap<String, ()> = HashMap::new();
    for chain in &configuration.chains {
        if seen.contains_key(&chain.network) {
            continue;
        }
        seen.insert(chain.network.clone(), ());

        let owner_address = match select_wallet_for_chain(chain, wallets) {
            Some(w) => w.address(),
            None => {
                native_balances.push(NativeBalance {
                    chain_network: chain.network.clone(),
                    balance: "no wallet".to_string(),
                });
                continue;
            }
        };

        let client = match ChainClient::from_chain_config(chain) {
            Ok(c) => c,
            Err(e) => {
                warn!("Failed to build client for {}: {}", chain.network, e);
                native_balances.push(NativeBalance {
                    chain_network: chain.network.clone(),
                    balance: "error".to_string(),
                });
                continue;
            }
        };
        let balance = client.native_balance(&owner_address).await.map_or_else(
            |e| {
                warn!("Failed to get native balance on {}: {}", chain.network, e);
                "error".to_string()
            },
            |v| v.to_string(),
        );
        native_balances.push(NativeBalance {
            chain_network: chain.network.clone(),
            balance,
        });
    }

    let output = display_all_token_balances(&all_token_balances, &native_balances);
    info!("{}", output);

    Ok(())
}

/// New config-driven balance function
/// Legacy EVM-privkey entry point. Kept for existing CLI/REPL call sites.
///
/// Builds an EVM wallet from `privkey` and opportunistically also loads a
/// Solana trader wallet from `TRADER_PRIVKEY_SOLANA` (when present and the
/// `solana` feature is on), then delegates to
/// [`balance_from_config_with_wallets`]. Chains whose architecture has no
/// matching wallet are reported as "no wallet" in the table.
pub async fn balance_from_config(config: GetConfigResponse, privkey: String) -> Result<()> {
    let evm = Wallet::from_evm_hex(&privkey)?;
    let solana = load_trader_wallet(CurveType::Ed25519).ok();
    let mut wallets: Vec<&Wallet> = vec![&evm];
    if let Some(ref s) = solana {
        wallets.push(s);
    }
    balance_from_config_with_wallets(config, &wallets).await
}

pub async fn balance(
    base_chain_rpc_url: String,
    base_chain_usdc_token_address: String,
    quote_chain_rpc_url: String,
    quote_chain_usdc_token_address: String,
    base_chain_contract_address: String,
    quote_chain_contract_address: String,
    privkey: String,
) -> Result<()> {
    let base_wallet_balance = call_get_erc20_balance(
        NamedChain::BaseGoerli,
        &base_chain_rpc_url,
        &base_chain_usdc_token_address,
        &privkey,
    )
    .await
    .map_or("error".to_string(), |v| v.to_string());
    let base_available_balance = call_get_balance(
        NamedChain::BaseGoerli,
        &base_chain_rpc_url,
        &base_chain_usdc_token_address,
        &base_chain_contract_address,
        &privkey,
    )
    .await
    .map_or("error".to_string(), |v| v.to_string());
    let base_locked_balance = call_get_locked_balance(
        &base_chain_rpc_url,
        &base_chain_usdc_token_address,
        &base_chain_contract_address,
        &privkey,
    )
    .await
    .map_or("error".to_string(), |v| v.to_string());
    let quote_wallet_balance = call_get_erc20_balance(
        NamedChain::BaseSepolia,
        &quote_chain_rpc_url,
        &quote_chain_usdc_token_address,
        &privkey,
    )
    .await
    .map_or("error".to_string(), |v| v.to_string());
    let quote_available_balance = call_get_balance(
        NamedChain::BaseSepolia,
        &quote_chain_rpc_url,
        &quote_chain_usdc_token_address,
        &quote_chain_contract_address,
        &privkey,
    )
    .await
    .map_or("error".to_string(), |v| v.to_string());
    let quote_locked_balance = call_get_locked_balance(
        &quote_chain_rpc_url,
        &quote_chain_usdc_token_address,
        &quote_chain_contract_address,
        &privkey,
    )
    .await
    .map_or("error".to_string(), |v| v.to_string());
    let balance_table = balance_table(
        vec!["USDC", "Base Chain", "Quote Chain"],
        &base_wallet_balance,
        &base_available_balance,
        &base_locked_balance,
        &quote_wallet_balance,
        &quote_available_balance,
        &quote_locked_balance,
    );
    info!("\n{}", balance_table);
    Ok(())
}

pub async fn call_get_balance(
    chain: NamedChain,
    rpc_url: &str,
    token_address: &str,
    contract_address: &str,
    privkey: &str,
) -> Result<Uint<256, 4>> {
    let contract_addr: Address = contract_address.parse()?;
    let token_addr: Address = token_address.parse()?;
    let signer = privkey.parse::<PrivateKeySigner>()?;
    let depositer_address: Address = signer.address();
    let rpc_url = Url::parse(rpc_url)?;
    let provider = ProviderBuilder::new()
        .with_chain(chain)
        .connect_http(rpc_url);
    let contract = MidribV2::new(contract_addr, &provider);
    let result = contract
        .tradeBalance(depositer_address, token_addr)
        .call()
        .await?;
    Ok(result)
}

pub async fn call_get_locked_balance(
    rpc_url: &str,
    token_address: &str,
    contract_address: &str,
    privkey: &str,
) -> Result<Uint<256, 4>> {
    let contract_addr: Address = contract_address.parse()?;
    let token_addr: Address = token_address.parse()?;
    let signer = privkey.parse::<PrivateKeySigner>()?;
    let depositer_address: Address = signer.address();
    let rpc_url = Url::parse(rpc_url)?;
    let provider = ProviderBuilder::new().connect_http(rpc_url);
    let contract = MidribV2::new(contract_addr, &provider);
    let result = contract
        .lockedTradeBalance(depositer_address, token_addr)
        .call()
        .await?;
    Ok(result)
}

/// Variant of `call_get_balance` that takes an `Address` directly instead of
/// deriving it from a private key. Used by curve-aware balance queries.
pub async fn call_get_balance_for_address(
    chain: NamedChain,
    rpc_url: &str,
    token_address: &str,
    contract_address: &str,
    depositer_address: Address,
) -> Result<Uint<256, 4>> {
    let contract_addr: Address = contract_address.parse()?;
    let token_addr: Address = token_address.parse()?;
    let rpc_url = Url::parse(rpc_url)?;
    let provider = ProviderBuilder::new()
        .with_chain(chain)
        .connect_http(rpc_url);
    let contract = MidribV2::new(contract_addr, &provider);
    let result = contract
        .tradeBalance(depositer_address, token_addr)
        .call()
        .await?;
    Ok(result)
}

/// Variant of `call_get_locked_balance` that takes an `Address` directly.
pub async fn call_get_locked_balance_for_address(
    rpc_url: &str,
    token_address: &str,
    contract_address: &str,
    depositer_address: Address,
) -> Result<Uint<256, 4>> {
    let contract_addr: Address = contract_address.parse()?;
    let token_addr: Address = token_address.parse()?;
    let rpc_url = Url::parse(rpc_url)?;
    let provider = ProviderBuilder::new().connect_http(rpc_url);
    let contract = MidribV2::new(contract_addr, &provider);
    let result = contract
        .lockedTradeBalance(depositer_address, token_addr)
        .call()
        .await?;
    Ok(result)
}

pub async fn call_get_erc20_balance(
    chain: NamedChain,
    rpc_url: &str,
    token_address: &str,
    privkey: &str,
) -> Result<Uint<256, 4>> {
    let token_addr: Address = token_address.parse()?;
    let signer = privkey.parse::<PrivateKeySigner>()?;
    let depositer_address: Address = signer.address();
    let rpc_url = Url::parse(rpc_url)?;
    let provider = ProviderBuilder::new()
        .with_chain(chain)
        .connect_http(rpc_url);
    let contract = IERC20::new(token_addr, &provider);
    let result = contract.balanceOf(depositer_address).call().await?;
    Ok(result)
}

pub async fn call_get_native_balance(rpc_url: &str, privkey: &str) -> Result<Uint<256, 4>> {
    let signer = privkey.parse::<PrivateKeySigner>()?;
    let address = signer.address();
    call_get_native_balance_for_address(rpc_url, address).await
}

pub async fn call_get_native_balance_for_address(
    rpc_url: &str,
    address: Address,
) -> Result<Uint<256, 4>> {
    let rpc_url = Url::parse(rpc_url)?;
    let provider = ProviderBuilder::new().connect_http(rpc_url);
    let balance = provider.get_balance(address).await?;
    Ok(balance)
}

pub async fn call_get_erc20_balance_for_address(
    rpc_url: &str,
    token_address: &str,
    holder: Address,
) -> Result<Uint<256, 4>> {
    let token_addr: Address = token_address.parse()?;
    let rpc_url = Url::parse(rpc_url)?;
    let provider = ProviderBuilder::new().connect_http(rpc_url);
    let contract = IERC20::new(token_addr, &provider);
    let result = contract.balanceOf(holder).call().await?;
    Ok(result)
}

pub fn format_balance(value: Uint<256, 4>, decimals: u32) -> String {
    let s = value.to_string();
    if decimals == 0 {
        return s;
    }
    let dec = decimals as usize;
    if s.len() <= dec {
        let padded = format!("{:0>width$}", s, width = dec + 1);
        let (int_part, frac_part) = padded.split_at(padded.len() - dec);
        format!("{}.{}", int_part, frac_part)
    } else {
        let (int_part, frac_part) = s.split_at(s.len() - dec);
        format!("{}.{}", int_part, frac_part)
    }
}

pub fn balance_table(
    header: Vec<&str>,
    base_wallet_bal: &str,
    base_available_bal: &str,
    base_locked_bal: &str,
    quote_wallet_bal: &str,
    quote_available_bal: &str,
    quote_locked_bal: &str,
) -> Table {
    let mut table = Table::new();

    table
        .load_preset(UTF8_BORDERS_ONLY)
        .set_header(header)
        .add_row(vec!["Wallet Balance", base_wallet_bal, quote_wallet_bal])
        .add_row(vec![
            "Available Balance",
            base_available_bal,
            quote_available_bal,
        ])
        .add_row(vec!["Locked Balance", base_locked_bal, quote_locked_bal]);

    table
}