Commit f0a6fdb9 authored by Moody Salem's avatar Moody Salem

Don't crash for zero liquidity pairs

parent 9da0e6a6
...@@ -166,6 +166,7 @@ export class Trade { ...@@ -166,6 +166,7 @@ export class Trade {
const pair = pairs[i] const pair = pairs[i]
// pair irrelevant // pair irrelevant
if (!pair.token0.equals(amountIn.token) && !pair.token1.equals(amountIn.token)) continue if (!pair.token0.equals(amountIn.token) && !pair.token1.equals(amountIn.token)) continue
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue
let amountOut: TokenAmount let amountOut: TokenAmount
try { try {
...@@ -234,6 +235,7 @@ export class Trade { ...@@ -234,6 +235,7 @@ export class Trade {
const pair = pairs[i] const pair = pairs[i]
// pair irrelevant // pair irrelevant
if (!pair.token0.equals(amountOut.token) && !pair.token1.equals(amountOut.token)) continue if (!pair.token0.equals(amountOut.token) && !pair.token1.equals(amountOut.token)) continue
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue
let amountIn: TokenAmount let amountIn: TokenAmount
try { try {
......
...@@ -13,6 +13,8 @@ describe('Trade', () => { ...@@ -13,6 +13,8 @@ describe('Trade', () => {
const pair_1_2 = new Pair(new TokenAmount(token1, JSBI.BigInt(1200)), new TokenAmount(token2, JSBI.BigInt(1000))) const pair_1_2 = new Pair(new TokenAmount(token1, JSBI.BigInt(1200)), new TokenAmount(token2, JSBI.BigInt(1000)))
const pair_1_3 = new Pair(new TokenAmount(token1, JSBI.BigInt(1200)), new TokenAmount(token3, JSBI.BigInt(1300))) const pair_1_3 = new Pair(new TokenAmount(token1, JSBI.BigInt(1200)), new TokenAmount(token3, JSBI.BigInt(1300)))
const empty_pair_0_1 = new Pair(new TokenAmount(token0, JSBI.BigInt(0)), new TokenAmount(token1, JSBI.BigInt(0)))
describe('#bestTradeExactIn', () => { describe('#bestTradeExactIn', () => {
it('throws with empty pairs', () => { it('throws with empty pairs', () => {
expect(() => Trade.bestTradeExactIn([], new TokenAmount(token0, JSBI.BigInt(100)), token2)).toThrow('PAIRS') expect(() => Trade.bestTradeExactIn([], new TokenAmount(token0, JSBI.BigInt(100)), token2)).toThrow('PAIRS')
...@@ -40,6 +42,12 @@ describe('Trade', () => { ...@@ -40,6 +42,12 @@ describe('Trade', () => {
expect(result[1].outputAmount).toEqual(new TokenAmount(token2, JSBI.BigInt(69))) expect(result[1].outputAmount).toEqual(new TokenAmount(token2, JSBI.BigInt(69)))
}) })
it('doesnt throw for zero liquidity pairs', () => {
expect(Trade.bestTradeExactIn([empty_pair_0_1], new TokenAmount(token0, JSBI.BigInt(100)), token1)).toHaveLength(
0
)
})
it('respects maxHops', () => { it('respects maxHops', () => {
const result = Trade.bestTradeExactIn( const result = Trade.bestTradeExactIn(
[pair_0_1, pair_0_2, pair_1_2], [pair_0_1, pair_0_2, pair_1_2],
...@@ -224,6 +232,12 @@ describe('Trade', () => { ...@@ -224,6 +232,12 @@ describe('Trade', () => {
expect(result[1].outputAmount).toEqual(new TokenAmount(token2, JSBI.BigInt(100))) expect(result[1].outputAmount).toEqual(new TokenAmount(token2, JSBI.BigInt(100)))
}) })
it('doesnt throw for zero liquidity pairs', () => {
expect(Trade.bestTradeExactOut([empty_pair_0_1], token1, new TokenAmount(token1, JSBI.BigInt(100)))).toHaveLength(
0
)
})
it('respects maxHops', () => { it('respects maxHops', () => {
const result = Trade.bestTradeExactOut( const result = Trade.bestTradeExactOut(
[pair_0_1, pair_0_2, pair_1_2], [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