Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add gnoclient reference & how-to guide #1562

Merged
merged 23 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
---
id: gno-js-getting-started
id: gno-js-client
---

# Getting Started
# Gno JavaScript Client

[@gnolang/gno-js-client](https://github.com/gnolang/gno-js-client) is a JavaScript/TypeScript client implementation for Gno chains. It is an extension of the
[tm2-js-client](https://github.com/gnolang/tm2-js-client), but with Gno-specific functionality.
[@gnolang/gno-js-client](https://github.com/gnolang/gno-js-client) is a JavaScript/TypeScript client implementation
for Gno chains. It is an extension of the [tm2-js-client](https://github.com/gnolang/tm2-js-client), but with
Gno-specific functionality.

## Key Features

Expand Down
148 changes: 148 additions & 0 deletions docs/reference/gnoclient/client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
id: client
---

# Client

## type [Client](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client.go#L8-L11>)

`Client` provides an interface for interacting with the blockchain. It is the main
struct of the `gnoclient` package, exposing the high level

```go
type Client struct {
Signer Signer // Signer for transaction authentication
RPCClient rpcclient.Client // RPC client for blockchain communication
}
```

### func \(\*Client\) [Call](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_txs.go#L56>)

```go
func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTxCommit, error)
```

`Call` executes a one or more [MsgCall](#type-msgcall) calls on the blockchain.

### func \(\*Client\) [Send](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_txs.go#L176>)

```go
func (c *Client) Send(cfg BaseTxCfg, msgs ...MsgSend) (*ctypes.ResultBroadcastTxCommit, error)
```

`Send` executes a one or more [MsgSend](#type-msgsend) calls on the blockchain.

### func \(\*Client\) [Run](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_txs.go#L112>)

```go
func (c *Client) Run(cfg BaseTxCfg, msgs ...MsgRun) (*ctypes.ResultBroadcastTxCommit, error)
```

`Run` executes a one or more MsgRun calls on the blockchain.

### func \(*Client\) [QEval](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_queries.go#L108>)

```go
func (c *Client) QEval(pkgPath string, expression string) (string, *ctypes.ResultABCIQuery, error)
```

`QEval` evaluates the given expression with the realm code at `pkgPath`.
The `pkgPath` should include the prefix like `gno.land/`. The expression is
usually a function call like `Render("")`.

### func \(*Client\) [Query](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_queries.go#L22>)

```go
func (c *Client) Query(cfg QueryCfg) (*ctypes.ResultABCIQuery, error)
```

`Query` performs a generic query on the blockchain.

### func \(*Client\) [QueryAccount](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_queries.go#L39>)

```go
func (c *Client) QueryAccount(addr crypto.Address) (*std.BaseAccount, *ctypes.ResultABCIQuery, error)
```

`QueryAccount` retrieves account information for a given address.

### func \(*Client\) [QueryAppVersion](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_queries.go#L65>)

```go
func (c *Client) QueryAppVersion() (string, *ctypes.ResultABCIQuery, error)
```

`QueryAppVersion` retrieves information about the Gno.land app version.

### func \(*Client\) [Render](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_queries.go#L85>)

```go
func (c *Client) Render(pkgPath string, args string) (string, *ctypes.ResultABCIQuery, error)
```

`Render` calls the Render function for pkgPath with optional args. The `pkgPath`
should include the prefix like `gno.land/`. This is similar to using a browser
URL `<testnet>/<pkgPath>:<args>` where `<pkgPath>` doesn't have the prefix like
`gno.land/`.

## type [BaseTxCfg](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_txs.go#L27-L33>)

`BaseTxCfg` defines the base transaction configuration, shared by all message
types.

```go
type BaseTxCfg struct {
GasFee string // Gas fee
GasWanted int64 // Gas wanted
AccountNumber uint64 // Account number
SequenceNumber uint64 // Sequence number
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
Memo string // Memo
}
```

## type [MsgCall](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_txs.go#L36-L41>)

`MsgCall` \- syntax sugar for `vm.MsgCall`.

```go
type MsgCall struct {
PkgPath string // Package path
FuncName string // Function name
Args []string // Function arguments
Send string // Send amount
}
```

## type [MsgRun](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_txs.go#L50-L53>)

`MsgRun` \- syntax sugar for `vm.MsgRun`.

```go
type MsgRun struct {
Package *std.MemPackage // Package to run
Send string // Send amount
}
```

## type [MsgSend](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_txs.go#L44-L47>)

`MsgSend` \- syntax sugar for `bank.MsgSend`.

```go
type MsgSend struct {
ToAddress crypto.Address // Send to address
Send string // Send amount
}
```

## type [QueryCfg](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/client_queries.go#L15-L19>)

`QueryCfg` contains configuration options for performing ABCI queries.

```go
type QueryCfg struct {
Path string // Query path
Data []byte // Query data
rpcclient.ABCIQueryOptions // ABCI query options
}
```
25 changes: 25 additions & 0 deletions docs/reference/gnoclient/gnoclient.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
id: gnoclient
---

# Gnoclient

[Gnoclient](https://github.com/gnolang/gno/tree/master/gno.land/pkg/gnoclient)
allows you to easily access Gno blockchains from your Go code, through exposing
APIs for common functionality.

## Key Features

- Connect to a Gno chain via RPC
- Use local keystore to sign & broadcast transactions containing any type of
Gno message
- Sign & broadcast transactions with batch messages
- Use [ABCI queries](../../gno-tooling/cli/gnokey.md#make-an-abci-query) in
your Go code

## Installation

To add Gnoclient to your Go project, run the following command:
```bash
go get github.com/gnolang/gno/gno.land/pkg/gnoclient
```
86 changes: 86 additions & 0 deletions docs/reference/gnoclient/signer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
id: signer
---

# Signer

`Signer` is an interface that provides functionality for signing transactions.
The signer can be created from a local keybase, or from a bip39 mnemonic phrase.

Useful types and functions when using the `Signer` can be found below.

## type [Signer](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/signer.go#L13-L17>)

`Signer` provides an interface for signing transactions.

```go
type Signer interface {
Sign(SignCfg) (*std.Tx, error) // Signs a transaction and returns a signed tx ready for broadcasting.
Info() keys.Info // Returns key information, including the address.
Validate() error // Checks whether the signer is properly configured.
}
```

## type [SignCfg](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/signer.go#L65-L69>)

`SignCfg` provides the signing configuration, containing the unsigned transaction
data, account number, and account sequence.

```go
type SignCfg struct {
UnsignedTX std.Tx
SequenceNumber uint64
AccountNumber uint64
}
```

## type [SignerFromKeybase](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/signer.go#L20-L25>)

`SignerFromKeybase` represents a signer created from a Keybase.

```go
type SignerFromKeybase struct {
Keybase keys.Keybase // Stores keys in memory or on disk
Account string // Account name or bech32 format
Password string // Password for encryption
ChainID string // Chain ID for transaction signing
}
```

### func \(SignerFromKeybase\) [Info](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/signer.go#L56>)

```go
func (s SignerFromKeybase) Info() keys.Info
```

`Info` gets keypair information.

### func \(SignerFromKeybase\) [Sign](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/signer.go#L72>)

```go
func (s SignerFromKeybase) Sign(cfg SignCfg) (*std.Tx, error)
```

`Sign` implements the Signer interface for SignerFromKeybase.

### func \(SignerFromKeybase\) [Validate](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/signer.go#L28>)

```go
func (s SignerFromKeybase) Validate() error
```

`Validate` checks if the signer is properly configured.

## func [SignerFromBip39](<https://github.com/gnolang/gno/blob/master/gno.land/pkg/gnoclient/signer.go#L130>)

```go
func SignerFromBip39(mnemonic string, chainID string, passphrase string, account uint32, index uint32) (Signer, error)
```

`SignerFromBip39` creates a `Signer` from an in-memory keybase with a single default
account, derived from the given mnemonic.
This can be useful in scenarios where storing private keys in the filesystem
isn't feasible, or for generating a signer for testing.

> Using `keys.NewKeyBaseFromDir()` to get a keypair from local storage is
recommended where possible, as it is more secure.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
id: tm2-js-getting-started
id: tm2-js-client
---

# Getting Started
# Tendermint2 JavaScript Client

[@gnolang/tm2-js-client](https://github.com/gnolang/tm2-js-client) is a JavaScript/TypeScript client implementation for
Tendermint2-based chains. It is designed to make it
easy for developers to interact with TM2 chains, providing a simplified API for account and transaction management. By
doing all the heavy lifting behind the scenes, `@gnolang/tm2-js-client` enables developers to focus on what really
matters -
building their dApps.
[@gnolang/tm2-js-client](https://github.com/gnolang/tm2-js-client) is a JavaScript/TypeScript client implementation
for Tendermint2-based chains. It is designed to make it
easy for developers to interact with TM2 chains, providing a simplified API for
account and transaction management. By doing all the heavy lifting behind the
scenes, `@gnolang/tm2-js-client` enables developers to focus on what really
matters - building their dApps.

## Key Features

Expand All @@ -33,33 +33,41 @@ npm install @gnolang/tm2-js-client

### Provider

A `Provider` is an interface that abstracts the interaction with the Tendermint2 chain, making it easier for users to
communicate with it. Rather than requiring users to understand which endpoints are exposed, what their return types are,
and how they are parsed, the `Provider` abstraction handles all of this behind the scenes. It exposes useful API methods
that users can use and expects concrete types in return.
A `Provider` is an interface that abstracts the interaction with the Tendermint2
chain, making it easier for users to communicate with it. Rather than requiring
users to understand which endpoints are exposed, what their return types are,
and how they are parsed, the `Provider` abstraction handles all of this behind
the scenes. It exposes useful API methods that users can use and expects
concrete types in return.

Currently, the `@gnolang/tm2-js-client` package provides support for two Provider implementations:
Currently, the `@gnolang/tm2-js-client` package provides support for two
Provider implementations:

- `JSON-RPC Provider`: executes each call as a separate HTTP RPC call.
- `WS Provider`: executes each call through an active WebSocket connection, which requires closing when not needed
anymore.
- `WS Provider`: executes each call through an active WebSocket connection,
which requires closing when not needed anymore.

### Signer

A `Signer` is an interface that abstracts the interaction with a single Secp256k1 key pair. It exposes methods for
signing data, verifying signatures, and getting metadata associated with the key pair, such as the address.
A `Signer` is an interface that abstracts the interaction with a single
Secp256k1 key pair. It exposes methods for signing data, verifying signatures,
and getting metadata associated with the key pair, such as the address.

Currently, the `@gnolang/tm2-js-client` package provides support for two `Signer` implementations:
Currently, the `@gnolang/tm2-js-client` package provides support for two
`Signer` implementations:

- `Key`: a signer that is based on a raw Secp256k1 key pair.
- `Ledger`: a signer that is based on a Ledger device, with all interaction flowing through the user's device.
- `Ledger`: a signer that is based on a Ledger device, with all interaction
flowing through the user's device.

### Wallet

A `Wallet` is a user-facing API that is used to interact with an account. A `Wallet` instance is tied to a single key
pair and essentially wraps the given `Provider` for that specific account.
A `Wallet` is a user-facing API that is used to interact with an account.
A `Wallet` instance is tied to a single key pair and essentially wraps the given
`Provider` for that specific account.

A wallet can be generated from a randomly generated seed, a private key, or instantiated using a Ledger device.
A wallet can be generated from a randomly generated seed, a private key, or
instantiated using a Ledger device.

Using the `Wallet`, users can easily interact with the Tendermint2 chain using their account without having to worry
about account management.
Using the `Wallet`, users can easily interact with the Tendermint2 chain using
their account without having to worry about account management.
4 changes: 2 additions & 2 deletions gno.land/pkg/gnoclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ type Client struct {
}

// validateSigner checks that the signer is correctly configured.
func (c Client) validateSigner() error {
func (c *Client) validateSigner() error {
if c.Signer == nil {
return ErrMissingSigner
}
return nil
}

// validateRPCClient checks that the RPCClient is correctly configured.
func (c Client) validateRPCClient() error {
func (c *Client) validateRPCClient() error {
if c.RPCClient == nil {
return ErrMissingRPCClient
}
Expand Down
Loading
Loading