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
+25 -3
View File
@@ -2,11 +2,11 @@ package db
import "context"
const userCols = `id, username, first_name, lang, lang_override, banned, created_at, last_seen_at`
const userCols = `id, username, first_name, lang, lang_override, banned, created_at, last_seen_at, referred_by`
func scanUser(sc interface{ Scan(...any) error }) (*User, error) {
var u User
err := sc.Scan(&u.ID, &u.Username, &u.FirstName, &u.Lang, &u.LangOverride, &u.Banned, &u.CreatedAt, &u.LastSeenAt)
err := sc.Scan(&u.ID, &u.Username, &u.FirstName, &u.Lang, &u.LangOverride, &u.Banned, &u.CreatedAt, &u.LastSeenAt, &u.ReferredBy)
if err != nil {
return nil, notFound(err)
}
@@ -29,10 +29,32 @@ func (s *Store) UpsertUser(ctx context.Context, id int64, username, firstName, l
if err != nil {
return nil, err
}
return s.GetUser(ctx, id)
u, err := s.GetUser(ctx, id)
if err == nil {
u.IsNew = u.CreatedAt == now && u.LastSeenAt == now
}
return u, err
}
// GetUser loads a user by Telegram ID.
func (s *Store) GetUser(ctx context.Context, id int64) (*User, error) {
return scanUser(s.row(ctx, `SELECT `+userCols+` FROM users WHERE id = ?`, id))
}
// SetReferrer records who invited a user. It only works once, and never
// for a self-invite. It reports whether the referrer was stored.
func (s *Store) SetReferrer(ctx context.Context, userID, referrerID int64) (bool, error) {
if userID == referrerID || referrerID == 0 {
return false, nil
}
n, err := s.exec(ctx, `UPDATE users SET referred_by = ? WHERE id = ? AND referred_by = 0
AND EXISTS (SELECT 1 FROM (SELECT id FROM users WHERE id = ?) r)`, referrerID, userID, referrerID)
return n == 1, err
}
// CountReferrals counts the users invited by a user.
func (s *Store) CountReferrals(ctx context.Context, referrerID int64) (int, error) {
var n int
err := s.row(ctx, `SELECT COUNT(*) FROM users WHERE referred_by = ?`, referrerID).Scan(&n)
return n, err
}