@jettxcypher/smart-router
v1.0.1
Published
A SDK for getting best routes from AMM
Downloads
5
Readme
Pancakeswap Smart Router
@jettxcypher/smart-router
is a SDK for getting best trade routes from Pancakeswap AMM.
Install
$ pnpm add @jettxcypher/smart-router
Usage
Use BSC 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
$ pnpm add viem graphql-request @jettxcypher/sdk @jettxcypher/tokens
- Prepare on-chain rpc provider and subgraph providers
import { createPublicClient, http } from 'viem'
import { GraphQLClient } from 'graphql-request'
import { SmartRouter } from '@jettxcypher/smart-router/evm'
const publicClient = createPublicClient({
chain: mainnet,
transport: http('https://bsc-dataseed1.binance.org'),
batch: {
multicall: {
batchSize: 1024 * 200,
},
},
})
const v3SubgraphClient = new GraphQLClient('https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-bsc')
const v2SubgraphClient = new GraphQLClient('https://proxy-worker-api.pancakeswap.com/bsc-exchange')
const quoteProvider = SmartRouter.createQuoteProvider({ onChainProvider: () => publicClient })
- Get candidate pools
import { Native } from '@jettxcypher/sdk'
import { SmartRouter } from '@jettxcypher/smart-router/evm'
import { bscTokens } from '@jettxcypher/tokens'
const swapFrom = Native.onChain(chainId)
const swapTo = bscTokens.cake
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,
}),
])
- Find the best swap trade route
import { CurrencyAmount, TradeType } from '@jettxcypher/sdk'
// 0.01 BNB 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,
})
- Build the swap transaction from trade
import { ChainId } from '@jettxcypher/chains'
import { SmartRouter, SmartRouterTrade, SMART_ROUTER_ADDRESSES, SwapRouter } from '@jettxcypher/smart-router/evm'
import { hexToBigInt } from 'viem'
const routerAddress = SMART_ROUTER_ADDRESSES[ChainId.BSC]
// 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)