admin.gno
2.46 Kb · 97 lines
1package gnocial
2
3import (
4 "chain"
5 "chain/banker"
6 "chain/runtime/unsafe"
7 "strconv"
8 "strings"
9)
10
11// SetPaused freezes posts, replies, tips, and ticket buys when on is true/1/yes.
12// Pass false/0/no to reopen. Ticket sells are never paused.
13func SetPaused(cur realm, on string) {
14 mustAdmin(cur)
15 switch on {
16 case "true", "1", "yes":
17 state.paused = true
18 case "false", "0", "no":
19 state.paused = false
20 default:
21 panic("paused must be true or false")
22 }
23 chain.Emit("ArenaPause", "paused", on, "admin", state.admin.String())
24}
25
26// SetTreasury moves future protocol fees and surplus sweeps to next.
27func SetTreasury(cur realm, next address) {
28 mustAdmin(cur)
29 if !next.IsValid() {
30 panic("bad treasury")
31 }
32 state.treasury = next
33 chain.Emit("ArenaTreasury", "treasury", next.String())
34}
35
36// TransferAdmin rotates the pause/sweep key.
37func TransferAdmin(cur realm, next address) {
38 mustAdmin(cur)
39 if !next.IsValid() {
40 panic("bad admin")
41 }
42 state.admin = next
43 chain.Emit("ArenaAdmin", "admin", next.String())
44}
45
46// SetSuccessor records the package path of a later realm.
47// An empty path clears it. This does not move coins or shares.
48func SetSuccessor(cur realm, next string) {
49 mustAdmin(cur)
50 next = strings.TrimSpace(next)
51 if next != "" {
52 if !validSuccessor(next) || next == unsafe.CurrentRealm().PkgPath() {
53 panic("bad successor")
54 }
55 }
56 state.successor = next
57 chain.Emit("ArenaSuccessor", "path", next)
58}
59
60func validSuccessor(path string) bool {
61 const prefix = "gno.land/r/"
62 if !strings.HasPrefix(path, prefix) || len(path) > 128 {
63 return false
64 }
65 rest := path[len(prefix):]
66 if rest == "" || strings.HasSuffix(rest, "/") || strings.Contains(rest, "//") || strings.Contains(rest, ".") {
67 return false
68 }
69 for i := 0; i < len(rest); i++ {
70 c := rest[i]
71 switch {
72 case c >= 'a' && c <= 'z', c >= '0' && c <= '9', c == '/', c == '_':
73 default:
74 return false
75 }
76 }
77 return true
78}
79
80// Sweep sends this realm's ugnot above TotalReserve to the treasury.
81// confirm must be the word "sweep". Ticket collateral is not included.
82func Sweep(cur realm, confirm string) {
83 mustAdmin(cur)
84 if confirm != "sweep" {
85 panic("confirm sweep")
86 }
87 if !state.treasury.IsValid() {
88 panic("treasury not set")
89 }
90 bal := banker.NewReadonlyBanker().GetCoins(cur.Address()).AmountOf("ugnot")
91 if bal < state.totalReserve {
92 panic("insolvent")
93 }
94 surplus := bal - state.totalReserve
95 payout(cur, state.treasury, surplus, "treasury")
96 chain.Emit("ArenaSweep", "ugnot", strconv.FormatInt(surplus, 10))
97}