Packages
Packages aim to encompass functionalities that are more closely aligned with the characteristics and capabilities of realms, as opposed to standard libraries. As opposed to realms, they are stateless.
The full list of pre-deployed available packages can be found under the demo package. Below are some of the most commonly used packages.
avl
In Go, the classic key/value data type is represented by the map
construct. However, while Gno also supports the use of map
, it is not a viable option as it lacks determinism due to its non-sequential nature.
To address this issue, Gno implements the AVL Tree (Adelson-Velsky-Landis Tree) as a solution. The AVL Tree is a self-balancing binary search tree.
The avl
package comprises a set of functions that can manipulate the leaves and nodes of the AVL Tree.
grc20
Gno includes an implementation of the erc20
fungible token standard referred to as grc20
. The interfaces of grc20
are as follows:
func TotalSupply() uint64
func BalanceOf(account std.Address) uint64
func Transfer(to std.Address, amount uint64)
func Approve(spender std.Address, amount uint64)
func TransferFrom(from, to std.Address, amount uint64)
func Allowance(owner, spender std.Address) uint64
The role of each function is as follows:
TotalSupply
: Returns the total supply of the token.BalanceOf
: Returns the balance of tokens of an account.Transfer
: Transfers specificamount
of tokens from thecaller
of the function to theto
address.Approve
: Grants thespender
(also referred to asoperator
) with the ability to send specificamount
of thecaller
's (also referred to asowner
) tokens on behalf of thecaller
.TransferFrom
: Can be called by theoperator
to send specificamount
ofowner
's tokens from theowner
's address to theto
address.Allowance
: Returns the number of tokens approved to thespender
by theowner
.
Two types of contracts exist ingrc20
:
Banker
- Implements the token factory with
Helper
functions. - The underlying struct should not be exposed to users. However, it can return a typecasted
Token
object using theToken()
method.
- Implements the token factory with
Token
- Implements the
GRC20
interface. - The underlying struct can be exposed to users. Created with the
Token()
method ofBanker
.
- Implements the
grc721
Gno includes an implementation of the erc721
non-fungible token standard referred to as grc721
. The interfaces of grc721
are as follows:
// functions that work similarly to those of grc20
func BalanceOf(owner std.Address) (uint64, error)
func Approve(approved std.Address, tid TokenID) error
func TransferFrom(from, to std.Address, tid TokenID) error
// functions unique to grc721
func OwnerOf(tid TokenID) (std.Address, error)
func SafeTransferFrom(from, to std.Address, tid TokenID) error
func SetApprovalForAll(operator std.Address, approved bool) error
func GetApproved(tid TokenID) (std.Address, error)
func IsApprovedForAll(owner, operator std.Address) bool
grc721
contains a new set of functions:
OwnerOf
: Returns theowner
's address of a token specified by itsTokenID
.SafeTransferFrom
: Equivalent to theTransferFrom
function ofgrc20
.- The
Safe
prefix indicates that the function runs a check to ensure that theto
address is a valid address that can receive tokens. - As you can see from the code, the concept of
Safe
has yet to be implemented.
- The
SetApprovalForAll
: Approves all tokens owned by theowner
to anoperator
.- You may not set multiple
operator
s.
- You may not set multiple
GetApproved
: Returns theaddress
of theoperator
for a token, specified with itsID
.IsApprovedForAll
: Returns if all NFTs of theowner
have been approved to theoperator
.
testutils
The testutils
package contains a set of functions that comes in handy when testing realms. The sample function below is the commonly used TestAddress
function that generates a random address.
func TestAddress(name string) std.Address {
if len(name) > std.RawAddressSize {
panic("address name cannot be greater than std.AddressSize bytes")
}
addr := std.RawAddress{}
// TODO: use strings.RepeatString or similar.
// NOTE: I miss python's "".Join().
blanks := "____________________"
copy(addr[:], []byte(blanks))
copy(addr[:], []byte(name))
return std.Address(std.EncodeBech32("g", addr))
}
The code takes the name
as the input and creates a random address. Below is a list of examples where it's used in the test case of the foo20
realm.
admin := users.AddressOrName("g1tntwtvzrkt2gex69f0pttan0fp05zmeg5yykv8")
test2 := users.AddressOrName(testutils.TestAddress("test2"))
recv := users.AddressOrName(testutils.TestAddress("recv"))
normal := users.AddressOrName(testutils.TestAddress("normal"))
owner := users.AddressOrName(testutils.TestAddress("owner"))
spender := users.AddressOrName(testutils.TestAddress("spender"))
recv2 := users.AddressOrName(testutils.TestAddress("recv2"))
mibu := users.AddressOrName(testutils.TestAddress("mint_burn"))