From f3ffcf5456c6556a61affa1eea71b6b90b31af99 Mon Sep 17 00:00:00 2001 From: ahmadghoniem Date: Fri, 24 Apr 2026 23:37:44 +0300 Subject: [PATCH] Fix gateway balance selection by domain in check_balance --- src/omniclaw/protocols/nanopayments/client.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/omniclaw/protocols/nanopayments/client.py b/src/omniclaw/protocols/nanopayments/client.py index 008aa77..4cc5b3f 100644 --- a/src/omniclaw/protocols/nanopayments/client.py +++ b/src/omniclaw/protocols/nanopayments/client.py @@ -754,7 +754,7 @@ async def check_balance( """ await self.get_supported() - _caip2_to_circle_domain_id(network) + expected_domain = _caip2_to_circle_domain_id(network) body: dict[str, Any] = { "token": "USDC", "sources": [ @@ -789,7 +789,19 @@ async def check_balance( formatted_total = "0 USDC" formatted_available = "0 USDC" if balances: - bal = balances[0] + depositor_lc = address.lower() + + # Circle may return multiple domains; never assume index 0 is the requested network. + by_depositor = [ + b + for b in balances + if str(b.get("depositor", "")).lower() == depositor_lc or not b.get("depositor") + ] + by_domain = [ + b for b in by_depositor if int(_to_int(b.get("domain", 0))) == expected_domain + ] + bal = by_domain[0] if by_domain else (by_depositor[0] if by_depositor else balances[0]) + # Circle returns "balance" field with string amount, no separate available field balance_str = bal.get("balance", "0") total = _to_int(balance_str)