@sundaeswap/math 0.0.0-dev-20250819140139 → 0.0.0-dev-20251014214822

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.
Files changed (32) hide show
  1. package/dist/cjs/PoolMath/ConstantProductPool.js +29 -4
  2. package/dist/cjs/PoolMath/ConstantProductPool.js.map +1 -1
  3. package/dist/cjs/PoolMath/index.js +1 -5
  4. package/dist/cjs/PoolMath/index.js.map +1 -1
  5. package/dist/esm/PoolMath/ConstantProductPool.js +26 -3
  6. package/dist/esm/PoolMath/ConstantProductPool.js.map +1 -1
  7. package/dist/esm/PoolMath/index.js +0 -2
  8. package/dist/esm/PoolMath/index.js.map +1 -1
  9. package/dist/types/PoolMath/ConstantProductPool.d.ts +36 -6
  10. package/dist/types/PoolMath/ConstantProductPool.d.ts.map +1 -1
  11. package/dist/types/PoolMath/index.d.ts +0 -2
  12. package/dist/types/PoolMath/index.d.ts.map +1 -1
  13. package/dist/types/tsconfig.build.tsbuildinfo +1 -1
  14. package/package.json +1 -4
  15. package/src/PoolMath/ConstantProductPool.ts +45 -10
  16. package/src/PoolMath/__tests__/ConstantProductPool.test.ts +4 -6
  17. package/src/PoolMath/index.ts +0 -2
  18. package/dist/cjs/PoolMath/SharedPoolMath.js +0 -33
  19. package/dist/cjs/PoolMath/SharedPoolMath.js.map +0 -1
  20. package/dist/cjs/PoolMath/StableSwapsPool.js +0 -233
  21. package/dist/cjs/PoolMath/StableSwapsPool.js.map +0 -1
  22. package/dist/esm/PoolMath/SharedPoolMath.js +0 -26
  23. package/dist/esm/PoolMath/SharedPoolMath.js.map +0 -1
  24. package/dist/esm/PoolMath/StableSwapsPool.js +0 -216
  25. package/dist/esm/PoolMath/StableSwapsPool.js.map +0 -1
  26. package/dist/types/PoolMath/SharedPoolMath.d.ts +0 -36
  27. package/dist/types/PoolMath/SharedPoolMath.d.ts.map +0 -1
  28. package/dist/types/PoolMath/StableSwapsPool.d.ts +0 -80
  29. package/dist/types/PoolMath/StableSwapsPool.d.ts.map +0 -1
  30. package/src/PoolMath/SharedPoolMath.ts +0 -41
  31. package/src/PoolMath/StableSwapsPool.ts +0 -301
  32. package/src/PoolMath/__tests__/StableSwapsPool.test.ts +0 -338
@@ -3,19 +3,34 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getSwapOutput = exports.getSwapInput = exports.getLp = exports.getFirstLp = exports.getAssetsRatio = exports.calculateLiquidity = exports.addLiquidity = void 0;
6
+ exports.getSwapOutput = exports.getSwapInput = exports.getShare = exports.getLp = exports.getFirstLp = exports.getAssetsRatio = exports.calculateLiquidity = exports.addLiquidity = void 0;
7
7
  exports.getSwapRatio = getSwapRatio;
8
8
  exports.getTokensForLp = void 0;
9
9
  var _asset = require("@sundaeswap/asset");
10
10
  var _fraction = require("@sundaeswap/fraction");
11
11
  var _bigintMath = require("@sundaeswap/bigint-math");
12
- var _index = require("./index.js");
13
12
  function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
14
13
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
15
14
  function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
16
15
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
17
16
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
18
17
  function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
18
+ /**
19
+ * The direction of a ratio (A per B or B per A)
20
+ */
21
+
22
+ /**
23
+ * Asset used in a ratio calculation
24
+ */
25
+
26
+ /**
27
+ * The result of a ratio calculation on a pool
28
+ */
29
+
30
+ /**
31
+ * The amounts for a token pair
32
+ */
33
+
19
34
  /**
20
35
  * Get the lp token amount for a, b
21
36
  * @param a tokenA amount
@@ -31,6 +46,16 @@ var getFirstLp = exports.getFirstLp = function getFirstLp(a, b) {
31
46
  */
32
47
  var getLp = exports.getLp = getFirstLp;
33
48
 
