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

halt.gno

4.19 Kb · 177 lines
  1package halt
  2
  3import (
  4	"chain"
  5
  6	"gno.land/p/gnoswap/utils/v1"
  7	"gno.land/p/onbloc/json/v0"
  8
  9	"gno.land/r/gnoswap/access/v1"
 10)
 11
 12var (
 13	// targets lists the registered targets, in registration order.
 14	targets = []string{TargetDCA, TargetLimit}
 15
 16	// halted holds the switches that are on, keyed by "<target>:<operation>".
 17	halted = map[string]bool{}
 18)
 19
 20// AddTarget registers a target, e.g. for a new order realm.
 21// Admin or governance only.
 22//
 23// A target is 1 to 32 characters of lowercase letters, digits, '_', or '-',
 24// and cannot be TargetAll.
 25func AddTarget(cur realm, target string) {
 26	caller := cur.Previous()
 27	access.AssertIsAdminOrGovernance(caller.Address())
 28
 29	if err := validateTargetName(target); err != nil {
 30		panic(err.Error())
 31	}
 32
 33	if isTarget(target) {
 34		panic(makeErrorWithDetails(errTargetExists, "target("+target+")").Error())
 35	}
 36
 37	targets = append(targets, target)
 38
 39	chain.Emit(
 40		"OrderHaltTargetAdded",
 41		"prevAddr", caller.Address().String(),
 42		"prevRealm", caller.PkgPath(),
 43		"target", target,
 44	)
 45}
 46
 47// SetHalted halts or resumes operation for target. Admin or governance only.
 48//
 49// Parameters:
 50//   - target: a registered target, or TargetAll for every target.
 51//   - operation: OperationExecute or OperationWithdraw.
 52func SetHalted(cur realm, target string, operation string, isHalted bool) {
 53	caller := cur.Previous()
 54	access.AssertIsAdminOrGovernance(caller.Address())
 55
 56	if err := validate(target, operation); err != nil {
 57		panic(err.Error())
 58	}
 59
 60	key := switchKey(target, operation)
 61
 62	prevValue := halted[key]
 63	if isHalted {
 64		halted[key] = true
 65	} else {
 66		delete(halted, key)
 67	}
 68
 69	chain.Emit(
 70		"OrderHaltUpdated",
 71		"prevAddr", caller.Address().String(),
 72		"prevRealm", caller.PkgPath(),
 73		"target", target,
 74		"operation", operation,
 75		"prevValue", utils.FormatBool(prevValue),
 76		"newValue", utils.FormatBool(isHalted),
 77	)
 78}
 79
 80// IsHalted reports whether operation is halted for target,
 81// by its own switch or by the TargetAll switch.
 82// It panics on an unknown target or operation.
 83func IsHalted(target string, operation string) bool {
 84	if err := validate(target, operation); err != nil {
 85		panic(err.Error())
 86	}
 87
 88	return halted[switchKey(TargetAll, operation)] || halted[switchKey(target, operation)]
 89}
 90
 91// GetTargets returns the registered targets, in registration order.
 92func GetTargets() []string {
 93	out := make([]string, len(targets))
 94	copy(out, targets)
 95
 96	return out
 97}
 98
 99// GetOperations returns the operations that can be halted.
100func GetOperations() []string {
101	out := make([]string, len(operations))
102	copy(out, operations)
103
104	return out
105}
106
107// GetHaltedJSON returns, as JSON, whether each operation is halted for
108// target, including the TargetAll switches.
109func GetHaltedJSON(target string) string {
110	if err := validateTarget(target); err != nil {
111		panic(err.Error())
112	}
113
114	nodes := make(map[string]*json.Node, len(operations))
115	for _, operation := range operations {
116		nodes[operation] = json.BoolNode("", IsHalted(target, operation))
117	}
118
119	encoded, err := json.Marshal(json.ObjectNode("", nodes))
120	if err != nil {
121		panic(err.Error())
122	}
123
124	return string(encoded)
125}
126
127func validate(target string, operation string) error {
128	if err := validateTarget(target); err != nil {
129		return err
130	}
131
132	if !contains(operations, operation) {
133		return makeErrorWithDetails(errInvalidOperation, "operation("+operation+")")
134	}
135
136	return nil
137}
138
139func validateTarget(target string) error {
140	if target != TargetAll && !isTarget(target) {
141		return makeErrorWithDetails(errInvalidTarget, "target("+target+") is not registered")
142	}
143
144	return nil
145}
146
147func validateTargetName(target string) error {
148	if target == "" || len(target) > maxTargetLength || target == TargetAll {
149		return makeErrorWithDetails(errInvalidTarget, "target("+target+")")
150	}
151
152	for _, c := range target {
153		if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_' || c == '-') {
154			return makeErrorWithDetails(errInvalidTarget, "target("+target+") has an invalid character")
155		}
156	}
157
158	return nil
159}
160
161func isTarget(target string) bool {
162	return contains(targets, target)
163}
164
165func switchKey(target string, operation string) string {
166	return target + ":" + operation
167}
168
169func contains(values []string, value string) bool {
170	for _, v := range values {
171		if v == value {
172			return true
173		}
174	}
175
176	return false
177}