Custom SDK for API Interaction

Our custom SDK, generated using Nestia, provides a type-safe and efficient way to interact with our API. This document outlines the structure, usage, and key features of the SDK.

SDK Structure

The SDK is organized into modules that mirror the API’s structure:

import api from "codemelt-retro-api-sdk";

// Modules
api.functional.swap
api.functional.liquidity
api.functional.currency

Each module contains functions corresponding to specific API endpoints.

Connection Setup

Before making API calls, set up a connection object:

const connection: IConnection = {
  host: "<enter you host url here>",
  headers: {
    'x-api-key': '<enter your api key here>'
  }
};

Key Modules and Endpoints

Currency Module

Retrieve Retro token details:

import api from "codemelt-retro-api-sdk";

const response = await api.function.currency.getPartners(connection);

Swap Module

Get swap data for native tokens:

import { swap } from "codemelt-retro-api-sdk/functional/api";

const swapDetails: swap.getSwapData.Input = {
  tokenAId: 1,
  tokenBId: 2,
  slippage: 50,
  amount: 100,
  sender: "0x1234...",
  recipient: "0x5678..."
};

const swapData = await swap.getSwapData(connection, swapDetails);

Liquidity Module

The liquidity module is further divided into submodules:

import { liquidity } from "codemelt-retro-api-sdk/functional/api";

liquidity.tvl
liquidity.emissions

Type Safety

The SDK provides strong type safety through detailed input and output type definitions:

export type SwapDetailsDTO = {
  tokenAId: number & Type<"uint32">;
  tokenBId: number & Type<"uint32">;
  slippage: number & Default<50> & Minimum<10>;
  amount: number & Type<"double">;
  sender: string & Pattern<"^0x[a-fA-F0-9]{40}|^[A-HJ-NP-Za-km-z1-9]*|^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$">;
  recipient: string & Pattern<"^0x[a-fA-F0-9]{40}$">;
};

Error Handling

The SDK uses the IPropagation type for responses, which includes error handling capabilities:

export type Output = IPropagation<{
  201: RequestResponseCrossChainSwapTransactionDTO;
}>;

Metadata

Each endpoint function includes metadata about the HTTP method, path, and request/response types:

export const METADATA = {
  method: "POST",
  path: "/api/swap/native",
  request: {
    type: "application/json",
    encrypted: false,
  },
  response: {
    type: "application/json",
    encrypted: false,
  },
  status: null,
} as const;

Best Practices

  1. Type Inference: Leverage TypeScript’s type inference to ensure you’re providing correct parameters.

  2. Error Handling: Always wrap API calls in try-catch blocks to handle potential errors gracefully.

  3. Connection Management: Reuse the connection object when making multiple calls to improve efficiency.

  4. Modular Imports: Import only the modules you need to keep your bundle size small.

Example Usage

Here’s a complete example of using the SDK to perform a swap:

import { IConnection } from "codemelt-retro-api-sdk";
import { swap } from 'codemelt-retro-api-sdk/functional/api'

async function getSwapData() {
  const connection: IConnection = {
    host: "<enter you host url here>",
    headers: {
      'x-api-key': '<enter your api key here>'
    }
  };

  const swapDetails: swap.getSwapData.Input = {
    tokenAId: 1,
    tokenBId: 2,
    slippage: 50,
    amount: 100,
    sender: "0x1234...",
    recipient: "0x5678..."
  };

  try {
    const swapData = await swap.getSwapData(connection, swapDetails);
    console.log("Swap data:", swapData);
  } catch (error) {
    console.error("Error performing swap:", error);
  }
}

performSwap();

This SDK provides a robust, type-safe way to interact with our API, ensuring that you can build reliable integrations with confidence.