49
+ /**
50
+ * Get the share ratio as Fraction
51
+ * @param lp
52
+ * @param totalLp
53
+ * @returns
54
+ */
55
+ var getShare = exports.getShare = function getShare(lp, totalLp) {
56
+ return new _fraction.Fraction(lp, totalLp);
57
+ };
58
+
34
59
  /**
35
60
  * Calculate the Add (Mixed-Deposit) Liquidity parameters
36
61
  *
@@ -74,7 +99,7 @@ var calculateLiquidity = exports.calculateLiquidity = function calculateLiquidit
74
99
  return {
75
100
  nextTotalLp: newTotalLpTokens,
76
101
  generatedLp: newLpTokens,
77
- shareAfterDeposit: _index.SharedPoolMath.getShare(newLpTokens, newTotalLpTokens),
102
+ shareAfterDeposit: getShare(newLpTokens, newTotalLpTokens),
78
103
  bChange: bChange,
79
104
  aChange: aChange,
80
105
  actualDepositedA: actualDepositedA,
@@ -91,7 +116,7 @@ var addLiquidity = exports.addLiquidity = function addLiquidity(a, aReserve, bRe
91
116
  var nextTotalLp = new _fraction.Fraction(totalLp * (a + aReserve), aReserve).quotient;
92
117
  var lp = nextTotalLp - totalLp;
93
118
  var b = new _fraction.Fraction(bReserve * a, aReserve);
94
- var share = _index.SharedPoolMath.getShare(lp, nextTotalLp);
119
+ var share = getShare(lp, nextTotalLp);
95
120
  if (b.quotient === 0n) {
96
121
  throw new Error("The provided a asset is not enough to equal at least 1 of the b asset.");
97
122
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ConstantProductPool.js","names":["_asset","require","_fraction","_bigintMath","_index","_slicedToArray","r","e","_arrayWithHoles","_iterableToArrayLimit","_unsupportedIterableToArray","_nonIterableRest","TypeError","a","_arrayLikeToArray","t","toString","call","slice","constructor","name","Array","from","test","length","n","l","Symbol","iterator","i","u","f","o","next","Object","done","push","value","isArray","getFirstLp","exports","b","sqrt","getLp","calculateLiquidity","aReserve","bReserve","totalLp","Error","bInUnitsOfA","aChange","bChange","actualDepositedA","actualDepositedB","newLpTokens","newTotalLpTokens","nextTotalLp","generatedLp","shareAfterDeposit","SharedPoolMath","getShare","addLiquidity","Fraction","quotient","lp","share","requiredB","getTokensForLp","getSwapOutput","input","inputReserve","outputReserve","fee","roundOutputUp","asFraction","lt","ZERO","gt","ONE","feeDiff","denominator","numerator","outputNumerator","outputDenominator","output","safeOutput","inputLpFee","nextInputReserve","nextOutputReserve","amountInLessFee","idealPrice","actualPrice","priceImpact","subtract","divide","getSwapInput","gte","BigInt","getAssetsRatio","firstValue","secondValue","first","second","greaterThan","getSwapRatio","direction","assets","calculatedAmount","rawRatio","adaIds","_assets$sort","sort","isASpecial","includes","assetId","isBSpecial","localeCompare","_assets$sort2","firstAsset","secondAsset","quantity","AssetAmount","fromValue","Number","Math","pow","decimals","display","ratioAsFraction","isDivisible","belowMinimumRatio"],"sources":["../../../src/PoolMath/ConstantProductPool.ts"],"sourcesContent":["import { AssetAmount } from \"@sundaeswap/asset\";\nimport { Fraction, type TFractionLike } from \"@sundaeswap/fraction\";\nimport { sqrt } from \"@sundaeswap/bigint-math\";\nimport { SharedPoolMath } from \"./index.js\";\n\n/**\n * Get the lp token amount for a, b\n * @param a tokenA amount\n * @param b tokenB amount\n * @returns the minted lp token amount\n */\nexport const getFirstLp = (a: bigint, b: bigint) => sqrt(a * b);\n\n/**\n * @deprecated\n */\nexport const getLp = getFirstLp;\n\n/**\n * Calculate the Add (Mixed-Deposit) Liquidity parameters\n *\n * @param {bigint} a - The amount of token A to deposit.\n * @param {bigint} b - The amount of token B to deposit.\n * @param {bigint} aReserve - The current reserve of token A in the pool.\n * @param {bigint} bReserve - The current reserve of token B in the pool.\n * @param {bigint} totalLp - The total lp tokens for the pool before the deposit.\n * @throws {Error} If the pool doesn't have enough liquidity.\n * @throws {Error} If either of the deposit amounts is zero.\n * @returns {Object} An object containing:\n * - nextTotalLp: The total lp tokens for the pool after the deposit.\n * - generatedLp: The amount of lp tokens generated by the deposit.\n * - shareAfterDeposit: The depositor's share of the pool after the deposit.\n */\nexport const calculateLiquidity = (\n a: bigint,\n b: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n if (aReserve === 0n || bReserve === 0n) {\n throw new Error(\"Not enough pool liquidity\");\n }\n\n if (a === 0n || b === 0n) {\n throw new Error(\"Cannot use a deposit asset amount of 0\");\n }\n\n const bInUnitsOfA = (b * aReserve) / bReserve;\n\n let aChange: bigint;\n let bChange: bigint;\n\n if (bInUnitsOfA > a) {\n bChange = (bReserve * (bInUnitsOfA - a)) / aReserve;\n aChange = 0n;\n } else {\n aChange = a - bInUnitsOfA;\n bChange = 0n;\n }\n\n // Subtract off the surplus as returned to the user\n const actualDepositedA = a - aChange;\n const actualDepositedB = b - bChange;\n\n // Calculate LP Tokens\n const newLpTokens = (actualDepositedA * totalLp) / aReserve;\n const newTotalLpTokens = totalLp + newLpTokens;\n\n return {\n nextTotalLp: newTotalLpTokens,\n generatedLp: newLpTokens,\n shareAfterDeposit: SharedPoolMath.getShare(newLpTokens, newTotalLpTokens),\n bChange,\n aChange,\n actualDepositedA,\n actualDepositedB,\n };\n};\n\n/**\n * @deprecated\n *\n * Use {@link calculateLiquidity} instead.\n */\nexport const addLiquidity = (\n a: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n const nextTotalLp = new Fraction(totalLp * (a + aReserve), aReserve).quotient;\n const lp = nextTotalLp - totalLp;\n const b = new Fraction(bReserve * a, aReserve);\n const share = SharedPoolMath.getShare(lp, nextTotalLp);\n\n if (b.quotient === 0n) {\n throw new Error(\n \"The provided a asset is not enough to equal at least 1 of the b asset.\",\n );\n }\n\n return {\n nextTotalLp,\n /** @deprecated */\n lp,\n generatedLp: lp,\n /** @deprecated */\n b: b.quotient,\n requiredB: b.quotient,\n /** @deprecated */\n share,\n shareAfterDeposit: share,\n };\n};\n\n/**\n * Get the token amounts the given lp represents\n * @param lp the lp amount\n * @param aReserve the pool's reserveA amount\n * @param bReserve the pool's reserveB amount\n * @param totalLp the pool's total minted lp currently\n * @returns [a, b] token amounts\n */\nexport const getTokensForLp = (\n lp: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n): SharedPoolMath.TPair => [\n new Fraction(lp * aReserve, totalLp).quotient,\n new Fraction(lp * bReserve, totalLp).quotient,\n];\n\n/**\n * Holds the calculated outcome of a swap\n */\nexport type TSwapOutcome = {\n input: bigint;\n output: bigint;\n inputLpFee: bigint;\n nextInputReserve: bigint;\n nextOutputReserve: bigint;\n priceImpact: Fraction;\n};\n\n/**\n * Calculate swap outcome for a given input and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n *\n * @param input The given amount of tokens to be swapped\n * @param inputReserve The amount of tokens in the input reserve\n * @param outputReserve The amount of tokens in the output reserve\n * @param fee The liquidity provider fee\n * @returns The swap details\n */\nexport const getSwapOutput = (\n input: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n roundOutputUp?: boolean,\n): TSwapOutcome => {\n if (input <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Input and reserves must be positive\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gt(Fraction.ONE))\n throw new Error(\"fee must be between 0 and 1\");\n\n const feeDiff = fee.denominator - fee.numerator;\n const outputNumerator = outputReserve * input * feeDiff;\n const outputDenominator = inputReserve * fee.denominator + input * feeDiff;\n const output = new Fraction(outputNumerator, outputDenominator);\n\n const safeOutput = roundOutputUp\n ? (outputNumerator + outputDenominator - 1n) / outputDenominator\n : output.quotient;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - safeOutput;\n\n // PRICEIMPACT: \"priceImpact\" is slightly misleadingly named in the industry as a whole\n // just by it's name, it would imply that it's the percentage difference between\n // the current price and the price after the swap, but it's actually the percentage\n // difference between the real price of your swap, and the price implied by the current\n // reserves; We got this wrong in v1, but this aligns it with the industry standard\n // Source: https://dailydefi.org/articles/price-impact-and-how-to-calculate/\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, safeOutput);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n return {\n input,\n output: safeOutput,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculate input required for a swap outcome for a given output and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n * - output is greater than or equal to output reserve\n *\n * @param output\n * @param inputReserve\n * @param outputReserve\n * @param fee\n * @returns The swap details\n */\nexport const getSwapInput = (\n output: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n): TSwapOutcome => {\n if (output <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Output and reserves must be positive\");\n\n if (output >= outputReserve)\n throw new Error(\"Output must be less than output reserve\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be [0,1)\");\n\n const feeDiff = BigInt(fee.denominator - fee.numerator);\n const input =\n new Fraction(\n inputReserve * output * fee.denominator,\n (outputReserve - output) * feeDiff,\n ).quotient + 1n;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - output;\n\n // See PRICEIMPACT\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, output);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n\n return {\n input,\n output,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculates the ratio between two values.\n *\n * @param {TFractionLike} firstValue - The first value for the ratio calculation.\n * @param {TFractionLike} secondValue - The second value for the ratio calculation.\n * @returns {string|null} The ratio of the first value to the second value, expressed as a string in base 10. If either of the inputs is zero or negative, or if either input is missing, the function returns null.\n * @throws {Error} Will throw an error if the inputs are not valid for the Fraction class's `asFraction` method.\n */\nexport const getAssetsRatio = (\n firstValue: TFractionLike,\n secondValue: TFractionLike,\n) => {\n const first = Fraction.asFraction(firstValue ?? 0);\n const second = Fraction.asFraction(secondValue ?? 0);\n\n if (!first.greaterThan(0) || !second.greaterThan(0)) {\n return \"0\";\n }\n\n return first.divide(second).toString(10);\n};\n\n/**\n * Calculates the swap ratio between two assets and returns an AssetAmount instance representing this ratio.\n *\n * The assets are first sorted lexicographically by their assetId to ensure any calculation will match\n * pool ratios (which are also sorted lexicographically when created). The direction of the swap determines the calculation.\n * For 'A_PER_B', it calculates how much of the lexicographically first asset (A) is obtained for each unit of the\n * lexicographically second asset (B). For 'B_PER_A', it calculates how much of the lexicographically second asset (B)\n * is obtained for each unit of the lexicographically first asset (A).\n *\n * The returned AssetAmount is represented in the decimal format of the asset that is being received in the swap operation.\n * In 'A_PER_B' direction, the lexicographically first asset (A) is received, so the AssetAmount will be in the decimal format\n * of Asset A. In 'B_PER_A' direction, the lexicographically second asset (B) is received, so the AssetAmount will be in the decimal format of Asset B.\n *\n * @param {TRatioDirection} direction - The direction of the swap: 'A_PER_B' means the first asset lexicographically is received and the second is given, 'B_PER_A' means the second asset lexicographically is received and the first is given.\n * @param {[IRatioCalculationAsset, IRatioCalculationAsset]} assets - An array of two assets involved in the swap. The order of the assets in this array does not matter as they will be sorted lexicographically by their assetId inside the function.\n *\n * @returns {TSwapRatio} The calculated swap ratio in different representations.\n *\n * @example\n *\n * ```ts\n * const asset1 = { quantity: 100, decimals: 0, assetId: 'B' };\n * const asset2 = { quantity: 2000000, decimals: 6, assetId: 'A' };\n * const amount = getSwapRatio('A_PER_B', [asset1, asset2]);\n * const sameAmount = getSwapRatio('A_PER_B', [asset2, asset1]);\n * console.log(amount.ratioAsFraction); // Returns a Fraction class of the ratio.\n * console.log(sameAmount.ratioAsFraction); // Same as above.\n * ```\n */\nexport function getSwapRatio(\n direction: SharedPoolMath.TRatioDirection,\n assets: [\n SharedPoolMath.IRatioCalculationAsset,\n SharedPoolMath.IRatioCalculationAsset,\n ],\n): SharedPoolMath.IRatioCalculationResult {\n let calculatedAmount: AssetAmount;\n let rawRatio: string;\n const adaIds = [\"\", \".\", \"ada.lovelace\"];\n\n const [firstAsset, secondAsset] = assets.sort((a, b) => {\n const isASpecial = adaIds.includes(a.assetId);\n const isBSpecial = adaIds.includes(b.assetId);\n\n if (isASpecial && !isBSpecial) {\n return -1;\n }\n\n if (!isASpecial && isBSpecial) {\n return 1;\n }\n\n return a.assetId.localeCompare(b.assetId);\n });\n\n if (direction === \"A_PER_B\") {\n rawRatio = getAssetsRatio(firstAsset.quantity, secondAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (secondAsset.decimals - firstAsset.decimals),\n firstAsset.decimals,\n );\n } else {\n rawRatio = getAssetsRatio(secondAsset.quantity, firstAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (firstAsset.decimals - secondAsset.decimals),\n secondAsset.decimals,\n );\n }\n\n return {\n calculatedAmount: calculatedAmount,\n display: calculatedAmount.value.toString(),\n ratioAsFraction: Fraction.asFraction(rawRatio),\n isDivisible: calculatedAmount.decimals > 0,\n belowMinimumRatio: rawRatio.toString() === \"0\",\n };\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,SAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AAA4C,SAAAI,eAAAC,CAAA,EAAAC,CAAA,WAAAC,eAAA,CAAAF,CAAA,KAAAG,qBAAA,CAAAH,CAAA,EAAAC,CAAA,KAAAG,2BAAA,CAAAJ,CAAA,EAAAC,CAAA,KAAAI,gBAAA;AAAA,SAAAA,iBAAA,cAAAC,SAAA;AAAA,SAAAF,4BAAAJ,CAAA,EAAAO,CAAA,QAAAP,CAAA,2BAAAA,CAAA,SAAAQ,iBAAA,CAAAR,CAAA,EAAAO,CAAA,OAAAE,CAAA,MAAAC,QAAA,CAAAC,IAAA,CAAAX,CAAA,EAAAY,KAAA,6BAAAH,CAAA,IAAAT,CAAA,CAAAa,WAAA,KAAAJ,CAAA,GAAAT,CAAA,CAAAa,WAAA,CAAAC,IAAA,aAAAL,CAAA,cAAAA,CAAA,GAAAM,KAAA,CAAAC,IAAA,CAAAhB,CAAA,oBAAAS,CAAA,+CAAAQ,IAAA,CAAAR,CAAA,IAAAD,iBAAA,CAAAR,CAAA,EAAAO,CAAA;AAAA,SAAAC,kBAAAR,CAAA,EAAAO,CAAA,aAAAA,CAAA,IAAAA,CAAA,GAAAP,CAAA,CAAAkB,MAAA,MAAAX,CAAA,GAAAP,CAAA,CAAAkB,MAAA,YAAAjB,CAAA,MAAAkB,CAAA,GAAAJ,KAAA,CAAAR,CAAA,GAAAN,CAAA,GAAAM,CAAA,EAAAN,CAAA,IAAAkB,CAAA,CAAAlB,CAAA,IAA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BG,QAAQ,GAAGV,cAAc,CAACoB,UAAU,CAACE,QAAQ,EAAED,WAAW,CAACC,QAAQ,CAAC;IACpEb,gBAAgB,GAAGc,kBAAW,CAACC,SAAS,CACtCC,MAAM,CAACf,QAAQ,CAAC,GAAAgB,IAAA,CAAAC,GAAA,CAAG,EAAE,EAAKN,WAAW,CAACO,QAAQ,GAAGR,UAAU,CAACQ,QAAQ,CAAC,EACrER,UAAU,CAACQ,QACb,CAAC;EACH,CAAC,MAAM;IACLlB,QAAQ,GAAGV,cAAc,CAACqB,WAAW,CAACC,QAAQ,EAAEF,UAAU,CAACE,QAAQ,CAAC;IACpEb,gBAAgB,GAAGc,kBAAW,CAACC,SAAS,CACtCC,MAAM,CAACf,QAAQ,CAAC,GAAAgB,IAAA,CAAAC,GAAA,CAAG,EAAE,EAAKP,UAAU,CAACQ,QAAQ,GAAGP,WAAW,CAACO,QAAQ,CAAC,EACrEP,WAAW,CAACO,QACd,CAAC;EACH;EAEA,OAAO;IACLnB,gBAAgB,EAAEA,gBAAgB;IAClCoB,OAAO,EAAEpB,gBAAgB,CAACtE,KAAK,CAACrB,QAAQ,CAAC,CAAC;IAC1CgH,eAAe,EAAElE,kBAAQ,CAACY,UAAU,CAACkC,QAAQ,CAAC;IAC9CqB,WAAW,EAAEtB,gBAAgB,CAACmB,QAAQ,GAAG,CAAC;IAC1CI,iBAAiB,EAAEtB,QAAQ,CAAC5F,QAAQ,CAAC,CAAC,KAAK;EAC7C,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"file":"ConstantProductPool.js","names":["_asset","require","_fraction","_bigintMath","_slicedToArray","r","e","_arrayWithHoles","_iterableToArrayLimit","_unsupportedIterableToArray","_nonIterableRest","TypeError","a","_arrayLikeToArray","t","toString","call","slice","constructor","name","Array","from","test","length","n","l","Symbol","iterator","i","u","f","o","next","Object","done","push","value","isArray","getFirstLp","exports","b","sqrt","getLp","getShare","lp","totalLp","Fraction","calculateLiquidity","aReserve","bReserve","Error","bInUnitsOfA","aChange","bChange","actualDepositedA","actualDepositedB","newLpTokens","newTotalLpTokens","nextTotalLp","generatedLp","shareAfterDeposit","addLiquidity","quotient","share","requiredB","getTokensForLp","getSwapOutput","input","inputReserve","outputReserve","fee","roundOutputUp","asFraction","lt","ZERO","gt","ONE","feeDiff","denominator","numerator","outputNumerator","outputDenominator","output","safeOutput","inputLpFee","nextInputReserve","nextOutputReserve","amountInLessFee","idealPrice","actualPrice","priceImpact","subtract","divide","getSwapInput","gte","BigInt","getAssetsRatio","firstValue","secondValue","first","second","greaterThan","getSwapRatio","direction","assets","calculatedAmount","rawRatio","adaIds","_assets$sort","sort","isASpecial","includes","assetId","isBSpecial","localeCompare","_assets$sort2","firstAsset","secondAsset","quantity","AssetAmount","fromValue","Number","Math","pow","decimals","display","ratioAsFraction","isDivisible","belowMinimumRatio"],"sources":["../../../src/PoolMath/ConstantProductPool.ts"],"sourcesContent":["import { AssetAmount } from \"@sundaeswap/asset\";\nimport { Fraction, type TFractionLike } from \"@sundaeswap/fraction\";\nimport { sqrt } from \"@sundaeswap/bigint-math\";\n\n/**\n * The direction of a ratio (A per B or B per A)\n */\nexport type TRatioDirection = \"A_PER_B\" | \"B_PER_A\";\n\n/**\n * Asset used in a ratio calculation\n */\nexport interface IRatioCalculationAsset {\n assetId: string;\n quantity: bigint;\n decimals: number;\n}\n\n/**\n * The result of a ratio calculation on a pool\n */\nexport interface IRatioCalculationResult {\n calculatedAmount: AssetAmount;\n ratioAsFraction: Fraction;\n display: string;\n isDivisible: boolean;\n belowMinimumRatio: boolean;\n}\n\n/**\n * The amounts for a token pair\n */\nexport type TPair = [bigint, bigint];\n\n/**\n * Get the lp token amount for a, b\n * @param a tokenA amount\n * @param b tokenB amount\n * @returns the minted lp token amount\n */\nexport const getFirstLp = (a: bigint, b: bigint) => sqrt(a * b);\n\n/**\n * @deprecated\n */\nexport const getLp = getFirstLp;\n\n/**\n * Get the share ratio as Fraction\n * @param lp\n * @param totalLp\n * @returns\n */\nexport const getShare = (lp: bigint, totalLp: bigint) =>\n new Fraction(lp, totalLp);\n\n/**\n * Calculate the Add (Mixed-Deposit) Liquidity parameters\n *\n * @param {bigint} a - The amount of token A to deposit.\n * @param {bigint} b - The amount of token B to deposit.\n * @param {bigint} aReserve - The current reserve of token A in the pool.\n * @param {bigint} bReserve - The current reserve of token B in the pool.\n * @param {bigint} totalLp - The total lp tokens for the pool before the deposit.\n * @throws {Error} If the pool doesn't have enough liquidity.\n * @throws {Error} If either of the deposit amounts is zero.\n * @returns {Object} An object containing:\n * - nextTotalLp: The total lp tokens for the pool after the deposit.\n * - generatedLp: The amount of lp tokens generated by the deposit.\n * - shareAfterDeposit: The depositor's share of the pool after the deposit.\n */\nexport const calculateLiquidity = (\n a: bigint,\n b: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n if (aReserve === 0n || bReserve === 0n) {\n throw new Error(\"Not enough pool liquidity\");\n }\n\n if (a === 0n || b === 0n) {\n throw new Error(\"Cannot use a deposit asset amount of 0\");\n }\n\n const bInUnitsOfA = (b * aReserve) / bReserve;\n\n let aChange: bigint;\n let bChange: bigint;\n\n if (bInUnitsOfA > a) {\n bChange = (bReserve * (bInUnitsOfA - a)) / aReserve;\n aChange = 0n;\n } else {\n aChange = a - bInUnitsOfA;\n bChange = 0n;\n }\n\n // Subtract off the surplus as returned to the user\n const actualDepositedA = a - aChange;\n const actualDepositedB = b - bChange;\n\n // Calculate LP Tokens\n const newLpTokens = (actualDepositedA * totalLp) / aReserve;\n const newTotalLpTokens = totalLp + newLpTokens;\n\n return {\n nextTotalLp: newTotalLpTokens,\n generatedLp: newLpTokens,\n shareAfterDeposit: getShare(newLpTokens, newTotalLpTokens),\n bChange,\n aChange,\n actualDepositedA,\n actualDepositedB,\n };\n};\n\n/**\n * @deprecated\n *\n * Use {@link calculateLiquidity} instead.\n */\nexport const addLiquidity = (\n a: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n const nextTotalLp = new Fraction(totalLp * (a + aReserve), aReserve).quotient;\n const lp = nextTotalLp - totalLp;\n const b = new Fraction(bReserve * a, aReserve);\n const share = getShare(lp, nextTotalLp);\n\n if (b.quotient === 0n) {\n throw new Error(\n \"The provided a asset is not enough to equal at least 1 of the b asset.\",\n );\n }\n\n return {\n nextTotalLp,\n /** @deprecated */\n lp,\n generatedLp: lp,\n /** @deprecated */\n b: b.quotient,\n requiredB: b.quotient,\n /** @deprecated */\n share,\n shareAfterDeposit: share,\n };\n};\n\n/**\n * Get the token amounts the given lp represents\n * @param lp the lp amount\n * @param aReserve the pool's reserveA amount\n * @param bReserve the pool's reserveB amount\n * @param totalLp the pool's total minted lp currently\n * @returns [a, b] token amounts\n */\nexport const getTokensForLp = (\n lp: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n): TPair => [\n new Fraction(lp * aReserve, totalLp).quotient,\n new Fraction(lp * bReserve, totalLp).quotient,\n];\n\n/**\n * Holds the calculated outcome of a swap\n */\nexport type TSwapOutcome = {\n input: bigint;\n output: bigint;\n inputLpFee: bigint;\n nextInputReserve: bigint;\n nextOutputReserve: bigint;\n priceImpact: Fraction;\n};\n\n/**\n * Calculate swap outcome for a given input and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n *\n * @param input The given amount of tokens to be swapped\n * @param inputReserve The amount of tokens in the input reserve\n * @param outputReserve The amount of tokens in the output reserve\n * @param fee The liquidity provider fee\n * @returns The swap details\n */\nexport const getSwapOutput = (\n input: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n roundOutputUp?: boolean,\n): TSwapOutcome => {\n if (input <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Input and reserves must be positive\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gt(Fraction.ONE))\n throw new Error(\"fee must be between 0 and 1\");\n\n const feeDiff = fee.denominator - fee.numerator;\n const outputNumerator = outputReserve * input * feeDiff;\n const outputDenominator = inputReserve * fee.denominator + input * feeDiff;\n const output = new Fraction(outputNumerator, outputDenominator);\n\n const safeOutput = roundOutputUp\n ? (outputNumerator + outputDenominator - 1n) / outputDenominator\n : output.quotient;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - safeOutput;\n\n // PRICEIMPACT: \"priceImpact\" is slightly misleadingly named in the industry as a whole\n // just by it's name, it would imply that it's the percentage difference between\n // the current price and the price after the swap, but it's actually the percentage\n // difference between the real price of your swap, and the price implied by the current\n // reserves; We got this wrong in v1, but this aligns it with the industry standard\n // Source: https://dailydefi.org/articles/price-impact-and-how-to-calculate/\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, safeOutput);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n return {\n input,\n output: safeOutput,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculate input required for a swap outcome for a given output and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n * - output is greater than or equal to output reserve\n *\n * @param output\n * @param inputReserve\n * @param outputReserve\n * @param fee\n * @returns The swap details\n */\nexport const getSwapInput = (\n output: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n): TSwapOutcome => {\n if (output <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Output and reserves must be positive\");\n\n if (output >= outputReserve)\n throw new Error(\"Output must be less than output reserve\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be [0,1)\");\n\n const feeDiff = BigInt(fee.denominator - fee.numerator);\n const input =\n new Fraction(\n inputReserve * output * fee.denominator,\n (outputReserve - output) * feeDiff,\n ).quotient + 1n;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - output;\n\n // See PRICEIMPACT\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, output);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n\n return {\n input,\n output,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculates the ratio between two values.\n *\n * @param {TFractionLike} firstValue - The first value for the ratio calculation.\n * @param {TFractionLike} secondValue - The second value for the ratio calculation.\n * @returns {string|null} The ratio of the first value to the second value, expressed as a string in base 10. If either of the inputs is zero or negative, or if either input is missing, the function returns null.\n * @throws {Error} Will throw an error if the inputs are not valid for the Fraction class's `asFraction` method.\n */\nexport const getAssetsRatio = (\n firstValue: TFractionLike,\n secondValue: TFractionLike,\n) => {\n const first = Fraction.asFraction(firstValue ?? 0);\n const second = Fraction.asFraction(secondValue ?? 0);\n\n if (!first.greaterThan(0) || !second.greaterThan(0)) {\n return \"0\";\n }\n\n return first.divide(second).toString(10);\n};\n\n/**\n * Calculates the swap ratio between two assets and returns an AssetAmount instance representing this ratio.\n *\n * The assets are first sorted lexicographically by their assetId to ensure any calculation will match\n * pool ratios (which are also sorted lexicographically when created). The direction of the swap determines the calculation.\n * For 'A_PER_B', it calculates how much of the lexicographically first asset (A) is obtained for each unit of the\n * lexicographically second asset (B). For 'B_PER_A', it calculates how much of the lexicographically second asset (B)\n * is obtained for each unit of the lexicographically first asset (A).\n *\n * The returned AssetAmount is represented in the decimal format of the asset that is being received in the swap operation.\n * In 'A_PER_B' direction, the lexicographically first asset (A) is received, so the AssetAmount will be in the decimal format\n * of Asset A. In 'B_PER_A' direction, the lexicographically second asset (B) is received, so the AssetAmount will be in the decimal format of Asset B.\n *\n * @param {TRatioDirection} direction - The direction of the swap: 'A_PER_B' means the first asset lexicographically is received and the second is given, 'B_PER_A' means the second asset lexicographically is received and the first is given.\n * @param {[IRatioCalculationAsset, IRatioCalculationAsset]} assets - An array of two assets involved in the swap. The order of the assets in this array does not matter as they will be sorted lexicographically by their assetId inside the function.\n *\n * @returns {TSwapRatio} The calculated swap ratio in different representations.\n *\n * @example\n *\n * ```ts\n * const asset1 = { quantity: 100, decimals: 0, assetId: 'B' };\n * const asset2 = { quantity: 2000000, decimals: 6, assetId: 'A' };\n * const amount = getSwapRatio('A_PER_B', [asset1, asset2]);\n * const sameAmount = getSwapRatio('A_PER_B', [asset2, asset1]);\n * console.log(amount.ratioAsFraction); // Returns a Fraction class of the ratio.\n * console.log(sameAmount.ratioAsFraction); // Same as above.\n * ```\n */\nexport function getSwapRatio(\n direction: TRatioDirection,\n assets: [IRatioCalculationAsset, IRatioCalculationAsset],\n): IRatioCalculationResult {\n let calculatedAmount: AssetAmount;\n let rawRatio: string;\n const adaIds = [\"\", \".\", \"ada.lovelace\"];\n\n const [firstAsset, secondAsset] = assets.sort((a, b) => {\n const isASpecial = adaIds.includes(a.assetId);\n const isBSpecial = adaIds.includes(b.assetId);\n\n if (isASpecial && !isBSpecial) {\n return -1;\n }\n\n if (!isASpecial && isBSpecial) {\n return 1;\n }\n\n return a.assetId.localeCompare(b.assetId);\n });\n\n if (direction === \"A_PER_B\") {\n rawRatio = getAssetsRatio(firstAsset.quantity, secondAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (secondAsset.decimals - firstAsset.decimals),\n firstAsset.decimals,\n );\n } else {\n rawRatio = getAssetsRatio(secondAsset.quantity, firstAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (firstAsset.decimals - secondAsset.decimals),\n secondAsset.decimals,\n );\n }\n\n return {\n calculatedAmount: calculatedAmount,\n display: calculatedAmount.value.toString(),\n ratioAsFraction: Fraction.asFraction(rawRatio),\n isDivisible: calculatedAmount.decim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AAD,aAAA;IAAEE,WAAW,GAAAF,aAAA;EAe9B,IAAIZ,SAAS,KAAK,SAAS,EAAE;IAC3BG,QAAQ,GAAGV,cAAc,CAACoB,UAAU,CAACE,QAAQ,EAAED,WAAW,CAACC,QAAQ,CAAC;IACpEb,gBAAgB,GAAGc,kBAAW,CAACC,SAAS,CACtCC,MAAM,CAACf,QAAQ,CAAC,GAAAgB,IAAA,CAAAC,GAAA,CAAG,EAAE,EAAKN,WAAW,CAACO,QAAQ,GAAGR,UAAU,CAACQ,QAAQ,CAAC,EACrER,UAAU,CAACQ,QACb,CAAC;EACH,CAAC,MAAM;IACLlB,QAAQ,GAAGV,cAAc,CAACqB,WAAW,CAACC,QAAQ,EAAEF,UAAU,CAACE,QAAQ,CAAC;IACpEb,gBAAgB,GAAGc,kBAAW,CAACC,SAAS,CACtCC,MAAM,CAACf,QAAQ,CAAC,GAAAgB,IAAA,CAAAC,GAAA,CAAG,EAAE,EAAKP,UAAU,CAACQ,QAAQ,GAAGP,WAAW,CAACO,QAAQ,CAAC,EACrEP,WAAW,CAACO,QACd,CAAC;EACH;EAEA,OAAO;IACLnB,gBAAgB,EAAEA,gBAAgB;IAClCoB,OAAO,EAAEpB,gBAAgB,CAACrE,KAAK,CAACrB,QAAQ,CAAC,CAAC;IAC1C+G,eAAe,EAAEhF,kBAAQ,CAAC0B,UAAU,CAACkC,QAAQ,CAAC;IAC9CqB,WAAW,EAAEtB,gBAAgB,CAACmB,QAAQ,GAAG,CAAC;IAC1CI,iBAAiB,EAAEtB,QAAQ,CAAC3F,QAAQ,CAAC,CAAC,KAAK;EAC7C,CAAC;AACH","ignoreList":[]}
@@ -4,12 +4,8 @@ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" ==
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.StableSwapsPool = exports.SharedPoolMath = exports.ConstantProductPool = void 0;
7
+ exports.ConstantProductPool = void 0;
8
8
  var _ConstantProductPool = _interopRequireWildcard(require("./ConstantProductPool.js"));
9
9
  exports.ConstantProductPool = _ConstantProductPool;
10
- var _StableSwapsPool = _interopRequireWildcard(require("./StableSwapsPool.js"));
11
- exports.StableSwapsPool = _StableSwapsPool;
12
- var _SharedPoolMath = _interopRequireWildcard(require("./SharedPoolMath.js"));
13
- exports.SharedPoolMath = _SharedPoolMath;
14
10
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
15
11
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../../src/PoolMath/index.ts"],"sourcesContent":["export * as ConstantProductPool from \"./ConstantProductPool.js\";\nexport * as StableSwapsPool from \"./StableSwapsPool.js\";\nexport * as SharedPoolMath from \"./SharedPoolMath.js\";\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../src/PoolMath/index.ts"],"sourcesContent":["export * as ConstantProductPool from \"./ConstantProductPool.js\";\n"],"mappings":"","ignoreList":[]}
@@ -1,7 +1,22 @@
1
1
  import { AssetAmount } from "@sundaeswap/asset";
