@gmx-io/sdk 1.5.0-alpha-14 → 1.5.0-alpha-15

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 (36) hide show
  1. package/build/cjs/scripts/subsquid-codegen.js +1 -1
  2. package/build/cjs/src/clients/v1/modules/trades/trades.js +4 -0
  3. package/build/cjs/src/codegen/subsquid.js +6 -0
  4. package/build/cjs/src/test/mock.js +1 -0
  5. package/build/cjs/src/utils/__tests__/markets.spec.js +88 -48
  6. package/build/cjs/src/utils/__tests__/positions.spec.js +93 -0
  7. package/build/cjs/src/utils/markets/utils.js +20 -15
  8. package/build/cjs/src/utils/positions/utils.js +34 -2
  9. package/build/cjs/src/utils/prices/utils.js +4 -4
  10. package/build/cjs/src/utils/trade/decrease.js +1 -1
  11. package/build/cjs/src/utils/trade/increase.js +1 -1
  12. package/build/cjs/src/utils/tradeHistory/utils.js +1 -0
  13. package/build/esm/scripts/subsquid-codegen.js +1 -1
  14. package/build/esm/src/clients/v1/modules/trades/trades.js +4 -0
  15. package/build/esm/src/codegen/subsquid.js +6 -0
  16. package/build/esm/src/test/mock.js +1 -0
  17. package/build/esm/src/utils/__tests__/markets.spec.js +90 -50
  18. package/build/esm/src/utils/__tests__/positions.spec.js +94 -1
  19. package/build/esm/src/utils/markets/utils.js +17 -12
  20. package/build/esm/src/utils/positions/utils.js +32 -2
  21. package/build/esm/src/utils/prices/utils.js +4 -4
  22. package/build/esm/src/utils/trade/decrease.js +1 -1
  23. package/build/esm/src/utils/trade/increase.js +1 -1
  24. package/build/esm/src/utils/tradeHistory/utils.js +1 -0
  25. package/build/types/src/codegen/subsquid.d.ts +16 -0
  26. package/build/types/src/utils/markets/utils.d.ts +5 -1
  27. package/build/types/src/utils/positions/types.d.ts +3 -0
  28. package/build/types/src/utils/positions/utils.d.ts +15 -0
  29. package/build/types/src/utils/prices/utils.d.ts +1 -1
  30. package/build/types/src/utils/swap/types.d.ts +1 -1
  31. package/build/types/src/utils/trade/types.d.ts +4 -1
  32. package/build/types/src/utils/tradeHistory/types.d.ts +1 -0
  33. package/package.json +1 -1
  34. package/build/cjs/src/configs/marketHours.js +0 -41
  35. package/build/esm/src/configs/marketHours.js +0 -38
  36. package/build/types/src/configs/marketHours.d.ts +0 -7
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const config = {
4
- schema: "https://gmx.squids.live/gmx-synthetics-arbitrum@24678a/api/graphql",
4
+ schema: "https://gmx.squids.live/gmx-synthetics-arbitrum@5eaa69/api/graphql",
5
5
  overwrite: true,
6
6
  debug: true,
7
7
  generates: {
@@ -173,9 +173,13 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
173
173
 
174
174
  priceImpactUsd
175
175
  priceImpactDiffUsd
176
+ swapImpactUsd
177
+ totalImpactUsd
176
178
  positionFeeAmount
177
179
  borrowingFeeAmount
178
180
  fundingFeeAmount
181
+ swapFeeUsd
182
+ liquidationFeeAmount
179
183
  pnlUsd
180
184
  basePnlUsd
181
185
 
@@ -1106,6 +1106,12 @@ var MarketInfoOrderByInput;
1106
1106
  MarketInfoOrderByInput["maxShortPoolUsdForDeposit_DESC"] = "maxShortPoolUsdForDeposit_DESC";
1107
1107
  MarketInfoOrderByInput["maxShortPoolUsdForDeposit_DESC_NULLS_FIRST"] = "maxShortPoolUsdForDeposit_DESC_NULLS_FIRST";
1108
1108
  MarketInfoOrderByInput["maxShortPoolUsdForDeposit_DESC_NULLS_LAST"] = "maxShortPoolUsdForDeposit_DESC_NULLS_LAST";
1109
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_ASC"] = "minCollateralFactorForLiquidation_ASC";
1110
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_ASC_NULLS_FIRST"] = "minCollateralFactorForLiquidation_ASC_NULLS_FIRST";
1111
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_ASC_NULLS_LAST"] = "minCollateralFactorForLiquidation_ASC_NULLS_LAST";
1112
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_DESC"] = "minCollateralFactorForLiquidation_DESC";
1113
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_DESC_NULLS_FIRST"] = "minCollateralFactorForLiquidation_DESC_NULLS_FIRST";
1114
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_DESC_NULLS_LAST"] = "minCollateralFactorForLiquidation_DESC_NULLS_LAST";
1109
1115
  MarketInfoOrderByInput["minCollateralFactorForOpenInterestLong_ASC"] = "minCollateralFactorForOpenInterestLong_ASC";
1110
1116
  MarketInfoOrderByInput["minCollateralFactorForOpenInterestLong_ASC_NULLS_FIRST"] = "minCollateralFactorForOpenInterestLong_ASC_NULLS_FIRST";
1111
1117
  MarketInfoOrderByInput["minCollateralFactorForOpenInterestLong_ASC_NULLS_LAST"] = "minCollateralFactorForOpenInterestLong_ASC_NULLS_LAST";
@@ -153,6 +153,7 @@ data = "0x1", to = "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64", receiver = "0x1
153
153
  priceIn,
154
154
  priceOut,
155
155
  feesUsd,
156
+ slippage: 0,
156
157
  txnData: {
157
158
  to,
158
159
  data,
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const chains_1 = require("../../configs/chains");
5
- const factors_1 = require("../../configs/factors");
6
5
  const tokens_1 = require("../../configs/tokens");
7
6
  const markets_1 = require("../markets");
8
7
  const numbers_1 = require("../numbers");
@@ -142,53 +141,94 @@ function getToken(symbol) {
142
141
  (0, vitest_1.expect)((0, markets_1.getMaxLeverageByMinCollateralFactor)(1000000000000000000n)).toBe(10000000000000000);
143
142
  });
144
143
  });
