@gainsnetwork/sdk 1.0.6-rc3 → 1.1.0-rc1

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.js +10 -10
  2. package/lib/backend/tradingVariables/backend.types.d.ts +11 -4
  3. package/lib/backend/tradingVariables/converter.d.ts +7 -3
  4. package/lib/backend/tradingVariables/converter.js +70 -63
  5. package/lib/backend/tradingVariables/index.js +11 -7
  6. package/lib/backend/tradingVariables/types.d.ts +4 -2
  7. package/lib/contracts/addresses.js +1 -4
  8. package/lib/contracts/index.d.ts +1 -1
  9. package/lib/contracts/index.js +3 -3
  10. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  11. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  12. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  13. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  14. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  15. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  16. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
  17. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
  18. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +223 -40
  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/GNSPairInfosV6_1.d.ts +911 -0
  24. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
  25. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
  26. package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
  27. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  28. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  29. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  30. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  31. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  32. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  33. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
  34. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
  35. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  36. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  37. package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
  38. package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
  39. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
  40. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
  41. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
  42. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
  43. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
  44. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
  45. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
  46. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
  47. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +10 -2
  48. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +317 -36
  49. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  50. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  51. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  52. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  53. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
  54. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
  55. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
  56. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
  57. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  58. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  59. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
  60. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
  61. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  62. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  63. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  64. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  65. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  66. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  67. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  68. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
  69. package/lib/contracts/utils/borrowingFees.js +20 -9
  70. package/lib/contracts/utils/openTrades.js +20 -11
  71. package/lib/contracts/utils/pairs.d.ts +13 -2
  72. package/lib/contracts/utils/pairs.js +89 -21
  73. package/lib/index.d.ts +1 -0
  74. package/lib/index.js +1 -0
  75. package/lib/markets/forex.js +1 -1
  76. package/lib/markets/leverage/builder.js +2 -2
  77. package/lib/markets/oi/fetcher.d.ts +58 -0
  78. package/lib/markets/oi/fetcher.js +181 -0
  79. package/lib/markets/oi/validation.d.ts +80 -0
  80. package/lib/markets/oi/validation.js +172 -0
  81. package/lib/markets/price/index.d.ts +0 -1
  82. package/lib/markets/price/index.js +0 -1
  83. package/lib/markets/price/signedPrices.d.ts +7 -0
  84. package/lib/markets/price/signedPrices.js +55 -2
  85. package/lib/markets/price/types.d.ts +0 -27
  86. package/lib/pricing/depthBands/converter.d.ts +65 -0
  87. package/lib/pricing/depthBands/converter.js +155 -0
  88. package/lib/pricing/depthBands/decoder.d.ts +32 -0
  89. package/lib/pricing/depthBands/decoder.js +109 -0
  90. package/lib/pricing/depthBands/encoder.d.ts +19 -0
  91. package/lib/pricing/depthBands/encoder.js +105 -0
  92. package/lib/pricing/depthBands/index.d.ts +8 -0
  93. package/lib/pricing/depthBands/index.js +26 -0
  94. package/lib/pricing/depthBands/types.d.ts +49 -0
  95. package/lib/pricing/depthBands/types.js +10 -0
  96. package/lib/pricing/depthBands/validator.d.ts +22 -0
  97. package/lib/pricing/depthBands/validator.js +113 -0
  98. package/lib/pricing/depthBands.d.ts +39 -0
  99. package/lib/pricing/depthBands.js +92 -0
  100. package/lib/pricing/index.d.ts +4 -0
  101. package/lib/pricing/index.js +20 -0
  102. package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
  103. package/lib/trade/effectiveLeverage/builder.js +30 -0
  104. package/lib/trade/fees/borrowing/builder.js +3 -2
  105. package/lib/trade/fees/borrowing/converter.js +1 -5
  106. package/lib/trade/fees/borrowing/index.js +5 -5
  107. package/lib/trade/fees/borrowingV2/builder.js +4 -3
  108. package/lib/trade/fees/borrowingV2/converter.js +1 -1
  109. package/lib/trade/fees/borrowingV2/fetcher.d.ts +0 -1
  110. package/lib/trade/fees/borrowingV2/fetcher.js +32 -26
  111. package/lib/trade/fees/borrowingV2/index.js +3 -3
  112. package/lib/trade/fees/converter.js +22 -22
  113. package/lib/trade/fees/fundingFees/builder.js +7 -6
  114. package/lib/trade/fees/fundingFees/converter.js +1 -1
  115. package/lib/trade/fees/fundingFees/fetcher.js +25 -16
  116. package/lib/trade/fees/fundingFees/index.js +3 -2
  117. package/lib/trade/fees/holdingFees/index.d.ts +46 -0
  118. package/lib/trade/fees/holdingFees/index.js +105 -0
  119. package/lib/trade/fees/holdingFees/types.d.ts +23 -0
  120. package/lib/trade/fees/holdingFees/types.js +5 -0
  121. package/lib/trade/fees/tiers/index.js +2 -1
  122. package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
  123. package/lib/trade/fees/trading/holdingFees.js +66 -0
  124. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
  125. package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
  126. package/lib/trade/fees/trading/index.js +3 -5
  127. package/lib/trade/liquidation/builder.js +3 -6
  128. package/lib/trade/liquidation/index.js +6 -4
  129. package/lib/trade/oiWindows.js +2 -1
  130. package/lib/trade/pnl/builder.js +2 -1
  131. package/lib/trade/pnl/converter.js +1 -1
  132. package/lib/trade/pnl/index.js +7 -4
  133. package/lib/trade/priceImpact/close/builder.js +2 -1
  134. package/lib/trade/priceImpact/close/index.js +1 -4
  135. package/lib/trade/priceImpact/cumulVol/builder.js +11 -18
  136. package/lib/trade/priceImpact/cumulVol/converter.d.ts +63 -0
  137. package/lib/trade/priceImpact/cumulVol/converter.js +97 -1
  138. package/lib/trade/priceImpact/cumulVol/index.d.ts +3 -0
  139. package/lib/trade/priceImpact/cumulVol/index.js +123 -25
  140. package/lib/trade/priceImpact/cumulVol/types.d.ts +11 -0
  141. package/lib/trade/priceImpact/cumulVol/types.js +2 -0
  142. package/lib/trade/priceImpact/open/builder.js +2 -1
  143. package/lib/trade/priceImpact/open/index.js +1 -4
  144. package/lib/trade/priceImpact/skew/builder.js +3 -2
  145. package/lib/trade/priceImpact/skew/converter.js +1 -1
  146. package/lib/trade/priceImpact/skew/fetcher.js +33 -24
  147. package/package.json +2 -2
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Depth bands decoding functions
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.bandsToCumulativeDepth = exports.decodeDepthBandsMapping = exports.decodePairDepthBands = exports.decodeDepthBands = void 0;
7
+ const types_1 = require("./types");
8
+ /**
9
+ * Get a specific band value from encoded slots
10
+ * @param slot1 First slot containing totalDepthUsd and bands 0-13
11
+ * @param slot2 Second slot containing bands 14-29
12
+ * @param bandIndex Index of the band to retrieve (0-29)
13
+ * @returns Band value
14
+ */
15
+ function getBandValue(slot1, slot2, bandIndex) {
16
+ if (bandIndex < 0 || bandIndex >= types_1.DEPTH_BANDS_COUNT) {
17
+ throw new Error(`Invalid band index: must be 0-${types_1.DEPTH_BANDS_COUNT - 1}, got ${bandIndex}`);
18
+ }
19
+ if (bandIndex < types_1.DEPTH_BANDS_PER_SLOT1) {
20
+ // Get from slot1 (skip the first 32 bits which contain totalDepthUsd)
21
+ const shift = 32 + bandIndex * 16;
22
+ return Number((slot1 >> BigInt(shift)) & BigInt(0xffff));
23
+ }
24
+ else {
25
+ // Get from slot2
26
+ const shift = (bandIndex - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
27
+ return Number((slot2 >> BigInt(shift)) & BigInt(0xffff));
28
+ }
29
+ }
30
+ /**
31
+ * Get total depth USD from encoded slot1
32
+ * @param slot1 First slot containing totalDepthUsd as uint32
33
+ * @returns Total depth in USD (6 decimal precision)
34
+ */
35
+ function getTotalDepthUsd(slot1) {
36
+ return Number(slot1 & BigInt(0xffffffff));
37
+ }
38
+ /**
39
+ * Decode depth bands from encoded slots
40
+ * @param encoded Encoded depth bands
41
+ * @returns Decoded depth bands with totalDepthUsd and band percentages
42
+ */
43
+ function decodeDepthBands(encoded) {
44
+ const { slot1, slot2 } = encoded;
45
+ const totalDepthUsd = getTotalDepthUsd(slot1);
46
+ const bands = [];
47
+ for (let i = 0; i < types_1.DEPTH_BANDS_COUNT; i++) {
48
+ bands.push(getBandValue(slot1, slot2, i));
49
+ }
50
+ return { totalDepthUsd, bands };
51
+ }
52
+ exports.decodeDepthBands = decodeDepthBands;
53
+ /**
54
+ * Decode pair depth bands (both directions)
55
+ * @param pairBands Encoded pair depth bands
56
+ * @returns Decoded depth bands for above and below
57
+ */
58
+ function decodePairDepthBands(pairBands) {
59
+ const above = decodeDepthBands({
60
+ slot1: pairBands.aboveSlot1,
61
+ slot2: pairBands.aboveSlot2,
62
+ });
63
+ const below = decodeDepthBands({
64
+ slot1: pairBands.belowSlot1,
65
+ slot2: pairBands.belowSlot2,
66
+ });
67
+ return { above, below };
68
+ }
69
+ exports.decodePairDepthBands = decodePairDepthBands;
70
+ /**
71
+ * Decode depth bands mapping (global offsets)
72
+ * @param slot1 First slot containing bands 0-13
73
+ * @param slot2 Second slot containing bands 14-29
74
+ * @returns Decoded mapping with band offset values in ppm
75
+ */
76
+ function decodeDepthBandsMapping(slot1, slot2) {
77
+ const bands = [];
78
+ // Note: For mapping, there's no totalDepthUsd, all bits are used for bands
79
+ for (let i = 0; i < types_1.DEPTH_BANDS_COUNT; i++) {
80
+ if (i < types_1.DEPTH_BANDS_PER_SLOT1) {
81
+ // Get from slot1
82
+ const shift = i * 16;
83
+ bands.push(Number((slot1 >> BigInt(shift)) & BigInt(0xffff)));
84
+ }
85
+ else {
86
+ // Get from slot2
87
+ const shift = (i - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
88
+ bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
89
+ }
90
+ }
91
+ return { bands };
92
+ }
93
+ exports.decodeDepthBandsMapping = decodeDepthBandsMapping;
94
+ /**
95
+ * Convert decoded band percentages back to cumulative depth USD
96
+ * @param decoded Decoded depth bands
97
+ * @returns Array of cumulative depth values in USD
98
+ */
99
+ function bandsToCumulativeDepth(decoded) {
100
+ const { totalDepthUsd, bands } = decoded;
101
+ const cumulativeDepthUsd = [];
102
+ for (const bandBps of bands) {
103
+ // Convert from basis points to USD
104
+ const depthUsd = (bandBps * totalDepthUsd) / 10000;
105
+ cumulativeDepthUsd.push(depthUsd);
106
+ }
107
+ return cumulativeDepthUsd;
108
+ }
109
+ exports.bandsToCumulativeDepth = bandsToCumulativeDepth;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @dev Depth bands encoding functions
3
+ */
4
+ import { type DepthBandsInput, type EncodedDepthBands } from "./types";
5
+ /**
6
+ * Encode depth bands data into two uint256 slots
7
+ * @param input Depth bands input data
8
+ * @returns Encoded depth bands with two slots
9
+ */
10
+ export declare function encodeDepthBands(input: DepthBandsInput): EncodedDepthBands;
11
+ /**
12
+ * Encode depth bands mapping (global offsets for all pairs)
13
+ * @param bands Array of 30 band offset values in parts per million (ppm)
14
+ * @returns Encoded mapping with two slots
15
+ */
16
+ export declare function encodeDepthBandsMapping(bands: number[]): {
17
+ slot1: bigint;
18
+ slot2: bigint;
19
+ };
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Depth bands encoding functions
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.encodeDepthBandsMapping = exports.encodeDepthBands = void 0;
7
+ const types_1 = require("./types");
8
+ /**
9
+ * Encode depth bands data into two uint256 slots
10
+ * @param input Depth bands input data
11
+ * @returns Encoded depth bands with two slots
12
+ */
13
+ function encodeDepthBands(input) {
14
+ const { totalDepthUsd, cumulativeDepthUsd } = input;
15
+ // Validate input
16
+ if (cumulativeDepthUsd.length !== types_1.DEPTH_BANDS_COUNT) {
17
+ throw new Error(`Invalid cumulativeDepthUsd length: expected ${types_1.DEPTH_BANDS_COUNT}, got ${cumulativeDepthUsd.length}`);
18
+ }
19
+ // Validate totalDepthUsd range (must fit in uint32)
20
+ const maxUint32 = Math.pow(2, 32) - 1;
21
+ if (totalDepthUsd < 0 || totalDepthUsd > maxUint32) {
22
+ throw new Error(`Invalid totalDepthUsd: must be between 0 and ${maxUint32}, got ${totalDepthUsd}`);
23
+ }
24
+ // Convert cumulative depths to band percentages
25
+ const bandPercentagesBps = [];
26
+ let prevCumulativeDepth = 0;
27
+ for (let i = 0; i < types_1.DEPTH_BANDS_COUNT; i++) {
28
+ const currentCumulativeDepth = cumulativeDepthUsd[i];
29
+ // Validate cumulative depth is non-decreasing
30
+ if (currentCumulativeDepth < prevCumulativeDepth) {
31
+ throw new Error(`Invalid cumulative depth at band ${i}: ${currentCumulativeDepth} is less than previous ${prevCumulativeDepth}`);
32
+ }
33
+ // Calculate percentage of total depth (in basis points)
34
+ const percentageBps = totalDepthUsd > 0
35
+ ? Math.round((currentCumulativeDepth * types_1.HUNDRED_P_BPS) / totalDepthUsd)
36
+ : 0;
37
+ // Validate percentage doesn't exceed 100%
38
+ if (percentageBps > types_1.HUNDRED_P_BPS) {
39
+ throw new Error(`Band ${i} percentage exceeds 100%: ${percentageBps} bps`);
40
+ }
41
+ // Validate percentage fits in uint16
42
+ const maxUint16 = Math.pow(2, 16) - 1;
43
+ if (percentageBps > maxUint16) {
44
+ throw new Error(`Band ${i} percentage too large for uint16: ${percentageBps}`);
45
+ }
46
+ bandPercentagesBps.push(percentageBps);
47
+ prevCumulativeDepth = currentCumulativeDepth;
48
+ }
49
+ // Validate that final band reaches 100% (or close to it due to rounding)
50
+ const finalPercentage = bandPercentagesBps[types_1.DEPTH_BANDS_COUNT - 1];
51
+ if (totalDepthUsd > 0 && Math.abs(finalPercentage - types_1.HUNDRED_P_BPS) > 10) {
52
+ throw new Error(`Final band does not reach 100%: ${finalPercentage} bps`);
53
+ }
54
+ // Pack into slot1: totalDepthUsd (32 bits) + bands 0-13 (14 * 16 bits)
55
+ let slot1 = BigInt(totalDepthUsd);
56
+ for (let i = 0; i < types_1.DEPTH_BANDS_PER_SLOT1; i++) {
57
+ const shift = 32 + i * 16;
58
+ slot1 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
59
+ }
60
+ // Pack into slot2: bands 14-29 (16 * 16 bits)
61
+ let slot2 = BigInt(0);
62
+ for (let i = types_1.DEPTH_BANDS_PER_SLOT1; i < types_1.DEPTH_BANDS_COUNT; i++) {
63
+ const shift = (i - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
64
+ slot2 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
65
+ }
66
+ return { slot1, slot2 };
67
+ }
68
+ exports.encodeDepthBands = encodeDepthBands;
69
+ /**
70
+ * Encode depth bands mapping (global offsets for all pairs)
71
+ * @param bands Array of 30 band offset values in parts per million (ppm)
72
+ * @returns Encoded mapping with two slots
73
+ */
74
+ function encodeDepthBandsMapping(bands) {
75
+ // Validate input
76
+ if (bands.length !== types_1.DEPTH_BANDS_COUNT) {
77
+ throw new Error(`Invalid bands length: expected ${types_1.DEPTH_BANDS_COUNT}, got ${bands.length}`);
78
+ }
79
+ // Validate each band value and monotonic increase
80
+ let prevBand = 0;
81
+ const maxUint16 = Math.pow(2, 16) - 1;
82
+ for (let i = 0; i < bands.length; i++) {
83
+ const band = bands[i];
84
+ if (band < 0 || band > maxUint16) {
85
+ throw new Error(`Band ${i} value out of range: must be 0-${maxUint16}, got ${band}`);
86
+ }
87
+ if (band < prevBand) {
88
+ throw new Error(`Band ${i} value ${band} is less than previous band ${prevBand}`);
89
+ }
90
+ prevBand = band;
91
+ }
92
+ // Pack into slot1: bands 0-13 (14 * 16 bits)
93
+ let slot1 = BigInt(0);
94
+ for (let i = 0; i < types_1.DEPTH_BANDS_PER_SLOT1; i++) {
95
+ slot1 |= BigInt(bands[i]) << BigInt(i * 16);
96
+ }
97
+ // Pack into slot2: bands 14-29 (16 * 16 bits)
98
+ let slot2 = BigInt(0);
99
+ for (let i = types_1.DEPTH_BANDS_PER_SLOT1; i < types_1.DEPTH_BANDS_COUNT; i++) {
100
+ const shift = (i - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
101
+ slot2 |= BigInt(bands[i]) << BigInt(shift);
102
+ }
103
+ return { slot1, slot2 };
104
+ }
105
+ exports.encodeDepthBandsMapping = encodeDepthBandsMapping;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @dev Depth bands module exports
3
+ */
4
+ export * from "./types";
5
+ export * from "./encoder";
6
+ export * from "./decoder";
7
+ export * from "./validator";
8
+ export * from "./converter";
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Depth bands module exports
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ // Types
21
+ __exportStar(require("./types"), exports);
22
+ // Core functions
23
+ __exportStar(require("./encoder"), exports);
24
+ __exportStar(require("./decoder"), exports);
25
+ __exportStar(require("./validator"), exports);
26
+ __exportStar(require("./converter"), exports);
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @dev Depth bands types for price impact calculations
3
+ */
4
+ export declare const DEPTH_BANDS_COUNT = 30;
5
+ export declare const DEPTH_BANDS_PER_SLOT1 = 14;
6
+ export declare const HUNDRED_P_BPS = 10000;
7
+ /**
8
+ * @dev Encoded depth bands for a single direction
9
+ */
10
+ export interface EncodedDepthBands {
11
+ slot1: bigint;
12
+ slot2: bigint;
13
+ }
14
+ /**
15
+ * @dev Encoded depth bands for both directions
16
+ */
17
+ export interface PairDepthBands {
18
+ aboveSlot1: bigint;
19
+ aboveSlot2: bigint;
20
+ belowSlot1: bigint;
21
+ belowSlot2: bigint;
22
+ }
23
+ /**
24
+ * @dev Decoded depth bands data
25
+ */
26
+ export interface DecodedDepthBands {
27
+ totalDepthUsd: number;
28
+ bands: number[];
29
+ }
30
+ /**
31
+ * @dev Input for encoding depth bands
32
+ */
33
+ export interface DepthBandsInput {
34
+ totalDepthUsd: number;
35
+ cumulativeDepthUsd: number[];
36
+ }
37
+ /**
38
+ * @dev Depth bands mapping for all pairs
39
+ */
40
+ export interface DepthBandsMapping {
41
+ slot1: bigint;
42
+ slot2: bigint;
43
+ }
44
+ /**
45
+ * @dev Decoded depth bands mapping
46
+ */
47
+ export interface DecodedDepthBandsMapping {
48
+ bands: number[];
49
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Depth bands types for price impact calculations
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HUNDRED_P_BPS = exports.DEPTH_BANDS_PER_SLOT1 = exports.DEPTH_BANDS_COUNT = void 0;
7
+ // Constants matching smart contract
8
+ exports.DEPTH_BANDS_COUNT = 30;
9
+ exports.DEPTH_BANDS_PER_SLOT1 = 14;
10
+ exports.HUNDRED_P_BPS = 10000;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @dev Depth bands validation functions
3
+ */
4
+ import { type DecodedDepthBands, type DepthBandsInput } from "./types";
5
+ /**
6
+ * Validate that depth bands are properly formed
7
+ * @param bands Decoded depth bands to validate
8
+ * @throws Error if bands are invalid
9
+ */
10
+ export declare function validateDepthBands(bands: DecodedDepthBands): void;
11
+ /**
12
+ * Validate depth bands input before encoding
13
+ * @param input Depth bands input to validate
14
+ * @throws Error if input is invalid
15
+ */
16
+ export declare function validateDepthBandsInput(input: DepthBandsInput): void;
17
+ /**
18
+ * Validate depth bands mapping
19
+ * @param bands Array of band offset values in ppm
20
+ * @throws Error if mapping is invalid
21
+ */
22
+ export declare function validateDepthBandsMapping(bands: number[]): void;
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Depth bands validation functions
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.validateDepthBandsMapping = exports.validateDepthBandsInput = exports.validateDepthBands = void 0;
7
+ const types_1 = require("./types");
8
+ /**
9
+ * Validate that depth bands are properly formed
10
+ * @param bands Decoded depth bands to validate
11
+ * @throws Error if bands are invalid
12
+ */
13
+ function validateDepthBands(bands) {
14
+ const { totalDepthUsd, bands: bandValues } = bands;
15
+ // Check band count
16
+ if (bandValues.length !== types_1.DEPTH_BANDS_COUNT) {
17
+ throw new Error(`Invalid band count: expected ${types_1.DEPTH_BANDS_COUNT}, got ${bandValues.length}`);
18
+ }
19
+ // Check totalDepthUsd is non-negative
20
+ if (totalDepthUsd < 0) {
21
+ throw new Error(`Total depth USD cannot be negative: ${totalDepthUsd}`);
22
+ }
23
+ // Check bands are monotonically increasing
24
+ let prevBand = 0;
25
+ for (let i = 0; i < bandValues.length; i++) {
26
+ const band = bandValues[i];
27
+ if (band < 0) {
28
+ throw new Error(`Band ${i} cannot be negative: ${band}`);
29
+ }
30
+ if (band < prevBand) {
31
+ throw new Error(`Band ${i} (${band}) is less than previous band (${prevBand})`);
32
+ }
33
+ if (band > types_1.HUNDRED_P_BPS) {
34
+ throw new Error(`Band ${i} exceeds 100%: ${band} bps > ${types_1.HUNDRED_P_BPS} bps`);
35
+ }
36
+ prevBand = band;
37
+ }
38
+ // Check that last band reaches 100% (with tolerance for rounding)
39
+ const lastBand = bandValues[bandValues.length - 1];
40
+ if (totalDepthUsd > 0 && Math.abs(lastBand - types_1.HUNDRED_P_BPS) > 10) {
41
+ throw new Error(`Last band does not reach 100%: ${lastBand} bps (expected ~${types_1.HUNDRED_P_BPS} bps)`);
42
+ }
43
+ }
44
+ exports.validateDepthBands = validateDepthBands;
45
+ /**
46
+ * Validate depth bands input before encoding
47
+ * @param input Depth bands input to validate
48
+ * @throws Error if input is invalid
49
+ */
50
+ function validateDepthBandsInput(input) {
51
+ const { totalDepthUsd, cumulativeDepthUsd } = input;
52
+ // Check array length
53
+ if (cumulativeDepthUsd.length !== types_1.DEPTH_BANDS_COUNT) {
54
+ throw new Error(`Invalid cumulative depth array length: expected ${types_1.DEPTH_BANDS_COUNT}, got ${cumulativeDepthUsd.length}`);
55
+ }
56
+ // Check totalDepthUsd
57
+ if (totalDepthUsd < 0) {
58
+ throw new Error(`Total depth USD cannot be negative: ${totalDepthUsd}`);
59
+ }
60
+ const maxUint32 = Math.pow(2, 32) - 1;
61
+ if (totalDepthUsd > maxUint32) {
62
+ throw new Error(`Total depth USD exceeds uint32 max: ${totalDepthUsd} > ${maxUint32}`);
63
+ }
64
+ // Check cumulative values are monotonically increasing
65
+ let prevValue = 0;
66
+ for (let i = 0; i < cumulativeDepthUsd.length; i++) {
67
+ const value = cumulativeDepthUsd[i];
68
+ if (value < 0) {
69
+ throw new Error(`Cumulative depth at band ${i} cannot be negative: ${value}`);
70
+ }
71
+ if (value < prevValue) {
72
+ throw new Error(`Cumulative depth at band ${i} (${value}) is less than previous (${prevValue})`);
73
+ }
74
+ if (value > totalDepthUsd) {
75
+ throw new Error(`Cumulative depth at band ${i} (${value}) exceeds total depth (${totalDepthUsd})`);
76
+ }
77
+ prevValue = value;
78
+ }
79
+ // Check that last cumulative value equals total depth (with tolerance)
80
+ const lastValue = cumulativeDepthUsd[cumulativeDepthUsd.length - 1];
81
+ if (totalDepthUsd > 0 && Math.abs(lastValue - totalDepthUsd) > 1) {
82
+ throw new Error(`Last cumulative depth (${lastValue}) does not match total depth (${totalDepthUsd})`);
83
+ }
84
+ }
85
+ exports.validateDepthBandsInput = validateDepthBandsInput;
86
+ /**
87
+ * Validate depth bands mapping
88
+ * @param bands Array of band offset values in ppm
89
+ * @throws Error if mapping is invalid
90
+ */
91
+ function validateDepthBandsMapping(bands) {
92
+ // Check array length
93
+ if (bands.length !== types_1.DEPTH_BANDS_COUNT) {
94
+ throw new Error(`Invalid band count: expected ${types_1.DEPTH_BANDS_COUNT}, got ${bands.length}`);
95
+ }
96
+ // Check bands are monotonically increasing and within range
97
+ let prevBand = 0;
98
+ const maxUint16 = Math.pow(2, 16) - 1;
99
+ for (let i = 0; i < bands.length; i++) {
100
+ const band = bands[i];
101
+ if (band < 0) {
102
+ throw new Error(`Band ${i} cannot be negative: ${band}`);
103
+ }
104
+ if (band > maxUint16) {
105
+ throw new Error(`Band ${i} exceeds uint16 max: ${band} > ${maxUint16}`);
106
+ }
107
+ if (band < prevBand) {
108
+ throw new Error(`Band ${i} (${band} ppm) is less than previous band (${prevBand} ppm)`);
109
+ }
110
+ prevBand = band;
111
+ }
112
+ }
113
+ exports.validateDepthBandsMapping = validateDepthBandsMapping;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @dev Depth bands encoding/decoding functions
3
+ */
4
+ /**
5
+ * Encode depth bands data into two uint256 slots
6
+ * @param totalDepthUsd Total depth in USD (must fit in uint32)
7
+ * @param bandPercentagesBps Array of 30 band percentages in basis points
8
+ * @returns Two slots as bigints
9
+ */
10
+ export declare function encodeDepthBands(totalDepthUsd: number, bandPercentagesBps: number[]): {
11
+ slot1: bigint;
12
+ slot2: bigint;
13
+ };
14
+ /**
15
+ * Decode depth bands from two uint256 slots
16
+ * @param slot1 First slot containing totalDepthUsd and bands 0-13
17
+ * @param slot2 Second slot containing bands 14-29
18
+ * @returns Total depth and array of band percentages
19
+ */
20
+ export declare function decodeDepthBands(slot1: bigint, slot2: bigint): {
21
+ totalDepthUsd: number;
22
+ bands: number[];
23
+ };
24
+ /**
25
+ * Encode depth bands mapping (global offsets for all pairs)
26
+ * @param bands Array of 30 band offset values in ppm
27
+ * @returns Two slots as bigints
28
+ */
29
+ export declare function encodeDepthBandsMapping(bands: number[]): {
30
+ slot1: bigint;
31
+ slot2: bigint;
32
+ };
33
+ /**
34
+ * Decode depth bands mapping from two uint256 slots
35
+ * @param slot1 First slot containing bands 0-13
36
+ * @param slot2 Second slot containing bands 14-29
37
+ * @returns Array of band offset values in ppm
38
+ */
39
+ export declare function decodeDepthBandsMapping(slot1: bigint, slot2: bigint): number[];
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Depth bands encoding/decoding functions
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.decodeDepthBandsMapping = exports.encodeDepthBandsMapping = exports.decodeDepthBands = exports.encodeDepthBands = void 0;
7
+ const DEPTH_BANDS_COUNT = 30;
8
+ const DEPTH_BANDS_PER_SLOT1 = 14;
9
+ /**
10
+ * Encode depth bands data into two uint256 slots
11
+ * @param totalDepthUsd Total depth in USD (must fit in uint32)
12
+ * @param bandPercentagesBps Array of 30 band percentages in basis points
13
+ * @returns Two slots as bigints
14
+ */
15
+ function encodeDepthBands(totalDepthUsd, bandPercentagesBps) {
16
+ // Pack slot1: totalDepthUsd (32 bits) + bands 0-13 (14 * 16 bits)
17
+ let slot1 = BigInt(totalDepthUsd);
18
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
19
+ const shift = 32 + i * 16;
20
+ slot1 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
21
+ }
22
+ // Pack slot2: bands 14-29 (16 * 16 bits)
23
+ let slot2 = BigInt(0);
24
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
25
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
26
+ slot2 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
27
+ }
28
+ return { slot1, slot2 };
29
+ }
30
+ exports.encodeDepthBands = encodeDepthBands;
31
+ /**
32
+ * Decode depth bands from two uint256 slots
33
+ * @param slot1 First slot containing totalDepthUsd and bands 0-13
34
+ * @param slot2 Second slot containing bands 14-29
35
+ * @returns Total depth and array of band percentages
36
+ */
37
+ function decodeDepthBands(slot1, slot2) {
38
+ const totalDepthUsd = Number(slot1 & BigInt(0xffffffff));
39
+ const bands = [];
40
+ // Extract bands 0-13 from slot1
41
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
42
+ const shift = 32 + i * 16;
43
+ bands.push(Number((slot1 >> BigInt(shift)) & BigInt(0xffff)));
44
+ }
45
+ // Extract bands 14-29 from slot2
46
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
47
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
48
+ bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
49
+ }
50
+ return { totalDepthUsd, bands };
51
+ }
52
+ exports.decodeDepthBands = decodeDepthBands;
53
+ /**
54
+ * Encode depth bands mapping (global offsets for all pairs)
55
+ * @param bands Array of 30 band offset values in ppm
56
+ * @returns Two slots as bigints
57
+ */
58
+ function encodeDepthBandsMapping(bands) {
59
+ // Pack slot1: bands 0-13 (14 * 16 bits)
60
+ let slot1 = BigInt(0);
61
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
62
+ slot1 |= BigInt(bands[i]) << BigInt(i * 16);
63
+ }
64
+ // Pack slot2: bands 14-29 (16 * 16 bits)
65
+ let slot2 = BigInt(0);
66
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
67
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
68
+ slot2 |= BigInt(bands[i]) << BigInt(shift);
69
+ }
70
+ return { slot1, slot2 };
71
+ }
72
+ exports.encodeDepthBandsMapping = encodeDepthBandsMapping;
73
+ /**
74
+ * Decode depth bands mapping from two uint256 slots
75
+ * @param slot1 First slot containing bands 0-13
76
+ * @param slot2 Second slot containing bands 14-29
77
+ * @returns Array of band offset values in ppm
78
+ */
79
+ function decodeDepthBandsMapping(slot1, slot2) {
80
+ const bands = [];
81
+ // Extract bands 0-13 from slot1
82
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
83
+ bands.push(Number((slot1 >> BigInt(i * 16)) & BigInt(0xffff)));
84
+ }
85
+ // Extract bands 14-29 from slot2
86
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
87
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
88
+ bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
89
+ }
90
+ return bands;
91
+ }
92
+ exports.decodeDepthBandsMapping = decodeDepthBandsMapping;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @dev Pricing module exports
3
+ */
4
+ export * from "./depthBands";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Pricing module exports
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ __exportStar(require("./depthBands"), exports);
@@ -0,0 +1,23 @@
1
+ import { GlobalTradingVariablesType } from "../../backend/tradingVariables/types";
2
+ import { TradeInfo } from "..";
3
+ import { TradeEffectiveLeverageContext } from "./types";
4
+ /**
5
+ * @dev Builds a complete context for effective leverage calculations
6
+ * @dev Uses the closing price impact context builder as a sub-context
7
+ * @param globalTradingVariables The transformed global trading variables from backend
8
+ * @param collateralIndex The collateral index (1-based)
9
+ * @param pairIndex The pair index
10
+ * @param tradeInfo Trade information including createdBlock
11
+ * @param additionalParams Additional parameters for price impact calculations
12
+ * @returns Complete context ready for getTradeNewEffectiveLeverage
13
+ */
14
+ export declare const buildTradeEffectiveLeverageContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, tradeInfo: TradeInfo, additionalParams: {
15
+ currentBlock: number;
16
+ contractsVersion?: number;
17
+ isPnlPositive?: boolean;
18
+ userPriceImpact?: {
19
+ cumulVolPriceImpactMultiplier: number;
20
+ fixedSpreadP: number;
21
+ };
22
+ protectionCloseFactorWhitelist?: boolean;
23
+ }) => TradeEffectiveLeverageContext | undefined;