Skip to content

Commit

Permalink
add methods for getting mid price to the pair
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Jul 13, 2020
1 parent fe859b3 commit 4e048df
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/entities/pair.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Price } from './fractions/price'
import { TokenAmount } from './fractions/tokenAmount'
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'
Expand Down Expand Up @@ -68,6 +69,32 @@ export class Pair {
return token.equals(this.token0) || token.equals(this.token1)
}

/**
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
*/
public get token0Price(): Price {
return new Price(this.token0, this.token1, this.tokenAmounts[0].raw, this.tokenAmounts[1].raw)
}

/**
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
*/
public get token1Price(): Price {
return new Price(this.token1, this.token0, this.tokenAmounts[1].raw, this.tokenAmounts[0].raw)
}

/**
* Return the price of the given token in terms of the other token in the pair.
* @param token token to return price of
*/
public priceOf(token: Token): Price {
invariant(this.involvesToken(token), 'TOKEN')
return token.equals(this.token0) ? this.token0Price : this.token1Price
}

/**
* Returns the chain ID of the tokens in the pair.
*/
public get chainId(): ChainId {
return this.token0.chainId
}
Expand Down
37 changes: 35 additions & 2 deletions test/pair.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Token, Pair, TokenAmount, WETH } from '../src/entities'
import { ChainId } from '../src/constants'
import { ChainId, Token, Pair, TokenAmount, WETH, Price } from '../src'

describe('Pair', () => {
const USDC = new Token(ChainId.MAINNET, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 18, 'USDC', 'USD Coin')
Expand Down Expand Up @@ -52,6 +51,40 @@ describe('Pair', () => {
})
})

describe('#token0Price', () => {
it('returns price of token0 in terms of token1', () => {
expect(new Pair(new TokenAmount(USDC, '101'), new TokenAmount(DAI, '100')).token0Price).toEqual(
new Price(DAI, USDC, '100', '101')
)
expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '101')).token0Price).toEqual(
new Price(DAI, USDC, '100', '101')
)
})
})

describe('#token1Price', () => {
it('returns price of token1 in terms of token0', () => {
expect(new Pair(new TokenAmount(USDC, '101'), new TokenAmount(DAI, '100')).token1Price).toEqual(
new Price(USDC, DAI, '101', '100')
)
expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '101')).token1Price).toEqual(
new Price(USDC, DAI, '101', '100')
)
})
})

describe('#priceOf', () => {
const pair = new Pair(new TokenAmount(USDC, '101'), new TokenAmount(DAI, '100'))
it('returns price of token in terms of other token', () => {
expect(pair.priceOf(DAI)).toEqual(pair.token0Price)
expect(pair.priceOf(USDC)).toEqual(pair.token1Price)
})

it('throws if invalid token', () => {
expect(() => pair.priceOf(WETH[ChainId.MAINNET])).toThrow('TOKEN')
})
})

describe('#reserveOf', () => {
it('returns reserves of the given token', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '101')).reserveOf(USDC)).toEqual(
Expand Down

0 comments on commit 4e048df

Please sign in to comment.