@gmx-io/sdk 2.9.0 → 2.9.2

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 (110) hide show
  1. package/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +2 -2
  2. package/build/cjs/src/clients/v2/__tests__/testUtil.js +8 -8
  3. package/build/cjs/src/clients/v2/index.js +61 -41
  4. package/build/cjs/src/clients/v2/solana.spec.js +188 -0
  5. package/build/cjs/src/configs/api.js +4 -0
  6. package/build/cjs/src/utils/fees/index.js +7 -1
  7. package/build/cjs/src/utils/fees/priceImpact.js +118 -18
  8. package/build/cjs/src/utils/gmsol/constants.js +9 -0
  9. package/build/cjs/src/utils/gmsol/index.js +53 -0
  10. package/build/cjs/src/utils/gmsol/numbers.js +176 -0
  11. package/build/cjs/src/utils/gmsol/types.js +2 -0
  12. package/build/cjs/src/utils/gmxAccountApi/api.js +4 -4
  13. package/build/cjs/src/utils/http/__tests__/chainPins.spec.js +3 -3
  14. package/build/cjs/src/utils/http/http.js +3 -4
  15. package/build/cjs/src/utils/http/httpFallback.js +11 -1
  16. package/build/cjs/src/utils/http/solana.js +17 -0
  17. package/build/cjs/src/utils/http/validation.js +21 -1
  18. package/build/cjs/src/utils/http/validation.spec.js +45 -0
  19. package/build/cjs/src/utils/markets/__tests__/api.spec.js +53 -0
  20. package/build/cjs/src/utils/markets/api.js +5 -0
  21. package/build/cjs/src/utils/markets/utils.js +25 -9
  22. package/build/cjs/src/utils/orderTransactions/api.js +6 -6
  23. package/build/cjs/src/utils/orders/api.js +3 -0
  24. package/build/cjs/src/utils/positions/__tests__/liquidationPrice.spec.js +64 -0
  25. package/build/cjs/src/utils/positions/api.js +3 -0
  26. package/build/cjs/src/utils/positions/utils.js +88 -19
  27. package/build/cjs/src/utils/prices/utils.js +69 -14
  28. package/build/cjs/src/utils/referrals/api.js +41 -0
  29. package/build/cjs/src/utils/subaccount/api.js +2 -2
  30. package/build/cjs/src/utils/swap/swapRouting.js +4 -0
  31. package/build/cjs/src/utils/swap/swapStats.js +6 -1
  32. package/build/cjs/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1012 -0
  33. package/build/cjs/src/utils/trade/decrease.js +266 -56
  34. package/build/cjs/src/utils/trade/decreaseMarginCheck.js +23 -4
  35. package/build/cjs/src/utils/trade/increase.js +86 -28
  36. package/build/cjs/src/utils/trade/increaseMarginCheck.js +51 -9
  37. package/build/cjs/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
  38. package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +2 -2
  39. package/build/esm/src/clients/v2/__tests__/testUtil.js +8 -8
  40. package/build/esm/src/clients/v2/index.js +39 -19
  41. package/build/esm/src/clients/v2/solana.spec.js +160 -0
  42. package/build/esm/src/configs/api.js +5 -1
  43. package/build/esm/src/utils/fees/index.js +7 -1
  44. package/build/esm/src/utils/fees/priceImpact.js +111 -12
  45. package/build/esm/src/utils/gmsol/constants.js +6 -0
  46. package/build/esm/src/utils/gmsol/index.js +43 -0
  47. package/build/esm/src/utils/gmsol/numbers.js +172 -0
  48. package/build/esm/src/utils/gmsol/types.js +1 -0
  49. package/build/esm/src/utils/gmxAccountApi/api.js +4 -4
  50. package/build/esm/src/utils/http/__tests__/chainPins.spec.js +3 -3
  51. package/build/esm/src/utils/http/http.js +3 -4
  52. package/build/esm/src/utils/http/httpFallback.js +11 -1
  53. package/build/esm/src/utils/http/solana.js +12 -0
  54. package/build/esm/src/utils/http/validation.js +18 -0
  55. package/build/esm/src/utils/http/validation.spec.js +43 -0
  56. package/build/esm/src/utils/markets/__tests__/api.spec.js +53 -0
  57. package/build/esm/src/utils/markets/api.js +6 -1
  58. package/build/esm/src/utils/markets/utils.js +23 -8
  59. package/build/esm/src/utils/orderTransactions/api.js +6 -6
  60. package/build/esm/src/utils/orders/api.js +3 -0
  61. package/build/esm/src/utils/positions/__tests__/liquidationPrice.spec.js +62 -0
  62. package/build/esm/src/utils/positions/api.js +3 -0
  63. package/build/esm/src/utils/positions/utils.js +89 -20
  64. package/build/esm/src/utils/prices/utils.js +70 -15
  65. package/build/esm/src/utils/referrals/api.js +36 -0
  66. package/build/esm/src/utils/subaccount/api.js +2 -2
  67. package/build/esm/src/utils/swap/swapRouting.js +4 -0
  68. package/build/esm/src/utils/swap/swapStats.js +7 -2
  69. package/build/esm/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1010 -0
  70. package/build/esm/src/utils/trade/decrease.js +266 -56
  71. package/build/esm/src/utils/trade/decreaseMarginCheck.js +23 -4
  72. package/build/esm/src/utils/trade/increase.js +86 -28
  73. package/build/esm/src/utils/trade/increaseMarginCheck.js +53 -11
  74. package/build/esm/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
  75. package/build/tsconfig.tsbuildinfo +1 -1
  76. package/build/types/src/clients/v2/index.d.ts +21 -12
  77. package/build/types/src/clients/v2/solana.spec.d.ts +1 -0
  78. package/build/types/src/utils/fees/index.d.ts +1 -1
  79. package/build/types/src/utils/fees/priceImpact.d.ts +7 -0
  80. package/build/types/src/utils/gmsol/constants.d.ts +5 -0
  81. package/build/types/src/utils/gmsol/index.d.ts +35 -0
  82. package/build/types/src/utils/gmsol/numbers.d.ts +1 -0
  83. package/build/types/src/utils/gmsol/types.d.ts +9 -0
  84. package/build/types/src/utils/http/http.d.ts +2 -5
  85. package/build/types/src/utils/http/httpFallback.d.ts +2 -5
  86. package/build/types/src/utils/http/solana.d.ts +4 -0
  87. package/build/types/src/utils/http/types.d.ts +17 -4
  88. package/build/types/src/utils/http/validation.d.ts +8 -0
  89. package/build/types/src/utils/http/validation.spec.d.ts +1 -0
  90. package/build/types/src/utils/markets/hashKeys.d.ts +1 -1
  91. package/build/types/src/utils/markets/types.d.ts +3 -2
  92. package/build/types/src/utils/markets/utils.d.ts +6 -2
  93. package/build/types/src/utils/orders/api.d.ts +2 -2
  94. package/build/types/src/utils/positions/__tests__/liquidationPrice.spec.d.ts +1 -0
  95. package/build/types/src/utils/positions/api.d.ts +2 -2
  96. package/build/types/src/utils/positions/types.d.ts +1 -0
  97. package/build/types/src/utils/positions/utils.d.ts +6 -1
  98. package/build/types/src/utils/prices/types.d.ts +16 -0
  99. package/build/types/src/utils/prices/utils.d.ts +3 -11
  100. package/build/types/src/utils/referrals/api.d.ts +8 -0
  101. package/build/types/src/utils/referrals/types.d.ts +5 -0
  102. package/build/types/src/utils/rpc/createViemRpc.d.ts +25 -3
  103. package/build/types/src/utils/trade/__tests__/gmsolTradeMath.spec.d.ts +1 -0
  104. package/build/types/src/utils/trade/decrease.d.ts +5 -0
  105. package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +2 -0
  106. package/build/types/src/utils/trade/increase.d.ts +2 -0
  107. package/build/types/src/utils/trade/increaseMarginCheck.d.ts +2 -0
  108. package/build/types/src/utils/trade/twapIncreaseMarginCheck.d.ts +1 -0
  109. package/build/types/src/utils/trade/types.d.ts +6 -0
  110. package/package.json +10 -1
