Search Apps Documentation Source Content File Folder Download Copy Actions Download

README.md

2.17 Kb ยท 88 lines

Referral

Referral system for tracking user relationships.

Overview

Manages referral relationships between users with cooldown periods to prevent gaming.

Global Functions

TryRegister(cur realm, addr address, referral string) bool

Attempts to register a referral relationship. Returns true on success, false on failure. Emits ReferralRegistrationFailed event on error.

GetReferral(addr string) string

Returns the referral address for the given address. Returns empty string if not found.

HasReferral(addr string) bool

Returns true if the given address has a referral.

IsEmpty() bool

Returns true if no referrals exist in the system.

ContractAddress() string

Returns the address of the referral contract. Use this address as the referral parameter in TryRegister to remove an existing referral.

Usage

Registering a Referral

 1package example
 2
 3import (
 4    "gno.land/r/gnoswap/referral"
 5)
 6
 7// RegisterUserReferral registers a referral relationship for a user.
 8// Must be called from an authorized realm (router, staker, etc.)
 9func RegisterUserReferral(userAddr, referrerAddr address) bool {
10    return referral.TryRegister(cross, userAddr, referrerAddr.String())
11}

Removing a Referral

 1package example
 2
 3import (
 4    "gno.land/r/gnoswap/referral"
 5)
 6
 7// RemoveUserReferral removes the referral relationship for a user.
 8// Pass the contract's own address as the referral to indicate removal.
 9func RemoveUserReferral(userAddr address) bool {
10    return referral.TryRegister(cross, userAddr, referral.ContractAddress())
11}

Querying Referrals

 1package example
 2
 3import (
 4    "gno.land/r/gnoswap/referral"
 5)
 6
 7// GetUserReferrer returns the referrer address for a user.
 8// Returns empty string if no referral exists.
 9func GetUserReferrer(userAddr string) string {
10    return referral.GetReferral(userAddr)
11}
12
13// CheckUserHasReferral returns true if the user has a registered referral.
14func CheckUserHasReferral(userAddr string) bool {
15    return referral.HasReferral(userAddr)
16}

Security

  • One referral per address
  • 24-hour change cooldown
  • No self-referrals
  • Immutable during cooldown
  • Only authorized callers can modify referrals