The Router class provides a smart order routing system to find optimal trade routes across multiple pools and protocols on the Sui network. It handles routing trades through various liquidity pools to achieve the best possible execution price.
Initialization
Create a Router instance:
Copy const afSdk = new Aftermath("MAINNET");
await afSdk.init(); // initialize provider
const router = afSdk.Router();
Methods
getVolume24hrs()
Retrieves the total trading volume in the last 24 hours.
Copy const volume = await router.getVolume24hrs();
console.log(volume); // Returns a number representing total 24h volume
getSupportedCoins()
Returns an array of supported coins that can be traded through the router.
Copy const supportedCoins = await router.getSupportedCoins();
getCompleteTradeRouteGivenAmountIn()
Creates an optimal route for a trade with a specified input amount.
Copy const route = await router.getCompleteTradeRouteGivenAmountIn({
coinInType: "0x2::sui::SUI",
coinOutType: "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
coinInAmount: BigInt(10_000_000_000), // Amount of input token
referrer?: "0x...", // Optional referrer address
externalFee?: { // Optional external fee configuration
recipient: "0x...",
feePercentage: 0.01 // 1%
},
protocolBlacklist?: ["Cetus", "BlueMove"], // Optional protocols to exclude
// OR
protocolWhitelist?: ["Aftermath", "Cetus"] // Optional protocols to include
}, abortSignal?);
getCompleteTradeRouteGivenAmountOut()
Creates an optimal route for a trade with a specified output amount.
Copy const route = await router.getCompleteTradeRouteGivenAmountOut({
coinInType: "0x2::sui::SUI",
coinOutType: "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
coinOutAmount: BigInt(20_000_000), // Desired output amount
slippage: 0.01, // 1% slippage tolerance
referrer?: "0x...", // Optional referrer address
externalFee?: { // Optional external fee configuration
recipient: "0x...",
feePercentage: 0.01 // 1%
},
protocolBlacklist?: ["Cetus", "BlueMove"], // Optional protocols to exclude
// OR
protocolWhitelist?: ["Aftermath", "Cetus"] // Optional protocols to include
}, abortSignal?);
getTransactionForCompleteTradeRoute()
Generates a transaction for executing a complete trade route.
Copy const transaction = await router.getTransactionForCompleteTradeRoute({
walletAddress: "0x...", // Trader's address
completeRoute: routeObject, // Route from getCompleteTradeRoute
slippage: 0.01, // 1% max slippage
isSponsoredTx: boolean, // Optional sponsored transaction flag
});
addTransactionForCompleteTradeRoute()
Adds a trade to an existing transaction (swap can be performed before or after any logic within the existing transaction).
Copy const result = await router.addTransactionForCompleteTradeRoute({
tx: Transaction, // Existing transaction
completeRoute: route, // Route from getCompleteTradeRoute
slippage: number, // Slippage tolerance (e.g. 0.01 for 1%)
walletAddress: string, // Trader's address
coinInId: TransactionObjectArgument, // Optional coin input ID
});
// Returns: { tx: Transaction, coinOutId: TransactionObjectArgument }
Types
RouterProtocolName
Supported DEX protocols for routing:
Copy type RouterProtocolName =
| "Aftermath"
| "BlueMove"
| "Cetus"
| "DeepBook"
| "DeepBookV3"
| "DoubleUpPump"
| "FlowX"
| "FlowXClmm"
| "HopFun"
| "Kriya"
| "KriyaClmm"
| "Metastable"
| "MovePump"
| "Obric"
| "SuiSwap"
| "Turbos"
| "SpringSui"
| "Bluefin"
| "TurbosFun";
RouterCompleteTradeRoute
Structure containing the complete trade route information:
Copy interface RouterCompleteTradeRoute {
routes: RouterTradeRoute[];
netTradeFeePercentage: number;
referrer?: string;
externalFee?: {
recipient: string;
feePercentage: number;
};
slippage?: number;
coinIn: {
type: string;
amount: bigint;
tradeFee: bigint;
};
coinOut: {
type: string;
amount: bigint;
tradeFee: bigint;
};
spotPrice: number;
}
Constants
Copy Router.constants = {
maxExternalFeePercentage: 0.5, // Maximum allowed external fee (50%)
};
Example Usage
Basic Trading
Copy // Initialize router
const afSdk = new Aftermath("MAINNET");
await afSdk.init();
const router = afSdk.Router();
// Get a trade route
const route = await router.getCompleteTradeRouteGivenAmountIn({
coinInType: "0x2::sui::SUI",
coinOutType:
"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
coinInAmount: BigInt(10_000_000_000),
referrer: "0x...",
});
// Generate transaction for the route
const tx = await router.getTransactionForCompleteTradeRoute({
walletAddress: "0x...",
completeRoute: route,
slippage: 0.01,
});
Advanced Transaction Building
Copy const WALLET_ADDRESS = "0x123";
// Initialize SDK and router
const af = new Aftermath("MAINNET");
await af.init();
const router = af.Router();
// Find first route (SUIP -> SUI)
const completeRoute = await router.getCompleteTradeRouteGivenAmountIn({
coinInAmount: BigInt("2000000000"),
coinInType:
"0xe4239cd951f6c53d9c41e25270d80d31f925ad1655e5ba5b543843d4a66975ee::SUIP::SUIP",
coinOutType: "0x2::sui::SUI",
});
// Create transaction and add first trade
const tx = new Transaction();
const { tx: newTx, coinOutId } =
await router.addTransactionForCompleteTradeRoute({
tx,
completeRoute,
slippage: 0.1,
walletAddress: WALLET_ADDRESS,
});
// Transfer first trade output
newTx.transferObjects([coinOutId!], WALLET_ADDRESS);
// Find second route (SUI -> SUIP)
const completeRoute2 = await router.getCompleteTradeRouteGivenAmountIn({
coinInAmount: BigInt("2000000000"),
coinInType: "0x2::sui::SUI",
coinOutType:
"0xe4239cd951f6c53d9c41e25270d80d31f925ad1655e5ba5b543843d4a66975ee::SUIP::SUIP",
});
// Add second trade to same transaction
const { tx: newTx2, coinOutId: coinOutId2 } =
await router.addTransactionForCompleteTradeRoute({
tx: newTx,
completeRoute: completeRoute2,
slippage: 0.1,
walletAddress: WALLET_ADDRESS,
});
// Transfer second trade output
newTx2.transferObjects([coinOutId2!], WALLET_ADDRESS);
// Execute transaction `newTx2`...