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

Playground

drops.gno

// Package drops creates Launchpad NFT collections and runs their primary
// mints. It charges the collection fee and each mint price, and pays the
// treasury and the creator inside the same transaction: it holds no funds
// between calls and has no admin.
package drops

import (
	"chain"
	"strconv"
	"time"

	"gno.land/p/nt/avl/v0"
	"gno.land/p/samcrew/launchpad/currency/v1"
	"gno.land/p/samcrew/launchpad/merkle/v1"
	"gno.land/p/samcrew/launchpad/meta/v1"
	sm "gno.land/p/samcrew/launchpad/safemath/v1"
	"gno.land/r/samcrew/launchpad/config/v1"
	"gno.land/r/samcrew/launchpad/nft/v1"
)

const (
	KindFixed     = "fixed"     // one price, open to every wallet
	KindAllowlist = "allowlist" // only addresses under a Merkle root, each with its own allowance
	KindHolder    = "holder"    // one mint per token held in another collection
	KindDutch     = "dutch"     // the price falls in a straight line until the stage ends

	// Hard limits, fixed for the life of this realm.
	maxPrimaryFeeBPS = 500 // 5% of a mint price
	maxStages        = 10
	maxStageSeconds  = 365 * 24 * 60 * 60
	maxPerWallet     = 1_000_000
)

// stage is one mint window of a collection, open during [Start, End).
type stage struct {
	Kind      string
	Start     int64
	End       int64
	Price     int64 // per token; for a dutch stage, the price at Start
	Floor     int64 // dutch only: the price reached at End
	Currency  string
	FeeBPS    int64  // protocol fee on each mint, pinned when the creator signed
	SupplyCap int64  // 0 means no cap beyond the collection's
	PerWallet int64  // most one wallet may mint; an allowlist sets it per address
	Root      string // allowlist only
	Gate      string // holder only: the collection whose tokens each allow one mint
	// Holder only: the gate collection's tokens numbered up to it, all minted
	// when the stage was placed, open a mint; later ones do not, so no loop of
	// collections gated on each other mints its own gate tokens.
	GateLimit int64
	Minted    int64
}

var (
	stages avl.Tree // collection ID -> []stage, at most maxStages
	minted avl.Tree // "<collection>/<stage>/<address>" -> int64
	gated  avl.Tree // "<collection>/<stage>/<gate token number>" -> true
)

func directCaller(cur realm) address {
	if !cur.IsCurrent() || !cur.Previous().IsUserCall() {
		panic("launchpad/drops: direct user call required")
	}
	return cur.Previous().Address()
}

// CreateCollection registers the caller's collection in the NFT ledger and
// pays the collection fee to the treasury. maxFee is the highest fee the
// caller accepts: a fee above it aborts before anything is taken, whatever
// the caller's allowance. Native coins are attached for exactly the fee; a
// token is pulled for exactly the fee. The collection terms are documented
// on nft.Create.
func CreateCollection(cur realm, name, symbol, description, image, banner,
	website, mode string, revocable bool, maxSupply int64, metadataMode,
	baseURI, placeholderURI, commitment, provenance, royalties,
	currencyKey string, maxFee int64) string {
	creator := directCaller(cur)
	p := policy(config.LaneCollection, currencyKey)
	if p.collectionFee > maxFee {
		panic("launchpad/drops: collection fee above the accepted maximum")
	}
	held := take(cur, currencyKey, p.collectionFee, p.collectionFee)
	id := nft.Create(cross(cur), creator, name, symbol, description, image,
		banner, website, mode, revocable, maxSupply, metadataMode, baseURI,
		placeholderURI, commitment, provenance, royalties)
	pay(cur, currencyKey, p.treasury, p.collectionFee)
	settled(cur, currencyKey, held)
	chain.Emit("CollectionFeePaid", "collection", id, "creator", creator.String(),
		"currency", currencyKey, "fee", strconv.FormatInt(p.collectionFee, 10),
		"treasury", p.treasury.String())
	return id
}

func stagesOf(id string) []stage {
	if list := stages.Get(id); list != nil {
		return list.([]stage)
	}
	return nil
}

