Core TypeScript utilities and abstractions for building applications on top of Uniswap V3
npx @tessl/cli install tessl/npm-uniswap--sdk-core@7.7.0The Uniswap SDK Core provides foundational TypeScript utilities and abstractions for building applications on top of Uniswap V3, the decentralized exchange protocol on Ethereum. It serves as shared foundational code across multiple Uniswap TypeScript SDKs, offering essential primitives for working with tokens, addresses, mathematical operations, and blockchain interactions.
npm install @uniswap/sdk-coreimport { ChainId, Token, Ether, CurrencyAmount, Price, Percent, Fraction } from "@uniswap/sdk-core";For CommonJS:
const { ChainId, Token, Ether, CurrencyAmount, Price, Percent, Fraction } = require("@uniswap/sdk-core");import { ChainId, Token, Ether, CurrencyAmount, Fraction, Percent } from "@uniswap/sdk-core";
// Create a token (UNI token)
const UNI = new Token(
ChainId.MAINNET,
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
18,
"UNI",
"Uniswap"
);
// Create native currency (Ether)
const ether = Ether.onChain(ChainId.MAINNET);
// Create currency amounts
const uniAmount = CurrencyAmount.fromRawAmount(UNI, "1000000000000000000"); // 1 UNI (18 decimals)
const etherAmount = CurrencyAmount.fromRawAmount(ether, "1000000000000000000"); // 1 ETH (18 decimals)
// Work with fractions and percentages
const fraction = new Fraction(1, 100);
const percent = new Percent(25, 100); // 25%
console.log(uniAmount.toExact()); // "1"
console.log(etherAmount.toExact()); // "1"
console.log(percent.toFixed(2)); // "25.00"The SDK Core is built around several key components:
Comprehensive blockchain network support with pre-configured contract addresses for Uniswap protocols across multiple chains including Ethereum, Polygon, Arbitrum, Optimism, and more.
enum ChainId {
MAINNET = 1,
OPTIMISM = 10,
ARBITRUM_ONE = 42161,
POLYGON = 137,
// ... and many more
}
const SUPPORTED_CHAINS: readonly ChainId[];Type-safe currency abstractions for native currencies and ERC20 tokens with comprehensive equality checking and wrapping functionality.
abstract class BaseCurrency {
readonly chainId: number;
readonly decimals: number;
readonly symbol?: string;
readonly name?: string;
abstract get wrapped(): Token;
abstract equals(other: Currency): boolean;
}
class Token extends BaseCurrency {
readonly address: string;
constructor(
chainId: number,
address: string,
decimals: number,
symbol?: string,
name?: string,
bypassChecksum?: boolean
);
}
class Ether extends NativeCurrency {
static onChain(chainId: number): Ether;
}Precise fractional arithmetic and currency amount calculations using JSBI for handling large numbers without precision loss, essential for financial applications.
class Fraction {
readonly numerator: JSBI;
readonly denominator: JSBI;
constructor(numerator: BigintIsh, denominator?: BigintIsh);
add(other: Fraction | BigintIsh): Fraction;
multiply(other: Fraction | BigintIsh): Fraction;
toSignificant(significantDigits: number): string;
}
class CurrencyAmount<T extends Currency> extends Fraction {
readonly currency: T;
static fromRawAmount<T extends Currency>(currency: T, rawAmount: BigintIsh): CurrencyAmount<T>;
toExact(): string;
}
class Price<TBase extends Currency, TQuote extends Currency> extends Fraction {
readonly baseCurrency: TBase;
readonly quoteCurrency: TQuote;
quote(currencyAmount: CurrencyAmount<TBase>): CurrencyAmount<TQuote>;
}Essential utility functions for address validation, price impact calculations, mathematical operations, and specialized blockchain utilities.
function validateAndParseAddress(address: string): string;
function computePriceImpact<TBase extends Currency, TQuote extends Currency>(
midPrice: Price<TBase, TQuote>,
inputAmount: CurrencyAmount<TBase>,
outputAmount: CurrencyAmount<TQuote>
): Percent;
function sqrt(value: JSBI): JSBI;type BigintIsh = JSBI | string | number;
type Currency = NativeCurrency | Token;
type SupportedChainsType = (typeof SUPPORTED_CHAINS)[number];
enum TradeType {
EXACT_INPUT,
EXACT_OUTPUT,
}
enum Rounding {
ROUND_DOWN,
ROUND_HALF_UP,
ROUND_UP,
}
interface AddressMap {
[chainId: number]: string;
}
// External types from @ethersproject
type BytesLike = string | Uint8Array;
interface BigNumber {
readonly _hex: string;
readonly _isBigNumber: boolean;
}