Skip to content

Latest commit

 

History

History
78 lines (58 loc) · 1.79 KB

getting-started.md

File metadata and controls

78 lines (58 loc) · 1.79 KB

Getting Started with TN SDK JS

Prerequisites

  • Node.js 18 or later

Installation

npm install @trufnetwork/sdk-js
# or
pnpm install @trufnetwork/sdk-js
# or 
yarn add @trufnetwork/sdk-js

Quick Start

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

// Initialize client
const wallet = new Wallet(privateKey);
const chainId = await NodeTNClient.getDefaultChainId("https://staging.tsn.truflation.com");

const client = new NodeTNClient({
  endpoint: "https://staging.tsn.truflation.com",
  signerInfo: {
    address: wallet.address,
    signer: wallet, // Must implement signMessage (e.g. ethers Wallet)
  },
  chainId,
});

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

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

await stream.initializeStream();

// Insert data
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"
});

Environment-Specific Usage

The SDK provides optimized clients for different environments:

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

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

Next Steps

Support

For support and issues, please visit our GitHub repository.