Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

common package

Overview

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

Functions

Allowance

func Allowance(tokenKey string, owner, spender address) int64

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.Allowance(,,)"

Result

Approve

func Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) error

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.Approve(,,,,)"

Result

AssertIsNotHandleNativeCoin

func AssertIsNotHandleNativeCoin()

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.

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.AssertIsNotHandleNativeCoin()"

Result

BalanceOf

func BalanceOf(tokenKey string, addr address) int64

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.BalanceOf(,)"

Result

GetImplementationPackagePath

func GetImplementationPackagePath() string

GetImplementationPackagePath returns the package path of the currently selected common implementation.

Returns:

  • packagePath: fully qualified current implementation package path

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.GetImplementationPackagePath()"

Result

IsRegistered

func IsRegistered(tokenKey string) error

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.

Param

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.IsRegistered()"

Result

MustRegistered

func MustRegistered(tokenKeys ...string)

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.

Param

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.MustRegistered()"

Result

RegisterInitializer

func RegisterInitializer(cur realm, initializer func(_ int, rlm realm) ICommon)

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

Param

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "RegisterInitializer" -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "gnoland-1" -remote "http://127.0.0.1:26657" ADDRESSgnokey query -remote "http://127.0.0.1:26657" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "RegisterInitializer" -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "gnoland-1" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "http://127.0.0.1:26657" call.tx
  

Render

func Render(path string) string

Render delegates web rendering to the active implementation.

Param

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.Render()"

Result

SafeGRC20Approve

func SafeGRC20Approve(_ int, rlm realm, tokenKey string, spender address, amount int64)

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.SafeGRC20Approve(,,,,)"

Result

SafeGRC20Transfer

func SafeGRC20Transfer(_ int, rlm realm, tokenKey string, to address, amount int64)

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.SafeGRC20Transfer(,,,,)"

Result

SafeGRC20TransferFrom

func SafeGRC20TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64)

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.SafeGRC20TransferFrom(,,,,,)"

Result

TotalSupply

func TotalSupply(tokenKey string) int64

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.

Param

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.TotalSupply()"

Result

Transfer

func Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) error

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.Transfer(,,,,)"

Result

TransferFrom

func TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) error

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.

Params

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.TransferFrom(,,,,,)"

Result

UpgradeImpl

func UpgradeImpl(cur realm, packagePath string)

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

Param

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "UpgradeImpl" -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "gnoland-1" -remote "http://127.0.0.1:26657" ADDRESSgnokey query -remote "http://127.0.0.1:26657" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "UpgradeImpl" -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "gnoland-1" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "http://127.0.0.1:26657" call.tx
  

ValidateRegistered

func ValidateRegistered(tokenKeys ...string) 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.

Param

Command

gnokey query vm/qeval -remote "http://127.0.0.1:26657" -data "gno.land/r/gnoswap/common.ValidateRegistered()"

Result