@pulsadev/defi-router
v0.1.0
Published
DEX swap router — optimal path finding, multi-hop routing, price quotes, calldata generation. Zero dependencies, pure RPC.
Downloads
42
Readme
@pulsadev/defi-router
DEX swap router — optimal path finding, multi-hop routing, price quotes, multi-DEX comparison, calldata generation. Zero dependencies, pure RPC.
Find the best swap route across multiple DEXes and paths. Build transaction calldata ready to submit. No SDK required.
Features
- Optimal path finding — direct and multi-hop routes via intermediate tokens
- Multi-DEX comparison — compare quotes across Uniswap, SushiSwap, PancakeSwap, etc.
- Price quotes — getAmountsOut / getAmountsIn with accurate fee calculation
- Swap calldata generation — build ready-to-submit transaction data
- Price impact calculation — know your slippage before swapping
- 7 chains / 10+ DEXes — Ethereum, BSC, Polygon, Arbitrum, Optimism, Base, Avalanche
- Auto chain detection — pass an RPC URL, DEX config loads automatically
- Built-in intermediate tokens — WETH, USDC, USDT, DAI per chain for multi-hop routing
- Uniswap V2 math — pure JS constant product AMM calculations
- Zero dependencies — ~12 KB bundled, ESM + CJS, pure TypeScript
Install
npm install @pulsadev/defi-routerQuick Start
Get a swap quote
import { DefiRouter } from '@pulsadev/defi-router'
const router = new DefiRouter({ rpcUrl: 'https://ethereum-rpc.publicnode.com' })
const quote = await router.getQuote(
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
1000000000000000000n, // 1 ETH
)
console.log(`Best output: ${quote.bestAmountOut} USDC`)
console.log(`Via: ${quote.bestRoute.dex.name}`)
console.log(`Path: ${quote.bestRoute.path.join(' → ')}`)
console.log(`Price impact: ${quote.bestRoute.priceImpact}%`)
console.log(`Routes found: ${quote.routes.length}`)Compare across DEXes
const best = await router.findBestDex(WETH, USDC, 1000000000000000000n)
console.log(`Best DEX: ${best.dex.name} — ${best.amountOut} out`)Build swap calldata
const calldata = router.buildSwapCalldata({
tokenIn: WETH,
tokenOut: USDC,
amountIn: 1000000000000000000n,
slippageBps: 50, // 0.5%
recipient: '0xYourAddress...',
}, quote.bestRoute)
// Ready to submit
console.log(`To: ${calldata.to}`)
console.log(`Data: ${calldata.data}`)
console.log(`Value: ${calldata.value}`)Use on any chain
// Auto-detects chain and loads DEX config
const bscRouter = new DefiRouter({ rpcUrl: 'https://bsc-dataseed.binance.org' })
const polygonRouter = new DefiRouter({ rpcUrl: 'https://polygon-bor-rpc.publicnode.com', chainId: 137 })API
new DefiRouter(options)
| Option | Type | Description |
|--------|------|-------------|
| rpcUrl | string | RPC endpoint URL |
| chainId | number | Chain ID (auto-detected if omitted) |
| dexes | DexConfig[] | Custom DEX configs (auto-loaded if omitted) |
| intermediateTokens | Address[] | Tokens for multi-hop (auto-loaded if omitted) |
Methods
| Method | Description |
|--------|-------------|
| getQuote(tokenIn, tokenOut, amountIn) | Find all routes and return best quote |
| getAmountsOut(dex, path, amountIn) | Calculate output amounts along a path |
| findBestDex(tokenIn, tokenOut, amountIn) | Compare single-hop across all DEXes |
| buildSwapCalldata(params, route) | Build swapExactTokensForTokens calldata |
| buildSwapExactETHCalldata(params, route) | Build swapExactETHForTokens calldata |
| pairExists(dex, tokenA, tokenB) | Check if a pair exists |
| getDexes() | Get configured DEX list |
| getIntermediateTokens() | Get intermediate token list |
Standalone utilities
| Function | Description |
|----------|-------------|
| getAmountOut(amountIn, reserveIn, reserveOut, feeBps) | Uniswap V2 output calculation |
| getAmountIn(amountOut, reserveIn, reserveOut, feeBps) | Uniswap V2 input calculation |
| getPriceImpact(amountIn, reserveIn) | Calculate price impact % |
| getPairAddress(rpcUrl, factory, tokenA, tokenB) | Query pair address from factory |
| getPairReserves(rpcUrl, pairAddress) | Get pair reserves |
| sortTokens(tokenA, tokenB) | Sort token pair |
Supported DEXes
| Chain | DEXes | |-------|-------| | Ethereum (1) | Uniswap V2, SushiSwap | | BSC (56) | PancakeSwap V2 | | Polygon (137) | QuickSwap, SushiSwap | | Arbitrum (42161) | SushiSwap | | Optimism (10) | Velodrome | | Base (8453) | Uniswap V2 | | Avalanche (43114) | TraderJoe |
License
MIT © Yuto Nakamura
