@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc3

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 (157) hide show
  1. package/lib/backend/globalTrades/index.d.ts +11 -0
  2. package/lib/backend/globalTrades/index.js +69 -0
  3. package/lib/backend/index.d.ts +2 -0
  4. package/lib/backend/index.js +18 -0
  5. package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
  6. package/lib/backend/tradingVariables/backend.types.js +2 -0
  7. package/lib/backend/tradingVariables/converter.d.ts +30 -0
  8. package/lib/backend/tradingVariables/converter.js +329 -0
  9. package/lib/backend/tradingVariables/index.d.ts +3 -0
  10. package/lib/backend/tradingVariables/index.js +78 -0
  11. package/lib/backend/tradingVariables/types.d.ts +109 -0
  12. package/lib/backend/tradingVariables/types.js +14 -0
  13. package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +0 -1
  14. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  15. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  16. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  17. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  18. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  19. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  20. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
  21. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
  22. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +7 -20
  23. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  24. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  25. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  26. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  27. package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
  28. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
  29. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
  30. package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
  31. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  32. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  33. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  34. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  35. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  36. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  37. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
  38. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
  39. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  40. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  41. package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
  42. package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
  43. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
  44. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
  45. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
  46. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
  47. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
  48. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
  49. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
  50. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
  51. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +7 -16
  52. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  53. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  54. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  55. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  56. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
  57. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
  58. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
  59. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
  60. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  61. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  62. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
  63. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
  64. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  65. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  66. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  67. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  68. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  69. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  70. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  71. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
  72. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
  73. package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
  74. package/lib/contracts/utils/index.d.ts +0 -3
  75. package/lib/contracts/utils/index.js +0 -3
  76. package/lib/contracts/utils/openTrades.js +14 -30
  77. package/lib/index.d.ts +1 -0
  78. package/lib/index.js +1 -0
  79. package/lib/markets/collateral/converter.d.ts +5 -0
  80. package/lib/markets/collateral/converter.js +11 -0
  81. package/lib/markets/collateral/index.d.ts +1 -0
  82. package/lib/markets/collateral/index.js +17 -0
  83. package/lib/markets/collateral/types.d.ts +7 -0
  84. package/lib/markets/collateral/types.js +2 -0
  85. package/lib/markets/index.d.ts +2 -0
  86. package/lib/markets/index.js +2 -0
  87. package/lib/markets/oi/converter.d.ts +63 -0
  88. package/lib/markets/oi/converter.js +103 -0
  89. package/lib/markets/oi/fetcher.d.ts +58 -0
  90. package/lib/markets/oi/fetcher.js +181 -0
  91. package/lib/markets/oi/index.d.ts +10 -0
  92. package/lib/markets/oi/index.js +37 -0
  93. package/lib/markets/oi/types.d.ts +82 -0
  94. package/lib/markets/oi/types.js +6 -0
  95. package/lib/markets/oi/validation.d.ts +80 -0
  96. package/lib/markets/oi/validation.js +172 -0
  97. package/lib/trade/fees/borrowing/index.d.ts +13 -2
  98. package/lib/trade/fees/borrowing/index.js +30 -14
  99. package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
  100. package/lib/trade/fees/borrowingV2/fetcher.js +193 -0
  101. package/lib/trade/fees/borrowingV2/index.d.ts +1 -0
  102. package/lib/trade/fees/borrowingV2/index.js +1 -0
  103. package/lib/trade/fees/converter.d.ts +48 -0
  104. package/lib/trade/fees/converter.js +110 -0
  105. package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
  106. package/lib/trade/fees/fundingFees/fetcher.js +150 -0
  107. package/lib/trade/fees/fundingFees/index.d.ts +1 -0
  108. package/lib/trade/fees/fundingFees/index.js +4 -0
  109. package/lib/trade/fees/index.d.ts +2 -1
  110. package/lib/trade/fees/index.js +44 -35
  111. package/lib/trade/fees/tiers/converter.d.ts +54 -0
  112. package/lib/trade/fees/tiers/converter.js +81 -0
  113. package/lib/trade/fees/tiers/index.d.ts +1 -0
  114. package/lib/trade/fees/tiers/index.js +1 -0
  115. package/lib/trade/fees/trading/index.d.ts +22 -2
  116. package/lib/trade/fees/trading/index.js +44 -1
  117. package/lib/trade/fees/trading/types.d.ts +9 -0
  118. package/lib/trade/index.d.ts +1 -1
  119. package/lib/trade/index.js +1 -1
  120. package/lib/trade/liquidation/converter.d.ts +23 -0
  121. package/lib/trade/liquidation/converter.js +46 -0
  122. package/lib/trade/liquidation/index.d.ts +31 -0
  123. package/lib/trade/liquidation/index.js +187 -0
  124. package/lib/trade/liquidation/types.d.ts +44 -0
  125. package/lib/trade/liquidation/types.js +2 -0
  126. package/lib/trade/liquidation.d.ts +2 -32
  127. package/lib/trade/liquidation.js +8 -93
  128. package/lib/trade/pnl/converter.d.ts +47 -0
  129. package/lib/trade/pnl/converter.js +72 -0
  130. package/lib/trade/pnl/index.d.ts +86 -0
  131. package/lib/trade/pnl/index.js +201 -0
  132. package/lib/trade/pnl/types.d.ts +86 -0
  133. package/lib/trade/pnl/types.js +5 -0
  134. package/lib/trade/pnl.d.ts +3 -3
  135. package/lib/trade/pnl.js +2 -7
  136. package/lib/trade/priceImpact/close/index.d.ts +21 -0
  137. package/lib/trade/priceImpact/close/index.js +131 -0
  138. package/lib/trade/priceImpact/close/types.d.ts +43 -0
  139. package/lib/trade/priceImpact/close/types.js +5 -0
  140. package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
  141. package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
  142. package/lib/trade/priceImpact/cumulVol/index.d.ts +107 -0
  143. package/lib/trade/priceImpact/cumulVol/index.js +228 -0
  144. package/lib/trade/priceImpact/index.d.ts +6 -2
  145. package/lib/trade/priceImpact/index.js +30 -3
  146. package/lib/trade/priceImpact/open/index.d.ts +22 -0
  147. package/lib/trade/priceImpact/open/index.js +76 -0
  148. package/lib/trade/priceImpact/open/types.d.ts +41 -0
  149. package/lib/trade/priceImpact/open/types.js +5 -0
  150. package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
  151. package/lib/trade/priceImpact/skew/fetcher.js +168 -0
  152. package/lib/trade/priceImpact/skew/index.d.ts +1 -0
  153. package/lib/trade/priceImpact/skew/index.js +4 -0
  154. package/lib/trade/spread.d.ts +5 -18
  155. package/lib/trade/spread.js +17 -106
  156. package/lib/trade/types.d.ts +9 -7
  157. package/package.json +2 -2
