Skip to content

Commit

Permalink
Fix reliability of error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed May 9, 2020
1 parent dcd2fcc commit e5b779f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/entities/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e5b779f

Please sign in to comment.