uniswapv2-path-optimizer v1.2.2
uniswapv2-path-optimizer
Optimal swap path finder for UniswapV2-based AMM DEX model
Installation
npm i uniswapv2-path-optimizerInitialize
import Optimizer from "uniswapv2-path-optimizer";
const provider = new providers.JsonRpcProvider("RPC_NODE_URL");
const UniswapFactoryAddress = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f";
const UniswapRouterAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
const optimizer = new Optimizer({
provider,
factoryAddress: UniswapFactoryAddress,
routerAddress: UniswapRouterAddress,
feeBps: 30 // optional, default 30 (0.3%)
multicallAddress: "0x..." // optional for some major chains
});
const WETH = "0x...";
const USDC = "0x...";
// and so on...
// method `optimizer.init` is async function to get token & pair info from blockchain.
// initiate for target tokens,
// Each element of the array is address of ERC20 tokens
await optimizer.init([WETH, USDC, USDT, DAI, WBTC, BNB, UNI, SUSHI]);Optimal Path for largest amountOut
const optimalResult = optimizer.getOptimalInPathOffChain({
from: USDC, // address of ERC20 token
to: WBTC, // address of ERC20 token
amountIn: '1000000',
maxLength: 4
});Optimal Path for smallest amountIn
const optimalResult = optimizer.getOptimalInPathOffChain({
from: WBTC, // address of ERC20 token
to: USDC, // address of ERC20 token
amountOut: '1000000',
maxLength: 4
});Refresh tokens & pairs
await optimizer.refresh();Core Entities
UniswapV2PathOptimizer
functions for get tokens & pools
ready(): boolean
Check fetching tokens/pools info process is finished.tokens(): Token[]
Returns array of all tokenspools(): Pool[]
Returns array of all pools(pairs)getToken(id: number): Tokenget specific token by index of tokens arraygetTokenByAddress(address: address): Token|undefined
Get specific token by addressgetTokenId(address: address): number
Get token's array indexgetPoolByTokenId(tokenAId: number, tokenBId: number): Pool
Get specific pool by index of pools arraygetPoolByAddress(tokenA: address, tokenB: address): Pool
Get specific pool by pair tokens' addresses. It doesn't matter the order of the tokens is changed.
functions for setting
async init(tokens: address[]): Promise<void>
fetch tokens & pools info from blockchainasync refresh()
refresh pools' reserved amountssetFee(tokenA: address, tokenB: address, newFeeBps: number)
change the feeBps of pool
Calculate path on-chain
async getOutPathsOnChain(props: GetOutPathParams):Promise<AmountsOutResult[]>
Calculate all possible paths ofgetAmountsOutfunction from onChain.async getOptimalOutPathOnChain(props: GetOutPathParams):Promise<AmountsOutResult>
Returns the path with the most optimal value among the results of thegetOutPathsOnChainfunction.async getInPathsOnChain(props: GetInPathParams):Promise<AmountsInResult[]>
Calculate all possible paths ofgetAmountsInfunction from onChain.async getOptimalInPathOnChain(props: GetOptimalInPathParams):Promise<AmountsInResult>
Returns the path with the most optimal value among the results of thegetInPathsOnChainfunction.
Calculate path off-chain
getOutPathsOffChain(props: GetOutPathParams):AmountsOutResult[]
Calculate all possible paths ofgetAmountsOutfunction from offChain(client-side).getOptimalOutPathOffChain(props: GetOptimalOutPathParams):AmountsOutResult
Returns the path with the most optimal value among the results of thegetOutPathsOffChainfunction.getInPathsOffChain(props: GetInPathParams):AmountsInResult[]
Calculate all possible paths ofgetAmountsInfunction from offChain(client-side).getOptimalInPathOffChain(props: GetOptimalInPathParams):AmountsInResult
Returns the path with the most optimal value among the results of thegetInPathsOffChainfunction.
Basic calculator
quote(tokenInId: number, tokenOutId: number, amountIn: BigNumberish): BigNumbergetAmountOut(tokenAId: number, tokenBId: number, amountIn: BigNumberish, priceImpact?:boolean)priceImpact: booleanif false, calculates without considering the fee.default: truegetAmountIn(tokenAId: number, tokenBId: number, amountOut: BigNumberish, priceImpact?:boolean)priceImpact: booleanif false, calculates without considering the fee.default: truegetAmountsOut(amountIn: BigNumberish, path: number[], priceImpact?:boolean): BigNumber[]priceImpact: booleanif false, calculates without considering the fee.default: truegetAmountsIn(amountOut: BigNumberish, path: number[], priceImpact?:boolean): BigNumber[]priceImpact: booleanif false, calculates without considering the fee.default: true
PathResult
extended by AmountsInResult, AmountsOutResult
Properties
| property | type |
|---|---|
| path | TokenWithAmount[] |
| amountIn | BigNumber |
| amountOut | BigNumber |
Methods
format(): String[]
returns array of formatted strings from amounts inpathtokens using tokens' own decimals.formatWithoutPriceImpact(): String[]
returns array of formatted strings from amountsWithoutPriceImpact inpathtokens using tokens' own decimals.priceImpactBps(): number
returns price impact when swapping withpathin basis point.
ex) 1234 => 12.34% (0.1234)amountInWithToken():TokenWithAmount
returns amountIn amount with "from" token info.amountOutWithToken():TokenWithAmount
returns amountOut amount with "to" token info.
TokenWithAmount
Properties
| property | type |
|---|---|
| address | string |
| symbol | string |
| decimals | number |
| amount | BigNumber |
| amountWithoutPriceImpact | BigNumber |
Methods
format(): string
returns formatted string fromamountusingdecimals.formatWithoutPriceImpact(): stringreturns formatted string fromamountWithoutPriceImpactusingdecimals.
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago