Percent
The Percent class represents a percentage value as a fraction.
Import
import { Percent } from '@uniswap/sdk-core-next'Constructor
new Percent(numerator: BigintIsh, denominator?: BigintIsh)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
numerator | BigintIsh | - | The numerator |
denominator | BigintIsh | 1n | The denominator |
Example
import { Percent } from '@uniswap/sdk-core-next'
// 50% = 50/100
const halfPercent = new Percent(50, 100)
// 0.5% = 50/10000 (common for slippage)
const slippage = new Percent(50, 10000)
// 1% = 1/100
const onePercent = new Percent(1, 100)
// 100% = 1/1 or 100/100
const fullPercent = new Percent(1)
// Display
halfPercent.toSignificant(2) // "50"
slippage.toSignificant(2) // "0.5"
onePercent.toFixed(2) // "1.00"Common Percentages
import { Percent } from '@uniswap/sdk-core-next'
// Slippage tolerances
const SLIPPAGE_05 = new Percent(50, 10000) // 0.5%
const SLIPPAGE_1 = new Percent(100, 10000) // 1%
const SLIPPAGE_5 = new Percent(500, 10000) // 5%
// Fee percentages
const FEE_001 = new Percent(1, 10000) // 0.01%
const FEE_005 = new Percent(5, 10000) // 0.05%
const FEE_03 = new Percent(30, 10000) // 0.3%
const FEE_1 = new Percent(100, 10000) // 1%Properties
isPercent
readonly isPercent: trueType discriminator to identify Percent instances.
Methods
All arithmetic methods return Percent instances:
add(other)
add(other: Fraction | BigintIsh): Percentsubtract(other)
subtract(other: Fraction | BigintIsh): Percentmultiply(other)
multiply(other: Fraction | BigintIsh): Percentdivide(other)
divide(other: Fraction | BigintIsh): PercenttoSignificant(significantDigits?, format?, rounding?)
toSignificant(
significantDigits?: number, // default: 5
format?: { groupSeparator?: string },
rounding?: Rounding
): stringReturns the percentage as a string (multiplied by 100).
const p = new Percent(1, 200) // 0.5%
p.toSignificant(2) // "0.5"toFixed(decimalPlaces?, format?, rounding?)
toFixed(
decimalPlaces?: number, // default: 2
format?: { groupSeparator?: string },
rounding?: Rounding
): stringReturns the percentage with fixed decimals.
const p = new Percent(1, 200)
p.toFixed(2) // "0.50"
p.toFixed(4) // "0.5000"Example: Slippage Calculation
import { CurrencyAmount, Percent, Token } from '@uniswap/sdk-core-next'
const USDC = new Token(1, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 'USDC')
// Expected output from a trade
const expectedOutput = CurrencyAmount.fromRawAmount(USDC, 1000_000000n) // 1000 USDC
// Slippage tolerance
const slippageTolerance = new Percent(50, 10000) // 0.5%
// Calculate minimum output
// minOutput = expectedOutput * (1 - slippage)
const slippageAdjustment = new Percent(1).subtract(slippageTolerance)
const minOutput = expectedOutput.multiply(slippageAdjustment)
console.log(`Expected: ${expectedOutput.toSignificant(6)} USDC`) // "1000"
console.log(`Slippage: ${slippageTolerance.toSignificant(2)}%`) // "0.5"
console.log(`Minimum: ${minOutput.toSignificant(6)} USDC`) // "995"
// Or calculate maximum input for exact output trades
const expectedInput = CurrencyAmount.fromRawAmount(USDC, 1000_000000n)
const maxInput = expectedInput.multiply(new Percent(1).add(slippageTolerance))
console.log(`Maximum input: ${maxInput.toSignificant(6)} USDC`) // "1005"Example: Price Impact
import { Percent } from '@uniswap/sdk-core-next'
// Price impact thresholds
const LOW_PRICE_IMPACT = new Percent(1, 100) // 1%
const MEDIUM_PRICE_IMPACT = new Percent(3, 100) // 3%
const HIGH_PRICE_IMPACT = new Percent(5, 100) // 5%
function getPriceImpactSeverity(priceImpact: Percent): string {
if (priceImpact.lessThan(LOW_PRICE_IMPACT)) {
return 'low'
}
if (priceImpact.lessThan(MEDIUM_PRICE_IMPACT)) {
return 'medium'
}
if (priceImpact.lessThan(HIGH_PRICE_IMPACT)) {
return 'high'
}
return 'very high'
}
const impact = new Percent(25, 1000) // 2.5%
console.log(getPriceImpactSeverity(impact)) // "medium"