Playground
assert.gno
package common
// AssertIsNotHandleNativeCoin validates that no native coins were sent with the transaction.
//
// Use this when a function should only work with GRC20 tokens and not accept native coins.
func AssertIsNotHandleNativeCoin() {
if err := getImplementation().ValidateNotHandleNativeCoin(); err != nil {
panic(err)
}
}
doc.gno
// Package common provides shared realm utilities for GnoSwap protocol contracts. // // The package contains helpers that must keep a realm boundary, including GRC20 // token operations and native coin validation. AMM math lives in // gno.land/p/gnoswap/gnsmath/v1. // // Key components: // - GRC20 Registry Helpers: convenient wrappers for GRC20 token operations // - Coin Utilities: native coin handling and validation // - Assertion Utilities: input validation for supported operations, including native-coin rejection package common
gnomod.toml
module = "gno.land/r/gnoswap/common" gno = "0.9" [addpkg] creator = "g1q5tjjv3kkslzwf2na9y9wk7e5a8ugph8xfsk04" height = 77248 max_deposit = "100000000ugnot"
grc20reg_helper.gno
package common
// IsRegistered checks whether a token key is registered.
//
// Parameters:
// - tokenKey: registry key identifying the GRC20 token to inspect.
//
// Returns:
// - error: nil when tokenKey is valid and registered; non-nil when the key is invalid or unregistered.
func IsRegistered(tokenKey string) error {
return getImplementation().IsRegistered(tokenKey)
}
// MustRegistered checks whether all provided tokens are registered and panics if any is not registered.
//
// Parameters:
// - tokenKeys: token registry keys to check; an empty list performs no checks.
//
// Panics if any of the provided tokens is not registered.
func MustRegistered(tokenKeys ...string) {
if err := ValidateRegistered(tokenKeys...); err != nil {
panic(err.Error())
}
}
// ValidateRegistered checks whether all provided tokens are registered, returning an
// error instead of panicking when a token is not registered.
//
// Parameters:
// - tokenKeys: token registry keys to check; an empty list is valid.
//
// Returns:
// - error: nil when every token is registered; otherwise the first
// registration failure.
func ValidateRegistered(tokenKeys ...string) error {
return getImplementation().ValidateRegistered(tokenKeys...)
}
// TotalSupply returns the total supply of the specified token.
//
// Parameters:
// - tokenKey: registry key identifying the GRC20 token.
//
// Returns:
// - totalSupply: current token supply for tokenKey, in the token's base units; panics if tokenKey is invalid or unregistered.
func TotalSupply(tokenKey string) int64 {
return getImplementation().TotalSupply(tokenKey)
}
// BalanceOf returns the token balance for the specified address.
//
// Parameters:
// - tokenKey: registry key identifying the GRC20 token.
// - addr: address whose token balance is queried.
//
// Returns:
// - balance: token balance held by addr, in the token's base units; panics if tokenKey is invalid or unregistered.
func BalanceOf(tokenKey string, addr address) int64 {
return getImplementation().BalanceOf(tokenKey, addr)
}
// Allowance returns the token allowance from owner to spender.
//
// Parameters:
// - tokenKey: registry key identifying the GRC20 token.
// - owner: address that owns the tokens.
// - spender: address authorized to spend owner's tokens.
//
// Returns:
// - allowance: amount owner has approved spender to use, in the token's base units; panics if tokenKey is invalid or unregistered.
func Allowance(tokenKey string, owner, spender address) int64 {
return getImplementation().Allowance(tokenKey, owner, spender)
}
// Transfer forwards a token transfer to the implementation bound to the current realm.
//
// Parameters:
// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
// - tokenKey: registry key identifying the GRC20 token to transfer.
// - to: recipient address.
// - amount: number of token base units to transfer.
//
// Returns:
// - error: nil when the implementation completes the transfer; otherwise a token
// validation or transfer-rule error.
func Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) error {
return getImplementation().Transfer(0, rlm, tokenKey, to, amount)
}
// TransferFrom forwards an allowance-backed token transfer to the implementation bound to the current realm.
//
// Parameters:
// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
// - tokenKey: registry key identifying the GRC20 token to transfer.
// - from: address whose allowance and balance are debited.
// - to: recipient address.
// - amount: number of token base units to transfer.
//
// Returns:
// - error: nil when the implementation completes the transfer; otherwise a token
// validation, allowance, or balance-rule error.
func TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) error {
return getImplementation().TransferFrom(0, rlm, tokenKey, from, to, amount)
}
// Approve forwards an allowance update to the implementation bound to the current realm.
//
// Parameters:
// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
// - tokenKey: registry key identifying the GRC20 token.
// - spender: address authorized to spend the caller's tokens.
// - amount: number of token base units to authorize.
//
// Returns:
// - error: nil when the implementation records the allowance; otherwise a token
// validation or approval-rule error.
func Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) error {
return getImplementation().Approve(0, rlm, tokenKey, spender, amount)
}
// SafeGRC20Transfer transfers tokens as the current realm and panics if it fails.
//
// Parameters:
// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
// - tokenKey: registry key identifying the GRC20 token to transfer.
// - to: recipient address.
// - amount: number of token base units to transfer.
//
// Panics if the underlying transfer returns an error.
func SafeGRC20Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) {
if err := Transfer(0, rlm, tokenKey, to, amount); err != nil {
panic(err)
}
}
// SafeGRC20TransferFrom transfers tokens as the current realm and panics if it fails.
//
// Parameters:
// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
// - tokenKey: registry key identifying the GRC20 token to transfer.
// - from: address whose allowance and balance are debited.
// - to: recipient address.
// - amount: number of token base units to transfer.
//
// Panics if the underlying transfer returns an error.
func SafeGRC20TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) {
if err := TransferFrom(0, rlm, tokenKey, from, to, amount); err != nil {
panic(err)
}
}
// SafeGRC20Approve approves tokens as the current realm and panics if it fails.
//
// Parameters:
// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
// - tokenKey: registry key identifying the GRC20 token.
// - spender: address authorized to spend the caller's tokens.
// - amount: number of token base units to authorize.
//
// Panics if the underlying approval returns an error.
func SafeGRC20Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) {
if err := Approve(0, rlm, tokenKey, spender, amount); err != nil {
panic(err)
}
}
render.gno
package common
// Render delegates web rendering to the active implementation.
func Render(path string) string {
return getImplementation().Render(path)
}
state.gno
package common
import (
"errors"
_ "gno.land/r/gnoswap/rbac/v1" // initialize readable contract role(s)
"gno.land/p/gnoswap/store/v1"
"gno.land/p/gnoswap/version_manager/v1"
)
var (
currentAddress address
domainPath string
kvStore store.KVStore
versionManager version_manager.VersionManager
implementation ICommon
)
func init(cur realm) {
currentAddress = cur.Address()
domainPath = cur.PkgPath()
kvStore = store.NewKVStore(currentAddress)
versionManager = version_manager.NewVersionManager(
domainPath,
kvStore,
initializeDomainStore,
)
implementation = nil
}
// common keeps no domain state; implementations only resolve tokens.
func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
return nil
}
func getImplementation() ICommon {
if implementation == nil {
panic("implementation is not initialized")
}
return implementation
}
func updateImplementation() error {
result := versionManager.GetCurrentImplementation()
if result == nil {
return errors.New("implementation is not initialized")
}
impl, ok := result.(ICommon)
if !ok {
return errors.New("impl is not an ICommon")
}
implementation = impl
return nil
}
types.gno
package common
type ICommon interface {
// ValidateNotHandleNativeCoin rejects a call carrying native coins.
//
// Returns:
// - error: non-nil when the transaction includes native coins; nil when no native coins are attached
ValidateNotHandleNativeCoin() error
// IsRegistered checks whether tokenKey identifies a registered token.
//
// Parameters:
// - tokenKey: token registry key to validate and look up
//
// Returns:
// - error: nil when tokenKey is valid and registered; non-nil for an invalid or unregistered key
IsRegistered(tokenKey string) error
// ValidateRegistered checks each token key in order.
//
// Parameters:
// - tokenKeys: token registry keys that must all be valid and registered
//
// Returns:
// - error: nil when every key is registered; otherwise the first registration failure in input order
ValidateRegistered(tokenKeys ...string) error
// TotalSupply returns the total supply of a registered token.
//
// Parameters:
// - tokenKey: token registry key whose supply is queried
//
// Returns:
// - totalSupply: token's total supply; panics if tokenKey is invalid or unregistered
TotalSupply(tokenKey string) int64
// BalanceOf returns a registered token's balance for an address.
//
// Parameters:
// - tokenKey: token registry key whose balance is queried
// - addr: account address whose token balance is queried
//
// Returns:
// - balance: token units held by addr; panics if tokenKey is invalid or unregistered
BalanceOf(tokenKey string, addr address) int64
// Allowance returns the approved spendable amount for an owner/spender pair.
//
// Parameters:
// - tokenKey: token registry key whose allowance is queried
// - owner: account that granted spending approval
// - spender: account allowed to spend owner's tokens
//
// Returns:
// - allowance: token units owner has approved for spender; panics if tokenKey is invalid or unregistered
Allowance(tokenKey string, owner, spender address) int64
// Transfer asks the token teller bound to rlm to move tokens to to.
//
// Parameters:
// - _: leading token-call discriminator; callers pass 0
// - rlm: propagated realm context used to bind the token teller and invoke the transfer
// - tokenKey: token registry key whose tokens are transferred
// - to: recipient account address
// - amount: number of token units to transfer
//
// Returns:
// - error: nil when the teller completes the transfer; non-nil when token validation or transfer rules reject it
Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) error
// TransferFrom asks the token teller bound to rlm to transfer tokens from one account to another.
//
// Parameters:
// - _: leading token-call discriminator; callers pass 0
// - rlm: propagated realm context used to bind the token teller and invoke the transfer
// - tokenKey: token registry key whose tokens are transferred
// - from: account whose token balance is debited
// - to: recipient account address
// - amount: number of token units to transfer
//
// Returns:
// - error: nil when the teller completes the transfer; non-nil when token validation, allowance, or balance rules reject it
TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) error
// Approve asks the token teller bound to rlm to set spender's allowance.
//
// Parameters:
// - _: leading token-call discriminator; callers pass 0
// - rlm: propagated realm context used to bind the token teller and invoke approval
// - tokenKey: token registry key whose tokens are approved
// - spender: account receiving permission to spend the caller's tokens
// - amount: number of token units to authorize
//
// Returns:
// - error: nil when the teller records the allowance; non-nil when token validation or approval rules reject it
Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) error
Render(path string) string
}
upgrade.gno
package common
import (
"gno.land/r/gnoswap/access/v1"
)
// RegisterInitializer registers the versioned common implementation initializer
// and activates the implementation returned by it.
//
// Parameters:
// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
// - initializer: callback invoked with discriminator 0 and the propagated
// implementation realm, returning the version's ICommon implementation
func RegisterInitializer(cur realm, initializer func(_ int, rlm realm) ICommon) {
initializerFunc := func(_ int, rlm realm, _ any) any {
access.AssertIsRlmCurrent(0, rlm)
return initializer(0, rlm)
}
err := versionManager.RegisterInitializer(0, cur, initializerFunc)
if err != nil {
panic(err)
}
err = updateImplementation()
if err != nil {
panic(err)
}
}
// UpgradeImpl changes the active common implementation package and refreshes
// the cached implementation.
//
// Parameters:
// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
// - packagePath: fully qualified package path of the replacement implementation
func UpgradeImpl(cur realm, packagePath string) {
prev := cur.Previous()
access.AssertIsAdminOrGovernance(prev.Address())
err := versionManager.ChangeImplementation(0, cur, packagePath)
if err != nil {
panic(err)
}
err = updateImplementation()
if err != nil {
panic(err)
}
}
// GetImplementationPackagePath returns the package path of the currently
// selected common implementation.
//
// Returns:
// - packagePath: fully qualified current implementation package path
func GetImplementationPackagePath() string {
return versionManager.GetCurrentPackagePath()
}