views.gno
2.56 Kb · 82 lines
1package drops
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/samcrew/launchpad/meta/v1"
9 "gno.land/r/samcrew/launchpad/nft/v1"
10)
11
12func num(n int64) string { return meta.JSONString(strconv.FormatInt(n, 10)) }
13
14// StagesJSON lists a collection's stages in index order. "currentPrice" is
15// the price a mint would pay now, and is only meaningful while "open" is true.
16func StagesJSON(id string) string {
17 nft.GetInfo(id) // an unknown collection aborts
18 now := time.Now().Unix()
19 var b strings.Builder
20 b.WriteByte('[')
21 for i, s := range stagesOf(id) {
22 if i > 0 {
23 b.WriteByte(',')
24 }
25 open := now >= s.Start && now < s.End
26 current := s.Price
27 if open {
28 current = priceAt(s, now)
29 }
30 b.WriteString("{" +
31 `"index":` + strconv.Itoa(i) +
32 `,"kind":` + meta.JSONString(s.Kind) +
33 `,"start":` + num(s.Start) +
34 `,"end":` + num(s.End) +
35 `,"open":` + strconv.FormatBool(open) +
36 `,"price":` + num(s.Price) +
37 `,"floor":` + num(s.Floor) +
38 `,"currentPrice":` + num(current) +
39 `,"currency":` + meta.JSONString(s.Currency) +
40 `,"feeBPS":` + num(s.FeeBPS) +
41 `,"supplyCap":` + num(s.SupplyCap) +
42 `,"perWallet":` + num(s.PerWallet) +
43 `,"root":` + meta.JSONString(s.Root) +
44 `,"gate":` + meta.JSONString(s.Gate) +
45 `,"gateLimit":` + num(s.GateLimit) +
46 `,"minted":` + num(s.Minted) + "}")
47 }
48 b.WriteByte(']')
49 return b.String()
50}
51
52// MintedBy is how many tokens who has minted in a stage. Only the chain's
53// spelling of an address is an account: any other is refused, never read as 0.
54func MintedBy(id string, index int, who string) int64 {
55 if !meta.ValidAddress(address(who)) {
56 panic("launchpad/drops: invalid account")
57 }
58 if v := minted.Get(stageKey(id, index) + who); v != nil {
59 return v.(int64)
60 }
61 return 0
62}
63
64// GateUsed reports whether a gate token has already paid for a mint in a
65// holder stage.
66func GateUsed(id string, index int, gateNumber int64) bool {
67 return gated.Has(stageKey(id, index) + strconv.FormatInt(gateNumber, 10))
68}
69
70// TermsJSON is what creating a collection and scheduling a stage cost now:
71// the collection fee in a currency and the protocol fee a new stage would
72// pin. A value of -1 means the lane is closed; config's ActionStatusJSON tells
73// whether a lane accepts new actions right now.
74func TermsJSON(currencyKey string) string {
75 t := termsOf(currencyKey)
76 return "{" +
77 `"currency":` + meta.JSONString(currencyKey) +
78 `,"collectionFee":` + num(t.collectionFee) +
79 `,"primaryFeeBPS":` + num(t.primaryFeeBPS) +
80 `,"maxPrimaryFeeBPS":` + num(maxPrimaryFeeBPS) +
81 `,"treasury":` + meta.JSONString(t.treasury.String()) + "}"
82}