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

Playground

consts.gno

package halt

// Operations that can be halted.
const (
	OperationExecute  = "execute"
	OperationWithdraw = "withdraw"
)

// TargetAll addresses every target at once.
const TargetAll = "all"

// Targets registered at deployment; more are added with AddTarget.
const (
	TargetDCA   = "dca"
	TargetLimit = "limit"
)

// maxTargetLength bounds a target name.
const maxTargetLength = 32

var operations = []string{OperationExecute, OperationWithdraw}

doc.gno

// Package halt holds the halt switches shared by the GnoSwap order realms
// (gno.land/r/gnoswap/order/...).
//
// Each target (an order type such as "dca" or "limit") has a switch per
// operation. Admin or governance turns a switch on for one target, or for
// every target with TargetAll, and can add targets for new order realms.
// Order realms check IsHalted with their own target, which reports the
// operation halted when either switch is on, and decide which entry points
// each operation guards.
//
// Operations:
//   - OperationExecute: creating or executing orders
//   - OperationWithdraw: executing orders, paying out or refunding funds
//
// The switches are independent of the GnoSwap protocol halt levels.
package halt

errors.gno

package halt

import (
	ufmt "gno.land/p/nt/ufmt/v0"
)

const (
	errInvalidOperation = "[GNOSWAP-ORDER-HALT-001] invalid operation"
	errInvalidTarget    = "[GNOSWAP-ORDER-HALT-002] invalid target"
	errTargetExists     = "[GNOSWAP-ORDER-HALT-003] target already exists"
)

func makeErrorWithDetails(message string, details string) error {
	return ufmt.Errorf("%s || %s", message, details)
}

gnomod.toml

module = "gno.land/r/gnoswap/order/halt"
gno = "0.9"

[addpkg]
  creator = "g1q5tjjv3kkslzwf2na9y9wk7e5a8ugph8xfsk04"
  height = 1108545
  max_deposit = "100000000ugnot"

halt.gno

package halt

import (
	"chain"

	"gno.land/p/gnoswap/utils/v1"
	"gno.land/p/onbloc/json/v0"

	"gno.land/r/gnoswap/access/v1"
)

var (
	// targets lists the registered targets, in registration order.
	targets = []string{TargetDCA, TargetLimit}

	// halted holds the switches that are on, keyed by "<target>:<operation>".
	halted = map[string]bool{}
)

// AddTarget registers a target, e.g. for a new order realm.
// Admin or governance only.
//
// A target is 1 to 32 characters of lowercase letters, digits, '_', or '-',
// and cannot be TargetAll.
func AddTarget(cur realm, target string) {
	caller := cur.Previous()
	access.AssertIsAdminOrGovernance(caller.Address())

	if err := validateTargetName(target); err != nil {
		panic(err.Error())
	}

	if isTarget(target) {
		panic(makeErrorWithDetails(errTargetExists, "target("+target+")").Error())
	}

	targets = append(targets, target)

	chain.Emit(
		"OrderHaltTargetAdded",
		"prevAddr", caller.Address().String(),
		"prevRealm", caller.PkgPath(),
		"target", target,
	)
}

// SetHalted halts or resumes operation for target. Admin or governance only.
//
// Parameters:
//   - target: a registered target, or TargetAll for every target.
//   - operation: OperationExecute or OperationWithdraw.
func SetHalted(cur realm, target string, operation string, isHalted bool) {
	caller := cur.Previous()
	access.AssertIsAdminOrGovernance(caller.Address())

	if err := validate(target, operation); err != nil {
		panic(err.Error())
	}

	key := switchKey(target, operation)

	prevValue := halted[key]
	if isHalted {
		halted[key] = true
	} else {
		delete(halted, key)
	}

	chain.Emit(
		"OrderHaltUpdated",
		"prevAddr", caller.Address().String(),
		"prevRealm", caller.PkgPath(),
		"target", target,
		"operation", operation,
		"prevValue", utils.FormatBool(prevValue),
		"newValue", utils.FormatBool(isHalted),
	)
}

// IsHalted reports whether operation is halted for target,
// by its own switch or by the TargetAll switch.
// It panics on an unknown target or operation.
func IsHalted(target string, operation string) bool {
	if err := validate(target, operation); err != nil {
		panic(err.Error())
	}

	return halted[switchKey(TargetAll, operation)] || halted[switchKey(target, operation)]
}

// GetTargets returns the registered targets, in registration order.
func GetTargets() []string {
	out := make([]string, len(targets))
	copy(out, targets)

	return out
}

// GetOperations returns the operations that can be halted.
func GetOperations() []string {
	out := make([]string, len(operations))
	copy(out, operations)

	return out
}

// GetHaltedJSON returns, as JSON, whether each operation is halted for
// target, including the TargetAll switches.
func GetHaltedJSON(target string) string {
	if err := validateTarget(target); err != nil {
		panic(err.Error())
	}

	nodes := make(map[string]*json.Node, len(operations))
	for _, operation := range operations {
		nodes[operation] = json.BoolNode("", IsHalted(target, operation))
	}

	encoded, err := json.Marshal(json.ObjectNode("", nodes))
	if err != nil {
		panic(err.Error())
	}

	return string(encoded)
}

func validate(target string, operation string) error {
	if err := validateTarget(target); err != nil {
		return err
	}

	if !contains(operations, operation) {
		return makeErrorWithDetails(errInvalidOperation, "operation("+operation+")")
	}

	return nil
}

func validateTarget(target string) error {
	if target != TargetAll && !isTarget(target) {
		return makeErrorWithDetails(errInvalidTarget, "target("+target+") is not registered")
	}

	return nil
}

func validateTargetName(target string) error {
	if target == "" || len(target) > maxTargetLength || target == TargetAll {
		return makeErrorWithDetails(errInvalidTarget, "target("+target+")")
	}

	for _, c := range target {
		if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_' || c == '-') {
			return makeErrorWithDetails(errInvalidTarget, "target("+target+") has an invalid character")
		}
	}

	return nil
}

func isTarget(target string) bool {
	return contains(targets, target)
}

func switchKey(target string, operation string) string {
	return target + ":" + operation
}

func contains(values []string, value string) bool {
	for _, v := range values {
		if v == value {
			return true
		}
	}

	return false
}