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.
Router.constants = { maxExternalFeePercentage:0.5,// Maximum allowed external fee (50%)};
Example Usage
Basic Trading
// Initialize routerconstafSdk=newAftermath("MAINNET");awaitafSdk.init();constrouter=afSdk.Router();// Get a trade routeconstroute=awaitrouter.getCompleteTradeRouteGivenAmountIn({ coinInType:"0x2::sui::SUI", coinOutType:"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN", coinInAmount:BigInt(10_000_000_000), referrer:"0x...",});// Generate transaction for the routeconsttx=awaitrouter.getTransactionForCompleteTradeRoute({ walletAddress:"0x...", completeRoute: route, slippage:0.01,});
Advanced Transaction Building
constWALLET_ADDRESS="0x123";// Initialize SDK and routerconstaf=newAftermath("MAINNET");awaitaf.init();constrouter=af.Router();// Find first route (SUIP -> SUI)constcompleteRoute=awaitrouter.getCompleteTradeRouteGivenAmountIn({ coinInAmount:BigInt("2000000000"), coinInType:"0xe4239cd951f6c53d9c41e25270d80d31f925ad1655e5ba5b543843d4a66975ee::SUIP::SUIP", coinOutType:"0x2::sui::SUI",});// Create transaction and add first tradeconsttx=newTransaction();const { tx: newTx,coinOutId } =awaitrouter.addTransactionForCompleteTradeRoute({ tx, completeRoute, slippage:0.1, walletAddress:WALLET_ADDRESS, });// Transfer first trade outputnewTx.transferObjects([coinOutId!],WALLET_ADDRESS);// Find second route (SUI -> SUIP)constcompleteRoute2=awaitrouter.getCompleteTradeRouteGivenAmountIn({ coinInAmount:BigInt("2000000000"), coinInType:"0x2::sui::SUI", coinOutType:"0xe4239cd951f6c53d9c41e25270d80d31f925ad1655e5ba5b543843d4a66975ee::SUIP::SUIP",});// Add second trade to same transactionconst { tx: newTx2, coinOutId: coinOutId2 } =awaitrouter.addTransactionForCompleteTradeRoute({ tx: newTx, completeRoute: completeRoute2, slippage:0.1, walletAddress:WALLET_ADDRESS, });// Transfer second trade outputnewTx2.transferObjects([coinOutId2!],WALLET_ADDRESS);// Execute transaction `newTx2`...