Skip to content

trufnetwork/sdk-js

Repository files navigation

TN SDK JS

The TN SDK provides developers with tools to interact with the Truf Network, a decentralized platform for publishing, composing, and consuming economic data streams.

Quick Start

Prerequisites

  • Node.js 18 or later

Installation

npm install @trufnetwork/sdk-js
# or your preferred package manager

Environment-specific Usage

// For Node.js applications
import { NodeTNClient } from "@trufnetwork/sdk-js";

// For browser applications
import { BrowserTNClient } from "@trufnetwork/sdk-js";

Example Usage

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

// Initialize client
const client = new NodeTNClient({
  endpoint: "https://staging.tsn.truflation.com",
  signerInfo: {
    address: wallet.address,
    signer: wallet, // Any object that implements signMessage
  },
  chainId: "tsn-1", // or use NodeTNClient.getDefaultChainId()
});

// Deploy and initialize a stream
const streamId = await StreamId.generate("my-data-stream");
await client.deployStream(streamId, "primitive", true);

const stream = client.loadPrimitiveStream({
  streamId,
  dataProvider: client.address(),
});

// here simplified, you might need to wait for the tx using client.waitForTx
await stream.initializeStream();

// Insert data, simplified
await stream.insertRecords([
  { dateValue: "2024-01-01", value: "100.5" }
]);

// Read data
const data = await stream.getRecord({
  dateFrom: "2024-01-01",
  dateTo: "2024-01-01",
});

For a complete working example:

Stream Types

TN supports two main types of streams:

  • Primitive Streams: Direct data sources from providers
  • Composed Streams: Aggregate data from multiple streams using weights

More information about TN components can be found in the Js TN-SDK Documentation.

Documentation

Staging Network

A staging network is available at https://staging.tsn.truflation.com for testing and experimentation.

Running with Deno

This package works with Deno when using the --allow-net permission flag:

import { ... } from "npm:@trufnetwork/sdk-js"

Deno Environment Permissions

By default, some dependencies requires environment permissions. If you need to run without environment permissions, please see this GitHub issue for workarounds.

Support

For support, please open an issue.

License

Licensed under the Apache License, Version 2.0. See LICENSE for details.