// requireCreator aborts unless the caller is the collection's creator. The
// creator may be a realm, a DAO for instance: what it signs here moves no
// funds. Coins attached to a user's call would stay in this realm, so they
// are refused; coins a realm's transaction carries belong to that realm.
func requireCreator(cur realm, id string) {
	if !cur.IsCurrent() || cur.Previous().Address() != nft.GetInfo(id).Creator {
		panic("launchpad/drops: creator only")
	}
	if cur.Previous().IsUserCall() {
		currency.AssertNoCoins(0, cur)
	}
}

// emitStage publishes every term of a stage, so a reader of events alone
// knows what each stage sells.
func emitStage(id string, index int, s stage) {
	chain.Emit("StageSet", "collection", id, "stage", strconv.Itoa(index),
		"kind", s.Kind, "start", strconv.FormatInt(s.Start, 10),
		"end", strconv.FormatInt(s.End, 10), "currency", s.Currency,
		"price", strconv.FormatInt(s.Price, 10),
		"floor", strconv.FormatInt(s.Floor, 10),
		"fee_bps", strconv.FormatInt(s.FeeBPS, 10),
		"supply_cap", strconv.FormatInt(s.SupplyCap, 10),
		"per_wallet", strconv.FormatInt(s.PerWallet, 10),
		"root", s.Root, "gate", s.Gate, "gate_limit", strconv.FormatInt(s.GateLimit, 10))
}

func validRoot(s string) bool {
	h, err := merkle.ParseHash(s)
	return err == nil && merkle.FormatHash(h) == s
}

// newStage validates a stage of collection id that has not started. Fields a
// kind does not use must be zero, so a stage reads the same to everyone. A
// holder stage is gated on another collection, and only on the tokens that
// collection had minted when the stage was placed: gated on its own, or on a
// collection gated on it in turn, every token minted would open one more mint.
func newStage(id, kind string, start, end, price, floor, supplyCap, perWallet int64,
	root, gate, currencyKey string) stage {
	ok := start >= time.Now().Unix() && end > start && end-start <= maxStageSeconds &&
		price >= 0 && supplyCap >= 0 && perWallet >= 0 && perWallet <= maxPerWallet
	var gateLimit int64
	switch kind {
	case KindFixed:
		ok = ok && perWallet > 0 && floor == 0 && root == "" && gate == ""
	case KindAllowlist:
		ok = ok && perWallet == 0 && floor == 0 && validRoot(root) && gate == ""
	case KindHolder:
		ok = ok && perWallet > 0 && floor == 0 && root == "" && gate != "" && gate != id
		if ok {
			gateLimit = nft.GetInfo(gate).Minted // an unknown gate collection aborts
		}
	case KindDutch:
		ok = ok && perWallet > 0 && floor >= 0 && floor < price && root == "" && gate == ""
	default:
		ok = false
	}
	if !ok {
		panic("launchpad/drops: invalid stage")
	}
	return stage{Kind: kind, Start: start, End: end, Price: price, Floor: floor,
		Currency: currencyKey, SupplyCap: supplyCap, PerWallet: perWallet,
		Root: root, Gate: gate, GateLimit: gateLimit}
}

// place writes s at index (len(list) appends) after checking that the caller
// is the collection's creator, the mint lane accepts the currency, the
// protocol fee is within what the creator accepts and no other stage
// overlaps. The fee is pinned on the stage: a later change does not reach it.
func place(cur realm, id string, index int, s stage, maxFeeBPS int64) {
	requireCreator(cur, id)
	s.FeeBPS = policy(config.LaneNFTDrops, s.Currency).primaryFeeBPS
	if s.FeeBPS > maxFeeBPS || s.FeeBPS > maxPrimaryFeeBPS {
		panic("launchpad/drops: protocol fee above the accepted maximum")
	}
	list := stagesOf(id)
	for i, other := range list {
		if i != index && s.Start < other.End && other.Start < s.End {
			panic("launchpad/drops: stages overlap")
		}
	}
	if index == len(list) {
		list = append(list, s)
	} else {
		list[index] = s
	}
	stages.Set(id, list)
	emitStage(id, index, s)
}

// AddStage schedules a mint window and returns its index. The arguments are
// the terms of the stage, then the highest protocol fee the creator accepts.
// A collection has ten stages for its whole life: a stage that ended keeps
// its slot.
func AddStage(cur realm, id, kind string, start, end, price, floor, supplyCap,
	perWallet int64, root, gate, currencyKey string, maxFeeBPS int64) int {
	index := len(stagesOf(id))
	if index >= maxStages {
		panic("launchpad/drops: too many stages")
	}
	place(cur, id, index, newStage(id, kind, start, end, price, floor, supplyCap,
		perWallet, root, gate, currencyKey), maxFeeBPS)
	return index
}

