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 shared GRC20 token operations and native coin validation.

Gnoweb

The root Render("") delegates to the active implementation and shows the realm address, implementation path, and network-wide token registry count. The count is not a Gnoswap whitelist. Token keys use realm-path.SYMBOL, and token amounts use each token's base units.

Rendering is read-only and does not enumerate registry entries. Unsupported paths return 404.

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 for supported operations (not authorization checks)

API Reference

GRC20 Registry Helpers

The write helpers are called without crossing, for example common.Transfer(0, cur, ...) and common.SafeGRC20Transfer(0, cur, ...). The token actor is bound to that current realm via RealmTeller before the operation is forwarded. Token lookup goes through the registered implementation (common/v1 resolves gno.land/r/nt/grc20reg/v0), which is swapped with UpgradeImpl like the other proxy realms.

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:

  • AssertIsNotHandleNativeCoin: Rejects native coins for GRC20-only functions and panics with [GNOSWAP-COMMON-002] handle native coin is not allowed when unsafe.OriginSend() is non-empty

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 17

func Allowance

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

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 Approve

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

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 AssertIsNotHandleNativeCoin

Action
1func AssertIsNotHandleNativeCoin()
source

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 BalanceOf

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

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 GetImplementationPackagePath

Action
1func GetImplementationPackagePath() string
source

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

Returns:

  • packagePath: fully qualified current implementation package path

func IsRegistered

Action
1func IsRegistered(tokenKey string) error
source

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 MustRegistered

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

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 RegisterInitializer

crossing Action
1func RegisterInitializer(cur realm, initializer func(_ int, rlm realm) ICommon)
source

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 Render

1func Render(path string) string
source

Render delegates web rendering to the active implementation.

func SafeGRC20Approve

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

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 SafeGRC20Transfer

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

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 SafeGRC20TransferFrom

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

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 TotalSupply

Action
1func TotalSupply(tokenKey string) int64
source

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 Transfer

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

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 TransferFrom

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

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 UpgradeImpl

crossing Action
1func UpgradeImpl(cur realm, packagePath string)
source

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 ValidateRegistered

Action
1func ValidateRegistered(tokenKeys ...string) error
source

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.

Types 1

type ICommon

interface
 1type ICommon interface {
 2	// ValidateNotHandleNativeCoin rejects a call carrying native coins.
 3	//
 4	// Returns:
 5	//   - error: non-nil when the transaction includes native coins; nil when no native coins are attached
 6	ValidateNotHandleNativeCoin() error
 7
 8	// IsRegistered checks whether tokenKey identifies a registered token.
 9	//
10	// Parameters:
11	//   - tokenKey: token registry key to validate and look up
12	//
13	// Returns:
14	//   - error: nil when tokenKey is valid and registered; non-nil for an invalid or unregistered key
15	IsRegistered(tokenKey string) error
16
17	// ValidateRegistered checks each token key in order.
18	//
19	// Parameters:
20	//   - tokenKeys: token registry keys that must all be valid and registered
21	//
22	// Returns:
23	//   - error: nil when every key is registered; otherwise the first registration failure in input order
24	ValidateRegistered(tokenKeys ...string) error
25
26	// TotalSupply returns the total supply of a registered token.
27	//
28	// Parameters:
29	//   - tokenKey: token registry key whose supply is queried
30	//
31	// Returns:
32	//   - totalSupply: token's total supply; panics if tokenKey is invalid or unregistered
33	TotalSupply(tokenKey string) int64
34
35	// BalanceOf returns a registered token's balance for an address.
36	//
37	// Parameters:
38	//   - tokenKey: token registry key whose balance is queried
39	//   - addr: account address whose token balance is queried
40	//
41	// Returns:
42	//   - balance: token units held by addr; panics if tokenKey is invalid or unregistered
43	BalanceOf(tokenKey string, addr address) int64
44
45	// Allowance returns the approved spendable amount for an owner/spender pair.
46	//
47	// Parameters:
48	//   - tokenKey: token registry key whose allowance is queried
49	//   - owner: account that granted spending approval
50	//   - spender: account allowed to spend owner's tokens
51	//
52	// Returns:
53	//   - allowance: token units owner has approved for spender; panics if tokenKey is invalid or unregistered
54	Allowance(tokenKey string, owner, spender address) int64
55
56	// Transfer asks the token teller bound to rlm to move tokens to to.
57	//
58	// Parameters:
59	//   - _: leading token-call discriminator; callers pass 0
60	//   - rlm: propagated realm context used to bind the token teller and invoke the transfer
61	//   - tokenKey: token registry key whose tokens are transferred
62	//   - to: recipient account address
63	//   - amount: number of token units to transfer
64	//
65	// Returns:
66	//   - error: nil when the teller completes the transfer; non-nil when token validation or transfer rules reject it
67	Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) error
68
69	// TransferFrom asks the token teller bound to rlm to transfer tokens from one account to another.
70	//
71	// Parameters:
72	//   - _: leading token-call discriminator; callers pass 0
73	//   - rlm: propagated realm context used to bind the token teller and invoke the transfer
74	//   - tokenKey: token registry key whose tokens are transferred
75	//   - from: account whose token balance is debited
76	//   - to: recipient account address
77	//   - amount: number of token units to transfer
78	//
79	// Returns:
80	//   - error: nil when the teller completes the transfer; non-nil when token validation, allowance, or balance rules reject it
81	TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) error
82
83	// Approve asks the token teller bound to rlm to set spender's allowance.
84	//
85	// Parameters:
86	//   - _: leading token-call discriminator; callers pass 0
87	//   - rlm: propagated realm context used to bind the token teller and invoke approval
88	//   - tokenKey: token registry key whose tokens are approved
89	//   - spender: account receiving permission to spend the caller's tokens
90	//   - amount: number of token units to authorize
91	//
92	// Returns:
93	//   - error: nil when the teller records the allowance; non-nil when token validation or approval rules reject it
94	Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) error
95	Render(path string) string
96}
source

Imports 5

Source Files 9

Directories 1