or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

chains-and-addresses.mdcurrency-system.mdindex.mdmathematical-operations.mdutilities.md
tile.json

tessl/npm-uniswap--sdk-core

Core TypeScript utilities and abstractions for building applications on top of Uniswap V3

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@uniswap/sdk-core@7.7.x

To install, run

npx @tessl/cli install tessl/npm-uniswap--sdk-core@7.7.0

index.mddocs/

Uniswap SDK Core

The 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.

Package Information

  • Package Name: @uniswap/sdk-core
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @uniswap/sdk-core

Core Imports

import { 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");

Basic Usage

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"

Architecture

The SDK Core is built around several key components:

  • Chain Management: Comprehensive support for 20+ blockchain networks with predefined chain IDs and contract addresses
  • Currency System: Type-safe abstractions for native currencies (ETH, MATIC, etc.) and ERC20 tokens
  • Mathematical Operations: Precise fraction-based arithmetic using JSBI to avoid floating-point errors
  • Price Calculations: Type-safe price representations between currency pairs
  • Utility Functions: Address validation, mathematical operations, and specialized blockchain utilities

Capabilities

Chain and Address Management

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[];

Chains and Addresses

Currency System

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;
}

Currency System

Mathematical Operations

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>;
}

Mathematical Operations

Utilities

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;

Utilities

Types

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;
}