145
- (0, vitest_1.describe)("getMaxAllowedLeverageByMinCollateralFactor", () => {
146
- const GOLD_MARKET = "0x0Df2BE76F517BCF0000AbfFcB6344B3b2aC4Cc4f";
147
- const SILVER_MARKET = "0x448Fa722717df299ee197E2F6d8EB7911EFF6cEc";
148
- const WTIOIL_MARKET = "0xda81cdd397210C08cFc567f93982E148A3aac8a6";
149
- const BRENTOIL_MARKET = "0x6F287D071800BfA847B4a7a7104BE33F87Ce9E74";
150
- const NATGAS_MARKET = "0x2Ce2bc8B0f9d000f359d756a5816C125474Bb39b";
151
- const NON_MARKET_HOURS_MARKET = "0x1234567890abcdef1234567890abcdef12345678";
152
- (0, vitest_1.it)("returns half of max leverage when no market address is provided", () => {
153
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(1000000000000000000n, undefined)).toBe(5000000000000000);
154
- });
155
- (0, vitest_1.it)("returns half of max leverage for a regular market", () => {
156
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(1000000000000000000n, NON_MARKET_HOURS_MARKET)).toBe(5000000000000000);
157
- });
158
- (0, vitest_1.it)("returns 100x for GOLD on on-hours MCF (0.009 → 110x contract max)", () => {
159
- const onHoursFactor = 9n * 10n ** 27n;
160
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(onHoursFactor, GOLD_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
161
- });
162
- (0, vitest_1.it)("returns 25x for GOLD on off-hours MCF (0.035 → 30x contract max)", () => {
163
- const offHoursFactor = 35n * 10n ** 27n;
164
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(offHoursFactor, GOLD_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
165
- });
166
- (0, vitest_1.it)("returns 100x / 25x for SILVER with the same GOLD MCFs", () => {
167
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(9n * 10n ** 27n, SILVER_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
168
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(35n * 10n ** 27n, SILVER_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
169
- });
170
- (0, vitest_1.it)("returns 100x for WTIOIL on-hours MCF and 25x for off-hours MCF", () => {
171
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(9n * 10n ** 27n, WTIOIL_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
172
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(35n * 10n ** 27n, WTIOIL_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
173
- });
174
- (0, vitest_1.it)("returns 100x for BRENTOIL on-hours MCF and 25x for off-hours MCF", () => {
175
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(9n * 10n ** 27n, BRENTOIL_MARKET)).toBe(100 * factors_1.BASIS_POINTS_DIVISOR);
176
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(35n * 10n ** 27n, BRENTOIL_MARKET)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
177
- });
178
- (0, vitest_1.it)("returns 40x for NATGAS on-hours MCF and 20x for off-hours MCF", () => {
179
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(22n * 10n ** 27n, NATGAS_MARKET)).toBe(40 * factors_1.BASIS_POINTS_DIVISOR);
180
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(40n * 10n ** 27n, NATGAS_MARKET)).toBe(20 * factors_1.BASIS_POINTS_DIVISOR);
181
- });
182
- (0, vitest_1.it)("does not apply market-hours overrides to other markets with same MCF", () => {
183
- const onHoursFactor = 9n * 10n ** 27n;
184
- const offHoursFactor = 35n * 10n ** 27n;
185
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(onHoursFactor, NON_MARKET_HOURS_MARKET)).toBe(55.5 * factors_1.BASIS_POINTS_DIVISOR);
186
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(offHoursFactor, NON_MARKET_HOURS_MARKET)).toBe(14.5 * factors_1.BASIS_POINTS_DIVISOR);
187
- });
188
- (0, vitest_1.it)("rounds allowed leverage to nearest .0 or .5", () => {
189
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)((0, numbers_1.numberToBigint)(1 / 24, numbers_1.PRECISION_DECIMALS), undefined)).toBe(12 * factors_1.BASIS_POINTS_DIVISOR);
190
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)((0, numbers_1.numberToBigint)(1 / 25, numbers_1.PRECISION_DECIMALS), undefined)).toBe(12.5 * factors_1.BASIS_POINTS_DIVISOR);
191
- (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)((0, numbers_1.numberToBigint)(1 / 50, numbers_1.PRECISION_DECIMALS), undefined)).toBe(25 * factors_1.BASIS_POINTS_DIVISOR);
144
+ (0, vitest_1.describe)("getMaxAllowedLeverage", () => {
145
+ // Factors are expressed in PRECISION units (1e30 == 100%).
146
+ const pct = (percent) => (0, numbers_1.expandDecimals)(percent * 100, 26); // 0.5% → 5e27
147
+ const bps = (lev) => lev * numbers_1.BASIS_POINTS_DIVISOR;
148
+ (0, vitest_1.it)("returns default 100x when any factor is undefined or zero", () => {
149
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
150
+ minCollateralFactor: undefined,
151
+ minCollateralFactorForLiquidation: undefined,
152
+ positionFeeFactorForBalanceWasNotImproved: undefined,
153
+ })).toBe(bps(100));
154
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
155
+ minCollateralFactor: 0n,
156
+ minCollateralFactorForLiquidation: pct(0.5),
157
+ positionFeeFactorForBalanceWasNotImproved: pct(0.05),
158
+ })).toBe(bps(100));
159
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
160
+ minCollateralFactor: pct(0.5),
161
+ minCollateralFactorForLiquidation: 0n,
162
+ positionFeeFactorForBalanceWasNotImproved: pct(0.05),
163
+ })).toBe(bps(100));
164
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
165
+ minCollateralFactor: pct(0.5),
166
+ minCollateralFactorForLiquidation: pct(0.5),
167
+ positionFeeFactorForBalanceWasNotImproved: undefined,
168
+ })).toBe(bps(100));
169
+ });
170
+ (0, vitest_1.it)("returns 100x for BTC/ETH (equal MCF and liqMCF, liquidation bound dominates)", () => {
171
+ // MCF = liqMCF = 0.5%, fee = 0.05%
172
+ // opening = 1 / (0.005 + 0.001) ≈ 166.67x
173
+ // liquidation = 1 / (2 × 0.005) = 100x
174
+ // min floored to 5x = 100x
175
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
176
+ minCollateralFactor: pct(0.5),
177
+ minCollateralFactorForLiquidation: pct(0.5),
178
+ positionFeeFactorForBalanceWasNotImproved: pct(0.05),
179
+ })).toBe(bps(100));
180
+ });
181
+ (0, vitest_1.it)("returns 85x for ZEC (opening bound dominates, floors to 5x)", () => {
182
+ // MCF = 1%, liqMCF = 0.5%, fee = 0.06%
183
+ // opening = 1 / (0.01 + 0.0012) ≈ 89.29x
184
+ // liquidation = 1 / (2 × 0.005) = 100x
185
+ // min floored to 5x = 85x
186
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
187
+ minCollateralFactor: pct(1),
188
+ minCollateralFactorForLiquidation: pct(0.5),
189
+ positionFeeFactorForBalanceWasNotImproved: pct(0.06),
190
+ })).toBe(bps(85));
191
+ });
192
+ (0, vitest_1.it)("returns 100x for GOLD on-hours (split factors)", () => {
193
+ // MCF = 0.9%, liqMCF = 0.5%, fee = 0.05%
194
+ // opening = 1 / (0.009 + 0.001) = 100x
195
+ // liquidation = 1 / (2 × 0.005) = 100x
196
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
197
+ minCollateralFactor: 9n * 10n ** 27n,
198
+ minCollateralFactorForLiquidation: pct(0.5),
199
+ positionFeeFactorForBalanceWasNotImproved: pct(0.05),
200
+ })).toBe(bps(100));
201
+ });
202
+ (0, vitest_1.it)("returns 25x for GOLD off-hours (split factors, opening bound dominates)", () => {
203
+ // MCF = 3.5%, liqMCF = 1%, fee = 0.05%
204
+ // opening = 1 / (0.035 + 0.001) ≈ 27.78x
205
+ // liquidation = 1 / (2 × 0.01) = 50x
206
+ // min floored to 5x = 25x
207
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
208
+ minCollateralFactor: 35n * 10n ** 27n,
209
+ minCollateralFactorForLiquidation: pct(1),
210
+ positionFeeFactorForBalanceWasNotImproved: pct(0.05),
211
+ })).toBe(bps(25));
212
+ });
213
+ (0, vitest_1.it)("floors to the nearest 5x", () => {
214
+ // Choose factors so opening = 47.17x exactly, should floor to 45x
215
+ // 1 / (MCF + 2×fee) = 47.17 → MCF + 2×fee ≈ 0.0212
216
+ // MCF = 2%, fee = 0.06% → 0.02 + 0.0012 = 0.0212 → 1/0.0212 ≈ 47.17
217
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
218
+ minCollateralFactor: pct(2),
219
+ minCollateralFactorForLiquidation: pct(0.5),
220
+ positionFeeFactorForBalanceWasNotImproved: pct(0.06),
221
+ })).toBe(bps(45));
222
+ });
223
+ (0, vitest_1.it)("liquidation bound dominates when MCF-for-liquidation is relatively high", () => {
224
+ // MCF = 0.5%, liqMCF = 2%, fee = 0.05%
225
+ // opening = 1 / (0.005 + 0.001) ≈ 166.67x
226
+ // liquidation = 1 / (2 × 0.02) = 25x → min 25x
227
+ (0, vitest_1.expect)((0, markets_1.getMaxAllowedLeverage)({
228
+ minCollateralFactor: pct(0.5),
229
+ minCollateralFactorForLiquidation: pct(2),
230
+ positionFeeFactorForBalanceWasNotImproved: pct(0.05),
231
+ })).toBe(bps(25));
192
232
  });
