SDK Core
The SDK Core (@uniswap/sdk-core-next) provides the foundational building blocks for all Uniswap SDKs. It includes core entities for representing tokens and currencies, precise arithmetic with fractions, and essential utilities.
Features
- Type-safe Currency Handling: Abstract currency types with Token and NativeCurrency implementations
- Precise Arithmetic: Fraction-based math for accurate DeFi calculations
- Native BigInt: All numeric operations use native JavaScript BigInt
- Address Validation: Zod-based schema validation with checksum verification
Installation
npm install @uniswap/sdk-core-nextpnpm add @uniswap/sdk-core-nextyarn add @uniswap/sdk-core-nextQuick Start
import { Token, CurrencyAmount, Percent, Price } from '@uniswap/sdk-core-next'
// Create a token
const USDC = new Token(
1, // chainId (Ethereum mainnet)
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
6, // decimals
'USDC',
'USD Coin'
)
// Create a currency amount
const amount = CurrencyAmount.fromRawAmount(USDC, 1000000n) // 1 USDC
// Format for display
console.log(amount.toSignificant(6)) // "1"
console.log(amount.toFixed(2)) // "1.00"
// Work with percentages
const slippage = new Percent(50, 10000) // 0.5%
const minAmount = amount.multiply(new Percent(9950, 10000))Modules
Entities
Core data structures for representing currencies and tokens:
- Token - ERC20 token representation
- Currency - Union type of Token and NativeCurrency
- Ether - ETH representation for mainnet
- NativeCurrency - Abstract base for chain-native currencies
- WETH9 - Wrapped Ether token addresses
Fractions
Precise fractional arithmetic for DeFi:
- Fraction - Base fraction class with arbitrary precision
- CurrencyAmount - Amount of a specific currency
- Price - Exchange rate between two currencies
- Percent - Percentage representation
Utilities
Helper functions for common operations:
- computePriceImpact - Calculate price impact of trades
- validateAndParseAddress - Address validation with checksum
- sqrt - BigInt square root
- sortedInsert - Insert into sorted array
Configuration
Chain and address configuration: