Handle insufficient reserves/input errors without crashing

This commit is contained in:
Moody Salem 2020-05-09 12:41:21 -04:00
parent f1ccd7a166
commit ee92df1554
No known key found for this signature in database
GPG Key ID: 8CB5CD10385138DB

@ -21,7 +21,12 @@ export function useTradeExactIn(amountIn?: TokenAmount, tokenOut?: Token): Trade
const allPairs = [pairBetween, aToETH, bToETH].filter(p => !!p)
if (amountIn && allPairs.length > 0 && tokenOut) {
return Trade.bestTradeExactIn(allPairs, amountIn, tokenOut)[0] ?? null
try {
// TODO(moodysalem): remove when the insufficient reserves/input errors do not throw exceptions
return Trade.bestTradeExactIn(allPairs, amountIn, tokenOut)[0] ?? null
} catch (error) {
return null
}
}
return null
}, [aToETH, bToETH, pairBetween, amountIn, tokenOut])
@ -44,7 +49,12 @@ export function useTradeExactOut(tokenIn?: Token, amountOut?: TokenAmount): Trad
const allPairs = [pairBetween, aToETH, bToETH].filter(p => !!p)
if (amountOut && allPairs.length > 0 && tokenIn) {
return Trade.bestTradeExactOut(allPairs, tokenIn, amountOut)[0] ?? null
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
}, [pairBetween, aToETH, bToETH, amountOut, tokenIn])