patus404.gno
6.95 Kb · 213 lines
1package patusworld404onyx
2
3import (
4 "chain/banker"
5 "chain/runtime/unsafe"
6 "strconv"
7 "time"
8 "gno.land/p/nt/avl/v0"
9 "gno.land/p/nt/grc721/v0"
10 "gno.land/p/nt/grc721/metadata/v0"
11)
12
13const (
14 MaxSupply = 404
15 PaidSupply = 378
16 FreeSupply = 24
17 BatchLimit = 10
18 PaidWalletLimit = 10
19 WhitelistSeconds int64 = 15 * 24 * 60 * 60
20)
21
22var (
23 collection *grc721.Token
24 ledger *grc721.PrivateLedger
25 nftMeta *metadata.Metadata
26 metaLedger *metadata.Ledger
27 admin address
28 recovery address
29 creator address
30 treasury address
31 opened bool
32 paused bool
33 openedAt int64
34 pausedAt int64
35 pauseSeconds int64
36 totalMinted int
37 paidMinted int
38 freeMinted int
39 creatorMinted int
40 whitelist avl.Tree
41 claimed avl.Tree
42 paidByWallet avl.Tree
43)
44
45func init(cur realm) {
46 collection, ledger = grc721.NewToken("404 PATUS NOT FOUND", "PATUS404", 0, cur)
47 nftMeta, metaLedger = metadata.NewMetadata(ledger)
48 admin = cur.Previous().Address()
49 creator = admin
50}
51
52func requireAdmin(cur realm) {
53 if cur.Previous().Address() != admin { panic("admin only") }
54}
55
56func Configure(cur realm, receiver, recoveryWallet, creatorWallet address) {
57 requireAdmin(cur)
58 if opened { panic("configuration frozen") }
59 if !receiver.IsValid() || !recoveryWallet.IsValid() || !creatorWallet.IsValid() { panic("invalid address") }
60 if receiver == admin { panic("treasury must differ from admin") }
61 treasury, recovery, creator = receiver, recoveryWallet, creatorWallet
62}
63
64func SetWhitelist(cur realm, wallet address, enabled bool) {
65 requireAdmin(cur)
66 if opened { panic("whitelist frozen") }
67 if !wallet.IsValid() { panic("invalid address") }
68 old := whitelist.Get(wallet.String()) != nil
69 if enabled && !old {
70 if WhitelistCount() >= FreeSupply { panic("24 whitelist slots maximum") }
71 whitelist.Set(wallet.String(), true)
72 } else if !enabled && old {
73 whitelist.Remove(wallet.String())
74 }
75}
76
77func WhitelistCount() int {
78 return whitelist.Size()
79}
80
81func OpenMint(cur realm) {
82 requireAdmin(cur)
83 if opened { panic("already opened") }
84 if !treasury.IsValid() || !recovery.IsValid() { panic("configure wallets first") }
85 opened = true
86 openedAt = time.Now().Unix()
87}
88
89func PauseMint(cur realm) {
90 requireAdmin(cur)
91 if !opened || paused { panic("mint not active") }
92 paused = true
93 pausedAt = time.Now().Unix()
94}
95
96func ResumeMint(cur realm) {
97 requireAdmin(cur)
98 if !paused { panic("not paused") }
99 pauseSeconds += time.Now().Unix() - pausedAt
100 pausedAt = 0
101 paused = false
102}
103
104func ReplaceAdmin(cur realm, newAdmin address) {
105 who := cur.Previous().Address()
106 if who != admin && who != recovery { panic("not authorized") }
107 if !newAdmin.IsValid() { panic("invalid admin") }
108 admin = newAdmin
109}
110
111func PriceAt(paidIndex int) int64 {
112 if paidIndex < 0 || paidIndex >= PaidSupply { panic("paid supply exceeded") }
113 if paidIndex < 24 { return 44000 }
114 if paidIndex < 68 { return 96000 }
115 if paidIndex < 334 { return 144000 }
116 return 404000
117}
118
119func PaidMintedBy(wallet address) int {
120 value := paidByWallet.Get(wallet.String())
121 if value == nil { return 0 }
122 return value.(int)
123}
124
125func IsWhitelisted(wallet address) bool { return whitelist.Get(wallet.String()) != nil }
126func HasClaimed(wallet address) bool { return claimed.Get(wallet.String()) != nil }
127
128func FreeClaimAvailable(wallet address) bool {
129 if !opened || paused || freeMinted >= FreeSupply || !IsWhitelisted(wallet) || HasClaimed(wallet) { return false }
130 elapsed := time.Now().Unix() - openedAt - pauseSeconds
131 return elapsed < WhitelistSeconds || paidMinted < PaidSupply
132}
133
134func QuoteMint(wallet address, quantity int) int64 {
135 if !opened || paused || quantity < 1 || quantity > BatchLimit { panic("mint unavailable or invalid quantity") }
136 if totalMinted + quantity > MaxSupply - (2 - creatorMinted) { panic("creator reserve protected") }
137 paidQty := quantity
138 if FreeClaimAvailable(wallet) { paidQty-- }
139 if paidMinted + paidQty > PaidSupply || PaidMintedBy(wallet) + paidQty > PaidWalletLimit { panic("paid limit exceeded") }
140 var total int64
141 for i := 0; i < paidQty; i++ { total += PriceAt(paidMinted+i) }
142 return total
143}
144
145func mintOne(to address) {
146 totalMinted++
147 tokenID := strconv.Itoa(totalMinted)
148 uri, _ := catalogEntry(tokenID)
149 if uri == "" { panic("missing catalog entry") }
150 if err := ledger.Mint(to, grc721.TokenID(tokenID)); err != nil { panic(err) }
151 if err := metaLedger.SetTokenURI(grc721.TokenID(tokenID), uri); err != nil { panic(err) }
152}
153
154// Mint accepts exactly the quoted amount in ugnot. maxAuthorized is a price ceiling,
155// not an instruction to send more than the actual quote.
156func Mint(cur realm, quantity int, maxAuthorized int64) {
157 if !cur.Previous().IsUserCall() { panic("direct wallet call only") }
158 caller := cur.Previous().Address()
159 cost := QuoteMint(caller, quantity)
160 if cost > maxAuthorized { panic("price changed: exceeds authorization") }
161 sent := unsafe.OriginSend()
162 if cost == 0 {
163 if len(sent) != 0 { panic("free mint: no payment required") }
164 } else if len(sent) != 1 || sent[0].Denom != "ugnot" || sent[0].Amount != cost {
165 panic("send exact quote in ugnot")
166 }
167 free := FreeClaimAvailable(caller)
168 paidQty := quantity
169 if free { paidQty-- }
170 for i := 0; i < quantity; i++ { mintOne(caller) }
171 if free { claimed.Set(caller.String(), true); freeMinted++ }
172 paidMinted += paidQty
173 paidByWallet.Set(caller.String(), PaidMintedBy(caller)+paidQty)
174 if cost > 0 {
175 bank := banker.NewBanker(banker.BankerTypeRealmSend, cur)
176 bank.SendCoins(cur.Address(), treasury, sent)
177 }
178}
179
180func MintCreator(cur realm) {
181 if cur.Previous().Address() != creator { panic("creator only") }
182 if creatorMinted >= 2 { panic("creator reserve exhausted") }
183 if opened && paused { panic("paused") }
184 if totalMinted >= MaxSupply { panic("supply exhausted") }
185 if len(unsafe.OriginSend()) != 0 { panic("creator mint is free") }
186 mintOne(creator)
187 creatorMinted++
188}
189
190func TotalSupply() int64 { return collection.TotalSupply() }
191func PaidMinted() int { return paidMinted }
192func FreeMinted() int { return freeMinted }
193func CreatorMinted() int { return creatorMinted }
194func RemainingSupply() int { return MaxSupply-totalMinted }
195func OwnerOf(id string) string {
196 owner, err := collection.OwnerOf(grc721.TokenID(id))
197 if err != nil { return "" }
198 return owner.String()
199}
200func TokenURI(id string) string {
201 uri, err := nftMeta.TokenURI(grc721.TokenID(id))
202 if err != nil { return "" }
203 return uri
204}
205func TokenMetadata(id grc721.TokenID) (string, error) {
206 _, err := collection.OwnerOf(id)
207 if err != nil { return "", err }
208 _, json := catalogEntry(string(id))
209 return json, nil
210}
211func TransferFrom(cur realm, from, to address, id string) {
212 if err := ledger.TransferFrom(cur.Previous().Address(), from, to, grc721.TokenID(id)); err != nil { panic(err) }
213}