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.

guardians.gno

4.07 Kb · 163 lines
  1package guardians
  2
  3import (
  4	"gno.land/p/nt/grc721/v0"
  5	"gno.land/p/nt/ufmt/v0"
  6)
  7
  8const (
  9	collectionName   = "Guardians"
 10	collectionSymbol = "GUARDIANS"
 11	maxSupply        = int64(3333)
 12	maxPublicMintBatch = 20
 13	maxMintBatch     = maxPublicMintBatch + 1 // One free claim plus 20 paid NFTs.
 14)
 15
 16// Testnet deployment owner and default royalty recipient.
 17const onyxAdmin address = "g1j4ehkkrqcr53a233wamtvqggt3skr86c6777xd"
 18
 19var (
 20	token        *grc721.Token
 21	ledger       *grc721.PrivateLedger
 22	callerTeller grc721.Teller
 23)
 24
 25func init(cur realm) {
 26	token, ledger = grc721.NewToken(collectionName, collectionSymbol, 0, cur)
 27	callerTeller = ledger.CallerTeller()
 28	attachExtensions(ledger)
 29}
 30
 31// Collection getters.
 32
 33func Status() string { return "REVEAL_ENGINE_READY" }
 34
 35func Name() string { return token.GetName() }
 36
 37func Symbol() string { return token.GetSymbol() }
 38
 39func CollectionID() string { return token.ID() }
 40
 41func TotalSupply() int64 { return token.TotalSupply() }
 42
 43func MaxSupply() int64 { return maxSupply }
 44
 45func Admin() address { return onyxAdmin }
 46
 47// GRC-721 read facade.
 48
 49func BalanceOf(owner address) int64 {
 50	balance, err := token.BalanceOf(owner)
 51	if err != nil {
 52		panic(err)
 53	}
 54	return balance
 55}
 56
 57func OwnerOf(tokenID string) address {
 58	owner, err := token.OwnerOf(grc721.TokenID(tokenID))
 59	if err != nil {
 60		panic(err)
 61	}
 62	return owner
 63}
 64
 65func GetApproved(tokenID string) address {
 66	approved, err := token.GetApproved(grc721.TokenID(tokenID))
 67	if err != nil {
 68		panic(err)
 69	}
 70	return approved
 71}
 72
 73func IsApprovedForAll(owner, operator address) bool {
 74	return token.IsApprovedForAll(owner, operator)
 75}
 76
 77// GRC-721 write facade. CallerTeller derives the acting account from the
 78// current cross-realm frame and keeps the PrivateLedger inside this realm.
 79
 80func Approve(cur realm, to address, tokenID string) {
 81	if err := callerTeller.Approve(0, cur, to, grc721.TokenID(tokenID)); err != nil {
 82		panic(err)
 83	}
 84}
 85
 86func SetApprovalForAll(cur realm, operator address, approved bool) {
 87	if err := callerTeller.SetApprovalForAll(0, cur, operator, approved); err != nil {
 88		panic(err)
 89	}
 90}
 91
 92func TransferFrom(cur realm, from, to address, tokenID string) {
 93	if cur.Previous().Address() != from {
 94		panic("guardians: operator transfers require marketplace settlement")
 95	}
 96	if err := callerTeller.TransferFrom(0, cur, from, to, grc721.TokenID(tokenID)); err != nil {
 97		panic(err)
 98	}
 99}
100
101// AdminMint is for reserve/local-development minting and is permanently locked
102// after the first sale activation to prevent post-sale rare-token allocation.
103func AdminMint(cur realm, to address, quantity int) {
104	assertAdmin(cur.Previous().Address())
105	assertMintingAllowed()
106	if !salePaused || allowlistRootLocked {
107		panic("guardians: admin mint is locked after sale activation")
108	}
109	mintTokens(to, quantity)
110}
111
112func mintTokens(to address, quantity int) {
113	mintAllocatedTokens(to, 0, quantity)
114}
115
116func mintAllocatedTokens(to address, freeQuantity, paidQuantity int) {
117	quantity := freeQuantity + paidQuantity
118	assertMintingAllowed()
119	assertMintQuantity(quantity)
120	assertReserveCapacity(freeQuantity, paidQuantity)
121	for i := 0; i < quantity; i++ {
122		nextID := token.TotalSupply() + 1
123		tokenID := grc721.TokenID(ufmt.Sprintf("%d", nextID))
124		if err := ledger.Mint(to, tokenID); err != nil {
125			panic(err)
126		}
127		setInitialTokenURI(tokenID)
128	}
129	allowlistMintedSupply += int64(freeQuantity)
130}
131
132func Render(path string) string {
133	if path != "" {
134		return "404\n"
135	}
136
137	out := token.RenderHome()
138	out += ufmt.Sprintf("* **Maximum supply**: %d\n", maxSupply)
139	out += ufmt.Sprintf("* **Sale phase**: %s\n", SalePhase())
140	out += ufmt.Sprintf("* **Sale paused**: %t\n", SalePaused())
141	out += ufmt.Sprintf("* **Status**: %s\n", Status())
142	return out
143}
144
145func assertAdmin(caller address) {
146	if !isAdmin(caller) {
147		panic("guardians: restricted access")
148	}
149}
150
151func isAdmin(caller address) bool {
152	return caller == onyxAdmin
153}
154
155func assertMintQuantity(quantity int) {
156	if !validMintQuantity(quantity) {
157		panic(ufmt.Sprintf("guardians: quantity must be between 1 and %d", maxMintBatch))
158	}
159}
160
161func validMintQuantity(quantity int) bool {
162	return quantity >= 1 && quantity <= maxMintBatch
163}