4.3.13 • Published 9 months ago

@baseswapfi/smart-router v4.3.13

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

Baseswap Smart Router

@baseswapfi/smart-router is a SDK for getting best trade routes from Baseswap AMM.

Install

$ pnpm add @baseswapfi/smart-router

Usage

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

For working code example, please refer to smart-router-example.

  1. Install other dependencies
$ pnpm add viem graphql-request @baseswapfi/sdk @baseswapfi/tokens
  1. Prepare on-chain rpc provider and subgraph providers
import { createPublicClient, http } from 'viem';
import { GraphQLClient } from 'graphql-request';
import { SmartRouter } from '@baseswapfi/smart-router/evm';

const publicClient = createPublicClient({
  chain: mainnet,
  transport: http('https://mainnet.base.org'),
  batch: {
    multicall: {
      batchSize: 1024 * 200,
    },
  },
});

const v3SubgraphClient = new GraphQLClient('');
const v2SubgraphClient = new GraphQLClient('');

const quoteProvider = SmartRouter.createQuoteProvider({ onChainProvider: () => publicClient });
  1. Get candidate pools
import { Native } from '@baseswapfi/sdk';
import { SmartRouter } from '@baseswapfi/smart-router/evm';
import { PROTOCOL_TOKEN } from '@baseswapfi/tokens';

const swapFrom = Native.onChain(chainId);
const swapTo = PROTOCOL_TOKEN[chainId];

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,
  }),
]);
  1. Find the best swap trade route
import { CurrencyAmount, TradeType } from '@baseswapfi/sdk';

// 0.01 WETH 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,
});
  1. Build the swap transaction from trade
import { ChainId } from '@baseswapfi/sdk';
import {
  SmartRouter,
  SmartRouterTrade,
  SMART_ROUTER_ADDRESSES,
  SwapRouter,
} from '@baseswapfi/smart-router/evm';
import { hexToBigInt } from 'viem';

const routerAddress = SMART_ROUTER_ADDRESSES[ChainId.BASE];
// 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);
4.3.13

9 months ago

4.3.12

9 months ago

4.3.11

9 months ago

4.3.10

9 months ago

4.3.9

9 months ago

4.3.8

9 months ago

4.3.7

9 months ago

4.3.6

9 months ago

4.3.5

9 months ago

4.3.4

9 months ago

4.3.3

9 months ago

4.3.2

9 months ago

4.3.1

9 months ago