Skip to content

Commit

Permalink
update Project
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfantaholic committed Mar 7, 2024
1 parent e8141a7 commit ea1f169
Show file tree
Hide file tree
Showing 25 changed files with 887 additions and 1,117 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
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
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.github
.github
node_modules
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
"dist"
],
"engines": {
"node": ">=16"
"node": ">=18"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"start": "tsup --watch",
"build": "tsup",
"test": "jest",
"test": "vitest",
"lint": "tsdx lint",
"prepare": "yarn build",
"size": "size-limit",
"analyze": "size-limit --why"
"analyze": "size-limit --why",
"format": "prettier --write \"*/**/*.ts\""
},
"author": "Wagmi",
"devDependencies": {
"@size-limit/preset-small-lib": "^9.0.0",
"@types/big.js": "^6.2.0",
"@types/jest": "^29.5.5",
"husky": "^8.0.3",
"jest": "^29.7.0",
"size-limit": "^9.0.0",
"ts-jest": "^29.1.1",
"tsdx": "^0.14.1",
"tslib": "^2.6.2",
"tsup": "^7.2.0",
Expand All @@ -48,6 +46,7 @@
"decimal.js-light": "^2.5.1",
"tiny-invariant": "^1.3.1",
"toformat": "^2.0.0",
"viem": "^1.12.2"
"viem": "^2.7.20",
"vitest": "^1.3.1"
}
}
4 changes: 2 additions & 2 deletions src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export enum ChainId {
KAVA = 2222,
AVALANCHE = 43114,
ARBITRUM = 42161,
BLAST = 81457
BLAST = 81457,
}

export enum ChainType {
L1,
L2,
}
}
21 changes: 21 additions & 0 deletions src/constants/index.test.ts
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",
]
`);
});
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './weth9';
export * from './chains';
export * from './misc';
export * from './wagmi';
export * from './stable-coins';
export * from './stable-coins';
8 changes: 4 additions & 4 deletions src/constants/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export const ONE = 1n;
export const TWO = 2n;
export const THREE = 3n;

export const MaxUint256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');

export enum Project {
WAGMI = 'wagmi',
UNISWAP = 'uniswap',
SUSHI = 'sushi',
PANCAKE = 'pancake',
KINETIX = 'kinetix'
}
KINETIX = 'kinetix',
THRUSTER = 'thruster',
AMBIENT = 'ambient',
}
14 changes: 7 additions & 7 deletions src/entities/fractions/currency-amount.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MaxUint256 } from '../../constants/misc';
import { maxUint256 } from 'viem';
import { ChainId } from '../../constants/chains';
import { Ether } from '../ether';
import { Token } from '../token';
Expand Down Expand Up @@ -33,18 +33,18 @@ describe('CurrencyAmount', () => {
});

it('token amount can be max uint256', () => {
const amount = CurrencyAmount.fromRawAmount(new Token(1, ADDRESS_ONE, 18), MaxUint256);
expect(amount.quotient).toEqual(MaxUint256);
const amount = CurrencyAmount.fromRawAmount(new Token(1, ADDRESS_ONE, 18), maxUint256);
expect(amount.quotient).toEqual(maxUint256);
});
it('token amount cannot exceed max uint256', () => {
expect(() => CurrencyAmount.fromRawAmount(new Token(1, ADDRESS_ONE, 18), MaxUint256 + 1n)).toThrow('AMOUNT');
expect(() => CurrencyAmount.fromRawAmount(new Token(1, ADDRESS_ONE, 18), maxUint256 + 1n)).toThrow('AMOUNT');
});
it('token amount quotient cannot exceed max uint256', () => {
expect(() => CurrencyAmount.fromFractionalAmount(new Token(1, ADDRESS_ONE, 18), MaxUint256 * 2n + 2n, 2n)).toThrow('AMOUNT');
expect(() => CurrencyAmount.fromFractionalAmount(new Token(1, ADDRESS_ONE, 18), maxUint256 * 2n + 2n, 2n)).toThrow('AMOUNT');
});
it('token amount numerator can be gt. uint256 if denominator is gt. 1', () => {
const amount = CurrencyAmount.fromFractionalAmount(new Token(1, ADDRESS_ONE, 18), MaxUint256 + 2n, 2);
expect(amount.numerator).toEqual(MaxUint256 + 2n);
const amount = CurrencyAmount.fromFractionalAmount(new Token(1, ADDRESS_ONE, 18), maxUint256 + 2n, 2);
expect(amount.numerator).toEqual(maxUint256 + 2n);
});

describe('#toFixed', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/entities/fractions/currency-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import toFormat from 'toformat';
import { Currency } from '../currency';
import { Token } from '../token';
import { Fraction } from './fraction';

import { BigintIsh, Rounding, MaxUint256 } from '../../constants/misc';
import { maxUint256 } from 'viem';
import { BigintIsh, Rounding } from '../../constants/misc';

const Big = toFormat(_Big);

Expand Down Expand Up @@ -35,7 +35,7 @@ export class CurrencyAmount<T extends Currency> extends Fraction {

protected constructor(currency: T, numerator: BigintIsh, denominator?: BigintIsh) {
super(numerator, denominator);
invariant(this.quotient <= MaxUint256, 'AMOUNT');
invariant(this.quotient <= maxUint256, 'AMOUNT');
this.currency = currency;
this.decimalScale = BigInt(10 ** currency.decimals);
}
Expand Down
13 changes: 13 additions & 0 deletions src/entities/fractions/index.test.ts
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",
]
`);
});
141 changes: 66 additions & 75 deletions src/entities/fractions/price.ts
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);
}
}
25 changes: 25 additions & 0 deletions src/entities/index.test.ts
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",
]
`);
});
2 changes: 1 addition & 1 deletion src/tokens/chains/arbitrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const arbitrumTokens = {
usdc_e: new Token(ChainId.ARBITRUM, '0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8', 6, 'USDC', 'USD Coin (Arb1)'),
usdt: new Token(ChainId.ARBITRUM, '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9', 6, 'USDT', 'Tether USD'),
wbtc: new Token(ChainId.ARBITRUM, '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f', 8, 'WBTC', 'Wrapped BTC'),
usdc: new Token(ChainId.ARBITRUM, '0xaf88d065e77c8cC2239327C5EDb3A432268e5831', 6, 'USDC', 'USD Coin')
usdc: new Token(ChainId.ARBITRUM, '0xaf88d065e77c8cC2239327C5EDb3A432268e5831', 6, 'USDC', 'USD Coin'),
};
20 changes: 20 additions & 0 deletions src/tokens/chains/index.test.ts
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",
]
`);
});

0 comments on commit ea1f169

Please sign in to comment.