6.0.6 • Published 5 days ago

@pancakeswap/smart-router v6.0.6

Weekly downloads
-
License
-
Repository
github
Last release
5 days ago

Pancakeswap Smart Router

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

Install

$ pnpm add @pancakeswap/smart-router

Usage (V4Router)

V4Router is utilize the new routing strategy introduced in smart router v5. Use BSC as an example. Here's how we use v4 router to find the best trade route swapping from BNB to CAKE.

For working code example, please refer to v4 router usage example.

  1. Install other dependencies
$ pnpm add @pancakeswap/smart-router@5 viem@1 @pancakeswap/sdk@5 @pancakeswap/tokens
  1. Prepare on-chain rpc provider
import { createPublicClient, http } from 'viem'

const client = createPublicClient({
  chain: mainnet,
  transport: http('https://bsc-dataseed1.binance.org'),
  batch: {
    multicall: {
      batchSize: 1024 * 200,
    },
  },
})
  1. Get candidate pools
import { Native } from '@pancakeswap/sdk'
import { V4Router } from '@pancakeswap/smart-router'
import { bscTokens } from '@pancakeswap/tokens'

const swapFrom = Native.onChain(chainId)
const swapTo = bscTokens.cake

const v3Pools = await V4Router.getV3CandidatePools({
  clientProvider: () => client,
  currencyA: swapFrom,
  currencyB: swapTo,
})
  1. Find the best swap trade route
import { CurrencyAmount, TradeType } from '@pancakeswap/sdk'

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

const trade = await V4Router.getBestTrade(amount, swapTo, TradeType.EXACT_INPUT, {
  gasPriceWei: () => client.getGasPrice(),
  candidatePools: v3Pools,
})

Usage (SmartRouter)

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.

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

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 })
  1. Get candidate pools
import { Native } from '@pancakeswap/sdk'
import { SmartRouter } from '@pancakeswap/smart-router'
import { bscTokens } from '@pancakeswap/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,
  }),
])
  1. Find the best swap trade route
import { CurrencyAmount, TradeType } from '@pancakeswap/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,
})
  1. Build the swap transaction from trade
import { ChainId } from '@pancakeswap/chains'
import { SmartRouter, SMART_ROUTER_ADDRESSES, SwapRouter } from '@pancakeswap/smart-router'
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)

FAQ

  1. What's the difference between SmartRouter and V4Router?

SmartRouter is getting swap quotes by querying quoter contract on chain, while V4Router is getting the liquidity pool data from on chain and do the swap quote calculation off chain.

Compared to SmartRouter, V4Router has better quoting performance since the calculation is done off chain. Also, V4Router doesn't rely on subgraph for price reference. However, the downside of V4Router is that the estimated quote may not be as accurate as on chain quoter due to pool data out dated or potential extra fee applied during the swap.

6.0.6

5 days ago

6.0.5

8 days ago

6.0.4

14 days ago

6.0.3

23 days ago

6.0.2

27 days ago

6.0.1

2 months ago

6.0.0

2 months ago

5.1.8

2 months ago

5.1.7

2 months ago

5.1.6

2 months ago

5.1.5

2 months ago

5.1.4

2 months ago

5.1.3

2 months ago

5.1.2

2 months ago

5.1.1

2 months ago

5.1.0

2 months ago

5.0.4

2 months ago

5.0.3

2 months ago

5.0.2

2 months ago

5.0.1

2 months ago

5.0.0

2 months ago

4.14.10

3 months ago

4.14.8

3 months ago

4.14.9

3 months ago

4.14.7

3 months ago

4.14.6

3 months ago

4.14.5

3 months ago

4.14.4

3 months ago

4.14.3

3 months ago

4.14.2

3 months ago

4.14.1

4 months ago

4.12.4

4 months ago

4.14.0

4 months ago

4.13.0

4 months ago

4.12.3

4 months ago

4.12.2

4 months ago

4.12.0

5 months ago

4.12.1

5 months ago

4.11.1

5 months ago

4.9.8

6 months ago

4.9.7

6 months ago

4.9.9

6 months ago

4.9.4

7 months ago

4.9.3

7 months ago

4.9.6

7 months ago

4.9.5

7 months ago

4.9.0

8 months ago

4.3.6

9 months ago

4.7.1

9 months ago

4.3.5

9 months ago

4.9.2

7 months ago

4.9.1

7 months ago

4.4.0

9 months ago

4.6.0

9 months ago

4.0.1

10 months ago

4.2.1

10 months ago

4.2.0

10 months ago

4.10.1

6 months ago

4.10.0

6 months ago

4.8.8

8 months ago

4.8.5

8 months ago

4.8.4

8 months ago

4.8.7

8 months ago

4.8.6

8 months ago

4.8.1

8 months ago

4.8.0

8 months ago

4.9.10

6 months ago

4.8.2

8 months ago

4.5.0

9 months ago

4.3.2

10 months ago

4.3.1

10 months ago

4.5.2

9 months ago

4.3.4

9 months ago

4.5.1

9 months ago

4.3.3

9 months ago

4.1.0

10 months ago

4.3.0

10 months ago

4.11.0

5 months ago

3.0.0

1 year ago

4.0.0

11 months ago

1.0.0

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

0.5.0

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago