fix(kraken): a crypto ledger row moves units, not cash

Merged
DG
2mo ago
+586-26

Problem

Kraken's ledger reports a coin deposit, withdrawal, staking reward or Earn
payout as a movement of units. The processor recorded each one as a cash
Transaction: the quantity was lost, the holdings calculator had nothing to
reverse, and the account's cash moved by an amount that never existed. Every
coin that ever arrived from outside, or left, was mis-stated twice over -- once
as phantom cash, once as a holding that never changed.

Dust sweeps ("convert small balances") were skipped outright, so a swept
position stayed on the books at its pre-sweep quantity forever.

Change

  • A crypto ledger row becomes a Trade carrying the quantity, priced from the
    stored price on its date, with a zero cash leg. Deposits and withdrawals are
    labelled as transfers, since a coin arriving from outside has a cost nothing
    here knows.
  • Both halves of a dust sweep are imported as the unit movements they are.
  • A crypto row carries its fee inside the quantity and has no second half, so
    once imported it is not revisited -- the caller's early return lets deposits
    and withdrawals through for the fee a fiat row may still be owed, which
    would otherwise re-create the crypto row on every sync and fail on its
    external id.
  • An account synced before this change holds each coin movement as the old
    Transaction. That row is replaced by the trade on the next sync, so an
    install does not have to re-import to be right. A row somebody has edited or
    matched into a transfer is theirs and stays. The security is resolved before
    anything is removed, and the removal and the trade are one transaction.
  • A trade recorded before its price existed carries zero and price_missing;
    it is priced on a later sync once a price on its date turns up. The flag is
    read in the same bulk pluck as everything else, by subselect, so the sync
    stays at one query on entries.
  • The spot-price fallback can no longer raise out of the rescue that reaches
    for it.
  • A crypto row that moves nothing -- a correction row, a deposit the fee
    consumed whole, or a reward below the eight decimals trades.qty holds --
    is skipped. It used to become a zero-quantity trade, which Trade accepts
    since it only checks that qty is present; a live account had one per weekly
    staking reward of a dust position.

Validation

  • test/models/kraken_account, test/models/kraken_item, the Kraken provider
    and adapter tests: 74 runs, 260 assertions, 0 failures, on current main.
  • New cases: a BTC deposit becomes a trade carrying the quantity; a withdrawal
    gives up units; the price is the stored one on the day; both halves of a dust
    sweep are imported and are internal movements; an already-imported crypto row
    is not re-created; a legacy transaction is replaced by the trade; one in a
    transfer, or edited, is left alone; a trade recorded without a price is
    priced on a later sync; a row that moves nothing creates no trade. The
    re-creation, replacement and zero-quantity cases fail without their fixes.
  • The two query-count tests still hold: a second pass issues one bulk read.
  • bin/rubocop clean.

Against a live account

The re-creation failure was found in production: four coin rows failing on
every sync with External has already been taken, surfaced by the debug log.
Fixed there first; the sync has been clean since. The zero-quantity case was
found the same way, after a fresh import: the guard compares at the column's
scale because the raw figure was not zero, only what the column could keep.

Notes

Sits beside the fee handling from #3759 in ledger_processor.rb, so it waited
for that to merge; main is merged in and the diff is only this change.

Existing Kraken accounts pick the new shape up on their next sync (legacy rows
are replaced in place); a re-import is not required, but is the quickest way
to a clean history.

Summary by CodeRabbit

  • New Features

    • Kraken crypto ledger movements, including dust-sweep activity, are recorded as quantity-based trades rather than cash transactions; fiat movements remain cash transactions.
    • Trades use historical prices when available and can be repriced on a later sync when prices become available.
    • Dust-sweep entries display distinct activity labels.
  • Bug Fixes

    • Existing legacy crypto entries are replaced only when unmodified and not matched to a transfer.