github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/docs/concepts/packages.md (about)

     1  ---
     2  id: packages
     3  ---
     4  
     5  # Packages
     6  
     7  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.
     8  
     9  The full list of pre-deployed available packages can be found under the [demo package](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo). Below are some of the most commonly used packages.
    10  
    11  ## `avl`
    12  
    13  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.
    14   
    15  To address this issue, Gno implements the [AVL Tree](https://en.wikipedia.org/wiki/AVL\_tree) (Adelson-Velsky-Landis Tree) as a solution. The AVL Tree is a self-balancing binary search tree.
    16  
    17  The `avl` package comprises a set of functions that can manipulate the leaves and nodes of the AVL Tree.
    18  
    19  ## `grc20`
    20  
    21  Gno includes an implementation of the `erc20` fungible token standard referred to as `grc20`. The interfaces of `grc20` are as follows:
    22  
    23  [embedmd]:# (../assets/explanation/packages/pkg-1.gno go)
    24  ```go
    25  func TotalSupply() uint64
    26  func BalanceOf(account std.Address) uint64
    27  func Transfer(to std.Address, amount uint64)
    28  func Approve(spender std.Address, amount uint64)
    29  func TransferFrom(from, to std.Address, amount uint64)
    30  func Allowance(owner, spender std.Address) uint64
    31  ```
    32  
    33  The role of each function is as follows:
    34  
    35  * `TotalSupply`: Returns the total supply of the token.
    36  * `BalanceOf`: Returns the balance of tokens of an account.
    37  * `Transfer`: Transfers specific `amount` of tokens from the `caller` of the function to the `to` address.
    38  * `Approve`: Grants the `spender`(also referred to as `operator`) with the ability to send specific `amount` of the `caller`'s (also referred to as `owner`) tokens on behalf of the `caller`.
    39  * `TransferFrom`: Can be called by the `operator` to send specific `amount` of `owner`'s tokens from the `owner`'s address to the `to` address.
    40  * `Allowance`: Returns the number of tokens approved to the `spender` by the `owner`.
    41  
    42  Two types of contracts exist in`grc20`:
    43  
    44  1. `AdminToken`\
    45     \- Implements the token factory with `Helper` functions.\
    46     \- The underlying struct should not be exposed to users. However, it can be typecasted as UserToken using the `GRC20()` method.
    47  2. `UserToken`\
    48     \- Implements the `IGRC20` interface.\
    49     \- The underlying struct can be exposed to users. Created with the `GRC20()` method of `adminToken`.
    50  
    51  ## `grc721`
    52  
    53  Gno includes an implementation of the `erc721` non-fungible token standard referred to as `grc721`. The interfaces of `grc721` are as follows:
    54  
    55  [embedmd]:# (../assets/explanation/packages/pkg-2.gno go)
    56  ```go
    57  // functions that work similarly to those of grc20
    58  func BalanceOf(owner std.Address) (uint64, error)
    59  func Approve(approved std.Address, tid TokenID) error
    60  func TransferFrom(from, to std.Address, tid TokenID) error
    61  
    62  // functions unique to grc721
    63  func OwnerOf(tid TokenID) (std.Address, error)
    64  func SafeTransferFrom(from, to std.Address, tid TokenID) error
    65  func SetApprovalForAll(operator std.Address, approved bool) error
    66  func GetApproved(tid TokenID) (std.Address, error)
    67  func IsApprovedForAll(owner, operator std.Address) bool
    68  ```
    69  
    70  `grc721` contains a new set of functions:
    71  
    72  * `OwnerOf`: Returns the `owner`'s address of a token specified by its `TokenID`.
    73  * `SafeTransferFrom`: Equivalent to the `TransferFrom` function of `grc20`.
    74    * The `Safe` prefix indicates that the function runs a check to ensure that the `to` address is a valid address that can receive tokens.
    75    * As you can see from the [code](https://github.com/gnolang/gno/blob/master/examples/gno.land/p/demo/grc/grc721/basic\_nft.gno#L341), the concept of `Safe` has yet to be implemented.
    76  * `SetApprovalForAll`: Approves all tokens owned by the `owner` to an `operator`.
    77    * You may not set multiple `operator`s.
    78  * `GetApproved`: Returns the `address` of the `operator` for a token, specified with its `ID`.
    79  * `IsApprovedForAll`: Returns if all NFTs of the `owner` have been approved to the `operator`.
    80  
    81  ## `testutils`
    82  
    83  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.
    84  
    85  [embedmd]:# (../assets/explanation/packages/pkg-3.gno go)
    86  ```go
    87  func TestAddress(name string) std.Address {
    88  	if len(name) > std.RawAddressSize {
    89  		panic("address name cannot be greater than std.AddressSize bytes")
    90  	}
    91  	addr := std.RawAddress{}
    92  	// TODO: use strings.RepeatString or similar.
    93  	// NOTE: I miss python's "".Join().
    94  	blanks := "____________________"
    95  	copy(addr[:], []byte(blanks))
    96  	copy(addr[:], []byte(name))
    97  	return std.Address(std.EncodeBech32("g", addr))
    98  }
    99  ```
   100  
   101  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.
   102  
   103  [embedmd]:# (../assets/explanation/packages/pkg-4.gno go)
   104  ```go
   105  admin := users.AddressOrName("g1tntwtvzrkt2gex69f0pttan0fp05zmeg5yykv8")
   106  test2 := users.AddressOrName(testutils.TestAddress("test2"))
   107  recv := users.AddressOrName(testutils.TestAddress("recv"))
   108  normal := users.AddressOrName(testutils.TestAddress("normal"))
   109  owner := users.AddressOrName(testutils.TestAddress("owner"))
   110  spender := users.AddressOrName(testutils.TestAddress("spender"))
   111  recv2 := users.AddressOrName(testutils.TestAddress("recv2"))
   112  mibu := users.AddressOrName(testutils.TestAddress("mint_burn"))
   113  ```