@@ -1,7 +1,8 @@
1
1
  import { BASIS_POINTS_DIVISOR_BIGINT } from "../../configs/factors";
2
2
  import { bigMath } from "../bigmath";
3
3
  import { capPositionImpactUsdByMaxImpactPool, capPositionImpactUsdByMaxPriceImpactFactor, getMaxPositionImpactFactors, getPositionFee, getPriceImpactForPosition, getProportionalPendingImpactValues, } from "../fees";
4
- import { getCappedPoolPnl, getMarketIndexName, getMarketPoolName, getMaxAllowedLeverage, getOpenInterestUsd, getPoolUsdWithoutPnl, getPositiveMarketPnl, } from "../markets";
4
+ import { getGmsolUsdAmount } from "../gmsol";
5
+ import { getCappedPoolPnl, getMarketIndexName, getMarketPoolName, getMaxAllowedLeverage, getOpenInterestUsd, getPoolUsdWithoutPnl, getPositiveMarketPnl, getMarketPnl, } from "../markets";
5
6
  import { applyFactor, expandDecimals, FLOAT_PRECISION_SQRT, getBasisPoints, PRECISION } from "../numbers";
6
7
  import { getAcceptablePriceInfo, getMarkPrice } from "../prices";
7
8
  import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens } from "../tokens";
