bsc-dex-helper
v1.0.0
Published
BSC DEX (PancakeSwap V2) helpers for trading bots: quotes, swap params, slippage
Maintainers
Readme
bsc-dex-helper
BSC DEX helpers for trading bots: PancakeSwap V2 router/factory addresses, getAmountsOut/getAmountsIn quotes, and slippage utilities.
Install
npm install bsc-dex-helperUse with a BSC provider (e.g. bsc-provider-helper):
npm install bsc-provider-helper bsc-dex-helperUsage
const { getProvider } = require('bsc-provider-helper');
const {
getRouterV2,
getWbnb,
getAmountsOut,
getAmountsIn,
quoteOut,
applySlippageBps,
} = require('bsc-dex-helper');
const provider = getProvider();
const WBNB = getWbnb(56);
const BUSD = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56'; // BSC mainnet example
// Quote: 1 BNB -> expected BUSD out
const oneBnb = 10n ** 18n;
const amounts = await getAmountsOut(provider, oneBnb, [WBNB, BUSD]);
const expectedOut = amounts[amounts.length - 1];
// Min amount out with 0.5% slippage (50 bps)
const minOut = applySlippageBps(expectedOut, 50);
// Required amount in for 1000 BUSD out
const wantOut = 1000n * 10n ** 18n;
const requiredIn = await quoteIn(provider, wantOut, WBNB, BUSD);Subpath imports
const { getRouterV2, getWbnb } = require('bsc-dex-helper/addresses');
const { getAmountsOut, quoteOut } = require('bsc-dex-helper/quotes');
const { applySlippageBps, addSlippageBps } = require('bsc-dex-helper/slippage');API
Addresses (lib/addresses.js)
| Method / Constant | Description |
|------------------|-------------|
| getRouterV2(chainId?) | PancakeSwap V2 router address (BSC 56 / testnet 97) |
| getFactoryV2(chainId?) | PancakeSwap V2 factory address |
| getWbnb(chainId?) | WBNB token address |
| ROUTER_V2, FACTORY_V2, WBNB_MAINNET, WBNB_TESTNET | Raw address maps |
Quotes (lib/quotes.js)
| Method | Description |
|--------|-------------|
| getAmountsOut(provider, amountIn, path, chainId?) | Router getAmountsOut; returns array of amounts (last = expected out) |
| getAmountsIn(provider, amountOut, path, chainId?) | Router getAmountsIn; returns array (first = required in) |
| quoteOut(provider, amountIn, tokenIn, tokenOut, chainId?) | Single-hop expected amountOut |
| quoteIn(provider, amountOut, tokenIn, tokenOut, chainId?) | Single-hop required amountIn |
Slippage (lib/slippage.js)
| Method | Description |
|--------|-------------|
| applySlippageBps(amount, slippageBps) | Min amount after slippage (e.g. 50 bps = 0.5%) |
| applySlippagePercent(amount, slippagePercent) | Same using percentage (e.g. 0.5) |
| addSlippageBps(amount, slippageBps) | Max amount (e.g. for amountInMax) |
Dependencies
ethers^6.x
Optional peer: bsc-provider-helper for getProvider() when using this in a bot.
License
MIT
