Skip to content

attestantio/go-near-client

Repository files navigation

go-near-client

Tag License GoDoc Lint Go Report Card

Go library providing an abstraction to NEAR Protocol nodes.

This library is under development; expect APIs and data structures to change until it reaches version 1.0. In addition, clients' implementations of both their own and the standard API are themselves under development so implementation of the full API can be incomplete.

Table of Contents

Install

go-near-client is a standard Go module which can be installed with:

go get github.com/attestantio/go-near-client

Support

go-near-client supports NEAR nodes that comply with the standard NEAR JSON-RPC API.

Usage

Please read the Go documentation for this library for interface information.

Example

Below is a complete annotated example to access a NEAR node.

package main

import (
    "context"
    "fmt"
    "time"

    nearclient "github.com/attestantio/go-near-client"
    "github.com/attestantio/go-near-client/api"
    "github.com/attestantio/go-near-client/jsonrpc"
    "github.com/rs/zerolog"
)

func main() {
    // Provide a cancellable context to the creation function.
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    client, err := jsonrpc.New(ctx,
        // WithAddress supplies the address of the NEAR node, as a URL.
        jsonrpc.WithAddress("https://rpc.mainnet.near.org"),
        // WithTimeout sets the maximum duration for all requests.
        jsonrpc.WithTimeout(time.Second*600),
        // WithLogLevel supplies the level of logging to carry out.
        jsonrpc.WithLogLevel(zerolog.DebugLevel),
        // WithAllowDelayedStart allows the service to start even if the client is unavailable.
        jsonrpc.WithAllowDelayedStart(true),
    )
    if err != nil {
        panic(err)
    }

    fmt.Printf("Connected to %s\n", client.Name())

    // Query account information
    opts := api.AccountOpts{
        AccountID:  "near",
        ContractID: "near",
    }
    acc, err := client.Account(ctx, &opts)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Account balance: %s\n", acc.Data.StakedBalance.PrintNEAR())

    // Query latest block
    blockOpts := api.BlockOpts{}
    block, err := client.Block(ctx, &blockOpts)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Latest block height: %d\n", block.Data.Header.Height)

    // Check node status
    status, err := client.Status(ctx, &api.StatusOpts{})
    if err != nil {
        panic(err)
    }
    fmt.Printf("Node version: %s\n", status.Data.Version.Version)

    // Cancelling the context passed to New() frees up resources held by the
    // client, closes connections, clears handlers, etc.
    cancel()
}

Maintainers

Chris Berry: @bez625. Ahmed Mohamed: @ahmohamed.

Contribute

Contributions welcome. Please check out the issues.

License

Apache-2.0 © 2025 Attestant Limited

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages