fork of @pancakeswap/smart-router
is a SDK for getting best trade routes from Pancakeswap AMM.
$ yarn add @real-wagmi/smart-router
Use KAVA 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.
- Install other dependencies
$ yarn add viem graphql-request @real-wagmi/sdk
- Prepare on-chain rpc provider and subgraph providers
import { createPublicClient, http } from 'viem'
import { GraphQLClient } from 'graphql-request'
import { SmartRouter } from '@real-wagmi/smart-router'
const publicClient = createPublicClient({
chain: mainnet,
transport: http('https://evm.kava.io'),
batch: {
multicall: {
batchSize: 1024 * 200,
},
},
})
const v3SubgraphClient = new GraphQLClient('https://kava.graph.wagmi.com/subgraphs/name/v3')
const quoteProvider = SmartRouter.createQuoteProvider({ onChainProvider: () => publicClient })
- Get candidate pools
import { kavaTokens } from '@real-wagmi/sdk'
import { SmartRouter } from '@real-wagmi/smart-router'
const swapFrom = kavaTokens.kava
const swapTo = kavaTokens.wagmi
const v3Pools = await SmartRouter.getV3CandidatePools({
onChainProvider: () => publicClient,
subgraphProvider: () => v3SubgraphClient,
currencyA: swapFrom,
currencyB: swapTo,
})
- Find the best swap trade route
import { tryParseAmount } from '@real-wagmi/sdk'
// 10 KAVA in our example
const amount = tryParseAmount("10", swapFrom)
const trade = await SmartRouter.getBestTrade(amount, swapTo, TradeType.EXACT_INPUT, {
gasPriceWei: () => publicClient.getGasPrice(),
maxHops: 2,
maxSplits: 2,
poolProvider: SmartRouter.createStaticPoolProvider(pools),
quoteProvider,
quoterOptimization: true,
})
- Build the swap transaction from trade
import { ChainId, Percent } from '@real-wagmi/sdk'
import { SmartRouter, SmartRouterTrade, SMART_ROUTER_ADDRESSES, SwapRouter } from '@real-wagmi/smart-router'
import { hexToBigInt } from 'viem'
const routerAddress = SMART_ROUTER_ADDRESSES[ChainId.KAVA]
// 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)