Quick Start
This guide will help you get up and running with the Ferra Aggregator SDK in minutes.
1. Initialize the SDK
Mainnet Setup
import { initFerraAggregatorSDK } from '@ferra-labs/aggregator'
const useAggreratorFerraSdk = (address: string) => {
return useMemo(
() =>
initFerraAggregatorSDK({
network: NETWORK,
fullNodeUrl: FULL_NODE_URL,
wallet: address,
}),
[address]
);
};
2. Get Best Quotes
Find the best trading routes for your swap:
import { InputFindBestQuotesParams } from '@ferra-labs/aggregator'
const quoteParams: InputFindBestQuotesParams = {
from: '0x2::sui::SUI', // SUI token
to: '0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', // USDC token
amount: '1000000000', // 1 SUI (9 decimals)
slippageTolerance: 0.5 // 0.5% slippage tolerance
}
try {
const routes = await sdk.Quoter.getBestQuotes(quoteParams)
if (routes.length > 0) {
console.log('Best route found:', routes[0])
} else {
console.log('No routes available')
}
} catch (error) {
console.error('Quote error:', error.message)
}
3. Execute Swap
Execute the swap using the best route:
try {
// Get trading routes
const routes = await sdk.Quoter.getBestQuotes(quoteParams)
if (routes.length === 0) {
throw new Error('No trading routes available')
}
// Execute swap with the best routes
const transaction = await sdk.AggSwap.swapWithTradingRoutes(routes)
console.log('Transaction prepared successfully!')
console.log('Transaction data:', transaction.serialize())
// Sign and execute the transaction with your wallet
// This depends on your wallet integration
} catch (error) {
console.error('Swap error:', error.message)
}
4. Complete Example
Here's a complete working example:
import { initMainnetSDK, InputFindBestQuotesParams } from '@ferra-labs/aggregator'
async function performSwap() {
// Initialize SDK
const sdk = useAggreratorFerraSdk(address)
// Define swap parameters
const swapParams: InputFindBestQuotesParams = {
from: '0x2::sui::SUI',
to: '0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN',
amount: '1000000000', // 1 SUI
slippageTolerance: 0.5
}
try {
// Step 1: Get best quotes
console.log('Finding best routes...')
const routes = await sdk.Quoter.getBestQuotes(swapParams)
if (routes.length === 0) {
console.log('No routes available for this swap')
return
}
console.log(`Found ${routes.length} routes`)
console.log('Best route output:', routes[0].outputAmount)
// Step 2: Execute swap
console.log('Preparing swap transaction...')
const transaction = await sdk.AggSwap.swapWithTradingRoutes(routes)
console.log('✅ Swap transaction prepared successfully!')
} catch (error) {
console.error('❌ Swap failed:', error.message)
}
}
// Run the example
performSwap()
Last updated