1.4.0 • Published 8 months ago

@real-wagmi/smart-router v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months 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.4.0

8 months ago

1.2.8

9 months ago

1.2.10

9 months ago

1.2.9

9 months ago

1.2.7

10 months ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.1

1 year ago

1.0.2

2 years ago

1.1.0

1 year ago

1.0.18

1 year ago

1.0.1

2 years ago

1.0.17

1 year ago

1.0.0

2 years ago

1.0.16

2 years ago

1.1.9

1 year ago

1.1.8

1 year ago

1.0.9

2 years ago

1.1.7

1 year ago

1.0.8

2 years ago

1.1.6

1 year ago

1.0.7

2 years ago

1.1.5

1 year ago

1.0.6

2 years ago

1.1.4

1 year ago

1.0.5

2 years ago

1.1.3

1 year ago

1.0.4

2 years ago

1.1.2

1 year ago

1.0.3

2 years ago

1.1.10

1 year ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago