Skip to content

Commit

Permalink
fix(swaps): band-aid fix for gas estimates to disable multihop for et…
Browse files Browse the repository at this point in the history
…h-ampl
  • Loading branch information
moodysalem committed Jul 31, 2020
1 parent 3050e96 commit e3b3d9e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/hooks/Trades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useMemo } from 'react'

import { BASES_TO_CHECK_TRADES_AGAINST } from '../constants'
import { PairState, usePairs } from '../data/Reserves'
import { maxHopsFor } from '../utils/maxHopsFor'
import { wrappedCurrency } from '../utils/wrappedCurrency'

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

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

return useMemo(() => {
if (currencyIn && currencyAmountOut && allowedPairs.length > 0) {
const maxHops = maxHopsFor(currencyIn, currencyAmountOut.currency)
return (
Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops: 3, maxNumResults: 1 })[0] ??
null
Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops, maxNumResults: 1 })[0] ?? null
)
}
return null
Expand Down
32 changes: 32 additions & 0 deletions src/utils/maxHopsFor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ChainId, Currency, ETHER, Token, WETH } from '@uniswap/sdk'

function isEtherish(currency: Currency): boolean {
return currency === ETHER || (currency instanceof Token && WETH[currency.chainId].equals(currency))
}

const AMPL_TOKEN_ADDRESS = '0xD46bA6D942050d489DBd938a2C909A5d5039A161'

/**
* Band-aid on maxHops because some tokens seems to always fail with multihop swaps
* @param currencyIn currency in
* @param currencyOut currency out
*/
export function maxHopsFor(currencyIn: Currency, currencyOut: Currency): number {
if (
isEtherish(currencyIn) &&
currencyOut instanceof Token &&
currencyOut.chainId === ChainId.MAINNET &&
currencyOut.address === AMPL_TOKEN_ADDRESS
) {
return 1
} else if (
isEtherish(currencyOut) &&
currencyIn instanceof Token &&
currencyIn.chainId === ChainId.MAINNET &&
currencyIn.address === AMPL_TOKEN_ADDRESS
) {
return 1
}

return 3
}

1 comment on commit e3b3d9e

@vercel
Copy link

@vercel vercel bot commented on e3b3d9e Jul 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.