Skip to content

Commit e3b3d9e

Browse files
committed
fix(swaps): band-aid fix for gas estimates to disable multihop for eth-ampl
1 parent 3050e96 commit e3b3d9e

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/hooks/Trades.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useMemo } from 'react'
44

55
import { BASES_TO_CHECK_TRADES_AGAINST } from '../constants'
66
import { PairState, usePairs } from '../data/Reserves'
7+
import { maxHopsFor } from '../utils/maxHopsFor'
78
import { wrappedCurrency } from '../utils/wrappedCurrency'
89

910
import { useActiveWeb3React } from './index'
@@ -58,8 +59,9 @@ export function useTradeExactIn(currencyAmountIn?: CurrencyAmount, currencyOut?:
5859

5960
return useMemo(() => {
6061
if (currencyAmountIn && currencyOut && allowedPairs.length > 0) {
62+
const maxHops = maxHopsFor(currencyAmountIn.currency, currencyOut)
6163
return (
62-
Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops: 3, maxNumResults: 1 })[0] ?? null
64+
Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops, maxNumResults: 1 })[0] ?? null
6365
)
6466
}
6567
return null
@@ -74,9 +76,9 @@ export function useTradeExactOut(currencyIn?: Currency, currencyAmountOut?: Curr
7476

7577
return useMemo(() => {
7678
if (currencyIn && currencyAmountOut && allowedPairs.length > 0) {
79+
const maxHops = maxHopsFor(currencyIn, currencyAmountOut.currency)
7780
return (
78-
Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops: 3, maxNumResults: 1 })[0] ??
79-
null
81+
Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops, maxNumResults: 1 })[0] ?? null
8082
)
8183
}
8284
return null

src/utils/maxHopsFor.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ChainId, Currency, ETHER, Token, WETH } from '@uniswap/sdk'
2+
3+
function isEtherish(currency: Currency): boolean {
4+
return currency === ETHER || (currency instanceof Token && WETH[currency.chainId].equals(currency))
5+
}
6+
7+
const AMPL_TOKEN_ADDRESS = '0xD46bA6D942050d489DBd938a2C909A5d5039A161'
8+
9+
/**
10+
* Band-aid on maxHops because some tokens seems to always fail with multihop swaps
11+
* @param currencyIn currency in
12+
* @param currencyOut currency out
13+
*/
14+
export function maxHopsFor(currencyIn: Currency, currencyOut: Currency): number {
15+
if (
16+
isEtherish(currencyIn) &&
17+
currencyOut instanceof Token &&
18+
currencyOut.chainId === ChainId.MAINNET &&
19+
currencyOut.address === AMPL_TOKEN_ADDRESS
20+
) {
21+
return 1
22+
} else if (
23+
isEtherish(currencyOut) &&
24+
currencyIn instanceof Token &&
25+
currencyIn.chainId === ChainId.MAINNET &&
26+
currencyIn.address === AMPL_TOKEN_ADDRESS
27+
) {
28+
return 1
29+
}
30+
31+
return 3
32+
}

0 commit comments

Comments
 (0)