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
+28 -4
View File
@@ -13,6 +13,7 @@ import (
"github.com/mymmrac/telego"
"github.com/noderunners/dlbot/internal/billing"
"github.com/noderunners/dlbot/internal/config"
"github.com/noderunners/dlbot/internal/db"
"github.com/noderunners/dlbot/internal/i18n"
@@ -29,6 +30,9 @@ type Deps struct {
Store *db.Store
Sched *sched.Scheduler
Service *service.Service
Billing *billing.Billing
// Cashu redeems ecash tokens. Nil turns that part of /redeem off.
Cashu billing.Cashu
Storage storage.Storage
I18n *i18n.Bundle
Log *slog.Logger
@@ -44,6 +48,8 @@ type Bot struct {
store *db.Store
sched *sched.Scheduler
svc *service.Service
bill *billing.Billing
cashu billing.Cashu
storage storage.Storage
tr *i18n.Bundle
log *slog.Logger
@@ -51,10 +57,12 @@ type Bot struct {
id int64
username string
events chan sched.Event
uploads chan struct{} // limits concurrent uploads to Telegram
bg sync.WaitGroup
baseCtx context.Context
events chan sched.Event
payEvents chan billing.Event
uploads chan struct{} // limits concurrent uploads to Telegram
rate *rateLimiter
bg sync.WaitGroup
baseCtx context.Context
statusMu sync.Mutex
statuses map[string]*statusState // by delivery ID
@@ -63,6 +71,9 @@ type Bot struct {
// Tunables, shortened in tests.
reconcileEvery time.Duration
statusMinGap time.Duration
// invoiceGrace is how long a paid purchase may lack an invoice message
// before the payer is told in a new message.
invoiceGrace time.Duration
}
// New creates the bot and learns its own username, which is needed to
@@ -84,6 +95,8 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
store: d.Store,
sched: d.Sched,
svc: d.Service,
bill: d.Billing,
cashu: d.Cashu,
storage: d.Storage,
tr: d.I18n,
log: d.Log,
@@ -91,12 +104,15 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
id: me.ID,
username: me.Username,
events: make(chan sched.Event, 1024),
payEvents: make(chan billing.Event, 256),
uploads: make(chan struct{}, 4),
rate: newRateLimiter(),
baseCtx: ctx,
statuses: map[string]*statusState{},
sending: map[string]bool{},
reconcileEvery: 5 * time.Second,
statusMinGap: 3 * time.Second,
invoiceGrace: 5 * time.Second,
}
d.Sched.Subscribe(func(ev sched.Event) {
select {
@@ -105,6 +121,13 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
// The reconcile loop picks up anything dropped here.
}
})
d.Billing.Subscribe(func(ev billing.Event) {
select {
case b.payEvents <- ev:
default:
// The reconcile loop picks up unfulfilled purchases.
}
})
return b, nil
}
@@ -116,6 +139,7 @@ func (b *Bot) Username() string { return b.username }
func (b *Bot) SetTimings(reconcile, statusGap time.Duration) {
b.reconcileEvery = reconcile
b.statusMinGap = statusGap
b.invoiceGrace = 4 * reconcile
}
// Run processes updates and job events until ctx ends, then waits for