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
+4 -8
View File
@@ -3,7 +3,6 @@ package bot
import (
"context"
"errors"
"strconv"
"strings"
"time"
@@ -302,13 +301,8 @@ func (b *Bot) cmdStart(ctx context.Context, m *telego.Message, act actor) {
case payload == "buy":
b.showBuy(ctx, m.Chat.ID, act)
return
case strings.HasPrefix(payload, "ref_"):
// Only somebody who is new to the bot can be referred.
if referrer, err := strconv.ParseInt(strings.TrimPrefix(payload, "ref_"), 10, 64); err == nil && act.user.IsNew {
if _, err := b.store.SetReferrer(ctx, act.user.ID, referrer); err != nil {
b.log.Error("set referrer", "user_id", act.user.ID, "err", err)
}
}
default:
b.applyReferral(ctx, act, payload)
}
// Somebody new picks a language under the welcome. It starts in the
// language of their Telegram app, which is usually right.
@@ -528,6 +522,8 @@ func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
b.cbRetryLookup(ctx, q, act, id)
case cbUpgrade:
b.cbShowUpgrade(ctx, q, act)
case cbReferralQR:
b.cbSendReferralQR(ctx, q, act)
case cbPersonal:
b.cbPersonalSetting(ctx, q, act, id, arg)
case cbSkipQueue: