Phase 3: own referral system (sats)

- Referral links carry a random 8 character base32 code (rf_<code>)
  built from the bot username from getMe. Old ref_<id> links still work
  but are never shown.
- The reward (REFERRAL_REWARD_SATS, old name REFERRAL_REWARD still read)
  is paid through the ledger once the invited user's first purchase
  settles, once per invited user, never for self-referrals.
- REFERRAL_DAILY_CAP limits what an inviter earns per UTC day; rewards
  above it wait as pending and are paid on a later day by the billing
  loop.
- /referral shows invited, converted, earned and pending, with Share,
  Copy link and QR code buttons. The Mini App Me page mirrors it.
- Migration 00008: users.ref_code (unique) and referral_rewards,
  backfilled from earlier referral credits in the ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 20:58:43 +00:00
co-authored by Claude Opus 5.5
parent 123cede7f3
commit f7ceadc462
30 changed files with 945 additions and 148 deletions
+78
View File
@@ -755,3 +755,81 @@ func TestGrantPassAndState(t *testing.T) {
t.Fatalf("group wallet = %+v, %v", g, err)
}
}
func TestReferralRewardWaitsForSettlementAndTheDailyCap(t *testing.T) {
r := newRig(t)
r.cfg.ReferralDailyCap = 300
r.b = r.newBilling()
ctx := context.Background()
const inviter = int64(600)
invitees := []int64{601, 602, 603}
for _, id := range append([]int64{inviter}, invitees...) {
if _, err := r.store.UpsertUser(ctx, id, "", "U", "en"); err != nil {
t.Fatal(err)
}
}
for _, id := range invitees {
if ok, err := r.store.SetReferrer(ctx, id, inviter); err != nil || !ok {
t.Fatalf("SetReferrer: %v %v", ok, err)
}
}
stats := func() db.ReferralStats {
st, err := r.store.ReferralStatsFor(ctx, inviter)
if err != nil {
t.Fatal(err)
}
return st
}
// An invoice that is created but not paid earns nothing.
unpaid := r.buy(invitees[0], billing.ProductPassDay, 0)
if r.balance(inviter) != 0 || stats().Converted != 0 {
t.Fatal("a reward before the payment settled")
}
// Paid: the reward arrives once, however often the webhook comes.
if err := r.ln.Pay(unpaid.PaymentHash); err != nil {
t.Fatal(err)
}
for i := 0; i < 3; i++ {
if err := r.b.HandleWebhook(ctx, unpaid.PaymentHash); err != nil {
t.Fatal(err)
}
}
if got := r.balance(inviter); got != 200 {
t.Fatalf("balance after one settled purchase = %d, want 200", got)
}
// The second reward of the day would pass the 300 sat cap: it waits.
_ = r.ln.Pay(r.buy(invitees[1], billing.ProductCredits, 1000).PaymentHash)
_ = r.ln.Pay(r.buy(invitees[2], billing.ProductCredits, 1000).PaymentHash)
if got := r.balance(inviter); got != 200 {
t.Fatalf("balance over the cap = %d, want 200", got)
}
if st := stats(); st.Invited != 3 || st.Converted != 3 || st.Earned != 200 || st.Pending != 400 {
t.Fatalf("stats = %+v", st)
}
if n, err := r.b.PayPendingReferrals(ctx); err != nil || n != 0 {
t.Fatalf("paid %d pending rewards on the same day (%v)", n, err)
}
// The next day one more fits under the cap, the day after the last.
r.advance(24 * time.Hour)
if n, _ := r.b.PayPendingReferrals(ctx); n != 1 {
t.Fatalf("paid %d the next day, want 1", n)
}
if n, _ := r.b.PayPendingReferrals(ctx); n != 0 {
t.Fatalf("paid %d more on the same day", n)
}
r.advance(24 * time.Hour)
if n, _ := r.b.PayPendingReferrals(ctx); n != 1 {
t.Fatalf("paid %d on the third day, want 1", n)
}
if st := stats(); st.Earned != 600 || st.Pending != 0 || r.balance(inviter) != 600 {
t.Fatalf("at the end: stats %+v, balance %d", st, r.balance(inviter))
}
// Later purchases of the same people earn nothing more.
_ = r.ln.Pay(r.buy(invitees[0], billing.ProductCredits, 1000).PaymentHash)
if r.balance(inviter) != 600 {
t.Fatal("a second purchase was rewarded")
}
r.booksBalance()
}