forked from Uniswap/v2-sdk
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating SDK addresses for xDai support (#2)
- Loading branch information
Showing
28 changed files
with
2,431 additions
and
1,406 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ignore-scripts true |
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,37 @@ | ||
import JSBI from 'jsbi' | ||
|
||
import { SolidityType } from '../constants' | ||
import { validateSolidityTypeInstance } from '../utils' | ||
|
||
/** | ||
* A currency is any fungible financial instrument on Ethereum, including Ether and all ERC20 tokens. | ||
* | ||
* The only instance of the base class `Currency` is Ether. | ||
*/ | ||
export class Currency { | ||
public readonly decimals: number | ||
public readonly symbol?: string | ||
public readonly name?: string | ||
|
||
/** | ||
* The only instance of the base class `Currency`. | ||
*/ | ||
public static readonly ETHER: Currency = new Currency(18, 'XDAI', 'xDai') | ||
|
||
/** | ||
* Constructs an instance of the base class `Currency`. The only instance of the base class `Currency` is `Currency.ETHER`. | ||
* @param decimals decimals of the currency | ||
* @param symbol symbol of the currency | ||
* @param name of the currency | ||
*/ | ||
protected constructor(decimals: number, symbol?: string, name?: string) { | ||
validateSolidityTypeInstance(JSBI.BigInt(decimals), SolidityType.uint8) | ||
|
||
this.decimals = decimals | ||
this.symbol = symbol | ||
this.name = name | ||
} | ||
} | ||
|
||
const ETHER = Currency.ETHER | ||
export { ETHER } |
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,69 @@ | ||
import { currencyEquals } from '../token' | ||
import { Currency, ETHER } from '../currency' | ||
import invariant from 'tiny-invariant' | ||
import JSBI from 'jsbi' | ||
import _Big from 'big.js' | ||
import toFormat from 'toformat' | ||
|
||
import { BigintIsh, Rounding, TEN, SolidityType } from '../../constants' | ||
import { parseBigintIsh, validateSolidityTypeInstance } from '../../utils' | ||
import { Fraction } from './fraction' | ||
|
||
const Big = toFormat(_Big) | ||
|
||
export class CurrencyAmount extends Fraction { | ||
public readonly currency: Currency | ||
|
||
/** | ||
* Helper that calls the constructor with the ETHER currency | ||
* @param amount ether amount in wei | ||
*/ | ||
public static ether(amount: BigintIsh): CurrencyAmount { | ||
return new CurrencyAmount(ETHER, amount) | ||
} | ||
|
||
// amount _must_ be raw, i.e. in the native representation | ||
protected constructor(currency: Currency, amount: BigintIsh) { | ||
const parsedAmount = parseBigintIsh(amount) | ||
validateSolidityTypeInstance(parsedAmount, SolidityType.uint256) | ||
|
||
super(parsedAmount, JSBI.exponentiate(TEN, JSBI.BigInt(currency.decimals))) | ||
this.currency = currency | ||
} | ||
|
||
public get raw(): JSBI { | ||
return this.numerator | ||
} | ||
|
||
public add(other: CurrencyAmount): CurrencyAmount { | ||
invariant(currencyEquals(this.currency, other.currency), 'TOKEN') | ||
return new CurrencyAmount(this.currency, JSBI.add(this.raw, other.raw)) | ||
} | ||
|
||
public subtract(other: CurrencyAmount): CurrencyAmount { | ||
invariant(currencyEquals(this.currency, other.currency), 'TOKEN') | ||
return new CurrencyAmount(this.currency, JSBI.subtract(this.raw, other.raw)) | ||
} | ||
|
||
public toSignificant( | ||
significantDigits: number = 6, | ||
format?: object, | ||
rounding: Rounding = Rounding.ROUND_DOWN | ||
): string { | ||
return super.toSignificant(significantDigits, format, rounding) | ||
} | ||
|
||
public toFixed( | ||
decimalPlaces: number = this.currency.decimals, | ||
format?: object, | ||
rounding: Rounding = Rounding.ROUND_DOWN | ||
): string { | ||
invariant(decimalPlaces <= this.currency.decimals, 'DECIMALS') | ||
return super.toFixed(decimalPlaces, format, rounding) | ||
} | ||
|
||
public toExact(format: object = { groupSeparator: '' }): string { | ||
Big.DP = this.currency.decimals | ||
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(format) | ||
} | ||
} |
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,4 +1,5 @@ | ||
export * from './fraction' | ||
export * from './percent' | ||
export * from './tokenAmount' | ||
export * from './currencyAmount' | ||
export * from './price' |
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,70 +1,76 @@ | ||
import { Token } from '../token' | ||
import { TokenAmount } from './tokenAmount' | ||
import { currencyEquals } from '../token' | ||
import invariant from 'tiny-invariant' | ||
import JSBI from 'jsbi' | ||
|
||
import { BigintIsh, Rounding, TEN } from '../../constants' | ||
import { Token } from '../token' | ||
import { Currency } from '../currency' | ||
import { Route } from '../route' | ||
import { Fraction } from './fraction' | ||
import { TokenAmount } from './tokenAmount' | ||
import { CurrencyAmount } from './currencyAmount' | ||
|
||
export class Price extends Fraction { | ||
public readonly baseToken: Token // input i.e. denominator | ||
public readonly quoteToken: Token // output i.e. numerator | ||
public readonly baseCurrency: Currency // input i.e. denominator | ||
public readonly quoteCurrency: Currency // output i.e. numerator | ||
public readonly scalar: Fraction // used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token | ||
|
||
static fromRoute(route: Route): Price { | ||
public static fromRoute(route: Route): Price { | ||
const prices: Price[] = [] | ||
for (const [i, pair] of route.pairs.entries()) { | ||
prices.push( | ||
route.path[i].equals(pair.token0) | ||
? new Price(pair.reserve0.token, pair.reserve1.token, pair.reserve0.raw, pair.reserve1.raw) | ||
: new Price(pair.reserve1.token, pair.reserve0.token, pair.reserve1.raw, pair.reserve0.raw) | ||
? new Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.raw, pair.reserve1.raw) | ||
: new Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.raw, pair.reserve0.raw) | ||
) | ||
} | ||
return prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]) | ||
} | ||
|
||
// denominator and numerator _must_ be raw, i.e. in the native representation | ||
constructor(baseToken: Token, quoteToken: Token, denominator: BigintIsh, numerator: BigintIsh) { | ||
public constructor(baseCurrency: Currency, quoteCurrency: Currency, denominator: BigintIsh, numerator: BigintIsh) { | ||
super(numerator, denominator) | ||
|
||
this.baseToken = baseToken | ||
this.quoteToken = quoteToken | ||
this.baseCurrency = baseCurrency | ||
this.quoteCurrency = quoteCurrency | ||
this.scalar = new Fraction( | ||
JSBI.exponentiate(TEN, JSBI.BigInt(baseToken.decimals)), | ||
JSBI.exponentiate(TEN, JSBI.BigInt(quoteToken.decimals)) | ||
JSBI.exponentiate(TEN, JSBI.BigInt(baseCurrency.decimals)), | ||
JSBI.exponentiate(TEN, JSBI.BigInt(quoteCurrency.decimals)) | ||
) | ||
} | ||
|
||
get raw(): Fraction { | ||
public get raw(): Fraction { | ||
return new Fraction(this.numerator, this.denominator) | ||
} | ||
|
||
get adjusted(): Fraction { | ||
public get adjusted(): Fraction { | ||
return super.multiply(this.scalar) | ||
} | ||
|
||
invert(): Price { | ||
return new Price(this.quoteToken, this.baseToken, this.numerator, this.denominator) | ||
public invert(): Price { | ||
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator) | ||
} | ||
|
||
multiply(other: Price): Price { | ||
invariant(this.quoteToken.equals(other.baseToken), 'BASE') | ||
public multiply(other: Price): Price { | ||
invariant(currencyEquals(this.quoteCurrency, other.baseCurrency), 'TOKEN') | ||
const fraction = super.multiply(other) | ||
return new Price(this.baseToken, other.quoteToken, fraction.denominator, fraction.numerator) | ||
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator) | ||
} | ||
|
||
// performs floor division on overflow | ||
quote(tokenAmount: TokenAmount): TokenAmount { | ||
invariant(tokenAmount.token.equals(this.baseToken), 'TOKEN') | ||
return new TokenAmount(this.quoteToken, super.multiply(tokenAmount.raw).quotient) | ||
public quote(currencyAmount: CurrencyAmount): CurrencyAmount { | ||
invariant(currencyEquals(currencyAmount.currency, this.baseCurrency), 'TOKEN') | ||
if (this.quoteCurrency instanceof Token) { | ||
return new TokenAmount(this.quoteCurrency, super.multiply(currencyAmount.raw).quotient) | ||
} | ||
return CurrencyAmount.ether(super.multiply(currencyAmount.raw).quotient) | ||
} | ||
|
||
toSignificant(significantDigits: number = 6, format?: object, rounding?: Rounding): string { | ||
public toSignificant(significantDigits: number = 6, format?: object, rounding?: Rounding): string { | ||
return this.adjusted.toSignificant(significantDigits, format, rounding) | ||
} | ||
|
||
toFixed(decimalPlaces: number = 4, format?: object, rounding?: Rounding): string { | ||
public toFixed(decimalPlaces: number = 4, format?: object, rounding?: Rounding): string { | ||
return this.adjusted.toFixed(decimalPlaces, format, rounding) | ||
} | ||
} |
Oops, something went wrong.