Swap Aggregator

Get Swap Router

WARNING: amountOut FROM QUOTE WHEN USE WITH COMMISSION ONLY FOR DISPLAY, NOT FOR CALCULATE ONCHAIN.

To find best route for swap

const quoter = new AggregatorQuoter('mainnet');
const params: AggregatorQuoterQueryParams = {
  tokenIn: '0x2::sui::SUI',
  tokenOut: '0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN',
  amountIn: '1000000000',
  includeSources: null, //optional
  excludeSources: null, //optional
  commission: null, //optional, and will be explain later
};

const routes = await quoter.getRoutes(params);

Build Transaction for aggregator swap

Normal case if you want fast swap

const tradeBuilder = new TradeBuilder(NETWORK.MAINNET, routes); //routes get from quoter
const tx = tradeBuilder
  .sender('0xSenderAddress') //Optional if you want pass coin later
  .slippage((1 / 100) * 1e6) // Slippage 1%
  .commission(null) // Optional: commission will be explain later
  .build()
  .buildTransaction({ client });

Return coin for later use

const tradeBuilder = new TradeBuilder(NETWORK.MAINNET, routes); //routes get from quoter
const tx = new Transaction();
const trade = tradeBuilder
  .sender('0xSenderAddress') //Optional if you want pass coin later
  .slippage((1 / 100) * 1e6) // Slippage 1%
  .commission(null) // Optional: commission will be explain later
  .build();
const coinOut = trade.swap({ client, tx }) 

Commission

The Commission class represents a commission configuration for transactions, defining the partner, commission type, and value. It includes methods for computing the commission amount based on the specified type.

const commission = new Commission('0xPartnerAddress', new Coin('0x2::sui:SUI'), CommissionType.PERCENTAGE, '500', true);

if CommissionType.PERCENTAGE then value should be input 1/100 * 1e6 it is example of 1% if CommissionType.FLAT then value should be the amount of token you want to fee include decimals Then you should pass commission variable to both tradeBuilder and getRoutes for exact values

if directTransfer= true then commission will transfer directly to partner address, else you need go to contract and claim partner fee later

The coinpass in commission that mean coin you want collect fee in transaction, for example, if you pass SUI is coin collect fee, when you swap SUI -> USDC or USDC->SUI you will collect SUI is a fee, but if you swap FLX->USDC and USDC->FLX you receive nothing and coin is SUI, then you SHOULD NOT pass commission to TradeBuilder

Usage

If const routes = await quoter.getRoutes(params)include commssion then amount will return amount that include commission

if trade = tradeBuilder.commission(commission) then the transaction will include commission, if not pass commission in tradeBuilder then transaction will execute without commission

Last updated