Skip to content

textileio/near-api-go

Repository files navigation

near-api-go

standard-readme compliant

A NEAR client written in Go

The goal of this project is to provide a fully featured NEAR cleint in Go. There is support for most NEAR RPC requests, including those that use signed transactions. Of course, there is room for improvement, especially with integration testing and a fully featured KeyStore, so please give it a spin and feel free to open a PR to help us improve the library.

We're currently relying on our fork of go-ethereum's JSON RPC client that adds support for named RPC parameters. That work is pending PR merge into their master branch.

Table of Contents

Install

go get github.com/textileio/near-api-go

Usage

Import the required modules.

import (
  api "github.com/textileio/near-api-go"
  "github.com/textileio/near-api-go/keys"
  "github.com/textileio/near-api-go/transaction"
  "github.com/textileio/near-api-go/types"
  "github.com/ethereum/go-ethereum/rpc"
)

Configure and create an API client.

rpcClient, err := rpc.DialContext(ctx, "https://rpc.testnet.near.org")

keyPair, err := keys.NewKeyPairFromString(
  "ed25519:...",
)

config := &types.Config{
  RPCClient: rpcClient,
  Signer:    keyPair, // Currently we use a key pair directly as a signer.
  NetworkID: "testnet",
}

client, err := api.NewClient(config)

Interact with top level functions like CallFunction, for example. It can be used for calling non-signed "view" functions.

res, err := client.CallFunction(
  ctx,
  "<account id>",
  "myFunction",
  api.CallFunctionWithFinality("final"),
)

Most other functionality is provided by the Account sub module. For example, you can call state-modifying functions that are sent as signed transactions, and even include a deposit while you're at it.

deposit, ok := (&big.Int{}).SetString("1000000000000000000000000", 10)
res, err := client.Account("<client account id>").FunctionCall(
  ctx,
  <contract account id>,
  "myTxnFunction",
  transaction.FunctionCallWithArgs(map[string]interface{}{
    "arg1": value1, 
    "arg2": value2
  }),
  transaction.FunctionCallWithDeposit(*deposit),
)

Check out the API docs to see all that is possible.

API

https://pkg.go.dev/github.com/textileio/near-api-go

Maintainers

@asutula

Contributing

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2021 Textile