Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

faucet.gno

// Package faucet is a toy points faucet realm for the gno.land sapphire testnet.
//
// Any caller may Claim() 100 points, but only once per cooldown window of 100
// blocks. Cooldown is enforced deterministically via runtime.ChainHeight() — no
// real clocks, no RNG. Per-caller balance and last-claim height live in an
// avl.Tree so Render iterates in deterministic (address-sorted) order.
package faucet

import (
	"chain"
	"chain/runtime"
	"chain/runtime/unsafe"
	"strconv"
	"strings"

	"gno.land/p/nt/avl/v0"
)

const (
	// ClaimAmount is granted on every successful claim.
	ClaimAmount = 100
	// CooldownBlocks is the minimum number of blocks between two claims by the
	// same caller.
	CooldownBlocks = 100
)

// claimer holds the per-address state.
type claimer struct {
	Balance   int64 // total points accumulated
	LastClaim int64 // chain height of the last successful claim
	Claims    int64 // number of successful claims by this address
}

var (
	// claimers maps address string -> *claimer.
	claimers = avl.NewTree()
	// totalDistributed is the running sum of all points ever granted.
	totalDistributed int64
	// totalClaims is the total number of successful claims across all callers.
	totalClaims int64
)

// Claim grants ClaimAmount points to the caller, subject to the per-caller
// cooldown. It aborts (cross-realm) if the caller must still wait.
func Claim(cur realm) {
	caller := unsafe.PreviousRealm().Address()
	key := caller.String()
	height := runtime.ChainHeight()

	var c *claimer
	if v := claimers.Get(key); v != nil {
		c = v.(*claimer)
		wait := c.LastClaim + CooldownBlocks - height
		if wait > 0 {
			panic("cooldown active: " + strconv.FormatInt(wait, 10) +
				" block(s) remaining before you can claim again")
		}
	} else {
		c = &claimer{}
	}

	c.Balance += ClaimAmount
	c.LastClaim = height
	c.Claims++
	claimers.Set(key, c)

	totalDistributed += ClaimAmount
	totalClaims++

	chain.Emit(
		"Claim",
		"caller", key,
		"amount", strconv.FormatInt(ClaimAmount, 10),
		"balance", strconv.FormatInt(c.Balance, 10),
		"height", strconv.FormatInt(height, 10),
	)
}

// BalanceOf returns the current point balance of addr (0 if never claimed).
func BalanceOf(addr string) int64 {
	if v := claimers.Get(addr); v != nil {
		return v.(*claimer).Balance
	}
	return 0
}

// Render produces a Markdown dashboard of faucet activity.
func Render(path string) string {
	var b strings.Builder

	b.WriteString("# Token Faucet\n\n")
	b.WriteString("A toy points faucet. Call `Claim` to receive **")
	b.WriteString(strconv.FormatInt(ClaimAmount, 10))
	b.WriteString(" points** — but only once every **")
	b.WriteString(strconv.FormatInt(CooldownBlocks, 10))
	b.WriteString(" blocks** per address.\n\n")

	b.WriteString("## Stats\n\n")
	b.WriteString("- Total distributed: **")
	b.WriteString(strconv.FormatInt(totalDistributed, 10))
	b.WriteString("** points\n")
	b.WriteString("- Total claims: **")
	b.WriteString(strconv.FormatInt(totalClaims, 10))
	b.WriteString("**\n")
	b.WriteString("- Unique claimers: **")
	b.WriteString(strconv.Itoa(claimers.Size()))
	b.WriteString("**\n")
	b.WriteString("- Current block height: **")
	b.WriteString(strconv.FormatInt(runtime.ChainHeight(), 10))
	b.WriteString("**\n\n")

	b.WriteString("## Claimers\n\n")
	if claimers.Size() == 0 {
		b.WriteString("_No one has claimed yet. Be the first!_\n")
		return b.String()
	}

	b.WriteString("| Address | Balance | Claims | Last claim (height) |\n")
	b.WriteString("| --- | ---: | ---: | ---: |\n")
	claimers.Iterate("", "", func(key string, value interface{}) bool {
		c := value.(*claimer)
		b.WriteString("| `")
		b.WriteString(key)
		b.WriteString("` | ")
		b.WriteString(strconv.FormatInt(c.Balance, 10))
		b.WriteString(" | ")
		b.WriteString(strconv.FormatInt(c.Claims, 10))
		b.WriteString(" | ")
		b.WriteString(strconv.FormatInt(c.LastClaim, 10))
		b.WriteString(" |\n")
		return false
	})

	return b.String()
}

faucet_test.gno

package faucet

import (
	"strings"
	"testing"

	"gno.land/p/nt/testutils/v0"
	"gno.land/p/nt/uassert/v0"
)

func TestClaimGrantsPoints(cur realm, t *testing.T) {
	alice := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(alice))

	Claim(cross(cur))

	uassert.Equal(t, int64(ClaimAmount), BalanceOf(alice.String()),
		"first claim should grant ClaimAmount")
	uassert.Equal(t, int64(ClaimAmount), totalDistributed,
		"totalDistributed should reflect the single claim")
	uassert.Equal(t, int64(1), totalClaims, "one claim recorded")
}

func TestClaimCooldownAborts(cur realm, t *testing.T) {
	bob := testutils.TestAddress("bob")
	testing.SetRealm(testing.NewUserRealm(bob))

	// First claim succeeds at the current height.
	Claim(cross(cur))

	// Immediate second claim (same block) must abort due to cooldown.
	uassert.AbortsWithMessage(t, cur,
		"cooldown active: 100 block(s) remaining before you can claim again",
		func() {
			Claim(cross(cur))
		},
		"second immediate claim should abort on cooldown",
	)
}

func TestRenderContainsStats(cur realm, t *testing.T) {
	carol := testutils.TestAddress("carol")
	testing.SetRealm(testing.NewUserRealm(carol))
	Claim(cross(cur))

	out := Render("")
	uassert.True(t, strings.Contains(out, "# Token Faucet"), "has title")
	uassert.True(t, strings.Contains(out, "Total distributed"), "has stats")
	uassert.True(t, strings.Contains(out, carol.String()),
		"claimer address listed in table")
}

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/faucet/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186407
  max_deposit = "5000000ugnot"