@the-situation/utils 0.5.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.
package/dist/hints.js ADDED
@@ -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 '@the-situation/core';
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/hints.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAErD,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,15 @@
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 { buildApproveCall } from './erc20';
8
+ export { toHexAddress } from './address';
9
+ export { type CallRpc, toRpcCall, toRpcCalls } from './call-format';
10
+ export { normalizeResponse, snakeToCamel } from './normalize';
11
+ export { SQRT_PI, SQRT_PI_SQ, sqrtSQ128x128 } from './math';
12
+ export { computeBackingDenomHint, computeBackingDenomHintNumber, computeHints, computeHintsFromNumber, computeL2NormDenomHint, computeL2NormDenomHintNumber, } from './hints';
13
+ export { type AbiNormalDistribution, type AbiNormalSqrtHints, type AbiSQ128x128, createTradeEncoder, decodeAbiType, decodeNormalDistributionRawType, decodeNormalSqrtHintsRawType, decodeSQ128x128RawType, encodeAbiType, encodeNormalDistributionRawType, encodeNormalSqrtHintsRawType, encodeSQ128x128RawType, toAbiDistribution, toAbiHints, toAbiSQ128x128, TradeCalldataEncoder, } from './abi-encode';
14
+ export { computeTwap, cumulative256ToNumber, cumulative256ToScaledBigInt, cumulativeDiff, formatCumulative256, } from './cumulative';
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/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,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,KAAK,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEpE,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"}
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
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 { buildApproveCall } from './erc20';
8
+ export { toHexAddress } from './address';
9
+ export { toRpcCall, toRpcCalls } from './call-format';
10
+ export { normalizeResponse, snakeToCamel } from './normalize';
11
+ export { SQRT_PI, SQRT_PI_SQ, sqrtSQ128x128 } from './math';
12
+ export { computeBackingDenomHint, computeBackingDenomHintNumber, computeHints, computeHintsFromNumber, computeL2NormDenomHint, computeL2NormDenomHintNumber, } from './hints';
13
+ export { createTradeEncoder, decodeAbiType, decodeNormalDistributionRawType, decodeNormalSqrtHintsRawType, decodeSQ128x128RawType, encodeAbiType, encodeNormalDistributionRawType, encodeNormalSqrtHintsRawType, encodeSQ128x128RawType, toAbiDistribution, toAbiHints, toAbiSQ128x128, TradeCalldataEncoder, } from './abi-encode';
14
+ export { computeTwap, cumulative256ToNumber, cumulative256ToScaledBigInt, cumulativeDiff, formatCumulative256, } from './cumulative';
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/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,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAgB,SAAS,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEpE,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"}
package/dist/math.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Mathematical utilities for SQ128x128 fixed-point arithmetic.
3
+ *
4
+ * @module
5
+ */
6
+ import { SQ128x128 } from '@the-situation/core';
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
+ * Uses the exact raw constant from Cairo contracts.
17
+ */
18
+ export declare const SQRT_PI_SQ: SQ128x128;
19
+ /**
20
+ * Computes floor(sqrt(n)) in SQ128x128 space with contract-equivalent semantics.
21
+ *
22
+ * @param n - The value to take the square root of (must be non-negative)
23
+ * @param maxIter - Deprecated legacy parameter (ignored)
24
+ * @returns The square root, or null if input is negative or computation fails
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // Compute sqrt(2)
29
+ * const two = SQ128x128.fromNumber(2)!;
30
+ * const sqrtTwo = sqrtSQ128x128(two);
31
+ * console.log(sqrtTwo?.toNumber()); // ~1.4142135623730951
32
+ *
33
+ * // Compute hints for variance trades
34
+ * const twoSigmaSqrtPi = sigma.mul(TWO)!.mul(SQRT_PI_SQ)!;
35
+ * const l2NormDenom = sqrtSQ128x128(twoSigmaSqrtPi);
36
+ * ```
37
+ */
38
+ export declare function sqrtSQ128x128(n: SQ128x128, maxIter?: number): SQ128x128 | null;
39
+ //# sourceMappingURL=math.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAQ,MAAM,qBAAqB,CAAC;AA2DtD;;;;GAIG;AACH,eAAO,MAAM,OAAO,EAAE,MAA2B,CAAC;AAElD;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,SAMrB,CAAC;AAEL;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,GAAE,MAAW,GAAG,SAAS,GAAG,IAAI,CAgBlF"}
package/dist/math.js ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Mathematical utilities for SQ128x128 fixed-point arithmetic.
3
+ *
4
+ * @module
5
+ */
6
+ import { SQ128x128, ZERO } from '@the-situation/core';
7
+ const LIMB_BITS = 64n;
8
+ const LIMB_MASK = (1n << LIMB_BITS) - 1n;
9
+ const SQRT_SCALE_SHIFT = 128n;
10
+ // Matches contracts/src/market/normal/constants.cairo::SQRT_PI_RAW.
11
+ const CONTRACT_SQRT_PI_RAW = {
12
+ limb0: 0xc3b0520d5db9383fn,
13
+ limb1: 0xc5bf891b4ef6aa79n,
14
+ limb2: 0x1n,
15
+ limb3: 0x0n,
16
+ neg: false,
17
+ };
18
+ function rawToMagnitude(raw) {
19
+ return (raw.limb0 +
20
+ (raw.limb1 << LIMB_BITS) +
21
+ (raw.limb2 << (LIMB_BITS * 2n)) +
22
+ (raw.limb3 << (LIMB_BITS * 3n)));
23
+ }
24
+ function magnitudeToRaw(magnitude) {
25
+ return {
26
+ limb0: magnitude & LIMB_MASK,
27
+ limb1: (magnitude >> LIMB_BITS) & LIMB_MASK,
28
+ limb2: (magnitude >> (LIMB_BITS * 2n)) & LIMB_MASK,
29
+ limb3: (magnitude >> (LIMB_BITS * 3n)) & LIMB_MASK,
30
+ neg: false,
31
+ };
32
+ }
33
+ /**
34
+ * Integer square root (floor): returns max r such that r^2 <= value.
35
+ */
36
+ function integerSqrtFloor(value) {
37
+ if (value < 0n) {
38
+ throw new Error('integerSqrtFloor requires non-negative input');
39
+ }
40
+ if (value < 2n) {
41
+ return value;
42
+ }
43
+ const bitLength = BigInt(value.toString(2).length);
44
+ let x = 1n << ((bitLength + 1n) >> 1n);
45
+ while (true) {
46
+ const next = (x + (value / x)) >> 1n;
47
+ if (next >= x) {
48
+ return x;
49
+ }
50
+ x = next;
51
+ }
52
+ }
53
+ /**
54
+ * sqrt(π) ≈ 1.7724538509055159
55
+ *
56
+ * Used for L2 norm and lambda computation.
57
+ */
58
+ export const SQRT_PI = Math.sqrt(Math.PI);
59
+ /**
60
+ * sqrt(π) as SQ128x128 constant.
61
+ *
62
+ * Uses the exact raw constant from Cairo contracts.
63
+ */
64
+ export const SQRT_PI_SQ = (() => {
65
+ const value = SQ128x128.fromRaw(CONTRACT_SQRT_PI_RAW);
66
+ if (!value) {
67
+ throw new Error('Invalid contract SQRT_PI raw constant');
68
+ }
69
+ return value;
70
+ })();
71
+ /**
72
+ * Computes floor(sqrt(n)) in SQ128x128 space with contract-equivalent semantics.
73
+ *
74
+ * @param n - The value to take the square root of (must be non-negative)
75
+ * @param maxIter - Deprecated legacy parameter (ignored)
76
+ * @returns The square root, or null if input is negative or computation fails
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * // Compute sqrt(2)
81
+ * const two = SQ128x128.fromNumber(2)!;
82
+ * const sqrtTwo = sqrtSQ128x128(two);
83
+ * console.log(sqrtTwo?.toNumber()); // ~1.4142135623730951
84
+ *
85
+ * // Compute hints for variance trades
86
+ * const twoSigmaSqrtPi = sigma.mul(TWO)!.mul(SQRT_PI_SQ)!;
87
+ * const l2NormDenom = sqrtSQ128x128(twoSigmaSqrtPi);
88
+ * ```
89
+ */
90
+ export function sqrtSQ128x128(n, maxIter = 20) {
91
+ void maxIter;
92
+ const raw = n.toRaw();
93
+ if (raw.neg) {
94
+ return null;
95
+ }
96
+ const magnitude = rawToMagnitude(raw);
97
+ if (magnitude === 0n) {
98
+ return ZERO;
99
+ }
100
+ // Contract uses integer sqrt over (raw << 128) and floor rounding.
101
+ const shifted = magnitude << SQRT_SCALE_SHIFT;
102
+ const rootMagnitude = integerSqrtFloor(shifted);
103
+ return SQ128x128.fromRaw(magnitudeToRaw(rootMagnitude));
104
+ }
105
+ //# sourceMappingURL=math.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math.js","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAGtD,MAAM,SAAS,GAAW,GAAG,CAAC;AAC9B,MAAM,SAAS,GAAW,CAAC,EAAE,IAAI,SAAS,CAAC,GAAG,EAAE,CAAC;AACjD,MAAM,gBAAgB,GAAW,IAAI,CAAC;AAEtC,oEAAoE;AACpE,MAAM,oBAAoB,GAAiB;IACzC,KAAK,EAAE,mBAAmB;IAC1B,KAAK,EAAE,mBAAmB;IAC1B,KAAK,EAAE,IAAI;IACX,KAAK,EAAE,IAAI;IACX,GAAG,EAAE,KAAK;CACX,CAAC;AAEF,SAAS,cAAc,CAAC,GAAiB;IACvC,OAAO,CACL,GAAG,CAAC,KAAK;QACT,CAAC,GAAG,CAAC,KAAK,IAAI,SAAS,CAAC;QACxB,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,SAAS,GAAG,EAAE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AAC;AAElD;;;;GAIG;AACH,MAAM,CAAC,MAAM,UAAU,GAAc,CAAC,GAAG,EAAE;IACzC,MAAM,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC;IACtD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC,EAAE,CAAC;AAEL;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,aAAa,CAAC,CAAY,EAAE,UAAkB,EAAE;IAC9D,KAAK,OAAO,CAAC;IACb,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;IACtB,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,SAAS,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;IACtC,IAAI,SAAS,KAAK,EAAE,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,mEAAmE;IACnE,MAAM,OAAO,GAAG,SAAS,IAAI,gBAAgB,CAAC;IAC9C,MAAM,aAAa,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAChD,OAAO,SAAS,CAAC,OAAO,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC,CAAC;AAC1D,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/normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA2CH;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,GAAG,CAAC,CA6BpD"}
@@ -0,0 +1,115 @@
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
+ * Checks if a value is a Cairo boolean enum and converts it to a JS boolean.
11
+ *
12
+ * Cairo's `core::bool` is an enum with variants `True` and `False`.
13
+ * starknet.js may return these as objects like `{ variant: 'True' }`,
14
+ * `{ 'True': {} }`, or `{ activeVariant: 'True' }`.
15
+ *
16
+ * @param value - The value to check
17
+ * @returns A JavaScript boolean if the value is a Cairo bool, undefined otherwise
18
+ */
19
+ function tryParseCairoBool(value) {
20
+ if (typeof value !== 'object' || value === null) {
21
+ return undefined;
22
+ }
23
+ const obj = value;
24
+ // Pattern 1: { variant: 'True' } or { variant: 'False' }
25
+ if ('variant' in obj) {
26
+ const variant = obj['variant'];
27
+ if (variant === 'True' || variant === true)
28
+ return true;
29
+ if (variant === 'False' || variant === false)
30
+ return false;
31
+ }
32
+ // Pattern 2: { activeVariant: 'True' }
33
+ if ('activeVariant' in obj) {
34
+ const variant = obj['activeVariant'];
35
+ if (variant === 'True' || variant === true)
36
+ return true;
37
+ if (variant === 'False' || variant === false)
38
+ return false;
39
+ }
40
+ // Pattern 3: { True: {} } or { False: {} }
41
+ const keys = Object.keys(obj);
42
+ if (keys.length === 1) {
43
+ if (keys[0] === 'True')
44
+ return true;
45
+ if (keys[0] === 'False')
46
+ return false;
47
+ }
48
+ return undefined;
49
+ }
50
+ /**
51
+ * Converts a snake_case string to camelCase.
52
+ *
53
+ * @param str - The snake_case string to convert
54
+ * @returns The camelCase string
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * snakeToCamel('is_initialized'); // 'isInitialized'
59
+ * snakeToCamel('total_collateral_locked'); // 'totalCollateralLocked'
60
+ * ```
61
+ */
62
+ export function snakeToCamel(str) {
63
+ return str.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
64
+ }
65
+ /**
66
+ * Recursively normalizes a contract response by converting all snake_case
67
+ * keys to camelCase.
68
+ *
69
+ * This function handles:
70
+ * - Objects with nested properties
71
+ * - Arrays of objects
72
+ * - Primitive values (passed through unchanged)
73
+ * - null/undefined (passed through unchanged)
74
+ * - BigInt values (passed through unchanged)
75
+ *
76
+ * @param raw - The raw response from a Starknet contract call
77
+ * @returns The normalized response with camelCase keys
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * const raw = {
82
+ * is_initialized: true,
83
+ * settlement_value: { limb0: 0n, limb1: 0n }
84
+ * };
85
+ * const normalized = normalizeResponse(raw);
86
+ * // { isInitialized: true, settlementValue: { limb0: 0n, limb1: 0n } }
87
+ * ```
88
+ */
89
+ export function normalizeResponse(raw) {
90
+ // Pass through null/undefined
91
+ if (raw === null || raw === undefined) {
92
+ return raw;
93
+ }
94
+ // Pass through primitives (string, number, boolean, bigint, symbol)
95
+ if (typeof raw !== 'object') {
96
+ return raw;
97
+ }
98
+ // Handle arrays
99
+ if (Array.isArray(raw)) {
100
+ return raw.map((item) => normalizeResponse(item));
101
+ }
102
+ // Check if this object is a Cairo boolean enum
103
+ const boolValue = tryParseCairoBool(raw);
104
+ if (boolValue !== undefined) {
105
+ return boolValue;
106
+ }
107
+ // Handle objects
108
+ const result = {};
109
+ for (const [key, value] of Object.entries(raw)) {
110
+ const camelKey = snakeToCamel(key);
111
+ result[camelKey] = normalizeResponse(value);
112
+ }
113
+ return result;
114
+ }
115
+ //# sourceMappingURL=normalize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalize.js","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;;GASG;AACH,SAAS,iBAAiB,CAAC,KAAc;IACvC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,GAAG,GAAG,KAAgC,CAAC;IAE7C,yDAAyD;IACzD,IAAI,SAAS,IAAI,GAAG,EAAE,CAAC;QACrB,MAAM,OAAO,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC;QAC/B,IAAI,OAAO,KAAK,MAAM,IAAI,OAAO,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC;QACxD,IAAI,OAAO,KAAK,OAAO,IAAI,OAAO,KAAK,KAAK;YAAE,OAAO,KAAK,CAAC;IAC7D,CAAC;IAED,uCAAuC;IACvC,IAAI,eAAe,IAAI,GAAG,EAAE,CAAC;QAC3B,MAAM,OAAO,GAAG,GAAG,CAAC,e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AG,iBAAiB,CAAC,GAAG,CAAC,CAAC;IACzC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,OAAO,SAAc,CAAC;IACxB,CAAC;IAED,iBAAiB;IACjB,MAAM,MAAM,GAA4B,EAAE,CAAC;IAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,QAAQ,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;QACnC,MAAM,CAAC,QAAQ,CAAC,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IACD,OAAO,MAAW,CAAC;AACrB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@the-situation/utils",
3
+ "version": "0.5.0-alpha.0",
4
+ "description": "Utility functions for The Situation SDK",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": ["dist"],
15
+ "scripts": {
16
+ "build": "tsc -b",
17
+ "typecheck": "tsc -b",
18
+ "test": "bun test src/ 2>/dev/null || true",
19
+ "clean": "rm -rf dist tsconfig.tsbuildinfo"
20
+ },
21
+ "dependencies": {
22
+ "@the-situation/core": "0.5.0-alpha.0",
23
+ "@the-situation/abi": "0.5.0-alpha.0",
24
+ "starknet": "^9.2.1"
25
+ },
26
+ "devDependencies": {
27
+ "typescript": "^5.4.0",
28
+ "@types/bun": "^1.1.0"
29
+ },
30
+ "license": "MIT"
31
+ }