@the-situation/sdk 0.3.0-alpha.4 → 0.3.0-alpha.5

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.
@@ -20,7 +20,7 @@ export const NETWORKS = {
20
20
  sepolia: {
21
21
  name: 'sepolia',
22
22
  chainId: 'SN_SEPOLIA',
23
- rpcUrl: 'https://starknet-sepolia.public.blastapi.io',
23
+ rpcUrl: 'https://api.cartridge.gg/x/starknet/sepolia',
24
24
  explorerUrl: 'https://sepolia.starkscan.co',
25
25
  },
26
26
  devnet: {
@@ -13,19 +13,14 @@ export declare const SQRT_PI: number;
13
13
  /**
14
14
  * sqrt(π) as SQ128x128 constant.
15
15
  *
16
- * Computed with high precision: 1.7724538509055159...
16
+ * Uses the exact raw constant from Cairo contracts.
17
17
  */
18
18
  export declare const SQRT_PI_SQ: SQ128x128;
19
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.
20
+ * Computes floor(sqrt(n)) in SQ128x128 space with contract-equivalent semantics.
26
21
  *
27
22
  * @param n - The value to take the square root of (must be non-negative)
28
- * @param maxIter - Maximum iterations (default: 20 for full precision)
23
+ * @param maxIter - Deprecated legacy parameter (ignored)
29
24
  * @returns The square root, or null if input is negative or computation fails
30
25
  *
31
26
  * @example
@@ -1 +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"}
1
+ {"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../../src/utils/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAQ,MAAM,gBAAgB,CAAC;AA2DjD;;;;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"}
@@ -3,7 +3,53 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- import { SQ128x128, TWO, ZERO } from '../types/sq128';
6
+ import { SQ128x128, ZERO } from '../types/sq128';
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
+ }
7
53
  /**
8
54
  * sqrt(π) ≈ 1.7724538509055159
9
55
  *
@@ -13,19 +59,20 @@ export const SQRT_PI = Math.sqrt(Math.PI);
13
59
  /**
14
60
  * sqrt(π) as SQ128x128 constant.
15
61
  *
16
- * Computed with high precision: 1.7724538509055159...
62
+ * Uses the exact raw constant from Cairo contracts.
17
63
  */
18
- export const SQRT_PI_SQ = SQ128x128.fromNumber(SQRT_PI);
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
+ })();
19
71
  /**
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.
72
+ * Computes floor(sqrt(n)) in SQ128x128 space with contract-equivalent semantics.
26
73
  *
27
74
  * @param n - The value to take the square root of (must be non-negative)
28
- * @param maxIter - Maximum iterations (default: 20 for full precision)
75
+ * @param maxIter - Deprecated legacy parameter (ignored)
29
76
  * @returns The square root, or null if input is negative or computation fails
30
77
  *
31
78
  * @example
@@ -41,45 +88,18 @@ export const SQRT_PI_SQ = SQ128x128.fromNumber(SQRT_PI);
41
88
  * ```
42
89
  */
43
90
  export function sqrtSQ128x128(n, maxIter = 20) {
91
+ void maxIter;
44
92
  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
93
  if (raw.neg) {
51
94
  return null;
52
95
  }
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;
96
+ const magnitude = rawToMagnitude(raw);
97
+ if (magnitude === 0n) {
98
+ return ZERO;
82
99
  }
83
- return x;
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));
84
104
  }
85
105
  //# sourceMappingURL=math.js.map
@@ -1 +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,EAA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AAC,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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@the-situation/sdk",
3
- "version": "0.3.0-alpha.4",
3
+ "version": "0.3.0-alpha.5",
4
4
  "description": "TypeScript SDK for The Situation prediction market contracts on Starknet",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",