2
2
  import { Fraction } from "@sundaeswap/fraction";
3
3
  import { sqrt } from "@sundaeswap/bigint-math";
4
- import { SharedPoolMath } from "./index.js";
4
+
5
+ /**
6
+ * The direction of a ratio (A per B or B per A)
7
+ */
8
+
9
+ /**
10
+ * Asset used in a ratio calculation
11
+ */
12
+
13
+ /**
14
+ * The result of a ratio calculation on a pool
15
+ */
16
+
17
+ /**
18
+ * The amounts for a token pair
19
+ */
5
20
 
6
21
  /**
7
22
  * Get the lp token amount for a, b
@@ -16,6 +31,14 @@ export const getFirstLp = (a, b) => sqrt(a * b);
16
31
  */
17
32
  export const getLp = getFirstLp;
18
33
 
34
+ /**
35
+ * Get the share ratio as Fraction
36
+ * @param lp
37
+ * @param totalLp
38
+ * @returns
39
+ */
40
+ export const getShare = (lp, totalLp) => new Fraction(lp, totalLp);
41
+
19
42
  /**
20
43
  * Calculate the Add (Mixed-Deposit) Liquidity parameters
21
44
  *
@@ -59,7 +82,7 @@ export const calculateLiquidity = (a, b, aReserve, bReserve, totalLp) => {
59
82
  return {
60
83
  nextTotalLp: newTotalLpTokens,
61
84
  generatedLp: newLpTokens,
62
- shareAfterDeposit: SharedPoolMath.getShare(newLpTokens, newTotalLpTokens),
85
+ shareAfterDeposit: getShare(newLpTokens, newTotalLpTokens),
63
86
  bChange,
64
87
  aChange,
65
88
  actualDepositedA,
@@ -76,7 +99,7 @@ export const addLiquidity = (a, aReserve, bReserve, totalLp) => {
76
99
  const nextTotalLp = new Fraction(totalLp * (a + aReserve), aReserve).quotient;
77
100
  const lp = nextTotalLp - totalLp;
78
101
  const b = new Fraction(bReserve * a, aReserve);
79
- const share = SharedPoolMath.getShare(lp, nextTotalLp);
102
+ const share = getShare(lp, nextTotalLp);
80
103
  if (b.quotient === 0n) {
81
104
  throw new Error("The provided a asset is not enough to equal at least 1 of the b asset.");
82
105
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ConstantProductPool.js","names":["AssetAmount","Fraction","sqrt","SharedPoolMath","getFirstLp","a","b","getLp","calculateLiquidity","aReserve","bReserve","totalLp","Error","bInUnitsOfA","aChange","bChange","actualDepositedA","actualDepositedB","newLpTokens","newTotalLpTokens","nextTotalLp","generatedLp","shareAfterDeposit","getShare","addLiquidity","quotient","lp","share","requiredB","getTokensForLp","getSwapOutput","input","inputReserve","outputReserve","fee","roundOutputUp","asFraction","lt","ZERO","gt","ONE","feeDiff","denominator","numerator","outputNumerator","outputDenominator","output","safeOutput","inputLpFee","nextInputReserve","nextOutputReserve","amountInLessFee","idealPrice","actualPrice","priceImpact","subtract","divide","getSwapInput","gte","BigInt","getAssetsRatio","firstValue","secondValue","first","second","greaterThan","toString","getSwapRatio","direction","assets","calculatedAmount","rawRatio","adaIds","firstAsset","secondAsset","sort","isASpecial","includes","assetId","isBSpecial","localeCompare","quantity","fromValue","Number","decimals","display","value","ratioAsFraction","isDivisible","belowMinimumRatio"],"sources":["../../../src/PoolMath/ConstantProductPool.ts"],"sourcesContent":["import { AssetAmount } from \"@sundaeswap/asset\";\nimport { Fraction, type TFractionLike } from \"@sundaeswap/fraction\";\nimport { sqrt } from \"@sundaeswap/bigint-math\";\nimport { SharedPoolMath } from \"./index.js\";\n\n/**\n * Get the lp token amount for a, b\n * @param a tokenA amount\n * @param b tokenB amount\n * @returns the minted lp token amount\n */\nexport const getFirstLp = (a: bigint, b: bigint) => sqrt(a * b);\n\n/**\n * @deprecated\n */\nexport const getLp = getFirstLp;\n\n/**\n * Calculate the Add (Mixed-Deposit) Liquidity parameters\n *\n * @param {bigint} a - The amount of token A to deposit.\n * @param {bigint} b - The amount of token B to deposit.\n * @param {bigint} aReserve - The current reserve of token A in the pool.\n * @param {bigint} bReserve - The current reserve of token B in the pool.\n * @param {bigint} totalLp - The total lp tokens for the pool before the deposit.\n * @throws {Error} If the pool doesn't have enough liquidity.\n * @throws {Error} If either of the deposit amounts is zero.\n * @returns {Object} An object containing:\n * - nextTotalLp: The total lp tokens for the pool after the deposit.\n * - generatedLp: The amount of lp tokens generated by the deposit.\n * - shareAfterDeposit: The depositor's share of the pool after the deposit.\n */\nexport const calculateLiquidity = (\n a: bigint,\n b: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n if (aReserve === 0n || bReserve === 0n) {\n throw new Error(\"Not enough pool liquidity\");\n }\n\n if (a === 0n || b === 0n) {\n throw new Error(\"Cannot use a deposit asset amount of 0\");\n }\n\n const bInUnitsOfA = (b * aReserve) / bReserve;\n\n let aChange: bigint;\n let bChange: bigint;\n\n if (bInUnitsOfA > a) {\n bChange = (bReserve * (bInUnitsOfA - a)) / aReserve;\n aChange = 0n;\n } else {\n aChange = a - bInUnitsOfA;\n bChange = 0n;\n }\n\n // Subtract off the surplus as returned to the user\n const actualDepositedA = a - aChange;\n const actualDepositedB = b - bChange;\n\n // Calculate LP Tokens\n const newLpTokens = (actualDepositedA * totalLp) / aReserve;\n const newTotalLpTokens = totalLp + newLpTokens;\n\n return {\n nextTotalLp: newTotalLpTokens,\n generatedLp: newLpTokens,\n shareAfterDeposit: SharedPoolMath.getShare(newLpTokens, newTotalLpTokens),\n bChange,\n aChange,\n actualDepositedA,\n actualDepositedB,\n };\n};\n\n/**\n * @deprecated\n *\n * Use {@link calculateLiquidity} instead.\n */\nexport const addLiquidity = (\n a: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n const nextTotalLp = new Fraction(totalLp * (a + aReserve), aReserve).quotient;\n const lp = nextTotalLp - totalLp;\n const b = new Fraction(bReserve * a, aReserve);\n const share = SharedPoolMath.getShare(lp, nextTotalLp);\n\n if (b.quotient === 0n) {\n throw new Error(\n \"The provided a asset is not enough to equal at least 1 of the b asset.\",\n );\n }\n\n return {\n nextTotalLp,\n /** @deprecated */\n lp,\n generatedLp: lp,\n /** @deprecated */\n b: b.quotient,\n requiredB: b.quotient,\n /** @deprecated */\n share,\n shareAfterDeposit: share,\n };\n};\n\n/**\n * Get the token amounts the given lp represents\n * @param lp the lp amount\n * @param aReserve the pool's reserveA amount\n * @param bReserve the pool's reserveB amount\n * @param totalLp the pool's total minted lp currently\n * @returns [a, b] token amounts\n */\nexport const getTokensForLp = (\n lp: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n): SharedPoolMath.TPair => [\n new Fraction(lp * aReserve, totalLp).quotient,\n new Fraction(lp * bReserve, totalLp).quotient,\n];\n\n/**\n * Holds the calculated outcome of a swap\n */\nexport type TSwapOutcome = {\n input: bigint;\n output: bigint;\n inputLpFee: bigint;\n nextInputReserve: bigint;\n nextOutputReserve: bigint;\n priceImpact: Fraction;\n};\n\n/**\n * Calculate swap outcome for a given input and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n *\n * @param input The given amount of tokens to be swapped\n * @param inputReserve The amount of tokens in the input reserve\n * @param outputReserve The amount of tokens in the output reserve\n * @param fee The liquidity provider fee\n * @returns The swap details\n */\nexport const getSwapOutput = (\n input: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n roundOutputUp?: boolean,\n): TSwapOutcome => {\n if (input <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Input and reserves must be positive\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gt(Fraction.ONE))\n throw new Error(\"fee must be between 0 and 1\");\n\n const feeDiff = fee.denominator - fee.numerator;\n const outputNumerator = outputReserve * input * feeDiff;\n const outputDenominator = inputReserve * fee.denominator + input * feeDiff;\n const output = new Fraction(outputNumerator, outputDenominator);\n\n const safeOutput = roundOutputUp\n ? (outputNumerator + outputDenominator - 1n) / outputDenominator\n : output.quotient;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - safeOutput;\n\n // PRICEIMPACT: \"priceImpact\" is slightly misleadingly named in the industry as a whole\n // just by it's name, it would imply that it's the percentage difference between\n // the current price and the price after the swap, but it's actually the percentage\n // difference between the real price of your swap, and the price implied by the current\n // reserves; We got this wrong in v1, but this aligns it with the industry standard\n // Source: https://dailydefi.org/articles/price-impact-and-how-to-calculate/\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, safeOutput);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n return {\n input,\n output: safeOutput,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculate input required for a swap outcome for a given output and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n * - output is greater than or equal to output reserve\n *\n * @param output\n * @param inputReserve\n * @param outputReserve\n * @param fee\n * @returns The swap details\n */\nexport const getSwapInput = (\n output: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n): TSwapOutcome => {\n if (output <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Output and reserves must be positive\");\n\n if (output >= outputReserve)\n throw new Error(\"Output must be less than output reserve\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be [0,1)\");\n\n const feeDiff = BigInt(fee.denominator - fee.numerator);\n const input =\n new Fraction(\n inputReserve * output * fee.denominator,\n (outputReserve - output) * feeDiff,\n ).quotient + 1n;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - output;\n\n // See PRICEIMPACT\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, output);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n\n return {\n input,\n output,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculates the ratio between two values.\n *\n * @param {TFractionLike} firstValue - The first value for the ratio calculation.\n * @param {TFractionLike} secondValue - The second value for the ratio calculation.\n * @returns {string|null} The ratio of the first value to the second value, expressed as a string in base 10. If either of the inputs is zero or negative, or if either input is missing, the function returns null.\n * @throws {Error} Will throw an error if the inputs are not valid for the Fraction class's `asFraction` method.\n */\nexport const getAssetsRatio = (\n firstValue: TFractionLike,\n secondValue: TFractionLike,\n) => {\n const first = Fraction.asFraction(firstValue ?? 0);\n const second = Fraction.asFraction(secondValue ?? 0);\n\n if (!first.greaterThan(0) || !second.greaterThan(0)) {\n return \"0\";\n }\n\n return first.divide(second).toString(10);\n};\n\n/**\n * Calculates the swap ratio between two assets and returns an AssetAmount instance representing this ratio.\n *\n * The assets are first sorted lexicographically by their assetId to ensure any calculation will match\n * pool ratios (which are also sorted lexicographically when created). The direction of the swap determines the calculation.\n * For 'A_PER_B', it calculates how much of the lexicographically first asset (A) is obtained for each unit of the\n * lexicographically second asset (B). For 'B_PER_A', it calculates how much of the lexicographically second asset (B)\n * is obtained for each unit of the lexicographically first asset (A).\n *\n * The returned AssetAmount is represented in the decimal format of the asset that is being received in the swap operation.\n * In 'A_PER_B' direction, the lexicographically first asset (A) is received, so the AssetAmount will be in the decimal format\n * of Asset A. In 'B_PER_A' direction, the lexicographically second asset (B) is received, so the AssetAmount will be in the decimal format of Asset B.\n *\n * @param {TRatioDirection} direction - The direction of the swap: 'A_PER_B' means the first asset lexicographically is received and the second is given, 'B_PER_A' means the second asset lexicographically is received and the first is given.\n * @param {[IRatioCalculationAsset, IRatioCalculationAsset]} assets - An array of two assets involved in the swap. The order of the assets in this array does not matter as they will be sorted lexicographically by their assetId inside the function.\n *\n * @returns {TSwapRatio} The calculated swap ratio in different representations.\n *\n * @example\n *\n * ```ts\n * const asset1 = { quantity: 100, decimals: 0, assetId: 'B' };\n * const asset2 = { quantity: 2000000, decimals: 6, assetId: 'A' };\n * const amount = getSwapRatio('A_PER_B', [asset1, asset2]);\n * const sameAmount = getSwapRatio('A_PER_B', [asset2, asset1]);\n * console.log(amount.ratioAsFraction); // Returns a Fraction class of the ratio.\n * console.log(sameAmount.ratioAsFraction); // Same as above.\n * ```\n */\nexport function getSwapRatio(\n direction: SharedPoolMath.TRatioDirection,\n assets: [\n SharedPoolMath.IRatioCalculationAsset,\n SharedPoolMath.IRatioCalculationAsset,\n ],\n): SharedPoolMath.IRatioCalculationResult {\n let calculatedAmount: AssetAmount;\n let rawRatio: string;\n const adaIds = [\"\", \".\", \"ada.lovelace\"];\n\n const [firstAsset, secondAsset] = assets.sort((a, b) => {\n const isASpecial = adaIds.includes(a.assetId);\n const isBSpecial = adaIds.includes(b.assetId);\n\n if (isASpecial && !isBSpecial) {\n return -1;\n }\n\n if (!isASpecial && isBSpecial) {\n return 1;\n }\n\n return a.assetId.localeCompare(b.assetId);\n });\n\n if (direction === \"A_PER_B\") {\n rawRatio = getAssetsRatio(firstAsset.quantity, secondAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (secondAsset.decimals - firstAsset.decimals),\n firstAsset.decimals,\n );\n } else {\n rawRatio = getAssetsRatio(secondAsset.quantity, firstAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (firstAsset.decimals - secondAsset.decimals),\n secondAsset.decimals,\n );\n }\n\n return {\n calculatedAmount: calculatedAmount,\n display: calculatedAmount.value.toString(),\n ratioAsFraction: Fraction.asFraction(rawRatio),\n isDivisible: calculatedAmount.decimals > 0,\n belowMinimumRatio: rawRatio.toString() === \"0\",\n };\n}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,mBAAmB;AAC/C,SAASC,QAAQ,QAA4B,sBAAsB;AACnE,SAASC,IAAI,QAAQ,yBAAyB;AAC9C,SAASC,cAAc,QAAQ,YAAY;;AAE3C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,UAAU,GAAGA,CAACC,CAAS,EAAEC,CAAS,KAAKJ,IAAI,CAACG,CAAC,GAAGC,CAAC,CAAC;;AAE/D;AACA;AACA;AACA,OAAO,MAAMC,KAAK,GAAGH,UAAU;;AAE/B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMI,kBAAkB,GAAGA,CAChCH,CAAS,EACTC,CAAS,EACTG,QAAgB,EAChB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B,CAACc,QAAQ,GAAG,CAAC;IAC1CK,iBAAiB,EAAElB,QAAQ,CAACL,QAAQ,CAAC,CAAC,KAAK;EAC7C,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"file":"ConstantProductPool.js","names":["AssetAmount","Fraction","sqrt","getFirstLp","a","b","getLp","getShare","lp","totalLp","calculateLiquidity","aReserve","bReserve","Error","bInUnitsOfA","aChange","bChange","actualDepositedA","actualDepositedB","newLpTokens","newTotalLpTokens","nextTotalLp","generatedLp","shareAfterDeposit","addLiquidity","quotient","share","requiredB","getTokensForLp","getSwapOutput","input","inputReserve","outputReserve","fee","roundOutputUp","asFraction","lt","ZERO","gt","ONE","feeDiff","denominator","numerator","outputNumerator","outputDenominator","output","safeOutput","inputLpFee","nextInputReserve","nextOutputReserve","amountInLessFee","idealPrice","actualPrice","priceImpact","subtract","divide","getSwapInput","gte","BigInt","getAssetsRatio","firstValue","secondValue","first","second","greaterThan","toString","getSwapRatio","direction","assets","calculatedAmount","rawRatio","adaIds","firstAsset","secondAsset","sort","isASpecial","includes","assetId","isBSpecial","localeCompare","quantity","fromValue","Number","decimals","display","value","ratioAsFraction","isDivisible","belowMinimumRatio"],"sources":["../../../src/PoolMath/ConstantProductPool.ts"],"sourcesContent":["import { AssetAmount } from \"@sundaeswap/asset\";\nimport { Fraction, type TFractionLike } from \"@sundaeswap/fraction\";\nimport { sqrt } from \"@sundaeswap/bigint-math\";\n\n/**\n * The direction of a ratio (A per B or B per A)\n */\nexport type TRatioDirection = \"A_PER_B\" | \"B_PER_A\";\n\n/**\n * Asset used in a ratio calculation\n */\nexport interface IRatioCalculationAsset {\n assetId: string;\n quantity: bigint;\n decimals: number;\n}\n\n/**\n * The result of a ratio calculation on a pool\n */\nexport interface IRatioCalculationResult {\n calculatedAmount: AssetAmount;\n ratioAsFraction: Fraction;\n display: string;\n isDivisible: boolean;\n belowMinimumRatio: boolean;\n}\n\n/**\n * The amounts for a token pair\n */\nexport type TPair = [bigint, bigint];\n\n/**\n * Get the lp token amount for a, b\n * @param a tokenA amount\n * @param b tokenB amount\n * @returns the minted lp token amount\n */\nexport const getFirstLp = (a: bigint, b: bigint) => sqrt(a * b);\n\n/**\n * @deprecated\n */\nexport const getLp = getFirstLp;\n\n/**\n * Get the share ratio as Fraction\n * @param lp\n * @param totalLp\n * @returns\n */\nexport const getShare = (lp: bigint, totalLp: bigint) =>\n new Fraction(lp, totalLp);\n\n/**\n * Calculate the Add (Mixed-Deposit) Liquidity parameters\n *\n * @param {bigint} a - The amount of token A to deposit.\n * @param {bigint} b - The amount of token B to deposit.\n * @param {bigint} aReserve - The current reserve of token A in the pool.\n * @param {bigint} bReserve - The current reserve of token B in the pool.\n * @param {bigint} totalLp - The total lp tokens for the pool before the deposit.\n * @throws {Error} If the pool doesn't have enough liquidity.\n * @throws {Error} If either of the deposit amounts is zero.\n * @returns {Object} An object containing:\n * - nextTotalLp: The total lp tokens for the pool after the deposit.\n * - generatedLp: The amount of lp tokens generated by the deposit.\n * - shareAfterDeposit: The depositor's share of the pool after the deposit.\n */\nexport const calculateLiquidity = (\n a: bigint,\n b: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n if (aReserve === 0n || bReserve === 0n) {\n throw new Error(\"Not enough pool liquidity\");\n }\n\n if (a === 0n || b === 0n) {\n throw new Error(\"Cannot use a deposit asset amount of 0\");\n }\n\n const bInUnitsOfA = (b * aReserve) / bReserve;\n\n let aChange: bigint;\n let bChange: bigint;\n\n if (bInUnitsOfA > a) {\n bChange = (bReserve * (bInUnitsOfA - a)) / aReserve;\n aChange = 0n;\n } else {\n aChange = a - bInUnitsOfA;\n bChange = 0n;\n }\n\n // Subtract off the surplus as returned to the user\n const actualDepositedA = a - aChange;\n const actualDepositedB = b - bChange;\n\n // Calculate LP Tokens\n const newLpTokens = (actualDepositedA * totalLp) / aReserve;\n const newTotalLpTokens = totalLp + newLpTokens;\n\n return {\n nextTotalLp: newTotalLpTokens,\n generatedLp: newLpTokens,\n shareAfterDeposit: getShare(newLpTokens, newTotalLpTokens),\n bChange,\n aChange,\n actualDepositedA,\n actualDepositedB,\n };\n};\n\n/**\n * @deprecated\n *\n * Use {@link calculateLiquidity} instead.\n */\nexport const addLiquidity = (\n a: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n) => {\n const nextTotalLp = new Fraction(totalLp * (a + aReserve), aReserve).quotient;\n const lp = nextTotalLp - totalLp;\n const b = new Fraction(bReserve * a, aReserve);\n const share = getShare(lp, nextTotalLp);\n\n if (b.quotient === 0n) {\n throw new Error(\n \"The provided a asset is not enough to equal at least 1 of the b asset.\",\n );\n }\n\n return {\n nextTotalLp,\n /** @deprecated */\n lp,\n generatedLp: lp,\n /** @deprecated */\n b: b.quotient,\n requiredB: b.quotient,\n /** @deprecated */\n share,\n shareAfterDeposit: share,\n };\n};\n\n/**\n * Get the token amounts the given lp represents\n * @param lp the lp amount\n * @param aReserve the pool's reserveA amount\n * @param bReserve the pool's reserveB amount\n * @param totalLp the pool's total minted lp currently\n * @returns [a, b] token amounts\n */\nexport const getTokensForLp = (\n lp: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n): TPair => [\n new Fraction(lp * aReserve, totalLp).quotient,\n new Fraction(lp * bReserve, totalLp).quotient,\n];\n\n/**\n * Holds the calculated outcome of a swap\n */\nexport type TSwapOutcome = {\n input: bigint;\n output: bigint;\n inputLpFee: bigint;\n nextInputReserve: bigint;\n nextOutputReserve: bigint;\n priceImpact: Fraction;\n};\n\n/**\n * Calculate swap outcome for a given input and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n *\n * @param input The given amount of tokens to be swapped\n * @param inputReserve The amount of tokens in the input reserve\n * @param outputReserve The amount of tokens in the output reserve\n * @param fee The liquidity provider fee\n * @returns The swap details\n */\nexport const getSwapOutput = (\n input: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n roundOutputUp?: boolean,\n): TSwapOutcome => {\n if (input <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Input and reserves must be positive\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gt(Fraction.ONE))\n throw new Error(\"fee must be between 0 and 1\");\n\n const feeDiff = fee.denominator - fee.numerator;\n const outputNumerator = outputReserve * input * feeDiff;\n const outputDenominator = inputReserve * fee.denominator + input * feeDiff;\n const output = new Fraction(outputNumerator, outputDenominator);\n\n const safeOutput = roundOutputUp\n ? (outputNumerator + outputDenominator - 1n) / outputDenominator\n : output.quotient;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - safeOutput;\n\n // PRICEIMPACT: \"priceImpact\" is slightly misleadingly named in the industry as a whole\n // just by it's name, it would imply that it's the percentage difference between\n // the current price and the price after the swap, but it's actually the percentage\n // difference between the real price of your swap, and the price implied by the current\n // reserves; We got this wrong in v1, but this aligns it with the industry standard\n // Source: https://dailydefi.org/articles/price-impact-and-how-to-calculate/\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, safeOutput);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n return {\n input,\n output: safeOutput,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculate input required for a swap outcome for a given output and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n * - output is greater than or equal to output reserve\n *\n * @param output\n * @param inputReserve\n * @param outputReserve\n * @param fee\n * @returns The swap details\n */\nexport const getSwapInput = (\n output: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n): TSwapOutcome => {\n if (output <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Output and reserves must be positive\");\n\n if (output >= outputReserve)\n throw new Error(\"Output must be less than output reserve\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be [0,1)\");\n\n const feeDiff = BigInt(fee.denominator - fee.numerator);\n const input =\n new Fraction(\n inputReserve * output * fee.denominator,\n (outputReserve - output) * feeDiff,\n ).quotient + 1n;\n\n const inputLpFee = new Fraction(input * fee.numerator, fee.denominator)\n .quotient;\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - output;\n\n // See PRICEIMPACT\n const amountInLessFee = input - inputLpFee;\n const idealPrice = new Fraction(inputReserve, outputReserve);\n const actualPrice = new Fraction(amountInLessFee, output);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n\n return {\n input,\n output,\n inputLpFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n };\n};\n\n/**\n * Calculates the ratio between two values.\n *\n * @param {TFractionLike} firstValue - The first value for the ratio calculation.\n * @param {TFractionLike} secondValue - The second value for the ratio calculation.\n * @returns {string|null} The ratio of the first value to the second value, expressed as a string in base 10. If either of the inputs is zero or negative, or if either input is missing, the function returns null.\n * @throws {Error} Will throw an error if the inputs are not valid for the Fraction class's `asFraction` method.\n */\nexport const getAssetsRatio = (\n firstValue: TFractionLike,\n secondValue: TFractionLike,\n) => {\n const first = Fraction.asFraction(firstValue ?? 0);\n const second = Fraction.asFraction(secondValue ?? 0);\n\n if (!first.greaterThan(0) || !second.greaterThan(0)) {\n return \"0\";\n }\n\n return first.divide(second).toString(10);\n};\n\n/**\n * Calculates the swap ratio between two assets and returns an AssetAmount instance representing this ratio.\n *\n * The assets are first sorted lexicographically by their assetId to ensure any calculation will match\n * pool ratios (which are also sorted lexicographically when created). The direction of the swap determines the calculation.\n * For 'A_PER_B', it calculates how much of the lexicographically first asset (A) is obtained for each unit of the\n * lexicographically second asset (B). For 'B_PER_A', it calculates how much of the lexicographically second asset (B)\n * is obtained for each unit of the lexicographically first asset (A).\n *\n * The returned AssetAmount is represented in the decimal format of the asset that is being received in the swap operation.\n * In 'A_PER_B' direction, the lexicographically first asset (A) is received, so the AssetAmount will be in the decimal format\n * of Asset A. In 'B_PER_A' direction, the lexicographically second asset (B) is received, so the AssetAmount will be in the decimal format of Asset B.\n *\n * @param {TRatioDirection} direction - The direction of the swap: 'A_PER_B' means the first asset lexicographically is received and the second is given, 'B_PER_A' means the second asset lexicographically is received and the first is given.\n * @param {[IRatioCalculationAsset, IRatioCalculationAsset]} assets - An array of two assets involved in the swap. The order of the assets in this array does not matter as they will be sorted lexicographically by their assetId inside the function.\n *\n * @returns {TSwapRatio} The calculated swap ratio in different representations.\n *\n * @example\n *\n * ```ts\n * const asset1 = { quantity: 100, decimals: 0, assetId: 'B' };\n * const asset2 = { quantity: 2000000, decimals: 6, assetId: 'A' };\n * const amount = getSwapRatio('A_PER_B', [asset1, asset2]);\n * const sameAmount = getSwapRatio('A_PER_B', [asset2, asset1]);\n * console.log(amount.ratioAsFraction); // Returns a Fraction class of the ratio.\n * console.log(sameAmount.ratioAsFraction); // Same as above.\n * ```\n */\nexport function getSwapRatio(\n direction: TRatioDirection,\n assets: [IRatioCalculationAsset, IRatioCalculationAsset],\n): IRatioCalculationResult {\n let calculatedAmount: AssetAmount;\n let rawRatio: string;\n const adaIds = [\"\", \".\", \"ada.lovelace\"];\n\n const [firstAsset, secondAsset] = assets.sort((a, b) => {\n const isASpecial = adaIds.includes(a.assetId);\n const isBSpecial = adaIds.includes(b.assetId);\n\n if (isASpecial && !isBSpecial) {\n return -1;\n }\n\n if (!isASpecial && isBSpecial) {\n return 1;\n }\n\n return a.assetId.localeCompare(b.assetId);\n });\n\n if (direction === \"A_PER_B\") {\n rawRatio = getAssetsRatio(firstAsset.quantity, secondAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (secondAsset.decimals - firstAsset.decimals),\n firstAsset.decimals,\n );\n } else {\n rawRatio = getAssetsRatio(secondAsset.quantity, firstAsset.quantity);\n calculatedAmount = AssetAmount.fromValue(\n Number(rawRatio) * 10 ** (firstAsset.decimals - secondAsset.decimals),\n secondAsset.decimals,\n );\n }\n\n return {\n calculatedAmount: calculatedAmount,\n display: calculatedAmount.value.toString(),\n ratioAsFraction: Fraction.asFraction(rawRatio),\n isDivisible: calculatedAmount.decimals > 0,\n belowMinimumRatio: rawRatio.toString() === \"0\",\n };\n}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,mBAAmB;AAC/C,SAASC,QAAQ,QAA4B,sBAAsB;AACnE,SAASC,IAAI,QAAQ,yBAAyB;;AAE9C;AACA;AACA;;AAGA;AACA;AACA;;AAOA;AACA;AACA;;AASA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,UAAU,GAAGA,CAACC,CAAS,EAAEC,CAAS,KAAKH,IAAI,CAACE,CAAC,GAAGC,CAAC,CAAC;;AAE/D;AACA;AACA;AACA,OAAO,MAAMC,KAAK,GAAGH,UAAU;;AAE/B;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMI,QAAQ,GAAGA,CAACC,EAAU,EAAEC,OAAe,KAClD,IAAIR,QAAQ,CAACO,EAAE,EAAEC,OAAO,CAAC;;AAE3B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,kBAAkB,GAAGA,CAChCN,CAAS,EACTC,CAAS,EACTM,QAAgB,EAChBC,QAAgB,EAChBH,OAAe,KACZ;EACH,IAAIE,QAAQ,KAAK,EAAE,IAAIC,QAAQ,KAAK,EAAE,EAAE;IACtC,MAAM,IAAIC,KAAK,CAAC,2BAA2B,CAAC;EAC9C;EAEA,IAAIT,CAAC,KAAK,EAAE,IAAIC,CAAC,KAAK,EAAE,EAAE;IACxB,MAAM,IAAIQ,KAAK,CAAC,wCAAwC,CAAC;EAC3D;EAEA,MAAMC,WAAW,GAAIT,CAAC,GAAGM,QAA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B,OAAO,CAAC;IACV;IAEA,OAAO1E,CAAC,CAACyE,OAAO,CAACE,aAAa,CAAC1E,CAAC,CAACwE,OAAO,CAAC;EAC3C,CAAC,CAAC;EAEF,IAAIV,SAAS,KAAK,SAAS,EAAE;IAC3BG,QAAQ,GAAGX,cAAc,CAACa,UAAU,CAACQ,QAAQ,EAAEP,WAAW,CAACO,QAAQ,CAAC;IACpEX,gBAAgB,GAAGrE,WAAW,CAACiF,SAAS,CACtCC,MAAM,CAACZ,QAAQ,CAAC,GAAG,EAAE,KAAKG,WAAW,CAACU,QAAQ,GAAGX,UAAU,CAACW,QAAQ,CAAC,EACrEX,UAAU,CAACW,QACb,CAAC;EACH,CAAC,MAAM;IACLb,QAAQ,GAAGX,cAAc,CAACc,WAAW,CAACO,QAAQ,EAAER,UAAU,CAACQ,QAAQ,CAAC;IACpEX,gBAAgB,GAAGrE,WAAW,CAACiF,SAAS,CACtCC,MAAM,CAACZ,QAAQ,CAAC,GAAG,EAAE,KAAKE,UAAU,CAACW,QAAQ,GAAGV,WAAW,CAACU,QAAQ,CAAC,EACrEV,WAAW,CAACU,QACd,CAAC;EACH;EAEA,OAAO;IACLd,gBAAgB,EAAEA,gBAAgB;IAClCe,OAAO,EAAEf,gBAAgB,CAACgB,KAAK,CAACpB,QAAQ,CAAC,CAAC;IAC1CqB,eAAe,EAAErF,QAAQ,CAACkC,UAAU,CAACmC,QAAQ,CAAC;IAC9CiB,WAAW,EAAElB,gBAAgB,CAACc,QAAQ,GAAG,CAAC;IAC1CK,iBAAiB,EAAElB,QAAQ,CAACL,QAAQ,CAAC,CAAC,KAAK;EAC7C,CAAC;AACH","ignoreList":[]}
@@ -1,4 +1,2 @@
1
1
  export * as ConstantProductPool from "./ConstantProductPool.js";
2
- export * as StableSwapsPool from "./StableSwapsPool.js";
3
- export * as SharedPoolMath from "./SharedPoolMath.js";
4
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["ConstantProductPool","StableSwapsPool","SharedPoolMath"],"sources":["../../../src/PoolMath/index.ts"],"sourcesContent":["export * as ConstantProductPool from \"./ConstantProductPool.js\";\nexport * as StableSwapsPool from \"./StableSwapsPool.js\";\nexport * as SharedPoolMath from \"./SharedPoolMath.js\";\n"],"mappings":"AAAA,OAAO,KAAKA,mBAAmB,MAAM,0BAA0B;AAC/D,OAAO,KAAKC,eAAe,MAAM,sBAAsB;AACvD,OAAO,KAAKC,cAAc,MAAM,qBAAqB","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["ConstantProductPool"],"sources":["../../../src/PoolMath/index.ts"],"sourcesContent":["export * as ConstantProductPool from \"./ConstantProductPool.js\";\n"],"mappings":"AAAA,OAAO,KAAKA,mBAAmB,MAAM,0BAA0B","ignoreList":[]}
@@ -1,5 +1,31 @@
1
+ import { AssetAmount } from "@sundaeswap/asset";
1
2
  import { Fraction, type TFractionLike } from "@sundaeswap/fraction";
