Build Swap Applications on Sui: A Comprehensive Guide

·

There are two primary approaches to building swap applications with OKX DEX on the Sui blockchain:

  1. The API-first approach: Directly interacting with OKX DEX API endpoints
  2. The SDK approach: Using the @okx-dex/okx-dex-sdk package for simplified development

This guide provides detailed instructions for both methods, helping you choose the optimal solution for your project requirements.

API-First Approach for Sui Token Swaps

1. Environment Setup

Begin by importing necessary Node.js libraries and configuring environment variables:

const axios = require('axios');
require('dotenv').config();

2. Token Information and Quote Retrieval

Create utility functions for API interaction:

function getAuthHeaders() {
  return {
    headers: {
      'X-API-KEY': process.env.API_KEY
    }
  };
}

async function getTokenInfo(tokenAddress) {
  const response = await axios.get(
    `https://api.okx-dex.com/tokens/${tokenAddress}`,
    getAuthHeaders()
  );
  return response.data;
}

3. Swap Transaction Preparation

Define swap parameters and request transaction data:

const swapParams = {
  fromToken: 'SUI',
  toToken: 'USDC',
  amount: '1.0',
  slippage: '0.5'
};

async function getSwapData(params) {
  const response = await axios.post(
    'https://api.okx-dex.com/swap',
    params,
    getAuthHeaders()
  );
  return response.data;
}

4. Transaction Simulation

Simulate transactions before execution:

async function simulateTransaction(txData) {
  const response = await axios.post(
    'https://api.okx-dex.com/simulate',
    txData,
    getAuthHeaders()
  );
  return response.data;
}

👉 Learn more about transaction simulation best practices

SDK Approach for Simplified Development

1. SDK Installation

npm install @okx-dex/okx-dex-sdk

2. Environment Configuration

Create .env file:

API_KEY=your_api_key
PRIVATE_KEY=your_private_key

3. Client Initialization

import { DexClient } from '@okx-dex/okx-dex-sdk';

const client = new DexClient({
  network: 'sui',
  apiKey: process.env.API_KEY,
  privateKey: process.env.PRIVATE_KEY
});

4. Token Reference Helper

const tokenList = {
  SUI: '0x2::sui::SUI',
  USDC: '0x5d4b302506...'
};

5. Executing Swaps with SDK

async function executeSwap() {
  const quote = await client.getQuote({
    fromToken: tokenList.SUI,
    toToken: tokenList.USDC,
    amount: '1.0'
  });

  const result = await client.executeSwap(quote);
  return result;
}

FAQ Section

What is the difference between API and SDK approaches?

The API approach offers more control and customization, while the SDK provides convenience with pre-built functionality.

How do I get API credentials?

API credentials are available for enterprise customers through the OKX developer portal.

👉 Explore advanced swap functionalities

Can I use both approaches together?

Yes, you can combine API calls with SDK methods for hybrid solutions.

What tokens are supported on Sui?

OKX DEX supports all major SUI tokens including native SUI and popular stablecoins.

How do I handle transaction failures?

Both approaches include retry mechanisms - the SDK handles this automatically while the API provides error responses.

What are the rate limits?

Standard API rate limits apply, with higher tiers available for enterprise customers.