@gainsnetwork/sdk 0.2.67-rc6 → 0.2.67-rc8

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 (147) 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 +3 -0
  4. package/lib/backend/index.js +28 -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 +31 -0
  8. package/lib/backend/tradingVariables/converter.js +330 -0
  9. package/lib/backend/tradingVariables/index.d.ts +5 -0
  10. package/lib/backend/tradingVariables/index.js +95 -0
  11. package/lib/backend/tradingVariables/types.d.ts +109 -0
  12. package/lib/backend/tradingVariables/types.js +14 -0
  13. package/lib/constants.js +2 -3
  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/GNSMultiCollatDiamond.d.ts +2140 -286
  19. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  20. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  21. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  22. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  23. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  24. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  25. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  26. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  27. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  28. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  29. package/lib/contracts/types/generated/GToken.d.ts +78 -107
  30. package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
  31. package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
  32. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
  33. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
  34. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
  35. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
  36. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +90 -53
  37. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +4502 -427
  38. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  39. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  40. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  41. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  42. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  43. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  44. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  45. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  46. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  47. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  48. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  49. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
  50. package/lib/contracts/types/generated/factories/GToken__factory.js +65 -142
  51. package/lib/contracts/utils/openLimitOrders.d.ts +8 -0
  52. package/lib/contracts/utils/openLimitOrders.js +88 -0
  53. package/lib/markets/collateral/converter.d.ts +5 -0
  54. package/lib/markets/collateral/converter.js +11 -0
  55. package/lib/markets/collateral/index.d.ts +1 -0
  56. package/lib/markets/collateral/index.js +17 -0
  57. package/lib/markets/collateral/types.d.ts +7 -0
  58. package/lib/markets/collateral/types.js +2 -0
  59. package/lib/markets/oi/converter.d.ts +63 -0
  60. package/lib/markets/oi/converter.js +103 -0
  61. package/lib/markets/oi/fetcher.d.ts +58 -0
  62. package/lib/markets/oi/fetcher.js +181 -0
  63. package/lib/markets/oi/index.d.ts +10 -0
  64. package/lib/markets/oi/index.js +37 -0
  65. package/lib/markets/oi/types.d.ts +82 -0
  66. package/lib/markets/oi/types.js +6 -0
  67. package/lib/markets/oi/validation.d.ts +80 -0
  68. package/lib/markets/oi/validation.js +172 -0
  69. package/lib/trade/fees/borrowing/builder.d.ts +14 -0
  70. package/lib/trade/fees/borrowing/builder.js +33 -0
  71. package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
  72. package/lib/trade/fees/borrowingV2/builder.js +24 -0
  73. package/lib/trade/fees/borrowingV2/converter.d.ts +75 -0
  74. package/lib/trade/fees/borrowingV2/converter.js +132 -0
  75. package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
  76. package/lib/trade/fees/borrowingV2/fetcher.js +185 -0
  77. package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
  78. package/lib/trade/fees/borrowingV2/index.js +112 -0
  79. package/lib/trade/fees/borrowingV2/types.d.ts +95 -0
  80. package/lib/trade/fees/borrowingV2/types.js +5 -0
  81. package/lib/trade/fees/converter.d.ts +48 -0
  82. package/lib/trade/fees/converter.js +110 -0
  83. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  84. package/lib/trade/fees/fundingFees/builder.js +35 -0
  85. package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
  86. package/lib/trade/fees/fundingFees/converter.js +196 -0
  87. package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
  88. package/lib/trade/fees/fundingFees/fetcher.js +150 -0
  89. package/lib/trade/fees/fundingFees/index.d.ts +146 -0
  90. package/lib/trade/fees/fundingFees/index.js +346 -0
  91. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  92. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  93. package/lib/trade/fees/fundingFees/types.d.ts +77 -0
  94. package/lib/trade/fees/fundingFees/types.js +5 -0
  95. package/lib/trade/fees/tiers/converter.d.ts +54 -0
  96. package/lib/trade/fees/tiers/converter.js +81 -0
  97. package/lib/trade/fees/trading/builder.d.ts +18 -0
  98. package/lib/trade/fees/trading/builder.js +20 -0
  99. package/lib/trade/fees/trading/converter.d.ts +30 -0
  100. package/lib/trade/fees/trading/converter.js +43 -0
  101. package/lib/trade/fees/trading/index.d.ts +62 -0
  102. package/lib/trade/fees/trading/index.js +155 -0
  103. package/lib/trade/fees/trading/types.d.ts +48 -0
  104. package/lib/trade/fees/trading/types.js +5 -0
  105. package/lib/trade/liquidation/builder.d.ts +25 -0
  106. package/lib/trade/liquidation/builder.js +59 -0
  107. package/lib/trade/liquidation/converter.d.ts +23 -0
  108. package/lib/trade/liquidation/converter.js +46 -0
  109. package/lib/trade/liquidation/index.d.ts +26 -0
  110. package/lib/trade/liquidation/index.js +142 -0
  111. package/lib/trade/liquidation/types.d.ts +59 -0
  112. package/lib/trade/liquidation/types.js +2 -0
  113. package/lib/trade/openLimitOrder.d.ts +2 -0
  114. package/lib/trade/openLimitOrder.js +23 -0
  115. package/lib/trade/pnl/builder.d.ts +16 -0
  116. package/lib/trade/pnl/builder.js +44 -0
  117. package/lib/trade/pnl/converter.d.ts +47 -0
  118. package/lib/trade/pnl/converter.js +72 -0
  119. package/lib/trade/pnl/index.d.ts +77 -0
  120. package/lib/trade/pnl/index.js +270 -0
  121. package/lib/trade/pnl/types.d.ts +114 -0
  122. package/lib/trade/pnl/types.js +5 -0
  123. package/lib/trade/priceImpact/close/index.d.ts +21 -0
  124. package/lib/trade/priceImpact/close/index.js +131 -0
  125. package/lib/trade/priceImpact/close/types.d.ts +43 -0
  126. package/lib/trade/priceImpact/close/types.js +5 -0
  127. package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
  128. package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
  129. package/lib/trade/priceImpact/cumulVol/index.d.ts +107 -0
  130. package/lib/trade/priceImpact/cumulVol/index.js +228 -0
  131. package/lib/trade/priceImpact/index.d.ts +12 -0
  132. package/lib/trade/priceImpact/index.js +59 -0
  133. package/lib/trade/priceImpact/open/index.d.ts +22 -0
  134. package/lib/trade/priceImpact/open/index.js +76 -0
  135. package/lib/trade/priceImpact/open/types.d.ts +41 -0
  136. package/lib/trade/priceImpact/open/types.js +5 -0
  137. package/lib/trade/priceImpact/skew/converter.d.ts +77 -0
  138. package/lib/trade/priceImpact/skew/converter.js +171 -0
  139. package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
  140. package/lib/trade/priceImpact/skew/fetcher.js +168 -0
  141. package/lib/trade/priceImpact/skew/index.d.ts +58 -0
  142. package/lib/trade/priceImpact/skew/index.js +179 -0
  143. package/lib/trade/priceImpact/skew/types.d.ts +55 -0
  144. package/lib/trade/priceImpact/skew/types.js +5 -0
  145. package/lib/trade/utils.d.ts +18 -0
  146. package/lib/trade/utils.js +30 -0
  147. package/package.json +1 -1
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @dev Converters for skew price impact data between contract and SDK formats
3
+ * @dev All BigNumber values are normalized to floats with appropriate precision
4
+ */
5
+ import { IPriceImpact } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
6
+ import { PairOiToken, PairOiCollateral, SkewDepth, SkewPriceImpactContext } from "./types";
7
+ /**
8
+ * @dev Converts contract pair OI token data to SDK format
9
+ * @param contractData Contract pair OI token struct
10
+ * @returns Normalized pair OI token data
11
+ */
12
+ export declare const convertPairOiToken: (contractData: IPriceImpact.PairOiTokenStruct) => PairOiToken;
13
+ /**
14
+ * @dev Converts array of contract pair OI token data to SDK format
15
+ * @param contractDataArray Array of contract pair OI token data
16
+ * @returns Array of normalized pair OI token data
17
+ */
18
+ export declare const convertPairOiTokenArray: (contractDataArray: IPriceImpact.PairOiTokenStruct[]) => PairOiToken[];
19
+ /**
20
+ * @dev Converts contract pair OI collateral data to SDK format
21
+ * @param contractData Contract pair OI collateral struct
22
+ * @param collateralDecimals Number of decimals for the collateral (e.g., 18 for DAI, 6 for USDC)
23
+ * @returns Normalized pair OI collateral data
24
+ */
25
+ export declare const convertPairOiCollateral: (contractData: IPriceImpact.PairOiCollateralStruct, collateralDecimals: number) => PairOiCollateral;
26
+ /**
27
+ * @dev Converts array of contract pair OI collateral data to SDK format
28
+ * @param contractDataArray Array of contract pair OI collateral data
29
+ * @param collateralDecimals Array of collateral decimals for each entry
30
+ * @returns Array of normalized pair OI collateral data
31
+ */
32
+ export declare const convertPairOiCollateralArray: (contractDataArray: IPriceImpact.PairOiCollateralStruct[], collateralDecimals: number[]) => PairOiCollateral[];
33
+ /**
34
+ * @dev Normalizes skew depth from contract format
35
+ * @param depth Skew depth from contract (in collateral wei)
36
+ * @param collateralDecimals Number of decimals for the collateral
37
+ * @returns Normalized skew depth
38
+ */
39
+ export declare const normalizeSkewDepth: (depth: bigint | number | string, collateralDecimals: number) => number;
40
+ /**
41
+ * @dev Creates a skew depth object
42
+ * @param collateralIndex Collateral index
43
+ * @param pairIndex Pair index
44
+ * @param depth Normalized depth value
45
+ * @returns Skew depth object
46
+ */
47
+ export declare const createSkewDepth: (collateralIndex: number, pairIndex: number, depth: number) => SkewDepth;
48
+ /**
49
+ * @dev Creates skew price impact context from arrays of data
50
+ * @param collateralIndices Array of collateral indices
51
+ * @param pairIndices Array of pair indices
52
+ * @param skewDepths Array of normalized skew depths
53
+ * @param pairOiTokens Array of pair OI token data
54
+ * @returns Complete skew price impact context
55
+ */
56
+ export declare const createSkewPriceImpactContext: (collateralIndices: number[], pairIndices: number[], skewDepths: number[], pairOiTokens: PairOiToken[]) => SkewPriceImpactContext;
57
+ /**
58
+ * @dev Validates skew depth is within reasonable bounds
59
+ * @param depth Normalized skew depth
60
+ * @param minDepth Minimum allowed depth (default: 0)
61
+ * @param maxDepth Maximum allowed depth (default: 1e12)
62
+ * @returns Whether depth is valid
63
+ */
64
+ export declare const isValidSkewDepth: (depth: number, minDepth?: number, maxDepth?: number) => boolean;
65
+ /**
66
+ * @dev Converts contract skew depths array to normalized values
67
+ * @param contractDepths Array of depths from contract
68
+ * @param collateralDecimals Array of decimals for each collateral
69
+ * @returns Array of normalized depths
70
+ */
71
+ export declare const convertSkewDepthsArray: (contractDepths: Array<bigint | number | string>, collateralDecimals: number[]) => number[];
72
+ /**
73
+ * @dev Merges multiple contexts into one
74
+ * @param contexts Array of contexts to merge
75
+ * @returns Merged context
76
+ */
77
+ export declare const mergeSkewPriceImpactContexts: (contexts: SkewPriceImpactContext[]) => SkewPriceImpactContext;
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Converters for skew price impact data between contract and SDK formats
4
+ * @dev All BigNumber values are normalized to floats with appropriate precision
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.mergeSkewPriceImpactContexts = exports.convertSkewDepthsArray = exports.isValidSkewDepth = exports.createSkewPriceImpactContext = exports.createSkewDepth = exports.normalizeSkewDepth = exports.convertPairOiCollateralArray = exports.convertPairOiCollateral = exports.convertPairOiTokenArray = exports.convertPairOiToken = void 0;
8
+ /**
9
+ * @dev Converts contract pair OI token data to SDK format
10
+ * @param contractData Contract pair OI token struct
11
+ * @returns Normalized pair OI token data
12
+ */
13
+ const convertPairOiToken = (contractData) => {
14
+ // Token amounts are stored as 1e18 in contract
15
+ return {
16
+ oiLongToken: Number(contractData.oiLongToken) / 1e18,
17
+ oiShortToken: Number(contractData.oiShortToken) / 1e18,
18
+ };
19
+ };
20
+ exports.convertPairOiToken = convertPairOiToken;
21
+ /**
22
+ * @dev Converts array of contract pair OI token data to SDK format
23
+ * @param contractDataArray Array of contract pair OI token data
24
+ * @returns Array of normalized pair OI token data
25
+ */
26
+ const convertPairOiTokenArray = (contractDataArray) => {
27
+ return contractDataArray.map(exports.convertPairOiToken);
28
+ };
29
+ exports.convertPairOiTokenArray = convertPairOiTokenArray;
30
+ /**
31
+ * @dev Converts contract pair OI collateral data to SDK format
32
+ * @param contractData Contract pair OI collateral struct
33
+ * @param collateralDecimals Number of decimals for the collateral (e.g., 18 for DAI, 6 for USDC)
34
+ * @returns Normalized pair OI collateral data
35
+ */
36
+ const convertPairOiCollateral = (contractData, collateralDecimals) => {
37
+ const divisor = Math.pow(10, collateralDecimals);
38
+ return {
39
+ oiLongCollateral: Number(contractData.oiLongCollateral) / divisor,
40
+ oiShortCollateral: Number(contractData.oiShortCollateral) / divisor,
41
+ };
42
+ };
43
+ exports.convertPairOiCollateral = convertPairOiCollateral;
44
+ /**
45
+ * @dev Converts array of contract pair OI collateral data to SDK format
46
+ * @param contractDataArray Array of contract pair OI collateral data
47
+ * @param collateralDecimals Array of collateral decimals for each entry
48
+ * @returns Array of normalized pair OI collateral data
49
+ */
50
+ const convertPairOiCollateralArray = (contractDataArray, collateralDecimals) => {
51
+ if (contractDataArray.length !== collateralDecimals.length) {
52
+ throw new Error("Contract data array and collateral decimals array must have the same length");
53
+ }
54
+ return contractDataArray.map((data, index) => (0, exports.convertPairOiCollateral)(data, collateralDecimals[index]));
55
+ };
56
+ exports.convertPairOiCollateralArray = convertPairOiCollateralArray;
57
+ /**
58
+ * @dev Normalizes skew depth from contract format
59
+ * @param depth Skew depth from contract (in collateral wei)
60
+ * @param collateralDecimals Number of decimals for the collateral
61
+ * @returns Normalized skew depth
62
+ */
63
+ const normalizeSkewDepth = (depth, collateralDecimals) => {
64
+ const divisor = Math.pow(10, collateralDecimals);
65
+ return Number(depth) / divisor;
66
+ };
67
+ exports.normalizeSkewDepth = normalizeSkewDepth;
68
+ /**
69
+ * @dev Creates a skew depth object
70
+ * @param collateralIndex Collateral index
71
+ * @param pairIndex Pair index
72
+ * @param depth Normalized depth value
73
+ * @returns Skew depth object
74
+ */
75
+ const createSkewDepth = (collateralIndex, pairIndex, depth) => {
76
+ return {
77
+ collateralIndex,
78
+ pairIndex,
79
+ depth,
80
+ };
81
+ };
82
+ exports.createSkewDepth = createSkewDepth;
83
+ /**
84
+ * @dev Creates skew price impact context from arrays of data
85
+ * @param collateralIndices Array of collateral indices
86
+ * @param pairIndices Array of pair indices
87
+ * @param skewDepths Array of normalized skew depths
88
+ * @param pairOiTokens Array of pair OI token data
89
+ * @returns Complete skew price impact context
90
+ */
91
+ const createSkewPriceImpactContext = (collateralIndices, pairIndices, skewDepths, pairOiTokens) => {
92
+ if (collateralIndices.length !== pairIndices.length ||
93
+ pairIndices.length !== skewDepths.length ||
94
+ skewDepths.length !== pairOiTokens.length) {
95
+ throw new Error("All input arrays must have the same length");
96
+ }
97
+ const context = {
98
+ skewDepths: {},
99
+ pairOiTokens: {},
100
+ };
101
+ // Build nested objects indexed by collateralIndex and pairIndex
102
+ for (let i = 0; i < collateralIndices.length; i++) {
103
+ const collateralIndex = collateralIndices[i];
104
+ const pairIndex = pairIndices[i];
105
+ // Initialize collateral index objects if they don't exist
106
+ if (!context.skewDepths[collateralIndex]) {
107
+ context.skewDepths[collateralIndex] = {};
108
+ }
109
+ if (!context.pairOiTokens[collateralIndex]) {
110
+ context.pairOiTokens[collateralIndex] = {};
111
+ }
112
+ // Store data
113
+ context.skewDepths[collateralIndex][pairIndex] = skewDepths[i];
114
+ context.pairOiTokens[collateralIndex][pairIndex] = pairOiTokens[i];
115
+ }
116
+ return context;
117
+ };
118
+ exports.createSkewPriceImpactContext = createSkewPriceImpactContext;
119
+ /**
120
+ * @dev Validates skew depth is within reasonable bounds
121
+ * @param depth Normalized skew depth
122
+ * @param minDepth Minimum allowed depth (default: 0)
123
+ * @param maxDepth Maximum allowed depth (default: 1e12)
124
+ * @returns Whether depth is valid
125
+ */
126
+ const isValidSkewDepth = (depth, minDepth = 0, maxDepth = 1e12) => {
127
+ return depth >= minDepth && depth <= maxDepth;
128
+ };
129
+ exports.isValidSkewDepth = isValidSkewDepth;
130
+ /**
131
+ * @dev Converts contract skew depths array to normalized values
132
+ * @param contractDepths Array of depths from contract
133
+ * @param collateralDecimals Array of decimals for each collateral
134
+ * @returns Array of normalized depths
135
+ */
136
+ const convertSkewDepthsArray = (contractDepths, collateralDecimals) => {
137
+ if (contractDepths.length !== collateralDecimals.length) {
138
+ throw new Error("Contract depths array and collateral decimals array must have the same length");
139
+ }
140
+ return contractDepths.map((depth, index) => (0, exports.normalizeSkewDepth)(depth, collateralDecimals[index]));
141
+ };
142
+ exports.convertSkewDepthsArray = convertSkewDepthsArray;
143
+ /**
144
+ * @dev Merges multiple contexts into one
145
+ * @param contexts Array of contexts to merge
146
+ * @returns Merged context
147
+ */
148
+ const mergeSkewPriceImpactContexts = (contexts) => {
149
+ const merged = {
150
+ skewDepths: {},
151
+ pairOiTokens: {},
152
+ };
153
+ for (const context of contexts) {
154
+ // Merge skew depths
155
+ for (const collateralIndex in context.skewDepths) {
156
+ if (!merged.skewDepths[collateralIndex]) {
157
+ merged.skewDepths[collateralIndex] = {};
158
+ }
159
+ Object.assign(merged.skewDepths[collateralIndex], context.skewDepths[collateralIndex]);
160
+ }
161
+ // Merge pair OI tokens
162
+ for (const collateralIndex in context.pairOiTokens) {
163
+ if (!merged.pairOiTokens[collateralIndex]) {
164
+ merged.pairOiTokens[collateralIndex] = {};
165
+ }
166
+ Object.assign(merged.pairOiTokens[collateralIndex], context.pairOiTokens[collateralIndex]);
167
+ }
168
+ }
169
+ return merged;
170
+ };
171
+ exports.mergeSkewPriceImpactContexts = mergeSkewPriceImpactContexts;
@@ -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;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @dev Skew price impact calculations for v10+ trades
3
+ * @dev Based on formula: (existingSkew + tradeSize/2) / skewDepth
4
+ */
5
+ import { PairOiToken, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext, TradeSkewParams } from "./types";
6
+ /**
7
+ * @dev Calculates net skew in tokens (long - short)
8
+ * @param pairOi Pair OI data with long and short token amounts
9
+ * @returns Net skew in tokens (positive = long heavy, negative = short heavy)
10
+ */
11
+ export declare const getNetSkewToken: (pairOi: PairOiToken) => number;
12
+ /**
13
+ * @dev Calculates net skew in collateral tokens
14
+ * @param netSkewToken Net skew in tokens
15
+ * @param currentPrice Current pair price
16
+ * @returns Net skew in collateral tokens
17
+ */
18
+ export declare const getNetSkewCollateral: (netSkewToken: number, currentPrice: number) => number;
19
+ /**
20
+ * @dev Determines trade direction impact on skew
21
+ * @param long Is long position
22
+ * @param open Is opening (true) or closing (false)
23
+ * @returns Whether trade increases or decreases skew
24
+ */
25
+ export declare const getTradeSkewDirection: (long: boolean, open: boolean) => boolean;
26
+ /**
27
+ * @dev Core skew price impact calculation
28
+ * @param existingSkewToken Current net skew in tokens (signed)
29
+ * @param tradeSizeToken Trade size in tokens (always positive)
30
+ * @param skewDepth Skew depth in collateral tokens
31
+ * @param tradeIncreasesSkew Whether trade increases skew in its direction
32
+ * @returns Price impact percentage (can be positive or negative)
33
+ */
34
+ export declare const calculateSkewPriceImpactP: (existingSkewToken: number, tradeSizeToken: number, skewDepth: number, tradeIncreasesSkew: boolean) => number;
35
+ /**
36
+ * @dev Main function to calculate skew price impact for a trade
37
+ * @param context Skew price impact context with depths and OI data
38
+ * @param input Trade parameters
39
+ * @returns Skew price impact result
40
+ */
41
+ export declare const getTradeSkewPriceImpact: (context: SkewPriceImpactContext, input: SkewPriceImpactInput) => SkewPriceImpactResult;
42
+ /**
43
+ * @dev Calculate skew price impact for a trade with all parameters
44
+ * @param params Trade parameters including price and version checks
45
+ * @param context Skew price impact context
46
+ * @returns Price impact percentage or 0 if not applicable
47
+ */
48
+ export declare const getTradeSkewPriceImpactWithChecks: (params: TradeSkewParams, context: SkewPriceImpactContext) => number;
49
+ /**
50
+ * @dev Calculate position sizes for partial operations
51
+ * @param originalSizeCollateral Original position size in collateral
52
+ * @param deltaCollateral Position size delta in collateral
53
+ * @param originalSizeToken Original position size in tokens
54
+ * @returns Delta in tokens proportional to collateral delta
55
+ */
56
+ export declare const calculatePartialSizeToken: (originalSizeCollateral: number, deltaCollateral: number, originalSizeToken: number) => number;
57
+ export * as SkewPriceImpact from "./types";
58
+ export * from "./fetcher";