193
233
  });
194
234
  (0, vitest_1.describe)("getOppositeCollateral", () => {
@@ -150,6 +150,99 @@ vitest_1.vi.mock("../fees", () => ({
150
150
  (0, vitest_1.expect)(result).toBe(-70n);
151
151
  });
152
152
  });
153
+ (0, vitest_1.describe)("getPositionNetValueAfterAllFees", () => {
154
+ (0, vitest_1.it)("subtracts all fees and adds signed price impact (adverse)", () => {
155
+ const result = (0, positions_1.getPositionNetValueAfterAllFees)({
156
+ collateralUsd: 1000n,
157
+ pnl: 200n,
158
+ pendingBorrowingFeesUsd: 15n,
159
+ pendingFundingFeesUsd: 10n,
160
+ netPriceImpactDeltaUsd: -50n,
161
+ closingFeeUsd: 30n,
162
+ });
163
+ // 1000 + 200 - 15 - 10 + (-50) - 30 = 1095
164
+ (0, vitest_1.expect)(result).toBe(1095n);
165
+ });
166
+ (0, vitest_1.it)("adds favorable price impact", () => {
167
+ const result = (0, positions_1.getPositionNetValueAfterAllFees)({
168
+ collateralUsd: 1000n,
169
+ pnl: 200n,
170
+ pendingBorrowingFeesUsd: 0n,
171
+ pendingFundingFeesUsd: 0n,
172
+ netPriceImpactDeltaUsd: 25n,
173
+ closingFeeUsd: 0n,
174
+ });
175
+ // 1000 + 200 + 25 = 1225
176
+ (0, vitest_1.expect)(result).toBe(1225n);
177
+ });
178
+ (0, vitest_1.it)("reduces to collateral when pnl, fees, and impact are zero", () => {
179
+ const result = (0, positions_1.getPositionNetValueAfterAllFees)({
180
+ collateralUsd: 500n,
181
+ pnl: 0n,
182
+ pendingBorrowingFeesUsd: 0n,
183
+ pendingFundingFeesUsd: 0n,
184
+ netPriceImpactDeltaUsd: 0n,
185
+ closingFeeUsd: 0n,
186
+ });
187
+ (0, vitest_1.expect)(result).toBe(500n);
188
+ });
189
+ (0, vitest_1.it)("handles negative pnl with adverse impact", () => {
190
+ const result = (0, positions_1.getPositionNetValueAfterAllFees)({
191
+ collateralUsd: 1000n,
192
+ pnl: -300n,
193
+ pendingBorrowingFeesUsd: 5n,
194
+ pendingFundingFeesUsd: 5n,
195
+ netPriceImpactDeltaUsd: -20n,
196
+ closingFeeUsd: 10n,
197
+ });
198
+ // 1000 - 300 - 5 - 5 - 20 - 10 = 660
199
+ (0, vitest_1.expect)(result).toBe(660n);
200
+ });
201
+ });
202
+ (0, vitest_1.describe)("getPositionPnlAfterAllFees", () => {
203
+ (0, vitest_1.it)("subtracts all fees and adds signed price impact (adverse) from pnl", () => {
204
+ const result = (0, positions_1.getPositionPnlAfterAllFees)({
205
+ pnl: 500n,
206
+ pendingBorrowingFeesUsd: 30n,
207
+ pendingFundingFeesUsd: 20n,
208
+ netPriceImpactDeltaUsd: -40n,
209
+ closingFeeUsd: 50n,
210
+ });
211
+ // 500 - 30 - 20 - 40 - 50 = 360
212
+ (0, vitest_1.expect)(result).toBe(360n);
213
+ });
214
+ (0, vitest_1.it)("adds favorable price impact to pnl", () => {
215
+ const result = (0, positions_1.getPositionPnlAfterAllFees)({
216
+ pnl: 100n,
217
+ pendingBorrowingFeesUsd: 0n,
218
+ pendingFundingFeesUsd: 0n,
219
+ netPriceImpactDeltaUsd: 50n,
220
+ closingFeeUsd: 0n,
221
+ });
222
+ (0, vitest_1.expect)(result).toBe(150n);
223
+ });
224
+ (0, vitest_1.it)("returns pnl when fees and impact are zero", () => {
225
+ const result = (0, positions_1.getPositionPnlAfterAllFees)({
226
+ pnl: 200n,
227
+ pendingBorrowingFeesUsd: 0n,
228
+ pendingFundingFeesUsd: 0n,
229
+ netPriceImpactDeltaUsd: 0n,
230
+ closingFeeUsd: 0n,
231
+ });
232
+ (0, vitest_1.expect)(result).toBe(200n);
233
+ });
234
+ (0, vitest_1.it)("handles negative pnl with adverse impact", () => {
235
+ const result = (0, positions_1.getPositionPnlAfterAllFees)({
236
+ pnl: -100n,
237
+ pendingBorrowingFeesUsd: 10n,
238
+ pendingFundingFeesUsd: 5n,
239
+ netPriceImpactDeltaUsd: -20n,
240
+ closingFeeUsd: 30n,
241
+ });
242
+ // -100 - 10 - 5 - 20 - 30 = -165
243
+ (0, vitest_1.expect)(result).toBe(-165n);
244
+ });
245
+ });
153
246
  (0, vitest_1.describe)("getLeverage", () => {
154
247
  (0, vitest_1.it)("returns undefined if remainingCollateralUsd <= 0", () => {
155
248
  const result = (0, positions_1.getLeverage)({
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMarketAddressByName = exports.getMarketWithTiers = exports.composeFullMarketsInfoData = exports.hydrateMarketInfo = exports.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketDeprecated = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverageByMinCollateralFactor = exports.getMaxLeverageByMinCollateralFactor = exports.getCappedPoolPnl = exports.getPoolUsdWithoutPnl = exports.getTokenPoolType = exports.getContractMarketPrices = exports.getMarketPoolName = exports.getMarketBaseName = exports.getMarketIndexName = exports.getMarketFullName = void 0;
3
+ exports.getMarketAddressByName = exports.getMarketWithTiers = exports.composeFullMarketsInfoData = exports.hydrateMarketInfo = exports.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketDeprecated = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverage = exports.getMaxLeverageByMinCollateralFactor = exports.getCappedPoolPnl = exports.getPoolUsdWithoutPnl = exports.getTokenPoolType = exports.getContractMarketPrices = exports.getMarketPoolName = exports.getMarketBaseName = exports.getMarketIndexName = exports.getMarketFullName = void 0;
4
4
  const viem_1 = require("viem");
5
5
  const factors_1 = require("../../configs/factors");
6
- const marketHours_1 = require("../../configs/marketHours");
7
6
  const tokens_1 = require("../../configs/tokens");
8
7
  const bigmath_1 = require("../bigmath");
9
8
  const fees_1 = require("../fees");
@@ -96,7 +95,9 @@ function getCappedPoolPnl(p) {
96
95
  }
97
96
  exports.getCappedPoolPnl = getCappedPoolPnl;
98
97
  /**
99
- * Rounds to nearest .0 leverage, so that getMaxAllowedLeverageByMinCollateralFactor can get .0 or .5 leverage.
98
+ * Theoretical max leverage derived purely from minCollateralFactor. Does not account for
99
+ * fees or the liquidation safety buffer. Used by the deposit-collateral path where the
100
+ * user is not opening a new size.
100
101
  */
101
102
  const ROUNDING_VALUE = 10000;
102
103
  function getMaxLeverageByMinCollateralFactor(minCollateralFactor) {
@@ -109,19 +110,23 @@ function getMaxLeverageByMinCollateralFactor(minCollateralFactor) {
109
110
  return rounded;
110
111
  }
111
112
  exports.getMaxLeverageByMinCollateralFactor = getMaxLeverageByMinCollateralFactor;
112
- function getMaxAllowedLeverageByMinCollateralFactor(minCollateralFactor, marketAddress) {
113
- if (marketAddress) {
114
- const cfg = marketHours_1.MARKET_HOURS_MARKETS[marketAddress];
115
- if (cfg) {
116
- if (minCollateralFactor === cfg.onHoursMcf)
117
- return cfg.onHoursMaxLeverage * factors_1.BASIS_POINTS_DIVISOR;
118
- if (minCollateralFactor === cfg.offHoursMcf)
119
- return cfg.offHoursMaxLeverage * factors_1.BASIS_POINTS_DIVISOR;
120
- }
113
+ const MAX_ALLOWED_LEVERAGE_STEP_BP = 5 * factors_1.BASIS_POINTS_DIVISOR;
114
+ function getMaxAllowedLeverage({ minCollateralFactor, minCollateralFactorForLiquidation, positionFeeFactorForBalanceWasNotImproved, }) {
115
+ if (minCollateralFactor === undefined ||
116
+ minCollateralFactor === 0n ||
117
+ minCollateralFactorForLiquidation === undefined ||
118
+ minCollateralFactorForLiquidation === 0n ||
119
+ positionFeeFactorForBalanceWasNotImproved === undefined) {
120
+ return 100 * factors_1.BASIS_POINTS_DIVISOR;
121
121
  }
122
- return getMaxLeverageByMinCollateralFactor(minCollateralFactor) / 2;
123
- }
124
- exports.getMaxAllowedLeverageByMinCollateralFactor = getMaxAllowedLeverageByMinCollateralFactor;
122
+ const openingDenominator = minCollateralFactor + 2n * positionFeeFactorForBalanceWasNotImproved;
123
+ const openingMaxBp = bigmath_1.bigMath.mulDiv(numbers_1.PRECISION, factors_1.BASIS_POINTS_DIVISOR_BIGINT, openingDenominator);
124
+ const liquidationDenominator = 2n * minCollateralFactorForLiquidation;
125
+ const liquidationMaxBp = bigmath_1.bigMath.mulDiv(numbers_1.PRECISION, factors_1.BASIS_POINTS_DIVISOR_BIGINT, liquidationDenominator);
126
+ const rawMaxBp = bigmath_1.bigMath.min(openingMaxBp, liquidationMaxBp);
127
+ return Math.floor(Number(rawMaxBp) / MAX_ALLOWED_LEVERAGE_STEP_BP) * MAX_ALLOWED_LEVERAGE_STEP_BP;
128
+ }
129
+ exports.getMaxAllowedLeverage = getMaxAllowedLeverage;
125
130
  function getOppositeCollateral(marketInfo, tokenAddress) {
126
131
  const poolType = getTokenPoolType(marketInfo, tokenAddress);
127
132
  if (poolType === "long") {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPositionInfo = exports.getContractPositionDynamicFees = exports.getMinCollateralFactorForPosition = exports.getPriceImpactDiffUsd = exports.getNetPriceImpactDeltaUsdForDecrease = exports.getLiquidationPrice = exports.getLeverage = exports.getPositionPnlAfterFees = exports.getPositionNetValue = exports.getPositionPendingFeesUsd = exports.getPositionValueUsd = exports.getPositionPnlUsd = exports.getEntryPrice = exports.parsePositionKey = exports.getPositionKey = void 0;
3
+ exports.getPositionInfo = exports.getContractPositionDynamicFees = exports.getMinCollateralFactorForPosition = exports.getPriceImpactDiffUsd = exports.getNetPriceImpactDeltaUsdForDecrease = exports.getLiquidationPrice = exports.getLeverage = exports.getPositionPnlAfterAllFees = exports.getPositionPnlAfterFees = exports.getPositionNetValueAfterAllFees = exports.getPositionNetValue = exports.getPositionPendingFeesUsd = exports.getPositionValueUsd = exports.getPositionPnlUsd = exports.getEntryPrice = exports.parsePositionKey = exports.getPositionKey = void 0;
4
4
  const factors_1 = require("../../configs/factors");
5
5
  const bigmath_1 = require("../bigmath");
6
6
  const fees_1 = require("../fees");
@@ -63,11 +63,20 @@ function getPositionNetValue(p) {
63
63
  return collateralUsd - pendingFeesUsd + pnl;
64
64
  }
65
65
  exports.getPositionNetValue = getPositionNetValue;
66
+ function getPositionNetValueAfterAllFees(p) {
67
+ const { collateralUsd, pnl, pendingBorrowingFeesUsd, pendingFundingFeesUsd, netPriceImpactDeltaUsd, closingFeeUsd } = p;
68
+ return collateralUsd + pnl - pendingBorrowingFeesUsd - pendingFundingFeesUsd + netPriceImpactDeltaUsd - closingFeeUsd;
69
+ }
70
+ exports.getPositionNetValueAfterAllFees = getPositionNetValueAfterAllFees;
66
71
  function getPositionPnlAfterFees({ pnl, pendingBorrowingFeesUsd, pendingFundingFeesUsd, }) {
67
72
  const pnlAfterFees = pnl - pendingBorrowingFeesUsd - pendingFundingFeesUsd;
68
73
  return pnlAfterFees;
69
74
  }
70
75
  exports.getPositionPnlAfterFees = getPositionPnlAfterFees;
76
+ function getPositionPnlAfterAllFees({ pnl, pendingBorrowingFeesUsd, pendingFundingFeesUsd, netPriceImpactDeltaUsd, closingFeeUsd, }) {
77
+ return pnl - pendingBorrowingFeesUsd - pendingFundingFeesUsd + netPriceImpactDeltaUsd - closingFeeUsd;
78
+ }
79
+ exports.getPositionPnlAfterAllFees = getPositionPnlAfterAllFees;
71
80
  function getLeverage(p) {
72
81
  const { pnl, sizeInUsd, collateralUsd, pendingBorrowingFeesUsd, pendingFundingFeesUsd } = p;
73
82
  const totalPendingFeesUsd = getPositionPendingFeesUsd({ pendingFundingFeesUsd, pendingBorrowingFeesUsd });
@@ -325,6 +334,22 @@ function getPositionInfo(p) {
325
334
  pendingFundingFeesUsd,
326
335
  });
327
336
  const pnlAfterFeesPercentage = collateralUsd !== 0n ? (0, numbers_1.getBasisPoints)(pnlAfterFees, collateralUsd) : 0n;
337
+ const netValueAfterAllFees = getPositionNetValueAfterAllFees({
338
+ collateralUsd,
339
+ pnl,
340
+ pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
341
+ pendingFundingFeesUsd,
342
+ netPriceImpactDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
343
+ closingFeeUsd,
344
+ });
345
+ const pnlAfterAllFees = getPositionPnlAfterAllFees({
346
+ pnl,
347
+ pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
348
+ pendingFundingFeesUsd,
349
+ netPriceImpactDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
350
+ closingFeeUsd,
351
+ });
352
+ const pnlAfterAllFeesPercentage = collateralUsd !== 0n ? (0, numbers_1.getBasisPoints)(pnlAfterAllFees, collateralUsd) : 0n;
328
353
  const leverage = getLeverage({
329
354
  sizeInUsd: position.sizeInUsd,
330
355
  collateralUsd,
@@ -346,7 +371,11 @@ function getPositionInfo(p) {
346
371
  pendingFundingFeesUsd,
347
372
  pnl: undefined,
348
373
  });
349
- const maxAllowedLeverage = (0, markets_1.getMaxAllowedLeverageByMinCollateralFactor)(marketInfo.minCollateralFactor, marketInfo.marketTokenAddress);
374
+ const maxAllowedLeverage = (0, markets_1.getMaxAllowedLeverage)({
375
+ minCollateralFactor: marketInfo.minCollateralFactor,
376
+ minCollateralFactorForLiquidation: marketInfo.minCollateralFactorForLiquidation,
377
+ positionFeeFactorForBalanceWasNotImproved: marketInfo.positionFeeFactorForBalanceWasNotImproved,
378
+ });
350
379
  const hasLowCollateral = (leverage !== undefined && leverage > maxAllowedLeverage) || false;
351
380
  const liquidationPrice = getLiquidationPrice({
352
381
  marketInfo,
@@ -399,6 +428,9 @@ function getPositionInfo(p) {
399
428
  pnlPercentage,
400
429
  pnlAfterFees,
401
430
  pnlAfterFeesPercentage,
431
+ netValueAfterAllFees,
432
+ pnlAfterAllFees,
433
+ pnlAfterAllFeesPercentage,
402
434
  netValue,
403
435
  netPriceImapctDeltaUsd: netPriceImpactValues.totalImpactDeltaUsd,
404
436
  priceImpactDiffUsd: netPriceImpactValues.priceImpactDiffUsd,
@@ -126,9 +126,9 @@ function getAcceptablePriceByPriceImpact(p) {
126
126
  }
127
127
  exports.getAcceptablePriceByPriceImpact = getAcceptablePriceByPriceImpact;
128
128
  function getDefaultAcceptablePriceImpactBps(p) {
129
- const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImapctBuffer = factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, } = p;
129
+ const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImpactBuffer = factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, } = p;
130
130
  if (priceImpactDeltaUsd > 0) {
131
- return BigInt(acceptablePriceImapctBuffer);
131
+ return BigInt(acceptablePriceImpactBuffer);
132
132
  }
133
133
  const baseAcceptablePriceValues = getAcceptablePriceByPriceImpact({
134
134
  isIncrease: p.isIncrease,
@@ -138,8 +138,8 @@ function getDefaultAcceptablePriceImpactBps(p) {
138
138
  priceImpactDeltaUsd,
139
139
  });
140
140
  if (baseAcceptablePriceValues.acceptablePriceDeltaBps < 0) {
141
- return bigmath_1.bigMath.abs(baseAcceptablePriceValues.acceptablePriceDeltaBps) + BigInt(acceptablePriceImapctBuffer);
141
+ return bigmath_1.bigMath.abs(baseAcceptablePriceValues.acceptablePriceDeltaBps) + BigInt(acceptablePriceImpactBuffer);
142
142
  }
143
- return BigInt(acceptablePriceImapctBuffer);
143
+ return BigInt(acceptablePriceImpactBuffer);
144
144
  }
145
145
  exports.getDefaultAcceptablePriceImpactBps = getDefaultAcceptablePriceImpactBps;
@@ -384,7 +384,7 @@ function applyAcceptablePrice(p) {
384
384
  indexPrice: values.indexPrice,
385
385
  sizeDeltaUsd: values.sizeDeltaUsd,
386
386
  priceImpactDeltaUsd: values.closePriceImpactDeltaUsd,
387
- acceptablePriceImapctBuffer: acceptablePriceImpactBuffer || factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER,
387
+ acceptablePriceImpactBuffer: acceptablePriceImpactBuffer || factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER,
388
388
  });
389
389
  if (maxNegativePriceImpactBps === undefined) {
390
390
  maxNegativePriceImpactBps = values.recommendedAcceptablePriceDeltaBps;
@@ -274,7 +274,7 @@ function getIncreasePositionAmounts(p) {
274
274
  indexPrice: values.indexPrice,
275
275
  sizeDeltaUsd: values.sizeDeltaUsd,
276
276
  priceImpactDeltaUsd: values.positionPriceImpactDeltaUsd,
277
- acceptablePriceImapctBuffer: acceptablePriceImpactBuffer || factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER,
277
+ acceptablePriceImpactBuffer: acceptablePriceImpactBuffer || factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER,
278
278
  });
279
279
  if (maxNegativePriceImpactBps === undefined) {
280
280
  maxNegativePriceImpactBps = values.recommendedAcceptablePriceDeltaBps;
@@ -117,6 +117,7 @@ function createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensDa
117
117
  basePnlUsd: rawAction.basePnlUsd ? BigInt(rawAction.basePnlUsd) : undefined,
118
118
  priceImpactDiffUsd: rawAction.priceImpactDiffUsd ? BigInt(rawAction.priceImpactDiffUsd) : undefined,
119
119
  priceImpactUsd: rawAction.priceImpactUsd ? BigInt(rawAction.priceImpactUsd) : undefined,
120
+ swapImpactUsd: rawAction.swapImpactUsd ? BigInt(rawAction.swapImpactUsd) : undefined,
120
121
  totalImpactUsd: rawAction.totalImpactUsd ? BigInt(rawAction.totalImpactUsd) : undefined,
121
122
  positionFeeAmount: rawAction.positionFeeAmount ? BigInt(rawAction.positionFeeAmount) : undefined,
122
123
  borrowingFeeAmount: rawAction.borrowingFeeAmount ? BigInt(rawAction.borrowingFeeAmount) : undefined,
@@ -1,5 +1,5 @@
1
1
  const config = {
2
- schema: "https://gmx.squids.live/gmx-synthetics-arbitrum@24678a/api/graphql",
2
+ schema: "https://gmx.squids.live/gmx-synthetics-arbitrum@5eaa69/api/graphql",
3
3
  overwrite: true,
4
4
  debug: true,
5
5
  generates: {
@@ -166,9 +166,13 @@ export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirec
166
166
 
167
167
  priceImpactUsd
168
168
  priceImpactDiffUsd
169
+ swapImpactUsd
170
+ totalImpactUsd
169
171
  positionFeeAmount
170
172
  borrowingFeeAmount
171
173
  fundingFeeAmount
174
+ swapFeeUsd
175
+ liquidationFeeAmount
172
176
  pnlUsd
173
177
  basePnlUsd
174
178
 
@@ -1102,6 +1102,12 @@ export var MarketInfoOrderByInput;
1102
1102
  MarketInfoOrderByInput["maxShortPoolUsdForDeposit_DESC"] = "maxShortPoolUsdForDeposit_DESC";
1103
1103
  MarketInfoOrderByInput["maxShortPoolUsdForDeposit_DESC_NULLS_FIRST"] = "maxShortPoolUsdForDeposit_DESC_NULLS_FIRST";
1104
1104
  MarketInfoOrderByInput["maxShortPoolUsdForDeposit_DESC_NULLS_LAST"] = "maxShortPoolUsdForDeposit_DESC_NULLS_LAST";
1105
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_ASC"] = "minCollateralFactorForLiquidation_ASC";
1106
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_ASC_NULLS_FIRST"] = "minCollateralFactorForLiquidation_ASC_NULLS_FIRST";
1107
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_ASC_NULLS_LAST"] = "minCollateralFactorForLiquidation_ASC_NULLS_LAST";
1108
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_DESC"] = "minCollateralFactorForLiquidation_DESC";
1109
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_DESC_NULLS_FIRST"] = "minCollateralFactorForLiquidation_DESC_NULLS_FIRST";
1110
+ MarketInfoOrderByInput["minCollateralFactorForLiquidation_DESC_NULLS_LAST"] = "minCollateralFactorForLiquidation_DESC_NULLS_LAST";
1105
1111
  MarketInfoOrderByInput["minCollateralFactorForOpenInterestLong_ASC"] = "minCollateralFactorForOpenInterestLong_ASC";
1106
1112
  MarketInfoOrderByInput["minCollateralFactorForOpenInterestLong_ASC_NULLS_FIRST"] = "minCollateralFactorForOpenInterestLong_ASC_NULLS_FIRST";
1107
1113
  MarketInfoOrderByInput["minCollateralFactorForOpenInterestLong_ASC_NULLS_LAST"] = "minCollateralFactorForOpenInterestLong_ASC_NULLS_LAST";
@@ -145,6 +145,7 @@ data = "0x1", to = "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64", receiver = "0x1
145
145
  priceIn,
146
146
  priceOut,
147
147
  feesUsd,
148
+ slippage: 0,
148
149
  txnData: {
149
150
  to,
150
151
  data,