@@ -26,7 +27,9 @@ export function getPositionPnlUsd(p) {
26
27
  if (totalPnl <= 0) {
27
28
  return totalPnl;
28
29
  }
29
- const poolPnl = getPositiveMarketPnl(marketInfo, isLong, false);
30
+ const poolPnl = marketInfo.tradeMath === "gmsol"
31
+ ? getMarketPnl(marketInfo, isLong, false)
32
+ : getPositiveMarketPnl(marketInfo, isLong, false);
30
33
  const poolUsd = getPoolUsdWithoutPnl(marketInfo, isLong, "minPrice");
31
34
  const cappedPnl = getCappedPoolPnl({
32
35
  marketInfo,
@@ -34,8 +37,11 @@ export function getPositionPnlUsd(p) {
34
37
  poolPnl,
35
38
  isLong,
36
39
  });
37
- if (cappedPnl !== poolPnl && cappedPnl > 0 && poolPnl > 0) {
40
+ if (cappedPnl !== poolPnl && (cappedPnl > 0 || marketInfo.tradeMath === "gmsol") && poolPnl > 0) {
38
41
  totalPnl = bigMath.mulDiv(totalPnl, cappedPnl, poolPnl);
42
+ if (marketInfo.tradeMath === "gmsol") {
43
+ totalPnl = getGmsolUsdAmount(totalPnl);
44
+ }
39
45
  }
40
46
  return totalPnl;
41
47
  }
@@ -74,7 +80,10 @@ export function getLeverage(p) {
74
80
  }
75
81
  export function getLiquidationPriceImpactDeltaUsd(p) {
76
82
  const { marketInfo, sizeInUsd, sizeInTokens, pendingImpactAmount, isLong, useMaxPriceImpact } = p;
77
- const maxNegativePriceImpactUsd = -1n * applyFactor(sizeInUsd, marketInfo.maxPositionImpactFactorForLiquidations);
83
+ let maxNegativePriceImpactUsd = -1n * applyFactor(sizeInUsd, marketInfo.maxPositionImpactFactorForLiquidations);
84
+ if (marketInfo.tradeMath === "gmsol") {
85
+ maxNegativePriceImpactUsd = getGmsolUsdAmount(maxNegativePriceImpactUsd);
86
+ }
78
87
  if (useMaxPriceImpact) {
79
88
  return maxNegativePriceImpactUsd;
80
89
  }
@@ -85,7 +94,7 @@ export function getLiquidationPriceImpactDeltaUsd(p) {
85
94
  if (priceImpactDeltaUsd > 0) {
86
95
  priceImpactDeltaUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeInUsd, priceImpactDeltaUsd);
87
96
  }
88
- const pendingImpactUsd = convertToUsd(pendingImpactAmount, marketInfo.indexToken.decimals, pendingImpactAmount > 0 ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice);
97
+ const pendingImpactUsd = convertToUsd(marketInfo.tradeMath === "gmsol" ? 0n : pendingImpactAmount, marketInfo.indexToken.decimals, pendingImpactAmount > 0 ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice);
89
98
  priceImpactDeltaUsd = priceImpactDeltaUsd + pendingImpactUsd;
90
99
  if (priceImpactDeltaUsd > 0) {
91
100
  return 0n;
@@ -101,7 +110,11 @@ export function getLiquidationPrice(p) {
101
110
  return undefined;
102
111
  }
103
112
  const { indexToken } = marketInfo;
104
- const closingFeeUsd = getPositionFee(marketInfo, sizeInUsd, false, userReferralInfo).positionFeeUsd;
113
+ const closingFeeUsd = getPositionFee(marketInfo, sizeInUsd, marketInfo.tradeMath === "gmsol" &&
114
+ getPriceImpactForPosition(marketInfo, -sizeInUsd, isLong, {
115
+ fallbackToZero: true,
116
+ sizeDeltaInTokens: sizeInTokens,
117
+ }).balanceWasImproved, userReferralInfo, undefined, undefined, p.feeDiscountFactor).positionFeeUsd;
105
118
  const totalPendingFeesUsd = getPositionPendingFeesUsd({ pendingFundingFeesUsd, pendingBorrowingFeesUsd });
106
119
  const totalFeesUsd = totalPendingFeesUsd + closingFeeUsd;
107
120
  const priceImpactDeltaUsd = getLiquidationPriceImpactDeltaUsd({
@@ -112,12 +125,34 @@ export function getLiquidationPrice(p) {
112
125
  isLong,
113
126
  useMaxPriceImpact,
114
127
  });
128
+ const effectiveMinCollateralUsd = marketInfo.tradeMath === "gmsol" ? marketInfo.minCollateralUsd ?? minCollateralUsd : minCollateralUsd;
115
129
  let liquidationCollateralUsd = applyFactor(sizeInUsd, marketInfo.minCollateralFactorForLiquidation);
116
- if (liquidationCollateralUsd < minCollateralUsd) {
117
- liquidationCollateralUsd = minCollateralUsd;
130
+ if (marketInfo.tradeMath === "gmsol") {
131
+ liquidationCollateralUsd = getGmsolUsdAmount(liquidationCollateralUsd);
132
+ }
133
+ if (liquidationCollateralUsd < effectiveMinCollateralUsd) {
134
+ liquidationCollateralUsd = effectiveMinCollateralUsd;
118
135
  }
119
136
  let liquidationPrice;
120
- if (getIsEquivalentTokens(collateralToken, indexToken)) {
137
+ if (marketInfo.tradeMath === "gmsol" && getIsEquivalentTokens(collateralToken, indexToken)) {
138
+ const fundingAmount = p.fundingFeeAmount ??
139
+ convertToTokenAmount(pendingFundingFeesUsd, collateralToken.decimals, collateralToken.prices.minPrice);
140
+ if (fundingAmount === undefined || collateralUsd + priceImpactDeltaUsd - totalFeesUsd < 0n) {
141
+ return undefined;
142
+ }
143
+ // Collateral and funding both move with the index price being solved for.
144
+ const netCollateralAmount = collateralAmount - fundingAmount;
145
+ const denominator = isLong ? sizeInTokens + netCollateralAmount : sizeInTokens - netCollateralAmount;
146
+ if (denominator <= 0n) {
147
+ return undefined;
148
+ }
149
+ const surchargeUsd = pendingBorrowingFeesUsd + closingFeeUsd - priceImpactDeltaUsd;
150
+ const numerator = isLong
151
+ ? sizeInUsd + liquidationCollateralUsd + surchargeUsd
152
+ : sizeInUsd - liquidationCollateralUsd - surchargeUsd;
153
+ liquidationPrice = bigMath.mulDiv(numerator, expandDecimals(1, indexToken.decimals), denominator);
154
+ }
155
+ else if (getIsEquivalentTokens(collateralToken, indexToken)) {
121
156
  if (isLong) {
122
157
  const denominator = sizeInTokens + collateralAmount;
123
158
  if (denominator == 0n) {
@@ -163,7 +198,11 @@ export function getMinCollateralUsdForLiquidationPrice(p) {
163
198
  if (sizeInUsd <= 0 || sizeInTokens <= 0) {
164
199
  return 0n;
165
200
  }
166
- const closingFeeUsd = getPositionFee(marketInfo, sizeInUsd, false, userReferralInfo).positionFeeUsd;
201
+ const closingFeeUsd = getPositionFee(marketInfo, sizeInUsd, marketInfo.tradeMath === "gmsol" &&
202
+ getPriceImpactForPosition(marketInfo, -sizeInUsd, isLong, {
203
+ fallbackToZero: true,
204
+ sizeDeltaInTokens: sizeInTokens,
205
+ }).balanceWasImproved, userReferralInfo, undefined, undefined, p.feeDiscountFactor).positionFeeUsd;
167
206
  const priceImpactDeltaUsd = getLiquidationPriceImpactDeltaUsd({
168
207
  marketInfo,
169
208
  sizeInUsd,
@@ -171,9 +210,13 @@ export function getMinCollateralUsdForLiquidationPrice(p) {
171
210
  pendingImpactAmount,
172
211
  isLong,
173
212
  });
213
+ const effectiveMinCollateralUsd = marketInfo.tradeMath === "gmsol" ? marketInfo.minCollateralUsd ?? minCollateralUsd : minCollateralUsd;
174
214
  let liquidationCollateralUsd = applyFactor(sizeInUsd, marketInfo.minCollateralFactorForLiquidation);
175
- if (liquidationCollateralUsd < minCollateralUsd) {
176
- liquidationCollateralUsd = minCollateralUsd;
215
+ if (marketInfo.tradeMath === "gmsol") {
216
+ liquidationCollateralUsd = getGmsolUsdAmount(liquidationCollateralUsd);
217
+ }
218
+ if (liquidationCollateralUsd < effectiveMinCollateralUsd) {
219
+ liquidationCollateralUsd = effectiveMinCollateralUsd;
177
220
  }
178
221
  return liquidationCollateralUsd - pnl - priceImpactDeltaUsd + closingFeeUsd;
179
222
  }
@@ -186,7 +229,11 @@ export function getIsPositionBelowMinCollateralForLeverage(position, collateralD
186
229
  return true;
187
230
  }
188
231
  const collateralUsdAfterDelta = convertToUsd(collateralAmount - collateralDeltaAmount, collateralToken.decimals, collateralToken.prices.minPrice);
189
- if (collateralUsdAfterDelta < applyFactor(sizeInUsd, getMinCollateralFactorForPosition(position, 0n))) {
232
+ let minCollateralUsdForOpenInterest = applyFactor(sizeInUsd, getMinCollateralFactorForPosition(position, 0n));
233
+ if (marketInfo.tradeMath === "gmsol") {
234
+ minCollateralUsdForOpenInterest = getGmsolUsdAmount(minCollateralUsdForOpenInterest);
235
+ }
236
+ if (collateralUsdAfterDelta < minCollateralUsdForOpenInterest) {
190
237
  return true;
191
238
  }
192
239
  const collateralDeltaUsd = convertToUsd(collateralDeltaAmount, collateralToken.decimals, collateralToken.prices.minPrice);
@@ -198,13 +245,17 @@ export function getIsPositionBelowMinCollateralForLeverage(position, collateralD
198
245
  isLong,
199
246
  });
200
247
  const marginUsd = remainingCollateralUsd - collateralDeltaUsd + pnl + priceImpactDeltaUsd - closingFeeUsd;
201
- return marginUsd <= 0n || marginUsd < applyFactor(sizeInUsd, marketInfo.minCollateralFactor);
248
+ let minCollateralUsdForLeverage = applyFactor(sizeInUsd, marketInfo.minCollateralFactor);
249
+ if (marketInfo.tradeMath === "gmsol") {
250
+ minCollateralUsdForLeverage = getGmsolUsdAmount(minCollateralUsdForLeverage);
251
+ }
252
+ return marginUsd <= 0n || marginUsd < minCollateralUsdForLeverage;
202
253
  }
203
254
  export function getNetPriceImpactDeltaUsdForDecrease({ marketInfo, sizeInUsd, pendingImpactAmount, priceImpactDeltaUsd, sizeDeltaUsd, }) {
204
255
  const { proportionalPendingImpactDeltaUsd } = getProportionalPendingImpactValues({
205
256
  sizeInUsd,
206
257
  sizeDeltaUsd,
207
- pendingImpactAmount,
258
+ pendingImpactAmount: marketInfo.tradeMath === "gmsol" ? 0n : pendingImpactAmount,
208
259
  indexToken: marketInfo.indexToken,
209
260
  });
210
261
  let totalImpactDeltaUsd = priceImpactDeltaUsd + proportionalPendingImpactDeltaUsd;
@@ -217,6 +268,9 @@ export function getNetPriceImpactDeltaUsdForDecrease({ marketInfo, sizeInUsd, pe
217
268
  totalImpactDeltaUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd);
218
269
  }
219
270
  totalImpactDeltaUsd = capPositionImpactUsdByMaxImpactPool(marketInfo, totalImpactDeltaUsd);
271
+ if (marketInfo.tradeMath === "gmsol") {
272
+ totalImpactDeltaUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd);
273
+ }
220
274
  return {
221
275
  totalImpactDeltaUsd,
222
276
  proportionalPendingImpactDeltaUsd,
@@ -227,6 +281,9 @@ export function getPriceImpactDiffUsd({ totalImpactDeltaUsd, marketInfo, sizeDel
227
281
  if (totalImpactDeltaUsd > 0) {
228
282
  return 0n;
229
283
  }
284
+ if (marketInfo.tradeMath === "gmsol") {
285
+ return (capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd) - totalImpactDeltaUsd);
286
+ }
230
287
  const { maxNegativeImpactFactor } = getMaxPositionImpactFactors(marketInfo);
231
288
  const maxNegativeImpactUsd = -applyFactor(sizeDeltaUsd, maxNegativeImpactFactor);
232
289
  let priceImpactDiffUsd = 0n;
@@ -243,6 +300,9 @@ export function getMinCollateralFactorForPosition(position, openInterestDelta) {
243
300
  ? marketInfo.minCollateralFactorForOpenInterestLong
244
301
  : marketInfo.minCollateralFactorForOpenInterestShort;
245
302
  let minCollateralFactor = bigMath.mulDiv(openInterest, minCollateralFactorMultiplier, PRECISION);
303
+ if (marketInfo.tradeMath === "gmsol") {
304
+ minCollateralFactor = getGmsolUsdAmount(minCollateralFactor);
305
+ }
246
306
  const minCollateralFactorForMarket = marketInfo.minCollateralFactor;
247
307
  if (minCollateralFactorForMarket > minCollateralFactor) {
248
308
  minCollateralFactor = minCollateralFactorForMarket;
@@ -254,7 +314,7 @@ function getFundingAmount(latestFundingAmountPerSize, positionFundingAmountPerSi
254
314
  // fundingAmountPerSize values are stored with FLOAT_PRECISION_SQRT precision
255
315
  return bigMath.mulDiv(positionSizeInUsd, fundingDiffFactor, PRECISION * FLOAT_PRECISION_SQRT);
256
316
  }
257
- export function getContractPositionDynamicFees({ position, marketInfo, marketFeeState, referralInfo, }) {
317
+ export function getContractPositionDynamicFees({ position, marketInfo, marketFeeState, referralInfo, feeDiscountFactor, }) {
258
318
  const { sizeInUsd, isLong, borrowingFactor } = position;
259
319
  const isCollateralLongToken = position.collateralTokenAddress === marketInfo.longToken.address;
260
320
  const collateralToken = isCollateralLongToken ? marketInfo.longToken : marketInfo.shortToken;
@@ -292,7 +352,7 @@ export function getContractPositionDynamicFees({ position, marketInfo, marketFee
292
352
  fallbackToZero: true,
293
353
  sizeDeltaInTokens: position.sizeInTokens,
294
354
  });
295
- const { positionFeeUsd, discountUsd, uiFeeUsd } = getPositionFee(marketInfo, sizeInUsd, balanceWasImproved, referralInfo);
355
+ const { positionFeeUsd, discountUsd, uiFeeUsd } = getPositionFee(marketInfo, sizeInUsd, balanceWasImproved, referralInfo, undefined, undefined, feeDiscountFactor);
296
356
  const positionFeeAmount = convertToTokenAmount(positionFeeUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
297
357
  const traderDiscountAmount = convertToTokenAmount(discountUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
298
358
  const uiFeeAmount = convertToTokenAmount(uiFeeUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
@@ -346,8 +406,10 @@ export function getPositionInfo(p) {
346
406
  indexPrice: markPrice,
347
407
  sizeDeltaUsd: position.sizeInUsd,
348
408
  sizeDeltaInTokens: position.sizeInTokens,
409
+ positionSizeInUsd: position.sizeInUsd,
410
+ positionSizeInTokens: position.sizeInTokens,
349
411
  });
350
- const positionFeeInfo = getPositionFee(marketInfo, position.sizeInUsd, closeAcceptablePriceInfo.balanceWasImproved, userReferralInfo, uiFeeFactor);
412
+ const positionFeeInfo = getPositionFee(marketInfo, position.sizeInUsd, closeAcceptablePriceInfo.balanceWasImproved, userReferralInfo, uiFeeFactor, undefined, p.feeDiscountFactor);
351
413
  const closingFeeUsd = positionFeeInfo.positionFeeUsd;
352
414
  const uiFeeUsd = positionFeeInfo.uiFeeUsd ?? 0n;
353
415
  const netPriceImpactValues = getNetPriceImpactDeltaUsdForDecrease({
@@ -357,6 +419,9 @@ export function getPositionInfo(p) {
357
419
  sizeDeltaUsd: position.sizeInUsd,
358
420
  priceImpactDeltaUsd: closeAcceptablePriceInfo.priceImpactDeltaUsd,
359
421
  });
422
+ // gmsol withholds the impact beyond the cap from the close output
423
+ const closeCostImpactDeltaUsd = netPriceImpactValues.totalImpactDeltaUsd -
424
+ (marketInfo.tradeMath === "gmsol" ? netPriceImpactValues.priceImpactDiffUsd : 0n);
360
425
  const netValue = getPositionNetValue({
361
426
  collateralUsd,
362
427
  pnl,
@@ -374,14 +439,14 @@ export function getPositionInfo(p) {
374
439
  pnl,
375
440
  pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
376
441
  pendingFundingFeesUsd,
377
- netPriceImpactDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
442
+ netPriceImpactDeltaUsd: closeCostImpactDeltaUsd,
378
443
  closingFeeUsd,
379
444
  });
380
445
  const pnlAfterAllFees = getPositionPnlAfterAllFees({
381
446
  pnl,
382
447
  pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
383
448
  pendingFundingFeesUsd,
384
- netPriceImpactDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
449
+ netPriceImpactDeltaUsd: closeCostImpactDeltaUsd,
385
450
  closingFeeUsd,
386
451
  });
387
452
  const pnlAfterAllFeesPercentage = collateralUsd !== 0n ? getBasisPoints(pnlAfterAllFees, collateralUsd) : 0n;
@@ -422,9 +487,11 @@ export function getPositionInfo(p) {
422
487
  collateralAmount: position.collateralAmount,
423
488
  pendingImpactAmount: position.pendingImpactAmount,
424
489
  userReferralInfo,
490
+ feeDiscountFactor: p.feeDiscountFactor,
425
491
  minCollateralUsd,
426
492
  pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
427
493
  pendingFundingFeesUsd,
494
+ fundingFeeAmount: position.fundingFeeAmount,
428
495
  isLong: position.isLong,
429
496
  });
430
497
  const indexName = getMarketIndexName({ indexToken, isSpotOnly: false });
@@ -441,6 +508,7 @@ export function getPositionInfo(p) {
441
508
  };
442
509
  return {
443
510
  ...position,
511
+ pendingImpactAmount: marketInfo.tradeMath === "gmsol" ? 0n : position.pendingImpactAmount,
444
512
  market,
445
513
  marketInfo,
446
514
  indexName,
@@ -476,5 +544,6 @@ export function getPositionInfo(p) {
476
544
  uiFeeUsd,
477
545
  pendingFundingFeesUsd,
478
546
  pendingClaimableFundingFeesUsd,
547
+ ...(marketInfo.tradeMath === "gmsol" ? { fundingSettlement: "nextOrder" } : {}),
479
548
  };
480
549
  }
@@ -1,6 +1,7 @@
1
1
  import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER } from "../../configs/factors";
2
2
  import { bigMath } from "../bigmath";
3
- import { getCappedPositionImpactUsd, getPriceImpactByAcceptablePrice } from "../fees";
3
+ import { capPositionImpactUsdByMaxPriceImpactFactor, getCappedPositionImpactUsd, getPriceImpactByAcceptablePrice, } from "../fees";
4
+ import { getGmsolIncreaseSizeDeltaInTokens, getGmsolUsdAmount, GMSOL_PRECISION_SCALE } from "../gmsol";
4
5
  import { expandDecimals, getBasisPoints, roundUpMagnitudeDivision } from "../numbers";
5
6
  import { OrderType } from "../orders/types";
6
7
  import { convertToTokenAmount } from "../tokens";
@@ -58,7 +59,16 @@ export function getIncreaseEvaluationIndexPrice(p) {
58
59
  return p.triggerPrice;
59
60
  }
60
61
  export function getAcceptablePriceInfo(p) {
61
- const { marketInfo, isIncrease, isLong, indexPrice, sizeDeltaUsd, maxNegativePriceImpactBps } = p;
62
+ if (p.marketInfo.tradeMath === "gmsol") {
63
+ p = { ...p, sizeDeltaUsd: getGmsolUsdAmount(p.sizeDeltaUsd) };
64
+ }
65
+ const { isIncrease, isLong, indexPrice, sizeDeltaUsd, maxNegativePriceImpactBps } = p;
66
+ const marketInfo = p.marketInfo.tradeMath === "gmsol" && p.isLimit
67
+ ? {
68
+ ...p.marketInfo,
69
+ indexToken: { ...p.marketInfo.indexToken, prices: { minPrice: indexPrice, maxPrice: indexPrice } },
70
+ }
71
+ : p.marketInfo;
62
72
  const { indexToken } = marketInfo;
63
73
  const values = {
64
74
  acceptablePrice: 0n,
@@ -94,11 +104,6 @@ export function getAcceptablePriceInfo(p) {
94
104
  shouldCapNegativeImpact: false,
95
105
  sizeDeltaInTokens: p.sizeDeltaInTokens,
96
106
  });
97
- /**
98
- * We display this value as price impact on action (increase or decrease)
99
- * But for acceptable price calculation uncapped price impact is used
100
- * Also on decrease action we calculate totalImpactUsd which will be deducted from the collateral
101
- */
102
107
  values.priceImpactDeltaUsd = priceImpactDeltaUsd;
103
108
  values.balanceWasImproved = balanceWasImproved;
104
109
  if (values.priceImpactDeltaUsd > 0) {
@@ -107,23 +112,68 @@ export function getAcceptablePriceInfo(p) {
107
112
  else {
108
113
  values.priceImpactDeltaAmount = roundUpMagnitudeDivision(values.priceImpactDeltaUsd * expandDecimals(1, indexToken.decimals), indexToken.prices.minPrice);
109
114
  }
110
- // Use uncapped price impact for the acceptable price calculation
111
- const { priceImpactDeltaUsd: priceImpactDeltaUsdForAcceptablePrice } = getCappedPositionImpactUsd(marketInfo, sizeDeltaUsd, isLong, isIncrease, {
112
- fallbackToZero: !isIncrease,
113
- shouldCapNegativeImpact: false,
114
- sizeDeltaInTokens: p.sizeDeltaInTokens,
115
- });
115
+ if (marketInfo.tradeMath === "gmsol") {
116
+ const priceParams = { ...p, marketInfo, priceImpactDeltaUsd };
117
+ const acceptablePrice = isIncrease
118
+ ? getGmsolIncreaseAcceptablePrice(priceParams)
119
+ : getGmsolDecreaseAcceptablePrice(priceParams);
120
+ if (acceptablePrice === undefined) {
121
+ return values;
122
+ }
123
+ values.acceptablePrice = acceptablePrice;
124
+ values.acceptablePriceDeltaBps = getBasisPoints((values.acceptablePrice - indexPrice) * (shouldFlipPriceImpact ? -1n : 1n), indexPrice);
125
+ return values;
126
+ }
116
127
  const acceptablePriceValues = getAcceptablePriceByPriceImpact({
117
128
  isIncrease,
118
129
  isLong,
119
130
  indexPrice,
120
131
  sizeDeltaUsd,
121
- priceImpactDeltaUsd: priceImpactDeltaUsdForAcceptablePrice,
132
+ priceImpactDeltaUsd,
122
133
  });
123
134
  values.acceptablePrice = acceptablePriceValues.acceptablePrice;
124
135
  values.acceptablePriceDeltaBps = acceptablePriceValues.acceptablePriceDeltaBps;
125
136
  return values;
126
137
  }
138
+ function getGmsolIncreaseAcceptablePrice(p) {
139
+ const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, isLong, marketInfo } = p;
140
+ const { indexToken } = marketInfo;
141
+ const tokenUnit = expandDecimals(1, indexToken.decimals);
142
+ let baseSizeDeltaInTokens = p.sizeDeltaInTokens;
143
+ if (baseSizeDeltaInTokens == null) {
144
+ baseSizeDeltaInTokens = isLong
145
+ ? bigMath.mulDiv(sizeDeltaUsd, tokenUnit, indexPrice)
146
+ : roundUpMagnitudeDivision(sizeDeltaUsd * tokenUnit, indexPrice);
147
+ }
148
+ const sizeDeltaInTokens = getGmsolIncreaseSizeDeltaInTokens({
149
+ baseSizeDeltaInTokens,
150
+ priceImpactDeltaUsd,
151
+ indexToken,
152
+ isLong,
153
+ });
154
+ return sizeDeltaInTokens === 0n
155
+ ? undefined
156
+ : getGmsolUsdAmount(bigMath.mulDiv(sizeDeltaUsd, 1n, sizeDeltaInTokens)) * tokenUnit;
157
+ }
158
+ function getGmsolDecreaseAcceptablePrice(p) {
159
+ const { marketInfo, indexPrice, sizeDeltaUsd, sizeDeltaInTokens, positionSizeInUsd, positionSizeInTokens, isLong } = p;
160
+ const cappedImpactUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, p.priceImpactDeltaUsd);
161
+ const tokenUnit = expandDecimals(1, marketInfo.indexToken.decimals);
162
+ let priceAdjustment;
163
+ if (positionSizeInUsd && positionSizeInTokens) {
164
+ const impactAtEntrySizeUsd = getGmsolUsdAmount(bigMath.mulDiv(positionSizeInUsd, cappedImpactUsd, sizeDeltaUsd));
165
+ priceAdjustment = bigMath.mulDiv(impactAtEntrySizeUsd, tokenUnit, positionSizeInTokens);
166
+ }
167
+ else if (sizeDeltaInTokens && sizeDeltaInTokens > 0n) {
168
+ priceAdjustment = bigMath.mulDiv(cappedImpactUsd, tokenUnit, sizeDeltaInTokens);
169
+ }
170
+ else {
171
+ priceAdjustment = bigMath.mulDiv(indexPrice, cappedImpactUsd, sizeDeltaUsd);
172
+ }
173
+ const priceUnit = tokenUnit * GMSOL_PRECISION_SCALE;
174
+ priceAdjustment = bigMath.mulDiv(priceAdjustment, 1n, priceUnit) * priceUnit;
175
+ return indexPrice + (isLong ? priceAdjustment : -priceAdjustment);
176
+ }
127
177
  export function getAcceptablePriceByPriceImpact(p) {
128
178
  const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd } = p;
129
179
  if (sizeDeltaUsd <= 0 || indexPrice == 0n) {
@@ -145,7 +195,12 @@ export function getAcceptablePriceByPriceImpact(p) {
145
195
  };
146
196
  }
147
197
  export function getDefaultAcceptablePriceImpactBps(p) {
148
- const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImpactBuffer = DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, } = p;
198
+ const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImpactBuffer = DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, executionPrice, } = p;
199
+ if (executionPrice !== undefined && executionPrice > 0n && indexPrice > 0n) {
200
+ const adversePriceDelta = (executionPrice - indexPrice) * (getShouldUseMaxPrice(p.isIncrease, p.isLong) ? 1n : -1n);
201
+ return ((adversePriceDelta > 0n ? getBasisPoints(adversePriceDelta, indexPrice, true) : 0n) +
202
+ BigInt(acceptablePriceImpactBuffer));
203
+ }
149
204
  if (priceImpactDeltaUsd > 0) {
150
205
  return BigInt(acceptablePriceImpactBuffer);
151
206
  }
@@ -0,0 +1,36 @@
1
+ import { readApiString, readApiUnsignedBigInt } from "../http/validation";
2
+ import { isRecord } from "../objects";
3
+ const FEE_DISCOUNT_ENDPOINT = "/v1/fee-discount";
4
+ export function parseFeeDiscount(raw) {
5
+ if (!isRecord(raw)) {
6
+ throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected an object`);
7
+ }
8
+ const record = raw;
9
+ const endpoint = FEE_DISCOUNT_ENDPOINT;
10
+ if (typeof record.attachedOnChain !== "boolean") {
11
+ throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected boolean field "attachedOnChain"`);
12
+ }
13
+ if (typeof record.tierId !== "number" || !Number.isSafeInteger(record.tierId) || record.tierId < 0) {
14
+ throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected number field "tierId"`);
15
+ }
16
+ return {
17
+ userReferralInfo: {
18
+ userReferralCode: readApiString({ record, name: "userReferralCode", endpoint }),
19
+ userReferralCodeString: readApiString({ record, name: "userReferralCodeString", endpoint }),
20
+ referralCodeForTxn: readApiString({ record, name: "referralCodeForTxn", endpoint }),
21
+ attachedOnChain: record.attachedOnChain,
22
+ affiliate: readApiString({ record, name: "affiliate", endpoint }),
23
+ tierId: record.tierId,
24
+ totalRebate: readApiUnsignedBigInt({ record, name: "totalRebate", endpoint }),
25
+ totalRebateFactor: readApiUnsignedBigInt({ record, name: "totalRebateFactor", endpoint }),
26
+ discountShare: readApiUnsignedBigInt({ record, name: "discountShare", endpoint }),
27
+ discountFactor: readApiUnsignedBigInt({ record, name: "discountFactor", endpoint }),
28
+ },
29
+ orderFeeDiscountFactor: readApiUnsignedBigInt({ record, name: "orderFeeDiscountFactor", endpoint }),
30
+ updatedAt: typeof record.updatedAt === "number" ? record.updatedAt : undefined,
31
+ };
32
+ }
33
+ export async function fetchApiFeeDiscount(ctx, params) {
34
+ const raw = await ctx.api.fetchJson(FEE_DISCOUNT_ENDPOINT, { query: { address: params.address } });
35
+ return parseFeeDiscount(raw);
36
+ }
@@ -1,9 +1,9 @@
1
1
  import { validateSubaccountApprovalTypedData } from "../orderTransactions/validateTypedData";
2
2
  export async function fetchSubaccountStatus(ctx, request) {
3
- return ctx.api.postJson("/v1/subaccounts/status", request);
3
+ return ctx.api.postJson("/v2/subaccounts/status", request);
4
4
  }
5
5
  export async function prepareSubaccountApproval(ctx, request) {
6
- return ctx.api.postJson("/v1/subaccounts/approval/prepare", request);
6
+ return ctx.api.postJson("/v2/subaccounts/approval/prepare", request);
7
7
  }
8
8
  export async function signSubaccountApproval(prepared, signer, options) {
9
9
  const domain = prepared.domain;
@@ -2,6 +2,7 @@ import { maxUint256 } from "viem";
2
2
  import { bigMath } from "../bigmath";
3
3
  import { estimateOrderOraclePriceCount, getExecutionFee } from "../fees";
4
4
  import { getNaiveEstimatedGasBySwapCount } from "../fees/getNaiveEstimatedGasBySwapCount";
5
+ import { GmsolPriceImpactError } from "../fees/priceImpact";
5
6
  import { getAvailableUsdLiquidityForCollateral, getTokenPoolType } from "../markets";
6
7
  import { PRECISION, PRECISION_DECIMALS, bigintToNumber } from "../numbers";
7
8
  import { getByKey } from "../objects";
@@ -114,6 +115,9 @@ export function getBestSwapPath({ routes, usdIn, estimator, networkEstimator, })
114
115
  }
115
116
  }
116
117
  catch (e) {
118
+ if (e instanceof GmsolPriceImpactError) {
119
+ throw e;
120
+ }
117
121
  continue;
118
122
  }
119
123
  }
@@ -1,7 +1,8 @@
1
1
  import { maxUint256 } from "viem";
2
2
  import { NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
3
3
  import { bigMath } from "../bigmath";
4
- import { applySwapImpactWithCap, getNextPoolAmountsParams, getPriceImpactForSwap, getSwapFee } from "../fees";
4
+ import { applySwapImpactWithCap, getNextPoolAmountsParams, getPriceImpactForSwap, getSwapFee, GmsolPriceImpactError, } from "../fees";
5
+ import { getGmsolMidPrice } from "../gmsol";
5
6
  import { getAvailableUsdLiquidityForCollateral, getMarketPnl, getOppositeCollateral, getTokenPoolType, } from "../markets";
6
7
  import { PRECISION } from "../numbers";
7
8
  import { getByKey } from "../objects";
@@ -148,11 +149,15 @@ export function getSwapStats(p) {
148
149
  let priceImpactDeltaUsd;
149
150
  let balanceWasImproved;
150
151
  try {
151
- const priceImpactValues = getPriceImpactForSwap(marketInfo, tokenIn, tokenOut, usdIn, usdIn * -1n);
152
+ const impactUsd = marketInfo.tradeMath === "gmsol" ? convertToUsd(amountIn, tokenIn.decimals, getGmsolMidPrice(tokenIn)) : usdIn;
153
+ const priceImpactValues = getPriceImpactForSwap(marketInfo, tokenIn, tokenOut, impactUsd, -impactUsd);
152
154
  priceImpactDeltaUsd = priceImpactValues.priceImpactDeltaUsd;
153
155
  balanceWasImproved = priceImpactValues.balanceWasImproved;
154
156
  }
155
157
  catch (e) {
158
+ if (e instanceof GmsolPriceImpactError) {
159
+ throw e;
160
+ }
156
161
  // Approximate if the market would be out of capacity
157
162
  const capacityUsd = getSwapCapacityUsd(marketInfo, isFromLongToShort);
158
163
  const swapFeeUsd = getSwapFee(marketInfo, usdIn, false, swapPricingType);