@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.
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Call format conversion utilities for wallet connector compatibility.
3
+ *
4
+ * starknet.js uses camelCase (`contractAddress`, `entrypoint`) but the
5
+ * Starknet JSON-RPC spec uses snake_case (`contract_address`, `entry_point`).
6
+ * Some wallet connectors (e.g., starknetkit) validate against the RPC format.
7
+ *
8
+ * @module
9
+ */
10
+ import { num } from 'starknet';
11
+ import { toHexAddress } from './address';
12
+ /**
13
+ * Converts calldata values to hex strings.
14
+ */
15
+ function normalizeCalldata(calldata) {
16
+ if (!calldata)
17
+ return [];
18
+ if (!Array.isArray(calldata))
19
+ return [];
20
+ return calldata.map((v) => {
21
+ if (typeof v === 'string') {
22
+ return v.startsWith('0x') ? v : num.toHex(v);
23
+ }
24
+ if (typeof v === 'bigint' || typeof v === 'number') {
25
+ return num.toHex(v);
26
+ }
27
+ return String(v);
28
+ });
29
+ }
30
+ /**
31
+ * Converts a starknet.js Call to snake_case RPC format.
32
+ *
33
+ * Also normalizes all addresses and calldata values to hex format
34
+ * for maximum wallet connector compatibility.
35
+ *
36
+ * Use this when your wallet connector (e.g., starknetkit) expects
37
+ * snake_case field names instead of starknet.js camelCase format.
38
+ *
39
+ * @param call - starknet.js Call object (camelCase)
40
+ * @returns Call in RPC format (snake_case) with hex-formatted values
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * import { toRpcCall } from '@the-situation/sdk';
45
+ *
46
+ * const trade = await market.prepareTrade({ targetMean: 105 });
47
+ * const rpcCalls = trade.calls.map(toRpcCall);
48
+ * await account.execute(rpcCalls);
49
+ * ```
50
+ */
51
+ export function toRpcCall(call) {
52
+ return {
53
+ contract_address: toHexAddress(call.contractAddress),
54
+ entry_point: call.entrypoint,
55
+ calldata: normalizeCalldata(call.calldata),
56
+ };
57
+ }
58
+ /**
59
+ * Converts an array of starknet.js Calls to snake_case RPC format.
60
+ *
61
+ * Convenience wrapper for `calls.map(toRpcCall)`.
62
+ *
63
+ * @param calls - Array of starknet.js Call objects (camelCase)
64
+ * @returns Array of Calls in RPC format (snake_case)
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * import { toRpcCalls } from '@the-situation/sdk';
69
+ *
70
+ * const trade = await market.prepareTrade({ targetMean: 105 });
71
+ * await account.execute(toRpcCalls(trade.calls));
72
+ * ```
73
+ */
74
+ export function toRpcCalls(calls) {
75
+ return calls.map(toRpcCall);
76
+ }
77
+ //# sourceMappingURL=call-format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"call-format.js","sourceRoot":"","sources":["../src/call-format.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAa,GAAG,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAczC;;GAEG;AACH,SAAS,iBAAiB,CAAC,QAAiB;IAC1C,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,CAAC;IACzB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;QAAE,OAAO,EAAE,CAAC;IACxC,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QACxB,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC1B,OAAO,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC/C,CAAC;QACD,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YACnD,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACtB,CAAC;QACD,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,SAAS,CAAC,IAAU;IAClC,OAAO;QACL,gBAAgB,EAAE,YAAY,CAAC,IAAI,CAAC,eAAe,CAAC;QACpD,WAAW,EAAE,IAAI,CAAC,UAAU;QAC5B,QAAQ,EAAE,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC;KAC3C,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Cumulative256 utilities for Oracle TWAP calculations.
3
+ *
4
+ * The Oracle stores cumulative mean and variance values as Cumulative256,
5
+ * which has the same structure as SQ128x128Raw but represents accumulated
6
+ * time-weighted values.
7
+ *
8
+ * TWAP calculation:
9
+ * average = (cumulative_end - cumulative_start) / (time_end - time_start)
10
+ *
11
+ * @module
12
+ */
13
+ import type { Cumulative256Raw } from '@the-situation/abi';
14
+ /**
15
+ * Converts a Cumulative256Raw to its scaled bigint representation.
16
+ *
17
+ * The returned value is scaled by 2^128 (same as SQ128x128).
18
+ *
19
+ * @param raw - The raw cumulative value from the Oracle contract
20
+ * @returns The signed scaled bigint
21
+ */
22
+ export declare function cumulative256ToScaledBigInt(raw: Cumulative256Raw): bigint;
23
+ /**
24
+ * Converts a Cumulative256Raw to a JavaScript number.
25
+ *
26
+ * Note: May lose precision for very large cumulative values.
27
+ * Use `cumulative256ToScaledBigInt` for precise calculations.
28
+ *
29
+ * @param raw - The raw cumulative value from the Oracle contract
30
+ * @returns The numeric value (unscaled)
31
+ */
32
+ export declare function cumulative256ToNumber(raw: Cumulative256Raw): number;
33
+ /**
34
+ * Computes the difference between two cumulative values.
35
+ *
36
+ * @param end - The ending cumulative value
37
+ * @param start - The starting cumulative value
38
+ * @returns The difference as a scaled bigint
39
+ */
40
+ export declare function cumulativeDiff(end: Cumulative256Raw, start: Cumulative256Raw): bigint;
41
+ /**
42
+ * Computes the TWAP (Time-Weighted Average Price) from cumulative values.
43
+ *
44
+ * TWAP = (cumulative_end - cumulative_start) / (time_end - time_start)
45
+ *
46
+ * @param startSnapshot - Snapshot at period start (timestamp and cumulative)
47
+ * @param endSnapshot - Snapshot at period end (timestamp and cumulative)
48
+ * @param field - Which cumulative field to use ('mean' or 'variance')
49
+ * @returns The computed TWAP as a number, or null if time difference is zero
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const twapMean = computeTwap(
54
+ * { blockTimestamp: 100n, meanCumulative: start },
55
+ * { blockTimestamp: 200n, meanCumulative: end },
56
+ * 'mean'
57
+ * );
58
+ * ```
59
+ */
60
+ export declare function computeTwap(startSnapshot: {
61
+ blockTimestamp: bigint;
62
+ meanCumulative: Cumulative256Raw;
63
+ varianceCumulative: Cumulative256Raw;
64
+ }, endSnapshot: {
65
+ blockTimestamp: bigint;
66
+ meanCumulative: Cumulative256Raw;
67
+ varianceCumulative: Cumulative256Raw;
68
+ }, field: 'mean' | 'variance'): number | null;
69
+ /**
70
+ * Formats a Cumulative256Raw for logging/debugging.
71
+ *
72
+ * @param raw - The raw cumulative value
73
+ * @param label - Optional label for the output
74
+ * @returns Formatted string showing limbs and numeric value
75
+ */
76
+ export declare function formatCumulative256(raw: Cumulative256Raw, label?: string): string;
77
+ //# sourceMappingURL=cumulative.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cumulative.d.ts","sourceRoot":"","sources":["../src/cumulative.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAY3D;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CAAC,GAAG,EAAE,gBAAgB,GAAG,MAAM,CAGzE;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,gBAAgB,GAAG,MAAM,CAInE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,gBAAgB,EAAE,KAAK,EAAE,gBAAgB,GAAG,MAAM,CAErF;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CACzB,aAAa,EAAE;IAAE,cAAc,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,gBAAgB,CAAC;IAAC,kBAAkB,EAAE,gBAAgB,CAAA;CAAE,EACjH,WAAW,EAAE;IAAE,cAAc,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,gBAAgB,CAAC;IAAC,kBAAkB,EAAE,gBAAgB,CAAA;CAAE,EAC/G,KAAK,EAAE,MAAM,GAAG,UAAU,GACzB,MAAM,GAAG,IAAI,CAgBf;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,gBAAgB,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAIjF"}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Cumulative256 utilities for Oracle TWAP calculations.
3
+ *
4
+ * The Oracle stores cumulative mean and variance values as Cumulative256,
5
+ * which has the same structure as SQ128x128Raw but represents accumulated
6
+ * time-weighted values.
7
+ *
8
+ * TWAP calculation:
9
+ * average = (cumulative_end - cumulative_start) / (time_end - time_start)
10
+ *
11
+ * @module
12
+ */
13
+ /** 2^128 - the scaling factor for SQ128x128 fractional part */
14
+ const SCALE = 1n << 128n;
15
+ /**
16
+ * Reconstructs a 256-bit magnitude from four 64-bit limbs.
17
+ */
18
+ function fromLimbs(limb0, limb1, limb2, limb3) {
19
+ return limb0 + (limb1 << 64n) + (limb2 << 128n) + (limb3 << 192n);
20
+ }
21
+ /**
22
+ * Converts a Cumulative256Raw to its scaled bigint representation.
23
+ *
24
+ * The returned value is scaled by 2^128 (same as SQ128x128).
25
+ *
26
+ * @param raw - The raw cumulative value from the Oracle contract
27
+ * @returns The signed scaled bigint
28
+ */
29
+ export function cumulative256ToScaledBigInt(raw) {
30
+ const magnitude = fromLimbs(raw.limb0, raw.limb1, raw.limb2, raw.limb3);
31
+ return raw.neg ? -magnitude : magnitude;
32
+ }
33
+ /**
34
+ * Converts a Cumulative256Raw to a JavaScript number.
35
+ *
36
+ * Note: May lose precision for very large cumulative values.
37
+ * Use `cumulative256ToScaledBigInt` for precise calculations.
38
+ *
39
+ * @param raw - The raw cumulative value from the Oracle contract
40
+ * @returns The numeric value (unscaled)
41
+ */
42
+ export function cumulative256ToNumber(raw) {
43
+ const magnitude = fromLimbs(raw.limb0, raw.limb1, raw.limb2, raw.limb3);
44
+ const value = Number(magnitude) / Number(SCALE);
45
+ return raw.neg ? -value : value;
46
+ }
47
+ /**
48
+ * Computes the difference between two cumulative values.
49
+ *
50
+ * @param end - The ending cumulative value
51
+ * @param start - The starting cumulative value
52
+ * @returns The difference as a scaled bigint
53
+ */
54
+ export function cumulativeDiff(end, start) {
55
+ return cumulative256ToScaledBigInt(end) - cumulative256ToScaledBigInt(start);
56
+ }
57
+ /**
58
+ * Computes the TWAP (Time-Weighted Average Price) from cumulative values.
59
+ *
60
+ * TWAP = (cumulative_end - cumulative_start) / (time_end - time_start)
61
+ *
62
+ * @param startSnapshot - Snapshot at period start (timestamp and cumulative)
63
+ * @param endSnapshot - Snapshot at period end (timestamp and cumulative)
64
+ * @param field - Which cumulative field to use ('mean' or 'variance')
65
+ * @returns The computed TWAP as a number, or null if time difference is zero
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * const twapMean = computeTwap(
70
+ * { blockTimestamp: 100n, meanCumulative: start },
71
+ * { blockTimestamp: 200n, meanCumulative: end },
72
+ * 'mean'
73
+ * );
74
+ * ```
75
+ */
76
+ export function computeTwap(startSnapshot, endSnapshot, field) {
77
+ const timeDiff = endSnapshot.blockTimestamp - startSnapshot.blockTimestamp;
78
+ if (timeDiff === 0n) {
79
+ return null;
80
+ }
81
+ const startCumulative = field === 'mean' ? startSnapshot.meanCumulative : startSnapshot.varianceCumulative;
82
+ const endCumulative = field === 'mean' ? endSnapshot.meanCumulative : endSnapshot.varianceCumulative;
83
+ const diff = cumulativeDiff(endCumulative, startCumulative);
84
+ // TWAP = diff / timeDiff
85
+ // Since diff is scaled by 2^128, we need to unscale: (diff / 2^128) / timeDiff
86
+ // = diff / (2^128 * timeDiff)
87
+ const scaledTimeDiff = SCALE * timeDiff;
88
+ return Number(diff) / Number(scaledTimeDiff);
89
+ }
90
+ /**
91
+ * Formats a Cumulative256Raw for logging/debugging.
92
+ *
93
+ * @param raw - The raw cumulative value
94
+ * @param label - Optional label for the output
95
+ * @returns Formatted string showing limbs and numeric value
96
+ */
97
+ export function formatCumulative256(raw, label) {
98
+ const numValue = cumulative256ToNumber(raw);
99
+ const prefix = label ? `${label}: ` : '';
100
+ return `${prefix}{ limb0: ${raw.limb0}, limb1: ${raw.limb1}, limb2: ${raw.limb2}, limb3: ${raw.limb3}, neg: ${raw.neg} } ≈ ${numValue.toFixed(6)}`;
101
+ }
102
+ //# sourceMappingURL=cumulative.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cumulative.js","sourceRoot":"","sources":["../src/cumulative.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,+DAA+D;AAC/D,MAAM,KAAK,GAAG,EAAE,IAAI,IAAI,CAAC;AAEzB;;GAEG;AACH,SAAS,SAAS,CAAC,KAAa,EAAE,KAAa,EAAE,KAAa,EAAE,KAAa;IAC3E,OAAO,KAAK,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;AACpE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,2BAA2B,CAAC,GAAqB;IAC/D,MAAM,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;IACxE,OAAO,GAAG,C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IAC9B,MAAM,cAAc,GAAG,KAAK,GAAG,QAAQ,CAAC;IACxC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAqB,EAAE,KAAc;IACvE,MAAM,QAAQ,GAAG,qBAAqB,CAAC,GAAG,CAAC,CAAC;IAC5C,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IACzC,OAAO,GAAG,MAAM,YAAY,GAAG,CAAC,KAAK,YAAY,GAAG,CAAC,KAAK,YAAY,GAAG,CAAC,KAAK,YAAY,GAAG,CAAC,KAAK,UAAU,GAAG,CAAC,GAAG,QAAQ,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;AACrJ,CAAC"}
@@ -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 '@the-situation/core';
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/decimal-scale.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,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 '@the-situation/core';
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/decimal-scale.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,4CAA4C;AAC5C,MAAM,CAAC,MAAM,WAAW,GAAW,GAAG,IAAI,GAAG,CAAC;AAE9C,sCAAsC;AACtC,MAAM,CAAC,MAAM,UAAU,GAAW,GAAG,IAAI,EAAE,CAAC;AAE5C,2CAA2C;AAC3C,MAAM,QAAQ,GAAW,EAAE,IAAI,IAAI,CAAC;AAEpC;;GAEG;AACH,SAAS,YAAY,CAAC,QAAgB;IACpC,OAAO,GAAG,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC;AACjC,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,eAAe,CAAC,MAAc,EAAE,QAAgB;IAC9D,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IACrC,6CAA6C;IAC7C,oCAAoC;IACpC,qCAAqC;IACrC,MAAM,WAAW,GAAG,CAAC,MAAM,GAAG,QAAQ,CAAC,GAAG,KAAK,CAAC;IAEhD,6CAA6C;IAC7C,MAAM,MAAM,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;IACjC,IAAI,WAAW,GAAG,MAAM,EAAE,CAAC;QACzB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,gDAAgD;IAChD,OAAO,SAAS,CAAC,OAAO,CAAC;QACvB,KAAK,EAAE,WAAW,GAAG,CA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AI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;IAC7F,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IAErC,4BAA4B;IAC5B,MAAM,SAAS,GAAG,SAAS,GAAG,KAAK,CAAC;IACpC,MAAM,MAAM,GAAG,SAAS,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,GAAG,QAAQ,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC;IAE9E,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;AACpC,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * ERC20 utilities for building token approval calls.
3
+ *
4
+ * @module
5
+ */
6
+ import { type Call } from 'starknet';
7
+ /**
8
+ * Builds an ERC20 approve call for the given token, spender, and amount.
9
+ *
10
+ * Includes a 5% buffer on the approve amount to handle rounding differences
11
+ * between SDK calculations and contract execution.
12
+ *
13
+ * @param tokenAddress - The ERC20 token contract address
14
+ * @param spender - The address to approve spending (e.g., the market contract)
15
+ * @param amount - The amount to approve (raw token amount with decimals)
16
+ * @param bufferPercent - Buffer percentage to add to amount (default: 5%)
17
+ * @returns Call object for batching with account.execute()
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const approveCall = buildApproveCall(usdcAddress, marketAddress, 1_000_000n);
22
+ * await account.execute([approveCall, tradeCall]);
23
+ * ```
24
+ */
25
+ export declare function buildApproveCall(tokenAddress: string, spender: string, amount: bigint, bufferPercent?: number): Call;
26
+ //# sourceMappingURL=erc20.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"erc20.d.ts","sourceRoot":"","sources":["../src/erc20.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,IAAI,EAAc,MAAM,UAAU,CAAC;AAMjD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAC9B,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,aAAa,GAAE,MAAuC,GACrD,IAAI,CAcN"}
package/dist/erc20.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * ERC20 utilities for building token approval calls.
3
+ *
4
+ * @module
5
+ */
6
+ import { cairo, num } from 'starknet';
7
+ import { toHexAddress } from './address';
8
+ /** Default buffer percentage for approve amounts (5%) */
9
+ const DEFAULT_APPROVE_BUFFER_PERCENT = 5;
10
+ /**
11
+ * Builds an ERC20 approve call for the given token, spender, and amount.
12
+ *
13
+ * Includes a 5% buffer on the approve amount to handle rounding differences
14
+ * between SDK calculations and contract execution.
15
+ *
16
+ * @param tokenAddress - The ERC20 token contract address
17
+ * @param spender - The address to approve spending (e.g., the market contract)
18
+ * @param amount - The amount to approve (raw token amount with decimals)
19
+ * @param bufferPercent - Buffer percentage to add to amount (default: 5%)
20
+ * @returns Call object for batching with account.execute()
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const approveCall = buildApproveCall(usdcAddress, marketAddress, 1_000_000n);
25
+ * await account.execute([approveCall, tradeCall]);
26
+ * ```
27
+ */
28
+ export function buildApproveCall(tokenAddress, spender, amount, bufferPercent = DEFAULT_APPROVE_BUFFER_PERCENT) {
29
+ // Apply buffer to approve amount
30
+ const bufferedAmount = amount + (amount * BigInt(bufferPercent)) / 100n;
31
+ const uint256 = cairo.uint256(bufferedAmount);
32
+ // Normalize addresses to hex format
33
+ const hexToken = toHexAddress(tokenAddress);
34
+ const hexSpender = toHexAddress(spender);
35
+ return {
36
+ contractAddress: hexToken,
37
+ entrypoint: 'approve',
38
+ calldata: [hexSpender, num.toHex(uint256.low), num.toHex(uint256.high)],
39
+ };
40
+ }
41
+ //# sourceMappingURL=erc20.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"erc20.js","sourceRoot":"","sources":["../src/erc20.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAa,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,yDAAyD;AACzD,MAAM,8BAA8B,GAAG,CAAC,CAAC;AAEzC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,YAAoB,EACpB,OAAe,EACf,MAAc,EACd,gBAAwB,8BAA8B;IAEtD,iCAAiC;IACjC,MAAM,cAAc,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC,GAAG,IAAI,CAAC;IACxE,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IAE9C,oCAAoC;IACpC,MAAM,QAAQ,GAAG,YAAY,CAAC,YAAY,CAAC,CAAC;IAC5C,MAAM,UAAU,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IAEzC,OAAO;QACL,eAAe,EAAE,QAAQ;QACzB,UAAU,EAAE,SAAS;QACrB,QAAQ,EAAE,CAAC,UAAU,EAAE,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;KACxE,CAAC;AACJ,CAAC"}
@@ -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 '@the-situation/core';
10
+ import type { NormalSqrtHintsRaw } from '@the-situation/core';
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/hints.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAO,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAG9D;;;;;;;;;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"}