Skip to content

Commit

Permalink
Some small edits to wording (Uniswap#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Jul 2, 2020
1 parent 35d6c34 commit d8cc586
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/entities/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { Price } from './fractions/price'
import { Pair } from './pair'
import { Route } from './route'

function getSlippage(midPrice: Price, inputAmount: TokenAmount, outputAmount: TokenAmount): Percent {
// returns the percent difference between the mid price and the execution price
// we call this price impact in the UI
function computePriceImpact(midPrice: Price, inputAmount: TokenAmount, outputAmount: TokenAmount): Percent {
const exactQuote = midPrice.raw.multiply(inputAmount.raw)
// calculate slippage := (exactQuote - outputAmount) / exactQuote
const slippage = exactQuote.subtract(outputAmount.raw).divide(exactQuote)
Expand Down Expand Up @@ -56,9 +58,9 @@ export function tradeComparator(a: Trade, b: Trade) {
}

// consider lowest slippage next, since these are less likely to fail
if (a.slippage.lessThan(b.slippage)) {
if (a.priceImpact.lessThan(b.priceImpact)) {
return -1
} else if (a.slippage.greaterThan(b.slippage)) {
} else if (a.priceImpact.greaterThan(b.priceImpact)) {
return 1
}

Expand All @@ -78,9 +80,17 @@ export class Trade {
public readonly tradeType: TradeType
public readonly inputAmount: TokenAmount
public readonly outputAmount: TokenAmount
// the price expressed in terms of output/input
public readonly executionPrice: Price
// the mid price after the trade executes assuming zero slippage
public readonly nextMidPrice: Price
public readonly slippage: Percent
// the percent difference between the mid price before the trade and the price after the trade
public readonly priceImpact: Percent

// this is a misnomer for price impact, but kept for compatibility
public get slippage(): Percent {
return this.priceImpact
}

public constructor(route: Route, amount: TokenAmount, tradeType: TradeType) {
invariant(amount.token.equals(tradeType === TradeType.EXACT_INPUT ? route.input : route.output), 'TOKEN')
Expand Down Expand Up @@ -112,7 +122,7 @@ export class Trade {
this.outputAmount = outputAmount
this.executionPrice = new Price(route.input, route.output, inputAmount.raw, outputAmount.raw)
this.nextMidPrice = Price.fromRoute(new Route(nextPairs, route.input))
this.slippage = getSlippage(route.midPrice, inputAmount, outputAmount)
this.priceImpact = computePriceImpact(route.midPrice, inputAmount, outputAmount)
}

// get the minimum amount that must be received from this trade for the given slippage tolerance
Expand Down
6 changes: 3 additions & 3 deletions test/entities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('entities', () => {
expect(trade.nextMidPrice.toSignificant(18)).toEqual('1.38958368072925352')
expect(trade.nextMidPrice.invert().toSignificant(18)).toEqual('0.71964')

expect(trade.slippage.toSignificant(18)).toEqual('16.8751042187760547')
expect(trade.priceImpact.toSignificant(18)).toEqual('16.8751042187760547')
})

it('TradeType.EXACT_OUTPUT', () => {
Expand All @@ -142,7 +142,7 @@ describe('entities', () => {
expect(trade.nextMidPrice.toSignificant(18)).toEqual('1.38958368072925352')
expect(trade.nextMidPrice.invert().toSignificant(18)).toEqual('0.71964')

expect(trade.slippage.toSignificant(18)).toEqual('16.8751042187760547')
expect(trade.priceImpact.toSignificant(18)).toEqual('16.8751042187760547')
})

it('minimum TradeType.EXACT_INPUT', () => {
Expand All @@ -163,7 +163,7 @@ describe('entities', () => {
const outputAmount = new TokenAmount(tokens[1], '1')
const trade = new Trade(route, outputAmount, TradeType.EXACT_INPUT)

expect(trade.slippage.toSignificant(18)).toEqual(
expect(trade.priceImpact.toSignificant(18)).toEqual(
tokens[1].decimals === 9 ? '0.300000099400899902' : '0.3000000000000001'
)
}
Expand Down

0 comments on commit d8cc586

Please sign in to comment.