// EditStage replaces a stage that has not started. Once a stage has opened
// its terms are final, whatever the replacement would have been: it can only
// be ended.
func EditStage(cur realm, id string, index int, kind string, start, end, price,
	floor, supplyCap, perWallet int64, root, gate, currencyKey string,
	maxFeeBPS int64) {
	list := stagesOf(id)
	if index < 0 || index >= len(list) {
		panic("launchpad/drops: unknown stage")
	}
	if time.Now().Unix() >= list[index].Start {
		panic("launchpad/drops: stage already started")
	}
	place(cur, id, index, newStage(id, kind, start, end, price, floor, supplyCap,
		perWallet, root, gate, currencyKey), maxFeeBPS)
}

// EndStage closes an open stage at once and for good. It asks no lane: a
// creator stops its own sale whatever is paused.
func EndStage(cur realm, id string, index int) {
	requireCreator(cur, id)
	list := stagesOf(id)
	if index < 0 || index >= len(list) {
		panic("launchpad/drops: unknown stage")
	}
	s := &list[index]
	now := time.Now().Unix()
	if now < s.Start || now >= s.End {
		panic("launchpad/drops: stage closed")
	}
	s.End = now
	stages.Set(id, list)
	emitStage(id, index, *s)
}

// priceAt is the stage price at a time inside its window.
func priceAt(s stage, now int64) int64 {
	if s.Kind != KindDutch {
		return s.Price
	}
	return s.Price - sm.MulDiv(s.Price-s.Floor, now-s.Start, s.End-s.Start)
}

func stageKey(id string, index int) string { return id + "/" + strconv.Itoa(index) + "/" }

// AllowlistLeaf is the Merkle leaf that lets who mint up to allowance tokens
// in one stage: sha256(0x00 || "launchpad-drop-v1|<collection>|<stage>|<address>|<allowance>").
// Only the chain's spelling of an address makes a leaf: a list written in
// capitals would name accounts that can never claim.
func AllowlistLeaf(id string, index int, who address, allowance int64) merkle.Hash {
	if !meta.ValidAddress(who) {
		panic("launchpad/drops: invalid account")
	}
	return merkle.Leaf([]byte("launchpad-drop-v1|" + id + "|" + strconv.Itoa(index) +
		"|" + who.String() + "|" + strconv.FormatInt(allowance, 10)))
}

// entitled checks what the stage's kind requires of the buyer and returns how
// many tokens that wallet may mint in the stage. For a holder stage it also
// returns the key under which the gate token must be recorded as used.
func entitled(s stage, id string, index int, buyer address, allowance int64,
	proof string, gateNumber int64) (limit int64, gateKey string) {
	switch s.Kind {
	case KindAllowlist:
		root, _ := merkle.ParseHash(s.Root)
		path, err := merkle.ParseProof(proof)
		if err != nil || gateNumber != 0 || allowance > maxPerWallet ||
			!merkle.Verify(root, AllowlistLeaf(id, index, buyer, allowance), path) {
			panic("launchpad/drops: not on the allowlist")
		}
		return allowance, ""
	case KindHolder:
		gateKey = stageKey(id, index) + strconv.FormatInt(gateNumber, 10)
		if allowance != 0 || proof != "" || gated.Has(gateKey) ||
			nft.OwnerOf(s.Gate, gateNumber) != buyer {
			panic("launchpad/drops: gate token missing or already used")
		}
		if gateNumber > s.GateLimit {
			panic("launchpad/drops: gate token minted after the stage was scheduled")
		}
	default:
		if allowance != 0 || proof != "" || gateNumber != 0 {
			panic("launchpad/drops: this stage takes no allowance, proof or gate token")
		}
	}
	return s.PerWallet, gateKey
}

