MetaRouter TypeScript SDK

The MetaRouter TypeScript SDK provides a powerful and flexible interface for cross-chain token bridging and swaps. It leverages Caldera’s Metalayer and the MetaRouter API to offer seamless interoperability across multiple blockchain networks.

Features

  • Cross-Chain Swaps and Bridges: Effortlessly swap tokens across different blockchain networks.
  • Real-Time Price Quotes: Get accurate and up-to-date price quotes and gas estimates.
  • WebSocket Streaming: Stream real-time price updates and transaction statuses.
  • Type-Safe: Full TypeScript support ensures type safety and better developer experience.
  • Middleware Support: Customize request and response handling with middleware.
  • Multi-Network Support: Operate across various blockchain networks with ease.

Installation

To install the SDK, use your preferred package manager:

pnpm add metarouter-sdk
# or
npm install metarouter-sdk
# or
yarn add metarouter-sdk

Quick Start

Here’s a quick guide to get you started with the MetaRouter SDK:

import { MetaRouter } from 'metarouter-sdk';

// Initialize the SDK
const router = MetaRouter.init({
  apiKey: 'your-api-key',
  environment: 'live',
  defaultOptions: {
    slippageTolerance: 0.005, // 0.5%
    deadline: 20 * 60, // 20 minutes
    gasSpeed: 'standard',
  },
});

// Get a quote for a token swap
const quote = await router.quote({
  source: {
    chain: 1, // Ethereum
    token: '0x...',
    amount: '1000000000000000000', // 1 ETH
  },
  destination: {
    chain: 56, // BSC
    token: '0x...',
  },
});

// Execute a swap
const result = await router.swap({
  quoteId: quote.id,
  userAddress: '0x...',
});

// Stream price updates
const subscription = router.streams.prices([
  {
    baseToken: '0x...',
    quoteToken: '0x...',
    chain: 1,
  },
]).subscribe({
  next: (update) => console.log('Price update:', update),
  error: (error) => console.error('Stream error:', error),
  complete: () => console.log('Stream completed'),
});

API Reference

MetaRouter

The main class for interacting with the SDK. It provides methods for obtaining quotes, executing swaps, and streaming data.

Configuration

interface MetaRouterConfig {
  apiKey: string;
  apiUrl?: string;
  environment: 'live' | 'test';
  defaultOptions?: {
    slippageTolerance: number;
    deadline: number;
    gasSpeed: 'slow' | 'standard' | 'fast' | 'instant';
    referralCode?: string;
  };
  providers?: {
    [chainId: number]: Provider;
  };
}

Methods

  • static init(config: Partial<MetaRouterConfig>): MetaRouter
  • swap(params: ExecuteParams): Promise<ExecutionResult>
  • bridge(params: ExecuteParams): Promise<ExecutionResult>
  • quote(params: QuoteParams): Promise<RouteQuote>

Clients

The SDK provides specialized clients for different functionalities:

  • RoutingClient: Handles route analysis and gas estimation.
  • ExecutionClient: Manages transaction execution and approval checks.
  • StreamingClient: Provides real-time data streams via WebSocket and provider subscriptions.

Error Handling

The SDK uses typed errors for better error handling. Ensure to catch and handle errors appropriately in your application.