Standard Libraries
Gno comes with a set of standard libraries which are included to ease development and provide extended functionality to the language. These include:
- standard libraries as we know them in classic Go, i.e.
strings,testing, etc. - a special
chainpackage with subpackages containing types, interfaces, and APIs created to handle blockchain-related functionality, such as fetching the last caller, fetching coins sent along with a transaction, getting the block timestamp and height, and more.
Standard libraries differ from on-chain packages in terms of their import path structure. Unlike on-chain packages, standard libraries do not incorporate a domain-like format at the beginning of their import path. For example:
import "strings"refers to a standard libraryimport "gno.land/p/nt/avl/v0"refers to an on-chain pure package.
To see concrete implementation details & API references of the chain package &
subpackages, see below.
Accessing documentation
Apart from the official documentation you are currently reading, you can also access documentation for the standard libraries in several other different ways. You can obtain a list of all the available standard libraries with the following commands:
$ cd gnovm/stdlibs # go to correct directory
$ find -type d
./testing
./math
./crypto
./crypto/chacha20
./crypto/chacha20/chacha
./crypto/chacha20/rand
./crypto/sha256
./crypto/cipher
...
All the packages have automatically generated documentation through the use of the
gno doc command, which has similar functionality and features to go doc:
$ gno doc encoding/binary
package binary // import "encoding/binary"
Package binary implements simple translation between numbers and byte sequences
and encoding and decoding of varints.
[...]
var BigEndian bigEndian
var LittleEndian littleEndian
type AppendByteOrder interface{ ... }
type ByteOrder interface{ ... }
$ gno doc -u -src encoding/binary littleEndian.AppendUint16
package binary // import "encoding/binary"
func (littleEndian) AppendUint16(b []byte, v uint16) []byte {
return append(b,
byte(v),
byte(v>>8),
)
}
gno doc will work automatically when used within the Gno repository or any
repository which has a go.mod dependency on github.com/gnolang/gno.
Another alternative is setting your environment variable GNOROOT to point to
where you cloned the Gno repository.
export GNOROOT=$HOME/gno
Concepts
Coin
A Coin is a native Gno type that has a denomination and an amount. Coins can be issued by the native Gno Banker.
A coin is defined by the following:
type Coin struct {
Denom string `json:"denom"`
Amount int64 `json:"amount"`
}
Denom is the denomination of the coin, i.e. ugnot, and Amount is a
non-negative amount of the coin.
Multiple coins can be bundled together into a Coins slice:
type Coins []Coin
This slice behaves like a mathematical set - it cannot contain duplicate Coin instances.
The Coins slice can be included in a transaction made by a user addresses or a realm.
Coins in this set are then available for access by specific types of Bankers,
which can manipulate them depending on access rights.
Read more about coins in the Effective Gno section.
Banker
The Banker's main purpose is to handle balance changes of native coins within Gno chains. This includes issuance, transfers, and burning of coins.
The Banker module can be cast into 4 subtypes of bankers that expose different functionalities and safety features within your packages and realms.
Banker Types
BankerTypeReadonly- read-only access to coin balancesBankerTypeOriginSend- full access to coins sent with the transaction that called the bankerBankerTypeRealmSend- full access to coins that the realm itself owns, including the ones sent with the transactionBankerTypeRealmIssue- able to issue new coins
Events
Events in Gno are a fundamental aspect of interacting with and monitoring on-chain applications. They serve as a bridge between the on-chain environment and off-chain services, making it simpler for developers, analytics tools, and monitoring services to track and respond to activities happening in gno.land.
Gno events are pieces of data that log specific activities or changes occurring within the state of an on-chain app. These activities are user-defined; they might be token transfers, changes in ownership, updates in user profiles, and more. Each event is recorded in the ABCI results of each block, ensuring that action that happened is verifiable and accessible to off-chain services.
To emit an event, you can use the Emit() function from the chain package
provided in the Gno standard library. The Emit() function takes in a string
representing the type of event, and an even number of arguments after representing
key:value pairs.
Read more about events & Emit() in
Effective Gno.
An event contained in an ABCI response of a block will include the following data:
{
"@type": "/tm.gnoEvent", // TM2 type
"type": "OwnershipChange", // Type/name of event defined in Gno
"pkg_path": "gno.land/r/demo/example", // Path of the emitter
"func": "ChangeOwner", // Gno function that emitted the event
"attrs": [ // Slice of key:value pairs emitted
{
"key": "oldOwner",
"value": "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
},
{
"key": "newOwner",
"value": "g1zzqd6phlfx0a809vhmykg5c6m44ap9756s7cjj"
}
]
}
You can fetch the ABCI response of a specific block by using the /block_results
RPC endpoint.
Builtins
Gno has a few custom builtin types & keywords that are used for handling Gno-specific cases:
realm- represents a Realm objectaddress- represents a Gno addresscross(rlm)- validatesrlmis the current realm and passes it as therealmtype argument of a crossing call, e.g.fn(cross(cur), ...)
address
Native address type in Gno, conforming to the Bech32 format.
type address string
func (a address) IsValid() bool {...}
func (a address) String() string {...}
IsValid
Check if address is of a valid length, and conforms to the bech32 format.
Usage
if !address.IsValid() {...}
String
Get string representation of address.
Usage
stringAddr := addr.String()
realm
realm is the structure representing a realm in Gno. See our realm documentation for more details.
type realm Realm
type Realm struct {
addr address
pkgPath string
}
func (r Realm) Address() address {...}
func (r Realm) PkgPath() string {...}
func (r Realm) String() string {...}
func (r Realm) IsUser() bool {...}
func (r Realm) IsUserRun() bool {...}
func (r Realm) IsUserCall() bool {...}
func (r Realm) IsEphemeral() bool {...}
func (r Realm) CoinDenom(coinName string) string {...}
Address
Returns the address field of the realm it was called upon.
Usage
realmAddr := r.Address() // eg. g1n2j0gdyv45aem9p0qsfk5d2gqjupv5z536na3d
PkgPath
Returns the string package path of the realm it was called upon.
Usage
realmPath := r.PkgPath() // eg. gno.land/r/gnoland/blog
String
Returns the string representation of the realm it was called upon. Also provides information whether the realm is a code realm or user realm.
Usage
s := r.String() // UserRealm{ g1... } OR CodeRealm{ g1..., gno.land/r/... }
IsUser
Checks if the receiver realm is a user realm. This check passes for both MsgCall and MsgRun transactions.
Usage
if r.IsUser() {...}
IsCode
Checks if the receiver realm is a code realm.
Usage
if r.IsCode() {...}
IsUserRun
Checks if the receiver realm is a user realm, given by a MsgRun transaction.
Usage
if r.IsUserRun() {...}
IsUserCall
Checks if the receiver realm is a user realm, given by a MsgCall transaction.
Usage
if r.IsUserCall() {...}
IsEphemeral
Checks if the receiver realm has an ephemeral package path (i.e. under /e/).
Usage
if r.IsEphemeral() {...}
CoinDenom
Composes a qualified denomination string from the realm's pkgPath and the
provided coin name, e.g. /gno.land/r/demo/blog:blgcoin. This method should be
used to get fully qualified denominations of coins when interacting with the
Banker module.
Parameters
coinNamestring - The coin name used to build the qualified denomination. Must start with a lowercase letter, followed by 2–15 lowercase letters or digits.
Usage
// in "gno.land/r/gnoland/blog"
denom := r.CoinDenom("blgcoin") // /gno.land/r/gnoland/blog:blgcoin
Package chain
Emit
func Emit(typ string, attrs ...string)
Emits a Gno event. Takes in a string type (event identifier), and an even number of string arguments acting as key-value pairs to be included in the emitted event.
Usage
chain.Emit("MyEvent", "myKey1", "myValue1", "myKey2", "myValue2")
PackageAddress
func PackageAddress(pkgPath string) address
Derives the Realm address from its pkgpath parameter.
Usage
realmAddr := chain.PackageAddress("gno.land/r/demo/tamagotchi") // g1a3tu874agjlkrpzt9x90xv3uzncapcn959yte4