eteu-technologies / near-api-go

Go library to interact with NEAR Protocol via RPC API (https://docs.near.org/docs/api/rpc)
MIT License
16 stars 13 forks source link
blockchain go golang near-api-go near-protocol rpc-client

near-api-go

Go Reference CI

WARNING: This library is still work in progress. While it covers about 90% of the use-cases, it does not have many batteries included.

Usage

go get github.com/eteu-technologies/near-api-go

Notes

What this library does not (and probably won't) provide:

What this library doesn't have yet:

Examples

See cmd/ in this repo for more fully featured CLI examples. See NEARKit for a project which makes use of this API.

Query latest block on NEAR testnet

package main

import (
    "context"
    "fmt"

    "github.com/eteu-technologies/near-api-go/pkg/client"
    "github.com/eteu-technologies/near-api-go/pkg/client/block"
)

func main() {
    rpc, err := client.NewClient("https://rpc.testnet.near.org")
    if err != nil {
        panic(err)
    }

    ctx := context.Background()

    res, err := rpc.BlockDetails(ctx, block.FinalityFinal())
    if err != nil {
        panic(err)
    }

    fmt.Println("latest block: ", res.Header.Hash)
}

Transfer 1 NEAR token between accounts

package main

import (
    "context"
    "fmt"

    "github.com/eteu-technologies/near-api-go/pkg/client"
    "github.com/eteu-technologies/near-api-go/pkg/types"
    "github.com/eteu-technologies/near-api-go/pkg/types/action"
    "github.com/eteu-technologies/near-api-go/pkg/types/key"
)

var (
    sender    = "mikroskeem.testnet"
    recipient = "mikroskeem2.testnet"

    senderPrivateKey = `ed25519:...`
)

func main() {
    rpc, err := client.NewClient("https://rpc.testnet.near.org")
    if err != nil {
        panic(err)
    }

    keyPair, err := key.NewBase58KeyPair(senderPrivateKey)
    if err != nil {
        panic(err)
    }

    ctx := client.ContextWithKeyPair(context.Background(), keyPair)
    res, err := rpc.TransactionSendAwait(ctx, sender, recipient, []action.Action{
        action.NewTransfer(types.NEARToYocto(1)),
    })
    if err != nil {
        panic(err)
    }

    fmt.Printf("https://rpc.testnet.near.org/transactions/%s\n", res.Transaction.Hash)
}

License

MIT