npm package: @summitx/smart-router
Description: A SDK for getting best routes from AMM
Latest version: 5.1.1

# SummitX Smart Router

`@summitx/smart-router` is a SDK for getting best trade routes from SummitX AMM.

## Install

```bash
$ pnpm add @summitx/smart-router
```

## Usage

Use Basecamp summit as an example. Here's how we use smart router sdk to find the best trade route swapping from ETH to SUMMIT and construct a valid swap transaction from the trade route we got.

For working code example, please refer to [smart-router-example](https://github.com/summitx/smart-router-example).

0. Install other dependencies

```bash
$ pnpm add viem graphql-request @summitx/sdk @summitx/tokens
```

1. Prepare on-chain rpc provider and subgraph providers

```typescript
import { createPublicClient, http } from "viem";
import { GraphQLClient } from "graphql-request";
import { SmartRouter } from "@summitx/smart-router/evm";

const publicClient = createPublicClient({
  chain: mainnet,
  transport: http(import.meta.env.VITE_CAMP_TESTNET_RPC_URL),
  batch: {
    multicall: {
      batchSize: 1024 * 200,
    },
  },
});

const v3SubgraphClient = new GraphQLClient("http://localhost:8000/subgraphs/name/summitx-exchange-v3");
const v2SubgraphClient = new GraphQLClient(
  "https://graph.summitx.finance/subgraphs/id/QmcDeyNXjCuT1BceXRxbxcdxP7ro3UF19h8uEatNPfKyp1",
);

const quoteProvider = SmartRouter.createQuoteProvider({ onChainProvider: () => publicClient });
```

2. Get candidate pools

```typescript
import { Native } from "@summitx/sdk";
import { SmartRouter } from "@summitx/smart-router/evm";
import { campTokens } from "@summitx/tokens";

const swapFrom = Native.onChain(chainId);
const swapTo = campTokens.summit;

const [v2Pools, v3Pools] = await Promise.all([
  SmartRouter.getV2CandidatePools({
    onChainProvider: () => publicClient,
    v2SubgraphProvider: () => v2SubgraphClient,
    v3SubgraphProvider: () => v3SubgraphClient,
    currencyA: swapFrom,
    currencyB: swapTo,
  }),
  SmartRouter.getV3CandidatePools({
    onChainProvider: () => publicClient,
    subgraphProvider: () => v3SubgraphClient,
    currencyA: swapFrom,
    currencyB: swapTo,
  }),
]);
```

3. Find the best swap trade route

```typescript
import { CurrencyAmount, TradeType } from "@summitx/sdk";

// 0.01 ETH in our example
const amount = CurrencyAmount.fromRawAmount(swapFrom, 10 ** 16);

const trade = await SmartRouter.getBestTrade(amount, swapTo, TradeType.EXACT_INPUT, {
  gasPriceWei: () => publicClient.getGasPrice(),
  maxHops: 2,
  maxSplits: 2,
  poolProvider: SmartRouter.createStaticPoolProvider(pools),
  quoteProvider,
  quoterOptimization: true,
});
```

4. Build the swap transaction from trade

```typescript
import { ChainId } from "@summitx/chains";
import { SmartRouter, SmartRouterTrade, SMART_ROUTER_ADDRESSES, SwapRouter } from "@summitx/smart-router/evm";
import { hexToBigInt } from "viem";

const routerAddress = SMART_ROUTER_ADDRESSES[ChainId.CAMP];
// Swap recipient address
const address = "0x";

const { value, calldata } = SwapRouter.swapCallParameters(trade, {
  recipient: address,
  slippageTolerance: new Percent(1),
});

const tx = {
  account: address,
  to: routerAddress,
  data: calldata,
  value: hexToBigInt(value),
};
const gasEstimate = await publicClient.estimateGas(tx);
```