@pancakeswap/swap-sdk-core 1.6.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -363,4 +363,101 @@ declare function getCurrencyAddress(currency: Currency): `0x${string}`;
363
363
  declare function getUnifiedCurrencyAddress(currency: UnifiedCurrency): string;
364
364
  declare function getMatchedCurrency(currency: Currency, list: Currency[], matchWrappedCurrency?: boolean): Currency | undefined;
365
365
 
366
- export { BaseCurrency, BigintIsh, Currency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, SPLNativeCurrency, SPLToken, SerializedSPLToken, SerializedToken, TEN, THREE, TWO, Token, TradeType, UnifiedCurrency, UnifiedCurrencyAmount, UnifiedNativeCurrency, UnifiedToken, VMType, VM_TYPE_MAXIMA, ZERO, ZERO_ADDRESS, _100, _10000, _9975, computePriceImpact, getCurrencyAddress, getMatchedCurrency, getTokenComparator, getUnifiedCurrencyAddress, isCurrencySorted, isUnifiedCurrencySorted, sortCurrencies, sortUnifiedCurrencies, sortedInsert, sqrt, validateVMTypeInstance };
366
+ /**
367
+ * Multiplier precision: ERC-8056 fixes this at 18 decimals. No precision-query function
368
+ * exists in the spec; do not query from chain or token metadata.
369
+ */
370
+ declare const SCALED_UI_MULTIPLIER_DECIMALS: 18;
371
+ /**
372
+ * Denominator for all multiplier math (`10n ** 18n`). Equivalent to a multiplier of `1.0×`.
373
+ */
374
+ declare const SCALED_UI_DENOMINATOR: bigint;
375
+ /**
376
+ * Branded type marking a `bigint` as a RAW on-chain token amount.
377
+ *
378
+ * Use `asRawAmount(n)` to mark an unbranded bigint. The math functions in this module
379
+ * accept plain `bigint` for ergonomic interop with the rest of the codebase; this brand
380
+ * exists as opt-in documentation at boundaries that want extra type-level discipline.
381
+ */
382
+ type RawAmount = bigint & {
383
+ readonly __brand: 'ScaledUI.RawAmount';
384
+ };
385
+ /**
386
+ * Branded type marking a `bigint` as a UI-display token amount (post-multiplier).
387
+ *
388
+ * See `RawAmount` for usage notes — this brand exists for boundary discipline, not
389
+ * enforced runtime checking.
390
+ */
391
+ type UiAmount = bigint & {
392
+ readonly __brand: 'ScaledUI.UiAmount';
393
+ };
394
+ declare const asRawAmount: (n: bigint) => RawAmount;
395
+ declare const asUiAmount: (n: bigint) => UiAmount;
396
+ /**
397
+ * Returns true if the multiplier represents the identity transform (`1.0×` = `1e18`).
398
+ * Use this for fast-path short-circuits in hot rendering paths (tokenlist render etc.).
399
+ */
400
+ declare function isIdentityScaledUIMultiplier(multiplier: bigint): boolean;
401
+ /**
402
+ * Convert a raw on-chain amount to its UI-display equivalent by applying the multiplier.
403
+ *
404
+ * uiAmount = (rawAmount × multiplier) / 1e18
405
+ *
406
+ * For non-scaled tokens (`multiplier === 1e18`), this is the identity function and
407
+ * short-circuits without bigint mul/div.
408
+ *
409
+ * Prefer the on-chain helper `toUIAmount(rawAmount)` when the contract supports it
410
+ * (interface ID `0xa60bf13d`) for exact rounding-match with the contract. Use this
411
+ * JS implementation as a fallback when the contract lacks the helper, or in pure-math
412
+ * contexts (router, server-side, tests).
413
+ *
414
+ * @param rawAmount the on-chain token amount (as returned by `balanceOf`, transfer events, etc.)
415
+ * @param multiplier the ERC-8056 multiplier (18-decimal fixed point; `1e18 = 1.0×`)
416
+ * @returns the UI-display equivalent
417
+ */
418
+ declare function toScaledUIAmount(rawAmount: bigint, multiplier: bigint): bigint;
419
+ /**
420
+ * Convert a UI-display amount back to its raw on-chain equivalent.
421
+ *
422
+ * rawAmount = (uiAmount × 1e18) / multiplier
423
+ *
424
+ * For non-scaled tokens (`multiplier === 1e18`), this is the identity function and
425
+ * short-circuits without bigint mul/div.
426
+ *
427
+ * Prefer the on-chain helper `fromUIAmount(uiAmount)` when the contract supports it
428
+ * (interface ID `0xa60bf13d`) for exact rounding-match with the contract. Use this
429
+ * JS implementation as a fallback when the contract lacks the helper.
430
+ *
431
+ * @param uiAmount the user-facing amount (post-multiplier)
432
+ * @param multiplier the ERC-8056 multiplier (18-decimal fixed point; `1e18 = 1.0×`)
433
+ * @returns the raw on-chain equivalent (safe to pass to `transfer`/`approve`/router calldata)
434
+ */
435
+ declare function fromScaledUIAmount(uiAmount: bigint, multiplier: bigint): bigint;
436
+ /**
437
+ * Convert a RAW pool price to its UI-display equivalent.
438
+ *
439
+ * A price is "quote per base". Each side scales by its own token's multiplier, so the
440
+ * displayed ratio scales by `quoteMultiplier / baseMultiplier`:
441
+ *
442
+ * uiPrice = rawPrice × (quoteMultiplier / baseMultiplier)
443
+ *
444
+ * Implemented on the underlying fraction (`numerator × mQuote`, `denominator × mBase`), so the
445
+ * decimal `scalar` is untouched — the multiplier is a pure ratio on the human amount and is
446
+ * decimal-agnostic. For an all-identity pair this returns the input Price unchanged (no alloc).
447
+ *
448
+ * Mirror of `toScaledUIAmount` for the two-sided price case. Used by the add-liquidity current
449
+ * price / range / rate displays and the swap execution-price display.
450
+ */
451
+ declare function toScaledUIPrice<TBase extends UnifiedCurrency, TQuote extends UnifiedCurrency>(price: Price<TBase, TQuote>, baseMultiplier: bigint, quoteMultiplier: bigint): Price<TBase, TQuote>;
452
+ /**
453
+ * Convert a UI-display price back to its RAW equivalent — the inverse of `toScaledUIPrice`:
454
+ *
455
+ * rawPrice = uiPrice × (baseMultiplier / quoteMultiplier)
456
+ *
457
+ * Used at the range-input boundary: a user types a UI price into the min/max boxes, which must
458
+ * be converted to the raw price before it feeds tick math. For an all-identity pair this returns
459
+ * the input Price unchanged.
460
+ */
461
+ declare function fromScaledUIPrice<TBase extends UnifiedCurrency, TQuote extends UnifiedCurrency>(price: Price<TBase, TQuote>, baseMultiplier: bigint, quoteMultiplier: bigint): Price<TBase, TQuote>;
462
+
463
+ export { BaseCurrency, BigintIsh, Currency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, RawAmount, Rounding, SCALED_UI_DENOMINATOR, SCALED_UI_MULTIPLIER_DECIMALS, SPLNativeCurrency, SPLToken, SerializedSPLToken, SerializedToken, TEN, THREE, TWO, Token, TradeType, UiAmount, UnifiedCurrency, UnifiedCurrencyAmount, UnifiedNativeCurrency, UnifiedToken, VMType, VM_TYPE_MAXIMA, ZERO, ZERO_ADDRESS, _100, _10000, _9975, asRawAmount, asUiAmount, computePriceImpact, fromScaledUIAmount, fromScaledUIPrice, getCurrencyAddress, getMatchedCurrency, getTokenComparator, getUnifiedCurrencyAddress, isCurrencySorted, isIdentityScaledUIMultiplier, isUnifiedCurrencySorted, sortCurrencies, sortUnifiedCurrencies, sortedInsert, sqrt, toScaledUIAmount, toScaledUIPrice, validateVMTypeInstance };
package/dist/index.js CHANGED
@@ -699,6 +699,40 @@ function getMatchedCurrency(currency, list, matchWrappedCurrency = true) {
699
699
  return void 0;
700
700
  }
701
701
 
702
+ // src/scaledUIAmount.ts
703
+ var SCALED_UI_MULTIPLIER_DECIMALS = 18;
704
+ var SCALED_UI_DENOMINATOR = 10n ** 18n;
705
+ var asRawAmount = (n) => n;
706
+ var asUiAmount = (n) => n;
707
+ function isIdentityScaledUIMultiplier(multiplier) {
708
+ return multiplier === SCALED_UI_DENOMINATOR;
709
+ }
710
+ function toScaledUIAmount(rawAmount, multiplier) {
711
+ if (multiplier === 0n)
712
+ return rawAmount;
713
+ return isIdentityScaledUIMultiplier(multiplier) ? rawAmount : rawAmount * multiplier / SCALED_UI_DENOMINATOR;
714
+ }
715
+ function fromScaledUIAmount(uiAmount, multiplier) {
716
+ if (multiplier === 0n)
717
+ return uiAmount;
718
+ return isIdentityScaledUIMultiplier(multiplier) ? uiAmount : uiAmount * SCALED_UI_DENOMINATOR / multiplier;
719
+ }
720
+ var sanitizeMultiplier = (m) => m === 0n ? SCALED_UI_DENOMINATOR : m;
721
+ function toScaledUIPrice(price, baseMultiplier, quoteMultiplier) {
722
+ const mBase = sanitizeMultiplier(baseMultiplier);
723
+ const mQuote = sanitizeMultiplier(quoteMultiplier);
724
+ if (isIdentityScaledUIMultiplier(mBase) && isIdentityScaledUIMultiplier(mQuote))
725
+ return price;
726
+ return new Price(price.baseCurrency, price.quoteCurrency, price.denominator * mBase, price.numerator * mQuote);
727
+ }
728
+ function fromScaledUIPrice(price, baseMultiplier, quoteMultiplier) {
729
+ const mBase = sanitizeMultiplier(baseMultiplier);
730
+ const mQuote = sanitizeMultiplier(quoteMultiplier);
731
+ if (isIdentityScaledUIMultiplier(mBase) && isIdentityScaledUIMultiplier(mQuote))
732
+ return price;
733
+ return new Price(price.baseCurrency, price.quoteCurrency, price.denominator * mQuote, price.numerator * mBase);
734
+ }
735
+
702
736
  exports.BaseCurrency = BaseCurrency;
