npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pintswap-v3/smart-router

v6.1.1

Published

A SDK for getting best routes from AMM

Downloads

65

Readme

Pinkswap Smart Router

@pintswap-v3/smart-router is a SDK for getting best trade routes from Pinkswap AMM.

Install

$ pnpm add @pintswap-v3/smart-router

Usage (V4Router)

NOTE: V4Router will be replaced by @pintswap-v3/routing-sdk in the future

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 @pintswap-v3/smart-router@5 viem@1 @pintswap-v3/sdk@5 @pintswap-v3/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 '@pintswap-v3/sdk'
import { V4Router } from '@pintswap-v3/smart-router'
import { bscTokens } from '@pintswap-v3/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 '@pintswap-v3/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 [email protected] @pintswap-v3/sdk @pintswap-v3/tokens
  1. Prepare on-chain rpc provider and subgraph providers
import { createPublicClient, http } from 'viem'
import { GraphQLClient } from 'graphql-request'
import { SmartRouter } from '@pintswap-v3/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/pinkswap/exchange-v3-bsc')
const v2SubgraphClient = new GraphQLClient('https://proxy-worker-api.pinkswap.com/bsc-exchange')

const quoteProvider = SmartRouter.createQuoteProvider({ onChainProvider: () => publicClient })
  1. Get candidate pools
import { Native } from '@pintswap-v3/sdk'
import { SmartRouter } from '@pintswap-v3/smart-router'
import { bscTokens } from '@pintswap-v3/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 '@pintswap-v3/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 { Percent } from '@pintswap-v3/sdk'
import { ChainId } from '@pintswap-v3/chains'
import { SmartRouter, SMART_ROUTER_ADDRESSES, SwapRouter } from '@pintswap-v3/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.