-
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.
- Loading branch information
1 parent
e8141a7
commit ea1f169
Showing
25 changed files
with
887 additions
and
1,117 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
yarn lint --fix & yarn test | ||
yarn format & yarn test |
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 +1,2 @@ | ||
.github | ||
.github | ||
node_modules |
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,21 @@ | ||
import { expect, test } from 'vitest'; | ||
import * as exportedNameSpaces from './index'; | ||
|
||
test('exports', () => { | ||
expect(Object.keys(exportedNameSpaces)).toMatchInlineSnapshot(` | ||
[ | ||
"WETH9", | ||
"ChainId", | ||
"ChainType", | ||
"TradeType", | ||
"Rounding", | ||
"ZERO", | ||
"ONE", | ||
"TWO", | ||
"THREE", | ||
"Project", | ||
"WAGMI", | ||
"STABLE_COINS", | ||
] | ||
`); | ||
}); |
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
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,13 @@ | ||
import { expect, test } from 'vitest'; | ||
import * as exportedNameSpaces from './index'; | ||
|
||
test('exports', () => { | ||
expect(Object.keys(exportedNameSpaces)).toMatchInlineSnapshot(` | ||
[ | ||
"Fraction", | ||
"Percent", | ||
"CurrencyAmount", | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,81 @@ | ||
import invariant from 'tiny-invariant' | ||
import invariant from 'tiny-invariant'; | ||
|
||
import { BigintIsh, Rounding } from '../../constants/misc' | ||
import { Currency } from '../currency' | ||
import { Fraction } from './fraction' | ||
import { CurrencyAmount } from './currency-amount' | ||
import { BigintIsh, Rounding } from '../../constants/misc'; | ||
import { Currency } from '../currency'; | ||
import { Fraction } from './fraction'; | ||
import { CurrencyAmount } from './currency-amount'; | ||
|
||
export class Price<TBase extends Currency, TQuote extends Currency> extends Fraction { | ||
public readonly baseCurrency: TBase // input i.e. denominator | ||
public readonly baseCurrency: TBase; // input i.e. denominator | ||
|
||
public readonly quoteCurrency: TQuote // output i.e. numerator | ||
public readonly quoteCurrency: TQuote; // output i.e. numerator | ||
|
||
public readonly scalar: Fraction // used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token | ||
public readonly scalar: Fraction; // used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token | ||
|
||
/** | ||
* Construct a price, either with the base and quote currency amount, or the | ||
* @param args | ||
*/ | ||
public constructor( | ||
...args: | ||
| [TBase, TQuote, BigintIsh, BigintIsh] | ||
| [{ baseAmount: CurrencyAmount<TBase>; quoteAmount: CurrencyAmount<TQuote> }] | ||
) { | ||
let baseCurrency: TBase | ||
let quoteCurrency: TQuote | ||
let denominator: BigintIsh | ||
let numerator: BigintIsh | ||
/** | ||
* Construct a price, either with the base and quote currency amount, or the | ||
* @param args | ||
*/ | ||
public constructor(...args: [TBase, TQuote, BigintIsh, BigintIsh] | [{ baseAmount: CurrencyAmount<TBase>; quoteAmount: CurrencyAmount<TQuote> }]) { | ||
let baseCurrency: TBase; | ||
let quoteCurrency: TQuote; | ||
let denominator: BigintIsh; | ||
let numerator: BigintIsh; | ||
|
||
if (args.length === 4) { | ||
// eslint-disable-next-line @typescript-eslint/no-extra-semi | ||
;[baseCurrency, quoteCurrency, denominator, numerator] = args | ||
} else { | ||
const result = args[0].quoteAmount.divide(args[0].baseAmount) | ||
;[baseCurrency, quoteCurrency, denominator, numerator] = [ | ||
args[0].baseAmount.currency, | ||
args[0].quoteAmount.currency, | ||
result.denominator, | ||
result.numerator, | ||
] | ||
} | ||
super(numerator, denominator) | ||
if (args.length === 4) { | ||
// eslint-disable-next-line @typescript-eslint/no-extra-semi | ||
[baseCurrency, quoteCurrency, denominator, numerator] = args; | ||
} else { | ||
const result = args[0].quoteAmount.divide(args[0].baseAmount); | ||
[baseCurrency, quoteCurrency, denominator, numerator] = [args[0].baseAmount.currency, args[0].quoteAmount.currency, result.denominator, result.numerator]; | ||
} | ||
super(numerator, denominator); | ||
|
||
this.baseCurrency = baseCurrency | ||
this.quoteCurrency = quoteCurrency | ||
this.scalar = new Fraction(BigInt(10 ** baseCurrency.decimals), BigInt(10 ** quoteCurrency.decimals)) | ||
} | ||
this.baseCurrency = baseCurrency; | ||
this.quoteCurrency = quoteCurrency; | ||
this.scalar = new Fraction(BigInt(10 ** baseCurrency.decimals), BigInt(10 ** quoteCurrency.decimals)); | ||
} | ||
|
||
/** | ||
* Flip the price, switching the base and quote currency | ||
*/ | ||
public invert(): Price<TQuote, TBase> { | ||
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator) | ||
} | ||
/** | ||
* Flip the price, switching the base and quote currency | ||
*/ | ||
public invert(): Price<TQuote, TBase> { | ||
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator); | ||
} | ||
|
||
/** | ||
* Multiply the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency | ||
* @param other the other price | ||
*/ | ||
public multiply<TOtherQuote extends Currency>(other: Price<TQuote, TOtherQuote>): Price<TBase, TOtherQuote> { | ||
invariant(this.quoteCurrency.equals(other.baseCurrency), 'TOKEN') | ||
const fraction = super.multiply(other) | ||
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator) | ||
} | ||
/** | ||
* Multiply the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency | ||
* @param other the other price | ||
*/ | ||
public multiply<TOtherQuote extends Currency>(other: Price<TQuote, TOtherQuote>): Price<TBase, TOtherQuote> { | ||
invariant(this.quoteCurrency.equals(other.baseCurrency), 'TOKEN'); | ||
const fraction = super.multiply(other); | ||
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator); | ||
} | ||
|
||
/** | ||
* Return the amount of quote currency corresponding to a given amount of the base currency | ||
* @param currencyAmount the amount of base currency to quote against the price | ||
*/ | ||
public quote(currencyAmount: CurrencyAmount<TBase>): CurrencyAmount<TQuote> { | ||
invariant(currencyAmount.currency.equals(this.baseCurrency), 'TOKEN') | ||
const result = super.multiply(currencyAmount) | ||
return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator) | ||
} | ||
/** | ||
* Return the amount of quote currency corresponding to a given amount of the base currency | ||
* @param currencyAmount the amount of base currency to quote against the price | ||
*/ | ||
public quote(currencyAmount: CurrencyAmount<TBase>): CurrencyAmount<TQuote> { | ||
invariant(currencyAmount.currency.equals(this.baseCurrency), 'TOKEN'); | ||
const result = super.multiply(currencyAmount); | ||
return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator); | ||
} | ||
|
||
/** | ||
* Get the value scaled by decimals for formatting | ||
* @private | ||
*/ | ||
private get adjustedForDecimals(): Fraction { | ||
return super.multiply(this.scalar) | ||
} | ||
/** | ||
* Get the value scaled by decimals for formatting | ||
* @private | ||
*/ | ||
private get adjustedForDecimals(): Fraction { | ||
return super.multiply(this.scalar); | ||
} | ||
|
||
public toSignificant(significantDigits = 6, format?: object, rounding?: Rounding): string { | ||
return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding) | ||
} | ||
public toSignificant(significantDigits = 6, format?: object, rounding?: Rounding): string { | ||
return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding); | ||
} | ||
|
||
public toFixed(decimalPlaces = 4, format?: object, rounding?: Rounding): string { | ||
return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding) | ||
} | ||
public toFixed(decimalPlaces = 4, format?: object, rounding?: Rounding): string { | ||
return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding); | ||
} | ||
} |
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,25 @@ | ||
import { expect, test } from 'vitest'; | ||
import * as exportedNameSpaces from './index'; | ||
|
||
test('exports', () => { | ||
expect(Object.keys(exportedNameSpaces)).toMatchInlineSnapshot(` | ||
[ | ||
"Ether", | ||
"FantomNativeCurrency", | ||
"BaseCurrency", | ||
"NativeCurrency", | ||
"Token", | ||
"Fraction", | ||
"Percent", | ||
"CurrencyAmount", | ||
"Price", | ||
"WrappedTokenInfo", | ||
"deserializeToken", | ||
"BscNativeCurrency", | ||
"PolygonNativeCurrency", | ||
"KavaNativeCurrency", | ||
"AvalancheNativeCurrency", | ||
"MetisNativeCurrency", | ||
] | ||
`); | ||
}); |
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,20 @@ | ||
import { expect, test } from 'vitest'; | ||
import * as exportedNameSpaces from './index'; | ||
|
||
test('exports', () => { | ||
expect(Object.keys(exportedNameSpaces)).toMatchInlineSnapshot(` | ||
[ | ||
"fantomTokens", | ||
"zkSyncTokens", | ||
"ethereumTokens", | ||
"optimismTokens", | ||
"bscTokens", | ||
"polygonTokens", | ||
"kavaTokens", | ||
"avalancheTokens", | ||
"arbitrumTokens", | ||
"metisTokens", | ||
"blastTokens", | ||
] | ||
`); | ||
}); |
Oops, something went wrong.