Commit 0733cdd6 authored by Moody Salem's avatar Moody Salem

Fix trade routing to not throw

parent 8a61b4e4
...@@ -114,7 +114,7 @@ const CircleWrapper = styled.div` ...@@ -114,7 +114,7 @@ const CircleWrapper = styled.div`
const LowerSection = styled.div` const LowerSection = styled.div`
${({ theme }) => theme.flexColumnNoWrap} ${({ theme }) => theme.flexColumnNoWrap}
padding: 2rem; padding: 1.5rem;
flex-grow: 1; flex-grow: 1;
overflow: auto; overflow: auto;
background-color: ${({ theme }) => theme.bg2}; background-color: ${({ theme }) => theme.bg2};
......
import { useMemo } from 'react' import { useMemo } from 'react'
import { WETH, Token, TokenAmount, Trade, ChainId } from '@uniswap/sdk' import { WETH, Token, TokenAmount, Trade, ChainId, Pair } from '@uniswap/sdk'
import { useWeb3React } from './index' import { useWeb3React } from './index'
import { usePair } from '../data/Reserves' import { usePair } from '../data/Reserves'
/**
* Returns the best trade for the exact amount of tokens in to the given token out
*/
const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18, 'DAI', 'Dai Stablecoin') const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18, 'DAI', 'Dai Stablecoin')
const USDC = new Token(ChainId.MAINNET, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 'USDC', 'USD//C') const USDC = new Token(ChainId.MAINNET, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 'USDC', 'USD//C')
export function useTradeExactIn(amountIn?: TokenAmount, tokenOut?: Token): Trade | null { function useAllCommonPairs(tokenA?: Token, tokenB?: Token): Pair[] {
const inputToken = amountIn?.token
const outputToken = tokenOut
const { chainId } = useWeb3React() const { chainId } = useWeb3React()
// check for direct pair between tokens // check for direct pair between tokens
const pairBetween = usePair(inputToken, outputToken) const pairBetween = usePair(tokenA, tokenB)
// get token<->WETH pairs // get token<->WETH pairs
const aToETH = usePair(inputToken, WETH[chainId]) const aToETH = usePair(tokenA, WETH[chainId])
const bToETH = usePair(outputToken, WETH[chainId]) const bToETH = usePair(tokenB, WETH[chainId])
// get token<->DAI pairs // get token<->DAI pairs
const aToDAI = usePair(inputToken, chainId === ChainId.MAINNET ? DAI : null) const aToDAI = usePair(tokenA, chainId === ChainId.MAINNET ? DAI : null)
const bToDAI = usePair(outputToken, chainId === ChainId.MAINNET ? DAI : null) const bToDAI = usePair(tokenB, chainId === ChainId.MAINNET ? DAI : null)
// get token<->USDC pairs // get token<->USDC pairs
const aToUSDC = usePair(inputToken, chainId === ChainId.MAINNET ? USDC : null) const aToUSDC = usePair(tokenA, chainId === ChainId.MAINNET ? USDC : null)
const bToUSDC = usePair(outputToken, chainId === ChainId.MAINNET ? USDC : null) const bToUSDC = usePair(tokenB, chainId === ChainId.MAINNET ? USDC : null)
return useMemo(() => [pairBetween, aToETH, bToETH, aToDAI, bToDAI, aToUSDC, bToUSDC].filter(p => !!p), [
pairBetween,
aToETH,
bToETH,
aToDAI,
bToDAI,
aToUSDC,
bToUSDC
])
}
return useMemo(() => { /**
const allPairs = [pairBetween, aToETH, bToETH, aToDAI, bToDAI, aToUSDC, bToUSDC].filter(p => !!p) * Returns the best trade for the exact amount of tokens in to the given token out
*/
export function useTradeExactIn(amountIn?: TokenAmount, tokenOut?: Token): Trade | null {
const inputToken = amountIn?.token
const outputToken = tokenOut
const allowedPairs = useAllCommonPairs(inputToken, outputToken)
if (amountIn && tokenOut && allPairs.length > 0) { return useMemo(() => {
try { if (amountIn && tokenOut && allowedPairs.length > 0) {
// TODO(moodysalem): remove when the insufficient reserves/input errors do not throw exceptions return Trade.bestTradeExactIn(allowedPairs, amountIn, tokenOut)[0] ?? null
return Trade.bestTradeExactIn(allPairs, amountIn, tokenOut)[0] ?? null
} catch (error) {
return null
}
} }
return null return null
}, [pairBetween, aToETH, bToETH, aToDAI, bToDAI, aToUSDC, bToUSDC, amountIn, tokenOut]) }, [allowedPairs, amountIn, tokenOut])
} }
/** /**
...@@ -51,34 +58,12 @@ export function useTradeExactOut(tokenIn?: Token, amountOut?: TokenAmount): Trad ...@@ -51,34 +58,12 @@ export function useTradeExactOut(tokenIn?: Token, amountOut?: TokenAmount): Trad
const inputToken = tokenIn const inputToken = tokenIn
const outputToken = amountOut?.token const outputToken = amountOut?.token
const { chainId } = useWeb3React() const allowedPairs = useAllCommonPairs(inputToken, outputToken)
// check for direct pair between tokens
const pairBetween = usePair(amountOut?.token, tokenIn)
// get token<->WETH pairs
const aToETH = usePair(inputToken, WETH[chainId])
const bToETH = usePair(outputToken, WETH[chainId])
// get token<->DAI pairs
const aToDAI = usePair(inputToken, chainId === ChainId.MAINNET ? DAI : null)
const bToDAI = usePair(outputToken, chainId === ChainId.MAINNET ? DAI : null)
// get token<->USDC pairs
const aToUSDC = usePair(inputToken, chainId === ChainId.MAINNET ? USDC : null)
const bToUSDC = usePair(outputToken, chainId === ChainId.MAINNET ? USDC : null)
return useMemo(() => { return useMemo(() => {
const allPairs = [pairBetween, aToETH, bToETH, aToDAI, bToDAI, aToUSDC, bToUSDC].filter(p => !!p) if (tokenIn && amountOut && allowedPairs.length > 0) {
return Trade.bestTradeExactOut(allowedPairs, tokenIn, amountOut)[0] ?? null
if (tokenIn && amountOut && allPairs.length > 0) {
try {
// TODO(moodysalem): remove when the insufficient reserves/input errors do not throw exceptions
return Trade.bestTradeExactOut(allPairs, tokenIn, amountOut)[0] ?? null
} catch (error) {
return null
}
} }
return null return null
}, [pairBetween, aToETH, bToETH, aToDAI, bToDAI, aToUSDC, bToUSDC, tokenIn, amountOut]) }, [allowedPairs, tokenIn, amountOut])
} }
...@@ -3116,10 +3116,10 @@ ...@@ -3116,10 +3116,10 @@
resolved "https://registry.yarnpkg.com/@uniswap/lib/-/lib-1.1.1.tgz#0afd29601846c16e5d082866cbb24a9e0758e6bc" resolved "https://registry.yarnpkg.com/@uniswap/lib/-/lib-1.1.1.tgz#0afd29601846c16e5d082866cbb24a9e0758e6bc"
integrity sha512-2yK7sLpKIT91TiS5sewHtOa7YuM8IuBXVl4GZv2jZFys4D2sY7K5vZh6MqD25TPA95Od+0YzCVq6cTF2IKrOmg== integrity sha512-2yK7sLpKIT91TiS5sewHtOa7YuM8IuBXVl4GZv2jZFys4D2sY7K5vZh6MqD25TPA95Od+0YzCVq6cTF2IKrOmg==
"@uniswap/sdk@^2.0.4": "@uniswap/sdk@^2.0.5":
version "2.0.4" version "2.0.5"
resolved "https://registry.yarnpkg.com/@uniswap/sdk/-/sdk-2.0.4.tgz#19392e166e8306c26809d83d03ad06b8bcf91f95" resolved "https://registry.yarnpkg.com/@uniswap/sdk/-/sdk-2.0.5.tgz#ca88c1ac36d75c669950747ffce00677e722832a"
integrity sha512-5M0ov0orjoCnLBy1TycxQjJdWLnxhM2ChZPRIipI8c0S1RtRchMHNmRlEdyfmIeBCvyjse4OqEF30DxR4ZL6dg== integrity sha512-5DSd303KQPi/j7gMp5phHHuC0TNspXOXYAF8OzJ3QFnAfIcNSVRuHiBsEXGL2+XeUDyqBQMJ5ItUJC7rW8Rxnw==
dependencies: dependencies:
"@ethersproject/address" "^5.0.0-beta.134" "@ethersproject/address" "^5.0.0-beta.134"
"@ethersproject/contracts" "^5.0.0-beta.143" "@ethersproject/contracts" "^5.0.0-beta.143"
......
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