Swap Module
The Swap Module provides DEX token swapping functionality, integrating the Astros aggregator to find optimal swap routes and execute trades across multiple decentralized exchanges.
Core Features
- Token Swapping: Swap between different tokens
- Aggregator Integration: Use Astros aggregator to find optimal routes
- Slippage Protection: Built-in slippage protection mechanism
- Multi-DEX Support: Support for multiple decentralized exchanges
Configuration Options
apiKey
API key for aggregator access.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient({
configs: {
swap: {
apiKey: 'your-api-key'
}
}
})
baseUrl
Base URL for the aggregator API, defaults to https://open-aggregator-api.naviprotocol.io/find_routes
.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient({
configs: {
swap: {
baseUrl: 'https://open-aggregator-api.naviprotocol.io/find_routes'
}
}
})
dexList
List of DEXes used for routing.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
import { Dex } from '@naviprotocol/astros-aggregator-sdk'
const walletClient = new WalletClient({
configs: {
swap: {
dexList: [Dex.CETUS, Dex.TURBOS, Dex.AFTERMATH]
}
}
})
depth
Maximum routing depth for finding optimal paths, defaults to 3.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient({
configs: {
swap: {
depth: 3
}
}
})
serviceFee
Service fee configuration.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient({
configs: {
swap: {
serviceFee: {
recipient: '0x123...',
fee: 0.001 // 0.1%
}
}
}
})
API Reference
swap
Execute a token swap operation.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient()
const result = await walletClient.swap.swap(
'0x2::sui::SUI', // Source token type
'0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', // Target token type
1000000000, // Swap amount
0.01, // Slippage tolerance (1%)
{ dryRun: false } // Optional parameters
)
referral
Get referral ID (if API key is configured).
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient()
const referralId = walletClient.swap.referral
swapOptions
Get the current swap configuration options.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient()
const options = walletClient.swap.swapOptions
console.log('Current swap options:', options)
Return Value Example
{
"baseUrl": "https://open-aggregator-api.naviprotocol.io/find_routes",
"dexList": ["cetus", "turbos"],
"depth": 3,
"serviceFee": {
"fee": 0.001,
"receiverAddress": "0x123..."
}
}
getQuote
Get a quote for a swap operation from the aggregator.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient()
const quote = await walletClient.swap.getQuote(
'0x2::sui::SUI', // Source token type
'0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', // Target token type
1000000000 // Amount to swap
)
console.log('Quote received:', quote)
Quote Object Structure
{
"routes": [
{
"dex": "cetus",
"amount_in": "1000000000",
"amount_out": "950000000",
"path": ["0x2::sui::SUI", "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN"]
}
],
"amount_in": "1000000000",
"amount_out": "950000000",
"from": "0x2::sui::SUI",
"target": "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
"dexList": ["cetus"],
"from_token": {
"address": "0x2::sui::SUI",
"decimals": 9,
"price": 1.0
},
"to_token": {
"address": "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
"decimals": 9,
"price": 0.95
},
"is_accurate": true
}
buildSwapPTBFromQuote
Build a swap transaction from a quote with slippage protection.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
import { Transaction } from '@mysten/sui/transactions'
const walletClient = new WalletClient()
const tx = new Transaction()
// Get quote first
const quote = await walletClient.swap.getQuote(
'0x2::sui::SUI',
'0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN',
1000000000
)
// Build swap transaction from quote
const coinOut = await walletClient.swap.buildSwapPTBFromQuote(
tx,
quote,
fromCoin, // Coin object from mergeCoinsPTB or similar
0.01 // 1% slippage tolerance
)
// Transfer output coins to wallet
tx.transferObjects([coinOut], walletClient.address)
Events
swap:swap-success
Event triggered when a swap is successfully completed.
Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
const walletClient = new WalletClient()
walletClient.events.on('swap:swap-success', (data) => {
console.log('Swap successful:', {
fromCoinType: data.fromCoinType,
toCoinType: data.toCoinType,
fromAmount: data.fromAmount,
toAmount: data.toAmount,
slippage: data.slippage
})
})
Event Data Example
{
"fromCoinType": "0x2::sui::SUI",
"toCoinType": "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
"fromAmount": 1000000000,
"toAmount": 950000000,
"slippage": 0.01
}
Complete Usage Example
import { WalletClient } from '@naviprotocol/wallet-client'
import { Dex } from '@naviprotocol/astros-aggregator-sdk'
// Create wallet client
const walletClient = new WalletClient({
configs: {
swap: {
apiKey: 'your-api-key',
serviceFee: {
recipient: '0x123...',
fee: 0.001
}
}
}
})
// Execute token swap
try {
const result = await walletClient.module('swap').swap(
'0x2::sui::SUI',
'0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN',
1000000000, // 1 SUI
0.01 // 1% slippage
)
console.log('Swap transaction submitted:', result.digest)
} catch (error) {
console.error('Swap failed:', error)
}