From 92c0dffd7c7e6cc4fdf5cb1586d32ad08477084d Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 20 Dec 2023 00:28:13 +0100 Subject: [PATCH 01/33] wip save --- docs/concepts/realms.md | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index fd6969db3f1..1ff7bb35a4d 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -5,7 +5,18 @@ id: realms # Realms A realm refers to a specific instance of a smart contract that can be written -in [Gnolang](./gno-language.md). The potentials of realms are endless - you can create virtually any +in [Gnolang](./gno-language.md). The most important characteristics of realms are the following: + +* Realms are stateful +* Realms can own assets ([coins](todo link concepts/coin)) +* Each realm is deployed under a unique package path, i.e. `gno.land/r/blog`, and also has a Gno + address derived from it, i.e. `g1n2j0gdyv45aem9p0qsfk5d2gqjupv5z536na3d` +* They are deployed with a package path beginning with `gno.land/r/` +* Realms can import packages from `gno.land/p/demo/` to gain more functionality +* Realms can implement `Render(path string) string` to simplify dApp frontend development by allowing users to request + markdown renderings from validators and full nodes without a transaction + +The potentials of realms are endless - you can create virtually any application in your mind with built-in composability, transparency, and censorship resistance. Here are some ideas of what you can build with realms: @@ -13,27 +24,11 @@ transparency, and censorship resistance. Here are some ideas of what you can bui * Lending platforms with better rates. * Transparent insurance systems. * Fair and accessible voting systems. -* Logistics and supply chain networks. +* Logistics and supply chain networks. // todo add non-blockchain stuff? ie r/GH, twitter clone, svg generator, gnochess? -## Packages vs Realms - -#### [**Pure Packages**](https://github.com/gnolang/gno/tree/master/examples/gno.land/p) - -* A unit that contains functionalities and utilities that can be used in realms. -* Packages are stateless. -* The default import path is `gno.land/p/~~~`. -* Can be imported to other realms or packages. -* Cannot import realms. - -#### [**Realms**](https://github.com/gnolang/gno/tree/master/examples/gno.land/r) - -* Smart contracts in Gnolang. -* Realms are stateful. -* Realms can own assets (tokens). -* The default import path is `gno.land/r/~~~`. -* Realms can implement `Render(path string) string` to simplify dapp frontend development by allowing users to request - markdown renderings from validators and full nodes without a transaction. +Example realms can be found on the Gno monorepo in the [examples folder](https://github.com/gnolang/gno/tree/master/examples/gno.land/r), or on-chain, under the `gno.land/r/` path. +// todo move to a new page? explain how to utilize arg path for muxing? A notable feature of realms is the `Render()` function. ```go @@ -44,6 +39,6 @@ func Render(path string) string { } ``` -Upon calling the realm above, `# Hello Gno!` is printed with a string-typed `path` declared in an argument. It should be +Upon calling the realm function above, `# Hello Gno!` will be returned with a string-typed `path` declared in an argument. It should be noted that while the `path` argument included in the sample code is not utilized, it serves the purpose of distinguishing the path during the rendering process. From d6d7700d9d1e3388d5f0c611680110f8f8f8d4e5 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 18 Jan 2024 10:58:31 +0100 Subject: [PATCH 02/33] add coins link --- docs/concepts/realms.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 1ff7bb35a4d..55a166dc981 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -8,11 +8,11 @@ A realm refers to a specific instance of a smart contract that can be written in [Gnolang](./gno-language.md). The most important characteristics of realms are the following: * Realms are stateful -* Realms can own assets ([coins](todo link concepts/coin)) -* Each realm is deployed under a unique package path, i.e. `gno.land/r/blog`, and also has a Gno +* Realms can own assets ([coins](./standard-library/coin.md)) +* Each realm is deployed under a unique package path, i.e. `gno.land/r/blog`, and also has a Gno address derived from it, i.e. `g1n2j0gdyv45aem9p0qsfk5d2gqjupv5z536na3d` * They are deployed with a package path beginning with `gno.land/r/` -* Realms can import packages from `gno.land/p/demo/` to gain more functionality +* Realms can import packages from `gno.land/p/` to gain more functionality * Realms can implement `Render(path string) string` to simplify dApp frontend development by allowing users to request markdown renderings from validators and full nodes without a transaction @@ -26,16 +26,15 @@ transparency, and censorship resistance. Here are some ideas of what you can bui * Fair and accessible voting systems. * Logistics and supply chain networks. // todo add non-blockchain stuff? ie r/GH, twitter clone, svg generator, gnochess? -Example realms can be found on the Gno monorepo in the [examples folder](https://github.com/gnolang/gno/tree/master/examples/gno.land/r), or on-chain, under the `gno.land/r/` path. +Example realms can be found on the Gno.land monorepo in the [examples folder](https://github.com/gnolang/gno/tree/master/examples/gno.land/r), or on-chain, under the `gno.land/r/` path. -// todo move to a new page? explain how to utilize arg path for muxing? A notable feature of realms is the `Render()` function. ```go package demo func Render(path string) string { - return "# Hello Gno!" + return "# Hello Gno!" } ``` From a38a32dc18f0b84317b4ce1f04429600d2925988 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 24 Jan 2024 16:39:34 +0100 Subject: [PATCH 03/33] update realm page --- docs/concepts/realms.md | 57 +++++++++----------- docs/reference/standard-library/std/realm.md | 34 ++++++++++++ 2 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 docs/reference/standard-library/std/realm.md diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index fd6969db3f1..1da4f6ab810 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -4,46 +4,37 @@ id: realms # Realms -A realm refers to a specific instance of a smart contract that can be written -in [Gnolang](./gno-language.md). The potentials of realms are endless - you can create virtually any +A realm is a smart contract written in [Gno](./gno-language.md). The most important characteristics of realms are the following: +- Realms are stateful, +- Realms can own assets ([coins](./standard-library/coin.md)), +- Each realm is deployed under a unique package path, i.e. `gno.land/r/blog`, and also has a Gno +address derived from it, i.e. `g1n2j0gdyv45aem9p0qsfk5d2gqjupv5z536na3d`, +- Realms are deployed with a package path beginning with `gno.land/r/`, +- Realms can import packages from `gno.land/p/` to gain more functionality, + +The potentials of realms are endless - you can create virtually any application in your mind with built-in composability, transparency, and censorship resistance. Here are some ideas of what you can build with realms: +- Complex DeFi applications +- Censorship-resistant social networks +- Fair and accessible voting systems +- Logistics and supply chain networks -* Self-custodial financial exchanges (decentralized exchanges). -* Lending platforms with better rates. -* Transparent insurance systems. -* Fair and accessible voting systems. -* Logistics and supply chain networks. +You can find illustrative examples of realms either in the Gno.land monorepo, +located within the [examples folder](https://github.com/gnolang/gno/tree/master/examples), or on-chain, under the `gno.land/r/` directory. -## Packages vs Realms +## Realms in code -#### [**Pure Packages**](https://github.com/gnolang/gno/tree/master/examples/gno.land/p) - -* A unit that contains functionalities and utilities that can be used in realms. -* Packages are stateless. -* The default import path is `gno.land/p/~~~`. -* Can be imported to other realms or packages. -* Cannot import realms. - -#### [**Realms**](https://github.com/gnolang/gno/tree/master/examples/gno.land/r) +Realms are represented by a Realm struct in Gno: +```go +type Realm struct { + addr Address + pkgPath string +} +``` +For the full Realm API, see the [reference page](../reference/). -* Smart contracts in Gnolang. -* Realms are stateful. -* Realms can own assets (tokens). -* The default import path is `gno.land/r/~~~`. -* Realms can implement `Render(path string) string` to simplify dapp frontend development by allowing users to request - markdown renderings from validators and full nodes without a transaction. -A notable feature of realms is the `Render()` function. -```go -package demo -func Render(path string) string { - return "# Hello Gno!" -} -``` -Upon calling the realm above, `# Hello Gno!` is printed with a string-typed `path` declared in an argument. It should be -noted that while the `path` argument included in the sample code is not utilized, it serves the purpose of -distinguishing the path during the rendering process. diff --git a/docs/reference/standard-library/std/realm.md b/docs/reference/standard-library/std/realm.md new file mode 100644 index 00000000000..ac4abab8f44 --- /dev/null +++ b/docs/reference/standard-library/std/realm.md @@ -0,0 +1,34 @@ +--- +id: realm +--- + +# Realm +Structure repesenting a Realm in Gno. + +```go +type Realm struct { + addr crypto.Bech32Address + pkgPath string +} + +func (r Realm) Addr() Addres {...} +func (r Realm) PkgPath() string {...} +func (r Realm) IsUser() bool {} + +``` + +## Addr +Returns the **Address** field of the realm it was called upon. + +#### Usage +```go +realmAddr := r.Addr() // eg. g1n2j0gdyv45aem9p0qsfk5d2gqjupv5z536na3d +``` +--- +## PkgPath +Returns the **string** package path of the realm it was called upon. + +#### Usage +```go +realmPath := r.PkgPath() // eg. gno.land/r/gnoland/blog +``` From d2f0acf1a6b70e7ae630fa81c4779870e93df046 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 24 Jan 2024 17:29:54 +0100 Subject: [PATCH 04/33] update realm concept page --- docs/concepts/realms.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 1da4f6ab810..9dd606f5d15 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -25,14 +25,14 @@ located within the [examples folder](https://github.com/gnolang/gno/tree/master/ ## Realms in code -Realms are represented by a Realm struct in Gno: +Realms are represented by a Realm type in Gno: ```go type Realm struct { addr Address pkgPath string } ``` -For the full Realm API, see the [reference page](../reference/). +For the full Realm API, see the [reference page](../reference/standard-library/std/realm.md). From 70e4d47b8fcadbfd23661c6fd5834f6cb812389b Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 24 Jan 2024 17:40:39 +0100 Subject: [PATCH 05/33] fix typo --- docs/reference/standard-library/std/realm.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/reference/standard-library/std/realm.md b/docs/reference/standard-library/std/realm.md index ac4abab8f44..450ff141605 100644 --- a/docs/reference/standard-library/std/realm.md +++ b/docs/reference/standard-library/std/realm.md @@ -3,7 +3,7 @@ id: realm --- # Realm -Structure repesenting a Realm in Gno. +Structure representing a Realm in Gno. See concept page [here](../../../concepts/realms.md). ```go type Realm struct { @@ -14,7 +14,6 @@ type Realm struct { func (r Realm) Addr() Addres {...} func (r Realm) PkgPath() string {...} func (r Realm) IsUser() bool {} - ``` ## Addr From 2f057f71836ee50ca687992f655bd75995b1c709 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 13:50:44 +0900 Subject: [PATCH 06/33] refactor realm page --- docs/concepts/packages.md | 113 ++------------------------------------ docs/concepts/realms.md | 52 +++++++++--------- 2 files changed, 33 insertions(+), 132 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index 3978de59120..e09959e8e76 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -1,113 +1,12 @@ ---- -id: packages ---- +## Realms in code -# 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](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo). 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](https://en.wikipedia.org/wiki/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: - -[embedmd]:# (../assets/explanation/packages/pkg-1.gno go) +Realms are represented by a Realm type in Gno: ```go -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 specific `amount` of tokens from the `caller` of the function to the `to` address. -* `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`. -* `TransferFrom`: Can be called by the `operator` to send specific `amount` of `owner`'s tokens from the `owner`'s address to the `to` address. -* `Allowance`: Returns the number of tokens approved to the `spender` by the `owner`. - -Two types of contracts exist in`grc20`: - -1. `AdminToken`\ - \- Implements the token factory with `Helper` functions.\ - \- The underlying struct should not be exposed to users. However, it can be typecasted as UserToken using the `GRC20()` method. -2. `UserToken`\ - \- Implements the `IGRC20` interface.\ - \- The underlying struct can be exposed to users. Created with the `GRC20()` method of `adminToken`. - -## `grc721` - -Gno includes an implementation of the `erc721` non-fungible token standard referred to as `grc721`. The interfaces of `grc721` are as follows: - -[embedmd]:# (../assets/explanation/packages/pkg-2.gno go) -```go -// 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 the `owner`'s address of a token specified by its `TokenID`. -* `SafeTransferFrom`: Equivalent to the `TransferFrom` function of `grc20`. - * The `Safe` prefix indicates that the function runs a check to ensure that the `to` address is a valid address that can receive tokens. - * 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. -* `SetApprovalForAll`: Approves all tokens owned by the `owner` to an `operator`. - * You may not set multiple `operator`s. -* `GetApproved`: Returns the `address` of the `operator` for a token, specified with its `ID`. -* `IsApprovedForAll`: Returns if all NFTs of the `owner` have been approved to the `operator`. - -## `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. - -[embedmd]:# (../assets/explanation/packages/pkg-3.gno go) -```go -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)) +type Realm struct { + addr Address + pkgPath string } ``` +For the full Realm API, see the [reference page](../reference/standard-library/std/realm.md). -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. -[embedmd]:# (../assets/explanation/packages/pkg-4.gno go) -```go -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")) -``` diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 9dd606f5d15..66f3cb94569 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -3,36 +3,38 @@ id: realms --- # Realms +In Gno, Realms are Gno accounts which are addressable by a +[Gno address](../reference/standard-library/std/address.md). Realms have several +properties: +- They can own, receive & send [Coins](./standard-library/coin.md) through the +[Banker](./standard-library/banker.md) module +- They can be part of a transaction call stack, as a caller or a callee +- They can be with or without code - EOAs, or smart contracts + +Realms are represented by a `Realm` type in Gno: +```go +type Realm struct { + addr Address // Gno address in the bech32 format + pkgPath string // realm's path on-chain +} +``` + +## Smart Contract Realms +Often simply called "realms", Gno smart contracts contain Gno code and exist +on-chain at a specific address and package path. + +## Externally Owned Accounts (EOAs) +EOAs, or simply user realms, are Gno addresses generated from a BIP39 mnemonic +phrase in a key management application, such as [`gnokey`](../gno-tooling/cli/gnokey.md). + + + + -A realm is a smart contract written in [Gno](./gno-language.md). The most important characteristics of realms are the following: -- Realms are stateful, -- Realms can own assets ([coins](./standard-library/coin.md)), -- Each realm is deployed under a unique package path, i.e. `gno.land/r/blog`, and also has a Gno -address derived from it, i.e. `g1n2j0gdyv45aem9p0qsfk5d2gqjupv5z536na3d`, -- Realms are deployed with a package path beginning with `gno.land/r/`, -- Realms can import packages from `gno.land/p/` to gain more functionality, -The potentials of realms are endless - you can create virtually any -application in your mind with built-in composability, -transparency, and censorship resistance. Here are some ideas of what you can build with realms: -- Complex DeFi applications -- Censorship-resistant social networks -- Fair and accessible voting systems -- Logistics and supply chain networks -You can find illustrative examples of realms either in the Gno.land monorepo, -located within the [examples folder](https://github.com/gnolang/gno/tree/master/examples), or on-chain, under the `gno.land/r/` directory. -## Realms in code -Realms are represented by a Realm type in Gno: -```go -type Realm struct { - addr Address - pkgPath string -} -``` -For the full Realm API, see the [reference page](../reference/standard-library/std/realm.md). From ad4b55d2c064baf65aa1e17f64ffd79a6c24e2dc Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 14:49:29 +0900 Subject: [PATCH 07/33] save --- docs/concepts/realms.md | 48 ++++++++++++++------ docs/concepts/standard-library/coin.md | 22 +++++---- docs/reference/standard-library/std/realm.md | 4 +- misc/docusaurus/sidebars.js | 1 + 4 files changed, 50 insertions(+), 25 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 66f3cb94569..53a09105ab8 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -3,8 +3,9 @@ id: realms --- # Realms -In Gno, Realms are Gno accounts which are addressable by a -[Gno address](../reference/standard-library/std/address.md). Realms have several +In Gno.land, realms are entities that are addressable and identifiable by a +[Gno address](../reference/standard-library/std/address.md). These can be user +realms (EOAs), as well as smart contract realms. Realms have several properties: - They can own, receive & send [Coins](./standard-library/coin.md) through the [Banker](./standard-library/banker.md) module @@ -18,25 +19,46 @@ type Realm struct { pkgPath string // realm's path on-chain } ``` +The full Realm API can be found under the +[reference section](../reference/standard-library/std/realm.md). ## Smart Contract Realms -Often simply called "realms", Gno smart contracts contain Gno code and exist -on-chain at a specific address and package path. +Often simply called `realms`, Gno smart contracts contain Gno code and exist +on-chain at a specific package path. A package path is the defining identifier +of a realm, while its address is derived from it. + +### On-chain paths +Since Gno.land is built for full transparency and auditability, all on-chain Gno +code is open-sourced. You can view realm code by simply going to its path in +your web browser. For example, to take a look at the `gno.land/r/demo/users` realm, +used for user registration, by visiting +[`gno.land/r/demo/users`](https://gno.land/r/demo/users/users.gno). + +:::info +Depending on the network, the realm domain might change. Currently, +the `gno.land/` domain (and all of its subdomains, such as `r/`) is pointing to +the [Portal Loop](./portal-loop.md) testnet endpoint, which is subject +to change. To view realms on the `test3` network (depr.), prepend `test3` to +the domain: [`test3.gno.land/r/demo/users`](https://test3.gno.land/r/demo/users). +::: + +[//]: # (Learn more about package paths & allowed namespaces [here].) + +To learn how to actually write a realm, +view the [Simple realm guide](../how-to-guides/simple-contract.md) ## Externally Owned Accounts (EOAs) -EOAs, or simply user realms, are Gno addresses generated from a BIP39 mnemonic -phrase in a key management application, such as [`gnokey`](../gno-tooling/cli/gnokey.md). - - - - - - - +EOAs, or simply `user realms`, are Gno addresses generated from a BIP39 mnemonic +phrase in a key management application, such as +[`gnokey`](../gno-tooling/cli/gnokey.md), and [Adena](https://adena.app). +## Working with realms +- when part of the call stack, using chain functions to get realm info +User realms are recognizable by the fact that their package path is empty. +This can be checked by calling the `IsUser()` method on the realm object. diff --git a/docs/concepts/standard-library/coin.md b/docs/concepts/standard-library/coin.md index db1618ef3f7..01e6695c602 100644 --- a/docs/concepts/standard-library/coin.md +++ b/docs/concepts/standard-library/coin.md @@ -4,7 +4,8 @@ id: coin # Coin -A Coin is a native Gno type that has a denomination and an amount. Coins can be issued by the [Banker](banker.md). +A Coin is a native Gno type that has a denomination and an amount. Coins can be +issued by the [Banker](banker.md). A coin is defined by the following: @@ -15,8 +16,8 @@ type Coin struct { } ``` -`Denom` is the denomination of the coin, i.e. `ugnot`, and `Amount` is a non-negative -amount of the coin. +`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: @@ -24,13 +25,14 @@ 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. +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. +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. -[//]: # (TODO ADD LINK TO Effective GNO) -Read more about coins in the [Effective Gno](https://docs.gno.land/concepts/effective-gno/#native-tokens) section. +Read more about coins in the [Effective Gno](../effective-gno.md#coins) section. -The Coin(s) API can be found in under the `std` package [reference](../../reference/standard-library/std/coin.md). +The Coin(s) API can be found in under the `std` package +[reference](../../reference/standard-library/std/coin.md). diff --git a/docs/reference/standard-library/std/realm.md b/docs/reference/standard-library/std/realm.md index 450ff141605..a801abb6929 100644 --- a/docs/reference/standard-library/std/realm.md +++ b/docs/reference/standard-library/std/realm.md @@ -7,11 +7,11 @@ Structure representing a Realm in Gno. See concept page [here](../../../concepts ```go type Realm struct { - addr crypto.Bech32Address + addr Address pkgPath string } -func (r Realm) Addr() Addres {...} +func (r Realm) Addr() Address {...} func (r Realm) PkgPath() string {...} func (r Realm) IsUser() bool {} ``` diff --git a/misc/docusaurus/sidebars.js b/misc/docusaurus/sidebars.js index cab3af1ce25..c041ef0e5d6 100644 --- a/misc/docusaurus/sidebars.js +++ b/misc/docusaurus/sidebars.js @@ -102,6 +102,7 @@ const sidebars = { label: 'std', items: [ 'reference/standard-library/std/address', + 'reference/standard-library/std/realm', 'reference/standard-library/std/banker', 'reference/standard-library/std/coin', 'reference/standard-library/std/coins', From 76d953b0f6e25885e467418c27a8eefc4f90cbd5 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 14:50:09 +0900 Subject: [PATCH 08/33] fixup signature --- docs/reference/standard-library/std/realm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/standard-library/std/realm.md b/docs/reference/standard-library/std/realm.md index a801abb6929..96154d04b68 100644 --- a/docs/reference/standard-library/std/realm.md +++ b/docs/reference/standard-library/std/realm.md @@ -13,7 +13,7 @@ type Realm struct { func (r Realm) Addr() Address {...} func (r Realm) PkgPath() string {...} -func (r Realm) IsUser() bool {} +func (r Realm) IsUser() bool {...} ``` ## Addr From 641ef59477bcc63ad065cd3c46999dcfe07a8954 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 14:50:27 +0900 Subject: [PATCH 09/33] fix capitalization --- docs/reference/standard-library/std/realm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/standard-library/std/realm.md b/docs/reference/standard-library/std/realm.md index 96154d04b68..1f6b6ca93df 100644 --- a/docs/reference/standard-library/std/realm.md +++ b/docs/reference/standard-library/std/realm.md @@ -3,7 +3,7 @@ id: realm --- # Realm -Structure representing a Realm in Gno. See concept page [here](../../../concepts/realms.md). +Structure representing a realm in Gno. See concept page [here](../../../concepts/realms.md). ```go type Realm struct { From 7eb6cfeddf842619c1eb853704e0d7530efcf217 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 15:24:02 +0900 Subject: [PATCH 10/33] start working w realms section, update chain reference --- docs/concepts/realms.md | 20 ++++- docs/reference/standard-library/std/chain.md | 90 ++++++++++---------- 2 files changed, 64 insertions(+), 46 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 53a09105ab8..56a7bc44a8f 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -45,7 +45,7 @@ the domain: [`test3.gno.land/r/demo/users`](https://test3.gno.land/r/demo/users) [//]: # (Learn more about package paths & allowed namespaces [here].) To learn how to actually write a realm, -view the [Simple realm guide](../how-to-guides/simple-contract.md) +see [How to write a simple Gno Smart Contract](../how-to-guides/simple-contract.md). ## Externally Owned Accounts (EOAs) EOAs, or simply `user realms`, are Gno addresses generated from a BIP39 mnemonic @@ -53,8 +53,24 @@ phrase in a key management application, such as [`gnokey`](../gno-tooling/cli/gnokey.md), and [Adena](https://adena.app). ## Working with realms +In Gno, each transaction consists of a call stack, which is made up of `frames`. +A single frame is a unique realm in the call stack. Every frame and its properties +can be accessed via different functions defined in the `std` package in Gno: +- `std.GetOrigCaller()` - returns the address of the original signer of the +transaction +- `std.GetOrigPkgAddr()` - returns the address of the first caller (entry point) +in a sequence of realm calls +- `std.PrevRealm()` - returns the previous realm instance, which can be a user realm +or a smart contract realm +- `std.CurrentRealm()` - returns the instance of the realm that has called it +- `std.CurrentRealmPath()` - shorthand for `std.CurrentRealm().PkgPath()` +- `std.GetCallerAt()` - returns the n-th caller's address, going back in +the transaction call stack + +Let's look at return values of these functions in two distinct situations: +- EOA calling a realm +- EOA calling a realm, in turn calling another realm -- when part of the call stack, using chain functions to get realm info diff --git a/docs/reference/standard-library/std/chain.md b/docs/reference/standard-library/std/chain.md index bda09b2fe80..5aa38a94e9d 100644 --- a/docs/reference/standard-library/std/chain.md +++ b/docs/reference/standard-library/std/chain.md @@ -8,7 +8,8 @@ id: chain ```go func IsOriginCall() bool ``` -Checks if the caller of the function is an EOA. Returns **true** if caller is an EOA, **false** otherwise. +Checks if the caller of the function is an EOA. Returns **true** if caller is an +EOA, **false** otherwise. #### Usage ```go @@ -28,47 +29,11 @@ std.AssertOriginCall() ``` --- -## CurrentRealmPath -```go -func CurrentRealmPath() string -``` -Returns the path of the realm it is called in. - -#### Usage -```go -realmPath := std.CurrentRealmPath() // gno.land/r/demo/users -``` ---- - -## GetChainID -```go -func GetChainID() string -``` -Returns the chain ID. - -#### Usage -```go -chainID := std.GetChainID() // dev | test3 | main ... -``` ---- - -## GetHeight -```go -func GetHeight() int64 -``` -Returns the current block number (height). - -#### Usage -```go -height := std.GetHeight() -``` ---- - ## GetOrigSend ```go func GetOrigSend() Coins ``` -Returns the `Coins` that were sent along with the calling transaction. +Returns the `Coins` that were sent along with the transaction. #### Usage ```go @@ -80,7 +45,7 @@ coinsSent := std.GetOrigSend() ```go func GetOrigCaller() Address ``` -Returns the original signer of the transaction. +Returns the address of the original signer of the transaction. #### Usage ```go @@ -92,7 +57,7 @@ caller := std.GetOrigSend() ```go func GetOrigPkgAddr() string ``` -Returns the address of the first (entry point) realm/package in a sequence of realm/package calls. +Returns the address of the first caller (entry point) in a sequence of realm calls #### Usage ```go @@ -104,20 +69,33 @@ origPkgAddr := std.GetOrigPkgAddr() ```go func CurrentRealm() Realm ``` -Returns current Realm object. +Returns the instance of the [realm](../../../concepts/realms.md) that has called +it. -[//]: # (todo link to realm type explanation) #### Usage ```go currentRealm := std.CurrentRealm() ``` --- +## CurrentRealmPath +```go +func CurrentRealmPath() string +``` +Returns the path of the realm it is called in. Shorthand for +`CurrentRealm().PkgPath()`. + +#### Usage +```go +realmPath := std.CurrentRealmPath() // gno.land/r/demo/users +``` +--- + ## PrevRealm ```go func PrevRealm() Realm ``` -Returns the previous caller realm (can be realm or EOA). If caller is am EOA, `pkgpath` will be empty. +Returns the previous caller realm. If caller is am EOA, `pkgpath` will be empty. #### Usage ```go @@ -143,9 +121,33 @@ std.GetCallerAt(0) // error, n must be > 0 ```go func DerivePkgAddr(pkgPath string) Address ``` -Derives the Realm address from its `pkgpath` parameter. +Derives a Gno address from a `pkgpath` parameter. #### Usage ```go realmAddr := std.DerivePkgAddr("gno.land/r/demo/tamagotchi") // g1a3tu874agjlkrpzt9x90xv3uzncapcn959yte4 ``` +--- + +## GetChainID +```go +func GetChainID() string +``` +Returns the chain ID. + +#### Usage +```go +chainID := std.GetChainID() // dev | test3 | main ... +``` +--- + +## GetHeight +```go +func GetHeight() int64 +``` +Returns the current block number (height). + +#### Usage +```go +height := std.GetHeight() +``` From 2105a75b638f9010ed5ca4638447864b6f192bf2 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 16:11:26 +0900 Subject: [PATCH 11/33] save --- docs/concepts/realms.md | 15 +++++++++++++-- docs/reference/standard-library/std/chain.md | 8 ++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 56a7bc44a8f..5b008909059 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -68,13 +68,24 @@ or a smart contract realm the transaction call stack Let's look at return values of these functions in two distinct situations: -- EOA calling a realm -- EOA calling a realm, in turn calling another realm +1. EOA calling a realm +2. EOA calling a realm, in turn calling another realm +### EOA calling a realm + +Let's look at values for each of the calls: +- `std.GetOrigCaller()` > 'g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5' +- `std.GetOrigPkgAddr()` > 'g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5' +- `std.PrevRealm()` > +- `std.CurrentRealm()` > +- `std.CurrentRealmPath()` > +- `std.GetCallerAt()` > + + User realms are recognizable by the fact that their package path is empty. This can be checked by calling the `IsUser()` method on the realm object. diff --git a/docs/reference/standard-library/std/chain.md b/docs/reference/standard-library/std/chain.md index 5aa38a94e9d..e5535bd69d8 100644 --- a/docs/reference/standard-library/std/chain.md +++ b/docs/reference/standard-library/std/chain.md @@ -57,7 +57,8 @@ caller := std.GetOrigSend() ```go func GetOrigPkgAddr() string ``` -Returns the address of the first caller (entry point) in a sequence of realm calls +Returns the address of the first non-EOA realm (entry point) in a sequence of +realm/package calls. #### Usage ```go @@ -69,8 +70,7 @@ origPkgAddr := std.GetOrigPkgAddr() ```go func CurrentRealm() Realm ``` -Returns the instance of the [realm](../../../concepts/realms.md) that has called -it. +Returns the instance of the [realm](../../../concepts/realms.md) that has called it. #### Usage ```go @@ -82,7 +82,7 @@ currentRealm := std.CurrentRealm() ```go func CurrentRealmPath() string ``` -Returns the path of the realm it is called in. Shorthand for +Returns the path of the realm it is called in. Shorthand for `CurrentRealm().PkgPath()`. #### Usage From c82f622866608335042d627d495a91ccecd1443b Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 16:54:29 +0900 Subject: [PATCH 12/33] add diagrams to realm page --- docs/concepts/realms.md | 103 +++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 13 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 5b008909059..7ea372c4a8b 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -58,8 +58,6 @@ A single frame is a unique realm in the call stack. Every frame and its properti can be accessed via different functions defined in the `std` package in Gno: - `std.GetOrigCaller()` - returns the address of the original signer of the transaction -- `std.GetOrigPkgAddr()` - returns the address of the first caller (entry point) -in a sequence of realm calls - `std.PrevRealm()` - returns the previous realm instance, which can be a user realm or a smart contract realm - `std.CurrentRealm()` - returns the instance of the realm that has called it @@ -69,23 +67,102 @@ the transaction call stack Let's look at return values of these functions in two distinct situations: 1. EOA calling a realm -2. EOA calling a realm, in turn calling another realm +2. EOA calling a sequence of realms -### EOA calling a realm +### 1. EOA calling a realm +Take these two actors in the call stack: +``` +EOA: + addr: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 + pkgPath: "" // empty as this is a user realm + +Realm A: + addr: g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s + pkgPath: gno.land/p/demo/users + + ┌─────────────────────┐ ┌─────────────────────────┐ + │ EOA │ │ Realm A │ + │ │ │ │ + │ addr: │ │ addr: │ + │ g1jg...sqf5 ├──────► g17m...8v2s │ + │ │ │ │ + │ pkgPath: │ │ pkgPath: │ + │ "" │ │ gno.land/p/demo/users │ + └─────────────────────┘ └─────────────────────────┘ +``` + +Let's look at return values for each of the methods: +```go +std.GetOrigCaller() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.PrevRealm() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.CurrentRealm() => Realm { + addr: `g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s` + pkgPath: `gno.land/r/demo/users` +} +std.CurrentRealmPath() => `gno.land/r/demo/users` +std.GetCallerAt(1) => `g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s` +std.GetCallerAt(2) => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.GetCallerAt(3) => error +``` + +### 2. EOA calling a sequence of realms +Take these three actors in the call stack: -Let's look at values for each of the calls: -- `std.GetOrigCaller()` > 'g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5' -- `std.GetOrigPkgAddr()` > 'g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5' -- `std.PrevRealm()` > -- `std.CurrentRealm()` > -- `std.CurrentRealmPath()` > -- `std.GetCallerAt()` > +``` +EOA: + addr: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 + pkgPath: "" // empty as this is a user realm + +Realm A: + addr: g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6 + pkgPath: gno.land/p/demo/a + +Realm B: + addr: g1rsk9cwv034cw3s6csjeun2jqypj0ztpecqcm3v + pkgPath: gno.land/p/demo/b + +┌─────────────────────┐ ┌──────────────────────┐ ┌─────────────────────┐ +│ EOA │ │ Realm A │ │ Realm B │ +│ │ │ │ │ │ +│ addr: │ │ addr: │ │ addr: │ +│ g1jg...sqf5 ├───► g17m...8v2s ├───► g17m...8v2s │ +│ │ │ │ │ │ +│ pkgPath: │ │ pkgPath: │ │ pkgPath: │ +│ "" │ │ gno.land/p/demo/a │ │ gno.land/p/demo/b │ +└─────────────────────┘ └──────────────────────┘ └─────────────────────┘ +``` -User realms are recognizable by the fact that their package path is empty. -This can be checked by calling the `IsUser()` method on the realm object. +Depending on which realm the methods are called in, the values will change. For +`Realm A`: +```go +std.GetOrigCaller() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.PrevRealm() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.CurrentRealm() => Realm { + addr: `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` + pkgPath: `gno.land/r/demo/a` +} +std.CurrentRealmPath() => `gno.land/r/demo/a` +std.GetCallerAt(1) => `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` +std.GetCallerAt(2) => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.GetCallerAt(3) => error +``` +For `Realm B`: +```go +std.GetOrigCaller() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.PrevRealm() => `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` +std.CurrentRealm() => Realm { + addr: `g1rsk9cwv034cw3s6csjeun2jqypj0ztpecqcm3v` + pkgPath: `gno.land/r/demo/b` +} +std.CurrentRealmPath() => `gno.land/r/demo/b` +std.GetCallerAt(1) => `g1rsk9cwv034cw3s6csjeun2jqypj0ztpecqcm3v` +std.GetCallerAt(2) => `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` +std.GetCallerAt(3) => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.GetCallerAt(4) => error +``` \ No newline at end of file From a778d9bc0b3581cd2a31a05bb27476851a9ab504 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 17:03:38 +0900 Subject: [PATCH 13/33] add more sections --- docs/concepts/realms.md | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 7ea372c4a8b..d26a73be18d 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -52,6 +52,12 @@ EOAs, or simply `user realms`, are Gno addresses generated from a BIP39 mnemonic phrase in a key management application, such as [`gnokey`](../gno-tooling/cli/gnokey.md), and [Adena](https://adena.app). +Currently, EOAs are the only realms that can initiate a transaction. User realms +can do this by calling any of the possible messages in Gno.land, such as +[Call](../gno-tooling/cli/gnokey.md#call), +[AddPackage](../gno-tooling/cli/gnokey.md#addpkg), +[Send](../gno-tooling/cli/gnokey.md#send), or Run. + ## Working with realms In Gno, each transaction consists of a call stack, which is made up of `frames`. A single frame is a unique realm in the call stack. Every frame and its properties @@ -70,7 +76,6 @@ Let's look at return values of these functions in two distinct situations: 2. EOA calling a sequence of realms ### 1. EOA calling a realm - Take these two actors in the call stack: ``` EOA: @@ -107,12 +112,7 @@ std.GetCallerAt(3) => error ``` ### 2. EOA calling a sequence of realms - Take these three actors in the call stack: - - - - ``` EOA: addr: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 @@ -130,7 +130,7 @@ Realm B: │ EOA │ │ Realm A │ │ Realm B │ │ │ │ │ │ │ │ addr: │ │ addr: │ │ addr: │ -│ g1jg...sqf5 ├───► g17m...8v2s ├───► g17m...8v2s │ +│ g1jg...sqf5 ├───► g17m...8v2s ├───► g1rs...cm3v │ │ │ │ │ │ │ │ pkgPath: │ │ pkgPath: │ │ pkgPath: │ │ "" │ │ gno.land/p/demo/a │ │ gno.land/p/demo/b │ @@ -165,4 +165,15 @@ std.GetCallerAt(1) => `g1rsk9cwv034cw3s6csjeun2jqypj0ztpecqcm3v` std.GetCallerAt(2) => `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` std.GetCallerAt(3) => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` std.GetCallerAt(4) => error -``` \ No newline at end of file +``` + +## Browsing & interacting with existing realms + +You can see the some of the currently deployed realms on the Portal Loop testnet +here. // link homepage +// mention [source] & [help] buttons + +## `Render()` + +// todo add render + From 51adc98171b5c2a26e780ec897d96584f2de1a5f Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 17:13:41 +0900 Subject: [PATCH 14/33] render section --- docs/concepts/realms.md | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index d26a73be18d..7ad79b8c07d 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -167,13 +167,32 @@ std.GetCallerAt(3) => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` std.GetCallerAt(4) => error ``` -## Browsing & interacting with existing realms +## State persistence -You can see the some of the currently deployed realms on the Portal Loop testnet -here. // link homepage -// mention [source] & [help] buttons +// todo add paragraph on state persistence -## `Render()` +## `Render` -// todo add render +A notable feature of non-EOA realms is the ability to have a render function. A +render function allows the developer of the realm to choose how to render the +state of the realm by returning a custom-made valid Markdown string. It also +allows the developer to define different renders depending on the `path` argument: +```go +package demo + +func Render(path string) string { + if path == "" { + return "# Hello Gno!" + } + + return "# Hello" + path +} +``` + +:::info +You can see the ender function in action by visiting the +[home page of Gno.land]((https://gno.land/) - it is actually the render of +`r/gnoland/home` realm. The same is true for the +[Gno.land Blog](https://gno.land/r/gnoland/blog), and most other pages on the domain. +::: From f8817e17b50cb1646ea2077c4f99bdfeb53046ca Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 17:44:21 +0900 Subject: [PATCH 15/33] add state persistence parag --- docs/concepts/realms.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 7ad79b8c07d..d545e9a9d6e 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -27,6 +27,12 @@ Often simply called `realms`, Gno smart contracts contain Gno code and exist on-chain at a specific package path. A package path is the defining identifier of a realm, while its address is derived from it. +As opposed to [packages](./packages.md), realms are stateful, meaning they keep +their state between transactions calls. In practice, global variables in the +realm code are automatically persisted after a transaction has been executed, +resulting in the fact that developers do not need to bother with the intricacies +of state management and persistence. + ### On-chain paths Since Gno.land is built for full transparency and auditability, all on-chain Gno code is open-sourced. You can view realm code by simply going to its path in @@ -35,25 +41,24 @@ used for user registration, by visiting [`gno.land/r/demo/users`](https://gno.land/r/demo/users/users.gno). :::info -Depending on the network, the realm domain might change. Currently, +Depending on the network, the realm domain might change. Currently, the `gno.land/` domain (and all of its subdomains, such as `r/`) is pointing to -the [Portal Loop](./portal-loop.md) testnet endpoint, which is subject -to change. To view realms on the `test3` network (depr.), prepend `test3` to +the [Portal Loop](./portal-loop.md) testnet endpoint, which is subject +to change. To view realms on the `test3` network (depr.), prepend `test3` to the domain: [`test3.gno.land/r/demo/users`](https://test3.gno.land/r/demo/users). ::: [//]: # (Learn more about package paths & allowed namespaces [here].) -To learn how to actually write a realm, -see [How to write a simple Gno Smart Contract](../how-to-guides/simple-contract.md). +To learn how to write a realm, see [How to write a simple Gno Smart Contract](../how-to-guides/simple-contract.md). ## Externally Owned Accounts (EOAs) EOAs, or simply `user realms`, are Gno addresses generated from a BIP39 mnemonic phrase in a key management application, such as [`gnokey`](../gno-tooling/cli/gnokey.md), and [Adena](https://adena.app). -Currently, EOAs are the only realms that can initiate a transaction. User realms -can do this by calling any of the possible messages in Gno.land, such as +Currently, EOAs are the only realms that can initiate a transaction. They can do +this by calling any of the possible messages in Gno.land, such as [Call](../gno-tooling/cli/gnokey.md#call), [AddPackage](../gno-tooling/cli/gnokey.md#addpkg), [Send](../gno-tooling/cli/gnokey.md#send), or Run. @@ -167,12 +172,7 @@ std.GetCallerAt(3) => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` std.GetCallerAt(4) => error ``` -## State persistence - -// todo add paragraph on state persistence - ## `Render` - A notable feature of non-EOA realms is the ability to have a render function. A render function allows the developer of the realm to choose how to render the state of the realm by returning a custom-made valid Markdown string. It also @@ -192,7 +192,7 @@ func Render(path string) string { :::info You can see the ender function in action by visiting the -[home page of Gno.land]((https://gno.land/) - it is actually the render of +[home page of Gno.land](https://gno.land/) - it is actually the render of `r/gnoland/home` realm. The same is true for the [Gno.land Blog](https://gno.land/r/gnoland/blog), and most other pages on the domain. ::: From ab7f8b35dfb140153913bfaa2a1a29cc2ff9ba0e Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 3 Apr 2024 22:54:49 +0900 Subject: [PATCH 16/33] save --- docs/concepts/packages.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index e09959e8e76..a5a5c91710d 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -1,12 +1,14 @@ -## Realms in code - -Realms are represented by a Realm type in Gno: -```go -type Realm struct { - addr Address - pkgPath string -} -``` -For the full Realm API, see the [reference page](../reference/standard-library/std/realm.md). +--- +id: packages +--- +# Packages +In Gno.land, packages are +- stateless +- meant to be reusable +- when writing write the g(n)o doc comments +- as opposed to the stdlibs, they are freely uploaded by the community, just like in go +- stdlibs usually provide low-level necessities for the langauge, while p/ packages +use them to create a broader range of functionality +- changes in packages are not breaking backwards comp, like changes in the stdlibs can From 899a3879067a3967b37bdf68b0a64032f7e321c3 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 4 Apr 2024 16:54:43 +0900 Subject: [PATCH 17/33] finish package page --- docs/concepts/packages.md | 112 +++++++++++++++++++++-- docs/concepts/realms.md | 10 +- examples/gno.land/p/demo/seqid/seqid.gno | 95 +++---------------- 3 files changed, 124 insertions(+), 93 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index a5a5c91710d..08997dd5846 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -3,12 +3,106 @@ id: packages --- # Packages -In Gno.land, packages are - -- stateless -- meant to be reusable -- when writing write the g(n)o doc comments -- as opposed to the stdlibs, they are freely uploaded by the community, just like in go -- stdlibs usually provide low-level necessities for the langauge, while p/ packages -use them to create a broader range of functionality -- changes in packages are not breaking backwards comp, like changes in the stdlibs can +In Gno.land, packages are Gno code which is meant to be reused by other Gno code, +be it by other packages or realms. Here are some defining features of packages: +- Packages are stored on-chain under the `"gno.land/p/"` path, and can de +written & deployed on-chain by anyone +- Packages are meant to be imported by other packages & realms +- Packages do not persist state - packages can have global variables & constants, +but any attempt to change their values will be discarded after a transaction +is completed +- Documentation for packages should be contained within package code itself, +in the form of comments, following the [Go doc standard](https://tip.golang.org/doc/comment). + +To learn how to write a package, +see [How to write a simple Gno Library](../how-to-guides/simple-library.md). + +## Commonly used packages +To better understand how packages work, let's look at a few commonly +used ones. Some of the most commonly used packages live in the +[`examples`](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/) +folder on the monorepo, and under the `"gno.land/p/demo"` on-chain path. + +### Package `avl` +Deployed under `gno.land/p/demo/avl`, the AVL package provides a tree structure +for storing data. Currently, the AVL package is used to replace the functionality +of the native `map` in Gno, as maps are not fully deterministic and thus do not +work as expected in the language. Here is how using the AVL package from your +realm might look like: + +```go +package myrealm + +import ( + "gno.land/p/demo/avl" +) + +// This AVL tree will be persisted after transaction calls +var tree *avl.Tree + +func Set(key string, value int) { + // tree.Set takes in a string key, and an interface{} value + tree.Set(key, value) +} + +func Get(key string) int { + // tree.Get returns the interface{} value at given key, + // and a bool to signify the existence of the key-value pair + rawValue, exists := tree.Get(key) + if !exists { + panic("value at given key does not exist") + } + // rawValue needs to be converted into the proper type + value := rawValue.(int) + return value +} +``` + +### Package `ufmt` +Deployed under `gno.land/p/demo/ufmt`, the `ufmt` package is a minimal version of +the `fmt` package. From [`ufmt.gno`](https://gno.land/p/demo/ufmt/ufmt.gno): + +```go +// Package ufmt provides utility functions for formatting strings, similarly +// to the Go package "fmt", of which only a subset is currently supported +// (hence the name µfmt - micro fmt). +package ufmt +``` + +View the package on-chain [here](https://gno.land/p/demo/ufmt), or on GitHub, +[here](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/ufmt). + +### Package `seqid` +Deployed under `gno.land/p/demo/seqid`, the `seqid` package provides a simple +way to have sequential IDs in Gno. Its encoding scheme is based on the `cford32` +package. From [`seqid.gno`](https://gno.land/p/demo/seqid/seqid.gno): + +```go +// Package seqid provides a simple way to have sequential IDs which will be +// ordered correctly when inserted in an AVL tree. +// +// Sample usage: +// +// var id seqid.ID +// var users avl.Tree +// +// func NewUser() { +// users.Set(id.Next().String(), &User{ ... }) +// } +package seqid +``` + +View the package on-chain [here](https://gno.land/p/demo/seqid), or on GitHub, +[here](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/seqid). + +## Packages vs Standard Libraries +Apart from packages, Gno, like Go, has a list of standard libraries. To better +understand the difference between these two concepts, let's compare a few +specific points: +- Packages can be written and deployed by anyone at any time, while standard + libraries require thorough battle-testing and reviews by the core team & community + before being added +- Standard libraries usually provide low-level necessities for the language, + while packages utilize them to create a broader range of functionality +- A breaking change in the standard libraries will equate to a breaking change + in the language, while the same is not true for packages \ No newline at end of file diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index d545e9a9d6e..3a6b13ba373 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -31,7 +31,7 @@ As opposed to [packages](./packages.md), realms are stateful, meaning they keep their state between transactions calls. In practice, global variables in the realm code are automatically persisted after a transaction has been executed, resulting in the fact that developers do not need to bother with the intricacies -of state management and persistence. +of state management and persistence, like they do with other languages. ### On-chain paths Since Gno.land is built for full transparency and auditability, all on-chain Gno @@ -183,7 +183,7 @@ package demo func Render(path string) string { if path == "" { - return "# Hello Gno!" + return "# Hello Gnopher!" } return "# Hello" + path @@ -191,8 +191,12 @@ func Render(path string) string { ``` :::info -You can see the ender function in action by visiting the +You can see the `Render` function in action by visiting the [home page of Gno.land](https://gno.land/) - it is actually the render of `r/gnoland/home` realm. The same is true for the [Gno.land Blog](https://gno.land/r/gnoland/blog), and most other pages on the domain. ::: + + +## Sending & receiving coins in Realms +// todo add section on this? \ No newline at end of file diff --git a/examples/gno.land/p/demo/seqid/seqid.gno b/examples/gno.land/p/demo/seqid/seqid.gno index b3ff815a421..2693e1b5ba3 100644 --- a/examples/gno.land/p/demo/seqid/seqid.gno +++ b/examples/gno.land/p/demo/seqid/seqid.gno @@ -1,91 +1,24 @@ -// Package seqid provides a simple way to have sequential IDs which will be -// ordered correctly when inserted in an AVL tree. -// -// Sample usage: -// -// var id seqid.ID -// var users avl.Tree -// -// func NewUser() { -// users.Set(id.Next().String(), &User{ ... }) -// } -package seqid +package main import ( - "encoding/binary" - - "gno.land/p/demo/cford32" + "gno.land/p/demo/avl" ) -// An ID is a simple sequential ID generator. -type ID uint64 +var tree *avl.Tree -// Next advances the ID i. -// It will panic if increasing ID would overflow. -func (i *ID) Next() ID { - next, ok := i.TryNext() - if !ok { - panic("seqid: next ID overflows uint64") - } - return next +func Set(key string, value int) { + // tree.Set takes in a string key, and an interface{} value + tree.Set(key, value) } -const maxID ID = 1<<64 - 1 - -// TryNext increases i by 1 and returns its value. -// It returns true if successful, or false if the increment would result in -// an overflow. -func (i *ID) TryNext() (ID, bool) { - if *i == maxID { - // Addition will overflow. - return 0, false +func Get(key string) { + // tree.Get returns the interface{} value at given key, + // and a bool to signify the existence of the key-value pair + rawValue, exists := tree.Get(key) + if !exists { + panic("value at given key does not exist") } - *i++ - return *i, true -} - -// Binary returns a big-endian binary representation of the ID, -// suitable to be used as an AVL key. -func (i ID) Binary() string { - buf := make([]byte, 8) - binary.BigEndian.PutUint64(buf, uint64(i)) - return string(buf) -} - -// String encodes i using cford32's compact encoding. For more information, -// see the documentation for package [gno.land/p/demo/cford32]. -// -// The result of String will be a 7-byte string for IDs [0,2^34), and a -// 13-byte string for all values following that. All generated string IDs -// follow the same lexicographic order as their number values; that is, for any -// two IDs (x, y) such that x < y, x.String() < y.String(). -// As such, this string representation is suitable to be used as an AVL key. -func (i ID) String() string { - return string(cford32.PutCompact(uint64(i))) -} - -// FromBinary creates a new ID from the given string, expected to be a binary -// big-endian encoding of an ID (such as that of [ID.Binary]). -// The second return value is true if the conversion was successful. -func FromBinary(b string) (ID, bool) { - if len(b) != 8 { - return 0, false - } - return ID(binary.BigEndian.Uint64([]byte(b))), true -} + // Get returns the + value := rawValue.(int) -// FromString creates a new ID from the given string, expected to be a string -// representation using cford32, such as that returned by [ID.String]. -// -// The encoding scheme used by cford32 allows the same ID to have many -// different representations (though the one returned by [ID.String] is only -// one, deterministic and safe to be used in AVL). The encoding scheme is -// "human-centric" and is thus case insensitive, and maps some ambiguous -// characters to be the same, ie. L = I = 1, O = 0. For this reason, when -// parsing user input to retrieve a key (encoded as a string), always sanitize -// it first using FromString, then run String(), instead of using the user's -// input directly. -func FromString(b string) (ID, error) { - n, err := cford32.Uint64([]byte(b)) - return ID(n), err } From d0848d42fb93674f208e356bdb27321a8da872e0 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 4 Apr 2024 16:55:15 +0900 Subject: [PATCH 18/33] remove coins section in realms --- docs/concepts/realms.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 3a6b13ba373..62e4ca3b269 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -195,8 +195,4 @@ You can see the `Render` function in action by visiting the [home page of Gno.land](https://gno.land/) - it is actually the render of `r/gnoland/home` realm. The same is true for the [Gno.land Blog](https://gno.land/r/gnoland/blog), and most other pages on the domain. -::: - - -## Sending & receiving coins in Realms -// todo add section on this? \ No newline at end of file +::: \ No newline at end of file From dca09477c208831da73e4b71aae44a8787c96164 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 4 Apr 2024 17:01:18 +0900 Subject: [PATCH 19/33] revert accidental change to seqid --- examples/gno.land/p/demo/seqid/seqid.gno | 95 ++++++++++++++++++++---- 1 file changed, 81 insertions(+), 14 deletions(-) diff --git a/examples/gno.land/p/demo/seqid/seqid.gno b/examples/gno.land/p/demo/seqid/seqid.gno index 2693e1b5ba3..b3ff815a421 100644 --- a/examples/gno.land/p/demo/seqid/seqid.gno +++ b/examples/gno.land/p/demo/seqid/seqid.gno @@ -1,24 +1,91 @@ -package main +// Package seqid provides a simple way to have sequential IDs which will be +// ordered correctly when inserted in an AVL tree. +// +// Sample usage: +// +// var id seqid.ID +// var users avl.Tree +// +// func NewUser() { +// users.Set(id.Next().String(), &User{ ... }) +// } +package seqid import ( - "gno.land/p/demo/avl" + "encoding/binary" + + "gno.land/p/demo/cford32" ) -var tree *avl.Tree +// An ID is a simple sequential ID generator. +type ID uint64 -func Set(key string, value int) { - // tree.Set takes in a string key, and an interface{} value - tree.Set(key, value) +// Next advances the ID i. +// It will panic if increasing ID would overflow. +func (i *ID) Next() ID { + next, ok := i.TryNext() + if !ok { + panic("seqid: next ID overflows uint64") + } + return next } -func Get(key string) { - // tree.Get returns the interface{} value at given key, - // and a bool to signify the existence of the key-value pair - rawValue, exists := tree.Get(key) - if !exists { - panic("value at given key does not exist") +const maxID ID = 1<<64 - 1 + +// TryNext increases i by 1 and returns its value. +// It returns true if successful, or false if the increment would result in +// an overflow. +func (i *ID) TryNext() (ID, bool) { + if *i == maxID { + // Addition will overflow. + return 0, false } - // Get returns the - value := rawValue.(int) + *i++ + return *i, true +} + +// Binary returns a big-endian binary representation of the ID, +// suitable to be used as an AVL key. +func (i ID) Binary() string { + buf := make([]byte, 8) + binary.BigEndian.PutUint64(buf, uint64(i)) + return string(buf) +} + +// String encodes i using cford32's compact encoding. For more information, +// see the documentation for package [gno.land/p/demo/cford32]. +// +// The result of String will be a 7-byte string for IDs [0,2^34), and a +// 13-byte string for all values following that. All generated string IDs +// follow the same lexicographic order as their number values; that is, for any +// two IDs (x, y) such that x < y, x.String() < y.String(). +// As such, this string representation is suitable to be used as an AVL key. +func (i ID) String() string { + return string(cford32.PutCompact(uint64(i))) +} + +// FromBinary creates a new ID from the given string, expected to be a binary +// big-endian encoding of an ID (such as that of [ID.Binary]). +// The second return value is true if the conversion was successful. +func FromBinary(b string) (ID, bool) { + if len(b) != 8 { + return 0, false + } + return ID(binary.BigEndian.Uint64([]byte(b))), true +} +// FromString creates a new ID from the given string, expected to be a string +// representation using cford32, such as that returned by [ID.String]. +// +// The encoding scheme used by cford32 allows the same ID to have many +// different representations (though the one returned by [ID.String] is only +// one, deterministic and safe to be used in AVL). The encoding scheme is +// "human-centric" and is thus case insensitive, and maps some ambiguous +// characters to be the same, ie. L = I = 1, O = 0. For this reason, when +// parsing user input to retrieve a key (encoded as a string), always sanitize +// it first using FromString, then run String(), instead of using the user's +// input directly. +func FromString(b string) (ID, error) { + n, err := cford32.Uint64([]byte(b)) + return ID(n), err } From 00e5469f4e19b3072028876b37d4c69022fc15c6 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 4 Apr 2024 22:10:35 +0900 Subject: [PATCH 20/33] typo --- docs/concepts/packages.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index 08997dd5846..6836086436d 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -5,7 +5,7 @@ id: packages # Packages In Gno.land, packages are Gno code which is meant to be reused by other Gno code, be it by other packages or realms. Here are some defining features of packages: -- Packages are stored on-chain under the `"gno.land/p/"` path, and can de +- Packages are stored on-chain under the `"gno.land/p/"` path, and can be written & deployed on-chain by anyone - Packages are meant to be imported by other packages & realms - Packages do not persist state - packages can have global variables & constants, From e791bd8991360a38989d9b50f9330c9e20b0ec60 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 4 Apr 2024 22:21:03 +0900 Subject: [PATCH 21/33] replace interface with any --- docs/concepts/packages.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index 6836086436d..e86de81b355 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -41,12 +41,12 @@ import ( var tree *avl.Tree func Set(key string, value int) { - // tree.Set takes in a string key, and an interface{} value + // tree.Set takes in a string key, and a value that can be of any type tree.Set(key, value) } func Get(key string) int { - // tree.Get returns the interface{} value at given key, + // tree.Get returns the value at given key in its raw form, // and a bool to signify the existence of the key-value pair rawValue, exists := tree.Get(key) if !exists { From 9fa722c707e3ddcc21e49d41186e990388aa9f55 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Fri, 5 Apr 2024 00:57:54 +0900 Subject: [PATCH 22/33] inline --- docs/concepts/packages.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index e86de81b355..d0a2c4349a5 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -52,9 +52,8 @@ func Get(key string) int { if !exists { panic("value at given key does not exist") } - // rawValue needs to be converted into the proper type - value := rawValue.(int) - return value + // rawValue needs to be converted into the proper type before returning it + return rawValue.(int) } ``` From b633dbc9c361c4869f04e7b9d16a749f4f595377 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Fri, 5 Apr 2024 00:58:39 +0900 Subject: [PATCH 23/33] remove "leftover" --- docs/concepts/realms.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 62e4ca3b269..536f79703b3 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -48,8 +48,6 @@ to change. To view realms on the `test3` network (depr.), prepend `test3` to the domain: [`test3.gno.land/r/demo/users`](https://test3.gno.land/r/demo/users). ::: -[//]: # (Learn more about package paths & allowed namespaces [here].) - To learn how to write a realm, see [How to write a simple Gno Smart Contract](../how-to-guides/simple-contract.md). ## Externally Owned Accounts (EOAs) From 3a857ad8411bb0173da10b5be87683fe705e40d3 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Fri, 5 Apr 2024 00:59:51 +0900 Subject: [PATCH 24/33] phrasing --- docs/concepts/packages.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index d0a2c4349a5..ee6b20ab792 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -95,13 +95,13 @@ View the package on-chain [here](https://gno.land/p/demo/seqid), or on GitHub, [here](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/seqid). ## Packages vs Standard Libraries -Apart from packages, Gno, like Go, has a list of standard libraries. To better +Apart from packages, Gno, like Go, has standard libraries. To better understand the difference between these two concepts, let's compare a few specific points: - Packages can be written and deployed by anyone at any time, while standard - libraries require thorough battle-testing and reviews by the core team & community - before being added +libraries require thorough battle-testing and reviews by the core team & community +before being added as a standard library - Standard libraries usually provide low-level necessities for the language, - while packages utilize them to create a broader range of functionality +while packages utilize them to create a broader range of functionality - A breaking change in the standard libraries will equate to a breaking change - in the language, while the same is not true for packages \ No newline at end of file +in the language, while the same is not true for packages \ No newline at end of file From a2efdc791e4771d535150c26fc1e6150bce63c74 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Fri, 5 Apr 2024 01:01:04 +0900 Subject: [PATCH 25/33] rephrasing --- docs/concepts/packages.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index ee6b20ab792..12d2db7ad07 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -100,7 +100,7 @@ understand the difference between these two concepts, let's compare a few specific points: - Packages can be written and deployed by anyone at any time, while standard libraries require thorough battle-testing and reviews by the core team & community -before being added as a standard library +before being added to the language - Standard libraries usually provide low-level necessities for the language, while packages utilize them to create a broader range of functionality - A breaking change in the standard libraries will equate to a breaking change From 28e23d4eee83d4a15e29807eed82f51ac8b96e21 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Fri, 5 Apr 2024 12:18:14 +0900 Subject: [PATCH 26/33] remove breaking change line --- docs/concepts/packages.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index 12d2db7ad07..f032c09840c 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -102,6 +102,4 @@ specific points: libraries require thorough battle-testing and reviews by the core team & community before being added to the language - Standard libraries usually provide low-level necessities for the language, -while packages utilize them to create a broader range of functionality -- A breaking change in the standard libraries will equate to a breaking change -in the language, while the same is not true for packages \ No newline at end of file +while packages utilize them to create a broader range of functionality \ No newline at end of file From 306afca456c2ca22f370acb204ac82044e69788c Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 8 Apr 2024 23:46:58 +0900 Subject: [PATCH 27/33] add abci query mention --- docs/concepts/realms.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 536f79703b3..cfea374f636 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -35,9 +35,9 @@ of state management and persistence, like they do with other languages. ### On-chain paths Since Gno.land is built for full transparency and auditability, all on-chain Gno -code is open-sourced. You can view realm code by simply going to its path in -your web browser. For example, to take a look at the `gno.land/r/demo/users` realm, -used for user registration, by visiting +code is open-sourced. You can view realm code with an ABCI query, or simply going +to its path in your web browser. For example, to take a look at the +`gno.land/r/demo/users` realm, used for user registration, by visiting [`gno.land/r/demo/users`](https://gno.land/r/demo/users/users.gno). :::info From 3fe4c6597cc9aedd14c6858e3cba3afb03603d88 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 8 Apr 2024 23:51:25 +0900 Subject: [PATCH 28/33] add IsUser ref --- docs/reference/standard-library/std/realm.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/reference/standard-library/std/realm.md b/docs/reference/standard-library/std/realm.md index 1f6b6ca93df..0c99b7134ea 100644 --- a/docs/reference/standard-library/std/realm.md +++ b/docs/reference/standard-library/std/realm.md @@ -31,3 +31,11 @@ Returns the **string** package path of the realm it was called upon. ```go realmPath := r.PkgPath() // eg. gno.land/r/gnoland/blog ``` +--- +## IsUser +Checks if the realm it was called upon is a user realm. + +#### Usage +```go +if r.IsUser() {...} +``` From 674e80c4573dca9554dd6a638aaef64f2d8f50e4 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 8 Apr 2024 23:54:04 +0900 Subject: [PATCH 29/33] fix prevrealm ref --- docs/concepts/realms.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index cfea374f636..7eff7ae7c7b 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -103,7 +103,10 @@ Realm A: Let's look at return values for each of the methods: ```go std.GetOrigCaller() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` -std.PrevRealm() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.PrevRealm() => Realm { + addr: `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` + pkgPath: `` +} std.CurrentRealm() => Realm { addr: `g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s` pkgPath: `gno.land/r/demo/users` @@ -144,7 +147,10 @@ Depending on which realm the methods are called in, the values will change. For `Realm A`: ```go std.GetOrigCaller() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` -std.PrevRealm() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` +std.PrevRealm() => Realm { + addr: `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` + pkgPath: `` +} std.CurrentRealm() => Realm { addr: `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` pkgPath: `gno.land/r/demo/a` @@ -158,7 +164,10 @@ std.GetCallerAt(3) => error For `Realm B`: ```go std.GetOrigCaller() => `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5` -std.PrevRealm() => `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` +std.PrevRealm() => Realm { + addr: `g1dvqd8qgvavqayxklzfdmccd2eps263p43pu2c6` + pkgPath: `gno.land/r/demo/a` +} std.CurrentRealm() => Realm { addr: `g1rsk9cwv034cw3s6csjeun2jqypj0ztpecqcm3v` pkgPath: `gno.land/r/demo/b` From 9f1c9bb08af3c9d3da24a53a0cdf09b99684903f Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 11 Sep 2024 14:00:40 +0200 Subject: [PATCH 30/33] minor fixups --- docs/concepts/packages.md | 7 ++++++- docs/concepts/realms.md | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index f032c09840c..d520c726470 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -3,7 +3,8 @@ id: packages --- # Packages -In Gno.land, packages are Gno code which is meant to be reused by other Gno code, + +In gno.land, packages are Gno code which is meant to be reused by other Gno code, be it by other packages or realms. Here are some defining features of packages: - Packages are stored on-chain under the `"gno.land/p/"` path, and can be written & deployed on-chain by anyone @@ -18,12 +19,14 @@ To learn how to write a package, see [How to write a simple Gno Library](../how-to-guides/simple-library.md). ## Commonly used packages + To better understand how packages work, let's look at a few commonly used ones. Some of the most commonly used packages live in the [`examples`](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/) folder on the monorepo, and under the `"gno.land/p/demo"` on-chain path. ### Package `avl` + Deployed under `gno.land/p/demo/avl`, the AVL package provides a tree structure for storing data. Currently, the AVL package is used to replace the functionality of the native `map` in Gno, as maps are not fully deterministic and thus do not @@ -58,6 +61,7 @@ func Get(key string) int { ``` ### Package `ufmt` + Deployed under `gno.land/p/demo/ufmt`, the `ufmt` package is a minimal version of the `fmt` package. From [`ufmt.gno`](https://gno.land/p/demo/ufmt/ufmt.gno): @@ -95,6 +99,7 @@ View the package on-chain [here](https://gno.land/p/demo/seqid), or on GitHub, [here](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/seqid). ## Packages vs Standard Libraries + Apart from packages, Gno, like Go, has standard libraries. To better understand the difference between these two concepts, let's compare a few specific points: diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 7eff7ae7c7b..a2bbe35821b 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -3,7 +3,8 @@ id: realms --- # Realms -In Gno.land, realms are entities that are addressable and identifiable by a + +In gno.land, realms are entities that are addressable and identifiable by a [Gno address](../reference/standard-library/std/address.md). These can be user realms (EOAs), as well as smart contract realms. Realms have several properties: @@ -23,6 +24,7 @@ The full Realm API can be found under the [reference section](../reference/standard-library/std/realm.md). ## Smart Contract Realms + Often simply called `realms`, Gno smart contracts contain Gno code and exist on-chain at a specific package path. A package path is the defining identifier of a realm, while its address is derived from it. @@ -34,7 +36,8 @@ resulting in the fact that developers do not need to bother with the intricacies of state management and persistence, like they do with other languages. ### On-chain paths -Since Gno.land is built for full transparency and auditability, all on-chain Gno + +Since gno.land is built for full transparency and auditability, all on-chain Gno code is open-sourced. You can view realm code with an ABCI query, or simply going to its path in your web browser. For example, to take a look at the `gno.land/r/demo/users` realm, used for user registration, by visiting @@ -51,17 +54,19 @@ the domain: [`test3.gno.land/r/demo/users`](https://test3.gno.land/r/demo/users) To learn how to write a realm, see [How to write a simple Gno Smart Contract](../how-to-guides/simple-contract.md). ## Externally Owned Accounts (EOAs) + EOAs, or simply `user realms`, are Gno addresses generated from a BIP39 mnemonic phrase in a key management application, such as [`gnokey`](../gno-tooling/cli/gnokey.md), and [Adena](https://adena.app). Currently, EOAs are the only realms that can initiate a transaction. They can do -this by calling any of the possible messages in Gno.land, such as +this by calling any of the possible messages in gno.land, such as [Call](../gno-tooling/cli/gnokey.md#call), [AddPackage](../gno-tooling/cli/gnokey.md#addpkg), [Send](../gno-tooling/cli/gnokey.md#send), or Run. ## Working with realms + In Gno, each transaction consists of a call stack, which is made up of `frames`. A single frame is a unique realm in the call stack. Every frame and its properties can be accessed via different functions defined in the `std` package in Gno: @@ -79,6 +84,7 @@ Let's look at return values of these functions in two distinct situations: 2. EOA calling a sequence of realms ### 1. EOA calling a realm + Take these two actors in the call stack: ``` EOA: @@ -118,6 +124,7 @@ std.GetCallerAt(3) => error ``` ### 2. EOA calling a sequence of realms + Take these three actors in the call stack: ``` EOA: @@ -180,6 +187,7 @@ std.GetCallerAt(4) => error ``` ## `Render` + A notable feature of non-EOA realms is the ability to have a render function. A render function allows the developer of the realm to choose how to render the state of the realm by returning a custom-made valid Markdown string. It also @@ -199,7 +207,7 @@ func Render(path string) string { :::info You can see the `Render` function in action by visiting the -[home page of Gno.land](https://gno.land/) - it is actually the render of +[home page of gno.land](https://gno.land/) - it is actually the render of `r/gnoland/home` realm. The same is true for the -[Gno.land Blog](https://gno.land/r/gnoland/blog), and most other pages on the domain. +[gno.land Blog](https://gno.land/r/gnoland/blog), and most other pages on the domain. ::: \ No newline at end of file From 4a77688d5fecdbd81b14b6a7edb44be2b3dcf123 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 11 Sep 2024 14:02:08 +0200 Subject: [PATCH 31/33] make build --- docs/concepts/packages.md | 2 +- docs/concepts/realms.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index d520c726470..eaf82861d93 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -107,4 +107,4 @@ specific points: libraries require thorough battle-testing and reviews by the core team & community before being added to the language - Standard libraries usually provide low-level necessities for the language, -while packages utilize them to create a broader range of functionality \ No newline at end of file +while packages utilize them to create a broader range of functionality diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index a2bbe35821b..c1ac7cdd7ad 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -210,4 +210,4 @@ You can see the `Render` function in action by visiting the [home page of gno.land](https://gno.land/) - it is actually the render of `r/gnoland/home` realm. The same is true for the [gno.land Blog](https://gno.land/r/gnoland/blog), and most other pages on the domain. -::: \ No newline at end of file +::: From edb9c429e4ec69e9709a26c1937bf4be523bcaa5 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 11 Sep 2024 14:05:49 +0200 Subject: [PATCH 32/33] fix links --- docs/concepts/realms.md | 17 +++++++++-------- docs/concepts/stdlibs/coin.md | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index c1ac7cdd7ad..931de4bee68 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -5,11 +5,11 @@ id: realms # Realms In gno.land, realms are entities that are addressable and identifiable by a -[Gno address](../reference/standard-library/std/address.md). These can be user +[Gno address](../reference/stdlibs/std/address.md). These can be user realms (EOAs), as well as smart contract realms. Realms have several properties: -- They can own, receive & send [Coins](./standard-library/coin.md) through the -[Banker](./standard-library/banker.md) module +- They can own, receive & send [Coins](./stdlibs/coin.md) through the +[Banker](./stdlibs/banker.md) module - They can be part of a transaction call stack, as a caller or a callee - They can be with or without code - EOAs, or smart contracts @@ -21,7 +21,7 @@ type Realm struct { } ``` The full Realm API can be found under the -[reference section](../reference/standard-library/std/realm.md). +[reference section](../reference/stdlibs/std/realm.md). ## Smart Contract Realms @@ -57,13 +57,14 @@ To learn how to write a realm, see [How to write a simple Gno Smart Contract](.. EOAs, or simply `user realms`, are Gno addresses generated from a BIP39 mnemonic phrase in a key management application, such as -[`gnokey`](../gno-tooling/cli/gnokey.md), and [Adena](https://adena.app). +[`gnokey`](../gno-tooling/cli/gnokey/gnokey.md), and [Adena](https://adena.app). Currently, EOAs are the only realms that can initiate a transaction. They can do this by calling any of the possible messages in gno.land, such as -[Call](../gno-tooling/cli/gnokey.md#call), -[AddPackage](../gno-tooling/cli/gnokey.md#addpkg), -[Send](../gno-tooling/cli/gnokey.md#send), or Run. +[Call](../gno-tooling/cli/gnokey/state-changing-calls.md#call), +[AddPackage](../gno-tooling/cli/gnokey/state-changing-calls.md#addpackage), +[Send](../gno-tooling/cli/gnokey/state-changing-calls.md#send), or +[Run](../gno-tooling/cli/gnokey/state-changing-calls.md#run). ## Working with realms diff --git a/docs/concepts/stdlibs/coin.md b/docs/concepts/stdlibs/coin.md index 0c64a61bed6..5f08f1942d1 100644 --- a/docs/concepts/stdlibs/coin.md +++ b/docs/concepts/stdlibs/coin.md @@ -35,4 +35,4 @@ Bankers, which can manipulate them depending on access rights. Read more about coins in the [Effective Gno](../effective-gno.md#coins) section. The Coin(s) API can be found in under the `std` package -[reference](../../reference/standard-library/std/coin.md). +[reference](../../reference/stdlibs/std/coin.md). From ee62df01b90da3304ee2129d3fbdd20a672fb141 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 11 Sep 2024 14:24:43 +0200 Subject: [PATCH 33/33] pure pacakges --- docs/concepts/packages.md | 37 +++++++++++++++++++------------------ docs/concepts/realms.md | 1 + 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/docs/concepts/packages.md b/docs/concepts/packages.md index eaf82861d93..d857386f8e5 100644 --- a/docs/concepts/packages.md +++ b/docs/concepts/packages.md @@ -2,27 +2,26 @@ id: packages --- -# Packages - -In gno.land, packages are Gno code which is meant to be reused by other Gno code, -be it by other packages or realms. Here are some defining features of packages: -- Packages are stored on-chain under the `"gno.land/p/"` path, and can be -written & deployed on-chain by anyone -- Packages are meant to be imported by other packages & realms -- Packages do not persist state - packages can have global variables & constants, -but any attempt to change their values will be discarded after a transaction -is completed -- Documentation for packages should be contained within package code itself, +# Pure Packages + +In gno.land, pure packages are Gno code that is meant to be reused by other Gno code, +be it by other pure packages or realms. Here are some defining features of packages: +- Pure packages are stored on-chain under the `"gno.land/p/"` path, and can be +written & deployed on-chain by anyone, permissionlessly +- Pure packages are meant to be imported by other packages & realms +- Pure packages do not persist state - packages can have global variables & constants, +but any attempt to change their values will be discarded, +- Documentation for pure packages should be contained within package code itself, in the form of comments, following the [Go doc standard](https://tip.golang.org/doc/comment). -To learn how to write a package, +To learn how to write a pure package, see [How to write a simple Gno Library](../how-to-guides/simple-library.md). -## Commonly used packages +## Commonly used `p/` packages To better understand how packages work, let's look at a few commonly used ones. Some of the most commonly used packages live in the -[`examples`](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/) +[`examples`](https://github.com/gnolang/gno/tree/master/examples/) folder on the monorepo, and under the `"gno.land/p/demo"` on-chain path. ### Package `avl` @@ -55,6 +54,7 @@ func Get(key string) int { if !exists { panic("value at given key does not exist") } + // rawValue needs to be converted into the proper type before returning it return rawValue.(int) } @@ -76,6 +76,7 @@ View the package on-chain [here](https://gno.land/p/demo/ufmt), or on GitHub, [here](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/ufmt). ### Package `seqid` + Deployed under `gno.land/p/demo/seqid`, the `seqid` package provides a simple way to have sequential IDs in Gno. Its encoding scheme is based on the `cford32` package. From [`seqid.gno`](https://gno.land/p/demo/seqid/seqid.gno): @@ -98,13 +99,13 @@ package seqid View the package on-chain [here](https://gno.land/p/demo/seqid), or on GitHub, [here](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/seqid). -## Packages vs Standard Libraries +## Pure packages vs Standard Libraries -Apart from packages, Gno, like Go, has standard libraries. To better +Apart from pure packages, Gno, like Go, has standard libraries. To better understand the difference between these two concepts, let's compare a few specific points: -- Packages can be written and deployed by anyone at any time, while standard +- Pure packages can be written and deployed by anyone at any time, while standard libraries require thorough battle-testing and reviews by the core team & community before being added to the language - Standard libraries usually provide low-level necessities for the language, -while packages utilize them to create a broader range of functionality +while pure packages utilize them to create a broader range of functionality diff --git a/docs/concepts/realms.md b/docs/concepts/realms.md index 931de4bee68..029a8201ab0 100644 --- a/docs/concepts/realms.md +++ b/docs/concepts/realms.md @@ -20,6 +20,7 @@ type Realm struct { pkgPath string // realm's path on-chain } ``` + The full Realm API can be found under the [reference section](../reference/stdlibs/std/realm.md).