Packages encompass functionalities that are more closely aligned with the characteristics and capabilities of realms, as opposed to standard libraries.
The full list of available packages can be found in the demo package. Below are some of the most commonly used packages.
In Golang, the classic key/value data type is represented by the map construct. However, while Gnolang 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, Gnolang 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.
Click on this link for a sample usage of the avl package.
Gnolang includes an implementation of the erc20 fungible token standard referred to as grc20. The interfaces of grc20 are as follows:
TotalSupply() uint64
BalanceOf(account std.Address) uint64
Transfer(to std.Address, amount uint64)
Approve(spender std.Address, amount uint64)
TransferFrom(from, to std.Address, amount uint64)
Allowance(owner, spender std.Address) uint64The 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 specificamountof tokens from thecallerof the function to thetoaddress.Approve: Grants thespender(also referred to asoperator) with the ability to send specificamountof thecaller's (also referred to asowner) tokens on behalf of thecaller.TransferFrom: Can be called by theoperatorto send specificamountofowner's tokens from theowner's address to thetoaddress.Allowance: Returns the number of tokens approved to thespenderby theowner.
Two types of contracts exist ingrc20:
AdminToken
- Implements the token factory withHelperfunctions.
- The underlying struct should not be exposed to users. However, it can be typecasted as UserToken using theGRC20()method.UserToken
- Implements theIGRC20interface.
- The underlying struct can be exposed to users. Created with theGRC20()method ofadminToken.
Gnolang 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
BalanceOf(owner std.Address) (uint64, error)
Approve(approved std.Address, tid TokenID) error
TransferFrom(from, to std.Address, tid TokenID) error
// functions unique to grc721
OwnerOf(tid TokenID) (std.Address, error)
SafeTransferFrom(from, to std.Address, tid TokenID) error
SetApprovalForAll(operator std.Address, approved bool) error
GetApproved(tid TokenID) (std.Address, error)
IsApprovedForAll(owner, operator std.Address) boolgrc721 contains a new set of functions:
OwnerOf: Returns theowner's address of a token specified by itsTokenID.SafeTransferFrom: Equivalent to theTransferFromfunction ofgrc20.- The
Safeprefix indicates that the function runs a check to ensure that thetoaddress is a valid address that can receive tokens. - As you can see from the code, the concept of
Safehas yet to be implemented.
- The
SetApprovalForAll: Approves all tokens owned by theownerto anoperator.- You may not set multiple
operators.
- You may not set multiple
GetApproved: Returns theaddressof theoperatorfor a token, specified with itsID.IsApprovedForAll: Returns if all NFTs of theownerhave been approved to theoperator.
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"))