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.

types.gno

6.80 Kb · 210 lines
  1// Package types holds the value types shared by core, the minters, the
  2// market and the readers. Every type is plain data (no pointers into realm
  3// state), so passing one across a realm boundary hands over a copy and
  4// never a capability.
  5package types
  6
  7import (
  8	"errors"
  9)
 10
 11// State is a collection's lifecycle (design §4.1):
 12//
 13//	Draft -> Scheduled -> Minting -> Closed -> Final
 14//
 15// Scheduled->Minting and Minting->Closed happen by block height; core
 16// reports the effective state and materializes it on the next write.
 17type State int
 18
 19const (
 20	StateDraft State = iota
 21	StateScheduled
 22	StateMinting
 23	StateClosed
 24	StateFinal
 25)
 26
 27func (s State) String() string {
 28	switch s {
 29	case StateDraft:
 30		return "draft"
 31	case StateScheduled:
 32		return "scheduled"
 33	case StateMinting:
 34		return "minting"
 35	case StateClosed:
 36		return "closed"
 37	case StateFinal:
 38		return "final"
 39	}
 40	return "unknown"
 41}
 42
 43// MaxUnitPrice caps every price (ugnot) so price*count and fee math stay
 44// far from int64 overflow: 1e15 ugnot = 1e9 GNOT.
 45const MaxUnitPrice = 1_000_000_000_000_000
 46
 47// Payee receives Bps/10000 of a payment.
 48type Payee struct {
 49	Addr address
 50	Bps  int64
 51}
 52
 53// CollectionSpec is what a minter asks core to create. Payees travel as
 54// "addr:bps,…" strings and are parsed by core, so core never stores a slice
 55// allocated by another realm.
 56type CollectionSpec struct {
 57	Name, Symbol, Description string
 58	EngineID, Params          string
 59	MaxSupply, RoyaltyBps     int64
 60	PrimaryPayees             string
 61	RoyaltyPayees             string
 62	// Soulbound makes the collection non-transferable, fixed at creation:
 63	// its tokens can never be transferred or approved (also not after
 64	// Final), the market never lists them, the owner may still burn one
 65	// after Final, and royaltyBps must be 0 (no secondary sales).
 66	Soulbound bool
 67}
 68
 69// BeaconCommitted is the platform's commit-reveal beacon as a collection's
 70// randomness source (LaunchSpec.BeaconSource "" means the same).
 71const BeaconCommitted = "committed"
 72
 73// Launch modes (LaunchSpec.Mode, CollectionInfo.Mode).
 74//
 75// ModeReveal ("") is the fair commit–reveal launch: the token map is fixed
 76// by the creator's secret, the beacon's (or a randomness provider's) value
 77// and the mint entropy, and stays hidden until Final.
 78//
 79// ModeOpen ("open") has no secrets at all: the salt is public from Launch
 80// (sha256("art|open|v1|" + chainId + "|" + cid)), so anyone can compute
 81// every token's art before minting. For badges, editions, event drops and
 82// 1/1s. An open collection has no commitment, no beacon or provider, no
 83// creator reserve, is never a fair launch (badge "open"), and its tokens
 84// still stay locked until Final, which anyone may call once the sale ended
 85// or sold out.
 86const (
 87	ModeReveal = ""
 88	ModeOpen   = "open"
 89)
 90
 91// LaunchSpec freezes a collection's sale window and commitments (I-6).
 92//
 93// BeaconSource picks the second entropy source when UseBeacon is set:
 94// "" or BeaconCommitted for the platform beacon's commit-reveal, or the
 95// package path of an admin-approved randomness provider realm (e.g. a drand
 96// relay); it must be "" without UseBeacon.
 97//
 98// Mode is ModeReveal ("") or ModeOpen. An open launch needs RevealWindow 0,
 99// CreatorCommit "", UseBeacon false and BeaconSource "".