703
737
  exports.CurrencyAmount = CurrencyAmount;
704
738
  exports.FIVE = FIVE;
@@ -712,6 +746,8 @@ exports.ONE = ONE;
712
746
  exports.Percent = Percent;
713
747
  exports.Price = Price;
714
748
  exports.Rounding = Rounding;
749
+ exports.SCALED_UI_DENOMINATOR = SCALED_UI_DENOMINATOR;
750
+ exports.SCALED_UI_MULTIPLIER_DECIMALS = SCALED_UI_MULTIPLIER_DECIMALS;
715
751
  exports.SPLNativeCurrency = SPLNativeCurrency;
716
752
  exports.SPLToken = SPLToken;
717
753
  exports.TEN = TEN;
@@ -727,15 +763,22 @@ exports.ZERO_ADDRESS = ZERO_ADDRESS;
727
763
  exports._100 = _100;
728
764
  exports._10000 = _10000;
729
765
  exports._9975 = _9975;
766
+ exports.asRawAmount = asRawAmount;
767
+ exports.asUiAmount = asUiAmount;
730
768
  exports.computePriceImpact = computePriceImpact;
769
+ exports.fromScaledUIAmount = fromScaledUIAmount;
770
+ exports.fromScaledUIPrice = fromScaledUIPrice;
731
771
  exports.getCurrencyAddress = getCurrencyAddress;
732
772
  exports.getMatchedCurrency = getMatchedCurrency;
733
773
  exports.getTokenComparator = getTokenComparator;
734
774
  exports.getUnifiedCurrencyAddress = getUnifiedCurrencyAddress;
735
775
  exports.isCurrencySorted = isCurrencySorted;
776
+ exports.isIdentityScaledUIMultiplier = isIdentityScaledUIMultiplier;
736
777
  exports.isUnifiedCurrencySorted = isUnifiedCurrencySorted;
737
778
  exports.sortCurrencies = sortCurrencies;
738
779
  exports.sortUnifiedCurrencies = sortUnifiedCurrencies;
739
780
  exports.sortedInsert = sortedInsert;
740
781
  exports.sqrt = sqrt;
782
+ exports.toScaledUIAmount = toScaledUIAmount;
783
+ exports.toScaledUIPrice = toScaledUIPrice;
741
784
  exports.validateVMTypeInstance = validateVMTypeInstance;
package/dist/index.mjs CHANGED
@@ -689,4 +689,38 @@ function getMatchedCurrency(currency, list, matchWrappedCurrency = true) {
689
689
  return void 0;
690
690
  }
691
691
 
692
- export { BaseCurrency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, SPLNativeCurrency, SPLToken, TEN, THREE, TWO, Token, TradeType, UnifiedCurrencyAmount, VMType, VM_TYPE_MAXIMA, ZERO, ZERO_ADDRESS, _100, _10000, _9975, computePriceImpact, getCurrencyAddress, getMatchedCurrency, getTokenComparator, getUnifiedCurrencyAddress, isCurrencySorted, isUnifiedCurrencySorted, sortCurrencies, sortUnifiedCurrencies, sortedInsert, sqrt, validateVMTypeInstance };
692
+ // src/scaledUIAmount.ts
693
+ var SCALED_UI_MULTIPLIER_DECIMALS = 18;
694
+ var SCALED_UI_DENOMINATOR = 10n ** 18n;
695
+ var asRawAmount = (n) => n;
696
+ var asUiAmount = (n) => n;
697
+ function isIdentityScaledUIMultiplier(multiplier) {
698
+ return multiplier === SCALED_UI_DENOMINATOR;
699
+ }
700
+ function toScaledUIAmount(rawAmount, multiplier) {
701
+ if (multiplier === 0n)
702
+ return rawAmount;
703
+ return isIdentityScaledUIMultiplier(multiplier) ? rawAmount : rawAmount * multiplier / SCALED_UI_DENOMINATOR;
704
+ }
705
+ function fromScaledUIAmount(uiAmount, multiplier) {
706
+ if (multiplier === 0n)
707
+ return uiAmount;
708
+ return isIdentityScaledUIMultiplier(multiplier) ? uiAmount : uiAmount * SCALED_UI_DENOMINATOR / multiplier;
709
+ }
710
+ var sanitizeMultiplier = (m) => m === 0n ? SCALED_UI_DENOMINATOR : m;
711
+ function toScaledUIPrice(price, baseMultiplier, quoteMultiplier) {
712
+ const mBase = sanitizeMultiplier(baseMultiplier);
713
+ const mQuote = sanitizeMultiplier(quoteMultiplier);
714
+ if (isIdentityScaledUIMultiplier(mBase) && isIdentityScaledUIMultiplier(mQuote))
715
+ return price;
716
+ return new Price(price.baseCurrency, price.quoteCurrency, price.denominator * mBase, price.numerator * mQuote);
717
+ }
718
+ function fromScaledUIPrice(price, baseMultiplier, quoteMultiplier) {
719
+ const mBase = sanitizeMultiplier(baseMultiplier);
720
+ const mQuote = sanitizeMultiplier(quoteMultiplier);
721
+ if (isIdentityScaledUIMultiplier(mBase) && isIdentityScaledUIMultiplier(mQuote))
722
+ return price;
723
+ return new Price(price.baseCurrency, price.quoteCurrency, price.denominator * mQuote, price.numerator * mBase);
724
+ }
725
+
726
+ export { BaseCurrency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, SCALED_UI_DENOMINATOR, SCALED_UI_MULTIPLIER_DECIMALS, SPLNativeCurrency, SPLToken, TEN, THREE, TWO, Token, TradeType, UnifiedCurrencyAmount, VMType, VM_TYPE_MAXIMA, ZERO, ZERO_ADDRESS, _100, _10000, _9975, asRawAmount, asUiAmount, computePriceImpact, fromScaledUIAmount, fromScaledUIPrice, getCurrencyAddress, getMatchedCurrency, getTokenComparator, getUnifiedCurrencyAddress, isCurrencySorted, isIdentityScaledUIMultiplier, isUnifiedCurrencySorted, sortCurrencies, sortUnifiedCurrencies, sortedInsert, sqrt, toScaledUIAmount, toScaledUIPrice, validateVMTypeInstance };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pancakeswap/swap-sdk-core",
3
3
  "license": "MIT",
4
- "version": "1.6.0",
4
+ "version": "1.6.1",
5
5
  "description": "🛠 An SDK for building applications on top of Pancakeswap.",
6
6
  "main": "dist/index.js",
7
7
  "typings": "dist/index.d.ts",