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

assert.gno

package whitelist

import (
	"gno.land/p/gnoswap/order/access/manager/v0"
	pwhitelist "gno.land/p/gnoswap/order/whitelist/v0"
	prbac "gno.land/p/gnoswap/rbac/v1"
	ufmt "gno.land/p/nt/ufmt/v0"

	"gno.land/r/gnoswap/access/v1"
	"gno.land/r/gnoswap/common"
	"gno.land/r/gnoswap/order/keeper"
	"gno.land/r/gnoswap/pool"
)

func assertIsManagerCall(_ int, rlm realm, selector manager.Selector) realm {
	access.AssertIsRlmCurrent(0, rlm)
	common.AssertIsNotHandleNativeCoin()

	prev := rlm.Previous()
	caller := prev.Address()

	if access.IsAuthorized(prbac.ROLE_ADMIN.String(), caller) || access.IsAuthorized(prbac.ROLE_GOVERNANCE.String(), caller) || keeper.IsAuthorized(rlm.PkgPath(), selector, caller) {
		return prev
	}

	panic(makeErrorWithDetails(errUnauthorized, ufmt.Sprintf("caller(%s) selector(%s)", caller.String(), string(selector))).Error())
}

func assertIsValidTarget(target string) {
	if target != TargetDCA && target != TargetLimit {
		panic(makeErrorWithDetails(errInvalidTarget, ufmt.Sprintf("target(%s)", target)).Error())
	}
}

func assertIsExistingPool(poolPath string) {
	if !pool.ExistsPoolPath(poolPath) {
		panic(makeErrorWithDetails(errInvalidPool, ufmt.Sprintf("poolPath(%s) does not exist", poolPath)).Error())
	}
}

func assertIsNotWhitelistedPool(state *pwhitelist.State, target, poolPath string) {
	if state.HasPool(poolPath) {
		panic(makeErrorWithDetails(errInvalidPool, ufmt.Sprintf("target(%s) poolPath(%s) is already whitelisted", target, poolPath)).Error())
	}
}

func assertIsWhitelistedPool(state *pwhitelist.State, target, poolPath string) {
	if !state.HasPool(poolPath) {
		panic(makeErrorWithDetails(errInvalidPool, ufmt.Sprintf("target(%s) poolPath(%s) is not whitelisted", target, poolPath)).Error())
	}
}

consts.gno

package whitelist

// Targets select independently managed order pool whitelists.
const (
	TargetDCA   = "dca"
	TargetLimit = "limit"
)

doc.gno

// Package whitelist manages the direct pools available to GnoSwap orders.
//
// DCA and Limit have independent pool whitelists, selected by TargetDCA
// ("dca") and TargetLimit ("limit"). Adding or removing a pool affects only
// the selected target. Pools must exist before being added; removing one
// prevents that target's orders from trading on it, not from cancelling.
//
// Admin, governance, or callers authorized by gno.land/r/gnoswap/order/keeper
// can update membership. At deployment, AddWhitelistedPool and
// RemoveWhitelistedPool are assigned to PoolManagerRole on this realm;
// the target argument selects the pool list, not the authorization policy.
// Queries expose membership, a read-only pool tree, and whether a pair has
// an existing whitelisted direct pool at any registered fee tier.
//
// Files:
//   - whitelist.gno: AddWhitelistedPool and RemoveWhitelistedPool
//   - getters.gno: read-only queries
//   - state.gno: independent DCA and Limit whitelist states
//   - assert.gno: authorization, target, and pool validation
//   - consts.gno: supported target names
//   - emits.gno: pool membership events
package whitelist

emits.gno

package whitelist

import (
	"chain"

	"gno.land/p/gnoswap/utils/v1"
)

func emitPoolUpdated(_ int, prev realm, target, poolPath string, whitelisted bool) {
	chain.Emit(
		"OrderWhitelistedPoolUpdated",
		"prevAddr", prev.Address().String(),
		"prevRealm", prev.PkgPath(),
		"target", target,
		"poolPath", poolPath,
		"whitelisted", utils.FormatBool(whitelisted),
	)
}

errors.gno

package whitelist

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

const (
	errUnauthorized  = "[GNOSWAP-ORDER-WHITELIST-001] unauthorized caller"
	errInvalidPool   = "[GNOSWAP-ORDER-WHITELIST-002] invalid whitelisted pool"
	errInvalidTarget = "[GNOSWAP-ORDER-WHITELIST-003] invalid target"
)

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

getters.gno

package whitelist

import (
	rotree "gno.land/p/nt/bptree/rotree/v0"

	"gno.land/r/gnoswap/pool"
)

func IsWhitelistedPool(target, poolPath string) bool {
	return getState(target).HasPool(poolPath)
}

// GetWhitelistedPools returns only target's read-only poolPath -> true tree.
func GetWhitelistedPools(target string) *rotree.ReadOnlyTree {
	return getState(target).Pools()
}

// HasWhitelistedPool reports whether target allows an existing direct pool of the pair.
func HasWhitelistedPool(target, tokenIn, tokenOut string) bool {
	state := getState(target)

	if tokenIn == tokenOut {
		return false
	}

	for fee := range pool.GetFeeAmountTickSpacings() {
		poolPath := pool.GetPoolPath(tokenIn, tokenOut, uint32(fee))
		if state.HasPool(poolPath) && pool.ExistsPoolPath(poolPath) {
			return true
		}
	}

	return false
}

gnomod.toml

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

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

state.gno

package whitelist

import (
	pwhitelist "gno.land/p/gnoswap/order/whitelist/v0"
)

var (
	dcaState   pwhitelist.State
	limitState pwhitelist.State
)

func init() {
	resetState()
}

func resetState() {
	dcaState = pwhitelist.NewState()
	limitState = pwhitelist.NewState()
}

func getState(target string) *pwhitelist.State {
	assertIsValidTarget(target)

	if target == TargetDCA {
		return &dcaState
	}

	return &limitState
}

whitelist.gno

package whitelist

import (
	"gno.land/r/gnoswap/order/keeper"
)

// AddWhitelistedPool permits target's orders to trade on an existing pool.
// Admin, governance, or the role assigned to this selector may call it.
func AddWhitelistedPool(cur realm, target, poolPath string) {
	prev := assertIsManagerCall(0, cur, keeper.SelectorAddWhitelistedPool)
	state := getState(target)

	assertIsExistingPool(poolPath)
	assertIsNotWhitelistedPool(state, target, poolPath)

	state.AddPool(poolPath)
	emitPoolUpdated(0, prev, target, poolPath, true)
}

// RemoveWhitelistedPool stops only target's orders from using this pool.
// Existing orders remain cancellable. Authorization matches AddWhitelistedPool.
func RemoveWhitelistedPool(cur realm, target, poolPath string) {
	prev := assertIsManagerCall(0, cur, keeper.SelectorRemoveWhitelistedPool)
	state := getState(target)
	assertIsWhitelistedPool(state, target, poolPath)

	state.RemovePool(poolPath)
	emitPoolUpdated(0, prev, target, poolPath, false)
}