The TRUF.network offers SDKs in Go and TypeScript/JavaScript to facilitate seamless integration with its decentralized platform for economic data streams.


What is a streamID?

A streamID is an identifier used in the TRUF.network (TN) to identify the deployed contract. It is a unique string generated from a descriptive name, such as an English name, to ensure easy reference and management of data streams.


Go SDK

The Go SDK provides tools to publish, compose, and consume economic data streams within the TRUF.network. It supports operations on primitive streams and composed streams.

Installation:

Ensure you have Go 1.20 or later installed. To install the SDK, run:

go get github.com/trufnetwork/sdk-go

Example Usage:

package main



import (

    "context"

    "fmt"

    "github.com/golang-sql/civil"

    "github.com/kwilteam/kwil-db/core/crypto"

    "github.com/kwilteam/kwil-db/core/crypto/auth"

    "github.com/trufnetwork/sdk-go/core/tsnclient"

    "github.com/trufnetwork/sdk-go/core/types"

    "github.com/trufnetwork/sdk-go/core/util"

)



func main() {

    ctx := context.Background()



    // Create TSN client

    pk, _ := crypto.Secp256k1PrivateKeyFromHex("<your-private-key-hex>")

    signer := &auth.EthPersonalSigner{Key: *pk}

    tsnClient, err := tsnclient.NewClient(ctx, "https://staging.tsn.truflation.com", tsnclient.WithSigner(signer))

    if err != nil {

        panic(err)

    }



    // Load an existing stream

    streamId := util.GenerateStreamId("your-stream-name")

    streamLocator := tsnClient.OwnStreamLocator(streamId)

    stream, err := tsnClient.LoadPrimitiveStream(streamLocator)

    if err != nil {

        panic(err)

    }



    // Read data from the stream

    dateFrom, _ := civil.ParseDate("2023-01-01")

    dateTo, _ := civil.ParseDate("2023-01-31")

    records, err := stream.GetRecord(ctx, types.GetRecordInput{

        DateFrom: &dateFrom,

        DateTo:   &dateTo,

    })

    if err != nil {

        panic(err)

    }



    for _, record := range records {

        fmt.Println(record.DateValue, record.Value.String())

    }

}

For comprehensive examples and usage patterns, refer to the test files in the SDK repository.


TypeScript/JavaScript SDK

The TypeScript/JavaScript SDK offers similar capabilities as the Go SDK, with implementations tailored for both Node.js and browser environments.

Installation:

Ensure you have Node.js 18 or later installed. To install the SDK, run:

npm install @trufnetwork/sdk-js

Example Usage:

import { NodeTNClient, StreamId } from "@trufnetwork/sdk-js";



// Create a wallet

const wallet = new Wallet(

    "<your-private-key-hex>"

);



// Initialize client

const client = new NodeTNClient({

	endpoint: "https://staging.tsn.truflation.com",

    signerInfo: {

        address: wallet.address,

        signer: wallet

    },

    chainId: "truflation-staging-2024-11-22" // or use NodeTNClient.getDefaultChainId()

});



// Load an existing stream

const streamId = StreamId.fromString("your-stream-name");

const stream = client.loadPrimitiveStream(streamId);



// Read data from the stream

const records = await stream.getRecord({

	dateFrom: "2023-01-01",

	dateTo: "2023-01-31"

});



records.forEach((record) => {

	console.log(record.dateValue, record.value);

});

For detailed examples and usage patterns, refer to the test files and examples in the SDK repository.


Timestamp support

In the contract version 2, we have added support for timestamps. Replacing the Date type with the number type in the GetRecordInput and Record types.

interface GetRecordInput {

    dateFrom: number;

    dateTo: number;

}



interface Record {

    dateValue: number;

    value: string;

}



// Read data from the stream with timestamps

const records = await stream.getRecord({

	dateFrom: 1672531200, // 2023-01-01

	dateTo: 1675123200, // 2023-01-31

});

For more information about the timestamp interface, feel free to navigate to the SDK repository. Most of the functions and interfaces are the same as the previous version, but with date values replaced with timestamps. For example, to deploy a new stream with timestamps, you can use the deployPrimitiveStream function with the contractVersion parameter set to 2.

Additional Resources

For support or questions, please open an issue in the respective SDK repository or contact our support team.