@@ -0,0 +1,63 @@
1
+ import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
2
+ import type { PairOiToken, SkewPriceImpactContext } from "..";
3
+ /**
4
+ * @dev Fetches pair open interest in tokens for a specific pair
5
+ * @param contract GNSMultiCollatDiamond contract instance
6
+ * @param collateralIndex Collateral index
7
+ * @param pairIndex Pair index
8
+ * @returns Promise resolving to pair OI in tokens
9
+ */
10
+ export declare const fetchPairOiAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<PairOiToken>;
11
+ /**
12
+ * @dev Fetches pair open interest in tokens for multiple pairs
13
+ * @param contract GNSMultiCollatDiamond contract instance
14
+ * @param collateralIndices Array of collateral indices
15
+ * @param pairIndices Array of pair indices
16
+ * @returns Promise resolving to array of pair OI in tokens
17
+ */
18
+ export declare const fetchPairOisAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<PairOiToken[]>;
19
+ /**
20
+ * @dev Fetches skew depth for a specific pair
21
+ * @param contract GNSMultiCollatDiamond contract instance
22
+ * @param collateralIndex Collateral index
23
+ * @param pairIndex Pair index
24
+ * @param collateralDecimals Number of decimals for the collateral
25
+ * @returns Promise resolving to normalized skew depth
26
+ */
27
+ export declare const fetchPairSkewDepth: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, collateralDecimals: number) => Promise<number>;
28
+ /**
29
+ * @dev Fetches skew depths for multiple pairs
30
+ * @param contract GNSMultiCollatDiamond contract instance
31
+ * @param collateralIndices Array of collateral indices
32
+ * @param pairIndices Array of pair indices
33
+ * @param collateralDecimals Array of collateral decimals for each pair
34
+ * @returns Promise resolving to array of normalized skew depths
35
+ */
36
+ export declare const fetchPairSkewDepths: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], collateralDecimals: number[]) => Promise<number[]>;
37
+ /**
38
+ * @dev Fetches complete skew price impact context for multiple pairs
39
+ * @param contract GNSMultiCollatDiamond contract instance
40
+ * @param collateralIndices Array of collateral indices
41
+ * @param pairIndices Array of pair indices
42
+ * @param collateralDecimals Array of collateral decimals for each pair
43
+ * @returns Promise resolving to complete skew price impact context
44
+ */
45
+ export declare const fetchSkewPriceImpactContext: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], collateralDecimals: number[]) => Promise<SkewPriceImpactContext>;
46
+ /**
47
+ * @dev Fetches collateral decimals for given collateral indices
48
+ * @param contract GNSMultiCollatDiamond contract instance
49
+ * @param collateralIndices Array of collateral indices
50
+ * @returns Promise resolving to array of decimals
51
+ */
52
+ export declare const fetchCollateralDecimals: (contract: GNSMultiCollatDiamond, collateralIndices: number[]) => Promise<number[]>;
53
+ /**
54
+ * @dev Calculates skew price impact for a trade using contract call
55
+ * @param contract GNSMultiCollatDiamond contract instance
56
+ * @param collateralIndex Collateral index
57
+ * @param pairIndex Pair index
58
+ * @param long Whether trade is long
59
+ * @param positionSizeToken Position size in tokens
60
+ * @param open Whether trade is opening
61
+ * @returns Promise resolving to price impact percentage (1e10)
62
+ */
63
+ export declare const calculateTradeSkewPriceImpact: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, long: boolean, positionSizeToken: number, open: boolean) => Promise<number>;
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
13
+ const __1 = require("..");
14
+ /**
15
+ * @dev Fetches pair open interest in tokens for a specific pair
16
+ * @param contract GNSMultiCollatDiamond contract instance
17
+ * @param collateralIndex Collateral index
18
+ * @param pairIndex Pair index
19
+ * @returns Promise resolving to pair OI in tokens
20
+ */
21
+ const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
22
+ try {
23
+ const contractData = yield contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
24
+ return (0, __1.convertPairOiToken)(contractData);
25
+ }
26
+ catch (error) {
27
+ console.error("Error fetching pair OI token:", error);
28
+ throw error;
29
+ }
30
+ });
31
+ exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
32
+ /**
33
+ * @dev Fetches pair open interest in tokens for multiple pairs
34
+ * @param contract GNSMultiCollatDiamond contract instance
35
+ * @param collateralIndices Array of collateral indices
36
+ * @param pairIndices Array of pair indices
37
+ * @returns Promise resolving to array of pair OI in tokens
38
+ */
39
+ const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
40
+ if (collateralIndices.length !== pairIndices.length) {
41
+ throw new Error("Collateral indices and pair indices arrays must have the same length");
42
+ }
43
+ try {
44
+ const contractDataArray = yield contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
45
+ return contractDataArray.map(__1.convertPairOiToken);
46
+ }
47
+ catch (error) {
48
+ console.error("Error fetching pair OIs token:", error);
49
+ throw error;
50
+ }
51
+ });
52
+ exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
53
+ /**
54
+ * @dev Fetches skew depth for a specific pair
55
+ * @param contract GNSMultiCollatDiamond contract instance
56
+ * @param collateralIndex Collateral index
57
+ * @param pairIndex Pair index
58
+ * @param collateralDecimals Number of decimals for the collateral
59
+ * @returns Promise resolving to normalized skew depth
60
+ */
61
+ const fetchPairSkewDepth = (contract, collateralIndex, pairIndex, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
62
+ try {
63
+ const contractDepth = yield contract.getPairSkewDepth(collateralIndex, pairIndex);
64
+ return (0, __1.normalizeSkewDepth)(contractDepth.toBigInt(), collateralDecimals);
65
+ }
66
+ catch (error) {
67
+ console.error("Error fetching skew depth:", error);
68
+ throw error;
69
+ }
70
+ });
71
+ exports.fetchPairSkewDepth = fetchPairSkewDepth;
72
+ /**
73
+ * @dev Fetches skew depths for multiple pairs
74
+ * @param contract GNSMultiCollatDiamond contract instance
75
+ * @param collateralIndices Array of collateral indices
76
+ * @param pairIndices Array of pair indices
77
+ * @param collateralDecimals Array of collateral decimals for each pair
78
+ * @returns Promise resolving to array of normalized skew depths
79
+ */
80
+ const fetchPairSkewDepths = (contract, collateralIndices, pairIndices, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
81
+ if (collateralIndices.length !== pairIndices.length ||
82
+ pairIndices.length !== collateralDecimals.length) {
83
+ throw new Error("All input arrays must have the same length");
84
+ }
85
+ try {
86
+ const contractDepths = yield contract.getPairSkewDepths(collateralIndices, pairIndices);
87
+ return contractDepths.map((depth, i) => (0, __1.normalizeSkewDepth)(depth.toBigInt(), collateralDecimals[i]));
88
+ }
89
+ catch (error) {
90
+ console.error("Error fetching skew depths:", error);
91
+ throw error;
92
+ }
93
+ });
94
+ exports.fetchPairSkewDepths = fetchPairSkewDepths;
95
+ /**
96
+ * @dev Fetches complete skew price impact context for multiple pairs
97
+ * @param contract GNSMultiCollatDiamond contract instance
98
+ * @param collateralIndices Array of collateral indices
99
+ * @param pairIndices Array of pair indices
100
+ * @param collateralDecimals Array of collateral decimals for each pair
101
+ * @returns Promise resolving to complete skew price impact context
102
+ */
103
+ const fetchSkewPriceImpactContext = (contract, collateralIndices, pairIndices, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
104
+ try {
105
+ // Fetch OI data and skew depths in parallel
106
+ const [pairOiTokens, skewDepths] = yield Promise.all([
107
+ (0, exports.fetchPairOisAfterV10Token)(contract, collateralIndices, pairIndices),
108
+ (0, exports.fetchPairSkewDepths)(contract, collateralIndices, pairIndices, collateralDecimals),
109
+ ]);
110
+ return (0, __1.createSkewPriceImpactContext)(collateralIndices, pairIndices, skewDepths, pairOiTokens);
111
+ }
112
+ catch (error) {
113
+ console.error("Error fetching skew price impact context:", error);
114
+ throw error;
115
+ }
116
+ });
117
+ exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
118
+ /**
119
+ * @dev Fetches collateral decimals for given collateral indices
120
+ * @param contract GNSMultiCollatDiamond contract instance
121
+ * @param collateralIndices Array of collateral indices
122
+ * @returns Promise resolving to array of decimals
123
+ */
124
+ const fetchCollateralDecimals = (contract, collateralIndices) => __awaiter(void 0, void 0, void 0, function* () {
125
+ try {
126
+ // Get unique collateral indices to minimize calls
127
+ const uniqueIndices = [...new Set(collateralIndices)];
128
+ // Fetch collateral info for unique indices
129
+ const promises = uniqueIndices.map((index) => __awaiter(void 0, void 0, void 0, function* () {
130
+ const collateral = yield contract.getCollateral(index);
131
+ return { index, decimals: Number(collateral.precision) };
132
+ }));
133
+ const collateralData = yield Promise.all(promises);
134
+ // Create a map for quick lookup
135
+ const decimalsMap = new Map(collateralData.map(data => [data.index, data.decimals]));
136
+ // Return decimals in the same order as input
137
+ return collateralIndices.map(index => decimalsMap.get(index) || 18 // Default to 18 if not found
138
+ );
139
+ }
140
+ catch (error) {
141
+ console.error("Error fetching collateral decimals:", error);
142
+ throw error;
143
+ }
144
+ });
145
+ exports.fetchCollateralDecimals = fetchCollateralDecimals;
146
+ /**
147
+ * @dev Calculates skew price impact for a trade using contract call
148
+ * @param contract GNSMultiCollatDiamond contract instance
149
+ * @param collateralIndex Collateral index
150
+ * @param pairIndex Pair index
151
+ * @param long Whether trade is long
152
+ * @param positionSizeToken Position size in tokens
153
+ * @param open Whether trade is opening
154
+ * @returns Promise resolving to price impact percentage (1e10)
155
+ */
156
+ const calculateTradeSkewPriceImpact = (contract, collateralIndex, pairIndex, long, positionSizeToken, open) => __awaiter(void 0, void 0, void 0, function* () {
157
+ try {
158
+ const priceImpactP = yield contract.getTradeSkewPriceImpactP(collateralIndex, pairIndex, long, BigInt(Math.round(positionSizeToken * 1e18)), // Convert to 1e18 precision
159
+ open);
160
+ // Convert from int256 1e10 to percentage
161
+ return Number(priceImpactP) / 1e10;
162
+ }
163
+ catch (error) {
164
+ console.error("Error calculating trade skew price impact:", error);
165
+ throw error;
166
+ }
167
+ });
168
+ exports.calculateTradeSkewPriceImpact = calculateTradeSkewPriceImpact;
@@ -55,3 +55,4 @@ export declare const getTradeSkewPriceImpactWithChecks: (params: TradeSkewParams
55
55
  */
56
56
  export declare const calculatePartialSizeToken: (originalSizeCollateral: number, deltaCollateral: number, originalSizeToken: number) => number;
57
57
  export * as SkewPriceImpact from "./types";
58
+ export * from "./fetcher";
@@ -26,6 +26,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  __setModuleDefault(result, mod);
27
27
  return result;
28
28
  };
29
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
30
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
31
+ };
29
32
  Object.defineProperty(exports, "__esModule", { value: true });
30
33
  exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpactWithChecks = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = void 0;
31
34
  const utils_1 = require("../../utils");
@@ -173,3 +176,4 @@ const calculatePartialSizeToken = (originalSizeCollateral, deltaCollateral, orig
173
176
  exports.calculatePartialSizeToken = calculatePartialSizeToken;
174
177
  // Export namespace for types
175
178
  exports.SkewPriceImpact = __importStar(require("./types"));
179
+ __exportStar(require("./fetcher"), exports);
@@ -1,18 +1,5 @@
1
- import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "./types";
2
- import { ContractsVersion } from "../contracts/types";
3
- export type SpreadContext = {
4
- isOpen?: boolean;
5
- isPnlPositive?: boolean;
6
- createdBlock?: number;
7
- liquidationParams?: LiquidationParams | undefined;
8
- currentBlock?: number | undefined;
9
- contractsVersion?: ContractsVersion | undefined;
10
- protectionCloseFactorWhitelist?: boolean;
11
- userPriceImpact?: UserPriceImpact | undefined;
12
- } & Partial<PairFactor>;
13
- export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
14
- export declare const isProtectionCloseFactorActive: (spreadCtx: SpreadContext | undefined) => boolean | undefined;
15
- export declare const getCumulativeFactor: (spreadCtx: SpreadContext | undefined) => number;
16
- export declare const getLegacyFactor: (spreadCtx: SpreadContext | undefined) => number;
17
- export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, spreadCtx?: SpreadContext | undefined) => number;
18
- export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined, userPriceImpact?: UserPriceImpact | undefined) => number;
1
+ /**
2
+ * @dev Pure spread calculations without price impact
3
+ * @dev For price impact calculations, see priceImpact module
4
+ */
5
+ export { getSpreadWithPriceImpactP, getSpreadWithCumulVolPriceImpactP, getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, CumulVolContext as SpreadContext, } from "./priceImpact/cumulVol";
@@ -1,108 +1,19 @@
1
1
  "use strict";
