Skip to content

Commit

Permalink
Make the #bestTradeExactIn and #bestTradeExactOut more strict about i…
Browse files Browse the repository at this point in the history
…nput pairs
  • Loading branch information
moodysalem committed May 6, 2020
1 parent 2d948bb commit f4bba5f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/entities/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ export class Trade {
originalAmountIn: TokenAmount = amountIn,
bestTrades: Trade[] = []
): Trade[] {
if (pairs.length === 0) {
return bestTrades
}

invariant(pairs.length > 0, 'PAIRS')
invariant(maxHops > 0, 'MAX_HOPS')
invariant(originalAmountIn === amountIn || currentPairs.length > 0, 'INVALID_RECURSION')

Expand Down Expand Up @@ -145,7 +142,7 @@ export class Trade {
maxNumResults,
inputOutputComparator
)
} else if (maxHops > 1) {
} else if (maxHops > 1 && pairs.length > 1) {
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length))

// otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
Expand Down Expand Up @@ -182,10 +179,7 @@ export class Trade {
originalAmountOut: TokenAmount = amountOut,
bestTrades: Trade[] = []
): Trade[] {
if (pairs.length === 0) {
return bestTrades
}

invariant(pairs.length > 0, 'PAIRS')
invariant(maxHops > 0, 'MAX_HOPS')
invariant(originalAmountOut === amountOut || currentPairs.length > 0, 'INVALID_RECURSION')

Expand All @@ -212,7 +206,7 @@ export class Trade {
maxNumResults,
inputOutputComparator
)
} else if (maxHops > 1) {
} else if (maxHops > 1 && pairs.length > 1) {
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length))

// otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
Expand Down
18 changes: 18 additions & 0 deletions test/trade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe('Trade', () => {
const pair_1_3 = new Pair(new TokenAmount(token1, JSBI.BigInt(1200)), new TokenAmount(token3, JSBI.BigInt(1300)))

describe('#bestTradeExactIn', () => {
it('throws with empty pairs', () => {
expect(() => Trade.bestTradeExactIn([], new TokenAmount(token0, JSBI.BigInt(100)), token2)).toThrow('PAIRS')
})
it('throws with max hops of 0', () => {
expect(() =>
Trade.bestTradeExactIn([pair_0_2], new TokenAmount(token0, JSBI.BigInt(100)), token2, { maxHops: 0 })
).toThrow('MAX_HOPS')
})

it('provides best route', () => {
const result = Trade.bestTradeExactIn(
[pair_0_1, pair_0_2, pair_1_2],
Expand Down Expand Up @@ -77,6 +86,15 @@ describe('Trade', () => {
})

describe('#bestTradeExactOut', () => {
it('throws with empty pairs', () => {
expect(() => Trade.bestTradeExactOut([], token0, new TokenAmount(token2, JSBI.BigInt(100)))).toThrow('PAIRS')
})
it('throws with max hops of 0', () => {
expect(() =>
Trade.bestTradeExactOut([pair_0_2], token0, new TokenAmount(token2, JSBI.BigInt(100)), { maxHops: 0 })
).toThrow('MAX_HOPS')
})

it('provides best route', () => {
const result = Trade.bestTradeExactOut(
[pair_0_1, pair_0_2, pair_1_2],
Expand Down

0 comments on commit f4bba5f

Please sign in to comment.