@the-situation/sdk 0.1.0-alpha.0

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 (221) hide show
  1. package/dist/__integration__/setup/accounts.d.ts +38 -0
  2. package/dist/__integration__/setup/accounts.d.ts.map +1 -0
  3. package/dist/__integration__/setup/accounts.js +50 -0
  4. package/dist/__integration__/setup/accounts.js.map +1 -0
  5. package/dist/__integration__/setup/artifacts.d.ts +51 -0
  6. package/dist/__integration__/setup/artifacts.d.ts.map +1 -0
  7. package/dist/__integration__/setup/artifacts.js +72 -0
  8. package/dist/__integration__/setup/artifacts.js.map +1 -0
  9. package/dist/__integration__/setup/deploy.d.ts +152 -0
  10. package/dist/__integration__/setup/deploy.d.ts.map +1 -0
  11. package/dist/__integration__/setup/deploy.js +344 -0
  12. package/dist/__integration__/setup/deploy.js.map +1 -0
  13. package/dist/__integration__/setup/devnet.d.ts +44 -0
  14. package/dist/__integration__/setup/devnet.d.ts.map +1 -0
  15. package/dist/__integration__/setup/devnet.js +97 -0
  16. package/dist/__integration__/setup/devnet.js.map +1 -0
  17. package/dist/__integration__/setup/erc20.d.ts +58 -0
  18. package/dist/__integration__/setup/erc20.d.ts.map +1 -0
  19. package/dist/__integration__/setup/erc20.js +194 -0
  20. package/dist/__integration__/setup/erc20.js.map +1 -0
  21. package/dist/__integration__/setup/fixtures.d.ts +125 -0
  22. package/dist/__integration__/setup/fixtures.d.ts.map +1 -0
  23. package/dist/__integration__/setup/fixtures.js +206 -0
  24. package/dist/__integration__/setup/fixtures.js.map +1 -0
  25. package/dist/__integration__/setup/index.d.ts +12 -0
  26. package/dist/__integration__/setup/index.d.ts.map +1 -0
  27. package/dist/__integration__/setup/index.js +12 -0
  28. package/dist/__integration__/setup/index.js.map +1 -0
  29. package/dist/__integration__/utils/assertions.d.ts +117 -0
  30. package/dist/__integration__/utils/assertions.d.ts.map +1 -0
  31. package/dist/__integration__/utils/assertions.js +191 -0
  32. package/dist/__integration__/utils/assertions.js.map +1 -0
  33. package/dist/__integration__/utils/balance-tracker.d.ts +85 -0
  34. package/dist/__integration__/utils/balance-tracker.d.ts.map +1 -0
  35. package/dist/__integration__/utils/balance-tracker.js +139 -0
  36. package/dist/__integration__/utils/balance-tracker.js.map +1 -0
  37. package/dist/__integration__/utils/calldata.d.ts +74 -0
  38. package/dist/__integration__/utils/calldata.d.ts.map +1 -0
  39. package/dist/__integration__/utils/calldata.js +119 -0
  40. package/dist/__integration__/utils/calldata.js.map +1 -0
  41. package/dist/__integration__/utils/index.d.ts +11 -0
  42. package/dist/__integration__/utils/index.d.ts.map +1 -0
  43. package/dist/__integration__/utils/index.js +11 -0
  44. package/dist/__integration__/utils/index.js.map +1 -0
  45. package/dist/__integration__/utils/trading.d.ts +169 -0
  46. package/dist/__integration__/utils/trading.d.ts.map +1 -0
  47. package/dist/__integration__/utils/trading.js +253 -0
  48. package/dist/__integration__/utils/trading.js.map +1 -0
  49. package/dist/__integration__/utils/wait.d.ts +52 -0
  50. package/dist/__integration__/utils/wait.d.ts.map +1 -0
  51. package/dist/__integration__/utils/wait.js +88 -0
  52. package/dist/__integration__/utils/wait.js.map +1 -0
  53. package/dist/abi/amm.d.ts +96 -0
  54. package/dist/abi/amm.d.ts.map +1 -0
  55. package/dist/abi/amm.js +70 -0
  56. package/dist/abi/amm.js.map +1 -0
  57. package/dist/abi/common.d.ts +329 -0
  58. package/dist/abi/common.d.ts.map +1 -0
  59. package/dist/abi/common.js +99 -0
  60. package/dist/abi/common.js.map +1 -0
  61. package/dist/abi/factory.d.ts +69 -0
  62. package/dist/abi/factory.d.ts.map +1 -0
  63. package/dist/abi/factory.js +29 -0
  64. package/dist/abi/factory.js.map +1 -0
  65. package/dist/abi/index.d.ts +14 -0
  66. package/dist/abi/index.d.ts.map +1 -0
  67. package/dist/abi/index.js +21 -0
  68. package/dist/abi/index.js.map +1 -0
  69. package/dist/abi/math-library.d.ts +113 -0
  70. package/dist/abi/math-library.d.ts.map +1 -0
  71. package/dist/abi/math-library.js +24 -0
  72. package/dist/abi/math-library.js.map +1 -0
  73. package/dist/abi/oracle.d.ts +63 -0
  74. package/dist/abi/oracle.d.ts.map +1 -0
  75. package/dist/abi/oracle.js +26 -0
  76. package/dist/abi/oracle.js.map +1 -0
  77. package/dist/collateral/compute.d.ts +316 -0
  78. package/dist/collateral/compute.d.ts.map +1 -0
  79. package/dist/collateral/compute.js +739 -0
  80. package/dist/collateral/compute.js.map +1 -0
  81. package/dist/collateral/index.d.ts +32 -0
  82. package/dist/collateral/index.d.ts.map +1 -0
  83. package/dist/collateral/index.js +35 -0
  84. package/dist/collateral/index.js.map +1 -0
  85. package/dist/collateral/verify.d.ts +97 -0
  86. package/dist/collateral/verify.d.ts.map +1 -0
  87. package/dist/collateral/verify.js +155 -0
  88. package/dist/collateral/verify.js.map +1 -0
  89. package/dist/contracts/amm-types.d.ts +311 -0
  90. package/dist/contracts/amm-types.d.ts.map +1 -0
  91. package/dist/contracts/amm-types.js +51 -0
  92. package/dist/contracts/amm-types.js.map +1 -0
  93. package/dist/contracts/amm.d.ts +417 -0
  94. package/dist/contracts/amm.d.ts.map +1 -0
  95. package/dist/contracts/amm.js +799 -0
  96. package/dist/contracts/amm.js.map +1 -0
  97. package/dist/contracts/base.d.ts +118 -0
  98. package/dist/contracts/base.d.ts.map +1 -0
  99. package/dist/contracts/base.js +143 -0
  100. package/dist/contracts/base.js.map +1 -0
  101. package/dist/contracts/factory.d.ts +120 -0
  102. package/dist/contracts/factory.d.ts.map +1 -0
  103. package/dist/contracts/factory.js +121 -0
  104. package/dist/contracts/factory.js.map +1 -0
  105. package/dist/contracts/index.d.ts +11 -0
  106. package/dist/contracts/index.d.ts.map +1 -0
  107. package/dist/contracts/index.js +11 -0
  108. package/dist/contracts/index.js.map +1 -0
  109. package/dist/contracts/math-library.d.ts +123 -0
  110. package/dist/contracts/math-library.d.ts.map +1 -0
  111. package/dist/contracts/math-library.js +233 -0
  112. package/dist/contracts/math-library.js.map +1 -0
  113. package/dist/contracts/oracle.d.ts +137 -0
  114. package/dist/contracts/oracle.d.ts.map +1 -0
  115. package/dist/contracts/oracle.js +220 -0
  116. package/dist/contracts/oracle.js.map +1 -0
  117. package/dist/index.d.ts +15 -0
  118. package/dist/index.d.ts.map +1 -0
  119. package/dist/index.js +22 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/providers/account.d.ts +46 -0
  122. package/dist/providers/account.d.ts.map +1 -0
  123. package/dist/providers/account.js +57 -0
  124. package/dist/providers/account.js.map +1 -0
  125. package/dist/providers/base.d.ts +75 -0
  126. package/dist/providers/base.d.ts.map +1 -0
  127. package/dist/providers/base.js +92 -0
  128. package/dist/providers/base.js.map +1 -0
  129. package/dist/providers/index.d.ts +8 -0
  130. package/dist/providers/index.d.ts.map +1 -0
  131. package/dist/providers/index.js +8 -0
  132. package/dist/providers/index.js.map +1 -0
  133. package/dist/sdk/client.d.ts +176 -0
  134. package/dist/sdk/client.d.ts.map +1 -0
  135. package/dist/sdk/client.js +273 -0
  136. package/dist/sdk/client.js.map +1 -0
  137. package/dist/sdk/index.d.ts +14 -0
  138. package/dist/sdk/index.d.ts.map +1 -0
  139. package/dist/sdk/index.js +15 -0
  140. package/dist/sdk/index.js.map +1 -0
  141. package/dist/sdk/internal/conversion.d.ts +104 -0
  142. package/dist/sdk/internal/conversion.d.ts.map +1 -0
  143. package/dist/sdk/internal/conversion.js +201 -0
  144. package/dist/sdk/internal/conversion.js.map +1 -0
  145. package/dist/sdk/internal/position-utils.d.ts +76 -0
  146. package/dist/sdk/internal/position-utils.d.ts.map +1 -0
  147. package/dist/sdk/internal/position-utils.js +131 -0
  148. package/dist/sdk/internal/position-utils.js.map +1 -0
  149. package/dist/sdk/internal/trade-builder.d.ts +112 -0
  150. package/dist/sdk/internal/trade-builder.d.ts.map +1 -0
  151. package/dist/sdk/internal/trade-builder.js +112 -0
  152. package/dist/sdk/internal/trade-builder.js.map +1 -0
  153. package/dist/sdk/market-ops.d.ts +194 -0
  154. package/dist/sdk/market-ops.d.ts.map +1 -0
  155. package/dist/sdk/market-ops.js +250 -0
  156. package/dist/sdk/market-ops.js.map +1 -0
  157. package/dist/sdk/position-ops.d.ts +121 -0
  158. package/dist/sdk/position-ops.d.ts.map +1 -0
  159. package/dist/sdk/position-ops.js +163 -0
  160. package/dist/sdk/position-ops.js.map +1 -0
  161. package/dist/sdk/simple-market.d.ts +168 -0
  162. package/dist/sdk/simple-market.d.ts.map +1 -0
  163. package/dist/sdk/simple-market.js +468 -0
  164. package/dist/sdk/simple-market.js.map +1 -0
  165. package/dist/sdk/simple-position.d.ts +77 -0
  166. package/dist/sdk/simple-position.d.ts.map +1 -0
  167. package/dist/sdk/simple-position.js +125 -0
  168. package/dist/sdk/simple-position.js.map +1 -0
  169. package/dist/sdk/simple-types.d.ts +155 -0
  170. package/dist/sdk/simple-types.d.ts.map +1 -0
  171. package/dist/sdk/simple-types.js +9 -0
  172. package/dist/sdk/simple-types.js.map +1 -0
  173. package/dist/types/index.d.ts +10 -0
  174. package/dist/types/index.d.ts.map +1 -0
  175. package/dist/types/index.js +11 -0
  176. package/dist/types/index.js.map +1 -0
  177. package/dist/types/normal-distribution.d.ts +125 -0
  178. package/dist/types/normal-distribution.d.ts.map +1 -0
  179. package/dist/types/normal-distribution.js +310 -0
  180. package/dist/types/normal-distribution.js.map +1 -0
  181. package/dist/types/normal-sqrt-hints-raw.d.ts +18 -0
  182. package/dist/types/normal-sqrt-hints-raw.d.ts.map +1 -0
  183. package/dist/types/normal-sqrt-hints-raw.js +9 -0
  184. package/dist/types/normal-sqrt-hints-raw.js.map +1 -0
  185. package/dist/types/sq128-raw.d.ts +69 -0
  186. package/dist/types/sq128-raw.d.ts.map +1 -0
  187. package/dist/types/sq128-raw.js +84 -0
  188. package/dist/types/sq128-raw.js.map +1 -0
  189. package/dist/types/sq128.d.ts +191 -0
  190. package/dist/types/sq128.d.ts.map +1 -0
  191. package/dist/types/sq128.js +611 -0
  192. package/dist/types/sq128.js.map +1 -0
  193. package/dist/utils/abi-encode.d.ts +187 -0
  194. package/dist/utils/abi-encode.d.ts.map +1 -0
  195. package/dist/utils/abi-encode.js +278 -0
  196. package/dist/utils/abi-encode.js.map +1 -0
  197. package/dist/utils/cumulative.d.ts +77 -0
  198. package/dist/utils/cumulative.d.ts.map +1 -0
  199. package/dist/utils/cumulative.js +102 -0
  200. package/dist/utils/cumulative.js.map +1 -0
  201. package/dist/utils/decimal-scale.d.ts +63 -0
  202. package/dist/utils/decimal-scale.d.ts.map +1 -0
  203. package/dist/utils/decimal-scale.js +108 -0
  204. package/dist/utils/decimal-scale.js.map +1 -0
  205. package/dist/utils/hints.d.ts +78 -0
  206. package/dist/utils/hints.d.ts.map +1 -0
  207. package/dist/utils/hints.js +118 -0
  208. package/dist/utils/hints.js.map +1 -0
  209. package/dist/utils/index.d.ts +12 -0
  210. package/dist/utils/index.d.ts.map +1 -0
  211. package/dist/utils/index.js +12 -0
  212. package/dist/utils/index.js.map +1 -0
  213. package/dist/utils/math.d.ts +44 -0
  214. package/dist/utils/math.d.ts.map +1 -0
  215. package/dist/utils/math.js +85 -0
  216. package/dist/utils/math.js.map +1 -0
  217. package/dist/utils/normalize.d.ts +47 -0
  218. package/dist/utils/normalize.d.ts.map +1 -0
  219. package/dist/utils/normalize.js +69 -0
  220. package/dist/utils/normalize.js.map +1 -0
  221. package/package.json +59 -0
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Decimal scaling utilities for token amount conversions.
3
+ *
4
+ * Converts between token amounts (with varying decimal places) and
5
+ * SQ128x128 fixed-point values used internally by the protocol.
6
+ *
7
+ * ## Rounding Convention
8
+ *
9
+ * - **Deposits**: Use `toTokenAmountUp()` - rounds up, conservative for protocol
10
+ * - **Withdrawals**: Use `toTokenAmountDown()` - rounds down, conservative for protocol
11
+ *
12
+ * This ensures the protocol never gives away more than it should.
13
+ *
14
+ * @module
15
+ */
16
+ import { SQ128x128 } from '../types/sq128';
17
+ /** 10^18 - standard ERC20 decimal places */
18
+ export declare const DECIMALS_18: bigint;
19
+ /** 10^6 - USDC/USDT decimal places */
20
+ export declare const DECIMALS_6: bigint;
21
+ /**
22
+ * Converts a token amount to SQ128x128.
23
+ *
24
+ * @param amount - Raw token amount (e.g., 1e18 for 1 token with 18 decimals)
25
+ * @param decimals - Token decimal places (e.g., 18 for ETH, 6 for USDC)
26
+ * @returns SQ128x128 value, or null on overflow
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * // 1.5 USDC (6 decimals) = 1_500_000 raw
31
+ * const sq = fromTokenAmount(1_500_000n, 6);
32
+ * // sq.toNumber() ≈ 1.5
33
+ * ```
34
+ */
35
+ export declare function fromTokenAmount(amount: bigint, decimals: number): SQ128x128 | null;
36
+ /**
37
+ * Converts SQ128x128 to token amount, rounding down.
38
+ *
39
+ * Use for withdrawals - conservative for protocol (pays less).
40
+ *
41
+ * @param value - The SQ128x128 value
42
+ * @param decimals - Target token decimal places
43
+ * @returns Raw token amount (rounded down)
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const sq = SQ128x128.fromNumber(1.5);
48
+ * const amount = toTokenAmountDown(sq, 6);
49
+ * // amount = 1_500_000n (1.5 USDC)
50
+ * ```
51
+ */
52
+ export declare function toTokenAmountDown(value: SQ128x128, decimals: number): bigint;
53
+ /**
54
+ * Converts SQ128x128 to token amount, rounding up.
55
+ *
56
+ * Use for deposits - conservative for protocol (requires more).
57
+ *
58
+ * @param value - The SQ128x128 value
59
+ * @param decimals - Target token decimal places
60
+ * @returns Raw token amount (rounded up)
61
+ */
62
+ export declare function toTokenAmountUp(value: SQ128x128, decimals: number): bigint;
63
+ //# sourceMappingURL=decimal-scale.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decimal-scale.d.ts","sourceRoot":"","sources":["../../src/utils/decimal-scale.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,4CAA4C;AAC5C,eAAO,MAAM,WAAW,EAAE,MAAmB,CAAC;AAE9C,sCAAsC;AACtC,eAAO,MAAM,UAAU,EAAE,MAAkB,CAAC;AAY5C;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAqBlF;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAY5E;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAU1E"}
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Decimal scaling utilities for token amount conversions.
3
+ *
4
+ * Converts between token amounts (with varying decimal places) and
5
+ * SQ128x128 fixed-point values used internally by the protocol.
6
+ *
7
+ * ## Rounding Convention
8
+ *
9
+ * - **Deposits**: Use `toTokenAmountUp()` - rounds up, conservative for protocol
10
+ * - **Withdrawals**: Use `toTokenAmountDown()` - rounds down, conservative for protocol
11
+ *
12
+ * This ensures the protocol never gives away more than it should.
13
+ *
14
+ * @module
15
+ */
16
+ import { SQ128x128 } from '../types/sq128';
17
+ /** 10^18 - standard ERC20 decimal places */
18
+ export const DECIMALS_18 = 10n ** 18n;
19
+ /** 10^6 - USDC/USDT decimal places */
20
+ export const DECIMALS_6 = 10n ** 6n;
21
+ /** 2^128 - the SQ128x128 scaling factor */
22
+ const SQ_SCALE = 1n << 128n;
23
+ /**
24
+ * Computes 10^decimals as a bigint.
25
+ */
26
+ function decimalScale(decimals) {
27
+ return 10n ** BigInt(decimals);
28
+ }
29
+ /**
30
+ * Converts a token amount to SQ128x128.
31
+ *
32
+ * @param amount - Raw token amount (e.g., 1e18 for 1 token with 18 decimals)
33
+ * @param decimals - Token decimal places (e.g., 18 for ETH, 6 for USDC)
34
+ * @returns SQ128x128 value, or null on overflow
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * // 1.5 USDC (6 decimals) = 1_500_000 raw
39
+ * const sq = fromTokenAmount(1_500_000n, 6);
40
+ * // sq.toNumber() ≈ 1.5
41
+ * ```
42
+ */
43
+ export function fromTokenAmount(amount, decimals) {
44
+ const scale = decimalScale(decimals);
45
+ // Convert: amount / 10^decimals -> SQ128x128
46
+ // SQ128x128 internal: value * 2^128
47
+ // So: (amount * 2^128) / 10^decimals
48
+ const scaledValue = (amount * SQ_SCALE) / scale;
49
+ // Check if within SQ128x128 range (256 bits)
50
+ const maxMag = (1n << 256n) - 1n;
51
+ if (scaledValue > maxMag) {
52
+ return null;
53
+ }
54
+ // Construct the SQ128x128 from the scaled value
55
+ return SQ128x128.fromRaw({
56
+ limb0: scaledValue & ((1n << 64n) - 1n),
57
+ limb1: (scaledValue >> 64n) & ((1n << 64n) - 1n),
58
+ limb2: (scaledValue >> 128n) & ((1n << 64n) - 1n),
59
+ limb3: scaledValue >> 192n,
60
+ neg: false,
61
+ });
62
+ }
63
+ /**
64
+ * Converts SQ128x128 to token amount, rounding down.
65
+ *
66
+ * Use for withdrawals - conservative for protocol (pays less).
67
+ *
68
+ * @param value - The SQ128x128 value
69
+ * @param decimals - Target token decimal places
70
+ * @returns Raw token amount (rounded down)
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const sq = SQ128x128.fromNumber(1.5);
75
+ * const amount = toTokenAmountDown(sq, 6);
76
+ * // amount = 1_500_000n (1.5 USDC)
77
+ * ```
78
+ */
79
+ export function toTokenAmountDown(value, decimals) {
80
+ const raw = value.toRaw();
81
+ const magnitude = raw.limb0 + (raw.limb1 << 64n) + (raw.limb2 << 128n) + (raw.limb3 << 192n);
82
+ const scale = decimalScale(decimals);
83
+ // Convert: SQ128x128 -> amount * 10^decimals
84
+ // value = magnitude / 2^128
85
+ // amount = value * 10^decimals = (magnitude * 10^decimals) / 2^128
86
+ // Round down: integer division naturally rounds down for positive numbers
87
+ const result = (magnitude * scale) / SQ_SCALE;
88
+ return raw.neg ? -result : result;
89
+ }
90
+ /**
91
+ * Converts SQ128x128 to token amount, rounding up.
92
+ *
93
+ * Use for deposits - conservative for protocol (requires more).
94
+ *
95
+ * @param value - The SQ128x128 value
96
+ * @param decimals - Target token decimal places
97
+ * @returns Raw token amount (rounded up)
98
+ */
99
+ export function toTokenAmountUp(value, decimals) {
100
+ const raw = value.toRaw();
101
+ const magnitude = raw.limb0 + (raw.limb1 << 64n) + (raw.limb2 << 128n) + (raw.limb3 << 192n);
102
+ const scale = decimalScale(decimals);
103
+ // Round up: (a + b - 1) / b
104
+ const numerator = magnitude * scale;
105
+ const result = numerator === 0n ? 0n : (numerator + SQ_SCALE - 1n) / SQ_SCALE;
106
+ return raw.neg ? -result : result;
107
+ }
108
+ //# sourceMappingURL=decimal-scale.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decimal-scale.js","sourceRoot":"","sources":["../../src/utils/decimal-scale.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Hint computation for NormalSqrtHints.
3
+ *
4
+ * Contract validates: l2_norm_denom² == 2 * sigma * sqrt(pi)
5
+ * For variance-change trades, must use Newton-Raphson sqrt for exact match.
6
+ *
7
+ * @module
8
+ */
9
+ import { SQ128x128 } from '../types/sq128';
10
+ import type { NormalSqrtHintsRaw } from '../types/normal-sqrt-hints-raw';
11
+ /**
12
+ * Computes the l2_norm_denom hint as a float.
13
+ *
14
+ * Formula: sqrt(2 * sigma * sqrt(pi))
15
+ *
16
+ * Use this for mean-only trades where float precision is sufficient.
17
+ *
18
+ * @param sigma - The standard deviation (as number)
19
+ * @returns The l2_norm_denom hint
20
+ */
21
+ export declare function computeL2NormDenomHintNumber(sigma: number): number;
22
+ /**
23
+ * Computes the backing_denom hint as a float.
24
+ *
25
+ * Formula: sqrt(sigma * sqrt(pi))
26
+ *
27
+ * Use this for mean-only trades where float precision is sufficient.
28
+ *
29
+ * @param sigma - The standard deviation (as number)
30
+ * @returns The backing_denom hint
31
+ */
32
+ export declare function computeBackingDenomHintNumber(sigma: number): number;
33
+ /**
34
+ * Computes the l2_norm_denom hint with full SQ128x128 precision.
35
+ *
36
+ * Formula: sqrt(2 * sigma * sqrt(pi))
37
+ *
38
+ * Required for variance-change trades where the contract validates
39
+ * l2_norm_denom² == 2 * sigma * sqrt(pi) in exact SQ128x128 arithmetic.
40
+ *
41
+ * @param sigma - The standard deviation as SQ128x128
42
+ * @returns The l2_norm_denom hint, or null if computation fails
43
+ */
44
+ export declare function computeL2NormDenomHint(sigma: SQ128x128): SQ128x128 | null;
45
+ /**
46
+ * Computes the backing_denom hint with full SQ128x128 precision.
47
+ *
48
+ * Formula: sqrt(sigma * sqrt(pi))
49
+ *
50
+ * Required for variance-change trades where the contract validates
51
+ * backing_denom² == sigma * sqrt(pi) in exact SQ128x128 arithmetic.
52
+ *
53
+ * @param sigma - The standard deviation as SQ128x128
54
+ * @returns The backing_denom hint, or null if computation fails
55
+ */
56
+ export declare function computeBackingDenomHint(sigma: SQ128x128): SQ128x128 | null;
57
+ /**
58
+ * Computes hints with full SQ128x128 precision.
59
+ *
60
+ * Required for variance-change trades where the contract validates
61
+ * hint values in exact SQ128x128 arithmetic.
62
+ *
63
+ * @param sigma - The standard deviation as SQ128x128
64
+ * @returns The hints, or null if computation fails
65
+ */
66
+ export declare function computeHints(sigma: SQ128x128): NormalSqrtHintsRaw | null;
67
+ /**
68
+ * Computes hints from a numeric sigma value.
69
+ *
70
+ * Convenience wrapper that converts sigma to SQ128x128 and computes
71
+ * hints with full precision.
72
+ *
73
+ * @param sigma - The standard deviation (as number)
74
+ * @returns The hints
75
+ * @throws Error if sigma cannot be converted or hints cannot be computed
76
+ */
77
+ export declare function computeHintsFromNumber(sigma: number): NormalSqrtHintsRaw;
78
+ //# sourceMappingURL=hints.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hints.d.ts","sourceRoot":"","sources":["../../src/utils/hints.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAO,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAGzE;;;;;;;;;GASG;AACH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;;;;;;;GASG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,SAAS,GAAG,SAAS,GAAG,IAAI,CAYzE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,SAAS,GAAG,SAAS,GAAG,IAAI,CAO1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,kBAAkB,GAAG,IAAI,CAYxE;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB,CAYxE"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Hint computation for NormalSqrtHints.
3
+ *
4
+ * Contract validates: l2_norm_denom² == 2 * sigma * sqrt(pi)
5
+ * For variance-change trades, must use Newton-Raphson sqrt for exact match.
6
+ *
7
+ * @module
8
+ */
9
+ import { SQ128x128, TWO } from '../types/sq128';
10
+ import { SQRT_PI, SQRT_PI_SQ, sqrtSQ128x128 } from './math';
11
+ /**
12
+ * Computes the l2_norm_denom hint as a float.
13
+ *
14
+ * Formula: sqrt(2 * sigma * sqrt(pi))
15
+ *
16
+ * Use this for mean-only trades where float precision is sufficient.
17
+ *
18
+ * @param sigma - The standard deviation (as number)
19
+ * @returns The l2_norm_denom hint
20
+ */
21
+ export function computeL2NormDenomHintNumber(sigma) {
22
+ return Math.sqrt(2 * sigma * SQRT_PI);
23
+ }
24
+ /**
25
+ * Computes the backing_denom hint as a float.
26
+ *
27
+ * Formula: sqrt(sigma * sqrt(pi))
28
+ *
29
+ * Use this for mean-only trades where float precision is sufficient.
30
+ *
31
+ * @param sigma - The standard deviation (as number)
32
+ * @returns The backing_denom hint
33
+ */
34
+ export function computeBackingDenomHintNumber(sigma) {
35
+ return Math.sqrt(sigma * SQRT_PI);
36
+ }
37
+ /**
38
+ * Computes the l2_norm_denom hint with full SQ128x128 precision.
39
+ *
40
+ * Formula: sqrt(2 * sigma * sqrt(pi))
41
+ *
42
+ * Required for variance-change trades where the contract validates
43
+ * l2_norm_denom² == 2 * sigma * sqrt(pi) in exact SQ128x128 arithmetic.
44
+ *
45
+ * @param sigma - The standard deviation as SQ128x128
46
+ * @returns The l2_norm_denom hint, or null if computation fails
47
+ */
48
+ export function computeL2NormDenomHint(sigma) {
49
+ const twoSigma = sigma.mul(TWO);
50
+ if (!twoSigma) {
51
+ return null;
52
+ }
53
+ const twoSigmaSqrtPi = twoSigma.mul(SQRT_PI_SQ);
54
+ if (!twoSigmaSqrtPi) {
55
+ return null;
56
+ }
57
+ return sqrtSQ128x128(twoSigmaSqrtPi);
58
+ }
59
+ /**
60
+ * Computes the backing_denom hint with full SQ128x128 precision.
61
+ *
62
+ * Formula: sqrt(sigma * sqrt(pi))
63
+ *
64
+ * Required for variance-change trades where the contract validates
65
+ * backing_denom² == sigma * sqrt(pi) in exact SQ128x128 arithmetic.
66
+ *
67
+ * @param sigma - The standard deviation as SQ128x128
68
+ * @returns The backing_denom hint, or null if computation fails
69
+ */
70
+ export function computeBackingDenomHint(sigma) {
71
+ const sigmaSqrtPi = sigma.mul(SQRT_PI_SQ);
72
+ if (!sigmaSqrtPi) {
73
+ return null;
74
+ }
75
+ return sqrtSQ128x128(sigmaSqrtPi);
76
+ }
77
+ /**
78
+ * Computes hints with full SQ128x128 precision.
79
+ *
80
+ * Required for variance-change trades where the contract validates
81
+ * hint values in exact SQ128x128 arithmetic.
82
+ *
83
+ * @param sigma - The standard deviation as SQ128x128
84
+ * @returns The hints, or null if computation fails
85
+ */
86
+ export function computeHints(sigma) {
87
+ const l2NormDenom = computeL2NormDenomHint(sigma);
88
+ const backingDenom = computeBackingDenomHint(sigma);
89
+ if (!l2NormDenom || !backingDenom) {
90
+ return null;
91
+ }
92
+ return {
93
+ l2_norm_denom: l2NormDenom.toRaw(),
94
+ backing_denom: backingDenom.toRaw(),
95
+ };
96
+ }
97
+ /**
98
+ * Computes hints from a numeric sigma value.
99
+ *
100
+ * Convenience wrapper that converts sigma to SQ128x128 and computes
101
+ * hints with full precision.
102
+ *
103
+ * @param sigma - The standard deviation (as number)
104
+ * @returns The hints
105
+ * @throws Error if sigma cannot be converted or hints cannot be computed
106
+ */
107
+ export function computeHintsFromNumber(sigma) {
108
+ const sigmaSq = SQ128x128.fromNumber(sigma);
109
+ if (!sigmaSq) {
110
+ throw new Error(`Failed to convert sigma=${sigma} to SQ128x128`);
111
+ }
112
+ const hints = computeHints(sigmaSq);
113
+ if (!hints) {
114
+ throw new Error(`Failed to compute hints for sigma=${sigma}`);
115
+ }
116
+ return hints;
117
+ }
118
+ //# sourceMappingURL=hints.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hints.js","sourceRoot":"","sources":["../../src/utils/hints.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5D;;;;;;;;;GASG;AACH,MAAM,UAAU,4BAA4B,CAAC,KAAa;IACxD,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,OAAO,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,6BAA6B,CAAC,KAAa;IACzD,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,CAAC;AACpC,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAgB;IACrD,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,cAAc,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAChD,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,aAAa,CAAC,cAAc,CAAC,CAAC;AACvC,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAgB;IACtD,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC1C,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,aAAa,CAAC,WAAW,CAAC,CAAC;AACpC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAAC,KAAgB;IAC3C,MAAM,WAAW,GAAG,sBAAsB,CAAC,KAAK,CAAC,CAAC;IAClD,MAAM,YAAY,GAAG,uBAAuB,CAAC,KAAK,CAAC,CAAC;IAEpD,IAAI,CAAC,WAAW,IAAI,CAAC,YAAY,EAAE,CAAC;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,aAAa,EAAE,WAAW,CAAC,KAAK,EAAE;QAClC,aAAa,EAAE,YAAY,CAAC,KAAK,EAAE;KACpC,CAAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAa;IAClD,MAAM,OAAO,GAAG,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAC5C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,2BAA2B,KAAK,eAAe,CAAC,CAAC;IACnE,CAAC;IAED,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IACpC,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,qCAAqC,KAAK,EAAE,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Utility functions for The Situation SDK.
3
+ *
4
+ * @module utils
5
+ */
6
+ export { DECIMALS_6, DECIMALS_18, fromTokenAmount, toTokenAmountDown, toTokenAmountUp, } from './decimal-scale';
7
+ export { normalizeResponse, snakeToCamel } from './normalize';
8
+ export { SQRT_PI, SQRT_PI_SQ, sqrtSQ128x128 } from './math';
9
+ export { computeBackingDenomHint, computeBackingDenomHintNumber, computeHints, computeHintsFromNumber, computeL2NormDenomHint, computeL2NormDenomHintNumber, } from './hints';
10
+ export { type AbiNormalDistribution, type AbiNormalSqrtHints, type AbiSQ128x128, createTradeEncoder, decodeAbiType, decodeNormalDistributionRawType, decodeNormalSqrtHintsRawType, decodeSQ128x128RawType, encodeAbiType, encodeNormalDistributionRawType, encodeNormalSqrtHintsRawType, encodeSQ128x128RawType, toAbiDistribution, toAbiHints, toAbiSQ128x128, TradeCalldataEncoder, } from './abi-encode';
11
+ export { computeTwap, cumulative256ToNumber, cumulative256ToScaledBigInt, cumulativeDiff, formatCumulative256, } from './cumulative';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,UAAU,EACV,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,eAAe,GAChB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE9D,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5D,OAAO,EACL,uBAAuB,EACvB,6BAA6B,EAC7B,YAAY,EACZ,sBAAsB,EACtB,sBAAsB,EACtB,4BAA4B,GAC7B,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,EACvB,KAAK,YAAY,EACjB,kBAAkB,EAClB,aAAa,EACb,+BAA+B,EAC/B,4BAA4B,EAC5B,sBAAsB,EACtB,aAAa,EACb,+BAA+B,EAC/B,4BAA4B,EAC5B,sBAAsB,EACtB,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,WAAW,EACX,qBAAqB,EACrB,2BAA2B,EAC3B,cAAc,EACd,mBAAmB,GACpB,MAAM,cAAc,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Utility functions for The Situation SDK.
3
+ *
4
+ * @module utils
5
+ */
6
+ export { DECIMALS_6, DECIMALS_18, fromTokenAmount, toTokenAmountDown, toTokenAmountUp, } from './decimal-scale';
7
+ export { normalizeResponse, snakeToCamel } from './normalize';
8
+ export { SQRT_PI, SQRT_PI_SQ, sqrtSQ128x128 } from './math';
9
+ export { computeBackingDenomHint, computeBackingDenomHintNumber, computeHints, computeHintsFromNumber, computeL2NormDenomHint, computeL2NormDenomHintNumber, } from './hints';
10
+ export { createTradeEncoder, decodeAbiType, decodeNormalDistributionRawType, decodeNormalSqrtHintsRawType, decodeSQ128x128RawType, encodeAbiType, encodeNormalDistributionRawType, encodeNormalSqrtHintsRawType, encodeSQ128x128RawType, toAbiDistribution, toAbiHints, toAbiSQ128x128, TradeCalldataEncoder, } from './abi-encode';
11
+ export { computeTwap, cumulative256ToNumber, cumulative256ToScaledBigInt, cumulativeDiff, formatCumulative256, } from './cumulative';
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,UAAU,EACV,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,eAAe,GAChB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE9D,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5D,OAAO,EACL,uBAAuB,EACvB,6BAA6B,EAC7B,YAAY,EACZ,sBAAsB,EACtB,sBAAsB,EACtB,4BAA4B,GAC7B,MAAM,SAAS,CAAC;AAEjB,OAAO,EAIL,kBAAkB,EAClB,aAAa,EACb,+BAA+B,EAC/B,4BAA4B,EAC5B,sBAAsB,EACtB,aAAa,EACb,+BAA+B,EAC/B,4BAA4B,EAC5B,sBAAsB,EACtB,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,WAAW,EACX,qBAAqB,EACrB,2BAA2B,EAC3B,cAAc,EACd,mBAAmB,GACpB,MAAM,cAAc,CAAC"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Mathematical utilities for SQ128x128 fixed-point arithmetic.
3
+ *
4
+ * @module
5
+ */
6
+ import { SQ128x128 } from '../types/sq128';
7
+ /**
8
+ * sqrt(π) ≈ 1.7724538509055159
9
+ *
10
+ * Used for L2 norm and lambda computation.
11
+ */
12
+ export declare const SQRT_PI: number;
13
+ /**
14
+ * sqrt(π) as SQ128x128 constant.
15
+ *
16
+ * Computed with high precision: 1.7724538509055159...
17
+ */
18
+ export declare const SQRT_PI_SQ: SQ128x128;
19
+ /**
20
+ * Computes the square root of an SQ128x128 value using Newton-Raphson iteration.
21
+ *
22
+ * The Newton-Raphson formula for sqrt(n) is:
23
+ * x_{k+1} = (x_k + n/x_k) / 2
24
+ *
25
+ * This converges quadratically, so ~10 iterations gives full 256-bit precision.
26
+ *
27
+ * @param n - The value to take the square root of (must be non-negative)
28
+ * @param maxIter - Maximum iterations (default: 20 for full precision)
29
+ * @returns The square root, or null if input is negative or computation fails
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * // Compute sqrt(2)
34
+ * const two = SQ128x128.fromNumber(2)!;
35
+ * const sqrtTwo = sqrtSQ128x128(two);
36
+ * console.log(sqrtTwo?.toNumber()); // ~1.4142135623730951
37
+ *
38
+ * // Compute hints for variance trades
39
+ * const twoSigmaSqrtPi = sigma.mul(TWO)!.mul(SQRT_PI_SQ)!;
40
+ * const l2NormDenom = sqrtSQ128x128(twoSigmaSqrtPi);
41
+ * ```
42
+ */
43
+ export declare function sqrtSQ128x128(n: SQ128x128, maxIter?: number): SQ128x128 | null;
44
+ //# sourceMappingURL=math.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../../src/utils/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAa,MAAM,gBAAgB,CAAC;AAEtD;;;;GAIG;AACH,eAAO,MAAM,OAAO,QAAqB,CAAC;AAE1C;;;;GAIG;AACH,eAAO,MAAM,UAAU,WAAiC,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,SAAK,GAAG,SAAS,GAAG,IAAI,CAkD1E"}
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Mathematical utilities for SQ128x128 fixed-point arithmetic.
3
+ *
4
+ * @module
5
+ */
6
+ import { SQ128x128, TWO, ZERO } from '../types/sq128';
7
+ /**
8
+ * sqrt(π) ≈ 1.7724538509055159
9
+ *
10
+ * Used for L2 norm and lambda computation.
11
+ */
12
+ export const SQRT_PI = Math.sqrt(Math.PI);
13
+ /**
14
+ * sqrt(π) as SQ128x128 constant.
15
+ *
16
+ * Computed with high precision: 1.7724538509055159...
17
+ */
18
+ export const SQRT_PI_SQ = SQ128x128.fromNumber(SQRT_PI);
19
+ /**
20
+ * Computes the square root of an SQ128x128 value using Newton-Raphson iteration.
21
+ *
22
+ * The Newton-Raphson formula for sqrt(n) is:
23
+ * x_{k+1} = (x_k + n/x_k) / 2
24
+ *
25
+ * This converges quadratically, so ~10 iterations gives full 256-bit precision.
26
+ *
27
+ * @param n - The value to take the square root of (must be non-negative)
28
+ * @param maxIter - Maximum iterations (default: 20 for full precision)
29
+ * @returns The square root, or null if input is negative or computation fails
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * // Compute sqrt(2)
34
+ * const two = SQ128x128.fromNumber(2)!;
35
+ * const sqrtTwo = sqrtSQ128x128(two);
36
+ * console.log(sqrtTwo?.toNumber()); // ~1.4142135623730951
37
+ *
38
+ * // Compute hints for variance trades
39
+ * const twoSigmaSqrtPi = sigma.mul(TWO)!.mul(SQRT_PI_SQ)!;
40
+ * const l2NormDenom = sqrtSQ128x128(twoSigmaSqrtPi);
41
+ * ```
42
+ */
43
+ export function sqrtSQ128x128(n, maxIter = 20) {
44
+ const raw = n.toRaw();
45
+ // sqrt(0) = 0
46
+ if (raw.limb0 === 0n && raw.limb1 === 0n && raw.limb2 === 0n && raw.limb3 === 0n) {
47
+ return ZERO;
48
+ }
49
+ // Negative numbers don't have real square roots
50
+ if (raw.neg) {
51
+ return null;
52
+ }
53
+ // Initial guess: use JavaScript sqrt for a good starting point
54
+ const floatGuess = Math.sqrt(n.toNumber());
55
+ let x = SQ128x128.fromNumber(floatGuess);
56
+ if (!x) {
57
+ return null;
58
+ }
59
+ // Newton-Raphson iteration: x = (x + n/x) / 2
60
+ for (let i = 0; i < maxIter; i++) {
61
+ const nDivX = n.div(x);
62
+ if (!nDivX) {
63
+ return null;
64
+ }
65
+ const sum = x.add(nDivX);
66
+ if (!sum) {
67
+ return null;
68
+ }
69
+ const next = sum.div(TWO);
70
+ if (!next) {
71
+ return null;
72
+ }
73
+ // Check for convergence (when x stops changing significantly)
74
+ const diff = next.sub(x);
75
+ if (diff) {
76
+ const diffRaw = diff.toRaw();
77
+ if (diffRaw.limb0 === 0n && diffRaw.limb1 === 0n) {
78
+ return next;
79
+ }
80
+ }
81
+ x = next;
82
+ }
83
+ return x;
84
+ }
85
+ //# sourceMappingURL=math.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math.js","sourceRoot":"","sources":["../../src/utils/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtD;;;;GAIG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAE1C;;;;GAIG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAC,UAAU,CAAC,OAAO,CAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,aAAa,CAAC,CAAY,EAAE,OAAO,GAAG,EAAE;IACtD,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;IAEtB,cAAc;IACd,IAAI,GAAG,CAAC,KAAK,KAAK,EAAE,IAAI,GAAG,CAAC,KAAK,KAAK,EAAE,IAAI,GAAG,CAAC,KAAK,KAAK,EAAE,IAAI,GAAG,CAAC,KAAK,KAAK,EAAE,EAAE,CAAC;QACjF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,gDAAgD;IAChD,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC;IACd,CAAC;IAED,+DAA+D;IAC/D,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC3C,IAAI,CAAC,GAAG,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;IACzC,IAAI,CAAC,CAAC,EAAE,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;IAED,8CAA8C;IAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACzB,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC1B,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,OAAO,IAAI,CAAC;QACd,CAAC;QAED,8DAA8D;QAC9D,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACzB,IAAI,IAAI,EAAE,CAAC;YACT,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;YAC7B,IAAI,OAAO,CAAC,KAAK,KAAK,EAAE,IAAI,OAAO,CAAC,KAAK,KAAK,EAAE,EAAE,CAAC;gBACjD,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QAED,CAAC,GAAG,IAAI,CAAC;IACX,CAAC;IAED,OAAO,CAAC,CAAC;AACX,CAAC"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Response normalization utilities for The Situation SDK.
3
+ *
4
+ * Handles conversion of snake_case keys from Starknet contract responses
5
+ * to camelCase for TypeScript consumption.
6
+ *
7
+ * @module utils/normalize
8
+ */
9
+ /**
10
+ * Converts a snake_case string to camelCase.
11
+ *
12
+ * @param str - The snake_case string to convert
13
+ * @returns The camelCase string
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * snakeToCamel('is_initialized'); // 'isInitialized'
18
+ * snakeToCamel('total_collateral_locked'); // 'totalCollateralLocked'
19
+ * ```
20
+ */
21
+ export declare function snakeToCamel(str: string): string;
22
+ /**
23
+ * Recursively normalizes a contract response by converting all snake_case
24
+ * keys to camelCase.
25
+ *
26
+ * This function handles:
27
+ * - Objects with nested properties
28
+ * - Arrays of objects
29
+ * - Primitive values (passed through unchanged)
30
+ * - null/undefined (passed through unchanged)
31
+ * - BigInt values (passed through unchanged)
32
+ *
33
+ * @param raw - The raw response from a Starknet contract call
34
+ * @returns The normalized response with camelCase keys
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const raw = {
39
+ * is_initialized: true,
40
+ * settlement_value: { limb0: 0n, limb1: 0n }
41
+ * };
42
+ * const normalized = normalizeResponse(raw);
43
+ * // { isInitialized: true, settlementValue: { limb0: 0n, limb1: 0n } }
44
+ * ```
45
+ */
46
+ export declare function normalizeResponse<T>(raw: unknown): T;
47
+ //# sourceMappingURL=normalize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,GAAG,CAAC,CAuBpD"}