Phase 2: money

Lightning payments through LNbits, wallets, passes, credits, the
entitlement resolver and priority lanes.

- internal/entitle: pure resolver (one-off, own pass, group pass, group
  credits, own credits, free) with a table-driven suite and an invariant
  check over every input combination
- internal/billing: wallets, append-only ledger with dedupe keys, stacking
  passes, purchases, gift codes, referral reward
- LNbits client verified against v0.12.12 and v1.7.1 source
- Settlement by one conditional UPDATE: idempotent under any mix of
  webhooks, polls and the check button. Webhooks are unsigned, so every
  payment is confirmed with LNbits before it is credited
- Polling fallback, reconciliation of all unsettled invoices at startup,
  late payments still honored, no expiry while LNbits is unreachable
- Charge, quota and job are written in one transaction; failed, cancelled
  and undeliverable downloads are refunded exactly once
- Paid lane first with one slot of the fleet reserved for free users
- Daily download quota and daily volume cap, per-user and per-group rate
  limits
- Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice
  message that flips to paid, over-limit offer (passes or this file only),
  deep links from groups, skip the queue
- Fake LNbits server for tests; end-to-end money flows including a payment
  made while the core was down

Cashu redemption is behind an interface and not implemented.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 08:09:35 +00:00
co-authored by Claude Fable 5.2
parent 3b361c17a1
commit 0d50f453a0
42 changed files with 7040 additions and 140 deletions
+27
View File
@@ -213,3 +213,30 @@ func (s *Store) CountRunning(ctx context.Context) (int, error) {
err := s.row(ctx, `SELECT COUNT(*) FROM jobs WHERE status = 'running'`).Scan(&n)
return n, err
}
// CountRunningByLane counts running jobs, in total and in the free lane.
func (s *Store) CountRunningByLane(ctx context.Context) (total, free int, err error) {
rows, err := s.rows(ctx, `SELECT lane, COUNT(*) FROM jobs WHERE status = 'running' GROUP BY lane`)
if err != nil {
return 0, 0, err
}
defer rows.Close()
for rows.Next() {
var lane string
var n int
if err := rows.Scan(&lane, &n); err != nil {
return 0, 0, err
}
total += n
if lane == LaneFree {
free += n
}
}
return total, free, rows.Err()
}
// PromoteJob moves a queued free job to the paid lane.
func (s *Store) PromoteJob(ctx context.Context, id string) (bool, error) {
n, err := s.exec(ctx, `UPDATE jobs SET lane = 'paid' WHERE id = ? AND status = 'queued' AND lane = 'free'`, id)
return n == 1, err
}