@skate-org/amm-core-v2 1.0.0 → 2.0.0-alpha.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @skate-org/amm-core-v2
2
2
 
3
+ ## 2.0.0-alpha.2 — 2026-05-05
4
+
5
+ Plan C of the SVM/Sui integration. Adds `@skate-org/amm-sui-v2` to
6
+ the workspace; this package's source is unchanged versus
7
+ `2.0.0-alpha.1` (re-versioned in lockstep).
8
+
9
+ ## 2.0.0-alpha.1 — 2026-05-05
10
+
11
+ Plan B of the SVM/Sui integration. Adds `@skate-org/amm-svm-v2` to
12
+ the workspace; this package's source is unchanged versus
13
+ `2.0.0-alpha.0` (re-versioned in lockstep).
14
+
15
+ ## 2.0.0-alpha.0 — 2026-05-05
16
+
17
+ Plan A of the SVM/Sui integration (see
18
+ docs/superpowers/specs/2026-05-04-svm-sui-integration-design.md).
19
+
20
+ - Add uniswap-v3 math module (`tickMath`, `sqrtPriceMath`,
21
+ `liquidityMath`, `fullMath`). Relocated from
22
+ `@skate-org/amm-evm-v2`.
23
+ - Add `EvmChain` / `SvmChain` / `SuiChain` literal-union sub-types of
24
+ `CHAIN`.
25
+ - **Breaking:** `PoolInfoType.peripheryInfo` is now a chain-id-
26
+ discriminated mapped type. EVM chain entries narrow to
27
+ `EvmPeripheryInfo`; SVM chain entries to `SvmPeripheryInfo`; Sui
28
+ to `SuiPeripheryInfo`. Code that read
29
+ `pool.peripheryInfo[chain].address` as `0x${string}` unconditionally
30
+ now type-narrows by chain.
31
+ - New named types `EvmPeripheryInfo`, `SvmPeripheryInfo`,
32
+ `SuiPeripheryInfo`, `PeripheryInfoFor<K>`.
33
+ - Cross-VM utilities (`base58ToBytes32`, `evmAddressToBytes32`,
34
+ `bytes32ToBase58`, `toBytes32Address`, `fromBytes32Address`)
35
+ unchanged in this release — already in core under
36
+ `adapter/skate.ts`.
37
+
3
38
  ## 1.0.0 — 2026-05-04
4
39
 
5
40
  Realigning all four `@skate-org/amm-*-v2` packages at a single version
@@ -1,13 +1,47 @@
1
- import { TokenPair } from "../../types/pair";
1
+ import type { Token } from "../../types/pair";
2
+ import type { EvmChain, SvmChain, SuiChain } from "../../types/chain-vm";
3
+ import type { CHAIN } from "../../types/chain";
2
4
  export type PoolKey = string;
5
+ export interface EvmPeripheryInfo {
6
+ /** Periphery contract address on the source EVM chain. */
7
+ address: `0x${string}`;
8
+ token0: Token;
9
+ token1: Token;
10
+ /** Human-readable chain label (e.g. "ARBITRUM"). */
11
+ chain: string;
12
+ }
13
+ export interface SvmPeripheryInfo {
14
+ /** Periphery pool PDA (base58 string). */
15
+ peripheryPool: string;
16
+ /** Periphery manager program address (base58 string). */
17
+ manager: string;
18
+ /** SPL or Token-2022 program id for token0. */
19
+ tokenProgramId0: string;
20
+ /** SPL or Token-2022 program id for token1. */
21
+ tokenProgramId1: string;
22
+ token0: Token;
23
+ token1: Token;
24
+ chain: string;
25
+ }
26
+ export interface SuiPeripheryInfo {
27
+ /** Sui object id of the periphery pool (0x-prefixed). */
28
+ poolId: string;
29
+ /** Sui object id of the manager cap. */
30
+ managerCapId: string;
31
+ /** Sui package id that owns the periphery module. */
32
+ peripheryPackage: string;
33
+ token0: Token;
34
+ token1: Token;
35
+ chain: string;
36
+ }
37
+ export type PeripheryInfoFor<K extends CHAIN> = K extends EvmChain ? EvmPeripheryInfo : K extends SvmChain ? SvmPeripheryInfo : K extends SuiChain ? SuiPeripheryInfo : never;
3
38
  export type PoolInfoType = Record<PoolKey, {
4
39
  kernelPool: `0x${string}`;
5
40
  token0: string;
6
41
  token1: string;
7
- peripheryInfo: Record<number, TokenPair & {
8
- address: `0x${string}` | string;
9
- chain: string;
10
- }>;
11
42
  feeBps: number;
43
+ peripheryInfo: {
44
+ [K in CHAIN]?: PeripheryInfoFor<K>;
45
+ };
12
46
  }>;