2
- import { SharedPoolMath } from "./index.js";
3
+ /**
4
+ * The direction of a ratio (A per B or B per A)
5
+ */
6
+ export type TRatioDirection = "A_PER_B" | "B_PER_A";
7
+ /**
8
+ * Asset used in a ratio calculation
9
+ */
10
+ export interface IRatioCalculationAsset {
11
+ assetId: string;
12
+ quantity: bigint;
13
+ decimals: number;
14
+ }
15
+ /**
16
+ * The result of a ratio calculation on a pool
17
+ */
18
+ export interface IRatioCalculationResult {
19
+ calculatedAmount: AssetAmount;
20
+ ratioAsFraction: Fraction;
21
+ display: string;
22
+ isDivisible: boolean;
23
+ belowMinimumRatio: boolean;
24
+ }
25
+ /**
26
+ * The amounts for a token pair
27
+ */
28
+ export type TPair = [bigint, bigint];
3
29
  /**
4
30
  * Get the lp token amount for a, b
5
31
  * @param a tokenA amount
@@ -11,6 +37,13 @@ export declare const getFirstLp: (a: bigint, b: bigint) => bigint;
11
37
  * @deprecated
12
38
  */
13
39
  export declare const getLp: (a: bigint, b: bigint) => bigint;
40
+ /**
41
+ * Get the share ratio as Fraction
42
+ * @param lp
43
+ * @param totalLp
44
+ * @returns
45
+ */
46
+ export declare const getShare: (lp: bigint, totalLp: bigint) => Fraction;
14
47
  /**
15
48
  * Calculate the Add (Mixed-Deposit) Liquidity parameters
16
49
  *
@@ -60,7 +93,7 @@ export declare const addLiquidity: (a: bigint, aReserve: bigint, bReserve: bigin
60
93
  * @param totalLp the pool's total minted lp currently
61
94
  * @returns [a, b] token amounts
62
95
  */
