Playground
assert.gno
package halt
// AssertIsNotHaltedPool panics if pool operations are halted.
func AssertIsNotHaltedPool() {
AssertIsNotHaltedOperation(OpTypePool)
}
// AssertIsNotHaltedPosition panics if position operations are halted.
func AssertIsNotHaltedPosition() {
AssertIsNotHaltedOperation(OpTypePosition)
}
// AssertIsNotHaltedProtocolFee panics if protocol fee operations are halted.
func AssertIsNotHaltedProtocolFee() {
AssertIsNotHaltedOperation(OpTypeProtocolFee)
}
// AssertIsNotHaltedRouter panics if router operations are halted.
func AssertIsNotHaltedRouter() {
AssertIsNotHaltedOperation(OpTypeRouter)
}
// AssertIsNotHaltedStaker panics if staker operations are halted.
func AssertIsNotHaltedStaker() {
AssertIsNotHaltedOperation(OpTypeStaker)
}
// AssertIsNotHaltedLaunchpad panics if launchpad operations are halted.
func AssertIsNotHaltedLaunchpad() {
AssertIsNotHaltedOperation(OpTypeLaunchpad)
}
// AssertIsNotHaltedGovernance panics if governance operations are halted.
func AssertIsNotHaltedGovernance() {
AssertIsNotHaltedOperation(OpTypeGovernance)
}
// AssertIsNotHaltedGovStaker panics if governance staker operations are halted.
func AssertIsNotHaltedGovStaker() {
AssertIsNotHaltedOperation(OpTypeGovStaker)
}
// AssertIsNotHaltedXGns panics if xGNS operations are halted.
func AssertIsNotHaltedXGns() {
AssertIsNotHaltedOperation(OpTypeXGns)
}
// AssertIsNotHaltedCommunityPool panics if community pool operations are halted.
func AssertIsNotHaltedCommunityPool() {
AssertIsNotHaltedOperation(OpTypeCommunityPool)
}
// AssertIsNotHaltedEmission panics if emission operations are halted.
func AssertIsNotHaltedEmission() {
AssertIsNotHaltedOperation(OpTypeEmission)
}
// AssertIsNotHaltedWithdraw panics if withdraw operations are halted.
func AssertIsNotHaltedWithdraw() {
AssertIsNotHaltedOperation(OpTypeWithdraw)
}
// AssertIsNotHaltedOperation panics if the specified operation type is halted.
// Panics with error details including operation type name.
//
// Parameters:
// - op: valid operation type whose halt status must permit execution; invalid
// operation types panic with an invalid-operation error.
func AssertIsNotHaltedOperation(op OpType) {
if halted := isHaltedOperation(op); halted {
panic(makeErrorWithDetails(errHalted, op.String()))
}
}
config.gno
package halt
// HaltConfig stores halt state for each operation type.
type HaltConfig map[OpType]bool
// IsHalted returns true if the specified operation is halted, false otherwise.
// Returns false if operation type is not found in configuration.
//
// Parameters:
// - op: operation type whose halt flag is queried.
//
// Returns:
// - halted: configured halt flag; false when op is absent from the configuration.
func (c HaltConfig) IsHalted(op OpType) bool {
halted, exists := c[op]
if !exists {
return false
}
return halted
}
// Clone creates a deep copy of the halt configuration and returns it.
//
// Returns:
// - clone: independent map containing the same operation halt flags as the receiver.
func (c HaltConfig) Clone() HaltConfig {
clone := make(HaltConfig)
for op, option := range c {
clone[op] = option
}
return clone
}
// get retrieves halt state for the specified operation type.
//
// Errors:
// - errOpTypeNotFound: the specified operation type does not exist in the config
func (c HaltConfig) get(op OpType) (bool, error) {
enabled, exists := c[op]
if !exists {
return false, makeErrorWithDetails(errOpTypeNotFound, op.String())
}
return enabled, nil
}
// set updates halt state for the specified operation type.
// Returns error if operation type is invalid.
func (c HaltConfig) set(op OpType, enabled bool) error {
if !op.IsValid() {
return makeErrorWithDetails(errInvalidOpType, op.String())
}
c[op] = enabled
return nil
}
// newNoneConfig creates configuration with all operations enabled (no halts).
func newNoneConfig() HaltConfig {
return HaltConfig{
OpTypePool: false,
OpTypePosition: false,
OpTypeProtocolFee: false,
OpTypeRouter: false,
OpTypeStaker: false,
OpTypeLaunchpad: false,
OpTypeGovernance: false,
OpTypeGovStaker: false,
OpTypeXGns: false,
OpTypeCommunityPool: false,
OpTypeEmission: false,
OpTypeWithdraw: false,
}
}
// newSafeModeConfig creates configuration for safe mode with only withdrawals halted.
func newSafeModeConfig() HaltConfig {
return HaltConfig{
OpTypePool: false,
OpTypePosition: false,
OpTypeProtocolFee: false,
OpTypeRouter: false,
OpTypeStaker: false,
OpTypeLaunchpad: false,
OpTypeGovernance: false,
OpTypeGovStaker: false,
OpTypeXGns: false,
OpTypeCommunityPool: false,
OpTypeEmission: false,
OpTypeWithdraw: true,
}
}
// newEmergencyConfig creates configuration for emergency mode with most operations halted.
// Only governance and withdraw operations remain enabled for emergency recovery.
func newEmergencyConfig() HaltConfig {
return HaltConfig{
OpTypePool: true,
OpTypePosition: true,
OpTypeProtocolFee: true,
OpTypeRouter: true,
OpTypeStaker: true,
OpTypeLaunchpad: true,
OpTypeGovernance: false,
OpTypeGovStaker: true,
OpTypeXGns: true,
OpTypeCommunityPool: true,
OpTypeEmission: true,
OpTypeWithdraw: false,
}
}
// newCompleteConfig creates configuration with all operations halted for complete lockdown.
func newCompleteConfig() HaltConfig {
return HaltConfig{
OpTypePool: true,
OpTypePosition: true,
OpTypeProtocolFee: true,
OpTypeRouter: true,
OpTypeStaker: true,
OpTypeLaunchpad: true,
OpTypeGovernance: true,
OpTypeGovStaker: true,
OpTypeXGns: true,
OpTypeCommunityPool: true,
OpTypeEmission: true,
OpTypeWithdraw: true,
}
}
doc.gno
// Package halt provides functionality for managing protocol halt levels and operations. package halt
errors.gno
package halt
import (
ufmt "gno.land/p/nt/ufmt/v0"
)
const (
errHalted = "halted"
errInvalidOpType = "invalid operation type"
errInvalidHaltLevel = "invalid halt level"
errOpTypeNotFound = "operation type not found in config"
)
// makeErrorWithDetails creates an error with additional details appended to the base error message.
func makeErrorWithDetails(message string, details string) error {
return ufmt.Errorf("%s: %s", message, details)
}
getters.gno
package halt
import (
"gno.land/p/onbloc/json/v0"
)
// IsHalted returns true if any of the specified operation types are halted.
// Returns error if any operation type is invalid.
//
// Parameters:
// - opTypes: operation types to inspect; an empty list reports no halted operations
//
// Returns:
// - halted: true when any requested operation is halted; on invalid input it
// is also true alongside the error
// - error: nil when all requested operation types are valid and inspected;
// an invalid-operation or state error otherwise
func IsHalted(opTypes ...OpType) (bool, error) {
for _, op := range opTypes {
if !op.IsValid() {
return true, makeErrorWithDetails(errInvalidOpType, op.String())
}
halted, err := haltStates.IsOperationHalted(op)
if err != nil {
return true, err
}
if halted {
return true, nil
}
}
return false, nil
}
// GetHaltConfig returns a copy of the current halt configuration.
//
// Returns:
// - config: copy of the current halt status keyed by operation type
func GetHaltConfig() HaltConfig {
return haltStates.ToConfig()
}
// GetHaltConfigJson returns the halt configuration as a JSON string.
//
// Returns:
// - json: JSON object containing each operation type's halt status
func GetHaltConfigJson() string {
haltConfig := GetHaltConfig()
statusNodes := make(map[string]*json.Node)
for op, halted := range haltConfig {
statusNodes[op.String()] = json.BoolNode(op.String(), halted)
}
objectNode := json.ObjectNode("status", statusNodes)
return objectNode.String()
}
// IsHaltedPool returns true if pool operations are halted.
//
// Returns:
// - halted: true when pool operations are halted; false otherwise
func IsHaltedPool() bool {
return isHaltedOperation(OpTypePool)
}
// IsHaltedPosition returns true if position operations are halted.
//
// Returns:
// - halted: true when position operations are halted; false otherwise
func IsHaltedPosition() bool {
return isHaltedOperation(OpTypePosition)
}
// IsHaltedProtocolFee returns true if protocol fee operations are halted.
//
// Returns:
// - halted: true when protocol-fee operations are halted; false otherwise
func IsHaltedProtocolFee() bool {
return isHaltedOperation(OpTypeProtocolFee)
}
// IsHaltedRouter returns true if router operations are halted.
//
// Returns:
// - halted: true when router operations are halted; false otherwise
func IsHaltedRouter() bool {
return isHaltedOperation(OpTypeRouter)
}
// IsHaltedStaker returns true if staker operations are halted.
//
// Returns:
// - halted: true when staker operations are halted; false otherwise
func IsHaltedStaker() bool {
return isHaltedOperation(OpTypeStaker)
}
// IsHaltedLaunchpad returns true if launchpad operations are halted.
//
// Returns:
// - halted: true when launchpad operations are halted; false otherwise
func IsHaltedLaunchpad() bool {
return isHaltedOperation(OpTypeLaunchpad)
}
// IsHaltedGovernance returns true if governance operations are halted.
//
// Returns:
// - halted: true when governance operations are halted; false otherwise
func IsHaltedGovernance() bool {
return isHaltedOperation(OpTypeGovernance)
}
// IsHaltedGovStaker returns true if governance staker operations are halted.
//
// Returns:
// - halted: true when governance-staker operations are halted; false otherwise
func IsHaltedGovStaker() bool {
return isHaltedOperation(OpTypeGovStaker)
}
// IsHaltedXGns returns true if xGNS operations are halted.
//
// Returns:
// - halted: true when xGNS operations are halted; false otherwise
func IsHaltedXGns() bool {
return isHaltedOperation(OpTypeXGns)
}
// IsHaltedCommunityPool returns true if community pool operations are halted.
//
// Returns:
// - halted: true when community-pool operations are halted; false otherwise
func IsHaltedCommunityPool() bool {
return isHaltedOperation(OpTypeCommunityPool)
}
// IsHaltedEmission returns true if emission operations are halted.
//
// Returns:
// - halted: true when emission operations are halted; false otherwise
func IsHaltedEmission() bool {
return isHaltedOperation(OpTypeEmission)
}
// IsHaltedWithdraw returns true if withdraw operations are halted.
//
// Returns:
// - halted: true when withdrawal operations are halted; false otherwise
func IsHaltedWithdraw() bool {
return isHaltedOperation(OpTypeWithdraw)
}
// isHaltedOperation returns halt status for the specified operation type.
// Panics if operation type is invalid.
func isHaltedOperation(op OpType) bool {
if !op.IsValid() {
panic(makeErrorWithDetails(errInvalidOpType, op.String()))
}
halted, err := haltStates.IsOperationHalted(op)
if err != nil {
panic(err)
}
return halted
}
gnomod.toml
module = "gno.land/r/gnoswap/halt/v1" gno = "0.9" [addpkg] creator = "g1q5tjjv3kkslzwf2na9y9wk7e5a8ugph8xfsk04" height = 77247 max_deposit = "100000000ugnot"
halt.gno
package halt
import (
"chain"
"strconv"
"gno.land/r/gnoswap/access/v1"
)
var haltStates HaltStateManager
func init() {
haltStates = newHaltStateManagerByConfig(newNoneConfig())
}
// SetHaltLevel sets the global halt level.
//
// Parameters:
// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
// - level: halt level to apply (None, SafeMode, Emergency, or Complete).
//
// Only callable by admin or governance.
func SetHaltLevel(cur realm, level HaltLevel) {
caller := cur.Previous().Address()
access.AssertIsAdminOrGovernance(caller)
err := setHaltLevel(level)
if err != nil {
panic(err)
}
chain.Emit(
"SetHaltLevel",
"level", level.String(),
"description", level.Description(),
"caller", caller.String(),
)
}
// SetOperationStatus sets the halt status for a specific operation.
//
// Parameters:
// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
// - op: operation type whose halt status is changed.
// - halted: true to block the operation, false to allow it.
//
// Only callable by admin or governance.
func SetOperationStatus(cur realm, op OpType, halted bool) {
caller := cur.Previous().Address()
access.AssertIsAdminOrGovernance(caller)
if !op.IsValid() {
panic(makeErrorWithDetails(errInvalidOpType, op.String()))
}
err := haltStates.setOperationHalted(op, halted)
if err != nil {
panic(err)
}
chain.Emit(
"SetOperationStatus",
"operation", string(op),
"halted", strconv.FormatBool(halted),
"caller", caller.String(),
)
}
// setHaltLevel applies predefined halt level configuration.
func setHaltLevel(level HaltLevel) error {
var config HaltConfig
switch level {
case HaltLevelNone:
config = newNoneConfig()
case HaltLevelSafeMode:
config = newSafeModeConfig()
case HaltLevelEmergency:
config = newEmergencyConfig()
case HaltLevelComplete:
config = newCompleteConfig()
default:
return makeErrorWithDetails(errInvalidHaltLevel, level.String())
}
return haltStates.updateOperationHaltsByConfig(config)
}
halt_state.gno
package halt
type HaltStateManager map[OpType]bool
// IsOperationHalted returns the halt state for an operation type.
//
// Parameters:
// - op: operation type whose halt flag is queried.
//
// Returns:
// - halted: configured halt flag for op.
// - error: nil when op exists; otherwise errOpTypeNotFound.
func (h HaltStateManager) IsOperationHalted(op OpType) (bool, error) {
return h.getOperationHalted(op)
}
// ToConfig converts the HaltStateManager to an independent HaltConfig copy.
//
// Returns:
// - config: copied operation-to-halt-flag map.
func (h HaltStateManager) ToConfig() HaltConfig {
config := make(HaltConfig)
for op, halted := range h {
config[op] = halted
}
return config
}
// getOperationHalted retrieves halt state for the specified operation type.
//
// Errors:
// - errOpTypeNotFound: the specified operation type does not exist in the manager
func (h HaltStateManager) getOperationHalted(op OpType) (bool, error) {
halted, exists := h[op]
if !exists {
return false, makeErrorWithDetails(errOpTypeNotFound, op.String())
}
return halted, nil
}
func (h HaltStateManager) setOperationHalted(op OpType, halted bool) error {
if !op.IsValid() {
return makeErrorWithDetails(errInvalidOpType, op.String())
}
h[op] = halted
return nil
}
func (h HaltStateManager) updateOperationHaltsByConfig(config HaltConfig) error {
for op, halted := range config {
if err := h.setOperationHalted(op, halted); err != nil {
return err
}
}
return nil
}
func newHaltStateManagerByConfig(config HaltConfig) HaltStateManager {
haltState := make(HaltStateManager)
for _, op := range OpTypes() {
halted, exists := config[op]
if !exists {
halted = true
}
haltState[op] = halted
}
return haltState
}
render.gno
package halt
import (
"gno.land/p/moul/md/v0"
"gno.land/p/moul/mdtable/v0"
ufmt "gno.land/p/nt/ufmt/v0"
)
// Render returns the current halt status for every operation.
func Render(path string) string {
if path != "" {
return "404\n"
}
table := mdtable.Table{
Headers: []string{"Operation", "Halted"},
Rows: [][]string{
{"Pool", ufmt.Sprintf("%t", IsHaltedPool())},
{"Position", ufmt.Sprintf("%t", IsHaltedPosition())},
{"Protocol fee", ufmt.Sprintf("%t", IsHaltedProtocolFee())},
{"Router", ufmt.Sprintf("%t", IsHaltedRouter())},
{"Staker", ufmt.Sprintf("%t", IsHaltedStaker())},
{"Launchpad", ufmt.Sprintf("%t", IsHaltedLaunchpad())},
{"Governance", ufmt.Sprintf("%t", IsHaltedGovernance())},
{"Governance staker", ufmt.Sprintf("%t", IsHaltedGovStaker())},
{"xGNS", ufmt.Sprintf("%t", IsHaltedXGns())},
{"Community pool", ufmt.Sprintf("%t", IsHaltedCommunityPool())},
{"Emission", ufmt.Sprintf("%t", IsHaltedEmission())},
{"Withdraw", ufmt.Sprintf("%t", IsHaltedWithdraw())},
},
}
return md.H1("Gnoswap Halt Status") +
"\n" +
md.H2("Operation status") +
"\n" +
table.String()
}
types.gno
package halt
// Halt levels define different states of system operation restriction.
const (
HaltLevelNone HaltLevel = "NONE" // All operations enabled.
HaltLevelSafeMode HaltLevel = "SAFE_MODE" // All operations enabled except withdrawals.
HaltLevelEmergency HaltLevel = "EMERGENCY" // Only governance and withdrawal operations enabled.
HaltLevelComplete HaltLevel = "COMPLETE" // All operations disabled.
)
// Operation types representing individual contracts.
const (
OpTypePool OpType = "pool"
OpTypePosition OpType = "position"
OpTypeProtocolFee OpType = "protocol_fee"
OpTypeRouter OpType = "router"
OpTypeStaker OpType = "staker"
OpTypeLaunchpad OpType = "launchpad"
OpTypeGovernance OpType = "governance"
OpTypeGovStaker OpType = "gov_staker"
OpTypeXGns OpType = "xgns"
OpTypeCommunityPool OpType = "community_pool"
OpTypeEmission OpType = "emission"
OpTypeWithdraw OpType = "withdraw"
)
// HaltLevel represents current system halt state.
type HaltLevel string
// String returns the string representation of the halt level.
//
// Returns:
// - text: exact string value represented by the halt level
func (h HaltLevel) String() string {
return string(h)
}
// Description returns a human-readable description of the halt level.
//
// Returns:
// - description: human-readable policy description; "Unknown halt level" for
// an unrecognized value
func (h HaltLevel) Description() string {
haltLevelDescriptions := map[HaltLevel]string{
HaltLevelNone: "All operations enabled",
HaltLevelSafeMode: "All operations enabled except withdrawals",
HaltLevelEmergency: "Only governance and withdrawal operations enabled",
HaltLevelComplete: "All operations disabled",
}
desc, ok := haltLevelDescriptions[h]
if !ok {
return "Unknown halt level"
}
return desc
}
// IsValid returns true if the halt level is valid.
//
// Returns:
// - valid: true for one of the defined halt levels; false otherwise
func (h HaltLevel) IsValid() bool {
switch h {
case HaltLevelNone, HaltLevelSafeMode, HaltLevelEmergency, HaltLevelComplete:
return true
default:
return false
}
}
// OpType represents operation types that can be controlled independently.
type OpType string
var opTypes = map[OpType]bool{
OpTypePool: true,
OpTypePosition: true,
OpTypeProtocolFee: true,
OpTypeRouter: true,
OpTypeStaker: true,
OpTypeLaunchpad: true,
OpTypeGovernance: true,
OpTypeGovStaker: true,
OpTypeXGns: true,
OpTypeCommunityPool: true,
OpTypeEmission: true,
OpTypeWithdraw: true,
}
// String returns the string representation of the operation type.
//
// Returns:
// - text: exact string value represented by the operation type
func (o OpType) String() string {
return string(o)
}
// IsValid returns true if the operation type is valid.
//
// Returns:
// - valid: true when the operation type is defined; false otherwise
func (o OpType) IsValid() bool {
_, ok := opTypes[o]
return ok
}
// OpTypes returns all operation types currently supported by the halt
// configuration.
//
// Returns:
// - operationTypes: slice containing each defined operation type in map iteration order
func OpTypes() []OpType {
allOpTypes := make([]OpType, 0, len(opTypes))
for op := range opTypes {
allOpTypes = append(allOpTypes, op)
}
return allOpTypes
}