# 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

```javascript
const quoter = new AggregatorQuoter('mainnet');
const params: SingleQuoteQueryParams = {
  tokenIn: '0x2::sui::SUI',
  tokenOut: '0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN',
  amountIn: '1000000000',
  includeSources: null, //optional
  excludeSources: null, //optional
  commission: null, //optional, and will be explain later
  maxHops: null, //optional: default and max is 3
  splitDistributionPercent: null, //optional: default 1 and max 100
  excludePools: null, //optional: list pool you want excude example: 0xpool1,0xpool2 
};

const routes = await quoter.getRoutes(params);
```

### Build Transaction for aggregator swap

#### Normal case if you want fast swap

```javascript
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

```javascript
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.

```typescript
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 `coin`pass 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