13
47
  //# sourceMappingURL=_type_.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"_type_.d.ts","sourceRoot":"","sources":["../../../src/deployment/aggregated/_type_.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAE7B,MAAM,MAAM,YAAY,GAAG,MAAM,CAC/B,OAAO,EACP;IACE,UAAU,EAAE,KAAK,MAAM,EAAE,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE,MAAM,CACnB,MAAM,EACN,SAAS,GAAG;QACV,OAAO,EAAE,KAAK,MAAM,EAAE,GAAG,MAAM,CAAC;QAChC,KAAK,EAAE,MAAM,CAAC;KACf,CACF,CAAC;IACF,MAAM,EAAE,MAAM,CAAC;CAChB,CACF,CAAC"}
1
+ {"version":3,"file":"_type_.d.ts","sourceRoot":"","sources":["../../../src/deployment/aggregated/_type_.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE/C,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAE7B,MAAM,WAAW,gBAAgB;IAC/B,0DAA0D;IAC1D,OAAO,EAAE,KAAK,MAAM,EAAE,CAAC;IACvB,MAAM,EAAE,KAAK,CAAC;IACd,MAAM,EAAE,KAAK,CAAC;IACd,oDAAoD;IACpD,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,0CAA0C;IAC1C,aAAa,EAAE,MAAM,CAAC;IACtB,yDAAyD;IACzD,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,eAAe,EAAE,MAAM,CAAC;IACxB,+CAA+C;IAC/C,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,EAAE,KAAK,CAAC;IACd,MAAM,EAAE,KAAK,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,yDAAyD;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,wCAAwC;IACxC,YAAY,EAAE,MAAM,CAAC;IACrB,qDAAqD;IACrD,gBAAgB,EAAE,MAAM,CAAC;IACzB,MAAM,EAAE,KAAK,CAAC;IACd,MAAM,EAAE,KAAK,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,KAAK,IAC1C,CAAC,SAAS,QAAQ,GAAG,gBAAgB,GACnC,CAAC,SAAS,QAAQ,GAAG,gBAAgB,GACrC,CAAC,SAAS,QAAQ,GAAG,gBAAgB,GACrC,KAAK,CAAC;AAEV,MAAM,MAAM,YAAY,GAAG,MAAM,CAC/B,OAAO,EACP;IACE,UAAU,EAAE,KAAK,MAAM,EAAE,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE;SAAG,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;KAAE,CAAC;CACvD,CACF,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { CHAIN, EnvMode, Token, TokenPair } from "../../types";
2
+ import type { PeripheryInfoFor } from "./_type_";
2
3
  import { PoolInfoType, PoolKey } from "./_type_";
3
4
  export declare function getAllPoolInfo(mode?: EnvMode): PoolInfoType;
4
5
  export declare function getAllPoolKeys(mode?: EnvMode): PoolKey[];
@@ -6,11 +7,8 @@ export declare function getPoolInfoByKey(key: PoolKey, mode?: EnvMode): {
6
7
  kernelPool: `0x${string}`;
7
8
  token0: string;
8
9
  token1: string;
9
- peripheryInfo: Record<number, TokenPair & {
10
- address: `0x${string}` | string;
11
- chain: string;
12
- }>;
13
10
  feeBps: number;
11
+ peripheryInfo: { [K in CHAIN]?: PeripheryInfoFor<K>; };
14
12
  } | null;
15
13
  export declare function getSupportedChains(mode?: EnvMode): CHAIN[];
16
14
  export declare function getSupportedPairs(chainId: CHAIN, mode?: EnvMode): (TokenPair & {
@@ -19,8 +17,6 @@ export declare function getSupportedPairs(chainId: CHAIN, mode?: EnvMode): (Toke
19
17
  export declare function getSupportedTokens(chainId: CHAIN, mode?: EnvMode): Token[];
20
18
  export declare function getOtherTokensInPairs(chainId: CHAIN, tokenAddress: string, mode?: EnvMode): Token[];
21
19
  export declare function kernelPoolToPoolKey(kernelPool: `0x${string}`, mode?: EnvMode): PoolKey;
22
- export declare function getPeripheryDetailsByKernelPoolAndChainId(kernelPool: `0x${string}`, chainId: number, mode?: EnvMode): (TokenPair & {
23
- address: `0x${string}` | string;
24
- chain: string;
25
- }) | null;
20
+ export declare function getPeripheryDetailsByKernelPoolAndChainId<K extends CHAIN>(kernelPool: `0x${string}`, chainId: K, mode?: EnvMode): PeripheryInfoFor<K> | null;
21
+ export declare function getPeripheryDetailsByKernelPoolAndChainId(kernelPool: `0x${string}`, chainId: number, mode?: EnvMode): PeripheryInfoFor<CHAIN> | null;
26
22
  //# sourceMappingURL=retrieval.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"retrieval.d.ts","sourceRoot":"","sources":["../../../src/deployment/aggregated/retrieval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAI/D,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEjD,wBAAgB,cAAc,CAAC,IAAI,GAAE,OAAsB,GAAG,YAAY,CAMzE;AAED,wBAAgB,cAAc,CAAC,IAAI,GAAE,OAAsB,GAAG,OAAO,EAAE,CAEtE;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,GAAE,OAAsB;;;;;;;;;SAE1E;AAED,wBAAgB,kBAAkB,CAAC,IAAI,GAAE,OAAsB,GAAG,KAAK,EAAE,CASxE;AAED,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,KAAK,EACd,IAAI,GAAE,OAAsB,GAC3B,CAAC,SAAS,GAAG;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,EAAE,CActC;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,GAAE,OAAsB,GAAG,KAAK,EAAE,CAcxF;AAED,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,KAAK,EACd,YAAY,EAAE,MAAM,EACpB,IAAI,GAAE,OAAsB,GAC3B,KAAK,EAAE,CAeT;AAED,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,KAAK,MAAM,EAAE,EAAE,IAAI,GAAE,OAAsB,GAAG,OAAO,CAOpG;AAED,wBAAgB,yCAAyC,CACvD,UAAU,EAAE,KAAK,MAAM,EAAE,EACzB,OAAO,EAAE,MAAM,EACf,IAAI,GAAE,OAAsB;;;UAK7B"}
1
+ {"version":3,"file":"retrieval.d.ts","sourceRoot":"","sources":["../../../src/deployment/aggregated/retrieval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAIjD,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEjD,wBAAgB,cAAc,CAAC,IAAI,GAAE,OAAsB,GAAG,YAAY,CAMzE;AAED,wBAAgB,cAAc,CAAC,IAAI,GAAE,OAAsB,GAAG,OAAO,EAAE,CAEtE;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,GAAE,OAAsB;;;;;iCAsBhE,CAAC;SApBX;AAED,wBAAgB,kBAAkB,CAAC,IAAI,GAAE,OAAsB,GAAG,KAAK,EAAE,CASxE;AAED,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,KAAK,EACd,IAAI,GAAE,OAAsB,GAC3B,CAAC,SAAS,GAAG;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,EAAE,CActC;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,GAAE,OAAsB,GAAG,KAAK,EAAE,CAcxF;AAED,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,KAAK,EACd,YAAY,EAAE,MAAM,EACpB,IAAI,GAAE,OAAsB,GAC3B,KAAK,EAAE,CAeT;AAED,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,KAAK,MAAM,EAAE,EAAE,IAAI,GAAE,OAAsB,GAAG,OAAO,CAOpG;AAED,wBAAgB,yCAAyC,CAAC,CAAC,SAAS,KAAK,EACvE,UAAU,EAAE,KAAK,MAAM,EAAE,EACzB,OAAO,EAAE,CAAC,EACV,IAAI,CAAC,EAAE,OAAO,GACb,gBAAgB,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;AAC9B,wBAAgB,yCAAyC,CACvD,UAAU,EAAE,KAAK,MAAM,EAAE,EACzB,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,OAAO,GACb,gBAAgB,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC"}
package/dist/index.cjs CHANGED
@@ -37,6 +37,12 @@ __export(index_exports, {
37
37
  KERNEL_POOL_USDC_USDT_Dev: () => KERNEL_POOL_USDC_USDT_Dev,
38
38
  KernelEventEmitterAddress: () => KernelEventEmitterAddress,
39
39
  KernelManagerAddress: () => KernelManagerAddress,
40
+ MAX_SQRT_PRICE_LIMIT: () => MAX_SQRT_PRICE_LIMIT,
41
+ MAX_SQRT_RATIO: () => MAX_SQRT_RATIO,
42
+ MAX_TICK: () => MAX_TICK,
43
+ MIN_SQRT_PRICE_LIMIT: () => MIN_SQRT_PRICE_LIMIT,
44
+ MIN_SQRT_RATIO: () => MIN_SQRT_RATIO,
45
+ MIN_TICK: () => MIN_TICK,
40
46
  PERIPHERY_DEV: () => PERIPHERY_DEV,
41
47
  PERIPHERY_PROD: () => PERIPHERY_PROD,
42
48
  PERIPHERY_STAGING: () => PERIPHERY_STAGING,
@@ -50,6 +56,7 @@ __export(index_exports, {
50
56
  SkateGatewayAddress: () => SkateGatewayAddress,
51
57
  TaskPhase: () => TaskPhase,
52
58
  VM: () => VM,
59
+ addDelta: () => addDelta,
53
60
  base58ToBytes32: () => base58ToBytes32,
54
61
  base58ToHex: () => base58ToHex,
55
62
  bufferFromBase58: () => bufferFromBase58,
@@ -60,17 +67,29 @@ __export(index_exports, {
60
67
  fromBytes32Address: () => fromBytes32Address,
61
68
  getAllPoolInfo: () => getAllPoolInfo,
62
69
  getAllPoolKeys: () => getAllPoolKeys,
70
+ getAmount0Delta: () => getAmount0Delta,
71
+ getAmount0DeltaSigned: () => getAmount0DeltaSigned,
72
+ getAmount1Delta: () => getAmount1Delta,
73
+ getAmount1DeltaSigned: () => getAmount1DeltaSigned,
63
74
  getBlockExplorer: () => getBlockExplorer,
75
+ getNextSqrtPriceFromAmount0RoundingUp: () => getNextSqrtPriceFromAmount0RoundingUp,
76
+ getNextSqrtPriceFromAmount1RoundingDown: () => getNextSqrtPriceFromAmount1RoundingDown,
77
+ getNextSqrtPriceFromInput: () => getNextSqrtPriceFromInput,
78
+ getNextSqrtPriceFromOutput: () => getNextSqrtPriceFromOutput,
64
79
  getOtherTokensInPairs: () => getOtherTokensInPairs,
65
80
  getPeripheryDetailsByKernelPoolAndChainId: () => getPeripheryDetailsByKernelPoolAndChainId,
66
81
  getPoolInfoByKey: () => getPoolInfoByKey,
82
+ getSqrtRatioAtTick: () => getSqrtRatioAtTick,
67
83
  getSupportedChains: () => getSupportedChains,
68
84
  getSupportedPairs: () => getSupportedPairs,
69
85
  getSupportedTokens: () => getSupportedTokens,
86
+ getTickAtSqrtRatio: () => getTickAtSqrtRatio,
70
87
  hexToBase58: () => hexToBase58,
71
88
  isHexString: () => isHexString,
72
89
  isHexStringLoose: () => isHexStringLoose,
73
90
  kernelPoolToPoolKey: () => kernelPoolToPoolKey,
91
+ mulDiv: () => mulDiv,
92
+ mulDivRoundingUp: () => mulDivRoundingUp,
74
93
  normalizeMode: () => normalizeMode,
75
94
  skateAdapter: () => skate_exports,
76
95
  toBase58: () => toBase58,
@@ -597,6 +616,352 @@ var SdkError = class extends Error {
597
616
  super(message, options);
598
617
  }
599
618
  };
619
+
620
+ // src/math/tickMath.ts
621
+ var MAX_UINT256 = (1n << 256n) - 1n;
622
+ var Q160_MASK = (1n << 160n) - 1n;
623
+ var Q256_MOD = 1n << 256n;
624
+ var MIN_TICK = -887272;
625
+ var MAX_TICK = 887272;
626
+ var MIN_SQRT_RATIO = 4295128739n;
627
+ var MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342n;
628
+ var MIN_SQRT_PRICE_LIMIT = MIN_SQRT_RATIO + 1n;
629
+ var MAX_SQRT_PRICE_LIMIT = MAX_SQRT_RATIO - 1n;
630
+ function getSqrtRatioAtTick(tick) {
631
+ if (!Number.isInteger(tick)) {
632
+ throw new Error("TickMath: tick must be an integer");
633
+ }
634
+ const absTick = BigInt(tick < 0 ? -tick : tick);
635
+ if (absTick > BigInt(MAX_TICK)) {
636
+ throw new Error("T");
637
+ }
638
+ let ratio = (absTick & 0x1n) !== 0n ? 0xfffcb933bd6fad37aa2d162d1a594001n : 0x100000000000000000000000000000000n;
639
+ if ((absTick & 0x2n) !== 0n)
640
+ ratio = (ratio * 0xfff97272373d413259a46990580e213an & MAX_UINT256) >> 128n;
641
+ if ((absTick & 0x4n) !== 0n)
642
+ ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdccn & MAX_UINT256) >> 128n;
643
+ if ((absTick & 0x8n) !== 0n)
644
+ ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0n & MAX_UINT256) >> 128n;
645
+ if ((absTick & 0x10n) !== 0n)
646
+ ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644n & MAX_UINT256) >> 128n;
647
+ if ((absTick & 0x20n) !== 0n)
648
+ ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0n & MAX_UINT256) >> 128n;
649
+ if ((absTick & 0x40n) !== 0n)
650
+ ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861n & MAX_UINT256) >> 128n;
651
+ if ((absTick & 0x80n) !== 0n)
652
+ ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053n & MAX_UINT256) >> 128n;
653
+ if ((absTick & 0x100n) !== 0n)
654
+ ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4n & MAX_UINT256) >> 128n;
655
+ if ((absTick & 0x200n) !== 0n)
656
+ ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54n & MAX_UINT256) >> 128n;
657
+ if ((absTick & 0x400n) !== 0n)
658
+ ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3n & MAX_UINT256) >> 128n;
659
+ if ((absTick & 0x800n) !== 0n)
660
+ ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9n & MAX_UINT256) >> 128n;
661
+ if ((absTick & 0x1000n) !== 0n)
662
+ ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825n & MAX_UINT256) >> 128n;
663
+ if ((absTick & 0x2000n) !== 0n)
664
+ ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5n & MAX_UINT256) >> 128n;
665
+ if ((absTick & 0x4000n) !== 0n)
666
+ ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7n & MAX_UINT256) >> 128n;
667
+ if ((absTick & 0x8000n) !== 0n)
668
+ ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6n & MAX_UINT256) >> 128n;
669
+ if ((absTick & 0x10000n) !== 0n)
670
+ ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9n & MAX_UINT256) >> 128n;
671
+ if ((absTick & 0x20000n) !== 0n)
672
+ ratio = (ratio * 0x5d6af8dedb81196699c329225ee604n & MAX_UINT256) >> 128n;
673
+ if ((absTick & 0x40000n) !== 0n)
674
+ ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98n & MAX_UINT256) >> 128n;
675
+ if ((absTick & 0x80000n) !== 0n)
676
+ ratio = (ratio * 0x48a170391f7dc42444e8fa2n & MAX_UINT256) >> 128n;
677
+ if (tick > 0) ratio = MAX_UINT256 / ratio;
678
+ const shifted = ratio >> 32n;
679
+ const remainder = ratio & (1n << 32n) - 1n;
680
+ const sqrtPriceX96 = remainder === 0n ? shifted : shifted + 1n;
681
+ return sqrtPriceX96 & Q160_MASK;
682
+ }
683
+ function getTickAtSqrtRatio(sqrtPriceX96) {
684
+ if (sqrtPriceX96 < MIN_SQRT_RATIO || sqrtPriceX96 >= MAX_SQRT_RATIO) {
685
+ throw new Error("R");
686
+ }
687
+ const ratio = sqrtPriceX96 << 32n;
688
+ let r = ratio;
689
+ let msb = 0n;
690
+ {
691
+ const f = r > 0xffffffffffffffffffffffffffffffffn ? 128n : 0n;
692
+ msb |= f;
693
+ r >>= f;
694
+ }
695
+ {
696
+ const f = r > 0xffffffffffffffffn ? 64n : 0n;
697
+ msb |= f;
698
+ r >>= f;
699
+ }
700
+ {
701
+ const f = r > 0xffffffffn ? 32n : 0n;
702
+ msb |= f;
703
+ r >>= f;
704
+ }
705
+ {
706
+ const f = r > 0xffffn ? 16n : 0n;
707
+ msb |= f;
708
+ r >>= f;
709
+ }
710
+ {
711
+ const f = r > 0xffn ? 8n : 0n;
712
+ msb |= f;
713
+ r >>= f;
714
+ }
715
+ {
716
+ const f = r > 0xfn ? 4n : 0n;
717
+ msb |= f;
718
+ r >>= f;
719
+ }
720
+ {
721
+ const f = r > 0x3n ? 2n : 0n;
722
+ msb |= f;
723
+ r >>= f;
724
+ }
725
+ {
726
+ const f = r > 0x1n ? 1n : 0n;
727
+ msb |= f;
728
+ }
729
+ if (msb >= 128n) r = ratio >> msb - 127n;
730
+ else r = ratio << 127n - msb & MAX_UINT256;
731
+ const offset = msb - 128n;
732
+ let log_2 = offset << 64n & MAX_UINT256;
733
+ for (let i = 0; i < 14; i++) {
734
+ r = r * r >> 127n;
735
+ const f = r >> 128n;
736
+ log_2 |= f << BigInt(63 - i);
737
+ r >>= f;
738
+ }
739
+ const log_2_signed = toInt256(log_2);
740
+ const log_sqrt10001 = log_2_signed * 255738958999603826347141n;
741
+ const tickLowBig = log_sqrt10001 - 3402992956809132418596140100660247210n >> 128n;
742
+ const tickHiBig = log_sqrt10001 + 291339464771989622907027621153398088495n >> 128n;
743
+ const tickLow = toInt24(tickLowBig);
744
+ const tickHi = toInt24(tickHiBig);
745
+ if (tickLow === tickHi) return tickLow;
746
+ return getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
747
+ }
748
+ function toInt256(x) {
749
+ const masked = x & MAX_UINT256;
750
+ return masked >= 1n << 255n ? masked - Q256_MOD : masked;
751
+ }
752
+ function toInt24(x) {
753
+ const mask = (1n << 24n) - 1n;
754
+ const truncated = x & mask;
755
+ const signed = truncated >= 1n << 23n ? truncated - (1n << 24n) : truncated;
756
+ return Number(signed);
757
+ }
758
+
759
+ // src/math/fullMath.ts
760
+ var MAX_UINT2562 = (1n << 256n) - 1n;
761
+ function mulDiv(a, b, denom) {
762
+ if (denom === 0n) {
763
+ throw new Error("FullMath: division by zero");
764
+ }
765
+ if (a < 0n || b < 0n || denom < 0n) {
766
+ throw new Error("FullMath: negative input");
767
+ }
768
+ const product = a * b;
769
+ const result = product / denom;
770
+ if (result > MAX_UINT2562) {
771
+ throw new Error("FullMath: result overflows uint256");
772
+ }
773
+ return result;
774
+ }
775
+ function mulDivRoundingUp(a, b, denom) {
776
+ if (denom === 0n) {
777
+ throw new Error("FullMath: division by zero");
778
+ }
779
+ if (a < 0n || b < 0n || denom < 0n) {
780
+ throw new Error("FullMath: negative input");
781
+ }
782
+ const product = a * b;
783
+ const quotient = product / denom;
784
+ if (quotient > MAX_UINT2562) {
785
+ throw new Error("FullMath: result overflows uint256");
786
+ }
787
+ if (product % denom !== 0n) {
788
+ if (quotient === MAX_UINT2562) {
789
+ throw new Error("FullMath: rounding up overflows uint256");
790
+ }
791
+ return quotient + 1n;
792
+ }
793
+ return quotient;
794
+ }
795
+
796
+ // src/math/sqrtPriceMath.ts
797
+ var MAX_UINT128 = (1n << 128n) - 1n;
798
+ var MAX_UINT160 = (1n << 160n) - 1n;
799
+ var MAX_UINT2563 = (1n << 256n) - 1n;
800
+ var Q96_RESOLUTION = 96n;
801
+ var Q96 = 1n << 96n;
802
+ function assertUint160(x, name) {
803
+ if (x < 0n || x > MAX_UINT160) {
804
+ throw new Error(`SqrtPriceMath: ${name} out of uint160 range`);
805
+ }
806
+ }
807
+ function assertUint128(x, name) {
808
+ if (x < 0n || x > MAX_UINT128) {
809
+ throw new Error(`SqrtPriceMath: ${name} out of uint128 range`);
810
+ }
811
+ }
812
+ function assertUint256(x, name) {
813
+ if (x < 0n || x > MAX_UINT2563) {
814
+ throw new Error(`SqrtPriceMath: ${name} out of uint256 range`);
815
+ }
816
+ }
817
+ function divRoundingUp(x, y) {
818
+ if (y === 0n) throw new Error("SqrtPriceMath: div by zero");
819
+ return x / y + (x % y === 0n ? 0n : 1n);
820
+ }
821
+ function toUint160(x) {
822
+ if (x > MAX_UINT160) throw new Error("SqrtPriceMath: toUint160 overflow");
823
+ return x;
824
+ }
825
+ function getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amount, add) {
826
+ assertUint160(sqrtPX96, "sqrtPX96");
827
+ assertUint128(liquidity, "liquidity");
828
+ assertUint256(amount, "amount");
829
+ if (amount === 0n) return sqrtPX96;
830
+ const numerator1 = liquidity << Q96_RESOLUTION;
831
+ if (add) {
832
+ const product = amount * sqrtPX96 & MAX_UINT2563;
833
+ const noOverflow = amount === 0n || product / amount === sqrtPX96;
834
+ if (noOverflow) {
835
+ const denominator = numerator1 + product;
836
+ if (denominator >= numerator1) {
837
+ return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
838
+ }
839
+ }
840
+ if (sqrtPX96 === 0n) throw new Error("SqrtPriceMath: sqrtPX96 == 0");
841
+ const denom = numerator1 / sqrtPX96 + amount;
842
+ return toUint160(divRoundingUp(numerator1, denom));
843
+ } else {
844
+ const product = amount * sqrtPX96 & MAX_UINT2563;
845
+ const noOverflow = amount === 0n || product / amount === sqrtPX96;
846
+ if (!noOverflow || numerator1 <= product) {
847
+ throw new Error("SqrtPriceMath: getNextSqrtPriceFromAmount0 denom <= 0");
848
+ }
849
+ const denominator = numerator1 - product;
850
+ return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
851
+ }
852
+ }
853
+ function getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amount, add) {
854
+ assertUint160(sqrtPX96, "sqrtPX96");
855
+ assertUint128(liquidity, "liquidity");
856
+ assertUint256(amount, "amount");
857
+ if (add) {
858
+ const quotient = amount <= MAX_UINT160 ? (amount << Q96_RESOLUTION) / liquidity : mulDiv(amount, Q96, liquidity);
859
+ return toUint160(sqrtPX96 + quotient);
860
+ } else {
861
+ const quotient = amount <= MAX_UINT160 ? divRoundingUp(amount << Q96_RESOLUTION, liquidity) : mulDivRoundingUp(amount, Q96, liquidity);
862
+ if (sqrtPX96 <= quotient) {
863
+ throw new Error("SqrtPriceMath: sqrtPX96 <= quotient");
864
+ }
865
+ return sqrtPX96 - quotient;
866
+ }
867
+ }
868
+ function getNextSqrtPriceFromInput(sqrtPX96, liquidity, amountIn, zeroForOne) {
869
+ if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
870
+ if (liquidity <= 0n)
871
+ throw new Error("SqrtPriceMath: liquidity must be > 0");
872
+ return zeroForOne ? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) : getNextSqrtPriceFromAmount1RoundingDown(
873
+ sqrtPX96,
874
+ liquidity,
875
+ amountIn,
876
+ true
877
+ );
878
+ }
879
+ function getNextSqrtPriceFromOutput(sqrtPX96, liquidity, amountOut, zeroForOne) {
880
+ if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
881
+ if (liquidity <= 0n)
882
+ throw new Error("SqrtPriceMath: liquidity must be > 0");
883
+ return zeroForOne ? getNextSqrtPriceFromAmount1RoundingDown(
884
+ sqrtPX96,
885
+ liquidity,
886
+ amountOut,
887
+ false
888
+ ) : getNextSqrtPriceFromAmount0RoundingUp(
889
+ sqrtPX96,
890
+ liquidity,
891
+ amountOut,
892
+ false
893
+ );
894
+ }
895
+ function getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
896
+ assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
897
+ assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
898
+ assertUint128(liquidity, "liquidity");
899
+ let a = sqrtRatioAX96;
900
+ let b = sqrtRatioBX96;
901
+ if (a > b) {
902
+ [a, b] = [b, a];
903
+ }
904
+ if (a === 0n) throw new Error("SqrtPriceMath: sqrtRatio must be > 0");
905
+ const numerator1 = liquidity << Q96_RESOLUTION;
906
+ const numerator2 = b - a;
907
+ if (roundUp) {
908
+ return divRoundingUp(mulDivRoundingUp(numerator1, numerator2, b), a);
909
+ }
910
+ return mulDiv(numerator1, numerator2, b) / a;
911
+ }
912
+ function getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
913
+ assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
914
+ assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
915
+ assertUint128(liquidity, "liquidity");
916
+ let a = sqrtRatioAX96;
917
+ let b = sqrtRatioBX96;
918
+ if (a > b) {
919
+ [a, b] = [b, a];
920
+ }
921
+ return roundUp ? mulDivRoundingUp(liquidity, b - a, Q96) : mulDiv(liquidity, b - a, Q96);
922
+ }
923
+ function getAmount0DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
924
+ const INT128_MIN2 = -(1n << 127n);
925
+ const INT128_MAX2 = (1n << 127n) - 1n;
926
+ if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
927
+ throw new Error("SqrtPriceMath: liquidity out of int128 range");
928
+ }
929
+ if (liquidity < 0n) {
930
+ return -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
931
+ }
932
+ return getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
933
+ }
934
+ function getAmount1DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
935
+ const INT128_MIN2 = -(1n << 127n);
936
+ const INT128_MAX2 = (1n << 127n) - 1n;
937
+ if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
938
+ throw new Error("SqrtPriceMath: liquidity out of int128 range");
939
+ }
940
+ if (liquidity < 0n) {
941
+ return -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
942
+ }
943
+ return getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
944
+ }
945
+
946
+ // src/math/liquidityMath.ts
947
+ var MAX_UINT1282 = (1n << 128n) - 1n;
948
+ var INT128_MAX = (1n << 127n) - 1n;
949
+ var INT128_MIN = -(1n << 127n);
950
+ function addDelta(x, y) {
951
+ if (x < 0n || x > MAX_UINT1282) {
952
+ throw new Error("LiquidityMath: x out of uint128 range");
953
+ }
954
+ if (y < INT128_MIN || y > INT128_MAX) {
955
+ throw new Error("LiquidityMath: y out of int128 range");
956
+ }
957
+ const z = x + y;
958
+ if (y < 0n) {
959
+ if (z < 0n) throw new Error("LS");
960
+ } else {
961
+ if (z > MAX_UINT1282) throw new Error("LA");
962
+ }
963
+ return z;
964
+ }
600
965
  // Annotate the CommonJS export names for ESM import in node:
