Core TypeScript utilities and abstractions for building applications on top of Uniswap V3
—
Precise fractional arithmetic and currency amount calculations using JSBI for handling large numbers without precision loss, essential for financial applications.
Core fraction arithmetic with precise decimal operations using JSBI to avoid floating-point errors.
/**
* Represents a fraction with numerator and denominator using JSBI for precision
*/
class Fraction {
/** The numerator of the fraction */
readonly numerator: JSBI;
/** The denominator of the fraction */
readonly denominator: JSBI;
/**
* @param numerator - The numerator as BigintIsh
* @param denominator - The denominator as BigintIsh (defaults to 1)
*/
constructor(numerator: BigintIsh, denominator?: BigintIsh);
/** Performs floor division - returns the quotient */
get quotient(): JSBI;
/** Returns remainder after floor division */
get remainder(): Fraction;
/** Helper method for converting any super class back to a fraction */
get asFraction(): Fraction;
/** Returns the inverted fraction (denominator/numerator) */
invert(): Fraction;
/**
* Add another fraction or BigintIsh value
* @param other - Fraction or BigintIsh to add
*/
add(other: Fraction | BigintIsh): Fraction;
/**
* Subtract another fraction or BigintIsh value
* @param other - Fraction or BigintIsh to subtract
*/
subtract(other: Fraction | BigintIsh): Fraction;
/**
* Multiply by another fraction or BigintIsh value
* @param other - Fraction or BigintIsh to multiply by
*/
multiply(other: Fraction | BigintIsh): Fraction;
/**
* Divide by another fraction or BigintIsh value
* @param other - Fraction or BigintIsh to divide by
*/
divide(other: Fraction | BigintIsh): Fraction;
/**
* Check if this fraction is less than another
* @param other - Fraction or BigintIsh to compare
*/
lessThan(other: Fraction | BigintIsh): boolean;
/**
* Check if this fraction equals another
* @param other - Fraction or BigintIsh to compare
*/
equalTo(other: Fraction | BigintIsh): boolean;
/**
* Check if this fraction is greater than another
* @param other - Fraction or BigintIsh to compare
*/
greaterThan(other: Fraction | BigintIsh): boolean;
/**
* Format to string with specified significant digits
* @param significantDigits - Number of significant digits
* @param format - Formatting options (default: { groupSeparator: '' })
* @param rounding - Rounding mode (default: ROUND_HALF_UP)
*/
toSignificant(
significantDigits: number,
format?: object,
rounding?: Rounding
): string;
/**
* Format to string with fixed decimal places
* @param decimalPlaces - Number of decimal places
* @param format - Formatting options (default: { groupSeparator: '' })
* @param rounding - Rounding mode (default: ROUND_HALF_UP)
*/
toFixed(
decimalPlaces: number,
format?: object,
rounding?: Rounding
): string;
}Represents an amount of a specific currency with automatic decimal scaling.
/**
* Currency amount with automatic decimal scaling and currency-aware operations
*/
class CurrencyAmount<T extends Currency> extends Fraction {
/** The currency this amount represents */
readonly currency: T;
/** Decimal scale factor for this currency */
readonly decimalScale: JSBI;
/**
* Create currency amount from raw amount (smallest unit)
* @param currency - The currency
* @param rawAmount - Raw amount in smallest unit (e.g., wei for ETH)
*/
static fromRawAmount<T extends Currency>(
currency: T,
rawAmount: BigintIsh
): CurrencyAmount<T>;
/**
* Create currency amount from fractional amount
* @param currency - The currency
* @param numerator - Numerator of the fraction
* @param denominator - Denominator of the fraction
*/
static fromFractionalAmount<T extends Currency>(
currency: T,
numerator: BigintIsh,
denominator: BigintIsh
): CurrencyAmount<T>;
/**
* Add another currency amount (must be same currency)
* @param other - CurrencyAmount to add
*/
add(other: CurrencyAmount<T>): CurrencyAmount<T>;
/**
* Subtract another currency amount (must be same currency)
* @param other - CurrencyAmount to subtract
*/
subtract(other: CurrencyAmount<T>): CurrencyAmount<T>;
/**
* Multiply by a fraction or BigintIsh
* @param other - Fraction or BigintIsh to multiply by
*/
multiply(other: Fraction | BigintIsh): CurrencyAmount<T>;
/**
* Divide by a fraction or BigintIsh
* @param other - Fraction or BigintIsh to divide by
*/
divide(other: Fraction | BigintIsh): CurrencyAmount<T>;
/**
* Format to string with significant digits
* @param significantDigits - Number of significant digits (default: 6)
* @param format - Formatting options
* @param rounding - Rounding mode (default: ROUND_DOWN)
*/
toSignificant(
significantDigits?: number,
format?: object,
rounding?: Rounding
): string;
/**
* Format to string with fixed decimal places
* @param decimalPlaces - Number of decimal places (default: currency.decimals)
* @param format - Formatting options
* @param rounding - Rounding mode (default: ROUND_DOWN)
*/
toFixed(
decimalPlaces?: number,
format?: object,
rounding?: Rounding
): string;
/**
* Format to exact string representation
* @param format - Formatting options (default: { groupSeparator: '' })
*/
toExact(format?: object): string;
/** Get wrapped version of this currency amount */
get wrapped(): CurrencyAmount<Token>;
}Represents exchange rate between two currencies with type safety.
/**
* Represents the exchange rate between two currencies
*/
class Price<TBase extends Currency, TQuote extends Currency> extends Fraction {
/** Input currency (denominator) */
readonly baseCurrency: TBase;
/** Output currency (numerator) */
readonly quoteCurrency: TQuote;
/** Scalar used to adjust the raw fraction w/r/t the decimals of the currencies */
readonly scalar: Fraction;
/**
* Construct a price with base and quote currencies and amounts
* @param args - Either [baseCurrency, quoteCurrency, denominator, numerator] or [{ baseAmount, quoteAmount }]
*/
constructor(
...args:
| [TBase, TQuote, BigintIsh, BigintIsh]
| [{ baseAmount: CurrencyAmount<TBase>; quoteAmount: CurrencyAmount<TQuote> }]
);
/** Flip the price, switching the base and quote currency */
invert(): Price<TQuote, TBase>;
/**
* Multiply the price by another price
* @param other - Price to multiply with (must have matching quote/base currencies)
*/
multiply<TOtherQuote extends Currency>(
other: Price<TQuote, TOtherQuote>
): Price<TBase, TOtherQuote>;
/**
* Return the amount of quote currency corresponding to a given amount of the base currency
* @param currencyAmount - Amount of base currency to quote
*/
quote(currencyAmount: CurrencyAmount<TBase>): CurrencyAmount<TQuote>;
/**
* Format price to significant digits
* @param significantDigits - Number of significant digits (default: 6)
* @param format - Formatting options
* @param rounding - Rounding mode
*/
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
/**
* Format price to fixed decimal places
* @param decimalPlaces - Number of decimal places (default: 4)
* @param format - Formatting options
* @param rounding - Rounding mode
*/
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
}Specialized fraction for representing percentages with automatic formatting.
/**
* Represents a percentage value
*/
class Percent extends Fraction {
/** Boolean identifier for percentage values */
readonly isPercent: true;
/** Add another fraction or BigintIsh, returning a Percent */
add(other: Fraction | BigintIsh): Percent;
/** Subtract another fraction or BigintIsh, returning a Percent */
subtract(other: Fraction | BigintIsh): Percent;
/** Multiply by another fraction or BigintIsh, returning a Percent */
multiply(other: Fraction | BigintIsh): Percent;
/** Divide by another fraction or BigintIsh, returning a Percent */
divide(other: Fraction | BigintIsh): Percent;
/**
* Format as percentage with significant digits
* @param significantDigits - Number of significant digits (default: 5)
* @param format - Formatting options
* @param rounding - Rounding mode
*/
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
/**
* Format as percentage with fixed decimal places
* @param decimalPlaces - Number of decimal places (default: 2)
* @param format - Formatting options
* @param rounding - Rounding mode
*/
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
}Usage Examples:
import {
Fraction,
CurrencyAmount,
Price,
Percent,
Token,
Ether,
ChainId,
Rounding
} from "@uniswap/sdk-core";
// Create fractions for precise arithmetic
const half = new Fraction(1, 2);
const quarter = new Fraction(1, 4);
console.log(half.add(quarter).toSignificant(2)); // "0.75"
console.log(half.multiply(2).toFixed(0)); // "1"
// Work with currency amounts
const USDC = new Token(ChainId.MAINNET, "0xA0b86a33E6424b73E63872f681e2230aDC3D3dC", 6, "USDC");
const ether = Ether.onChain(ChainId.MAINNET);
// Create amounts from raw values (smallest units)
const usdcAmount = CurrencyAmount.fromRawAmount(USDC, "1000000"); // 1 USDC
const etherAmount = CurrencyAmount.fromRawAmount(ether, "1000000000000000000"); // 1 ETH
console.log(usdcAmount.toExact()); // "1"
console.log(etherAmount.toExact()); // "1"
// Arithmetic with currency amounts
const doubleUsdc = usdcAmount.multiply(2);
console.log(doubleUsdc.toExact()); // "2"
const halfEther = etherAmount.divide(2);
console.log(halfEther.toExact()); // "0.5"
// Create prices between currencies
const price = new Price({
baseAmount: etherAmount,
quoteAmount: CurrencyAmount.fromRawAmount(USDC, "2000000000") // 2000 USDC per ETH
});
console.log(price.toSignificant(4)); // "2000"
// Quote amounts using price
const ethToQuote = CurrencyAmount.fromRawAmount(ether, "500000000000000000"); // 0.5 ETH
const quotedUsdc = price.quote(ethToQuote);
console.log(quotedUsdc.toExact()); // "1000" (0.5 ETH * 2000 USDC/ETH)
// Work with percentages
const fivePercent = new Percent(5, 100);
const tenPercent = new Percent(1, 10);
console.log(fivePercent.toFixed(2)); // "5.00"
console.log(tenPercent.add(fivePercent).toFixed(1)); // "15.0"
// Use different rounding modes
const fraction = new Fraction(1, 3);
console.log(fraction.toFixed(2, undefined, Rounding.ROUND_DOWN)); // "0.33"
console.log(fraction.toFixed(2, undefined, Rounding.ROUND_UP)); // "0.34"/**
* Union type for big integer-like values
*/
type BigintIsh = JSBI | string | number;
/**
* Rounding modes for decimal formatting
*/
enum Rounding {
ROUND_DOWN,
ROUND_HALF_UP,
ROUND_UP,
}Install with Tessl CLI
npx tessl i tessl/npm-uniswap--sdk-core