@trufnetwork/sdk-js v0.2.4
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";
import { Wallet } from "ethers";
// Create a wallet
const wallet = new Wallet("0000000000000000000000000000000000000000000000000000000000000001");
// Initialize client
const client = new NodeTNClient({
endpoint: "https://staging.tsn.truflation.com",
signerInfo: {
address: wallet.address,
signer: wallet, // Any object that implements signMessage
},
chainId: "truflation-staging-2024-11-22", // 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:
- Check our TN SDK Demo Repository
- Try the Live Demo on CodeSandbox
- Try reading from a Truflation Stream on CodeSandbox with NodeJS
- NEW Check out the TN SDK JS Example Directory. It contains examples for stream deployment, stream initialization, data insertion, data retrieval, and stream destruction.
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.
5 months ago
4 months ago
7 months ago
8 months ago
8 months ago
8 months ago
8 months ago