Skip to content

orbs-network/orbs-client-sdk-go

Repository files navigation

Orbs Client SDK Go

Client SDK for the Orbs blockchain in golang

Usage

  1. Create a few end user accounts:

    sender, err := orbs.CreateAccount()
    receiver, err := orbs.CreateAccount()
  2. Create a client instance:

    const virtualChainId = 42
    client := orbs.NewClient("http://node-endpoint.com", virtualChainId, codec.NETWORK_TYPE_TEST_NET)
  3. Send a transaction:

    tx, txId, err := client.CreateTransaction(
        sender.PublicKey,
        sender.PrivateKey,
        "BenchmarkToken",
        "transfer",
        uint64(10), receiver.AddressAsBytes())
    response, err := client.SendTransaction(tx)
  4. Check the transaction status:

    response, err := client.GetTransactionStatus(txId)
  5. Call a smart contract method:

    query, err = client.CreateQuery(
        receiver.PublicKey,
        "BenchmarkToken",
        "getBalance",
        receiver.AddressAsBytes())
    response, err := client.SendQuery(query)

Installation

  1. Make sure Go is installed (version 1.10 or later).

    Verify with go version

  2. Get the library into your Go workspace:

    go get -u github.com/orbs-network/orbs-client-sdk-go/...
  3. Import the client in your project:

    import "github.com/orbs-network/orbs-client-sdk-go/orbs" 

Test

  1. Run end to end test together with Gamma server:

    go test ./test/e2e
  2. Run all tests:

    go test ./...
  3. Run codec contract test (generate output.json as this is the reference implementation):

    cd ./test/codec/
    go run main.go

Alternative Client SDK Implementations

Creating alternative client SDK implementations is highly encouraged, particularly in languages not supported by the core team. To make sure your client implementation is compliant to the Orbs protocol specificiations, we've created a set of compliance tests.

Codec contract test

The codec is the part that encodes and decodes protocol messages. The contract is created by the reference implementation (this repo) and embodied into an input JSON file and an output JSON file.

To run the contract test in your own implementation, encode/decode the messages according to input.json and compare your results to output.json. You can generate the JSON files from code by running

cd ./test/codec/
go run main.go

End to end test with Gamma server

Gamma server is a local development server for the Orbs network that can run on your own machine and be used for testing. This server can process smart contract deployments and transactions. The server is accessed via HTTP (just like a regular node) which makes it excellent for testing clients.

Take a look at the example e2e test implemented in this repo.

Example compliant implementation

The JavaScript alternative implementation is a great source for inspiration. It contains both a codec contract test and an end-to-end test.