8. Connect engine network providers.

Loading Engine Provider

The loadEngineProvider function is designed to initialize a provider for the specified test network within a TypeScript application. By default, it utilizes a local development provider but offers flexibility to switch to custom URLs for external providers.

Parameters:

  • providerUrl: Optional. The provider URL to be used. Defaults to http://127.0.0.1:8600.

Functionality

  1. Configuration: The function constructs a FallbackProviderJsonConfig, setting up the network with a default or custom provider URL.

Usage

Use this function to load a network provider by adapting to local or external resources with ease:

// Load with the default local provider
await loadEngineProvider();

// Load with a specified provider URL
await loadEngineProvider("http://my-custom-provider:8600");

The getProviderInfo function aids by structuring the provider's necessary details like priority and performance metrics, ensuring seamless provider management.

import {
  FallbackProviderJsonConfig,
  NETWORK_CONFIG,
  NetworkName,
} from "@railgun-community/shared-models";
import { loadProvider } from "@railgun-community/wallet";
import { TEST_NETWORK } from "./utils/0_constants";

// forked function, replace with your own provider
/**
 * Loads an Engine provider with the specified URL.
 *
 * This function initializes a provider for the test network using either:
 * 1. A local development provider (default: http://127.0.0.1:8600)
 * 2. External providers that can be uncommented and configured
 *
 * The provider is configured with fallback options, priorities, and weights
 * to ensure reliable connectivity.
 *
 * @param providerUrl - The URL of the primary provider, defaults to "http://127.0.0.1:8600"
 * @returns A Promise that resolves when the provider is successfully loaded
 *
 * @example
 * // Load with default local provider
 * await loadEngineProvider();
 *
 * // Load with custom provider URL
 * await loadEngineProvider("http://my-custom-provider:8600");
 */
export const loadEngineProvider = async (
  providerUrl = "http://127.0.0.1:8600"
) => {
  const TEST_PROVIDERS_JSON: FallbackProviderJsonConfig = {
    chainId: NETWORK_CONFIG[TEST_NETWORK].chain.id,
    providers: [
      // The following is an example of how to use a forked setup

      getProviderInfo(providerUrl),
      // These should be uncommented and filled in with your preferred providers.
      // The above is for using FORK only, do not mix them.
      //   {
      //     provider: "https://cloudflare-eth.com/",
      //     priority: 3,
      //     weight: 2,
      //     maxLogsPerBatch: 1,
      //   },
      //   {
      //     provider: "https://rpc.ankr.com/eth",
      //     priority: 2,
      //     weight: 2,
      //     maxLogsPerBatch: 1,
      //   },
    ],
  };

  const pollingInterval = 1000 * 60 * 5; // 5 min

  await loadProvider(TEST_PROVIDERS_JSON, TEST_NETWORK, pollingInterval);
};

export const getProviderInfo = (providerUrl: string) => {
  return {
    provider: providerUrl,
    priority: 3,
    weight: 2,
    maxLogsPerBatch: 1,
  };
};

Last updated