Execute token swaps on Sui using the 7K Protocol for aggregation, ensuring slippage protection.

Usage

// Swap token use symbol
const result = await agent.swap({
  fromToken: "SUI";
  toToken: "USDC";
  inputAmount: 1;
  slippage: 0.01; // 1%
})

// Swap token use address
const result = await agent.swap({
  fromToken: "SUI";
  toToken: "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC";
  inputAmount: 1;
  slippage: 0.01; // 1%
})

Parameters

ParameterTypeRequiredDescription
fromTokenstringYesAddress or symbol of the input token
toTokenstringYesAddress or symbol of the output token
inputAmountnumberYesAmount of the input token to swap
slippagenumberNoMaximum allowable slippage (e.g., 0.01 for 1%)

Example Prompts

Natural Language Prompts

"Swap 1 SUI for USDC"

"Exchange 100 USDC for SUI with 1% slippage"

"Convert 50 USDT to SUI"

LangChain Tool Prompts

// Swap Sui for USDC
{
 "fromToken": "SUI";
 "toToken": "USDC";
 "inputAmount": 1;
 "slippage": 0.01; // 1%
}

// Swap Sui for USDC
{
 "fromToken": "SUI";
 "toToken": "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC";
 "inputAmount": 1;
 "slippage": 0.01; // 1%
}

Example Implementation

import { SuiAgentKit } from "sui-agent-kit";

async function executeSwaps(agent: SuiAgentKit) {
  try {
    const res = await agent.swap({
      fromToken: "SUI",
      toToken: "USDC",
      inputAmount: 100,
      slippage: 0.01, // 1%
    });
    
    console.log("Swap result:", res);
  } catch (error) {
    console.error("Swap failed:", error);
  }
}

Implementation Details

  • Uses 7k Protocol for best prices
  • Dynamic compute unit limits
  • Auto-calculated priority fees
  • Direct route optimization

Error Handling

try {
  const res = await agent.swap({
    fromToken: "SUI",
    toToken: "USDC",
    inputAmount: 100,
    slippage: 0.01, // 1%
  });
} catch (error) {
  if (error.message.includes("Insufficient balance")) {
    // Handle insufficient balance
  } else {
    // Handle other transaction failures
  }
}

Best Practices

  1. Slippage Management
  • Use appropriate slippage for token
  • Consider market volatility
  • Monitor price impact
  • Handle failed transactions
  1. Amount Calculation
  • Account for token decimals
  • Check minimum amounts
  • Consider fees
  • Verify available balance
  1. Error Handling
  • Implement retries
  • Monitor transaction status
  • Handle timeouts
  • Verify swap results
  1. Performance
  • Use direct routes when possible
  • Set appropriate compute limits
  • Monitor network conditions
  • Consider priority fees

Response Format

// Successful response
{
  status: "success",
  result: {
    tx_hash: "5JvBtQveYFsZFYxXMfSxYzUJgzGfyRgkH9YuZxpvuR9Y",
    tx_status: "success",
  },
}

// Error response
{
  status: "error",
  message: "Error message here",
  code: "ERROR_CODE"
}
  • get_holding: Check token balances
  • transfer: Transfer token.