Skip to content

Latest commit

 

History

History
85 lines (60 loc) · 2.37 KB

core-concepts.md

File metadata and controls

85 lines (60 loc) · 2.37 KB

Core Concepts

Stream Types

TN supports two types of streams:

  • Primitive Streams: Single data source streams that store raw values
  • Composed Streams: Aggregate multiple streams with configurable weights
import { StreamType } from "@trufnetwork/sdk-js";

// Available stream types
StreamType.Primitive  // Single data source
StreamType.Composed   // Aggregates multiple streams with weights

For detailed information about stream types and their use cases, see the Js SDK Documentation.

Error Handling

The SDK uses Either monad from monads-io for parsing addresses and stream IDs:

// Safe address parsing
const addressResult = EthereumAddress.fromString("0x123...")
  .mapRight(addr => addr.getAddress())
  .mapLeft(error => handleError(error));

// Safe stream ID parsing  
const streamIdResult = StreamId.fromString("st123...")
  .mapRight(id => id.getId())
  .mapLeft(error => handleError(error));

Stream Identification

Each stream is identified by two components:

interface StreamLocator {
  streamId: StreamId;        // Unique stream identifier
  dataProvider: EthereumAddress;  // Stream owner's address
}

// Generate a deterministic stream ID
const streamId = await StreamId.generate("my-unique-name");

Permissions Model

Streams support granular permissions for both reading and composing:

import { visibility } from "@trufnetwork/sdk-js";

// Set visibility
await stream.setReadVisibility(visibility.private);
await stream.setComposeVisibility(visibility.public);

// Grant specific permissions
await stream.allowReadWallet(new EthereumAddress("0x..."));
await stream.allowComposeStream(otherStreamLocator);

Transaction Handling

The SDK provides transaction monitoring with configurable timeouts:

// Wait for transaction with custom timeout
await client.waitForTx(txHash, 15000); // 15 second timeout

// Default handling
const tx = await stream.initializeStream();
if (tx.data?.tx_hash) {
  await client.waitForTx(tx.data.tx_hash);
}

Further Reading