1.2.4 • Published 10 days ago

@real-wagmi/smart-router v1.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days ago

Smart Router

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

Install

$ yarn add @real-wagmi/smart-router

Usage

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.

  1. Install other dependencies
$ yarn add viem graphql-request @real-wagmi/sdk
  1. 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 })
  1. 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,
})
  1. 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,
})
  1. 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)
1.2.4

10 days ago

1.2.3

16 days ago

1.2.2

1 month ago

1.2.1

1 month ago

1.2.0

1 month ago

1.1.12

2 months ago

1.1.11

3 months ago

1.1.1

4 months ago

1.0.2

6 months ago

1.1.0

4 months ago

1.0.18

4 months ago

1.0.1

6 months ago

1.0.17

4 months ago

1.0.0

6 months ago

1.0.16

5 months ago

1.1.9

3 months ago

1.1.8

3 months ago

1.0.9

6 months ago

1.1.7

4 months ago

1.0.8

6 months ago

1.1.6

4 months ago

1.0.7

6 months ago

1.1.5

4 months ago

1.0.6

6 months ago

1.1.4

4 months ago

1.0.5

6 months ago

1.1.3

4 months ago

1.0.4

6 months ago

1.1.2

4 months ago

1.0.3

6 months ago

1.1.10

3 months ago

1.0.11

6 months ago

1.0.10

6 months ago

1.0.15

5 months ago

1.0.14

5 months ago

1.0.13

5 months ago

1.0.12

5 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago