Skip to content

Reconcile exchange-reported balances against computed balances after an API import #759

Description

@NikolayMetchev

Problem

We already download the exchange's own balance for every signed API strategy and then throw the
information away. On Binance, sapi/v3/asset/getUserAsset is declared as a value endpoint
(BuiltInApiStrategies.kt, the binance() builder) purely so the fan-out can discover asset names —
the balances it returns are never compared with what the ledger computes.

On the live default database that hides a large error. Every Binance balance in
account_balance_materialized_view is wrong:

asset computed actual (API) asset computed actual
GBP 96 217.88 0 BNB −55.57 0.00019589
XRP 11 356.68 0 MATIC −33.68 0
NEXO 1 406.06 0 SOL −28.61 0
DAI 1 180.70 0 DOT −16.56 0
ADA 481.47 0 ETH −15.04 0
BUSD 278.10 0 LTC −11.63 0
VTHO 207.09 0.0001345 BTC −2.81 0
VET / FTT / LUNA / TWT / LINK −220 / −141.97 / −100.72 / −100 / −82.36 0

Nothing in the app says so. A user has to notice by eye.

Proposal

After an API import, compare the exchange-reported balance per asset with the computed account
balance and surface the per-asset delta (import summary and/or the account screen). Keep it generic
across signed exchange strategies rather than Binance-specific: the strategy config already knows
which endpoint carries balances, so a balanceMappings-style declaration (asset field, free/locked
amount fields, optional wallet label) plus an engine-side comparison covers Crypto.com and Kraken too.

Notes for whoever picks this up:

  • Balances are per wallet, not per exchange. getUserAsset is spot only — funding
    (sapi/v1/asset/get-funding-asset, POST), Simple Earn positions
    (sapi/v1/simple-earn/{flexible,locked}/position) and margin (sapi/v1/margin/account) are separate
    calls. On the default database all four are currently empty/dust, but a reconciliation that only
    looks at spot would report false deltas for anyone with funds in Earn.
  • The delta is diagnostic, not something to auto-correct — do not invent balancing transactions.

Found while investigating the Binance balance discrepancy; the data gaps behind it are #760 and #761.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions