Search Apps Documentation Source Content File Folder Download Copy Actions Download

halt.gno

1.79 Kb · 85 lines
 1package halt
 2
 3import (
 4	"chain"
 5	"strconv"
 6
 7	"gno.land/r/gnoswap/access"
 8)
 9
10var haltStates HaltStateManager
11
12func init() {
13	haltStates = newHaltStateManagerByConfig(newNoneConfig())
14}
15
16// SetHaltLevel sets the global halt level.
17//
18// Parameters:
19//   - level: halt level to apply (None, SafeMode, Emergency, Complete)
20//
21// Only callable by admin or governance.
22func SetHaltLevel(cur realm, level HaltLevel) {
23	caller := cur.Previous().Address()
24	access.AssertIsAdminOrGovernance(caller)
25
26	err := setHaltLevel(level)
27	if err != nil {
28		panic(err)
29	}
30
31	chain.Emit(
32		"SetHaltLevel",
33		"level", level.String(),
34		"description", level.Description(),
35		"caller", caller.String(),
36	)
37}
38
39// SetOperationStatus sets halt status for a specific operation.
40//
41// Parameters:
42//   - op: operation type
43//   - halted: true to halt, false to resume
44//
45// Only callable by admin or governance.
46func SetOperationStatus(cur realm, op OpType, halted bool) {
47	caller := cur.Previous().Address()
48	access.AssertIsAdminOrGovernance(caller)
49
50	if !op.IsValid() {
51		panic(makeErrorWithDetails(errInvalidOpType, op.String()))
52	}
53
54	err := haltStates.setOperationHalted(op, halted)
55	if err != nil {
56		panic(err)
57	}
58
59	chain.Emit(
60		"SetOperationStatus",
61		"operation", string(op),
62		"halted", strconv.FormatBool(halted),
63		"caller", caller.String(),
64	)
65}
66
67// setHaltLevel applies predefined halt level configuration.
68func setHaltLevel(level HaltLevel) error {
69	var config HaltConfig
70
71	switch level {
72	case HaltLevelNone:
73		config = newNoneConfig()
74	case HaltLevelSafeMode:
75		config = newSafeModeConfig()
76	case HaltLevelEmergency:
77		config = newEmergencyConfig()
78	case HaltLevelComplete:
79		config = newCompleteConfig()
80	default:
81		return makeErrorWithDetails(errInvalidHaltLevel, level.String())
82	}
83
84	return haltStates.updateOperationHaltsByConfig(config)
85}