gnoclient

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2024 License: UNKNOWN not legal advice Imports: 0 Imported by: 0

README

Gno.land Go Client

The Gno.land Go client is a dedicated library for interacting seamlessly with the Gno.land RPC API. This library simplifies the process of querying or sending transactions to the Gno.land RPC API and interpreting the responses.

Documentation may be found at gnoclient.

Installation

Integrate this library into your Go project with the following command:

go get github.com/gnolang/gno/gno.land/pkg/gnoclient

Development Plan

The roadmap for the Gno.land Go client includes:

  • Initial Development: Kickstart the development specifically for Gno.land. Subsequently, transition the generic functionalities to other modules like tm2, gnovm, gnosdk.
  • Integration: Begin incorporating this library within various components such as gno.land/cmd/* and other external clients, including gnoblog-client, the Discord community faucet bot, and GnoMobile.
  • Enhancements: Once the generic client establishes a robust foundation, we aim to utilize code generation for contracts. This will streamline the creation of type-safe, contract-specific clients.

Documentation

Overview

Example (ReadOnly)

Example_readOnly demonstrates how to initialize a read-only gnoclient, which can only query.

package main

import (
	"github.com/gnolang/gno/gno.land/pkg/gnoclient"
	rpcclient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
)

func main() {
	remote := "127.0.0.1:26657"
	rpcClient, _ := rpcclient.NewHTTPClient(remote)

	client := gnoclient.Client{
		RPCClient: rpcClient,
	}
	_ = client
}
Output:

Example (WithDisk)

Example_withDisk demonstrates how to initialize a gnoclient with a keybase sourced from a directory.

package main

import (
	"github.com/gnolang/gno/gno.land/pkg/gnoclient"
	rpcclient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
	"github.com/gnolang/gno/tm2/pkg/crypto/keys"
)

func main() {
	kb, _ := keys.NewKeyBaseFromDir("/path/to/dir")
	signer := gnoclient.SignerFromKeybase{
		Keybase:  kb,
		Account:  "mykey",
		Password: "secure",
	}

	remote := "127.0.0.1:26657"
	rpcClient, _ := rpcclient.NewHTTPClient(remote)

	client := gnoclient.Client{
		Signer:    signer,
		RPCClient: rpcClient,
	}
	_ = client
}
Output:

Example (WithInMemCrypto)

Example_withInMemCrypto demonstrates how to initialize a gnoclient with an in-memory keybase using BIP39 mnemonics.

package main

import (
	"github.com/gnolang/gno/gno.land/pkg/gnoclient"
	rpcclient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
)

func main() {
	mnemo := "index brass unknown lecture autumn provide royal shrimp elegant wink now zebra discover swarm act ill you bullet entire outdoor tilt usage gap multiply"
	bip39Passphrase := ""
	account := uint32(0)
	index := uint32(0)
	chainID := "dev"
	signer, _ := gnoclient.SignerFromBip39(mnemo, chainID, bip39Passphrase, account, index)

	remote := "127.0.0.1:26657"
	rpcClient, _ := rpcclient.NewHTTPClient(remote)

	client := gnoclient.Client{
		Signer:    signer,
		RPCClient: rpcClient,
	}
	_ = client
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyPackage      = errors.New("empty package to run")
	ErrEmptyPkgPath      = errors.New("empty pkg path")
	ErrEmptyFuncName     = errors.New("empty function name")
	ErrInvalidGasWanted  = errors.New("invalid gas wanted")
	ErrInvalidGasFee     = errors.New("invalid gas fee")
	ErrMissingSigner     = errors.New("missing Signer")
	ErrMissingRPCClient  = errors.New("missing RPCClient")
	ErrInvalidToAddress  = errors.New("invalid send to address")
	ErrInvalidSendAmount = errors.New("invalid send amount")
)
View Source
var ErrInvalidBlockHeight = errors.New("invalid block height provided")

Functions

This section is empty.

Types

type BaseTxCfg

type BaseTxCfg struct {
	GasFee         string // Gas fee
	GasWanted      int64  // Gas wanted
	AccountNumber  uint64 // Account number
	SequenceNumber uint64 // Sequence number
	Memo           string // Memo
}

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

type Client

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

Client provides an interface for interacting with the blockchain.

func (*Client) AddPackage

func (c *Client) AddPackage(cfg BaseTxCfg, msgs ...MsgAddPackage) (*ctypes.ResultBroadcastTxCommit, error)

AddPackage executes one or more AddPackage calls on the blockchain

func (*Client) Block

func (c *Client) Block(height int64) (*ctypes.ResultBlock, error)

Block gets the latest block at height, if any Height must be larger than 0

func (*Client) BlockResult

func (c *Client) BlockResult(height int64) (*ctypes.ResultBlockResults, error)

BlockResult gets the block results at height, if any Height must be larger than 0

func (*Client) Call

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

Call executes one or more MsgCall calls on the blockchain

func (*Client) LatestBlockHeight

func (c *Client) LatestBlockHeight() (int64, error)

LatestBlockHeight gets the latest block height on the chain

func (*Client) QEval

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 "GetBoardIDFromName(\"testboard\")". The return value is a typed expression like "(1 gno.land/r/demo/boards.BoardID)\n(true bool)".

func (*Client) Query

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

Query performs a generic query on the blockchain.

func (*Client) QueryAccount

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

QueryAccount retrieves account information for a given address.

func (*Client) QueryAppVersion

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

QueryAppVersion retrieves information about the app version

func (*Client) Render

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/".

func (*Client) Run

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

Run executes one or more MsgRun calls on the blockchain

func (*Client) Send

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

Send executes one or more MsgSend calls on the blockchain

type MsgAddPackage

type MsgAddPackage struct {
	Package *std.MemPackage // Package to add
	Deposit string          // Coin deposit
}

MsgAddPackage - syntax sugar for vm.MsgAddPackage

type MsgCall

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

MsgCall - syntax sugar for vm.MsgCall

type MsgRun

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

MsgRun - syntax sugar for vm.MsgRun

type MsgSend

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

MsgSend - syntax sugar for bank.MsgSend

type QueryCfg

type QueryCfg struct {
	Path                       string // Query path
	Data                       []byte // Query data
	rpcclient.ABCIQueryOptions        // ABCI query options
}

QueryCfg contains configuration options for performing ABCI queries.

type SignCfg

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

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

type Signer

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

Signer provides an interface for signing transactions.

func SignerFromBip39

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.

Warning: Using keys.NewKeyBaseFromDir to get a keypair from local storage is recommended where possible, as it is more secure.

type SignerFromKeybase

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
}

SignerFromKeybase represents a signer created from a Keybase.

func (SignerFromKeybase) Info

func (s SignerFromKeybase) Info() (keys.Info, error)

Info gets keypair information.

func (SignerFromKeybase) Sign

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

Sign implements the Signer interface for SignerFromKeybase.

func (SignerFromKeybase) Validate

func (s SignerFromKeybase) Validate() error

Validate checks if the signer is properly configured.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL