Skip to content

fix(sdk): parse amount_rtc and amount_i64 in CLI wallet balance (#8580) - #8596

Open
ArtyormSatori wants to merge 3 commits into
Scottcjn:mainfrom
ArtyormSatori:fix/cli-wallet-balance-canonical-amount-rtc-8580
Open

ArtyormSatori wants to merge 3 commits into
Scottcjn:mainfrom
ArtyormSatori:fix/cli-wallet-balance-canonical-amount-rtc-8580

Conversation

@ArtyormSatori

Copy link
Copy Markdown
Contributor

Summary

Fixes #8580: rustchain wallet balance <address> displaying 0 RTC for funded wallets when connected to canonical RustChain nodes.

Problem Statement

In sdk/python/rustchain_sdk/cli.py, _wallet_balance looked strictly for result.get("balance", 0).
However, canonical RustChain node balance responses (from GET /wallet/balance) return:

{
  "amount_i64": 1000000,
  "amount_rtc": 1.0,
  "miner_id": "RTC..."
}

Because the legacy "balance" key is absent, result.get("balance", 0) evaluated to 0, displaying Balance: 0 RTC even for wallets with positive balances.

Solution

  1. Canonical Field Resolution: Read amount_rtc as the primary balance field if present.
  2. Compatibility Aliases & Fallbacks:
    • Check amount_rtc first.
    • Fall back to balance_rtc or legacy balance if present.
    • Fall back to calculating amount_i64 / 1_000_000 if numeric amount_i64 is provided.
    • Display unavailable if no recognized balance field is returned in the payload instead of misleadingly printing 0 RTC.
  3. Unit Tests:
    • Added sdk/python/rustchain_sdk/tests/test_cli.py covering canonical amount_rtc, legacy balance/nonce, amount_i64 conversion, unavailable display, and the --json output flag.

Verification

Ran test suite against canonical mock endpoints:

sdk/python/rustchain_sdk/tests/test_cli.py::TestCliWalletBalance::test_canonical_amount_rtc_displayed PASSED
sdk/python/rustchain_sdk/tests/test_cli.py::TestCliWalletBalance::test_legacy_balance_compatibility PASSED
sdk/python/rustchain_sdk/tests/test_cli.py::TestCliWalletBalance::test_amount_i64_fallback_when_amount_rtc_missing PASSED
sdk/python/rustchain_sdk/tests/test_cli.py::TestCliWalletBalance::test_balance_unavailable_when_no_balance_fields PASSED
sdk/python/rustchain_sdk/tests/test_cli.py::TestCliWalletBalance::test_wallet_balance_json_flag PASSED

106 passed in 0.82s

Live CLI verification against https://rustchain.org:

  • Before: Balance: 0 RTC

  • After: Balance: 1.0 RTC

  • Claimant: @ArtyormSatori

  • Native RTC Wallet: RTC22ad34877d8c24944c0601a8594e9d8166e8bc56

@github-actions

Copy link
Copy Markdown
Contributor

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Non-doc PRs have a BCOS-L1 or BCOS-L2 label
  • Doc-only PRs are exempt from BCOS tier labels when they only touch docs/**, *.md, or common image/PDF files
  • New code files include an SPDX license header
  • You've tested your changes against the live node

Bounty tiers: Micro (1-10 RTC) | Standard (20-50) | Major (75-100) | Critical (100-150)

A maintainer will review your PR soon. Thanks for contributing!

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) size/M PR: 51-200 lines labels Oct 28, 2026

@Scottcjn Scottcjn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. The balance output needs to be strict, since people act on it:

  1. Accept only a finite numeric balance. Today true, nan, inf or a non-numeric string in amount_rtc/balance_rtc/balance prints as a balance. Converting amount_i64 through float also loses precision; use integer micro-RTC or Decimal.
  2. When both amount_rtc and amount_i64 are present and disagree, error out rather than silently picking one.
  3. A missing balance or nonce should exit non-zero instead of printing unavailable / inventing Nonce: 0 with exit 0. Also check that the response's miner_id matches the address that was asked for.

…d without precision loss

- Require finite numeric balance and reject boolean, NaN, Inf, and non-numeric strings
- Use Decimal arithmetic for amount_i64 parsing to avoid float precision loss
- Error out when amount_rtc and amount_i64 disagree
- Exit non-zero when balance or nonce is missing instead of inventing defaults
- Validate that response miner_id/address matches the requested query address
- Add comprehensive test suite covering all validation branches
@github-actions github-actions Bot added size/L PR: 201-500 lines and removed size/M PR: 51-200 lines labels Nov 3, 2026
@ArtyormSatori

Copy link
Copy Markdown
Contributor Author

Addressed all review points in b501c14:

  • Validated balance as finite numeric Decimal, rejecting boolean, NaN, Inf, and non-numeric strings. Converted amount_i64 using Decimal arithmetic to preserve micro-RTC precision without float loss.
  • Added strict consistency check when both amount_rtc and amount_i64 are present, exiting non-zero if they disagree.
  • Exited non-zero on missing balance or nonce instead of defaulting. Added validation to verify miner_id/address matches the requested address.
  • Added unit tests covering each case; all pass.

@elvergon8888 elvergon8888 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revisando el parseo de montos:

  1. Puntos correctos: La prioridad amount_rtc → balance → amount_i64 / 1e6 es correcta para mantener la retrocompatibilidad.
  2. Sugerencia (Testing): Falta un test unitario específico que pruebe la rama donde viene amount_i64 pero no viene amount_rtc, para asegurar que la división entre 1e6 se aplica correctamente y no se rompe en el futuro.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) size/L PR: 201-500 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SDK CLI prints 0 RTC for canonical /wallet/balance responses

4 participants