601
966
  0 && (module.exports = {
602
967
  ActionBoxAddress,
@@ -616,6 +981,12 @@ var SdkError = class extends Error {
616
981
  KERNEL_POOL_USDC_USDT_Dev,
617
982
  KernelEventEmitterAddress,
618
983
  KernelManagerAddress,
984
+ MAX_SQRT_PRICE_LIMIT,
985
+ MAX_SQRT_RATIO,
986
+ MAX_TICK,
987
+ MIN_SQRT_PRICE_LIMIT,
988
+ MIN_SQRT_RATIO,
989
+ MIN_TICK,
619
990
  PERIPHERY_DEV,
620
991
  PERIPHERY_PROD,
621
992
  PERIPHERY_STAGING,
@@ -629,6 +1000,7 @@ var SdkError = class extends Error {
629
1000
  SkateGatewayAddress,
630
1001
  TaskPhase,
631
1002
  VM,
1003
+ addDelta,
632
1004
  base58ToBytes32,
633
1005
  base58ToHex,
634
1006
  bufferFromBase58,
@@ -639,17 +1011,29 @@ var SdkError = class extends Error {
639
1011
  fromBytes32Address,
640
1012
  getAllPoolInfo,
641
1013
  getAllPoolKeys,
1014
+ getAmount0Delta,
1015
+ getAmount0DeltaSigned,
1016
+ getAmount1Delta,
1017
+ getAmount1DeltaSigned,
642
1018
  getBlockExplorer,
1019
+ getNextSqrtPriceFromAmount0RoundingUp,
1020
+ getNextSqrtPriceFromAmount1RoundingDown,
1021
+ getNextSqrtPriceFromInput,
1022
+ getNextSqrtPriceFromOutput,
643
1023
  getOtherTokensInPairs,
644
1024
  getPeripheryDetailsByKernelPoolAndChainId,
645
1025
  getPoolInfoByKey,
1026
+ getSqrtRatioAtTick,
646
1027
  getSupportedChains,
647
1028
  getSupportedPairs,
648
1029
  getSupportedTokens,
1030
+ getTickAtSqrtRatio,
649
1031
  hexToBase58,
650
1032
  isHexString,
651
1033
  isHexStringLoose,
652
1034
  kernelPoolToPoolKey,
1035
+ mulDiv,
1036
+ mulDivRoundingUp,
653
1037
  normalizeMode,
654
1038
  skateAdapter,
655
1039
  toBase58,