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

@deltaswapio/relayer-engine

v0.3.2

Published

Relayer Engine

Downloads

2

Readme

Relayer Engine

The Relayer Engine is a package meant to provide the structure and a starting point for a custom relayer.

With the Relayer Engine, a developer can write specific logic for filtering to receive only the messages they care about.

Once a deltaswap message is received, the developer may apply additional logic to parse custom payloads or submit the VAA to one or many destination chains.

To use the Relayer engine, a developer may specify how to relay deltaswap messages for their app using an idiomatic express/koa middleware inspired api then let the library handle all the details!

Checkout the quick start example here, or for a more advanced relayer app, see the advanced example

Quick Start

The source for this example is available here

Install Package

First, install the relayer-engine package with your favorite package manager

npm i @deltaswapio/relayer-engine

Start Background Processes

note: These processes must be running in order for the relayer app below to work

Next, we must start a Spy to listen for available VAAs published on the phylax network as well as a persistence layer, in this case we're using Redis.

More details about the Spy are available in the docs

Deltaswap Network Spy

In order for our Relayer app to receive messages, a local Spy must be running that watches the phylax network. Our relayer app will receive updates from this Spy.

docker run --platform=linux/amd64 \
-p 7073:7073 \
--entrypoint /phylaxd ghcr.io/deltaswapio/phylaxd:latest \
spy \
--nodeKey /node.key \
--spyRPC "[::]:7073" \
--network /deltaswap/testnet/2/1 \
--bootstrap /dns4/deltaswap-testnet-v2-bootstrap.certus.one/udp/8999/quic/p2p/12D3KooWAkB9ynDur1Jtoa97LBUp8RXdhzS5uHgAfdTquJbrbN7i
docker run --platform=linux/amd64 \
-p 7073:7073 \
--entrypoint /phylaxd ghcr.io/deltaswapio/phylaxd:latest \
spy \
--nodeKey /node.key \
--spyRPC "[::]:7073" \
--network /deltaswap/mainnet/2 \
--bootstrap /dns4/deltaswap-mainnet-v2-bootstrap.certus.one/udp/8999/quic/p2p/12D3KooWQp644DK27fd3d4Km3jr7gHiuJJ5ZGmy8hH4py7fP4FP7

Redis Persistence

Note: While we're using Redis here, the persistence layer can be swapped out for some other db by implementing the appropriate interface.

A Redis instance must also be available to persist job data for fetching VAAs from the Spy.

docker run --rm -p 6379:6379 --name redis-docker -d redis

Simple Relayer Code Example

In the following example, we'll:

  1. Set up a StandardRelayerApp, passing configuration options for our Relayer
  2. Add a filter to capture only those messages our app cares about with a callback to do something with the VAA once we've gotten it
  3. Start the Relayer app
import {
  Environment,
  StandardRelayerApp,
  StandardRelayerContext,
} from "@deltaswapio/relayer-engine";
import { CHAIN_ID_SOLANA } from "@deltaswapio/deltaswap-sdk";

(async function main() {
  // initialize relayer engine app, pass relevant config options
  const app = new StandardRelayerApp<StandardRelayerContext>(
    Environment.TESTNET,
    // other app specific config options can be set here for things
    // like retries, logger, or redis connection settings.
    {
      name: "ExampleRelayer",
    },
  );

  // add a filter with a callback that will be
  // invoked on finding a VAA that matches the filter
  app.chain(CHAIN_ID_SOLANA).address(
    // emitter address on Solana
    "DZnkkTmCiFWfYTfT41X3Rd1kDgozqzxWaHqsw6W4x2oe",
    // callback function to invoke on new message
    async (ctx, next) => {
      const vaa = ctx.vaa;
      const hash = ctx.sourceTxHash;
      console.log(
        `Got a VAA with sequence: ${vaa.sequence} from with txhash: ${hash}`,
      );
    },
  );

  // add and configure any other middleware ..

  // start app, blocks until unrecoverable error or process is stopped
  await app.listen();
})();

Explanation

The first meaningful line instantiates the StandardRelayerApp, which is a subclass of the RelayerApp with common defaults.

export class StandardRelayerApp<
  ContextT extends StandardRelayerContext = StandardRelayerContext,
> extends RelayerApp<ContextT> {
  // ...
  constructor(env: Environment, opts: StandardRelayerAppOpts) {

The only field we pass in the StandardRelayerAppOpts is the name to help with identifying log messages and reserve a namespace in Redis.

  deltaswapRpcs?: string[];  // List of URLs from which to query missed VAAs
  concurrency?: number;     // How many concurrent requests to make for workflows
  spyEndpoint?: string;     // The hostname and port of our Spy
  logger?: Logger;          // A custom Logger
  privateKeys?: Partial<{ [k in ChainId]: any[]; }>; // A set of keys that can be used to sign and send transactions
  tokensByChain?: TokensByChain;    // The token list we care about
  workflows?: { retries: number; }; // How many times to retry a given workflow
  providers?: ProvidersOpts;        // Configuration for the default providers
  fetchSourceTxhash?: boolean;      // whether or not to get the original transaction id/hash
  // Redis config
  redisClusterEndpoints?: ClusterNode[];
  redisCluster?: ClusterOptions;
  redis?: RedisOptions;

The next meaningful line in the example adds a filter middleware component. This middleware will cause the Relayer app to request a subscription from the Spy for any VAAs that match the criteria and invoke the callback with the VAA.

If you'd like your program to subscribe to multiple chains and addresses, the same method can be called several times or the multiple helper can be used.

app.multiple(
  {
    [CHAIN_ID_SOLANA]: "DZnkkTmCiFWfYTfT41X3Rd1kDgozqzxWaHqsw6W4x2oe"
    [CHAIN_ID_ETH]: ["0xabc1230000000...","0xdef456000....."]
  },
  myCallback
);

The last line in the simple example runs await app.listen(), which will start the relayer engine. Once started, the relayer engine will issue subscription requests to the spy and begin any other workflows (e.g. tracking missed VAAs).

This will run until the process is killed or it encounters an unrecoverable error. If you'd like to shut down the relayer gracefully, call app.stop().

Advanced Example

For a more advanced example that details other middleware and more complex configuration and actions including a built in UI, see the Advanced Tutorial