From e5b779f6ea78b93ef2f8e5f599728e8346e467b3 Mon Sep 17 00:00:00 2001 From: Moody Salem Date: Sat, 9 May 2020 14:19:28 -0400 Subject: [PATCH] Fix reliability of error checking --- src/entities/trade.ts | 6 +++--- src/errors.ts | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/entities/trade.ts b/src/entities/trade.ts index 0ed8f224c..3355261bf 100644 --- a/src/entities/trade.ts +++ b/src/entities/trade.ts @@ -8,7 +8,6 @@ import { Price } from './fractions/price' import { Percent } from './fractions/percent' import { Token } from 'entities/token' import { sortedInsert } from '../utils' -import { InsufficientReservesError, InsufficientInputAmountError } from '../errors' function getSlippage(midPrice: Price, inputAmount: TokenAmount, outputAmount: TokenAmount): Percent { const exactQuote = midPrice.raw.multiply(inputAmount.raw) @@ -143,7 +142,8 @@ export class Trade { try { ;[amountOut] = pair.getOutputAmount(amountIn) } catch (error) { - if (error instanceof InsufficientInputAmountError) { + // input too low + if (error.isInsufficientInputAmountError) { continue } throw error @@ -211,7 +211,7 @@ export class Trade { ;[amountIn] = pair.getInputAmount(amountOut) } catch (error) { // not enough liquidity in this pair - if (error instanceof InsufficientReservesError) { + if (error.isInsufficientReservesError) { continue } throw error diff --git a/src/errors.ts b/src/errors.ts index 72e54a552..9edab47f9 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -2,6 +2,8 @@ const CAN_SET_PROTOTYPE = 'setPrototypeOf' in Object export class InsufficientReservesError extends Error { + public readonly isInsufficientReservesError: true = true + public constructor() { super() this.name = this.constructor.name @@ -10,6 +12,8 @@ export class InsufficientReservesError extends Error { } export class InsufficientInputAmountError extends Error { + public readonly isInsufficientInputAmountError: true = true + public constructor() { super() this.name = this.constructor.name