// Mint sells the caller one token of an open stage and returns its number.
// currencyKey and offered are what the caller accepts to pay: the stage must
// be priced in that currency, and offered is the most it will take. Native
// coins are attached for exactly offered and the difference with the price
// comes back, which is what makes a falling price payable; a token is pulled
// for exactly the price. The price is split between the treasury (the fee
// pinned on the stage) and the creator. allowance and proof are for an
// allowlist stage, gateNumber for a holder stage; elsewhere they are zero and
// empty.
func Mint(cur realm, id string, index int, currencyKey string, offered,
	allowance int64, proof string, gateNumber int64) int64 {
	buyer := directCaller(cur)
	list := stagesOf(id)
	if index < 0 || index >= len(list) {
		panic("launchpad/drops: unknown stage")
	}
	s := &list[index]
	if s.Currency != currencyKey {
		panic("launchpad/drops: stage priced in another currency")
	}
	now := time.Now().Unix()
	if now < s.Start || now >= s.End || (s.SupplyCap > 0 && s.Minted >= s.SupplyCap) {
		panic("launchpad/drops: stage closed")
	}
	p := policy(config.LaneNFTDrops, s.Currency)
	wallet := stageKey(id, index) + buyer.String()
	var count int64
	if v := minted.Get(wallet); v != nil {
		count = v.(int64)
	}
	limit, gateKey := entitled(*s, id, index, buyer, allowance, proof, gateNumber)
	if count >= limit {
		panic("launchpad/drops: wallet limit reached")
	}
	price := priceAt(*s, now)
	held := take(cur, s.Currency, price, offered)
	number := nft.MintTo(cross(cur), id, buyer)
	creator := nft.GetInfo(id).Creator
	fee := sm.BPS(price, s.FeeBPS)
	pay(cur, s.Currency, p.treasury, fee)
	pay(cur, s.Currency, creator, price-fee)
	settled(cur, s.Currency, held)
	s.Minted++
	stages.Set(id, list)
	minted.Set(wallet, count+1)
	if gateKey != "" {
		gated.Set(gateKey, true)
	}
	chain.Emit("Minted", "collection", id, "number", strconv.FormatInt(number, 10),
		"stage", strconv.Itoa(index), "buyer", buyer.String(),
		"currency", s.Currency, "price", strconv.FormatInt(price, 10),
		"fee", strconv.FormatInt(fee, 10), "treasury", p.treasury.String(),
		"creator", creator.String())
	return number
}

func Render(path string) string {
	return "# Launchpad NFT drops\n\n" + strconv.Itoa(stages.Size()) +
		" collections have mint stages.\n"
}

gnomod.toml

module = "gno.land/r/samcrew/launchpad/drops/v1"
gno = "0.9"

[addpkg]
  creator = "g136j0m08pkm2lwwde9dmlx8uee26llent9s5cpf"
  height = 228565
  max_deposit = "8000000ugnot"

payment.gno

package drops

import (
	"chain/banker"

	"gno.land/p/samcrew/launchpad/currency/v1"
	sm "gno.land/p/samcrew/launchpad/safemath/v1"
	"gno.land/r/nt/grc20reg/v0"
	"gno.land/r/samcrew/launchpad/config/v1"
)

// terms is what this realm needs from config.
type terms struct {
	treasury      address
	primaryFeeBPS int64 // share of each mint price
	collectionFee int64 // in the currency asked for
}

// termsOf reads the terms in force. The collection fee is -1 for a currency
// that has none.
func termsOf(currencyKey string) terms {
	p := config.GetVersion(config.GetCurrentVersion())
	t := terms{treasury: p.Treasury, primaryFeeBPS: p.NFTPrimaryFeeBPS, collectionFee: -1}
	for _, c := range p.Currencies {
		if c.Key == currencyKey {
			t.collectionFee = c.CollectionFee
		}
	}
	return t
}

// policy returns the terms in force after checking that the lane is open for
// the currency. It is a variable only so that unit tests can stand in for the
// config realm, whose admin cannot be set from another package's tests.
var policy = func(lane, currencyKey string) terms {
	config.AssertLaneOpen(lane, currencyKey)
	return termsOf(currencyKey)
}

// balance reads one denomination, so coins of other denominations sent to
// the realm never add to the cost of a payment.
func balance(cur realm, key string) int64 {
	if key == config.NativeCurrency {
		return banker.NewReadonlyBanker().GetCoin(cur.Address(), key)
	}
	return grc20reg.MustGet(key).BalanceOf(cur.Address())
}

