fix: cap DCA allocations when ATM cash exceeds tracked balances #2

Closed
padreug wants to merge 1 commit from fix/cap-dca-allocations-sync-mismatch into main
Owner

Summary

  • Adds sync mismatch detection when total tracked client balances < transaction fiat amount
  • Caps each client's allocation to their remaining fiat balance (instead of proportional share of transaction)
  • Tracks and logs orphan sats that remain in source wallet due to sync mismatch

Problem

When the ATM has more physical cash than satmachineadmin tracks (sync mismatch), the proportional allocation formula gives each client more GTQ-equivalent sats than their actual remaining balance, causing all DCA payments to be refused.

Example:

  • Transaction: 2000 GTQ → 301,137 sats
  • Total tracked balances: 700 GTQ
  • Client A (690 GTQ): gets 98.57% share → 1971 GTQ worth → REFUSED (balance only 690 GTQ)

Solution

When sync mismatch is detected:

  1. Each client receives sats equivalent to their full remaining fiat balance (not proportional share)
  2. Orphan sats (transaction amount - sum of client balances) remain in source wallet
  3. Orphan amount is logged for reconciliation tracking

After fix:

  • Client A (690 GTQ): gets 690 × 150.57 = 103,893 sats ✓
  • Orphan: ~195,741 sats (logged, stays in source wallet)

Test Plan

  • Verify normal transactions (total_balances >= fiat_amount) work as before
  • Test sync mismatch scenario: each client should receive their full balance worth
  • Verify orphan sats are logged correctly
  • Check edge case: no clients with positive balance → no distributions

Fixes #1

🤖 Generated with Claude Code

## Summary - Adds sync mismatch detection when total tracked client balances < transaction fiat amount - Caps each client's allocation to their remaining fiat balance (instead of proportional share of transaction) - Tracks and logs orphan sats that remain in source wallet due to sync mismatch ## Problem When the ATM has more physical cash than satmachineadmin tracks (sync mismatch), the proportional allocation formula gives each client more GTQ-equivalent sats than their actual remaining balance, causing all DCA payments to be refused. **Example:** - Transaction: 2000 GTQ → 301,137 sats - Total tracked balances: 700 GTQ - Client A (690 GTQ): gets 98.57% share → 1971 GTQ worth → **REFUSED** (balance only 690 GTQ) ## Solution When sync mismatch is detected: 1. Each client receives sats equivalent to their **full remaining fiat balance** (not proportional share) 2. Orphan sats (transaction amount - sum of client balances) remain in source wallet 3. Orphan amount is logged for reconciliation tracking **After fix:** - Client A (690 GTQ): gets 690 × 150.57 = 103,893 sats ✓ - Orphan: ~195,741 sats (logged, stays in source wallet) ## Test Plan - [ ] Verify normal transactions (total_balances >= fiat_amount) work as before - [ ] Test sync mismatch scenario: each client should receive their full balance worth - [ ] Verify orphan sats are logged correctly - [ ] Check edge case: no clients with positive balance → no distributions Fixes #1 🤖 Generated with [Claude Code](https://claude.com/claude-code)
padreug added 1 commit 2025-12-31 22:42:12 +00:00
fix: cap DCA allocations when ATM cash exceeds tracked balances
Some checks failed
CI / lint (pull_request) Has been cancelled
CI / tests (3.10) (pull_request) Has been cancelled
CI / tests (3.9) (pull_request) Has been cancelled
5e8110f322
When total tracked client balances < transaction fiat amount (sync
mismatch), the proportional allocation formula was giving each client
more GTQ-equivalent sats than their actual remaining balance, causing
all payments to be refused.

Changes:
- Add sync mismatch detection in calculate_distribution_amounts()
- Cap allocations to remaining balance when sync mismatch detected
- Track orphan sats (unallocated due to sync mismatch)
- Update return type to tuple (distributions, orphan_sats)
- Log orphan amounts for reconciliation tracking

Fixes #1

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Author
Owner

Closing this PR - the sync mismatch fix has been implemented directly on main in commit 545a028.

The implementation:

  • Uses the extracted pure functions from calculations.py (tested with 22 unit tests)
  • Maintains all the same logic: sync mismatch detection, capped allocations, orphan sats tracking
  • Cleaner code structure by leveraging the tested calculation functions

All acceptance criteria from #1 are met.

Closing this PR - the sync mismatch fix has been implemented directly on main in commit 545a028. The implementation: - Uses the extracted pure functions from `calculations.py` (tested with 22 unit tests) - Maintains all the same logic: sync mismatch detection, capped allocations, orphan sats tracking - Cleaner code structure by leveraging the tested calculation functions All acceptance criteria from #1 are met.
padreug closed this pull request 2026-01-11 14:59:12 +00:00
Some checks failed
CI / lint (pull_request) Has been cancelled
CI / tests (3.10) (pull_request) Has been cancelled
CI / tests (3.9) (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: aiolabs/satmachineadmin#2
No description provided.