Commit ca2b84ec authored by Moody Salem's avatar Moody Salem

get the best route in v3 for an exact in swap

parent ac7cf35b
......@@ -27,7 +27,6 @@ function computeAllRoutes(
const outputToken = pool.token0.equals(tokenIn) ? pool.token1 : pool.token0
if (outputToken.equals(tokenOut)) {
console.log(startCurrencyIn, [...currentPath, pool], currencyOut)
allPaths.push(new Route([...currentPath, pool], startCurrencyIn, currencyOut))
} else if (maxHops > 1) {
computeAllRoutes(
......
import { Token, ChainId, Currency, CurrencyAmount, TokenAmount } from '@uniswap/sdk-core'
import { Route } from '@uniswap/v3-sdk'
import { Pool } from '@uniswap/v3-sdk/dist/'
import { useMemo } from 'react'
import { useSingleContractMultipleData } from '../state/multicall/hooks'
import { wrappedCurrency } from '../utils/wrappedCurrency'
import { useActiveWeb3React } from './index'
import { useAllV3Routes } from './useAllV3Routes'
import { useV3Quoter } from './useContract'
import { BigNumber, utils } from 'ethers'
/**
* Converts a route to a path
* @param route the v3 path to convert to an encoded path
* @param chainId the current chain ID, used to wrap the route's input currency
*/
function routeToPath(route: Route, chainId: ChainId): string {
const firstInputToken = wrappedCurrency(route.input, chainId)
if (!firstInputToken) throw new Error('Could not wrap input currency')
return route.pools.reduce(
(
{ inputToken, path }: { inputToken: Token; path: string },
pool: Pool,
index
): { inputToken: Token; path: string } => {
const outputToken: Token = pool.token0.equals(inputToken) ? pool.token1 : pool.token0
if (index === 0) {
return {
inputToken: outputToken,
path: utils.solidityPack(
['address', 'uint24', 'address'],
[inputToken.address, pool.fee, outputToken.address]
),
}
} else {
return {
inputToken: outputToken,
path: `${path}${utils.solidityPack(['uint24', 'address'], [pool.fee, outputToken.address]).slice(2)}`,
}
}
},
{ inputToken: firstInputToken, path: '' }
).path
}
export function useBestV3RouteExactIn(
amountIn?: CurrencyAmount,
currencyOut?: Currency
): { route: Route; amountOut: CurrencyAmount } | null {
const { chainId } = useActiveWeb3React()
const quoter = useV3Quoter()
const routes = useAllV3Routes(amountIn?.currency, currencyOut)
const paths = useMemo(() => {
if (!chainId) return []
return routes.map((route) => routeToPath(route, chainId))
}, [chainId, routes])
const quoteInputs = useMemo(() => {
return paths.map((path) => [path, amountIn ? `0x${amountIn.raw.toString(16)}` : undefined])
}, [amountIn, paths])
const quotesResults = useSingleContractMultipleData(quoter, 'quoteExactInput', quoteInputs)
return useMemo(() => {
const { bestRoute, amountOut } = quotesResults.reduce(
(best: { bestRoute: Route | null; amountOut: BigNumber | null }, { valid, loading, result }, i) => {
if (loading || !valid || !result) return best
if (best.amountOut === null) {
return {
bestRoute: routes[i],
amountOut: result.amountOut,
}
} else if (best.amountOut.lt(result.amountOut)) {
return {
bestRoute: routes[i],
amountOut: result.amountOut,
}
}
return best
},
{
bestRoute: null,
amountOut: null,
}
)
if (!bestRoute || !amountOut) return null
return {
route: bestRoute,
amountOut:
currencyOut instanceof Token
? new TokenAmount(currencyOut, amountOut.toString())
: CurrencyAmount.ether(amountOut.toString()),
}
}, [currencyOut, quotesResults, routes])
}
import { useBestV3RouteExactIn } from '../../hooks/useBestV3Route'
import useENS from '../../hooks/useENS'
import { parseUnits } from '@ethersproject/units'
import { Currency, CurrencyAmount, ETHER, Token, TokenAmount } from '@uniswap/sdk-core'
......@@ -138,6 +139,10 @@ export function useDerivedSwapInfo(): {
const bestTradeExactIn = useV2TradeExactIn(isExactIn ? parsedAmount : undefined, outputCurrency ?? undefined)
const bestTradeExactOut = useV2TradeExactOut(inputCurrency ?? undefined, !isExactIn ? parsedAmount : undefined)
const bestRouteExactInV3 = useBestV3RouteExactIn(isExactIn ? parsedAmount : undefined, outputCurrency ?? undefined)
// todo: do something with this information
console.log('best v3 route for the swap', bestRouteExactInV3)
const v2Trade = isExactIn ? bestTradeExactIn : bestTradeExactOut
const currencyBalances = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment