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
+55 -7
View File
@@ -7,6 +7,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"strings"
"testing"
"time"
@@ -948,24 +949,47 @@ func TestReferralRewardEndToEnd(t *testing.T) {
seq := h.srv.Seq()
h.srv.PushMessage(alice, alice, "/referral")
ref := h.wait(seq, "sendMessage", textHas("Invite friends"))
link := fmt.Sprintf("https://t.me/%s?start=ref_%d", fakeserver.BotUsername, alice)
if !strings.Contains(ref.Text(), link) || !strings.Contains(ref.Text(), "you get 200 sats") ||
!strings.Contains(ref.Text(), "You invited 0 people") {
code, err := h.app.Store.RefCode(context.Background(), alice)
if err != nil {
t.Fatal(err)
}
link := fmt.Sprintf("https://t.me/%s?start=rf_%s", fakeserver.BotUsername, code)
// The link carries a random code, never the Telegram user ID.
if !strings.Contains(ref.Text(), link) || strings.Contains(ref.Text(), fmt.Sprint(alice)) ||
!strings.Contains(ref.Text(), "you get 200 sats") || !strings.Contains(ref.Text(), "Invited: 0") ||
!strings.Contains(ref.Text(), "at most 2,000 sats a day") {
t.Fatalf("referral text = %q", ref.Text())
}
share, _ := ref.Button("")
if !strings.HasPrefix(share.URL, "https://t.me/share/url?url="+url.QueryEscape(link)) {
t.Fatalf("share button = %+v", share)
}
if !strings.Contains(ref.Params["reply_markup"], `"copy_text":{"text":"`+link+`"}`) {
t.Fatalf("no copy button with the link: %s", ref.Params["reply_markup"])
}
seq = h.press(alice, alice, ref, "rq")
if qr := h.wait(seq, "sendPhoto", nil); qr.Files["photo"].Size == 0 || !strings.Contains(qr.Text(), link) {
t.Fatalf("QR = %+v", qr.Params)
}
// Bob is new and arrives through the link.
seq = h.srv.Seq()
h.srv.PushMessage(bob, bob, fmt.Sprintf("/start ref_%d", alice))
h.srv.PushMessage(bob, bob, "/start rf_"+code)
h.wait(seq, "sendMessage", textHas("welcome"))
// Alice already knows the bot: a link to herself or from bob does nothing.
h.srv.PushMessage(alice, alice, fmt.Sprintf("/start ref_%d", bob))
// Alice already knows the bot: a link from bob, or her own, does nothing.
bobCode, _ := h.app.Store.RefCode(context.Background(), bob)
h.srv.PushMessage(alice, alice, "/start rf_"+bobCode)
h.srv.PushMessage(alice, alice, "/start rf_"+code)
seq = h.srv.Seq()
h.srv.PushMessage(bob, bob, "/credits")
menu := h.wait(seq, "sendMessage", textHas("Your balance"))
seq = h.press(bob, bob, menu, "by:credits:1000")
inv, _ := h.invoice(seq, 1)
// Not paid yet: nothing for alice.
if got := h.balance(alice); got != 0 {
t.Fatalf("inviter balance before payment = %d", got)
}
h.pay(inv)
h.wait(seq, "editMessageCaption", textHas("Paid."))
@@ -977,7 +1001,7 @@ func TestReferralRewardEndToEnd(t *testing.T) {
}
seq = h.srv.Seq()
h.srv.PushMessage(alice, alice, "/referral")
h.wait(seq, "sendMessage", textHas("You invited 1 person so far"))
h.wait(seq, "sendMessage", textHas("Invited: 1 · Bought something: 1\nEarned: 200 sats · Waiting: 0 sats"))
u, _ := h.app.Store.GetUser(context.Background(), alice)
if u.ReferredBy != 0 {
t.Fatalf("an existing user was referred: %+v", u)
@@ -985,6 +1009,30 @@ func TestReferralRewardEndToEnd(t *testing.T) {
h.booksBalance()
}
func TestOldReferralLinksStillWork(t *testing.T) {
h := startMoney(t, nil, nil)
h.askEveryTime(alice) // alice exists
seq := h.srv.Seq()
h.srv.PushMessage(bob, bob, fmt.Sprintf("/start ref_%d", alice))
h.wait(seq, "sendMessage", textHas("welcome"))
if u, _ := h.app.Store.GetUser(context.Background(), bob); u.ReferredBy != alice {
t.Fatalf("bob was not referred by the old link: %+v", u)
}
// A newcomer cannot refer themselves, with either kind of link.
seq = h.srv.Seq()
h.srv.PushMessage(carol, carol, fmt.Sprintf("/start ref_%d", carol))
h.wait(seq, "sendMessage", textHas("welcome"))
if u, _ := h.app.Store.GetUser(context.Background(), carol); u.ReferredBy != 0 {
t.Fatalf("self-referral stored: %+v", u)
}
// Broken or unknown links do not get in the way of the welcome.
for _, p := range []string{"rf_", "rf_ZZZZZZZZ", "ref_", "ref_abc", "rf_" + strings.Repeat("A", 60)} {
seq = h.srv.Seq()
h.srv.PushMessage(44+int64(len(p)), 44+int64(len(p)), "/start "+p)
h.wait(seq, "sendMessage", textHas("welcome"))
}
}
func TestDonate(t *testing.T) {
h := startMoney(t, nil, nil)
seq := h.srv.Seq()