100type LaunchSpec struct {
101	StartHeight, EndHeight int64
102	RevealWindow           int64 // blocks after close; core bounds it (0 in open mode)
103	CreatorCommit          string
104	UseBeacon              bool
105	BeaconSource           string
106	Mode                   string
107}
108
109// CollectionInfo is core's typed read model for other realms.
110type CollectionInfo struct {
111	ID, Name, Symbol, Description string
112	Creator                       address
113	Minter, EngineID, Params      string
114	State                         State
115	MaxSupply, Minted, Reserved   int64
116	StartHeight, EndHeight        int64
117	RevealDeadline                int64 // 0 before Launch
118	RoyaltyBps                    int64
119	Badge                         string   // provenance badge once Final
120	Mode                          string   // ModeReveal ("") or ModeOpen; "" before Launch
121	TraitTypes                    []string // the collection's trait types (a copy)
122	Salt                          string   // final salt (hex); "" before Final
123	Soulbound                     bool     // non-transferable (CollectionSpec.Soulbound)
124}
125
126// Fees are the protocol's fee parameters; core enforces hard caps.
127type Fees struct {
128	CreateFee    int64 // ugnot per collection
129	PrimaryBps   int64 // of every primary sale
130	SecondaryBps int64 // of every market sale
131	Treasury     address
132	EngineCapBps int64 // platform cap on engine-author fees (<= engine.MaxFeeBps)
133}
134
135// Phase kinds.
136const (
137	PhaseAllow  = "allow"
138	PhasePublic = "public"
139)
140
141// Phase is one sale window. A fixed price has StartPrice == FloorPrice and
142// no steps; a Dutch auction starts at StartPrice and drops StepAmount every
143// StepBlocks until FloorPrice (design §4.5). WalletCap counts lifetime
144// mints of the collection (I-9); 0 means no cap.
145type Phase struct {
146	Kind, Set              string
147	StartHeight, EndHeight int64
148	StartPrice, FloorPrice int64
149	StepBlocks, StepAmount int64
150	WalletCap              int64
151}
152
153// Validate checks a phase in isolation; the minter checks the set grammar
154// and how phases relate to each other.
155func (p Phase) Validate() error {
156	switch p.Kind {
157	case PhasePublic:
158		if p.Set != "" {
159			return errors.New("a public phase has no allowlist set")
160		}
161	case PhaseAllow:
162		if p.Set == "" {
163			return errors.New("an allow phase needs a set")
164		}
165	default:
166		return errors.New("phase kind must be allow or public")
167	}
168	if p.StartHeight <= 0 || p.EndHeight < p.StartHeight {
169		return errors.New("phase heights must satisfy 0 < start <= end")
170	}
171	if p.FloorPrice < 0 || p.StartPrice < p.FloorPrice || p.StartPrice > MaxUnitPrice {
172		return errors.New("prices must satisfy 0 <= floor <= start <= MaxUnitPrice")
173	}
174	if p.StartPrice == p.FloorPrice {
175		if p.StepBlocks != 0 || p.StepAmount != 0 {
176			return errors.New("a fixed-price phase has no steps")
177		}
178	} else if p.StepBlocks <= 0 || p.StepAmount <= 0 {
179		return errors.New("a Dutch phase needs positive stepBlocks and stepAmount")
180	}
181	if p.WalletCap < 0 {
182		return errors.New("walletCap must be >= 0")
183	}
184	return nil
185}
186
187// Covers reports whether height h is inside the phase window.
188func (p Phase) Covers(h int64) bool {
189	return h >= p.StartHeight && h <= p.EndHeight
190}
191
192// PriceAt is the unit price at height h:
193//
194//	max(floor, start - floor((h-startH)/stepBlocks) * stepAmount)
195//
196// computed so that no intermediate value can overflow.
197func (p Phase) PriceAt(h int64) int64 {
198	if p.StartPrice == p.FloorPrice || h <= p.StartHeight {
199		return p.StartPrice
200	}
201	steps := (h - p.StartHeight) / p.StepBlocks
202	if steps > (p.StartPrice-p.FloorPrice)/p.StepAmount {
203		return p.FloorPrice
204	}
205	price := p.StartPrice - steps*p.StepAmount
206	if price < p.FloorPrice {
207		return p.FloorPrice
208	}
209	return price
210}