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

@tensor-hq/smart-rpc

v1.1.3

Published

Intelligent transport layer for Solana RPCs.

Downloads

216

Readme

Solana Smart RPC

Intelligent transport layer for Solana RPCs that handles load balancing, rate limit throttling, failover, fanouts, and retries. Built on top of @solana/web3.js.

smart rpc metrics dashboard

Getting Started

Smart RPC is a proxy to the existing Connection class, so migrating should be very simple. Under the hood, when you make requests, it proxies to underlying logic that determines which transport to send your request to.

import { TransportManager, TransportConfig } from "@tensor-hq/smart-rpc";

let defaultTransportConfig: TransportConfig[] = [
  {
    rateLimit: 50,
    weight: 50,
    blacklist: [],
    id: "Triton",
    url: TRITON_MAINNET_P0,
    enableSmartDisable: true,
    enableFailover: true,
    maxRetries: 2,
  },
  {
    rateLimit: 50,
    weight: 50,
    blacklist: [],
    id: "Helius",
    url: HELIUS_FE_MAINNET_P0,
    enableSmartDisable: true,
    enableFailover: true,
    maxRetries: 2,
  },
];

const smartRpcMetricLogger: MetricCallback = (_, metricValue) => {
  console.log(metricValue);
};

let optionalConfig: TransportManagerConfig = {
  strictPriorityMode: true, // if set, will always try highest weighted provider first instead of load balancing
  metricCallback: smartRpcMetricLogger, // callback function for successful and failed requests
  queueSize: 1000, // configurable queue size for underlying rate limit queue
  timeoutMs: 5000, // configurable timeout for underlying requests
};

const transportManager = new TransportManager(defaultTransportConfig, optionalConfig);
export const smartConnection = transportManager.smartConnection;

const resp = await smartConnection.getLatestBlockhash();
console.log(resp.blockhash.toString());

Connection Types

Smart connection

This is the recommended connection for most cases. It's a drop-in replacement for the existing Connection class within built-in features such as load balancing, failover, and retries.

Fanout connection

Sometimes you might want to send the same request to multiple connections. One example is getLatestBlockhash, since some RPC providers may occasionally lag behind others. When signing transacitons, fetching the most up-to-date blockhash across providers helps increase the chance your transaction will succeed. Another example is sendTransaction. You may want to send your transaction to multiple providers to get the fastest confirmation and increase the chance of success.

Race connection

Assuming most providers are up-to-date, you may want to race among them to get the fastest response. For example, you could send getAccountInfo to multiple providers and abort after the fastest provider returns a response.

Features

Load Balancing

By default, Smart RPC will load balance based on the provided weights. You can configure these weights and update them at runtime (e.g. if you need a killswitch for an RPC provider).

In-memory and Redis Rate Limiting

Smart RPC supports in-memory and pooled rate limiting. By default, rate limting will happen in-memory. If you want to pool rate limits, you can pass in an optional IORedisClient (either a single instance or cluster).

Retries & Failover

If a particular transport has filled up its rate limit queue or encounters an error, it will automatically failover to the next transport. This makes your requests more likely to succeed if there are transient issues.

Smart Disable

If a particular transport reaches an error threshold within a short period of time, Smart RPC will shut off requests to that transport for a short cooloff period. This can happen if you hit rate limits or if the RPC provider is having issues. Instead of spamming them, requests will be sent to a different provider.

Known Limitations

Smart RPC doesn't support subscription endpoints, such as onSlotUpdate. Smart RPC is a proxy to underlying connections, and doesn't maintain long-lived connections. We recommend using the Connection class directly for subscription endpoints.