// take collects price from the direct caller and returns what the realm held
// before. Native coins come attached: the caller attaches exactly offered, at
// least the price, and the difference goes straight back. A token is pulled
// for exactly the price.
func take(cur realm, key string, price, offered int64) int64 {
	if offered < price {
		panic("launchpad/drops: price above the amount offered")
	}
	if key != config.NativeCurrency {
		held := balance(cur, key)
		if price == 0 {
			currency.AssertNoCoins(0, cur)
		} else {
			currency.CollectToken(0, cur, grc20reg.MustGet(key), price)
		}
		return held
	}
	if offered == 0 {
		currency.AssertNoCoins(0, cur)
		return balance(cur, key)
	}
	buyer := currency.CollectNative(0, cur, offered)
	held := sm.Sub(balance(cur, key), offered)
	pay(cur, key, buyer, offered-price)
	return held
}

func pay(cur realm, key string, to address, amount int64) {
	if amount == 0 {
		return
	}
	if key == config.NativeCurrency {
		currency.PayNative(0, cur, to, amount)
	} else {
		currency.PayToken(0, cur, grc20reg.MustGet(key), to, amount)
	}
}

// settled aborts the transaction unless everything taken has been paid out:
// the realm must hold exactly what it held before the call.
func settled(cur realm, key string, held int64) {
	if balance(cur, key) != held {
		panic("launchpad/drops: payment not fully settled")
	}
}

views.gno

package drops

import (
	"strconv"
	"strings"
	"time"

	"gno.land/p/samcrew/launchpad/meta/v1"
	"gno.land/r/samcrew/launchpad/nft/v1"
)

func num(n int64) string { return meta.JSONString(strconv.FormatInt(n, 10)) }

// StagesJSON lists a collection's stages in index order. "currentPrice" is
// the price a mint would pay now, and is only meaningful while "open" is true.
func StagesJSON(id string) string {
	nft.GetInfo(id) // an unknown collection aborts
	now := time.Now().Unix()
	var b strings.Builder
	b.WriteByte('[')
	for i, s := range stagesOf(id) {
		if i > 0 {
			b.WriteByte(',')
		}
		open := now >= s.Start && now < s.End
		current := s.Price
		if open {
			current = priceAt(s, now)
		}
		b.WriteString("{" +
			`"index":` + strconv.Itoa(i) +
			`,"kind":` + meta.JSONString(s.Kind) +
			`,"start":` + num(s.Start) +
			`,"end":` + num(s.End) +
			`,"open":` + strconv.FormatBool(open) +
			`,"price":` + num(s.Price) +
			`,"floor":` + num(s.Floor) +
			`,"currentPrice":` + num(current) +
			`,"currency":` + meta.JSONString(s.Currency) +
			`,"feeBPS":` + num(s.FeeBPS) +
			`,"supplyCap":` + num(s.SupplyCap) +
			`,"perWallet":` + num(s.PerWallet) +
			`,"root":` + meta.JSONString(s.Root) +
			`,"gate":` + meta.JSONString(s.Gate) +
			`,"gateLimit":` + num(s.GateLimit) +
			`,"minted":` + num(s.Minted) + "}")
	}
	b.WriteByte(']')
	return b.String()
}

// MintedBy is how many tokens who has minted in a stage. Only the chain's
// spelling of an address is an account: any other is refused, never read as 0.
func MintedBy(id string, index int, who string) int64 {
	if !meta.ValidAddress(address(who)) {
		panic("launchpad/drops: invalid account")
	}
	if v := minted.Get(stageKey(id, index) + who); v != nil {
		return v.(int64)
	}
	return 0
}

// GateUsed reports whether a gate token has already paid for a mint in a
// holder stage.
func GateUsed(id string, index int, gateNumber int64) bool {
	return gated.Has(stageKey(id, index) + strconv.FormatInt(gateNumber, 10))
}

// TermsJSON is what creating a collection and scheduling a stage cost now:
// the collection fee in a currency and the protocol fee a new stage would
// pin. A value of -1 means the lane is closed; config's ActionStatusJSON tells
// whether a lane accepts new actions right now.
func TermsJSON(currencyKey string) string {
	t := termsOf(currencyKey)
	return "{" +
		`"currency":` + meta.JSONString(currencyKey) +
		`,"collectionFee":` + num(t.collectionFee) +
		`,"primaryFeeBPS":` + num(t.primaryFeeBPS) +
		`,"maxPrimaryFeeBPS":` + num(maxPrimaryFeeBPS) +
		`,"treasury":` + meta.JSONString(t.treasury.String()) + "}"
}