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 source realm

Package common provides shared realm utilities for GnoSwap protocol contracts.

Readme View source

Common Package

Package common provides shared realm utilities for GnoSwap protocol contracts.

Overview

The common package contains helpers that must keep a realm boundary, including GRC20 token operations and native coin validation.

Key Components

  1. GRC20 Registry Helpers: Convenient wrappers for GRC20 token operations
  2. Coin Utilities: Native coin (GNOT) handling and validation
  3. Assertion Utilities: Input validation and authorization checks

API Reference

GRC20 Registry Helpers

The write helpers are crossing adapters and should be called as common.Transfer(cross(cur), ...), common.SafeGRC20Transfer(cross(cur), ...), and so on. They intentionally keep cur realm as the first parameter so the helper runs in common's crossing frame, then forward that live cur to grc20.Teller as Transfer(0, cur, ...). Because GetTokenTeller returns a CallerTeller, the token actor is derived from cur.Previous(), which is the realm that crossed into common. Do not convert these helpers to _ int, rlm realm; that would remove the common crossing boundary and change which realm CallerTeller treats as the actor.

Token Operations:

  • GetToken: Retrieves GRC20 token instance
  • GetTokenTeller: Gets a CallerTeller for token operations
  • IsRegistered: Checks token registration status
  • MustRegistered: Validates multiple tokens are registered

Token Queries:

  • TotalSupply: Returns total supply of a token
  • BalanceOf: Returns token balance for an address
  • Allowance: Returns allowance from owner to spender

Token Transfers:

  • Transfer/TransferFrom/Approve: Returns error on failure
  • SafeGRC20Transfer/SafeGRC20TransferFrom/SafeGRC20Approve: Panics on failure

Coin Utilities

Coin Validation:

  • ExistsUserSendCoins: Checks if user sent any coins
  • AssertIsNotHandleNativeCoin: Ensures no native coins in transaction

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 14

func Allowance

Action
1func Allowance(tokenKey string, owner, spender address) int64
source

Allowance returns the token allowance from owner to spender.

Parameters:

  • tokenKey: GRC20 registry key
  • owner: address of the token owner
  • spender: address of the spender

Returns the allowance amount as int64.

func Approve

crossing Action
1func Approve(cur realm, tokenKey string, spender address, amount int64) error
source

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
  • tokenKey: GRC20 registry key
  • spender: address allowed to spend the tokens
  • amount: amount of tokens to approve

Returns an error if the approval fails, nil otherwise.

func AssertIsNotHandleNativeCoin

Action
1func AssertIsNotHandleNativeCoin()
source

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.

func BalanceOf

Action
1func BalanceOf(tokenKey string, addr address) int64
source

BalanceOf returns the token balance for the specified address.

Parameters:

  • tokenKey: GRC20 registry key
  • addr: address to query the balance for

Returns the token balance as int64.

func GetToken

Action
1func GetToken(tokenKey string) *grc20.Token
source

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

Parameters:

  • tokenKey: GRC20 registry key

Returns a pointer to the grc20.Token instance.

Panics if the token is not registered in grc20reg.

func GetTokenTeller

Action
1func GetTokenTeller(tokenKey string) grc20.Teller
source

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

Parameters:

  • tokenKey: GRC20 registry key

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

Panics if the token is not registered in grc20reg.

func IsRegistered

Action
1func IsRegistered(tokenKey string) error
source

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

Parameters:

  • tokenKey: GRC20 registry key

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

func MustRegistered

Action
1func MustRegistered(tokenKeys ...string)
source

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

Parameters:

  • tokenKeys: variable number of GRC20 registry keys to check

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

func SafeGRC20Approve

crossing Action
1func SafeGRC20Approve(cur realm, tokenKey string, spender address, amount int64)
source

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
  • tokenKey: GRC20 registry key
  • spender: address allowed to spend the tokens
  • amount: amount of tokens to approve

Panics if the approval fails.

func SafeGRC20Transfer

crossing Action
1func SafeGRC20Transfer(cur realm, tokenKey string, to address, amount int64)
source

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
  • tokenKey: GRC20 registry key
  • to: recipient address
  • amount: amount of tokens to transfer

Panics if the transfer fails.

func SafeGRC20TransferFrom

crossing Action
1func SafeGRC20TransferFrom(cur realm, tokenKey string, from, to address, amount int64)
source

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
  • tokenKey: GRC20 registry key
  • from: sender address
  • to: recipient address
  • amount: amount of tokens to transfer

Panics if the transfer fails.

func TotalSupply

Action
1func TotalSupply(tokenKey string) int64
source

TotalSupply returns the total supply of the specified token.

Parameters:

  • tokenKey: GRC20 registry key

Returns the total supply of the token as int64.

func Transfer

crossing Action
1func Transfer(cur realm, tokenKey string, to address, amount int64) error
source

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
  • tokenKey: GRC20 registry key
  • to: recipient address
  • amount: amount of tokens to transfer

Returns an error if the transfer fails, nil otherwise.

func TransferFrom

crossing Action
1func TransferFrom(cur realm, tokenKey string, from, to address, amount int64) error
source

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
  • tokenKey: GRC20 registry key
  • from: sender address
  • to: recipient address
  • amount: amount of tokens to transfer

Returns an error if the transfer fails, nil otherwise.

Imports 5

Source Files 6