diff --git a/src/entities/trade.ts b/src/entities/trade.ts index b37b6072d..7ace5e7d7 100644 --- a/src/entities/trade.ts +++ b/src/entities/trade.ts @@ -166,6 +166,7 @@ export class Trade { const pair = pairs[i] // pair irrelevant 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 try { @@ -234,6 +235,7 @@ export class Trade { const pair = pairs[i] // pair irrelevant 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 try { diff --git a/test/trade.test.ts b/test/trade.test.ts index 8ab336c91..e2134d83c 100644 --- a/test/trade.test.ts +++ b/test/trade.test.ts @@ -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_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', () => { it('throws with empty pairs', () => { expect(() => Trade.bestTradeExactIn([], new TokenAmount(token0, JSBI.BigInt(100)), token2)).toThrow('PAIRS') @@ -40,6 +42,12 @@ describe('Trade', () => { 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', () => { const result = Trade.bestTradeExactIn( [pair_0_1, pair_0_2, pair_1_2], @@ -224,6 +232,12 @@ describe('Trade', () => { 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', () => { const result = Trade.bestTradeExactOut( [pair_0_1, pair_0_2, pair_1_2],