Commit f6a464cb authored by Moody Salem's avatar Moody Salem

fix(ampl): do not swap ampl via pairs other than DAI/ETH

parent e589c751
...@@ -15,6 +15,7 @@ export const USDC = new Token(ChainId.MAINNET, '0xA0b86991c6218b36c1d19D4a2e9Eb0 ...@@ -15,6 +15,7 @@ export const USDC = new Token(ChainId.MAINNET, '0xA0b86991c6218b36c1d19D4a2e9Eb0
export const USDT = new Token(ChainId.MAINNET, '0xdAC17F958D2ee523a2206206994597C13D831ec7', 6, 'USDT', 'Tether USD') export const USDT = new Token(ChainId.MAINNET, '0xdAC17F958D2ee523a2206206994597C13D831ec7', 6, 'USDT', 'Tether USD')
export const COMP = new Token(ChainId.MAINNET, '0xc00e94Cb662C3520282E6f5717214004A7f26888', 18, 'COMP', 'Compound') export const COMP = new Token(ChainId.MAINNET, '0xc00e94Cb662C3520282E6f5717214004A7f26888', 18, 'COMP', 'Compound')
export const MKR = new Token(ChainId.MAINNET, '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2', 18, 'MKR', 'Maker') export const MKR = new Token(ChainId.MAINNET, '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2', 18, 'MKR', 'Maker')
export const AMPL = new Token(ChainId.MAINNET, '0xD46bA6D942050d489DBd938a2C909A5d5039A161', 9, 'AMPL', 'Ampleforth')
const WETH_ONLY: ChainTokenList = { const WETH_ONLY: ChainTokenList = {
[ChainId.MAINNET]: [WETH[ChainId.MAINNET]], [ChainId.MAINNET]: [WETH[ChainId.MAINNET]],
...@@ -30,6 +31,16 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = { ...@@ -30,6 +31,16 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
[ChainId.MAINNET]: [...WETH_ONLY[ChainId.MAINNET], DAI, USDC, USDT, COMP, MKR] [ChainId.MAINNET]: [...WETH_ONLY[ChainId.MAINNET], DAI, USDC, USDT, COMP, MKR]
} }
/**
* Some tokens can only be swapped via certain pairs, so we override the list of bases that are considered for these
* tokens.
*/
export const CUSTOM_BASES: { [chainId in ChainId]?: { [tokenAddress: string]: Token[] } } = {
[ChainId.MAINNET]: {
[AMPL.address]: [DAI, WETH[ChainId.MAINNET]]
}
}
// used for display in the default list when adding liquidity // used for display in the default list when adding liquidity
export const SUGGESTED_BASES: ChainTokenList = { export const SUGGESTED_BASES: ChainTokenList = {
...WETH_ONLY, ...WETH_ONLY,
......
...@@ -2,9 +2,8 @@ import { Currency, CurrencyAmount, Pair, Token, Trade } from '@uniswap/sdk' ...@@ -2,9 +2,8 @@ import { Currency, CurrencyAmount, Pair, Token, Trade } from '@uniswap/sdk'
import flatMap from 'lodash.flatmap' import flatMap from 'lodash.flatmap'
import { useMemo } from 'react' import { useMemo } from 'react'
import { BASES_TO_CHECK_TRADES_AGAINST } from '../constants' import { BASES_TO_CHECK_TRADES_AGAINST, CUSTOM_BASES } from '../constants'
import { PairState, usePairs } from '../data/Reserves' import { PairState, usePairs } from '../data/Reserves'
import { maxHopsFor } from '../utils/maxHopsFor'
import { wrappedCurrency } from '../utils/wrappedCurrency' import { wrappedCurrency } from '../utils/wrappedCurrency'
import { useActiveWeb3React } from './index' import { useActiveWeb3React } from './index'
...@@ -18,18 +17,36 @@ function useAllCommonPairs(currencyA?: Currency, currencyB?: Currency): Pair[] { ...@@ -18,18 +17,36 @@ function useAllCommonPairs(currencyA?: Currency, currencyB?: Currency): Pair[] {
? [wrappedCurrency(currencyA, chainId), wrappedCurrency(currencyB, chainId)] ? [wrappedCurrency(currencyA, chainId), wrappedCurrency(currencyB, chainId)]
: [undefined, undefined] : [undefined, undefined]
const allPairCombinations: [Token | undefined, Token | undefined][] = useMemo( const allPairCombinations: [Token, Token][] = useMemo(
() => [ () =>
// the direct pair [
[tokenA, tokenB], // the direct pair
// token A against all bases [tokenA, tokenB],
...bases.map((base): [Token | undefined, Token | undefined] => [tokenA, base]), // token A against all bases
// token B against all bases ...bases.map((base): [Token | undefined, Token | undefined] => [tokenA, base]),
...bases.map((base): [Token | undefined, Token | undefined] => [tokenB, base]), // token B against all bases
// each base against all bases ...bases.map((base): [Token | undefined, Token | undefined] => [tokenB, base]),
...flatMap(bases, (base): [Token, Token][] => bases.map(otherBase => [base, otherBase])) // each base against all bases
], ...flatMap(bases, (base): [Token, Token][] => bases.map(otherBase => [base, otherBase]))
[tokenA, tokenB, bases] ]
.filter((tokens): tokens is [Token, Token] => Boolean(tokens[0] && tokens[1]))
.filter(([tokenA, tokenB]) => {
if (!chainId) return true
const customBases = CUSTOM_BASES[chainId]
if (!customBases) return true
const customBasesA: Token[] | undefined = customBases[tokenA.address]
const customBasesB: Token[] | undefined = customBases[tokenB.address]
if (!customBasesA && !customBasesB) return true
if (customBasesA && customBasesA.findIndex(base => tokenB.equals(base)) === -1) return false
if (customBasesB && customBasesB.findIndex(base => tokenA.equals(base)) === -1) return false
console.log(tokenA, tokenB, customBasesA, customBasesB, 'allowing')
return true
}),
[tokenA, tokenB, bases, chainId]
) )
const allPairs = usePairs(allPairCombinations) const allPairs = usePairs(allPairCombinations)
...@@ -59,9 +76,8 @@ export function useTradeExactIn(currencyAmountIn?: CurrencyAmount, currencyOut?: ...@@ -59,9 +76,8 @@ export function useTradeExactIn(currencyAmountIn?: CurrencyAmount, currencyOut?:
return useMemo(() => { return useMemo(() => {
if (currencyAmountIn && currencyOut && allowedPairs.length > 0) { if (currencyAmountIn && currencyOut && allowedPairs.length > 0) {
const maxHops = maxHopsFor(currencyAmountIn.currency, currencyOut)
return ( return (
Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops, maxNumResults: 1 })[0] ?? null Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops: 3, maxNumResults: 1 })[0] ?? null
) )
} }
return null return null
...@@ -76,9 +92,9 @@ export function useTradeExactOut(currencyIn?: Currency, currencyAmountOut?: Curr ...@@ -76,9 +92,9 @@ export function useTradeExactOut(currencyIn?: Currency, currencyAmountOut?: Curr
return useMemo(() => { return useMemo(() => {
if (currencyIn && currencyAmountOut && allowedPairs.length > 0) { if (currencyIn && currencyAmountOut && allowedPairs.length > 0) {
const maxHops = maxHopsFor(currencyIn, currencyAmountOut.currency)
return ( return (
Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops, maxNumResults: 1 })[0] ?? null Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops: 3, maxNumResults: 1 })[0] ??
null
) )
} }
return null return null
......
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
}
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