Skip to content

Commit

Permalink
usdc usdt swap issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaysngh-mudrex committed Feb 6, 2024
1 parent 9c5b368 commit 84bf29e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useAllV3Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function useAllV3Routes(
currencyOut?: Currency
): { loading: boolean; routes: any[] } {
const { chainId } = useAccountDetails()
const { pools, loading: poolsLoading } = useV3SwapPools(allPools)
const { pools, loading: poolsLoading } = useV3SwapPools(allPools, currencyIn, currencyOut)

// const [singleHopOnly] = useUserSingleHopOnly()
const singleHopOnly = true
Expand Down
20 changes: 14 additions & 6 deletions src/hooks/useBestV3Trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ export function useBestV3TradeExactIn(
}, [filteredAmountOutResults])

return useMemo(() => {
if (!routes.length) {
return {
state: TradeState.NO_ROUTE_FOUND,
trade: null,
}
}

if (!amountIn || !currencyOut || !filteredAmountOutResults) {
return {
state: TradeState.INVALID,
Expand All @@ -278,12 +285,6 @@ export function useBestV3TradeExactIn(
}
}

// const results = await filteredAmountOutResults
// const bestRoute = results?.bestRoute
// const amountOut = results?.amountOut

// bestRoute = results?.bestRoute;

if (!bestRoute || !amountOut) {
return {
state: TradeState.NO_ROUTE_FOUND,
Expand Down Expand Up @@ -530,6 +531,13 @@ export function useBestV3TradeExactOut(
}, [filteredAmountInResults])

return useMemo(() => {
if (!routes.length) {
return {
state: TradeState.NO_ROUTE_FOUND,
trade: null,
}
}

if (!amountOut || !currencyIn || !filteredAmountInResults) {
return {
state: TradeState.INVALID,
Expand Down
32 changes: 16 additions & 16 deletions src/hooks/usePools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,22 @@ export function usePools(
}

export function usePoolsForSwap(results: any): [PoolState, Pool | null][] {
// return useMemo(() => {
return results.map((result: any) => {
const { tickCurrent, liquidity, sqrtPriceX96, token0, token1, fee } = result
const sqrtPriceHex = sqrtPriceX96 && JSBI.BigInt(num.toHex(sqrtPriceX96 as BigNumberish))
const liquidityHex = Boolean(liquidity) ? JSBI.BigInt(num.toHex(liquidity as BigNumberish)) : JSBI.BigInt('0x0')

if (!tickCurrent || !liquidityHex || !sqrtPriceHex) return [PoolState.NOT_EXISTS, null]
try {
const pool = PoolCache.getPool(token0, token1, fee, sqrtPriceHex, liquidityHex, tickCurrent)
return [PoolState.EXISTS, pool]
} catch (error) {
console.error('Error when constructing the pool', error)
return [PoolState.NOT_EXISTS, null]
}
})
// }, [results, poolKeys, poolTokens])
return useMemo(() => {
return results.map((result: any) => {
const { tickCurrent, liquidity, sqrtPriceX96, token0, token1, fee } = result
const sqrtPriceHex = sqrtPriceX96 && JSBI.BigInt(num.toHex(sqrtPriceX96 as BigNumberish))
const liquidityHex = Boolean(liquidity) ? JSBI.BigInt(num.toHex(liquidity as BigNumberish)) : JSBI.BigInt('0x0')

if (!tickCurrent || !liquidityHex || !sqrtPriceHex) return [PoolState.NOT_EXISTS, null]
try {
const pool = PoolCache.getPool(token0, token1, fee, sqrtPriceHex, liquidityHex, tickCurrent)
return [PoolState.EXISTS, pool]
} catch (error) {
console.error('Error when constructing the pool', error)
return [PoolState.NOT_EXISTS, null]
}
})
}, [results])
}

export function usePool(
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useV3SwapPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ const getPoolProps = (address: string) => {
return { liquidity, sqrtPriceX96, tickCurrent, token0, token1, fee: Number(fee) }
}

export function useV3SwapPools(allPools: string[]): {
export function useV3SwapPools(
allPools: string[],
currencyIn?: Currency,
currencyOut?: Currency
): {
pools: Pool[]
loading: boolean
} {
Expand Down

0 comments on commit 84bf29e

Please sign in to comment.