Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Fix calculation for Trade.minimumAmountOut #44

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
40 changes: 20 additions & 20 deletions src/entities/trade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,31 +830,29 @@ 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)
})
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)
)
})
})

Expand Down Expand Up @@ -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)
})
Expand All @@ -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(
Expand All @@ -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()
})
})

Expand Down Expand Up @@ -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))
})
})

Expand All @@ -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))
})
})

Expand All @@ -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))
})
})
})
Expand All @@ -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)
})
Expand All @@ -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', () => {
Expand Down
8 changes: 3 additions & 5 deletions src/entities/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,12 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
* @returns The amount out
*/
public minimumAmountOut(slippageTolerance: Percent, amountOut = this.outputAmount): CurrencyAmount<TOutput> {
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)
}
}
Expand Down