From 90b73948cbf71fdfb2bcfe095994ec3524c08684 Mon Sep 17 00:00:00 2001 From: Josh DeCristi Date: Sat, 13 May 2023 21:53:07 -0400 Subject: [PATCH 1/2] fix: incorrect calculation for the Trade.minimumAmountOut() --- src/entities/trade.test.ts | 40 +++++++++++++++++++------------------- src/entities/trade.ts | 8 +++----- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/entities/trade.test.ts b/src/entities/trade.test.ts index 2d9a550..17ee222 100644 --- a/src/entities/trade.test.ts +++ b/src/entities/trade.test.ts @@ -830,6 +830,10 @@ describe('Trade', () => { expect(() => exactInV3.worstExecutionPrice(new Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE') expect(() => exactInMixed.worstExecutionPrice(new Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE') }) + it('throws if greater than 100%', () => { + expect(() => exactInV3.worstExecutionPrice(new Percent(200, 100))).toThrow('SLIPPAGE_TOLERANCE') + expect(() => exactInMixed.worstExecutionPrice(new Percent(200, 100))).toThrow('SLIPPAGE_TOLERANCE') + }) it('returns exact if 0', () => { expect(exactInV3.worstExecutionPrice(new Percent(0, 100))).toEqual(exactInV3.executionPrice) expect(exactInMixed.worstExecutionPrice(new Percent(0, 100))).toEqual(exactInV3.executionPrice) @@ -837,24 +841,18 @@ describe('Trade', () => { it('returns exact if nonzero', () => { expect(exactInV3.worstExecutionPrice(new Percent(0, 100))).toEqual(new Price(token0, token2, 100, 69)) expect(exactInV3.worstExecutionPrice(new Percent(5, 100))).toEqual(new Price(token0, token2, 100, 65)) - expect(exactInV3.worstExecutionPrice(new Percent(200, 100))).toEqual(new Price(token0, token2, 100, 23)) expect(exactInMixed.worstExecutionPrice(new Percent(0, 100))).toEqual(new Price(token0, token2, 100, 69)) expect(exactInMixed.worstExecutionPrice(new Percent(5, 100))).toEqual(new Price(token0, token2, 100, 65)) - expect(exactInMixed.worstExecutionPrice(new Percent(200, 100))).toEqual(new Price(token0, token2, 100, 23)) }) it('returns exact if nonzero with multiple routes', () => { expect(exactInMultiRoute.worstExecutionPrice(new Percent(0, 100))).toEqual(new Price(token0, token2, 100, 69)) expect(exactInMultiRoute.worstExecutionPrice(new Percent(5, 100))).toEqual(new Price(token0, token2, 100, 65)) - expect(exactInMultiRoute.worstExecutionPrice(new Percent(200, 100))).toEqual(new Price(token0, token2, 100, 23)) expect(exactInMultiMixedRoute.worstExecutionPrice(new Percent(0, 100))).toEqual( new Price(token0, token2, 100, 69) ) expect(exactInMultiMixedRoute.worstExecutionPrice(new Percent(5, 100))).toEqual( new Price(token0, token2, 100, 65) ) - expect(exactInMultiMixedRoute.worstExecutionPrice(new Percent(200, 100))).toEqual( - new Price(token0, token2, 100, 23) - ) }) }) @@ -894,6 +892,9 @@ describe('Trade', () => { it('throws if less than 0', () => { expect(() => exactOut.worstExecutionPrice(new Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE') }) + it('throws if greater than 100%', () => { + expect(() => exactOut.worstExecutionPrice(new Percent(200, 100))).toThrow('SLIPPAGE_TOLERANCE') + }) it('returns exact if 0', () => { expect(exactOut.worstExecutionPrice(new Percent(0, 100))).toEqual(exactOut.executionPrice) }) @@ -904,9 +905,6 @@ describe('Trade', () => { expect( exactOut.worstExecutionPrice(new Percent(5, 100)).equalTo(new Price(token0, token2, 163, 100)) ).toBeTruthy() - expect( - exactOut.worstExecutionPrice(new Percent(200, 100)).equalTo(new Price(token0, token2, 468, 100)) - ).toBeTruthy() }) it('returns exact if nonzero with multiple routes', () => { expect( @@ -915,9 +913,6 @@ describe('Trade', () => { expect( exactOutMultiRoute.worstExecutionPrice(new Percent(5, 100)).equalTo(new Price(token0, token2, 163, 100)) ).toBeTruthy() - expect( - exactOutMultiRoute.worstExecutionPrice(new Percent(200, 100)).equalTo(new Price(token0, token2, 468, 100)) - ).toBeTruthy() }) }) @@ -952,13 +947,15 @@ describe('Trade', () => { it('throws if less than 0', () => { expect(() => exactIn.worstExecutionPrice(new Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE') }) + it('throws if greater than 100%', () => { + expect(() => exactIn.worstExecutionPrice(new Percent(200, 100))).toThrow('SLIPPAGE_TOLERANCE') + }) it('returns exact if 0', () => { expect(exactIn.worstExecutionPrice(new Percent(0, 100))).toEqual(exactIn.executionPrice) }) it('returns exact if nonzero', () => { expect(exactIn.worstExecutionPrice(new Percent(0, 100))).toEqual(new Price(token0, token2, 350, 250)) - expect(exactIn.worstExecutionPrice(new Percent(5, 100))).toEqual(new Price(token0, token2, 350, 238)) - expect(exactIn.worstExecutionPrice(new Percent(200, 100))).toEqual(new Price(token0, token2, 350, 83)) + expect(exactIn.worstExecutionPrice(new Percent(5, 100))).toEqual(new Price(token0, token2, 350, 237)) }) }) @@ -985,13 +982,15 @@ describe('Trade', () => { it('throws if less than 0', () => { expect(() => exactIn.worstExecutionPrice(new Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE') }) + it('throws if greater than 100%', () => { + expect(() => exactIn.worstExecutionPrice(new Percent(200, 100))).toThrow('SLIPPAGE_TOLERANCE') + }) it('returns exact if 0', () => { expect(exactIn.worstExecutionPrice(new Percent(0, 100))).toEqual(exactIn.executionPrice) }) it('returns exact if nonzero', () => { expect(exactIn.worstExecutionPrice(new Percent(0, 100))).toEqual(new Price(token0, token2, 256, 200)) expect(exactIn.worstExecutionPrice(new Percent(5, 100))).toEqual(new Price(token0, token2, 256, 190)) - expect(exactIn.worstExecutionPrice(new Percent(200, 100))).toEqual(new Price(token0, token2, 256, 66)) }) }) @@ -1018,13 +1017,15 @@ describe('Trade', () => { it('throws if less than 0', () => { expect(() => exactOut.worstExecutionPrice(new Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE') }) + it('throws if greater than 100%', () => { + expect(() => exactOut.worstExecutionPrice(new Percent(200, 100))).toThrow('SLIPPAGE_TOLERANCE') + }) it('returns exact if 0', () => { expect(exactOut.worstExecutionPrice(new Percent(0, 100))).toEqual(exactOut.executionPrice) }) it('returns exact if nonzero', () => { expect(exactOut.worstExecutionPrice(new Percent(0, 100))).toEqual(new Price(token0, token2, 256, 200)) expect(exactOut.worstExecutionPrice(new Percent(5, 100))).toEqual(new Price(token0, token2, 268, 200)) - expect(exactOut.worstExecutionPrice(new Percent(200, 100))).toEqual(new Price(token0, token2, 768, 200)) }) }) }) @@ -1049,7 +1050,9 @@ describe('Trade', () => { it('throws if less than 0', () => { expect(() => trade.minimumAmountOut(new Percent(JSBI.BigInt(-1), 100))).toThrow('SLIPPAGE_TOLERANCE') }) - + it('throws if greater than 100%', () => { + expect(() => trade.minimumAmountOut(new Percent(JSBI.BigInt(-1), 100))).toThrow('SLIPPAGE_TOLERANCE') + }) it('returns exact if 0', () => { expect(trade.minimumAmountOut(new Percent(JSBI.BigInt(0), 100))).toEqual(trade.outputAmount) }) @@ -1058,9 +1061,6 @@ describe('Trade', () => { expect(trade.minimumAmountOut(new Percent(JSBI.BigInt(5), 100))).toEqual( CurrencyAmount.fromRawAmount(token1, 285) // 300 * 0.95 ) - expect(trade.minimumAmountOut(new Percent(JSBI.BigInt(200), 100))).toEqual( - CurrencyAmount.fromRawAmount(token1, 100) - ) }) describe('tradeType = EXACT_OUTPUT', () => { diff --git a/src/entities/trade.ts b/src/entities/trade.ts index 730f630..fe639b8 100644 --- a/src/entities/trade.ts +++ b/src/entities/trade.ts @@ -189,14 +189,12 @@ export class Trade { - invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE') + invariant(!(slippageTolerance.lessThan(ZERO) || slippageTolerance.greaterThan(ONE)), 'SLIPPAGE_TOLERANCE') if (this.tradeType === TradeType.EXACT_OUTPUT) { return amountOut } else { - const slippageAdjustedAmountOut = new Fraction(ONE) - .add(slippageTolerance) - .invert() - .multiply(amountOut.quotient).quotient + const slippageAdjustedAmountOut = new Fraction(ONE).subtract(slippageTolerance).multiply(amountOut).quotient + return CurrencyAmount.fromRawAmount(amountOut.currency, slippageAdjustedAmountOut) } } From c13cf06ec36e91821a63f8e278555c0ee1968c7c Mon Sep 17 00:00:00 2001 From: Josh DeCristi Date: Sat, 13 May 2023 21:53:20 -0400 Subject: [PATCH 2/2] 1.4.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c0556d8..74b09d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uniswap/router-sdk", - "version": "1.4.0", + "version": "1.4.1", "description": "An sdk for routing swaps using Uniswap v2 and Uniswap v3.", "publishConfig": { "access": "public"