Skip to content

Commit

Permalink
chore: add error handing with uniswap sdk (DimensionDev#5642)
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-0229 authored Feb 14, 2022
1 parent 6f710a0 commit 0778acf
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 62 deletions.
6 changes: 5 additions & 1 deletion packages/mask/src/plugins/Trader/helpers/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ export function toUniswapCurrencyAmount(chainId: ChainId, token?: FungibleTokenD
if (!token || !amount) return
const currency = toUniswapCurrency(chainId, token)
if (!currency) return
if (isGreaterThan(amount, 0)) return CurrencyAmount.fromRawAmount(currency, JSBI.BigInt(amount))
try {
if (isGreaterThan(amount, 0)) return CurrencyAmount.fromRawAmount(currency, JSBI.BigInt(amount))
} catch {
return
}
return
}

Expand Down
36 changes: 20 additions & 16 deletions packages/mask/src/plugins/Trader/trader/uniswap/useAllV3Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ function computeAllRoutes(
const tokenOut = currencyOut?.wrapped
if (!tokenIn || !tokenOut) throw new Error('Missing tokenIn/tokenOut')

for (const pool of pools) {
if (currentPath.includes(pool) || !pool.involvesToken(tokenIn)) continue
try {
for (const pool of pools) {
if (currentPath.includes(pool) || !pool.involvesToken(tokenIn)) continue

const outputToken = pool.token0.equals(tokenIn) ? pool.token1 : pool.token0
if (outputToken.equals(tokenOut)) {
allPaths.push(new Route([...currentPath, pool], startCurrencyIn, currencyOut))
} else if (maxHops > 1) {
computeAllRoutes(
outputToken,
currencyOut,
pools,
chainId,
[...currentPath, pool],
allPaths,
startCurrencyIn,
maxHops - 1,
)
const outputToken = pool.token0.equals(tokenIn) ? pool.token1 : pool.token0
if (outputToken.equals(tokenOut)) {
allPaths.push(new Route([...currentPath, pool], startCurrencyIn, currencyOut))
} else if (maxHops > 1) {
computeAllRoutes(
outputToken,
currencyOut,
pools,
chainId,
[...currentPath, pool],
allPaths,
startCurrencyIn,
maxHops - 1,
)
}
}
} catch {
return []
}

return allPaths
Expand Down
32 changes: 18 additions & 14 deletions packages/mask/src/plugins/Trader/trader/uniswap/usePairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,24 @@ export function usePairs(tradeProvider: TradeProvider, tokenPairs: readonly Toke
// compose pairs from list of reserves
const pairs = useMemo(() => {
return listOfPairAddress.map((address, i) => {
const tokenA = tokenPairs[i][0]
const tokenB = tokenPairs[i][1]
if (!tokenA || !tokenB || tokenA.equals(tokenB)) return [PairState.INVALID, null]
const { reserve0, reserve1 } =
listOfReserves.find((x) => x.id.toLowerCase() === address?.toLowerCase()) ?? {}
if (!reserve0 || !reserve1) return [PairState.NOT_EXISTS, null]
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]
return [
PairState.EXISTS,
new Pair(
CurrencyAmount.fromRawAmount(token0, reserve0),
CurrencyAmount.fromRawAmount(token1, reserve1),
),
] as const
try {
const tokenA = tokenPairs[i][0]
const tokenB = tokenPairs[i][1]
if (!tokenA || !tokenB || tokenA.equals(tokenB)) return [PairState.INVALID, null]
const { reserve0, reserve1 } =
listOfReserves.find((x) => x.id.toLowerCase() === address?.toLowerCase()) ?? {}
if (!reserve0 || !reserve1) return [PairState.NOT_EXISTS, null]
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]
return [
PairState.EXISTS,
new Pair(
CurrencyAmount.fromRawAmount(token0, reserve0),
CurrencyAmount.fromRawAmount(token1, reserve1),
),
] as const
} catch {
return []
}
})
}, [listOfPairAddress, listOfReserves, tokenPairs])

Expand Down
22 changes: 13 additions & 9 deletions packages/mask/src/plugins/Trader/trader/uniswap/usePools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ export function usePools(
}, [chainId, poolKeys])

const poolAddresses = useMemo(() => {
return transformed.map((value) => {
if (!context?.IS_UNISWAP_V3_LIKE) return ''
if (!context?.FACTORY_CONTRACT_ADDRESS || !value) return ''
return computePoolAddress({
factoryAddress: context.FACTORY_CONTRACT_ADDRESS,
tokenA: value[0],
tokenB: value[1],
fee: value[2],
try {
return transformed.map((value) => {
if (!context?.IS_UNISWAP_V3_LIKE) return ''
if (!context?.FACTORY_CONTRACT_ADDRESS || !value) return ''
return computePoolAddress({
factoryAddress: context.FACTORY_CONTRACT_ADDRESS,
tokenA: value[0],
tokenB: value[1],
fee: value[2],
})
})
})
} catch {
return []
}
}, [chainId, transformed, context?.FACTORY_CONTRACT_ADDRESS])

const poolContracts = usePoolContracts(poolAddresses, chainId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ function computeRealizedLPFeePercent(trade: Trade): Percent {

// computes price breakdown for the trade
function computeRealizedLPFeeAmount(trade?: Trade | null): CurrencyAmount<Currency> | undefined {
if (trade) {
const realizedLPFee = computeRealizedLPFeePercent(trade)
try {
if (trade) {
const realizedLPFee = computeRealizedLPFeePercent(trade)

// the amount of the input that accrues to LPs
return CurrencyAmount.fromRawAmount(
trade.inputAmount.currency,
trade.inputAmount.multiply(realizedLPFee).quotient,
)
// the amount of the input that accrues to LPs
return CurrencyAmount.fromRawAmount(
trade.inputAmount.currency,
trade.inputAmount.multiply(realizedLPFee).quotient,
)
}
return undefined
} catch {
return undefined
}
return undefined
}

export function useTradeBreakdown(trade: Trade | null) {
Expand Down
36 changes: 22 additions & 14 deletions packages/mask/src/plugins/Trader/trader/uniswap/useV3BestTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ export function useV3BestTradeExactIn(
const quoterContract = useQuoterContract(targetChainId)
const { routes, loading: routesLoading } = useAllV3Routes(amountIn?.currency, currencyOut)
const quoteExactInInputs = useMemo(() => {
return routes.map(
(route) =>
[encodeRouteToPath(route, false), amountIn ? `0x${amountIn.quotient.toString(16)}` : undefined] as [
string,
string,
],
)
try {
return routes.map(
(route) =>
[encodeRouteToPath(route, false), amountIn ? `0x${amountIn.quotient.toString(16)}` : undefined] as [
string,
string,
],
)
} catch {
return []
}
}, [amountIn, routes])

const { value: blockNumber } = useTargetBlockNumber(targetChainId)
Expand Down Expand Up @@ -149,13 +153,17 @@ export function useV3BestTradeExactOut(
const { targetChainId } = TargetChainIdContext.useContainer()
const quoterContract = useQuoterContract(targetChainId)
const quoteExactOutInputs = useMemo(() => {
return routes.map(
(route) =>
[encodeRouteToPath(route, true), amountOut ? `0x${amountOut.quotient.toString(16)}` : undefined] as [
string,
string,
],
)
try {
return routes.map(
(route) =>
[
encodeRouteToPath(route, true),
amountOut ? `0x${amountOut.quotient.toString(16)}` : undefined,
] as [string, string],
)
} catch {
return []
}
}, [amountOut, routes])

const { value: blockNumber } = useTargetBlockNumber(targetChainId)
Expand Down

0 comments on commit 0778acf

Please sign in to comment.