Search Apps Documentation Source Content File Folder Download Copy Actions Download

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.

Key components: - GRC20 Registry Helpers: convenient wrappers for GRC20 token operations - Coin Utilities: native coin handling and validation - Assertion Utilities: input validation and authorization checks

Functions

Allowance

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

Allowance returns the token allowance from owner to spender.

Parameters:

  • path: GRC20 token path
  • owner: address of the token owner
  • spender: address of the spender

Returns the allowance amount as int64.

Params

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.Allowance(,,)"

Result

Approve

func Approve(cur realm, path string, spender address, amount int64) error

Approve crosses into common before forwarding to grc20.Teller.Approve. CallerTeller uses cur.Previous() as the approver, so this helper must remain a crossing adapter called with cross(cur).

Parameters:

  • cur: current realm
  • path: GRC20 token path
  • spender: address allowed to spend the tokens
  • amount: amount of tokens to approve

Returns an error if the approval fails, nil otherwise.

Params

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 "Approve" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "test-13" -remote "https://rpc.test13.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test13.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "Approve" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test-13" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test13.testnets.gno.land" call.tx
  

AssertIsNotHandleNativeCoin

func AssertIsNotHandleNativeCoin()

AssertIsNotHandleNativeCoin validates that no native coins were sent with the transaction. Panics with errNotHandleNativeCoin if any native coins were sent.

Use this when a function should only work with GRC20 tokens and not accept native coins.

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.AssertIsNotHandleNativeCoin()"

Result

BalanceOf

func BalanceOf(path string, addr address) int64

BalanceOf returns the token balance for the specified address.

Parameters:

  • path: GRC20 token path
  • addr: address to query the balance for

Returns the token balance as int64.

Params

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.BalanceOf(,)"

Result

GetToken

func GetToken(path string) *grc20.Token

GetToken returns a grc20.Token instance for the specified path, panicking if not registered.

Parameters:

  • path: GRC20 token path

Returns a pointer to the grc20.Token instance.

Panics if the token is not registered in grc20reg.

Param

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.GetToken()"

Result

GetTokenTeller

func GetTokenTeller(path string) grc20.Teller

GetTokenTeller returns a CallerTeller for the specified path, panicking if not registered.

Parameters:

  • path: GRC20 token path

Returns a grc20.Teller whose write methods derive the actor from the caller realm.

Panics if the token is not registered in grc20reg.

Param

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.GetTokenTeller()"

Result

IsRegistered

func IsRegistered(path string) error

IsRegistered checks if a token is registered in grc20reg, returning nil if registered or error if not.

Parameters:

  • path: GRC20 token path

Returns nil if the token is registered, or an error describing the issue.

Param

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.IsRegistered()"

Result

MustRegistered

func MustRegistered(paths ...string)

MustRegistered checks if all provided tokens are registered, panicking if any is not registered.

Parameters:

  • paths: variable number of GRC20 token paths to check

Panics if any of the provided tokens is not registered in grc20reg.

Param

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.MustRegistered()"

Result

SafeGRC20Approve

func SafeGRC20Approve(cur realm, path string, spender address, amount int64)

SafeGRC20Approve crosses into common, approves tokens, and panics if it fails. CallerTeller uses cur.Previous() as the approver, so this helper must remain a crossing adapter called with cross(cur).

Parameters:

  • cur: current realm
  • path: GRC20 token path
  • spender: address allowed to spend the tokens
  • amount: amount of tokens to approve

Panics if the approval fails.

Params

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 "SafeGRC20Approve" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "test-13" -remote "https://rpc.test13.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test13.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "SafeGRC20Approve" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test-13" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test13.testnets.gno.land" call.tx
  

SafeGRC20Transfer

func SafeGRC20Transfer(cur realm, path string, to address, amount int64)

SafeGRC20Transfer crosses into common, transfers tokens, and panics if it fails. CallerTeller uses cur.Previous() as the token actor, so this helper must remain a crossing adapter called with cross(cur).

Parameters:

  • cur: current realm
  • path: GRC20 token path
  • to: recipient address
  • amount: amount of tokens to transfer

Panics if the transfer fails.

Params

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 "SafeGRC20Transfer" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "test-13" -remote "https://rpc.test13.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test13.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "SafeGRC20Transfer" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test-13" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test13.testnets.gno.land" call.tx
  

SafeGRC20TransferFrom

func SafeGRC20TransferFrom(cur realm, path string, from, to address, amount int64)

SafeGRC20TransferFrom crosses into common, transfers tokens, and panics if it fails. CallerTeller uses cur.Previous() as the spender, so this helper must remain a crossing adapter called with cross(cur).

Parameters:

  • cur: current realm
  • path: GRC20 token path
  • from: sender address
  • to: recipient address
  • amount: amount of tokens to transfer

Panics if the transfer fails.

Params

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 "SafeGRC20TransferFrom" -args $'' -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "test-13" -remote "https://rpc.test13.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test13.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "SafeGRC20TransferFrom" -args $'' -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test-13" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test13.testnets.gno.land" call.tx
  

TotalSupply

func TotalSupply(path string) int64

TotalSupply returns the total supply of the specified token.

Parameters:

  • path: GRC20 token path

Returns the total supply of the token as int64.

Param

Command

gnokey query vm/qeval -remote "https://rpc.test13.testnets.gno.land" -data "gno.land/r/gnoswap/common.TotalSupply()"

Result

Transfer

func Transfer(cur realm, path string, to address, amount int64) error

Transfer crosses into common before forwarding to grc20.Teller.Transfer. CallerTeller uses cur.Previous() as the token actor, so this helper must remain a crossing adapter called with cross(cur).

Parameters:

  • cur: current realm
  • path: GRC20 token path
  • to: recipient address
  • amount: amount of tokens to transfer

Returns an error if the transfer fails, nil otherwise.

Params

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 "Transfer" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "test-13" -remote "https://rpc.test13.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test13.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "Transfer" -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test-13" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test13.testnets.gno.land" call.tx
  

TransferFrom

func TransferFrom(cur realm, path string, from, to address, amount int64) error

TransferFrom crosses into common before forwarding to grc20.Teller.TransferFrom. CallerTeller uses cur.Previous() as the spender, so this helper must remain a crossing adapter called with cross(cur).

Parameters:

  • cur: current realm
  • path: GRC20 token path
  • from: sender address
  • to: recipient address
  • amount: amount of tokens to transfer

Returns an error if the transfer fails, nil otherwise.

Params

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 "TransferFrom" -args $'' -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "test-13" -remote "https://rpc.test13.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test13.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/gnoswap/common" -func "TransferFrom" -args $'' -args $'' -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test-13" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test13.testnets.gno.land" call.tx