Initializing a Nexus Server

Nexus is the entrypoint for your proxy. It can be configured in many different ways, on many different platforms. Let’s take a look at the anatomy of a simple Nexus server instance.

import {
  Nexus,
  NodeProvider,
  CHAIN,
} from "@whatsgood/nexus";

const llamaRpcNodeProvider = new NodeProvider({
  name: "llama-rpc",
  chain: CHAIN.ETHEREUM_MAINNET,
  url: "https://eth.llamarpc.com",
});

const tenderlyNodeProvider = new NodeProvider({
  name: "tenderly",
  chain: CHAIN.ETHEREUM_MAINNET,
  url: "https://gateway.tenderly.co/public/mainnet",
});

const nexus = Nexus.create({
  nodeProviders: [llamaRpcNodeProvider, tenderlyNodeProvider],
  port: 4000,
  log: { level: "debug" },
  relay: {
    failure: {
      kind: "cycle-requests",
      maxAttempts: 3,
    },
    order: "random",
  },
});

In this example, we first create two NodeProvider instances, which we’ll use to connect to Ethereum Mainnet via Alchemy and Infura. We then create a Nexus instance, passing in the NodeProvider instances, the port we want to run the server on, and some logging and relay configuration. In this particular example, we configure the relay to retry failed requests up to 3 times, and to randomize the order of requests.

Running a Nexus Server

Nexus servers are designed to run on many different platforms. See Integrations for more information on how to run Nexus on your platform of choice. In this example, let’s set up a NodeJs standalone server.

// ... continue from the previous example

import { createServer } from "node:http";

createServer(nexus).listen(nexus.port, () => {
  console.log(`🚀 Server ready at http://localhost:${nexus.port}`);
});

Interacting with a Nexus Server

Once you have a Nexus server running, you can interact with is using any JSON-RPC client. Here’s an example using curl. Pay attention to the endpoint. Nexus servers are designed to handle multiple chains, so you need to specify the chain ID in the endpoint. In this example, since we have configured the server to connect to Ethereum Mainnet, we use the chain ID 1.

curl -X POST http://localhost:4000/1 \
     -H "Content-Type: application/json" \
     -d '{
           "jsonrpc": "2.0", 
           "method": "eth_blockNumber", 
           "params": [], 
           "id": 1
         }'