Commit f4bba5fc authored by Moody Salem's avatar Moody Salem

Make the #bestTradeExactIn and #bestTradeExactOut more strict about input pairs

parent 2d948bba
......@@ -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')
......@@ -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
......@@ -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')
......@@ -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
......
......@@ -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],
......@@ -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],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment