Defi Integrations
Learn how to swap tokens using Jupiter Exchange integration
Execute token swaps on Solana using Jupiter Exchange aggregation. Support for all SPL tokens with automatic SOL wrapping/unwrapping and slippage protection.
Copy
// Swap SOL for USDC
const signature = await agent.methods.trade(
new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"), // USDC
1, // 1 SOL
);
// Swap USDC for SOL with custom slippage
const signature = await agent.methods.trade(
new PublicKey("So11111111111111111111111111111111111111112"), // SOL
100, // 100 USDC
new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"), // USDC
100 // 1% slippage
);
outputMint
PublicKey
Yes
Target token mint address
inputAmount
number
Yes
Amount to swap
inputMint
PublicKey
No
Source token mint (defaults to SOL)
slippageBps
number
No
Slippage tolerance in basis points (default: 300)
Copy
"Swap 1 SOL for USDC"
"Exchange 100 USDC for SOL with 1% slippage"
"Trade my BONK tokens for USDC"
"Convert 50 USDT to jitoSOL"
Copy
// Swap SOL for USDC
{
"outputMint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"inputAmount": 1
}
// Swap USDC for SOL with custom slippage
{
"outputMint": "So11111111111111111111111111111111111111112",
"inputAmount": 100,
"inputMint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"slippageBps": 100
}
// Swap with specific decimals
{
"outputMint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"inputAmount": 1000,
"inputMint": "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
"inputDecimal": 5
}
Copy
import { SolanaAgentKit } from "solana-agent-kit";
import { PublicKey } from "@solana/web3.js";
async function executeSwaps(agent: SolanaAgentKit) {
// Common token addresses
const USDC = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
const SOL = new PublicKey("So11111111111111111111111111111111111111112");
try {
// Swap SOL for USDC
const swap1 = await agent.methods.trade(
USDC,
1 // 1 SOL
);
console.log("SOL -> USDC swap:", swap1);
// Swap USDC back to SOL
const swap2 = await agent.methods.trade(
SOL,
100, // 100 USDC
USDC,
100 // 1% slippage
);
console.log("USDC -> SOL swap:", swap2);
} catch (error) {
console.error("Swap failed:", error);
}
}
Uses Jupiter Exchange for best prices
Automatic SOL wrapping/unwrapping
Dynamic compute unit limits
Auto-calculated priority fees
Optional referral integration
Direct route optimization
Copy
try {
const signature = await agent.methods.trade(outputMint, amount, inputMint);
} catch (error) {
if (error.message.includes("insufficient funds")) {
// Handle insufficient balance
} else if (error.message.includes("slippage")) {
// Handle price movement
}
}
Slippage Management
Use appropriate slippage for token
Consider market volatility
Monitor price impact
Handle failed transactions
Amount Calculation
Account for token decimals
Check minimum amounts
Consider fees
Verify available balance
Error Handling
Implement retries
Monitor transaction status
Handle timeouts
Verify swap results
Performance
Use direct routes when possible
Set appropriate compute limits
Monitor network conditions
Consider priority fees
SOL:
So11111111111111111111111111111111111111112
USDC:
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
USDT:
Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB
BONK:
DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263
jitoSOL:
J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn
Copy
// Successful response
{
status: "success",
message: "Trade executed successfully",
transaction: "5UfgJ5vVZxUxefDGqzqkVLHzHxVTyYH9StYyHKgvHYmXJgqJKxEqy9k4Rz9LpXrHF9kUZB7",
inputAmount: 1,
inputToken: "SOL",
outputToken: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
}
// Error response
{
status: "error",
message: "Error message here",
code: "ERROR_CODE"
}
getBalance
: Check token balancesfetchPrice
: Get token pricesgetTokenData
: Get token informationtransfer
: Transfer tokens
Last updated