@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.
- package/dist/providers/base.js +1 -1
- package/dist/utils/math.d.ts +3 -8
- package/dist/utils/math.d.ts.map +1 -1
- package/dist/utils/math.js +65 -45
- package/dist/utils/math.js.map +1 -1
- package/package.json +1 -1
package/dist/providers/base.js
CHANGED
|
@@ -20,7 +20,7 @@ export const NETWORKS = {
|
|
|
20
20
|
sepolia: {
|
|
21
21
|
name: 'sepolia',
|
|
22
22
|
chainId: 'SN_SEPOLIA',
|
|
23
|
-
rpcUrl: 'https://starknet
|
|
23
|
+
rpcUrl: 'https://api.cartridge.gg/x/starknet/sepolia',
|
|
24
24
|
explorerUrl: 'https://sepolia.starkscan.co',
|
|
25
25
|
},
|
|
26
26
|
devnet: {
|
package/dist/utils/math.d.ts
CHANGED
|
@@ -13,19 +13,14 @@ export declare const SQRT_PI: number;
|
|
|
13
13
|
/**
|
|
14
14
|
* sqrt(π) as SQ128x128 constant.
|
|
15
15
|
*
|
|
16
|
-
*
|
|
16
|
+
* Uses the exact raw constant from Cairo contracts.
|
|
17
17
|
*/
|
|
18
18
|
export declare const SQRT_PI_SQ: SQ128x128;
|
|
19
19
|
/**
|
|
20
|
-
* Computes
|
|
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 -
|
|
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
|
package/dist/utils/math.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../../src/utils/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,
|
|
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"}
|
package/dist/utils/math.js
CHANGED
|
@@ -3,7 +3,53 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import { SQ128x128,
|
|
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
|
-
*
|
|
62
|
+
* Uses the exact raw constant from Cairo contracts.
|
|
17
63
|
*/
|
|
18
|
-
export const SQRT_PI_SQ =
|
|
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
|
|
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 -
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
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
|
package/dist/utils/math.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"math.js","sourceRoot":"","sources":["../../src/utils/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,
|
|
1
|
+
{"version":3,"file":"math.js","sourceRoot":"","sources":["../../src/utils/math.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAGjD,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,mBA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