2
+ /**
3
+ * @dev Pure spread calculations without price impact
4
+ * @dev For price impact calculations, see priceImpact module
5
+ */
2
6
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSpreadP = exports.getSpreadWithPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = void 0;
4
- const oiWindows_1 = require("./oiWindows");
5
- const constants_1 = require("../constants");
6
- const types_1 = require("../contracts/types");
7
- const getProtectionCloseFactor = (spreadCtx) => {
8
- var _a;
9
- const protectionCloseFactor = spreadCtx === undefined ||
10
- spreadCtx.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
11
- spreadCtx.isOpen === undefined ||
12
- spreadCtx.isPnlPositive === undefined ||
13
- spreadCtx.protectionCloseFactor === undefined ||
14
- (0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true
15
- ? constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR
16
- : spreadCtx.protectionCloseFactor;
17
- const protectionCloseFactorMultiplier = ((_a = spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
18
- spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier > 0
19
- ? spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier
20
- : 1;
21
- return protectionCloseFactor * protectionCloseFactorMultiplier;
22
- };
23
- exports.getProtectionCloseFactor = getProtectionCloseFactor;
24
- const isProtectionCloseFactorActive = (spreadCtx) => {
25
- if (spreadCtx === undefined ||
26
- spreadCtx.currentBlock === undefined ||
27
- spreadCtx.createdBlock === undefined ||
28
- spreadCtx.protectionCloseFactorBlocks === undefined ||
29
- spreadCtx.protectionCloseFactor === undefined) {
30
- return undefined;
31
- }
32
- return (spreadCtx.isPnlPositive === true &&
33
- spreadCtx.isOpen === false &&
34
- spreadCtx.protectionCloseFactor > 0 &&
35
- spreadCtx.currentBlock <=
36
- spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks &&
37
- spreadCtx.protectionCloseFactorWhitelist !== true);
38
- };
39
- exports.isProtectionCloseFactorActive = isProtectionCloseFactorActive;
40
- const getCumulativeFactor = (spreadCtx) => {
41
- if (spreadCtx === undefined ||
42
- spreadCtx.cumulativeFactor === undefined ||
43
- spreadCtx.cumulativeFactor === 0) {
44
- return constants_1.DEFAULT_CUMULATIVE_FACTOR;
45
- }
46
- return spreadCtx.cumulativeFactor;
47
- };
48
- exports.getCumulativeFactor = getCumulativeFactor;
49
- const getLegacyFactor = (spreadCtx) => {
50
- return (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
51
- };
52
- exports.getLegacyFactor = getLegacyFactor;
53
- const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
54
- if (pairSpreadP === undefined) {
55
- return 0;
56
- }
57
- if (
58
- // No spread or price impact when closing pre-v9.2 trades
59
- ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
60
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
61
- // No spread or price impact for opens when `pair.exemptOnOpen` is true
62
- ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === true && (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptOnOpen) === true) ||
63
- // No spread or price impact for closes after `protectionCloseFactor` has expired
64
- // when `pair.exemptAfterProtectionCloseFactor` is true
65
- ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
66
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptAfterProtectionCloseFactor) === true &&
67
- (0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true)) {
68
- return 0;
69
- }
70
- const onePercentDepth = buy
71
- ? // if `long`
72
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
73
- ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
74
- : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
75
- : // if `short`
76
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
77
- ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
78
- : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
79
- let activeOi = undefined;
80
- if (oiWindowsSettings !== undefined) {
81
- activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows, (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false ? buy : !buy);
82
- }
83
- if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
84
- return pairSpreadP / 2;
85
- }
86
- return ((0, exports.getSpreadP)(pairSpreadP, undefined, undefined, spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) +
87
- ((activeOi * (0, exports.getCumulativeFactor)(spreadCtx) + (collateral * leverage) / 2) /
88
- onePercentDepth /
89
- 100 /
90
- (0, exports.getLegacyFactor)(spreadCtx)) *
91
- (0, exports.getProtectionCloseFactor)(spreadCtx));
92
- };
93
- exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
94
- const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
95
- var _a;
96
- const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
97
- if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
98
- return 0;
99
- }
100
- const spreadP = pairSpreadP / 2 + fixedSpreadP;
101
- return isLiquidation === true &&
102
- liquidationParams !== undefined &&
103
- liquidationParams.maxLiqSpreadP > 0 &&
104
- spreadP > liquidationParams.maxLiqSpreadP
105
- ? liquidationParams.maxLiqSpreadP
106
- : spreadP;
107
- };
108
- exports.getSpreadP = getSpreadP;
7
+ exports.getSpreadP = exports.getFixedSpreadP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = exports.getCumulVolPriceImpact = exports.getTradeCumulVolPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadWithPriceImpactP = void 0;
8
+ // Re-export from priceImpact/cumulVol for backward compatibility
9
+ var cumulVol_1 = require("./priceImpact/cumulVol");
10
+ Object.defineProperty(exports, "getSpreadWithPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithPriceImpactP; } });
11
+ Object.defineProperty(exports, "getSpreadWithCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithCumulVolPriceImpactP; } });
12
+ Object.defineProperty(exports, "getTradeCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getTradeCumulVolPriceImpactP; } });
13
+ Object.defineProperty(exports, "getCumulVolPriceImpact", { enumerable: true, get: function () { return cumulVol_1.getCumulVolPriceImpact; } });
14
+ Object.defineProperty(exports, "getProtectionCloseFactor", { enumerable: true, get: function () { return cumulVol_1.getProtectionCloseFactor; } });
15
+ Object.defineProperty(exports, "isProtectionCloseFactorActive", { enumerable: true, get: function () { return cumulVol_1.isProtectionCloseFactorActive; } });
16
+ Object.defineProperty(exports, "getCumulativeFactor", { enumerable: true, get: function () { return cumulVol_1.getCumulativeFactor; } });
17
+ Object.defineProperty(exports, "getLegacyFactor", { enumerable: true, get: function () { return cumulVol_1.getLegacyFactor; } });
18
+ Object.defineProperty(exports, "getFixedSpreadP", { enumerable: true, get: function () { return cumulVol_1.getFixedSpreadP; } });
19
+ Object.defineProperty(exports, "getSpreadP", { enumerable: true, get: function () { return cumulVol_1.getSpreadP; } });
@@ -13,6 +13,14 @@ export type TradeFeesData = {
13
13
  initialAccFundingFeeP: number;
14
14
  initialAccBorrowingFeeP: number;
15
15
  };
16
+ export type UiRealizedPnlData = {
17
+ realizedTradingFeesCollateral: number;
18
+ realizedOldBorrowingFeesCollateral: number;
19
+ realizedNewBorrowingFeesCollateral: number;
20
+ realizedFundingFeesCollateral: number;
21
+ realizedPnlPartialCloseCollateral: number;
22
+ pnlWithdrawnCollateral: number;
23
+ };
16
24
  export type CounterTradeSettings = {
17
25
  maxLeverage: number;
18
26
  feeRateMultiplier: number;
@@ -23,6 +31,7 @@ export type TradeContainer = {
23
31
  liquidationParams: LiquidationParams;
24
32
  initialAccFees: TradeInitialAccFees;
25
33
  tradeFeesData?: TradeFeesData;
34
+ uiRealizedPnlData?: UiRealizedPnlData;
26
35
  receivedAt?: number;
27
36
  };
28
37
  export type Trade = {
@@ -175,13 +184,6 @@ export type OiWindow = PairOi;
175
184
  export type OiWindows = {
176
185
  [key: string]: OiWindow;
177
186
  };
178
- export type CollateralConfig = {
179
- collateral: string;
180
- isActive: boolean;
181
- precision: number;
182
- precisionDelta: number;
183
- decimals?: number;
184
- };
185
187
  export type FeeTiers = {
186
188
  tiers: FeeTier[];
187
189
  multipliers: number[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.0-v10.rc2",
3
+ "version": "0.0.0-v10.rc3",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -98,7 +98,7 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "@ethersproject/providers": "^5.7.2",
101
- "build": "^0.1.4",
101
+ "@gainsnetwork/contests": "^0.0.1",
102
102
  "ethcall": "^4.8.13",
103
103
  "ethers": "^5.7.2",
104
104
  "luxon": "^3.2.0"