63
- export declare const getTokensForLp: (lp: bigint, aReserve: bigint, bReserve: bigint, totalLp: bigint) => SharedPoolMath.TPair;
96
+ export declare const getTokensForLp: (lp: bigint, aReserve: bigint, bReserve: bigint, totalLp: bigint) => TPair;
64
97
  /**
65
98
  * Holds the calculated outcome of a swap
66
99
  */
@@ -137,8 +170,5 @@ export declare const getAssetsRatio: (firstValue: TFractionLike, secondValue: TF
137
170
  * console.log(sameAmount.ratioAsFraction); // Same as above.
138
171
  * ```
139
172
  */
140
- export declare function getSwapRatio(direction: SharedPoolMath.TRatioDirection, assets: [
141
- SharedPoolMath.IRatioCalculationAsset,
142
- SharedPoolMath.IRatioCalculationAsset
143
- ]): SharedPoolMath.IRatioCalculationResult;
173
+ export declare function getSwapRatio(direction: TRatioDirection, assets: [IRatioCalculationAsset, IRatioCalculationAsset]): IRatioCalculationResult;
144
174
  //# sourceMappingURL=ConstantProductPool.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ConstantProductPool.d.ts","sourceRoot":"","sources":["../../../src/PoolMath/ConstantProductPool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C;;;;;GAKG;AACH,eAAO,MAAM,UAAU,MAAO,MAAM,KAAK,MAAM,WAAgB,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,KAAK,MALY,MAAM,KAAK,MAAM,WAKhB,CAAC;AAEhC;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,kBAAkB,MAC1B,MAAM,KACN,MAAM,YACC,MAAM,YACN,MAAM,WACP,MAAM;;;;;;;;CAwChB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,MACpB,MAAM,YACC,MAAM,YACN,MAAM,WACP,MAAM;;IAeb,kBAAkB;;;IAGlB,kBAAkB;;;IAGlB,kBAAkB;;;CAIrB,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,OACrB,MAAM,YACA,MAAM,YACN,MAAM,WACP,MAAM,KACd,cAAc,CAAC,KAGjB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,WAAW,EAAE,QAAQ,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,UACjB,MAAM,gBACC,MAAM,iBACL,MAAM,OAChB,aAAa,kBACF,OAAO,KACtB,YAwCF,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,YAAY,WACf,MAAM,gBACA,MAAM,iBACL,MAAM,OAChB,aAAa,KACjB,YAqCF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,eACb,aAAa,eACZ,aAAa,WAU3B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,cAAc,CAAC,eAAe,EACzC,MAAM,EAAE;IACN,cAAc,CAAC,sBAAsB;IACrC,cAAc,CAAC,sBAAsB;CACtC,GACA,cAAc,CAAC,uBAAuB,CAyCxC"}
1
+ {"version":3,"file":"ConstantProductPool.d.ts","sourceRoot":"","sources":["../../../src/PoolMath/ConstantProductPool.ts"],"names":[],"mappings":"A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G;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,WAAW,EAAE,QAAQ,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,UACjB,MAAM,gBACC,MAAM,iBACL,MAAM,OAChB,aAAa,kBACF,OAAO,KACtB,YAwCF,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,YAAY,WACf,MAAM,gBACA,MAAM,iBACL,MAAM,OAChB,aAAa,KACjB,YAqCF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,eACb,aAAa,eACZ,aAAa,WAU3B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,eAAe,EAC1B,MAAM,EAAE,CAAC,sBAAsB,EAAE,sBAAsB,CAAC,GACvD,uBAAuB,CAyCzB"}
@@ -1,4 +1,2 @@
1
1
  export * as ConstantProductPool from "./ConstantProductPool.js";
2
- export * as StableSwapsPool from "./StableSwapsPool.js";
3
- export * as SharedPoolMath from "./SharedPoolMath.js";
4
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/PoolMath/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,mBAAmB,MAAM,0BAA0B,CAAC;AAChE,OAAO,KAAK,eAAe,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/PoolMath/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,mBAAmB,MAAM,0BAA0B,CAAC"}