Search Apps Documentation Source Content File Folder Download Copy Actions Download

admin.gno

2.12 Kb · 78 lines
 1package gnogle_nftmarket
 2
 3import (
 4	"chain"
 5
 6	"gno.land/p/nt/ufmt/v0"
 7)
 8
 9// ClaimAdmin assigns the platform admin to the caller. It can only be called
10// once (when no admin is set yet). Deployers should call this immediately
11// after publishing the realm — ideally in the same deploy script — to avoid
12// someone else claiming it first.
13func ClaimAdmin(cur realm) {
14	if admin != zeroAddr {
15		panic("admin already claimed")
16	}
17	admin = cur.Previous().Address()
18	chain.Emit("ClaimAdmin", "admin", admin.String())
19}
20
21// SetAdmin transfers the platform admin role to newAdmin. Admin only.
22func SetAdmin(cur realm, newAdmin address) {
23	assertAdmin(cur)
24	if !newAdmin.IsValid() {
25		panic("invalid admin address")
26	}
27	prev := admin
28	admin = newAdmin
29	chain.Emit("SetAdmin", "from", prev.String(), "to", newAdmin.String())
30}
31
32// SetFeeBps sets the platform fee charged on secondary sales, in basis points
33// (e.g. 250 = 2.5%). Capped at maxFeeBps. Admin only.
34func SetFeeBps(cur realm, bps int64) {
35	assertAdmin(cur)
36	if bps < 0 || bps > maxFeeBps {
37		panic(ufmt.Sprintf("feeBps must be between 0 and %d", maxFeeBps))
38	}
39	feeBps = bps
40	chain.Emit("SetFee", "bps", ufmt.Sprintf("%d", bps))
41}
42
43// WithdrawFees sends all accrued platform fees to addr and returns the amount.
44// Admin only.
45func WithdrawFees(cur realm, addr address) int64 {
46	assertAdmin(cur)
47	if !addr.IsValid() {
48		panic("invalid address")
49	}
50	amount := feePot
51	if amount <= 0 {
52		return 0
53	}
54	feePot = 0
55	payout(cur, addr, amount)
56	chain.Emit("WithdrawFees", "to", addr.String(), "amount", ufmt.Sprintf("%d", amount))
57	return amount
58}
59
60func assertAdmin(cur realm) {
61	if admin == zeroAddr {
62		panic("admin not set; call ClaimAdmin first")
63	}
64	if cur.Previous().Address() != admin {
65		panic("unauthorized: admin only")
66	}
67}
68
69// --- read-only views --------------------------------------------------------
70
71// Admin returns the current platform admin address.
72func Admin() address { return admin }
73
74// FeeBps returns the current platform fee in basis points.
75func FeeBps() int64 { return feeBps }
76
77// FeePot returns the amount of ugnot currently withdrawable as platform fees.
78func FeePot() int64 { return feePot }