Documentation Index
Fetch the complete documentation index at: https://docs.kynesys.xyz/llms.txt
Use this file to discover all available pages before exploring further.
Referral System
The referral system lets a user earn bonus points by inviting new users to the platform. It is implemented by theReferrals class
(src/features/incentive/referrals.ts) and is invoked from the points pipeline
during identity-linking operations. Referral state lives in the
referralInfo field of each account’s GCRMain row (the GCR).
See also the Points System page for how
points are awarded and stored.
What it does
Every account owns a deterministicreferralCode. When a new user links their
first identity (wallet or social) and includes someone else’s referral code,
the node:
- Looks up the referrer account that owns the code.
- Validates the relationship (not self, not already referred, referee still eligible).
- Awards a bonus to both parties and records the relationship.
Bonus constants
Defined on theReferrals class:
points.totalPoints and tracked under
points.breakdown.referrals.
Referral code generation
Referrals.generateReferralCode(publicKey, options?) derives a code from an
ed25519 public key:
- Strip an optional
0xprefix; the key must be 64 hex characters. - SHA-256 hash the cleaned key (
Hashing.sha256). - Take the leading bytes of the hash and Base58-encode them (
bs58). - Truncate to the requested length. The default
lengthis12(~70 bits of entropy).
8 | 10 | 12 | 16), an optional 2-character
checksum, and a prefix, but the system uses the 12-character default.
GCR referral state
ThereferralInfo field on GCRMain has this shape:
Key methods
findAccountByReferralCode(referralCode): Promise<GCRMain | null>
Locates the account that owns a code using a Postgres JSONB query:
isEligibleForReferral(account): boolean
Returns true only if the account has never been referred before. It returns
false when any of the following hold:
referralInfo.referredByis setreferralInfo.referralsis non-emptyreferralInfo.totalReferrals > 0points.totalPoints > 0(the account already earned points)
isAlreadyReferred(referrerAccount, newUserPubkey): boolean
Checks whether newUserPubkey already appears in the referrer’s
referralInfo.referrals[], preventing the same pair from being counted twice.
processReferral(newAccount, referralCode, gcrMainRepository): Promise<void>
Entry point invoked from the points pipeline. It resolves the referrer, then
silently returns (no error, no points) if:
- the code does not resolve to any account,
- the referrer and the new account are the same
pubkey, or - the new account is already in the referrer’s
referrals[].
awardReferralPoints, which:
- adds
REFERRER_BONUSto the referrer’s total andbreakdown.referrals, incrementstotalReferrals, and appends areferrals[]entry; - adds
REFERRED_USER_BONUSto the new user and setsreferralInfo.referredByto the referrer’s pubkey; - saves the referrer account (the new account is saved by the caller).
How a code flows in from the SDK
AreferralCode is supplied by the SDK on identity-linking calls (for example
inferXmIdentity / addTwitterIdentity). It is embedded in the request payload
and the GCR edit operation.
When an identity transaction is broadcast,
handleIdentityRequest
(src/libs/network/routines/transactions/handleIdentityRequest.ts) validates a
present referralCode up front: it rejects the request if the code resolves to
no account (“Referrer account not found”) or if the referrer equals the sender
(“Referrer and new user are the same”).
During consensus, the identity-add routines call into IncentiveManager hooks
(walletLinked, twitterLinked, etc.), which forward the referralCode to the
matching PointSystem award method. That method calls addPointsToGCR, which
invokes Referrals.processReferral when a code is present and the account is
eligible:
How codes are returned
A user retrieves their own referral code viaPointSystem.getUserPoints, whose
response includes the referralCode field. For legacy accounts created before
referral support, getUserPoints generates and persists a code on first read.