forked from Uniswap/v2-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make new Fraction class more robust / generic tweak tsconfig
- Loading branch information
1 parent
8ecdc26
commit 2953d4c
Showing
13 changed files
with
905 additions
and
899 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import invariant from 'tiny-invariant' | ||
|
||
import { ZERO, ONE, TEN, _100 } from '../constants' | ||
import { BigintIsh } from '../types' | ||
import { parseBigintIsh } from '../utils/parseInputs' | ||
import { formatSignificant, formatFixed } from '../utils/formatOutputs' | ||
import { Route } from './route' | ||
|
||
export class Fraction { | ||
public readonly numerator: bigint | ||
public readonly denominator: bigint | ||
|
||
constructor(numerator: BigintIsh, denominator: BigintIsh = ONE) { | ||
this.numerator = parseBigintIsh(numerator) | ||
this.denominator = parseBigintIsh(denominator) | ||
} | ||
|
||
// warning: this can truncate! | ||
get quotient() { | ||
return this.numerator / this.denominator | ||
} | ||
|
||
public invert(): Fraction { | ||
return new Fraction(this.denominator, this.numerator) | ||
} | ||
|
||
public multiply(other: Fraction): Fraction { | ||
return new Fraction(this.numerator * other.numerator, this.denominator * other.denominator) | ||
} | ||
|
||
public formatSignificant(significantDigits: number, ...rest: any[]): string { | ||
return formatSignificant(this.numerator, this.denominator, significantDigits, ...rest) | ||
} | ||
|
||
public formatFixed(decimalPlaces: number, ...rest: any[]): string { | ||
return formatFixed(this.numerator, this.denominator, decimalPlaces, ...rest) | ||
} | ||
} | ||
|
||
export class Price { | ||
public readonly price: Fraction // normalized | ||
public readonly scalar: Fraction // used to convert back to raw balances | ||
|
||
static fromRoute(route: Route): Price { | ||
const prices: Fraction[] = route.exchanges.map((exchange, i) => { | ||
const input = route.path[i] | ||
const baseIndex = input.address === exchange.pair[0].address ? 0 : 1 | ||
const quoteIndex = input.address === exchange.pair[0].address ? 1 : 0 | ||
return new Fraction( | ||
exchange.balances[quoteIndex] * TEN ** BigInt(exchange.pair[baseIndex].decimals), | ||
exchange.balances[baseIndex] * TEN ** BigInt(exchange.pair[quoteIndex].decimals) | ||
) | ||
}) | ||
const price = prices.reduce((accumulator, currentValue) => accumulator.multiply(currentValue), new Fraction(ONE)) | ||
const scalar = new Fraction(TEN ** BigInt(route.output.decimals), TEN ** BigInt(route.input.decimals)) | ||
return new Price(price, scalar) | ||
} | ||
|
||
constructor(price: Fraction, scalar: Fraction) { | ||
this.price = price | ||
this.scalar = scalar | ||
} | ||
|
||
public invert(): Price { | ||
return new Price(this.price.invert(), this.scalar.invert()) | ||
} | ||
|
||
public quote(amount: BigintIsh): bigint { | ||
const amountParsed = parseBigintIsh(amount) | ||
invariant(amountParsed > ZERO, `${amountParsed} isn't positive.`) | ||
|
||
return this.price.multiply(this.scalar).multiply(new Fraction(amount)).quotient | ||
} | ||
|
||
public formatSignificant(significantDigits = 6, ...rest: any[]) { | ||
return this.price.formatSignificant(significantDigits, ...rest) | ||
} | ||
|
||
public formatFixed(decimalPlaces = 6, ...rest: any[]) { | ||
return this.price.formatFixed(decimalPlaces, ...rest) | ||
} | ||
} | ||
|
||
export class Percent { | ||
public readonly percent: Fraction | ||
|
||
constructor(percent: Fraction) { | ||
this.percent = percent | ||
} | ||
|
||
public formatSignificant(significantDigits = 5, ...rest: any[]) { | ||
return this.percent.multiply(new Fraction(_100)).formatSignificant(significantDigits, ...rest) | ||
} | ||
|
||
public formatFixed(decimalPlaces = 2, ...rest: any[]) { | ||
return this.percent.multiply(new Fraction(_100)).formatFixed(decimalPlaces, ...rest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './types' | ||
export { ChainId, WETH, TradeType } from './constants' | ||
|
||
export * from './entities' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export type BigintIsh = bigint | string | ||
|
||
export type Fraction = [bigint, bigint] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.