npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pragmaoracle/theoros-sdk

v1.0.0

Published

The official SDK for Pragma Theoros

Downloads

71

Readme

🧩 Theoros SDK

The official TypeScript SDK for interacting with the Pragma Theoros API. The Theoros SDK simplifies the process of:

  • Fetching calldata for given feed IDs.
  • Subscribing to real-time data feed updates via WebSocket.

Installation

npm install @pragmaoracle/theoros-sdk

Introduction

The Theoros SDK provides a convenient way to interact with the Pragma Theoros API. It allows developers to:

  • Fetch available data feeds and their details.
  • Retrieve the list of supported chains.
  • Fetch calldata for specific feeds on a given chain.
  • Subscribe to real-time updates for data feeds over WebSockets.

Getting Started

Initializing the SDK

Import the SDK and create an instance:

import { TheorosSDK } from "@pragmaoracle/theoros-sdk";

const sdk = new TheorosSDK({
  baseUrl: "https://api.pragma.build/v1", // Optional, defaults to this value
  timeout: 10000, // Optional, in milliseconds
});
  • baseUrl (optional): The base URL of the Pragma Theoros API. Defaults to 'https://api.pragma.build/v1'.
  • timeout (optional): The request timeout in milliseconds. Defaults to 10000.

Usage

Fetching Available Feeds

Retrieve the list of available feeds:

const feeds = await sdk.getAvailableFeeds();
console.log("Available Feeds:", feeds);

This method returns a promise that resolves to an array of Feed objects, each containing:

  • feed_id: The unique identifier for the feed.
  • asset_class: The asset class of the feed.
  • feed_type: The type of the feed.
  • pair_id: The pair identifier associated with the feed.

Fetching Supported Chains

Retrieve the list of supported chains:

const chains = await sdk.getSupportedChains();
console.log("Supported Chains:", chains);

This method returns a promise that resolves to an array of strings representing the chain names.

Fetching Calldata

Fetch calldata for specific feed IDs on a given chain:

const chain = "zircuit_testnet";
const feedIds = ["0x4e5354522f555344", "0x4c5553442f555344"];

try {
  const calldataResponses = await sdk.getCalldata(chain, feedIds);
  console.log("Calldata Responses:", calldataResponses);
} catch (error) {
  console.error("Error fetching calldata:", error);
}
  • chain: The name of the chain.
  • feedIds: An array of feed IDs.

The method returns a promise that resolves to an array of CalldataResponse objects, each containing:

  • feed_id: The feed ID.
  • encoded_calldata: The calldata encoded as a hex string.

Subscribing to Data Feeds

Subscribe to data feed updates over WebSocket:

const chain = "zircuit_testnet";
const feedIds = ["0x4e5354522f555344", "0x4c5553442f555344"];

const subscription = sdk.subscribe(chain, feedIds);

Handling Updates

Listen for updates and other events:

subscription.on("update", (dataFeeds) => {
  console.log("Data Feed Update:", dataFeeds);
});

subscription.on("error", (error) => {
  console.error("Subscription Error:", error);
});

subscription.on("close", () => {
  console.log("Subscription Closed");
});
  • 'update': Emitted when new data feed updates are received. The callback receives an array of RpcDataFeed objects.
  • 'error': Emitted when an error occurs. The callback receives an error object.
  • 'close': Emitted when the subscription is closed.

Adding and Removing Feed IDs

You can dynamically add or remove feed IDs from the subscription:

// Add new feed IDs
subscription.addFeedIds(["0x4e5354522f555344"]);

// Remove feed IDs
subscription.removeFeedIds(["0x4e5354522f555344"]);

Unsubscribing

To unsubscribe from all feeds and close the connection:

subscription.unsubscribe();

Example

Here's a complete example demonstrating how to use the SDK:

import {
  TheorosSDK,
  type Feed,
  type RpcDataFeed,
  type TheorosSDKError,
} from "@pragmaoracle/theoros-sdk";

const sdk = new TheorosSDK({
  baseUrl: "http://localhost:3000/v1", // Local Theoros instance
});

try {
  // Fetch available feeds
  const feeds = await sdk.getAvailableFeeds();
  console.log("📜 Available Feeds:", feeds);

  // Fetch supported chains
  const chains = await sdk.getSupportedChains();
  console.log("⛓️‍💥 Supported Chains:", chains);

  // Choose a chain and feed IDs
  const chain = chains[0];
  const feedIds = feeds.slice(0, 2).map((feed: Feed) => feed.feed_id);

  // Fetch calldata
  const calldataResponses = await sdk.getCalldata(chain, feedIds);
  console.log("👉 Calldata Responses:", calldataResponses);

  // Subscribe to data feed updates
  const subscription = sdk.subscribe(chain, feedIds);

  subscription.on("update", (dataFeeds: RpcDataFeed[]) => {
    console.log("👉 Data Feed Update:", dataFeeds);
  });

  subscription.on("error", (error: TheorosSDKError) => {
    console.error("😱😱 Subscription Error:", error);
  });

  // Add a new feed ID after some time
  setTimeout(() => {
    subscription.addFeedIds(["0x574254432f555344"]);
  }, 5000);

  // Unsubscribe after some time
  setTimeout(() => {
    subscription.unsubscribe();
  }, 15000);
} catch (error) {
  console.error("An error occurred:", error);
}

License

This project is licensed under the MIT License.