@whetstone-research/doppler-sdk 1.0.1 → 1.0.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.
@@ -1,5 +1,5 @@
1
- import { MAX_SENTINEL_ALLOWLIST, MAX_ORACLE_OBSERVATIONS, ACCOUNT_DISCRIMINATORS, PROGRAM_ID, Q64_ONE, BPS_DENOM, TOKEN_PROGRAM_ID, INSTRUCTION_DISCRIMINATORS, ACCOUNT_ROLE_WRITABLE, ACCOUNT_ROLE_SIGNER, ACCOUNT_ROLE_READONLY, getPoolAddress, sortMints, getPositionAddress, getOracleAddress } from './chunk-T46KDO65.js';
2
- import { getAddressCodec, getBooleanCodec, getU8Codec, getU16Codec, getU32Codec, getU64Codec, getU128Codec, transformCodec, getArrayCodec, fixCodecSize, getBytesCodec, getStructCodec, getHiddenPrefixDecoder, getConstantDecoder, getOptionCodec, unwrapOption, mergeBytes } from '@solana/kit';
1
+ import { MAX_SENTINEL_ALLOWLIST, MAX_ORACLE_OBSERVATIONS, ACCOUNT_DISCRIMINATORS, CPMM_PROGRAM_ID, Q64_ONE, BPS_DENOM, TOKEN_PROGRAM_ADDRESS, INSTRUCTION_DISCRIMINATORS, getPoolAddress, sortMints, getPositionAddress, getOracleAddress } from './chunk-J57ROY36.js';
2
+ import { getAddressCodec, getBooleanCodec, getU8Codec, getU16Codec, getU32Codec, getU64Codec, getU128Codec, transformCodec, getArrayCodec, fixCodecSize, getBytesCodec, getStructCodec, getHiddenPrefixDecoder, getConstantDecoder, getOptionCodec, unwrapOption, mergeBytes, AccountRole } from '@solana/kit';
3
3
 
4
4
  var addressCodec = getAddressCodec();
5
5
  var boolCodec = getBooleanCodec();
@@ -176,7 +176,7 @@ var addLiquidityArgsWithOracleCodec = getStructCodec([
176
176
  ["minSharesOut", u128Codec],
177
177
  ["updateOracle", boolCodec]
178
178
  ]);
179
- transformCodec(
179
+ var addLiquidityArgsCodec = transformCodec(
180
180
  addLiquidityArgsWithOracleCodec,
181
181
  (value) => ({
182
182
  ...value,
@@ -247,6 +247,51 @@ var quoteToNumeraireArgsCodec = getStructCodec([
247
247
  ["useTwap", boolCodec],
248
248
  ["windowSeconds", u32Codec]
249
249
  ]);
250
+ function encodeSwapExactInArgs(args) {
251
+ return new Uint8Array(swapExactInArgsCodec.encode(args));
252
+ }
253
+ function encodeAddLiquidityArgs(args) {
254
+ return new Uint8Array(addLiquidityArgsCodec.encode(args));
255
+ }
256
+ function encodeRemoveLiquidityArgs(args) {
257
+ return new Uint8Array(removeLiquidityArgsCodec.encode(args));
258
+ }
259
+ function encodeCollectFeesArgs(args) {
260
+ return new Uint8Array(collectFeesArgsCodec.encode(args));
261
+ }
262
+ function encodeCollectProtocolFeesArgs(args) {
263
+ return new Uint8Array(collectProtocolFeesArgsCodec.encode(args));
264
+ }
265
+ function encodeCreatePositionArgs(args) {
266
+ return new Uint8Array(createPositionArgsCodec.encode(args));
267
+ }
268
+ function encodeInitializeConfigArgs(args) {
269
+ return new Uint8Array(initializeConfigArgsCodec.encode(args));
270
+ }
271
+ function encodeInitializePoolArgs(args) {
272
+ return new Uint8Array(initializePoolArgsCodec.encode(args));
273
+ }
274
+ function encodeInitializeOracleArgs(args) {
275
+ return new Uint8Array(initializeOracleArgsCodec.encode(args));
276
+ }
277
+ function encodeSetSentinelArgs(args) {
278
+ return new Uint8Array(setSentinelArgsCodec.encode(args));
279
+ }
280
+ function encodeSetFeesArgs(args) {
281
+ return new Uint8Array(setFeesArgsCodec.encode(args));
282
+ }
283
+ function encodeSetRouteArgs(args) {
284
+ return new Uint8Array(setRouteArgsCodec.encode(args));
285
+ }
286
+ function encodeTransferAdminArgs(args) {
287
+ return new Uint8Array(transferAdminArgsCodec.encode(args));
288
+ }
289
+ function encodeOracleConsultArgs(args) {
290
+ return new Uint8Array(oracleConsultArgsCodec.encode(args));
291
+ }
292
+ function encodeQuoteToNumeraireArgs(args) {
293
+ return new Uint8Array(quoteToNumeraireArgsCodec.encode(args));
294
+ }
250
295
 
251
296
  // src/solana/core/math.ts
252
297
  function q64ToNumber(q64) {
@@ -441,9 +486,7 @@ function calculateTwapNumber(cumulativeStart, cumulativeEnd, timestampStart, tim
441
486
  );
442
487
  return q64ToNumber(twapQ64);
443
488
  }
444
-
445
- // src/solana/instructions/collectFees.ts
446
- function createCollectFeesInstruction(accounts, args, programId = PROGRAM_ID) {
489
+ function createCollectFeesInstruction(accounts, args, programId = CPMM_PROGRAM_ID) {
447
490
  const {
448
491
  pool,
449
492
  position,
@@ -455,20 +498,20 @@ function createCollectFeesInstruction(accounts, args, programId = PROGRAM_ID) {
455
498
  token1Mint,
456
499
  user0,
457
500
  user1,
458
- tokenProgram = TOKEN_PROGRAM_ID
501
+ tokenProgram = TOKEN_PROGRAM_ADDRESS
459
502
  } = accounts;
460
503
  const keys = [
461
- { address: pool, role: ACCOUNT_ROLE_WRITABLE },
462
- { address: position, role: ACCOUNT_ROLE_WRITABLE },
463
- { address: owner, role: ACCOUNT_ROLE_SIGNER },
464
- { address: authority, role: ACCOUNT_ROLE_READONLY },
465
- { address: vault0, role: ACCOUNT_ROLE_WRITABLE },
466
- { address: vault1, role: ACCOUNT_ROLE_WRITABLE },
467
- { address: token0Mint, role: ACCOUNT_ROLE_READONLY },
468
- { address: token1Mint, role: ACCOUNT_ROLE_READONLY },
469
- { address: user0, role: ACCOUNT_ROLE_WRITABLE },
470
- { address: user1, role: ACCOUNT_ROLE_WRITABLE },
471
- { address: tokenProgram, role: ACCOUNT_ROLE_READONLY }
504
+ { address: pool, role: AccountRole.WRITABLE },
505
+ { address: position, role: AccountRole.WRITABLE },
506
+ { address: owner, role: AccountRole.READONLY_SIGNER },
507
+ { address: authority, role: AccountRole.READONLY },
508
+ { address: vault0, role: AccountRole.WRITABLE },
509
+ { address: vault1, role: AccountRole.WRITABLE },
510
+ { address: token0Mint, role: AccountRole.READONLY },
511
+ { address: token1Mint, role: AccountRole.READONLY },
512
+ { address: user0, role: AccountRole.WRITABLE },
513
+ { address: user1, role: AccountRole.WRITABLE },
514
+ { address: tokenProgram, role: AccountRole.READONLY }
472
515
  ];
473
516
  const data = encodeInstructionData(
474
517
  INSTRUCTION_DISCRIMINATORS.collectFees,
@@ -510,7 +553,7 @@ async function fetchPool(rpc, address, config) {
510
553
  return decodePool(base64ToBytes(response.value.data[0]));
511
554
  }
512
555
  async function fetchAllPools(rpc, config) {
513
- const programId = config?.programId ?? PROGRAM_ID;
556
+ const programId = config?.programId ?? CPMM_PROGRAM_ID;
514
557
  const discriminatorFilter = {
515
558
  memcmp: {
516
559
  offset: 0n,
@@ -539,7 +582,7 @@ async function fetchAllPools(rpc, config) {
539
582
  return pools;
540
583
  }
541
584
  async function getPoolByMints(rpc, mint0, mint1, config) {
542
- const programId = config?.programId ?? PROGRAM_ID;
585
+ const programId = config?.programId ?? CPMM_PROGRAM_ID;
543
586
  const [poolAddress] = await getPoolAddress(mint0, mint1, programId);
544
587
  const pool = await fetchPool(rpc, poolAddress, config);
545
588
  if (!pool) {
@@ -567,7 +610,7 @@ async function poolExists(rpc, mint0, mint1, config) {
567
610
  const result = await getPoolByMints(rpc, mint0, mint1, config);
568
611
  return result !== null;
569
612
  }
570
- async function getPoolAddressFromMints(mint0, mint1, programId = PROGRAM_ID) {
613
+ async function getPoolAddressFromMints(mint0, mint1, programId = CPMM_PROGRAM_ID) {
571
614
  const [token0, token1] = sortMints(mint0, mint1);
572
615
  const [poolAddress] = await getPoolAddress(mint0, mint1, programId);
573
616
  return {
@@ -617,7 +660,7 @@ async function fetchPosition(rpc, address, config) {
617
660
  return decodePosition(base64ToBytes2(response.value.data[0]));
618
661
  }
619
662
  async function fetchUserPositions(rpc, owner, pool, config) {
620
- const programId = config?.programId ?? PROGRAM_ID;
663
+ const programId = config?.programId ?? CPMM_PROGRAM_ID;
621
664
  const filters = [
622
665
  // Discriminator filter (first 8 bytes)
623
666
  {
@@ -668,7 +711,7 @@ async function fetchUserPositions(rpc, owner, pool, config) {
668
711
  return positions;
669
712
  }
670
713
  async function fetchPoolPositions(rpc, pool, config) {
671
- const programId = config?.programId ?? PROGRAM_ID;
714
+ const programId = config?.programId ?? CPMM_PROGRAM_ID;
672
715
  const filters = [
673
716
  // Discriminator filter
674
717
  {
@@ -736,7 +779,7 @@ function getPositionValue(pool, position) {
736
779
  };
737
780
  }
738
781
  async function fetchPositionByParams(rpc, pool, owner, positionId, config) {
739
- const programId = config?.programId ?? PROGRAM_ID;
782
+ const programId = config?.programId ?? CPMM_PROGRAM_ID;
740
783
  const [address] = await getPositionAddress(
741
784
  pool,
742
785
  owner,
@@ -752,7 +795,7 @@ async function fetchPositionByParams(rpc, pool, owner, positionId, config) {
752
795
  account: position
753
796
  };
754
797
  }
755
- async function getPositionAddressFromParams(pool, owner, positionId, programId = PROGRAM_ID) {
798
+ async function getPositionAddressFromParams(pool, owner, positionId, programId = CPMM_PROGRAM_ID) {
756
799
  const [address] = await getPositionAddress(
757
800
  pool,
758
801
  owner,
@@ -804,7 +847,7 @@ async function fetchOracle(rpc, address, config) {
804
847
  return decodeOracleState(base64ToBytes3(response.value.data[0]));
805
848
  }
806
849
  async function getOracleForPool(rpc, pool, config) {
807
- const programId = config?.programId ?? PROGRAM_ID;
850
+ const programId = config?.programId ?? CPMM_PROGRAM_ID;
808
851
  const [oracleAddress] = await getOracleAddress(pool, programId);
809
852
  const oracle = await fetchOracle(rpc, oracleAddress, config);
810
853
  if (!oracle) {
@@ -815,7 +858,7 @@ async function getOracleForPool(rpc, pool, config) {
815
858
  account: oracle
816
859
  };
817
860
  }
818
- async function getOracleAddressFromPool(pool, programId = PROGRAM_ID) {
861
+ async function getOracleAddressFromPool(pool, programId = CPMM_PROGRAM_ID) {
819
862
  const [address] = await getOracleAddress(pool, programId);
820
863
  return address;
821
864
  }
@@ -939,7 +982,7 @@ function getOracleBufferStats(oracle) {
939
982
  };
940
983
  }
941
984
  async function fetchOraclesBatch(rpc, pools, config) {
942
- const programId = config?.programId ?? PROGRAM_ID;
985
+ const programId = config?.programId ?? CPMM_PROGRAM_ID;
943
986
  const oracles = /* @__PURE__ */ new Map();
944
987
  const oracleAddresses = await Promise.all(
945
988
  pools.map((pool) => getOracleAddress(pool, programId))
@@ -969,6 +1012,6 @@ function comparePoolAndOraclePrices(pool, oracle) {
969
1012
  };
970
1013
  }
971
1014
 
972
- export { MAX_FEE_AMOUNT, calculateAccruedFees, calculateTwap, calculateTwapNumber, ceilDiv, collectProtocolFeesArgsCodec, comparePoolAndOraclePrices, computePrice0Q64, computePrice1Q64, consultTwap, createCollectFeesInstruction, createPositionArgsCodec, decodeAmmConfig, decodeOracleState, decodePool, decodePosition, encodeInstructionData, fetchAllPools, fetchOracle, fetchOraclesBatch, fetchPool, fetchPoolPositions, fetchPoolsBatch, fetchPosition, fetchPositionByParams, fetchPositionsBatch, fetchUserPositions, filterActivePositions, filterPoolsByMint, getAddLiquidityQuote, getK, getOracleAddressFromPool, getOracleAge, getOracleBufferStats, getOracleDeviation, getOracleForPool, getOracleSpotPrices, getPendingFees, getPoolAddressFromMints, getPoolByMints, getPositionAddressFromParams, getPositionValue, getRemoveLiquidityQuote, getSpotPrice0, getSpotPrice1, getSwapQuote, getSwapQuoteExactOut, getTvl, initializeConfigArgsCodec, initializeOracleArgsCodec, initializePoolArgsCodec, isOracleStale, isqrt, maxBigInt, minBigInt, numberToQ64, oracleConsultArgsCodec, poolExists, q64Div, q64Mul, q64ToNumber, quoteToNumeraireArgsCodec, ratioToNumber, removeLiquidityArgsCodec, setFeesArgsCodec, setRouteArgsCodec, setSentinelArgsCodec, sortPoolsByReserves, sortPositionsByShares, swapExactInArgsCodec, transferAdminArgsCodec };
973
- //# sourceMappingURL=chunk-LIXSTCMM.js.map
974
- //# sourceMappingURL=chunk-LIXSTCMM.js.map
1015
+ export { MAX_FEE_AMOUNT, addLiquidityArgsCodec, ammConfigDataCodec, calculateAccruedFees, calculateTwap, calculateTwapNumber, ceilDiv, collectFeesArgsCodec, collectProtocolFeesArgsCodec, comparePoolAndOraclePrices, computePrice0Q64, computePrice1Q64, consultTwap, createCollectFeesInstruction, createPositionArgsCodec, decodeAmmConfig, decodeOracleState, decodePool, decodePosition, encodeAddLiquidityArgs, encodeCollectFeesArgs, encodeCollectProtocolFeesArgs, encodeCreatePositionArgs, encodeInitializeConfigArgs, encodeInitializeOracleArgs, encodeInitializePoolArgs, encodeInstructionData, encodeOracleConsultArgs, encodeQuoteToNumeraireArgs, encodeRemoveLiquidityArgs, encodeSetFeesArgs, encodeSetRouteArgs, encodeSetSentinelArgs, encodeSwapExactInArgs, encodeTransferAdminArgs, fetchAllPools, fetchOracle, fetchOraclesBatch, fetchPool, fetchPoolPositions, fetchPoolsBatch, fetchPosition, fetchPositionByParams, fetchPositionsBatch, fetchUserPositions, filterActivePositions, filterPoolsByMint, getAddLiquidityQuote, getK, getOracleAddressFromPool, getOracleAge, getOracleBufferStats, getOracleDeviation, getOracleForPool, getOracleSpotPrices, getPendingFees, getPoolAddressFromMints, getPoolByMints, getPositionAddressFromParams, getPositionValue, getRemoveLiquidityQuote, getSpotPrice0, getSpotPrice1, getSwapQuote, getSwapQuoteExactOut, getTvl, initializeConfigArgsCodec, initializeOracleArgsCodec, initializePoolArgsCodec, isOracleStale, isqrt, maxBigInt, minBigInt, numberToQ64, observationCodec, oracleConsultArgsCodec, oracleStateDataCodec, poolDataCodec, poolExists, positionDataCodec, q64Div, q64Mul, q64ToNumber, quoteToNumeraireArgsCodec, ratioToNumber, removeLiquidityArgsCodec, setFeesArgsCodec, setRouteArgsCodec, setSentinelArgsCodec, sortPoolsByReserves, sortPositionsByShares, swapExactInArgsCodec, transferAdminArgsCodec };
1016
+ //# sourceMappingURL=chunk-BSHIMMA4.js.map
1017
+ //# sourceMappingURL=chunk-BSHIMMA4.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/solana/core/codecs.ts","../src/solana/core/math.ts","../src/solana/instructions/collectFees.ts","../src/solana/client/pool.ts","../src/solana/client/position.ts","../src/solana/client/oracle.ts"],"names":["sharesOut","byt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MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA,GAAI,CAAA;AAEvE,EAAA,MAAM,YAAA,GAAe,WAAA,CAAY,MAAA,CAAO,cAAc,CAAA;AAEtD,EAAA,MAAM,gBACJ,YAAA,GAAe,CAAA,GAAA,CAAM,UAAA,GAAa,YAAA,IAAgB,eAAgB,GAAA,GAAM,CAAA;AAE1E,EAAA,OAAO;AAAA,IACL,UAAA;AAAA,IACA,YAAA;AAAA,IACA;AAAA,GACF;AACF","file":"chunk-BSHIMMA4.js","sourcesContent":["/**\n * Borsh serialization/deserialization for CPMM accounts and instructions\n *\n * This module provides encoding/decoding utilities for program accounts\n * and instruction data using Borsh-compatible binary formats.\n */\n\nimport { getAddressCodec, type Address } from '@solana/kit';\nimport type { ReadonlyUint8Array } from '@solana/kit';\nimport {\n fixCodecSize,\n getArrayCodec,\n getBooleanCodec,\n getBytesCodec,\n getConstantDecoder,\n getHiddenPrefixDecoder,\n getOptionCodec,\n getStructCodec,\n getU8Codec,\n getU16Codec,\n getU32Codec,\n getU64Codec,\n getU128Codec,\n mergeBytes,\n transformCodec,\n unwrapOption,\n type Codec,\n} from '@solana/kit';\nimport {\n MAX_ORACLE_OBSERVATIONS,\n MAX_SENTINEL_ALLOWLIST,\n ACCOUNT_DISCRIMINATORS,\n} from './constants.js';\nimport type {\n AmmConfig,\n Pool,\n Position,\n OracleState,\n Observation,\n SwapExactInArgs,\n AddLiquidityArgs,\n RemoveLiquidityArgs,\n CollectFeesArgs,\n CollectProtocolFeesArgs,\n CreatePositionArgs,\n InitializeConfigArgs,\n InitializePoolArgs,\n InitializeOracleArgs,\n SetSentinelArgs,\n SetFeesArgs,\n SetRouteArgs,\n TransferAdminArgs,\n OracleConsultArgs,\n QuoteToNumeraireArgs,\n} from './types.js';\n\nconst addressCodec = getAddressCodec();\nconst boolCodec = getBooleanCodec();\nconst u8Codec = getU8Codec();\nconst u16Codec = getU16Codec();\nconst u32Codec = getU32Codec();\nconst u64Codec = getU64Codec();\nconst u128Codec = getU128Codec();\nconst u256Codec: Codec<bigint> = transformCodec(\n getArrayCodec(u64Codec, { size: 4 }),\n (value) => {\n if (value < 0n) {\n throw new Error('u256 cannot be negative');\n }\n if (value >> 256n !== 0n) {\n throw new Error('u256 overflow');\n }\n const mask = (1n << 64n) - 1n;\n return [\n value & mask,\n (value >> 64n) & mask,\n (value >> 128n) & mask,\n (value >> 192n) & mask,\n ];\n },\n (value) => {\n let out = 0n;\n for (let i = 0; i < 4; i += 1) {\n out |= value[i] << (64n * BigInt(i));\n }\n return out;\n },\n);\n\nconst reservedBytesCodec = transformCodec(\n fixCodecSize(getBytesCodec(), 7),\n (value: Uint8Array) => value,\n (value) => new Uint8Array(value),\n);\n\n// ============================================================================\n// Account Codecs\n// ============================================================================\n\nexport const observationCodec: Codec<Observation> = getStructCodec([\n ['timestamp', u32Codec],\n ['price0Cumulative', u256Codec],\n ['price1Cumulative', u256Codec],\n]);\n\nexport const ammConfigDataCodec: Codec<AmmConfig> = getStructCodec([\n ['admin', addressCodec],\n ['paused', boolCodec],\n ['numeraireMint', addressCodec],\n ['sentinelAllowlistLen', u8Codec],\n [\n 'sentinelAllowlist',\n getArrayCodec(addressCodec, { size: MAX_SENTINEL_ALLOWLIST }),\n ],\n ['maxSwapFeeBps', u16Codec],\n ['maxFeeSplitBps', u16Codec],\n ['maxRouteHops', u8Codec],\n ['protocolFeeEnabled', boolCodec],\n ['protocolFeeBps', u16Codec],\n ['version', u8Codec],\n ['reserved', reservedBytesCodec],\n]);\n\nexport const poolDataCodec: Codec<Pool> = getStructCodec([\n ['config', addressCodec],\n ['token0Mint', addressCodec],\n ['token1Mint', addressCodec],\n ['vault0', addressCodec],\n ['vault1', addressCodec],\n ['authority', addressCodec],\n ['bump', u8Codec],\n ['reserve0', u64Codec],\n ['reserve1', u64Codec],\n ['totalShares', u128Codec],\n ['swapFeeBps', u16Codec],\n ['feeSplitBps', u16Codec],\n ['feeGrowthGlobal0Q64', u128Codec],\n ['feeGrowthGlobal1Q64', u128Codec],\n ['feesUnclaimed0', u64Codec],\n ['feesUnclaimed1', u64Codec],\n ['sentinelProgram', addressCodec],\n ['sentinelFlags', u32Codec],\n ['numeraireMint', addressCodec],\n ['liquidityMeasureSide', u8Codec],\n ['routeNextPool', addressCodec],\n ['routeBridgeMint', addressCodec],\n ['kLast', u128Codec],\n ['protocolPosition', addressCodec],\n ['locked', u8Codec],\n ['version', u8Codec],\n ['reserved', reservedBytesCodec],\n]);\n\nexport const positionDataCodec: Codec<Position> = getStructCodec([\n ['pool', addressCodec],\n ['owner', addressCodec],\n ['positionId', u64Codec],\n ['shares', u128Codec],\n ['feeGrowthLast0Q64', u128Codec],\n ['feeGrowthLast1Q64', u128Codec],\n ['feeOwed0', u64Codec],\n ['feeOwed1', u64Codec],\n ['version', u8Codec],\n ['reserved', reservedBytesCodec],\n]);\n\nexport const oracleStateDataCodec: Codec<OracleState> = getStructCodec([\n ['pool', addressCodec],\n ['initialized', boolCodec],\n ['maxPriceChangeRatioQ64', u128Codec],\n ['lastSlot', u64Codec],\n ['truncPrice0Q64', u128Codec],\n ['truncPrice1Q64', u128Codec],\n ['deviation0Q64', u128Codec],\n ['deviation1Q64', u128Codec],\n ['price0Cumulative', u256Codec],\n ['price1Cumulative', u256Codec],\n ['lastTimestamp', u32Codec],\n ['lastObservationTimestamp', u32Codec],\n ['observationIntervalSec', u32Codec],\n ['observationIndex', u16Codec],\n [\n 'observations',\n getArrayCodec(observationCodec, { size: MAX_ORACLE_OBSERVATIONS }),\n ],\n ['version', u8Codec],\n ['reserved', reservedBytesCodec],\n]);\n\nconst ammConfigDecoder = getHiddenPrefixDecoder(ammConfigDataCodec, [\n getConstantDecoder(ACCOUNT_DISCRIMINATORS.AmmConfig),\n]);\nconst poolDecoder = getHiddenPrefixDecoder(poolDataCodec, [\n getConstantDecoder(ACCOUNT_DISCRIMINATORS.Pool),\n]);\nconst positionDecoder = getHiddenPrefixDecoder(positionDataCodec, [\n getConstantDecoder(ACCOUNT_DISCRIMINATORS.Position),\n]);\nconst oracleStateDecoder = getHiddenPrefixDecoder(oracleStateDataCodec, [\n getConstantDecoder(ACCOUNT_DISCRIMINATORS.OracleState),\n]);\n\n/**\n * Decode AmmConfig from raw account data (including discriminator)\n */\nexport function decodeAmmConfig(data: ReadonlyUint8Array): AmmConfig {\n return ammConfigDecoder.decode(data);\n}\n\n/**\n * Decode Pool from raw account data (including discriminator)\n */\nexport function decodePool(data: ReadonlyUint8Array): Pool {\n return poolDecoder.decode(data);\n}\n\n/**\n * Decode Position from raw account data (including discriminator)\n */\nexport function decodePosition(data: ReadonlyUint8Array): Position {\n return positionDecoder.decode(data);\n}\n\n/**\n * Decode OracleState from raw account data (including discriminator)\n */\nexport function decodeOracleState(data: ReadonlyUint8Array): OracleState {\n return oracleStateDecoder.decode(data);\n}\n\n// ============================================================================\n// Instruction Encoders\n// ============================================================================\n\n/**\n * Encode instruction data with discriminator prefix\n */\nexport function encodeInstructionData<T>(\n discriminator: Uint8Array,\n codec?: { encode: (args: T) => ReadonlyUint8Array | Uint8Array },\n args?: T,\n): Uint8Array {\n const encodedArgs = (() => {\n if (!codec) {\n return new Uint8Array();\n }\n if (args === undefined) {\n throw new Error('Instruction args are required for codec encoders');\n }\n return new Uint8Array(codec.encode(args));\n })();\n\n return mergeBytes([discriminator, encodedArgs]);\n}\n\ntype AddLiquidityArgsWithOracle = AddLiquidityArgs & { updateOracle?: boolean };\n\nconst optionAddressCodec: Codec<Address | null> = transformCodec(\n getOptionCodec(addressCodec, { prefix: u8Codec }),\n (value: Address | null) => value,\n (value) => unwrapOption(value),\n);\n\nexport const swapExactInArgsCodec = getStructCodec([\n ['amountIn', u64Codec],\n ['minAmountOut', u64Codec],\n ['direction', u8Codec],\n ['updateOracle', boolCodec],\n]) as Codec<SwapExactInArgs>;\n\nconst addLiquidityArgsWithOracleCodec: Codec<\n AddLiquidityArgs & { updateOracle: boolean }\n> = getStructCodec([\n ['amount0Max', u64Codec],\n ['amount1Max', u64Codec],\n ['minSharesOut', u128Codec],\n ['updateOracle', boolCodec],\n]);\n\nexport const addLiquidityArgsCodec: Codec<AddLiquidityArgsWithOracle> =\n transformCodec(\n addLiquidityArgsWithOracleCodec,\n (value: AddLiquidityArgsWithOracle) => ({\n ...value,\n updateOracle: value.updateOracle ?? false,\n }),\n (value) => value,\n );\n\nexport const removeLiquidityArgsCodec: Codec<RemoveLiquidityArgs> =\n getStructCodec([\n ['sharesIn', u128Codec],\n ['minAmount0Out', u64Codec],\n ['minAmount1Out', u64Codec],\n ['updateOracle', boolCodec],\n ]);\n\nexport const collectFeesArgsCodec: Codec<CollectFeesArgs> = getStructCodec([\n ['max0', u64Codec],\n ['max1', u64Codec],\n]);\n\nexport const collectProtocolFeesArgsCodec: Codec<CollectProtocolFeesArgs> =\n getStructCodec([\n ['max0', u64Codec],\n ['max1', u64Codec],\n ]);\n\nexport const createPositionArgsCodec: Codec<CreatePositionArgs> =\n getStructCodec([['positionId', u64Codec]]);\n\nexport const initializeConfigArgsCodec: Codec<InitializeConfigArgs> =\n getStructCodec([\n ['admin', addressCodec],\n ['numeraireMint', addressCodec],\n ['maxSwapFeeBps', u16Codec],\n ['maxFeeSplitBps', u16Codec],\n ['maxRouteHops', u8Codec],\n ['protocolFeeEnabled', boolCodec],\n ['protocolFeeBps', u16Codec],\n ['sentinelAllowlist', getArrayCodec(addressCodec, { size: u32Codec })],\n ]);\n\nexport const initializePoolArgsCodec: Codec<InitializePoolArgs> =\n getStructCodec([\n ['mintA', addressCodec],\n ['mintB', addressCodec],\n ['initialSwapFeeBps', u16Codec],\n ['initialFeeSplitBps', u16Codec],\n ['liquidityMeasureSide', u8Codec],\n ['numeraireMintOverride', optionAddressCodec],\n ]);\n\nexport const initializeOracleArgsCodec: Codec<InitializeOracleArgs> =\n getStructCodec([\n ['maxPriceChangeRatioQ64', u128Codec],\n ['observationIntervalSec', u32Codec],\n ['numObservations', u16Codec],\n ]);\n\nexport const setSentinelArgsCodec: Codec<SetSentinelArgs> = getStructCodec([\n ['sentinelProgram', addressCodec],\n ['sentinelFlags', u32Codec],\n]);\n\nexport const setFeesArgsCodec: Codec<SetFeesArgs> = getStructCodec([\n ['swapFeeBps', u16Codec],\n ['feeSplitBps', u16Codec],\n]);\n\nexport const setRouteArgsCodec: Codec<SetRouteArgs> = getStructCodec([\n ['routeNextPool', addressCodec],\n ['routeBridgeMint', addressCodec],\n]);\n\nexport const transferAdminArgsCodec: Codec<TransferAdminArgs> = getStructCodec([\n ['newAdmin', addressCodec],\n]);\n\nexport const oracleConsultArgsCodec: Codec<OracleConsultArgs> = getStructCodec([\n ['windowSeconds', u32Codec],\n]);\n\nexport const quoteToNumeraireArgsCodec: Codec<QuoteToNumeraireArgs> =\n getStructCodec([\n ['amount', u128Codec],\n ['side', u8Codec],\n ['maxHops', u8Codec],\n ['useTwap', boolCodec],\n ['windowSeconds', u32Codec],\n ]);\n\n/** Encode SwapExactIn args */\nexport function encodeSwapExactInArgs(args: SwapExactInArgs): Uint8Array {\n return new Uint8Array(swapExactInArgsCodec.encode(args));\n}\n\n/** Encode AddLiquidity args */\nexport function encodeAddLiquidityArgs(\n args: AddLiquidityArgsWithOracle,\n): Uint8Array {\n return new Uint8Array(addLiquidityArgsCodec.encode(args));\n}\n\n/** Encode RemoveLiquidity args */\nexport function encodeRemoveLiquidityArgs(\n args: RemoveLiquidityArgs,\n): Uint8Array {\n return new Uint8Array(removeLiquidityArgsCodec.encode(args));\n}\n\n/** Encode CollectFees args */\nexport function encodeCollectFeesArgs(args: CollectFeesArgs): Uint8Array {\n return new Uint8Array(collectFeesArgsCodec.encode(args));\n}\n\n/** Encode CollectProtocolFees args */\nexport function encodeCollectProtocolFeesArgs(\n args: CollectProtocolFeesArgs,\n): Uint8Array {\n return new Uint8Array(collectProtocolFeesArgsCodec.encode(args));\n}\n\n/** Encode CreatePosition args */\nexport function encodeCreatePositionArgs(args: CreatePositionArgs): Uint8Array {\n return new Uint8Array(createPositionArgsCodec.encode(args));\n}\n\n/** Encode InitializeConfig args */\nexport function encodeInitializeConfigArgs(\n args: InitializeConfigArgs,\n): Uint8Array {\n return new Uint8Array(initializeConfigArgsCodec.encode(args));\n}\n\n/** Encode InitializePool args */\nexport function encodeInitializePoolArgs(args: InitializePoolArgs): Uint8Array {\n return new Uint8Array(initializePoolArgsCodec.encode(args));\n}\n\n/** Encode InitializeOracle args */\nexport function encodeInitializeOracleArgs(\n args: InitializeOracleArgs,\n): Uint8Array {\n return new Uint8Array(initializeOracleArgsCodec.encode(args));\n}\n\n/** Encode SetSentinel args */\nexport function encodeSetSentinelArgs(args: SetSentinelArgs): Uint8Array {\n return new Uint8Array(setSentinelArgsCodec.encode(args));\n}\n\n/** Encode SetFees args */\nexport function encodeSetFeesArgs(args: SetFeesArgs): Uint8Array {\n return new Uint8Array(setFeesArgsCodec.encode(args));\n}\n\n/** Encode SetRoute args */\nexport function encodeSetRouteArgs(args: SetRouteArgs): Uint8Array {\n return new Uint8Array(setRouteArgsCodec.encode(args));\n}\n\n/** Encode TransferAdmin args */\nexport function encodeTransferAdminArgs(args: TransferAdminArgs): Uint8Array {\n return new Uint8Array(transferAdminArgsCodec.encode(args));\n}\n\n/** Encode OracleConsult args */\nexport function encodeOracleConsultArgs(args: OracleConsultArgs): Uint8Array {\n return new Uint8Array(oracleConsultArgsCodec.encode(args));\n}\n\n/** Encode QuoteToNumeraire args */\nexport function encodeQuoteToNumeraireArgs(\n args: QuoteToNumeraireArgs,\n): Uint8Array {\n return new Uint8Array(quoteToNumeraireArgsCodec.encode(args));\n}\n","import { BPS_DENOM, Q64_ONE } from './constants.js';\nimport type {\n Pool,\n SwapQuote,\n SwapQuoteExactOut,\n AddLiquidityQuote,\n RemoveLiquidityQuote,\n SwapDirection,\n} from './types.js';\n\n// ============================================================================\n// Q64.64 Fixed-Point Arithmetic\n// ============================================================================\n\n/**\n * Convert a Q64.64 fixed-point number to a decimal number\n */\nexport function q64ToNumber(q64: bigint): number {\n // Split into integer and fractional parts for precision\n const intPart = q64 >> 64n;\n const fracPart = q64 & ((1n << 64n) - 1n);\n return Number(intPart) + Number(fracPart) / Number(Q64_ONE);\n}\n\n/**\n * Convert a decimal number to Q64.64 fixed-point\n */\nexport function numberToQ64(n: number): bigint {\n const intPart = Math.floor(n);\n const fracPart = n - intPart;\n return (\n (BigInt(intPart) << 64n) + BigInt(Math.round(fracPart * Number(Q64_ONE)))\n );\n}\n\n/**\n * Multiply two Q64.64 numbers, returning Q64.64 result\n */\nexport function q64Mul(a: bigint, b: bigint): bigint {\n return (a * b) >> 64n;\n}\n\n/**\n * Divide two Q64.64 numbers, returning Q64.64 result\n */\nexport function q64Div(a: bigint, b: bigint): bigint {\n return (a << 64n) / b;\n}\n\n/**\n * Compute spot price (reserve1/reserve0) as Q64.64\n * Price of token0 denominated in token1\n */\nexport function computePrice0Q64(reserve0: bigint, reserve1: bigint): bigint {\n if (reserve0 === 0n) return 0n;\n return (reserve1 << 64n) / reserve0;\n}\n\n/**\n * Compute spot price (reserve0/reserve1) as Q64.64\n * Price of token1 denominated in token0\n */\nexport function computePrice1Q64(reserve0: bigint, reserve1: bigint): bigint {\n if (reserve1 === 0n) return 0n;\n return (reserve0 << 64n) / reserve1;\n}\n\n// ============================================================================\n// Integer Math Helpers\n// ============================================================================\n\n/**\n * Integer square root (floor)\n */\nexport function isqrt(n: bigint): bigint {\n if (n < 0n) throw new Error('isqrt: negative input');\n if (n === 0n) return 0n;\n\n let x = n;\n let y = (x + 1n) / 2n;\n\n while (y < x) {\n x = y;\n y = (x + n / x) / 2n;\n }\n\n return x;\n}\n\n/**\n * Ceiling division: ceil(a / b)\n */\nexport function ceilDiv(a: bigint, b: bigint): bigint {\n return (a + b - 1n) / b;\n}\n\n/**\n * Minimum of two bigints\n */\nexport function minBigInt(a: bigint, b: bigint): bigint {\n return a < b ? a : b;\n}\n\n/**\n * Maximum of two bigints\n */\nexport function maxBigInt(a: bigint, b: bigint): bigint {\n return a > b ? a : b;\n}\n\n/**\n * Convert a ratio of two bigints to a decimal number using Q64.64 precision\n */\nexport function ratioToNumber(numerator: bigint, denominator: bigint): number {\n if (denominator === 0n) return 0;\n const q64 = (numerator << 64n) / denominator;\n return q64ToNumber(q64);\n}\n\n// ============================================================================\n// Swap Quote Calculations\n// ============================================================================\n\n/**\n * Calculate swap output and fees for exact input amount\n *\n * Fee calculation (from spec):\n * - fee_total = floor(amount_in * fee_bps / 10_000)\n * - fee_dist = floor(fee_total * split_bps / 10_000)\n * - fee_comp = fee_total - fee_dist\n * - amount_in_eff = amount_in - fee_total\n *\n * CPMM output:\n * - amount_out = floor(amount_in_eff * reserve_out / (reserve_in + amount_in_eff))\n */\nexport function getSwapQuote(\n pool: Pool,\n amountIn: bigint,\n direction: SwapDirection,\n): SwapQuote {\n if (amountIn === 0n) {\n return {\n amountOut: 0n,\n feeTotal: 0n,\n feeDist: 0n,\n feeComp: 0n,\n priceImpact: 0,\n executionPrice: 0,\n };\n }\n\n // Get reserves based on direction\n const [reserveIn, reserveOut] =\n direction === 0\n ? [pool.reserve0, pool.reserve1]\n : [pool.reserve1, pool.reserve0];\n\n if (reserveIn === 0n || reserveOut === 0n) {\n throw new Error('Pool has zero liquidity');\n }\n\n // Calculate fees\n const feeTotal = (amountIn * BigInt(pool.swapFeeBps)) / BPS_DENOM;\n const feeDist = (feeTotal * BigInt(pool.feeSplitBps)) / BPS_DENOM;\n const feeComp = feeTotal - feeDist;\n const amountInEff = amountIn - feeTotal;\n\n // CPMM output calculation\n const amountOut = (amountInEff * reserveOut) / (reserveIn + amountInEff);\n\n // Calculate price impact\n const spotPrice = ratioToNumber(reserveOut, reserveIn);\n const executionPrice = ratioToNumber(amountOut, amountIn);\n const priceImpact =\n spotPrice === 0 ? 0 : Math.abs(spotPrice - executionPrice) / spotPrice;\n\n return {\n amountOut,\n feeTotal,\n feeDist,\n feeComp,\n priceImpact,\n executionPrice,\n };\n}\n\n/**\n * Calculate input amount needed for exact output (reverse quote)\n */\nexport function getSwapQuoteExactOut(\n pool: Pool,\n amountOut: bigint,\n direction: SwapDirection,\n): SwapQuoteExactOut {\n // Get reserves based on direction\n const [reserveIn, reserveOut] =\n direction === 0\n ? [pool.reserve0, pool.reserve1]\n : [pool.reserve1, pool.reserve0];\n\n if (reserveIn === 0n || reserveOut === 0n) {\n throw new Error('Pool has zero liquidity');\n }\n\n if (amountOut >= reserveOut) {\n throw new Error('Insufficient liquidity for output amount');\n }\n\n // Reverse CPMM formula:\n // amountOut = amountInEff * reserveOut / (reserveIn + amountInEff)\n // => amountInEff = amountOut * reserveIn / (reserveOut - amountOut)\n const amountInEff = ceilDiv(amountOut * reserveIn, reserveOut - amountOut);\n\n // Reverse fee calculation:\n // amountInEff = amountIn - feeTotal = amountIn * (1 - feeBps/10000)\n // => amountIn = amountInEff * 10000 / (10000 - feeBps)\n const amountIn = ceilDiv(\n amountInEff * BPS_DENOM,\n BPS_DENOM - BigInt(pool.swapFeeBps),\n );\n const feeTotal = amountIn - amountInEff;\n\n return { amountIn, feeTotal };\n}\n\n// ============================================================================\n// Liquidity Quote Calculations\n// ============================================================================\n\n/**\n * Calculate shares and actual amounts for adding liquidity\n *\n * From spec:\n * - If initial: shares = floor_sqrt(amount0 * amount1)\n * - Else: shares = min(amount0 * totalShares / reserve0, amount1 * totalShares / reserve1)\n */\nexport function getAddLiquidityQuote(\n pool: Pool,\n amount0Max: bigint,\n amount1Max: bigint,\n): AddLiquidityQuote {\n if (pool.totalShares === 0n) {\n // Initial liquidity deposit\n const rawShares = isqrt(amount0Max * amount1Max);\n if (rawShares === 0n) {\n throw new Error('Initial liquidity too small');\n }\n const sharesOut = rawShares;\n\n return {\n sharesOut,\n amount0: amount0Max,\n amount1: amount1Max,\n poolShare: ratioToNumber(sharesOut, rawShares),\n };\n }\n\n // Existing pool - calculate proportional deposit\n if (pool.reserve0 === 0n || pool.reserve1 === 0n) {\n throw new Error('Invalid pool state: zero reserves with non-zero shares');\n }\n\n // Calculate shares for each token amount\n const shares0 = (amount0Max * pool.totalShares) / pool.reserve0;\n const shares1 = (amount1Max * pool.totalShares) / pool.reserve1;\n const sharesOut = minBigInt(shares0, shares1);\n\n // Calculate actual amounts needed for exact ratio\n const amount0 = ceilDiv(sharesOut * pool.reserve0, pool.totalShares);\n const amount1 = ceilDiv(sharesOut * pool.reserve1, pool.totalShares);\n\n // Calculate pool share after deposit\n const newTotalShares = pool.totalShares + sharesOut;\n const poolShare = ratioToNumber(sharesOut, newTotalShares);\n\n return {\n sharesOut,\n amount0,\n amount1,\n poolShare,\n };\n}\n\n/**\n * Calculate token amounts for removing liquidity\n *\n * From spec:\n * - amount0 = shares * reserve0 / totalShares\n * - amount1 = shares * reserve1 / totalShares\n */\nexport function getRemoveLiquidityQuote(\n pool: Pool,\n sharesIn: bigint,\n): RemoveLiquidityQuote {\n if (pool.totalShares === 0n) {\n throw new Error('Pool has no shares');\n }\n if (sharesIn > pool.totalShares) {\n throw new Error('Shares exceed total supply');\n }\n\n const amount0 = (sharesIn * pool.reserve0) / pool.totalShares;\n const amount1 = (sharesIn * pool.reserve1) / pool.totalShares;\n\n return { amount0, amount1 };\n}\n\n// ============================================================================\n// Fee Growth Calculations\n// ============================================================================\n\n/**\n * Calculate accrued fees for a position\n *\n * From spec:\n * - delta = pool.fee_growth_global - position.fee_growth_last\n * - owed_inc = floor(position.shares * delta / 2^64)\n */\nexport function calculateAccruedFees(\n shares: bigint,\n feeGrowthLastQ64: bigint,\n feeGrowthGlobalQ64: bigint,\n): bigint {\n const delta = feeGrowthGlobalQ64 - feeGrowthLastQ64;\n return (shares * delta) >> 64n;\n}\n\n/**\n * Calculate pending fees for a position (both tokens)\n */\nexport function getPendingFees(\n pool: Pool,\n position: {\n shares: bigint;\n feeGrowthLast0Q64: bigint;\n feeGrowthLast1Q64: bigint;\n feeOwed0: bigint;\n feeOwed1: bigint;\n },\n): { pending0: bigint; pending1: bigint } {\n const accrued0 = calculateAccruedFees(\n position.shares,\n position.feeGrowthLast0Q64,\n pool.feeGrowthGlobal0Q64,\n );\n const accrued1 = calculateAccruedFees(\n position.shares,\n position.feeGrowthLast1Q64,\n pool.feeGrowthGlobal1Q64,\n );\n\n return {\n pending0: position.feeOwed0 + accrued0,\n pending1: position.feeOwed1 + accrued1,\n };\n}\n\n// ============================================================================\n// Price Helpers\n// ============================================================================\n\n/**\n * Calculate spot price of token0 in terms of token1\n */\nexport function getSpotPrice0(pool: Pool): number {\n if (pool.reserve0 === 0n) return 0;\n return q64ToNumber(computePrice0Q64(pool.reserve0, pool.reserve1));\n}\n\n/**\n * Calculate spot price of token1 in terms of token0\n */\nexport function getSpotPrice1(pool: Pool): number {\n if (pool.reserve1 === 0n) return 0;\n return q64ToNumber(computePrice1Q64(pool.reserve0, pool.reserve1));\n}\n\n/**\n * Calculate k (constant product invariant)\n */\nexport function getK(pool: Pool): bigint {\n return pool.reserve0 * pool.reserve1;\n}\n\n/**\n * Calculate pool TVL in terms of one token\n * @param pool Pool data\n * @param side 0 = denominate in token0, 1 = denominate in token1\n */\nexport function getTvl(pool: Pool, side: SwapDirection = 0): bigint {\n if (side === 0) {\n // TVL in token0: reserve0 + (reserve1 * price1)\n // where price1 = reserve0/reserve1 (price of 1 in terms of 0)\n // = reserve0 + reserve1 * reserve0 / reserve1 = 2 * reserve0\n return 2n * pool.reserve0;\n } else {\n // TVL in token1: reserve1 + (reserve0 * price0)\n return 2n * pool.reserve1;\n }\n}\n\n// ============================================================================\n// TWAP Helpers\n// ============================================================================\n\n/**\n * Calculate TWAP price from oracle observations\n *\n * TWAP = (cumulative_end - cumulative_start) / (timestamp_end - timestamp_start)\n */\nexport function calculateTwap(\n cumulativeStart: bigint,\n cumulativeEnd: bigint,\n timestampStart: number,\n timestampEnd: number,\n): bigint {\n const dt = BigInt(timestampEnd - timestampStart);\n if (dt === 0n) return 0n;\n\n // Handle wrapping (cumulative values can wrap around)\n const cumulativeDiff =\n cumulativeEnd >= cumulativeStart\n ? cumulativeEnd - cumulativeStart\n : (1n << 256n) - cumulativeStart + cumulativeEnd;\n\n return cumulativeDiff / dt;\n}\n\n/**\n * Calculate TWAP as a decimal number\n */\nexport function calculateTwapNumber(\n cumulativeStart: bigint,\n cumulativeEnd: bigint,\n timestampStart: number,\n timestampEnd: number,\n): number {\n const twapQ64 = calculateTwap(\n cumulativeStart,\n cumulativeEnd,\n timestampStart,\n timestampEnd,\n );\n return q64ToNumber(twapQ64);\n}\n","import type { Address, Instruction, AccountMeta } from '@solana/kit';\nimport { AccountRole } from '@solana/kit';\nimport {\n CPMM_PROGRAM_ID,\n TOKEN_PROGRAM_ADDRESS,\n INSTRUCTION_DISCRIMINATORS,\n} from '../core/constants.js';\nimport type { CollectFeesArgs } from '../core/types.js';\nimport { collectFeesArgsCodec, encodeInstructionData } from '../core/codecs.js';\n\n/**\n * Accounts required for collect_fees instruction\n */\nexport interface CollectFeesAccounts {\n /** Pool account (writable) */\n pool: Address;\n /** User's position account (writable) */\n position: Address;\n /** Position owner (signer) */\n owner: Address;\n /** Pool authority PDA (read-only) */\n authority: Address;\n /** Pool vault for token0 (writable) */\n vault0: Address;\n /** Pool vault for token1 (writable) */\n vault1: Address;\n /** Token0 mint (read-only) */\n token0Mint: Address;\n /** Token1 mint (read-only) */\n token1Mint: Address;\n /** User's token0 account (writable) */\n user0: Address;\n /** User's token1 account (writable) */\n user1: Address;\n /** SPL Token program */\n tokenProgram?: Address;\n}\n\n/**\n * Create a collect_fees instruction\n *\n * Collects accrued LP fees from a position. Fees are first accrued from the global\n * fee growth, then transferred from the pool vaults to the user's token accounts.\n * You can specify max amounts to partially collect fees.\n *\n * @param accounts - Required accounts for collecting fees\n * @param args - Instruction arguments (max0, max1)\n * @param programId - Program ID (defaults to CPMM program)\n * @returns Instruction to collect fees\n *\n * @example\n * ```ts\n * // Collect all accrued fees\n * const ix = createCollectFeesInstruction(\n * {\n * pool: poolAddress,\n * position: positionAddress,\n * owner: userPublicKey,\n * authority: authorityPda,\n * vault0: vault0Address,\n * vault1: vault1Address,\n * token0Mint: mint0,\n * token1Mint: mint1,\n * user0: userToken0Account,\n * user1: userToken1Account,\n * },\n * {\n * max0: BigInt('18446744073709551615'), // u64::MAX to collect all\n * max1: BigInt('18446744073709551615'),\n * }\n * );\n * ```\n */\nexport function createCollectFeesInstruction(\n accounts: CollectFeesAccounts,\n args: CollectFeesArgs,\n programId: Address = CPMM_PROGRAM_ID,\n): Instruction {\n const {\n pool,\n position,\n owner,\n authority,\n vault0,\n vault1,\n token0Mint,\n token1Mint,\n user0,\n user1,\n tokenProgram = TOKEN_PROGRAM_ADDRESS,\n } = accounts;\n\n // Build account metas in order expected by the program\n // Order: pool, position, owner, authority, vault0, vault1,\n // token0_mint, token1_mint, user0, user1, token_program\n const keys: AccountMeta[] = [\n { address: pool, role: AccountRole.WRITABLE },\n { address: position, role: AccountRole.WRITABLE },\n { address: owner, role: AccountRole.READONLY_SIGNER },\n { address: authority, role: AccountRole.READONLY },\n { address: vault0, role: AccountRole.WRITABLE },\n { address: vault1, role: AccountRole.WRITABLE },\n { address: token0Mint, role: AccountRole.READONLY },\n { address: token1Mint, role: AccountRole.READONLY },\n { address: user0, role: AccountRole.WRITABLE },\n { address: user1, role: AccountRole.WRITABLE },\n { address: tokenProgram, role: AccountRole.READONLY },\n ];\n\n const data = encodeInstructionData(\n INSTRUCTION_DISCRIMINATORS.collectFees,\n collectFeesArgsCodec,\n args,\n );\n\n return {\n programAddress: programId,\n accounts: keys,\n data,\n };\n}\n\n/**\n * Convenience constant for collecting all fees (u64::MAX)\n */\nexport const MAX_FEE_AMOUNT = BigInt('18446744073709551615');\n","/**\n * Pool fetching and utility functions for the CPMM SDK\n */\n\nimport type { Address } from '@solana/kit';\nimport type { Rpc, GetAccountInfoApi } from '@solana/kit';\nimport type { Base64EncodedBytes } from '@solana/kit';\nimport type { GetProgramAccountsRpc } from '../core/rpc.js';\nimport type { Pool } from '../core/types.js';\nimport { decodePool } from '../core/codecs.js';\nimport { CPMM_PROGRAM_ID, ACCOUNT_DISCRIMINATORS } from '../core/constants.js';\nimport { getPoolAddress, sortMints } from '../core/pda.js';\n\n// Browser-compatible base64 encoding/decoding utilities\nfunction bytesToBase64(bytes: Uint8Array): string {\n let binary = '';\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary);\n}\n\nfunction base64ToBytes(base64: string): Uint8Array {\n const binary = atob(base64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n return bytes;\n}\n\n/**\n * Configuration for fetching pools\n */\nexport interface FetchPoolsConfig {\n /** Program ID (defaults to CPMM program) */\n programId?: Address;\n /** Commitment level */\n commitment?: 'processed' | 'confirmed' | 'finalized';\n}\n\n/**\n * Pool with its address\n */\nexport interface PoolWithAddress {\n address: Address;\n account: Pool;\n}\n\ntype ProgramAccount = Readonly<{\n pubkey: Address;\n account: Readonly<{ data: [string, 'base64'] }>;\n}>;\n\n/**\n * Fetch and decode a single pool account\n *\n * @param rpc - Solana RPC client\n * @param address - Pool account address\n * @param config - Optional configuration\n * @returns Decoded pool data or null if not found\n *\n * @example\n * ```ts\n * const pool = await fetchPool(rpc, poolAddress);\n * if (pool) {\n * console.log(`Pool reserves: ${pool.reserve0} / ${pool.reserve1}`);\n * }\n * ```\n */\nexport async function fetchPool(\n rpc: Rpc<GetAccountInfoApi>,\n address: Address,\n config?: FetchPoolsConfig,\n): Promise<Pool | null> {\n const response = await rpc\n .getAccountInfo(address, {\n encoding: 'base64',\n commitment: config?.commitment,\n })\n .send();\n\n if (!response.value) {\n return null;\n }\n\n return decodePool(base64ToBytes(response.value.data[0]));\n}\n\n/**\n * Fetch all pool accounts for the CPMM program\n *\n * Uses getProgramAccounts with a discriminator filter for efficiency.\n *\n * @param rpc - Solana RPC client\n * @param config - Optional configuration\n * @returns Array of pools with their addresses\n *\n * @example\n * ```ts\n * const pools = await fetchAllPools(rpc);\n * console.log(`Found ${pools.length} pools`);\n * for (const { address, account } of pools) {\n * console.log(`Pool ${address}: ${account.token0Mint} / ${account.token1Mint}`);\n * }\n * ```\n */\nexport async function fetchAllPools(\n rpc: GetProgramAccountsRpc,\n config?: FetchPoolsConfig,\n): Promise<PoolWithAddress[]> {\n const programId = config?.programId ?? CPMM_PROGRAM_ID;\n\n // Filter by Pool discriminator (first 8 bytes)\n const discriminatorFilter = {\n memcmp: {\n offset: 0n,\n bytes: bytesToBase64(ACCOUNT_DISCRIMINATORS.Pool) as Base64EncodedBytes,\n encoding: 'base64' as const,\n },\n };\n\n const response = (await rpc\n .getProgramAccounts(programId, {\n encoding: 'base64',\n commitment: config?.commitment,\n filters: [discriminatorFilter],\n })\n .send()) as unknown;\n\n const accounts = (\n Array.isArray(response)\n ? response\n : (response as { value: ProgramAccount[] }).value\n ) as ProgramAccount[];\n\n const pools: PoolWithAddress[] = [];\n\n for (const account of accounts) {\n try {\n const pool = decodePool(base64ToBytes(account.account.data[0]));\n pools.push({\n address: account.pubkey,\n account: pool,\n });\n } catch {\n // Skip accounts that fail to decode (shouldn't happen with proper filter)\n console.warn(`Failed to decode pool account: ${account.pubkey}`);\n }\n }\n\n return pools;\n}\n\n/**\n * Find a pool by its token pair mints\n *\n * Derives the pool PDA from the mints (automatically sorted) and fetches it.\n *\n * @param rpc - Solana RPC client\n * @param mint0 - First token mint\n * @param mint1 - Second token mint\n * @param config - Optional configuration\n * @returns Pool data with address, or null if not found\n *\n * @example\n * ```ts\n * const result = await getPoolByMints(rpc, usdcMint, wsolMint);\n * if (result) {\n * console.log(`Found pool at ${result.address}`);\n * console.log(`Swap fee: ${result.account.swapFeeBps} bps`);\n * }\n * ```\n */\nexport async function getPoolByMints(\n rpc: Rpc<GetAccountInfoApi>,\n mint0: Address,\n mint1: Address,\n config?: FetchPoolsConfig,\n): Promise<PoolWithAddress | null> {\n const programId = config?.programId ?? CPMM_PROGRAM_ID;\n\n // Derive pool address (mints are sorted internally)\n const [poolAddress] = await getPoolAddress(mint0, mint1, programId);\n\n const pool = await fetchPool(rpc, poolAddress, config);\n\n if (!pool) {\n return null;\n }\n\n return {\n address: poolAddress,\n account: pool,\n };\n}\n\n/**\n * Get multiple pools by their addresses in a single batch request\n *\n * @param rpc - Solana RPC client\n * @param addresses - Array of pool addresses to fetch\n * @param config - Optional configuration\n * @returns Map of address to pool (missing pools are not included)\n *\n * @example\n * ```ts\n * const poolMap = await fetchPoolsBatch(rpc, [pool1, pool2, pool3]);\n * for (const [addr, pool] of poolMap) {\n * console.log(`Pool ${addr}: TVL = ${pool.reserve0 + pool.reserve1}`);\n * }\n * ```\n */\nexport async function fetchPoolsBatch(\n rpc: Rpc<GetAccountInfoApi>,\n addresses: Address[],\n config?: FetchPoolsConfig,\n): Promise<Map<Address, Pool>> {\n const pools = new Map<Address, Pool>();\n\n // Fetch all in parallel\n const results = await Promise.all(\n addresses.map((addr) => fetchPool(rpc, addr, config)),\n );\n\n for (let i = 0; i < addresses.length; i++) {\n const pool = results[i];\n if (pool) {\n pools.set(addresses[i], pool);\n }\n }\n\n return pools;\n}\n\n/**\n * Check if a pool exists for a token pair\n *\n * @param rpc - Solana RPC client\n * @param mint0 - First token mint\n * @param mint1 - Second token mint\n * @param config - Optional configuration\n * @returns true if pool exists, false otherwise\n */\nexport async function poolExists(\n rpc: Rpc<GetAccountInfoApi>,\n mint0: Address,\n mint1: Address,\n config?: FetchPoolsConfig,\n): Promise<boolean> {\n const result = await getPoolByMints(rpc, mint0, mint1, config);\n return result !== null;\n}\n\n/**\n * Get the pool address for a token pair without fetching\n *\n * @param mint0 - First token mint\n * @param mint1 - Second token mint\n * @param programId - Program ID (defaults to CPMM program)\n * @returns Pool address and sorted mints\n */\nexport async function getPoolAddressFromMints(\n mint0: Address,\n mint1: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<{\n poolAddress: Address;\n token0: Address;\n token1: Address;\n}> {\n const [token0, token1] = sortMints(mint0, mint1);\n const [poolAddress] = await getPoolAddress(mint0, mint1, programId);\n\n return {\n poolAddress,\n token0,\n token1,\n };\n}\n\n/**\n * Filter pools by a specific token mint\n *\n * @param pools - Array of pools to filter\n * @param mint - Token mint to filter by\n * @returns Pools that contain the specified mint\n */\nexport function filterPoolsByMint(\n pools: PoolWithAddress[],\n mint: Address,\n): PoolWithAddress[] {\n return pools.filter(\n ({ account }) => account.token0Mint === mint || account.token1Mint === mint,\n );\n}\n\n/**\n * Sort pools by total reserves (proxy for TVL)\n *\n * @param pools - Array of pools to sort\n * @param descending - Sort descending (highest first) if true\n * @returns Sorted array (does not mutate input)\n */\nexport function sortPoolsByReserves(\n pools: PoolWithAddress[],\n descending = true,\n): PoolWithAddress[] {\n return [...pools].sort((a, b) => {\n const totalA = a.account.reserve0 + a.account.reserve1;\n const totalB = b.account.reserve0 + b.account.reserve1;\n const cmp = totalA < totalB ? -1 : totalA > totalB ? 1 : 0;\n return descending ? -cmp : cmp;\n });\n}\n","/**\n * Position fetching and utility functions for the CPMM SDK\n */\n\nimport type { Address } from '@solana/kit';\nimport type { Rpc, GetAccountInfoApi } from '@solana/kit';\nimport type { GetProgramAccountsRpc } from '../core/rpc.js';\nimport type { Base58EncodedBytes, Base64EncodedBytes } from '@solana/kit';\nimport type { Position, Pool } from '../core/types.js';\nimport { decodePosition } from '../core/codecs.js';\nimport { CPMM_PROGRAM_ID, ACCOUNT_DISCRIMINATORS } from '../core/constants.js';\nimport { getPositionAddress } from '../core/pda.js';\nimport { getPendingFees, ratioToNumber } from '../core/math.js';\n\n// Browser-compatible base64 encoding/decoding utilities\nfunction bytesToBase64(bytes: Uint8Array): string {\n let binary = '';\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary);\n}\n\nfunction base64ToBytes(base64: string): Uint8Array {\n const binary = atob(base64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n return bytes;\n}\n\n/**\n * Configuration for fetching positions\n */\nexport interface FetchPositionsConfig {\n /** Program ID (defaults to CPMM program) */\n programId?: Address;\n /** Commitment level */\n commitment?: 'processed' | 'confirmed' | 'finalized';\n}\n\n/**\n * Position with its address\n */\nexport interface PositionWithAddress {\n address: Address;\n account: Position;\n}\n\ntype ProgramAccount = Readonly<{\n pubkey: Address;\n account: Readonly<{ data: [string, 'base64'] }>;\n}>;\n\n/**\n * Position value in underlying tokens\n */\nexport interface PositionValue {\n /** Amount of token0 the position is worth */\n amount0: bigint;\n /** Amount of token1 the position is worth */\n amount1: bigint;\n /** Pending uncollected fees in token0 */\n pendingFees0: bigint;\n /** Pending uncollected fees in token1 */\n pendingFees1: bigint;\n /** Total value in token0 (amount0 + pending0) */\n totalValue0: bigint;\n /** Total value in token1 (amount1 + pending1) */\n totalValue1: bigint;\n /** Share of pool as a decimal (0-1) */\n poolShare: number;\n}\n\n/**\n * Fetch and decode a single position account\n *\n * @param rpc - Solana RPC client\n * @param address - Position account address\n * @param config - Optional configuration\n * @returns Decoded position data or null if not found\n *\n * @example\n * ```ts\n * const position = await fetchPosition(rpc, positionAddress);\n * if (position) {\n * console.log(`Position shares: ${position.shares}`);\n * }\n * ```\n */\nexport async function fetchPosition(\n rpc: Rpc<GetAccountInfoApi>,\n address: Address,\n config?: FetchPositionsConfig,\n): Promise<Position | null> {\n const response = await rpc\n .getAccountInfo(address, {\n encoding: 'base64',\n commitment: config?.commitment,\n })\n .send();\n\n if (!response.value) {\n return null;\n }\n\n return decodePosition(base64ToBytes(response.value.data[0]));\n}\n\n/**\n * Fetch all positions for a specific owner\n *\n * @param rpc - Solana RPC client\n * @param owner - Owner address\n * @param pool - Optional pool address to filter by\n * @param config - Optional configuration\n * @returns Array of positions with their addresses\n *\n * @example\n * ```ts\n * // Get all positions for a user\n * const allPositions = await fetchUserPositions(rpc, userWallet);\n *\n * // Get positions for a specific pool\n * const poolPositions = await fetchUserPositions(rpc, userWallet, poolAddress);\n * ```\n */\nexport async function fetchUserPositions(\n rpc: GetProgramAccountsRpc,\n owner: Address,\n pool?: Address,\n config?: FetchPositionsConfig,\n): Promise<PositionWithAddress[]> {\n const programId = config?.programId ?? CPMM_PROGRAM_ID;\n\n // Build filters\n const filters = [\n // Discriminator filter (first 8 bytes)\n {\n memcmp: {\n offset: 0n,\n bytes: bytesToBase64(\n ACCOUNT_DISCRIMINATORS.Position,\n ) as Base64EncodedBytes,\n encoding: 'base64' as const,\n },\n },\n // Owner filter (after 8-byte discriminator + 32-byte pool = offset 40)\n {\n memcmp: {\n offset: 40n,\n bytes: owner as unknown as Base58EncodedBytes,\n encoding: 'base58' as const,\n },\n },\n ];\n\n // Add pool filter if specified (after 8-byte discriminator = offset 8)\n if (pool) {\n filters.push({\n memcmp: {\n offset: 8n,\n bytes: pool as unknown as Base58EncodedBytes,\n encoding: 'base58' as const,\n },\n });\n }\n\n const response = (await rpc\n .getProgramAccounts(programId, {\n encoding: 'base64',\n commitment: config?.commitment,\n filters,\n })\n .send()) as unknown;\n\n const accounts = (\n Array.isArray(response)\n ? response\n : (response as { value: ProgramAccount[] }).value\n ) as ProgramAccount[];\n\n const positions: PositionWithAddress[] = [];\n\n for (const account of accounts) {\n try {\n const position = decodePosition(base64ToBytes(account.account.data[0]));\n positions.push({\n address: account.pubkey,\n account: position,\n });\n } catch {\n console.warn(`Failed to decode position account: ${account.pubkey}`);\n }\n }\n\n return positions;\n}\n\n/**\n * Fetch all positions for a specific pool\n *\n * @param rpc - Solana RPC client\n * @param pool - Pool address\n * @param config - Optional configuration\n * @returns Array of positions with their addresses\n */\nexport async function fetchPoolPositions(\n rpc: GetProgramAccountsRpc,\n pool: Address,\n config?: FetchPositionsConfig,\n): Promise<PositionWithAddress[]> {\n const programId = config?.programId ?? CPMM_PROGRAM_ID;\n\n const filters = [\n // Discriminator filter\n {\n memcmp: {\n offset: 0n,\n bytes: bytesToBase64(\n ACCOUNT_DISCRIMINATORS.Position,\n ) as Base64EncodedBytes,\n encoding: 'base64' as const,\n },\n },\n // Pool filter (after 8-byte discriminator = offset 8)\n {\n memcmp: {\n offset: 8n,\n bytes: pool as unknown as Base58EncodedBytes,\n encoding: 'base58' as const,\n },\n },\n ];\n\n const response = (await rpc\n .getProgramAccounts(programId, {\n encoding: 'base64',\n commitment: config?.commitment,\n filters,\n })\n .send()) as unknown;\n\n const accounts = (\n Array.isArray(response)\n ? response\n : (response as { value: ProgramAccount[] }).value\n ) as ProgramAccount[];\n\n const positions: PositionWithAddress[] = [];\n\n for (const account of accounts) {\n try {\n const position = decodePosition(base64ToBytes(account.account.data[0]));\n positions.push({\n address: account.pubkey,\n account: position,\n });\n } catch {\n console.warn(`Failed to decode position account: ${account.pubkey}`);\n }\n }\n\n return positions;\n}\n\n/**\n * Calculate the value of a position in underlying tokens\n *\n * @param pool - Pool data\n * @param position - Position data\n * @returns Position value breakdown\n *\n * @example\n * ```ts\n * const pool = await fetchPool(rpc, poolAddress);\n * const position = await fetchPosition(rpc, positionAddress);\n *\n * if (pool && position) {\n * const value = getPositionValue(pool, position);\n * console.log(`Position worth ${value.amount0} token0 + ${value.amount1} token1`);\n * console.log(`Pool share: ${(value.poolShare * 100).toFixed(2)}%`);\n * }\n * ```\n */\nexport function getPositionValue(\n pool: Pool,\n position: Position,\n): PositionValue {\n if (pool.totalShares === 0n) {\n return {\n amount0: 0n,\n amount1: 0n,\n pendingFees0: 0n,\n pendingFees1: 0n,\n totalValue0: 0n,\n totalValue1: 0n,\n poolShare: 0,\n };\n }\n\n // Calculate underlying token amounts\n const amount0 = (position.shares * pool.reserve0) / pool.totalShares;\n const amount1 = (position.shares * pool.reserve1) / pool.totalShares;\n\n // Calculate pending fees\n const { pending0, pending1 } = getPendingFees(pool, position);\n\n // Calculate pool share\n const poolShare = ratioToNumber(position.shares, pool.totalShares);\n\n return {\n amount0,\n amount1,\n pendingFees0: pending0,\n pendingFees1: pending1,\n totalValue0: amount0 + pending0,\n totalValue1: amount1 + pending1,\n poolShare,\n };\n}\n\n/**\n * Derive and fetch a position by its deterministic parameters\n *\n * @param rpc - Solana RPC client\n * @param pool - Pool address\n * @param owner - Position owner\n * @param positionId - Position ID\n * @param config - Optional configuration\n * @returns Position with address or null if not found\n */\nexport async function fetchPositionByParams(\n rpc: Rpc<GetAccountInfoApi>,\n pool: Address,\n owner: Address,\n positionId: bigint,\n config?: FetchPositionsConfig,\n): Promise<PositionWithAddress | null> {\n const programId = config?.programId ?? CPMM_PROGRAM_ID;\n const [address] = await getPositionAddress(\n pool,\n owner,\n positionId,\n programId,\n );\n\n const position = await fetchPosition(rpc, address, config);\n\n if (!position) {\n return null;\n }\n\n return {\n address,\n account: position,\n };\n}\n\n/**\n * Get the position address for given parameters without fetching\n *\n * @param pool - Pool address\n * @param owner - Position owner\n * @param positionId - Position ID\n * @param programId - Program ID (defaults to CPMM program)\n * @returns Position address\n */\nexport async function getPositionAddressFromParams(\n pool: Address,\n owner: Address,\n positionId: bigint,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<Address> {\n const [address] = await getPositionAddress(\n pool,\n owner,\n positionId,\n programId,\n );\n return address;\n}\n\n/**\n * Batch fetch multiple positions\n *\n * @param rpc - Solana RPC client\n * @param addresses - Array of position addresses to fetch\n * @param config - Optional configuration\n * @returns Map of address to position (missing positions are not included)\n */\nexport async function fetchPositionsBatch(\n rpc: Rpc<GetAccountInfoApi>,\n addresses: Address[],\n config?: FetchPositionsConfig,\n): Promise<Map<Address, Position>> {\n const positions = new Map<Address, Position>();\n\n const results = await Promise.all(\n addresses.map((addr) => fetchPosition(rpc, addr, config)),\n );\n\n for (let i = 0; i < addresses.length; i++) {\n const position = results[i];\n if (position) {\n positions.set(addresses[i], position);\n }\n }\n\n return positions;\n}\n\n/**\n * Filter positions with non-zero shares\n *\n * @param positions - Array of positions to filter\n * @returns Positions with shares > 0\n */\nexport function filterActivePositions(\n positions: PositionWithAddress[],\n): PositionWithAddress[] {\n return positions.filter(({ account }) => account.shares > 0n);\n}\n\n/**\n * Sort positions by share amount\n *\n * @param positions - Array of positions to sort\n * @param descending - Sort descending (largest first) if true\n * @returns Sorted array (does not mutate input)\n */\nexport function sortPositionsByShares(\n positions: PositionWithAddress[],\n descending = true,\n): PositionWithAddress[] {\n return [...positions].sort((a, b) => {\n const cmp =\n a.account.shares < b.account.shares\n ? -1\n : a.account.shares > b.account.shares\n ? 1\n : 0;\n return descending ? -cmp : cmp;\n });\n}\n","/**\n * Oracle fetching and TWAP utility functions for the CPMM SDK\n */\n\nimport type { Address } from '@solana/kit';\nimport type { Rpc, GetAccountInfoApi } from '@solana/kit';\nimport type { OracleState, TwapResult, Pool } from '../core/types.js';\nimport { decodeOracleState } from '../core/codecs.js';\nimport { CPMM_PROGRAM_ID } from '../core/constants.js';\nimport { getOracleAddress } from '../core/pda.js';\nimport { q64ToNumber } from '../core/math.js';\n\n// Browser-compatible base64 decoding\nfunction base64ToBytes(base64: string): Uint8Array {\n const binary = atob(base64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n return bytes;\n}\n\n/**\n * Configuration for fetching oracles\n */\nexport interface FetchOracleConfig {\n /** Program ID (defaults to CPMM program) */\n programId?: Address;\n /** Commitment level */\n commitment?: 'processed' | 'confirmed' | 'finalized';\n}\n\n/**\n * Oracle with its address\n */\nexport interface OracleWithAddress {\n address: Address;\n account: OracleState;\n}\n\n/**\n * Fetch and decode an oracle state account\n *\n * @param rpc - Solana RPC client\n * @param address - Oracle account address\n * @param config - Optional configuration\n * @returns Decoded oracle state or null if not found\n *\n * @example\n * ```ts\n * const oracle = await fetchOracle(rpc, oracleAddress);\n * if (oracle) {\n * console.log(`Oracle initialized: ${oracle.initialized}`);\n * console.log(`Last update: ${oracle.lastTimestamp}`);\n * }\n * ```\n */\nexport async function fetchOracle(\n rpc: Rpc<GetAccountInfoApi>,\n address: Address,\n config?: FetchOracleConfig,\n): Promise<OracleState | null> {\n const response = await rpc\n .getAccountInfo(address, {\n encoding: 'base64',\n commitment: config?.commitment,\n })\n .send();\n\n if (!response.value) {\n return null;\n }\n\n return decodeOracleState(base64ToBytes(response.value.data[0]));\n}\n\n/**\n * Get the oracle for a specific pool\n *\n * Derives the oracle PDA from the pool address and fetches it.\n *\n * @param rpc - Solana RPC client\n * @param pool - Pool address\n * @param config - Optional configuration\n * @returns Oracle data with address, or null if not found/initialized\n *\n * @example\n * ```ts\n * const result = await getOracleForPool(rpc, poolAddress);\n * if (result) {\n * console.log(`Oracle at ${result.address}`);\n * const twap = consultTwap(result.account, 300); // 5-minute TWAP\n * console.log(`TWAP price: ${twap.price0}`);\n * }\n * ```\n */\nexport async function getOracleForPool(\n rpc: Rpc<GetAccountInfoApi>,\n pool: Address,\n config?: FetchOracleConfig,\n): Promise<OracleWithAddress | null> {\n const programId = config?.programId ?? CPMM_PROGRAM_ID;\n const [oracleAddress] = await getOracleAddress(pool, programId);\n\n const oracle = await fetchOracle(rpc, oracleAddress, config);\n\n if (!oracle) {\n return null;\n }\n\n return {\n address: oracleAddress,\n account: oracle,\n };\n}\n\n/**\n * Get the oracle address for a pool without fetching\n *\n * @param pool - Pool address\n * @param programId - Program ID (defaults to CPMM program)\n * @returns Oracle address\n */\nexport async function getOracleAddressFromPool(\n pool: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<Address> {\n const [address] = await getOracleAddress(pool, programId);\n return address;\n}\n\n/**\n * Calculate TWAP price from oracle observations\n *\n * Finds the appropriate observations in the circular buffer and computes\n * the time-weighted average price over the specified window.\n *\n * @param oracle - Oracle state data\n * @param windowSeconds - TWAP window in seconds\n * @param currentTimestamp - Optional override for current time (seconds)\n * @returns TWAP prices for both directions, or null if insufficient data\n *\n * @example\n * ```ts\n * const oracle = await fetchOracle(rpc, oracleAddress);\n * if (oracle && oracle.initialized) {\n * // Get 5-minute TWAP\n * const twap = consultTwap(oracle, 300);\n * if (twap) {\n * console.log(`Token0 price: ${twap.price0} token1 per token0`);\n * console.log(`Token1 price: ${twap.price1} token0 per token1`);\n * }\n * }\n * ```\n */\nexport function consultTwap(\n oracle: OracleState,\n windowSeconds: number,\n currentTimestamp?: number,\n): TwapResult | null {\n if (!oracle.initialized || oracle.lastTimestamp === 0) {\n return null;\n }\n\n const nowTs = Math.max(0, Math.floor(currentTimestamp ?? Date.now() / 1000));\n const dtSinceLast = Math.max(0, nowTs - oracle.lastTimestamp);\n\n const cum0Now =\n oracle.price0Cumulative + oracle.truncPrice0Q64 * BigInt(dtSinceLast);\n const cum1Now =\n oracle.price1Cumulative + oracle.truncPrice1Q64 * BigInt(dtSinceLast);\n\n if (windowSeconds === 0) {\n return {\n price0Q64: oracle.truncPrice0Q64,\n price1Q64: oracle.truncPrice1Q64,\n price0: q64ToNumber(oracle.truncPrice0Q64),\n price1: q64ToNumber(oracle.truncPrice1Q64),\n };\n }\n\n const targetTs = Math.max(0, nowTs - windowSeconds);\n const sample = selectSample(oracle, targetTs);\n if (!sample) {\n return null;\n }\n\n const dt = Math.max(0, nowTs - sample.timestamp);\n if (dt === 0) {\n return null;\n }\n\n const price0Q64 = (cum0Now - sample.price0Cumulative) / BigInt(dt);\n const price1Q64 = (cum1Now - sample.price1Cumulative) / BigInt(dt);\n\n return {\n price0Q64,\n price1Q64,\n price0: q64ToNumber(price0Q64),\n price1: q64ToNumber(price1Q64),\n };\n}\n\nfunction selectSample(\n oracle: OracleState,\n targetTimestamp: number,\n): OracleState['observations'][number] | null {\n let best: OracleState['observations'][number] | null = null;\n const base = {\n timestamp: oracle.lastTimestamp,\n price0Cumulative: oracle.price0Cumulative,\n price1Cumulative: oracle.price1Cumulative,\n };\n\n if (base.timestamp !== 0 && base.timestamp <= targetTimestamp) {\n best = base;\n }\n\n for (const obs of oracle.observations) {\n if (obs.timestamp === 0) {\n continue;\n }\n if (obs.timestamp <= targetTimestamp) {\n if (!best || obs.timestamp > best.timestamp) {\n best = obs;\n }\n }\n }\n\n if (best) {\n return best;\n }\n\n let oldest: OracleState['observations'][number] | null =\n base.timestamp !== 0 ? base : null;\n for (const obs of oracle.observations) {\n if (obs.timestamp === 0) {\n continue;\n }\n if (!oldest || obs.timestamp < oldest.timestamp) {\n oldest = obs;\n }\n }\n\n return oldest;\n}\n\n/**\n * Get the current spot prices from oracle\n *\n * Uses the truncated (manipulation-resistant) prices stored in the oracle.\n *\n * @param oracle - Oracle state data\n * @returns Current truncated prices\n */\nexport function getOracleSpotPrices(oracle: OracleState): {\n price0Q64: bigint;\n price1Q64: bigint;\n price0: number;\n price1: number;\n} {\n return {\n price0Q64: oracle.truncPrice0Q64,\n price1Q64: oracle.truncPrice1Q64,\n price0: q64ToNumber(oracle.truncPrice0Q64),\n price1: q64ToNumber(oracle.truncPrice1Q64),\n };\n}\n\n/**\n * Get the price deviation metrics from oracle\n *\n * Deviation indicates how much the spot price has moved from the truncated price.\n * High deviation may indicate price manipulation or rapid market movement.\n *\n * @param oracle - Oracle state data\n * @returns Deviation values for both price directions\n */\nexport function getOracleDeviation(oracle: OracleState): {\n deviation0Q64: bigint;\n deviation1Q64: bigint;\n deviation0: number;\n deviation1: number;\n} {\n return {\n deviation0Q64: oracle.deviation0Q64,\n deviation1Q64: oracle.deviation1Q64,\n deviation0: q64ToNumber(oracle.deviation0Q64),\n deviation1: q64ToNumber(oracle.deviation1Q64),\n };\n}\n\n/**\n * Calculate the age of the oracle (time since last update)\n *\n * @param oracle - Oracle state data\n * @param currentTimestamp - Current timestamp (defaults to Date.now() / 1000)\n * @returns Age in seconds\n */\nexport function getOracleAge(\n oracle: OracleState,\n currentTimestamp?: number,\n): number {\n const now = currentTimestamp ?? Math.floor(Date.now() / 1000);\n return now - oracle.lastTimestamp;\n}\n\n/**\n * Check if oracle is stale (hasn't been updated recently)\n *\n * @param oracle - Oracle state data\n * @param maxAgeSeconds - Maximum acceptable age in seconds\n * @param currentTimestamp - Current timestamp (defaults to Date.now() / 1000)\n * @returns true if oracle is stale\n */\nexport function isOracleStale(\n oracle: OracleState,\n maxAgeSeconds: number,\n currentTimestamp?: number,\n): boolean {\n return getOracleAge(oracle, currentTimestamp) > maxAgeSeconds;\n}\n\n/**\n * Get observation buffer statistics\n *\n * @param oracle - Oracle state data\n * @returns Information about the observation buffer\n */\nexport function getOracleBufferStats(oracle: OracleState): {\n /** Total buffer capacity */\n capacity: number;\n /** Number of observations with data */\n filledCount: number;\n /** Current write index */\n currentIndex: number;\n /** Oldest observation timestamp (0 if not filled) */\n oldestTimestamp: number;\n /** Newest observation timestamp */\n newestTimestamp: number;\n /** Time span covered by observations */\n timeSpanSeconds: number;\n} {\n const observations = oracle.observations;\n const capacity = observations.length;\n const currentIndex = oracle.observationIndex;\n\n let filledCount = 0;\n let oldestTimestamp = 0;\n let newestTimestamp = 0;\n\n for (let i = 0; i < capacity; i++) {\n const obs = observations[i];\n if (obs.timestamp > 0) {\n filledCount++;\n if (oldestTimestamp === 0 || obs.timestamp < oldestTimestamp) {\n oldestTimestamp = obs.timestamp;\n }\n if (obs.timestamp > newestTimestamp) {\n newestTimestamp = obs.timestamp;\n }\n }\n }\n\n return {\n capacity,\n filledCount,\n currentIndex,\n oldestTimestamp,\n newestTimestamp,\n timeSpanSeconds:\n newestTimestamp > oldestTimestamp ? newestTimestamp - oldestTimestamp : 0,\n };\n}\n\n/**\n * Batch fetch oracles for multiple pools\n *\n * @param rpc - Solana RPC client\n * @param pools - Array of pool addresses\n * @param config - Optional configuration\n * @returns Map of pool address to oracle (missing oracles not included)\n */\nexport async function fetchOraclesBatch(\n rpc: Rpc<GetAccountInfoApi>,\n pools: Address[],\n config?: FetchOracleConfig,\n): Promise<Map<Address, OracleWithAddress>> {\n const programId = config?.programId ?? CPMM_PROGRAM_ID;\n const oracles = new Map<Address, OracleWithAddress>();\n\n // Derive all oracle addresses\n const oracleAddresses = await Promise.all(\n pools.map((pool) => getOracleAddress(pool, programId)),\n );\n\n // Fetch all oracles in parallel\n const results = await Promise.all(\n oracleAddresses.map(([addr]) => fetchOracle(rpc, addr, config)),\n );\n\n for (let i = 0; i < pools.length; i++) {\n const oracle = results[i];\n if (oracle) {\n oracles.set(pools[i], {\n address: oracleAddresses[i][0],\n account: oracle,\n });\n }\n }\n\n return oracles;\n}\n\n/**\n * Calculate price from pool reserves and compare with oracle\n *\n * Useful for detecting price discrepancies or manipulation.\n *\n * @param pool - Pool data\n * @param oracle - Oracle data\n * @returns Comparison metrics\n */\nexport function comparePoolAndOraclePrices(\n pool: Pool,\n oracle: OracleState,\n): {\n /** Spot price from pool reserves (token1 per token0) */\n poolPrice0: number;\n /** Truncated price from oracle (token1 per token0) */\n oraclePrice0: number;\n /** Difference as percentage (positive = pool > oracle) */\n divergencePct: number;\n} {\n const poolPrice0 =\n pool.reserve0 > 0n ? Number(pool.reserve1) / Number(pool.reserve0) : 0;\n\n const oraclePrice0 = q64ToNumber(oracle.truncPrice0Q64);\n\n const divergencePct =\n oraclePrice0 > 0 ? ((poolPrice0 - oraclePrice0) / oraclePrice0) * 100 : 0;\n\n return {\n poolPrice0,\n oraclePrice0,\n divergencePct,\n };\n}\n"]}
@@ -1,24 +1,15 @@
1
1
  import { address, getAddressCodec, getProgramDerivedAddress } from '@solana/kit';
2
+ export { TOKEN_PROGRAM_ADDRESS } from '@solana-program/token';
3
+ export { SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';
4
+ import '@solana/sysvars';
2
5
 
3
6
  // src/solana/core/pda.ts
4
- var PROGRAM_ID = address(
7
+ var CPMM_PROGRAM_ID = address(
5
8
  "9PSxVPoPfnbZ8Q1uQhgS6ZxvBjFboZtebNsu34umxkgQ"
6
9
  );
7
- var SENTINEL_PROGRAM_ID = address(
8
- "2vJ1c62knEwZbxp3XdHB4RSmCfz55pA6tRkCho63gW7u"
9
- );
10
- var TOKEN_PROGRAM_ID = address(
11
- "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
12
- );
13
10
  var TOKEN_METADATA_PROGRAM_ID = address(
14
11
  "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
15
12
  );
16
- var SYSTEM_PROGRAM_ID = address(
17
- "11111111111111111111111111111111"
18
- );
19
- var ASSOCIATED_TOKEN_PROGRAM_ID = address(
20
- "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
21
- );
22
13
  var BPS_DENOM = 10000n;
23
14
  var Q64_ONE = 1n << 64n;
24
15
  var ACCOUNT_VERSION = 1;
@@ -37,10 +28,6 @@ var SF_AFTER_ADD_LIQ = 1 << 3;
37
28
  var SF_BEFORE_REMOVE_LIQ = 1 << 4;
38
29
  var SF_AFTER_REMOVE_LIQ = 1 << 5;
39
30
  var SENTINEL_NO_CHANGE = 65535;
40
- var ACCOUNT_ROLE_READONLY = 0;
41
- var ACCOUNT_ROLE_WRITABLE = 1;
42
- var ACCOUNT_ROLE_SIGNER = 2;
43
- var ACCOUNT_ROLE_WRITABLE_SIGNER = 3;
44
31
  var INSTRUCTION_DISCRIMINATORS = {
45
32
  // SHA256("global:initialize_config")[0:8]
46
33
  initializeConfig: new Uint8Array([
@@ -227,13 +214,13 @@ function areMintsOrdered(mint0, mint1) {
227
214
  }
228
215
  return false;
229
216
  }
230
- async function getConfigAddress(programId = PROGRAM_ID) {
217
+ async function getConfigAddress(programId = CPMM_PROGRAM_ID) {
231
218
  return getProgramDerivedAddress({
232
219
  programAddress: programId,
233
220
  seeds: [textEncoder.encode(SEED_CONFIG)]
234
221
  });
235
222
  }
236
- async function getPoolAddress(mint0, mint1, programId = PROGRAM_ID) {
223
+ async function getPoolAddress(mint0, mint1, programId = CPMM_PROGRAM_ID) {
237
224
  const [token0, token1] = sortMints(mint0, mint1);
238
225
  return getProgramDerivedAddress({
239
226
  programAddress: programId,
@@ -244,13 +231,13 @@ async function getPoolAddress(mint0, mint1, programId = PROGRAM_ID) {
244
231
  ]
245
232
  });
246
233
  }
247
- async function getPoolAuthorityAddress(pool, programId = PROGRAM_ID) {
234
+ async function getPoolAuthorityAddress(pool, programId = CPMM_PROGRAM_ID) {
248
235
  return getProgramDerivedAddress({
249
236
  programAddress: programId,
250
237
  seeds: [textEncoder.encode(SEED_AUTHORITY), addressCodec.encode(pool)]
251
238
  });
252
239
  }
253
- async function getPositionAddress(pool, owner, positionId, programId = PROGRAM_ID) {
240
+ async function getPositionAddress(pool, owner, positionId, programId = CPMM_PROGRAM_ID) {
254
241
  const positionIdBytes = new Uint8Array(8);
255
242
  const view = new DataView(positionIdBytes.buffer);
256
243
  view.setBigUint64(0, positionId, true);
@@ -264,13 +251,13 @@ async function getPositionAddress(pool, owner, positionId, programId = PROGRAM_I
264
251
  ]
265
252
  });
266
253
  }
267
- async function getOracleAddress(pool, programId = PROGRAM_ID) {
254
+ async function getOracleAddress(pool, programId = CPMM_PROGRAM_ID) {
268
255
  return getProgramDerivedAddress({
269
256
  programAddress: programId,
270
257
  seeds: [textEncoder.encode(SEED_ORACLE), addressCodec.encode(pool)]
271
258
  });
272
259
  }
273
- async function getProtocolPositionAddress(pool, programId = PROGRAM_ID) {
260
+ async function getProtocolPositionAddress(pool, programId = CPMM_PROGRAM_ID) {
274
261
  return getProgramDerivedAddress({
275
262
  programAddress: programId,
276
263
  seeds: [
@@ -279,7 +266,7 @@ async function getProtocolPositionAddress(pool, programId = PROGRAM_ID) {
279
266
  ]
280
267
  });
281
268
  }
282
- async function getPoolInitAddresses(mint0, mint1, programId = PROGRAM_ID) {
269
+ async function getPoolInitAddresses(mint0, mint1, programId = CPMM_PROGRAM_ID) {
283
270
  const [token0, token1] = sortMints(mint0, mint1);
284
271
  const [config, pool] = await Promise.all([
285
272
  getConfigAddress(programId),
@@ -298,7 +285,7 @@ async function getPoolInitAddresses(mint0, mint1, programId = PROGRAM_ID) {
298
285
  protocolPosition
299
286
  };
300
287
  }
301
- async function getSwapAddresses(pool, programId = PROGRAM_ID) {
288
+ async function getSwapAddresses(pool, programId = CPMM_PROGRAM_ID) {
302
289
  const [config, authority, oracle] = await Promise.all([
303
290
  getConfigAddress(programId),
304
291
  getPoolAuthorityAddress(pool, programId),
@@ -306,7 +293,7 @@ async function getSwapAddresses(pool, programId = PROGRAM_ID) {
306
293
  ]);
307
294
  return { config, authority, oracle };
308
295
  }
309
- async function getLiquidityAddresses(pool, owner, positionId, programId = PROGRAM_ID) {
296
+ async function getLiquidityAddresses(pool, owner, positionId, programId = CPMM_PROGRAM_ID) {
310
297
  const [config, authority, position, protocolPosition, oracle] = await Promise.all([
311
298
  getConfigAddress(programId),
312
299
  getPoolAuthorityAddress(pool, programId),
@@ -317,6 +304,6 @@ async function getLiquidityAddresses(pool, owner, positionId, programId = PROGRA
317
304
  return { config, authority, position, protocolPosition, oracle };
318
305
  }
319
306
 
320
- export { ACCOUNT_DISCRIMINATORS, ACCOUNT_ROLE_READONLY, ACCOUNT_ROLE_SIGNER, ACCOUNT_ROLE_WRITABLE, ACCOUNT_ROLE_WRITABLE_SIGNER, ACCOUNT_VERSION, ASSOCIATED_TOKEN_PROGRAM_ID, BPS_DENOM, INSTRUCTION_DISCRIMINATORS, MAX_ORACLE_OBSERVATIONS, MAX_SENTINEL_ALLOWLIST, PROGRAM_ID, Q64_ONE, SEED_AUTHORITY, SEED_CONFIG, SEED_ORACLE, SEED_POOL, SEED_POSITION, SEED_PROTOCOL_POSITION, SENTINEL_NO_CHANGE, SENTINEL_PROGRAM_ID, SF_AFTER_ADD_LIQ, SF_AFTER_REMOVE_LIQ, SF_AFTER_SWAP, SF_BEFORE_ADD_LIQ, SF_BEFORE_REMOVE_LIQ, SF_BEFORE_SWAP, SYSTEM_PROGRAM_ID, TOKEN_METADATA_PROGRAM_ID, TOKEN_PROGRAM_ID, areMintsOrdered, getConfigAddress, getLiquidityAddresses, getOracleAddress, getPoolAddress, getPoolAuthorityAddress, getPoolInitAddresses, getPositionAddress, getProtocolPositionAddress, getSwapAddresses, sortMints };
321
- //# sourceMappingURL=chunk-T46KDO65.js.map
322
- //# sourceMappingURL=chunk-T46KDO65.js.map
307
+ export { ACCOUNT_DISCRIMINATORS, ACCOUNT_VERSION, BPS_DENOM, CPMM_PROGRAM_ID, INSTRUCTION_DISCRIMINATORS, MAX_ORACLE_OBSERVATIONS, MAX_SENTINEL_ALLOWLIST, Q64_ONE, SEED_AUTHORITY, SEED_CONFIG, SEED_ORACLE, SEED_POOL, SEED_POSITION, SEED_PROTOCOL_POSITION, SENTINEL_NO_CHANGE, SF_AFTER_ADD_LIQ, SF_AFTER_REMOVE_LIQ, SF_AFTER_SWAP, SF_BEFORE_ADD_LIQ, SF_BEFORE_REMOVE_LIQ, SF_BEFORE_SWAP, TOKEN_METADATA_PROGRAM_ID, areMintsOrdered, getConfigAddress, getLiquidityAddresses, getOracleAddress, getPoolAddress, getPoolAuthorityAddress, getPoolInitAddresses, getPositionAddress, getProtocolPositionAddress, getSwapAddresses, sortMints };
308
+ //# sourceMappingURL=chunk-J57ROY36.js.map
309
+ //# sourceMappingURL=chunk-J57ROY36.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/solana/core/constants.ts","../src/solana/core/pda.ts"],"names":[],"mappings":";;;;;;AAWO,IAAM,eAAA,GAA2B,OAAA;AAAA,EACtC;AACF;AAKO,IAAM,yBAAA,GAAqC,OAAA;AAAA,EAChD;AACF;AAOO,IAAM,SAAA,GAAY;AAGlB,IAAM,UAAU,EAAA,IAAM;AAGtB,IAAM,eAAA,GAAkB;AAOxB,IAAM,sBAAA,GAAyB;AAG/B,IAAM,uBAAA,GAA0B;AAOhC,IAAM,WAAA,GAAc;AAGpB,IAAM,SAAA,GAAY;AAGlB,IAAM,cAAA,GAAiB;AAGvB,IAAM,aAAA,GAAgB;AAGtB,IAAM,WAAA,GAAc;AAGpB,IAAM,sBAAA,GAAyB;AAO/B,IAAM,iBAAiB,CAAA,IAAK;AAG5B,IAAM,gBAAgB,CAAA,IAAK;AAG3B,IAAM,oBAAoB,CAAA,IAAK;AAG/B,IAAM,mBAAmB,CAAA,IAAK;AAG9B,IAAM,uBAAuB,CAAA,IAAK;AAGlC,IAAM,sBAAsB,CAAA,IAAK;AAGjC,IAAM,kBAAA,GAAqB;AAU3B,IAAM,0BAAA,GAA6B;AAAA;AAAA,EAExC,gBAAA,EAAkB,IAAI,UAAA,CAAW;AAAA,IAC/B,GAAA;AAA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AA,IAChB,iBAAiB,SAAS,CAAA;AAAA,IAC1B,uBAAA,CAAwB,MAAM,SAAS,CAAA;AAAA,IACvC,kBAAA,CAAmB,IAAA,EAAM,KAAA,EAAO,UAAA,EAAY,SAAS,CAAA;AAAA,IACrD,0BAAA,CAA2B,MAAM,SAAS,CAAA;AAAA,IAC1C,gBAAA,CAAiB,MAAM,SAAS;AAAA,GACjC,CAAA;AAEH,EAAA,OAAO,EAAE,MAAA,EAAQ,SAAA,EAAW,QAAA,EAAU,kBAAkB,MAAA,EAAO;AACjE","file":"chunk-J57ROY36.js","sourcesContent":["import { address, type Address } from '@solana/kit';\nexport {\n TOKEN_PROGRAM_ADDRESS,\n ASSOCIATED_TOKEN_PROGRAM_ADDRESS,\n} from '@solana-program/token';\nexport { SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';\nexport { SYSVAR_RENT_ADDRESS } from '@solana/sysvars';\n\n/**\n * CPMM AMM program ID (devnet)\n */\nexport const CPMM_PROGRAM_ID: Address = address(\n '9PSxVPoPfnbZ8Q1uQhgS6ZxvBjFboZtebNsu34umxkgQ',\n);\n\n/**\n * Metaplex Token Metadata Program ID\n */\nexport const TOKEN_METADATA_PROGRAM_ID: Address = address(\n 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s',\n);\n\n// ============================================================================\n// Math Constants\n// ============================================================================\n\n/** Basis points denominator (10,000 = 100%) */\nexport const BPS_DENOM = 10_000n;\n\n/** Q64.64 fixed-point representation of 1.0 (2^64) */\nexport const Q64_ONE = 1n << 64n;\n\n/** Current account version */\nexport const ACCOUNT_VERSION = 1;\n\n// ============================================================================\n// Array Size Constants\n// ============================================================================\n\n/** Maximum number of programs in sentinel allowlist */\nexport const MAX_SENTINEL_ALLOWLIST = 32;\n\n/** Maximum number of oracle observations (TWAP buffer size) */\nexport const MAX_ORACLE_OBSERVATIONS = 64;\n\n// ============================================================================\n// PDA Seeds\n// ============================================================================\n\n/** Seed for AmmConfig PDA: ['config'] */\nexport const SEED_CONFIG = 'config';\n\n/** Seed prefix for Pool PDA: ['pool', token0_mint, token1_mint] */\nexport const SEED_POOL = 'pool';\n\n/** Seed prefix for pool authority PDA: ['authority', pool] */\nexport const SEED_AUTHORITY = 'authority';\n\n/** Seed prefix for Position PDA: ['position', pool, owner, position_id] */\nexport const SEED_POSITION = 'position';\n\n/** Seed prefix for OracleState PDA: ['oracle', pool] */\nexport const SEED_ORACLE = 'oracle';\n\n/** Seed prefix for protocol position PDA: ['protocol_position', pool] */\nexport const SEED_PROTOCOL_POSITION = 'protocol_position';\n\n// ============================================================================\n// Sentinel Flags\n// ============================================================================\n\n/** Sentinel hook: called before swap */\nexport const SF_BEFORE_SWAP = 1 << 0;\n\n/** Sentinel hook: called after swap */\nexport const SF_AFTER_SWAP = 1 << 1;\n\n/** Sentinel hook: called before add liquidity */\nexport const SF_BEFORE_ADD_LIQ = 1 << 2;\n\n/** Sentinel hook: called after add liquidity */\nexport const SF_AFTER_ADD_LIQ = 1 << 3;\n\n/** Sentinel hook: called before remove liquidity */\nexport const SF_BEFORE_REMOVE_LIQ = 1 << 4;\n\n/** Sentinel hook: called after remove liquidity */\nexport const SF_AFTER_REMOVE_LIQ = 1 << 5;\n\n/** Sentinel return value indicating \"no change\" to fee parameter */\nexport const SENTINEL_NO_CHANGE = 0xffff;\n\n// ============================================================================\n// Instruction Discriminators (Anchor 8-byte hashes)\n// ============================================================================\n\n/**\n * Anchor instruction discriminator (first 8 bytes of SHA256(\"global:<instruction_name>\"))\n * These are computed at build time from the instruction names.\n */\nexport const INSTRUCTION_DISCRIMINATORS = {\n // SHA256(\"global:initialize_config\")[0:8]\n initializeConfig: new Uint8Array([\n 0xd0, 0x7f, 0x15, 0x01, 0xc2, 0xbe, 0xc4, 0x46,\n ]),\n // SHA256(\"global:initialize_pool\")[0:8]\n initializePool: new Uint8Array([\n 0x5f, 0xb4, 0x0a, 0xac, 0x54, 0xae, 0xe8, 0x28,\n ]),\n // SHA256(\"global:initialize_oracle\")[0:8]\n initializeOracle: new Uint8Array([\n 0x90, 0xdf, 0x83, 0x78, 0xc4, 0xfd, 0xb5, 0x63,\n ]),\n // SHA256(\"global:create_position\")[0:8]\n createPosition: new Uint8Array([\n 0x30, 0xd7, 0xc5, 0x99, 0x60, 0xcb, 0xb4, 0x85,\n ]),\n // SHA256(\"global:add_liquidity\")[0:8]\n addLiquidity: new Uint8Array([\n 0xb5, 0x9d, 0x59, 0x43, 0x8f, 0xb6, 0x34, 0x48,\n ]),\n // SHA256(\"global:remove_liquidity\")[0:8]\n removeLiquidity: new Uint8Array([\n 0x50, 0x55, 0xd1, 0x48, 0x18, 0xce, 0xb1, 0x6c,\n ]),\n // SHA256(\"global:swap_exact_in\")[0:8]\n swapExactIn: new Uint8Array([0x68, 0x68, 0x83, 0x56, 0xa1, 0xbd, 0xb4, 0xd8]),\n // SHA256(\"global:collect_fees\")[0:8]\n collectFees: new Uint8Array([0xa4, 0x98, 0xcf, 0x63, 0x1e, 0xba, 0x13, 0xb6]),\n // SHA256(\"global:collect_protocol_fees\")[0:8]\n collectProtocolFees: new Uint8Array([\n 0x16, 0x43, 0x17, 0x62, 0x96, 0xb2, 0x46, 0xdc,\n ]),\n // SHA256(\"global:close_position\")[0:8]\n closePosition: new Uint8Array([\n 0x7b, 0x86, 0x51, 0x00, 0x31, 0x44, 0x62, 0x62,\n ]),\n // SHA256(\"global:oracle_update\")[0:8]\n oracleUpdate: new Uint8Array([\n 0x55, 0xd1, 0xf8, 0x8e, 0xba, 0xf9, 0x78, 0xef,\n ]),\n // SHA256(\"global:oracle_consult\")[0:8]\n oracleConsult: new Uint8Array([\n 0xef, 0xed, 0xff, 0xb1, 0x8e, 0x48, 0x60, 0xaf,\n ]),\n // SHA256(\"global:quote_to_numeraire\")[0:8]\n quoteToNumeraire: new Uint8Array([\n 0x04, 0x8e, 0xf9, 0xf0, 0x81, 0x0f, 0x8f, 0x39,\n ]),\n // SHA256(\"global:set_sentinel\")[0:8]\n setSentinel: new Uint8Array([0x5e, 0xc8, 0x52, 0x81, 0x35, 0x95, 0xe8, 0x71]),\n // SHA256(\"global:set_fees\")[0:8]\n setFees: new Uint8Array([0x89, 0xb2, 0x31, 0x3a, 0x00, 0xf5, 0xf2, 0xbe]),\n // SHA256(\"global:set_route\")[0:8]\n setRoute: new Uint8Array([0xf4, 0xe7, 0x03, 0x54, 0xe9, 0x3d, 0x92, 0x95]),\n // SHA256(\"global:transfer_admin\")[0:8]\n transferAdmin: new Uint8Array([\n 0x2a, 0xf2, 0x42, 0x6a, 0xe4, 0x0a, 0x6f, 0x9c,\n ]),\n // SHA256(\"global:pause\")[0:8]\n pause: new Uint8Array([0xd3, 0x16, 0xdd, 0xfb, 0x4a, 0x79, 0xc1, 0x2f]),\n // SHA256(\"global:unpause\")[0:8]\n unpause: new Uint8Array([0xa9, 0x90, 0x04, 0x26, 0x0a, 0x8d, 0xbc, 0xff]),\n // SHA256(\"global:skim\")[0:8]\n skim: new Uint8Array([0xee, 0x78, 0xdd, 0x8a, 0x52, 0x3c, 0x64, 0xda]),\n} as const;\n\n// ============================================================================\n// Account Discriminators (Anchor 8-byte hashes)\n// ============================================================================\n\n/**\n * Anchor account discriminator (first 8 bytes of SHA256(\"account:<AccountName>\"))\n */\nexport const ACCOUNT_DISCRIMINATORS = {\n // SHA256(\"account:AmmConfig\")[0:8]\n AmmConfig: new Uint8Array([0xda, 0xf4, 0x21, 0x68, 0xcb, 0xcb, 0x2b, 0x6f]),\n // SHA256(\"account:Pool\")[0:8]\n Pool: new Uint8Array([0xf1, 0x9a, 0x6d, 0x04, 0x11, 0xb1, 0x6d, 0xbc]),\n // SHA256(\"account:Position\")[0:8]\n Position: new Uint8Array([0xaa, 0xbc, 0x8f, 0xe4, 0x7a, 0x40, 0xf7, 0xd0]),\n // SHA256(\"account:OracleState\")[0:8]\n OracleState: new Uint8Array([0x61, 0x9c, 0x9d, 0xbd, 0xc2, 0x49, 0x08, 0x0f]),\n} as const;\n","import {\n type Address,\n type ProgramDerivedAddress,\n getAddressCodec,\n getProgramDerivedAddress,\n} from '@solana/kit';\nimport {\n CPMM_PROGRAM_ID,\n SEED_CONFIG,\n SEED_POOL,\n SEED_AUTHORITY,\n SEED_POSITION,\n SEED_ORACLE,\n SEED_PROTOCOL_POSITION,\n} from './constants.js';\n\nconst addressCodec = getAddressCodec();\nconst textEncoder = new TextEncoder();\n\n// ============================================================================\n// Token Sorting\n// ============================================================================\n\n/**\n * Sort two mints into canonical order (token0 < token1 by bytes)\n * This is required for Pool PDA derivation and instruction ordering.\n *\n * @param mint0 - First mint address\n * @param mint1 - Second mint address\n * @returns Tuple of [token0, token1] in canonical order\n * @throws Error if mints are equal\n */\nexport function sortMints(mint0: Address, mint1: Address): [Address, Address] {\n const bytesA = addressCodec.encode(mint0);\n const bytesB = addressCodec.encode(mint1);\n\n for (let i = 0; i < 32; i++) {\n if (bytesA[i] < bytesB[i]) {\n return [mint0, mint1];\n }\n if (bytesA[i] > bytesB[i]) {\n return [mint1, mint0];\n }\n }\n\n throw new Error('Mints are equal - cannot create pool with identical tokens');\n}\n\n/**\n * Check if mints are in canonical order\n */\nexport function areMintsOrdered(mint0: Address, mint1: Address): boolean {\n const bytes0 = addressCodec.encode(mint0);\n const bytes1 = addressCodec.encode(mint1);\n\n for (let i = 0; i < 32; i++) {\n if (bytes0[i] < bytes1[i]) return true;\n if (bytes0[i] > bytes1[i]) return false;\n }\n return false; // equal\n}\n\n// ============================================================================\n// PDA Derivation Functions\n// ============================================================================\n\n/**\n * Derive the AmmConfig PDA address\n * Seeds: ['config']\n */\nexport async function getConfigAddress(\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<ProgramDerivedAddress> {\n return getProgramDerivedAddress({\n programAddress: programId,\n seeds: [textEncoder.encode(SEED_CONFIG)],\n });\n}\n\n/**\n * Derive the Pool PDA address for a token pair\n * Seeds: ['pool', token0_mint, token1_mint]\n *\n * Note: Mints will be automatically sorted if not in canonical order.\n */\nexport async function getPoolAddress(\n mint0: Address,\n mint1: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<ProgramDerivedAddress> {\n const [token0, token1] = sortMints(mint0, mint1);\n return getProgramDerivedAddress({\n programAddress: programId,\n seeds: [\n textEncoder.encode(SEED_POOL),\n addressCodec.encode(token0),\n addressCodec.encode(token1),\n ],\n });\n}\n\n/**\n * Derive the Pool authority PDA (vault owner)\n * Seeds: ['authority', pool]\n */\nexport async function getPoolAuthorityAddress(\n pool: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<ProgramDerivedAddress> {\n return getProgramDerivedAddress({\n programAddress: programId,\n seeds: [textEncoder.encode(SEED_AUTHORITY), addressCodec.encode(pool)],\n });\n}\n\n/**\n * Derive the Position PDA address\n * Seeds: ['position', pool, owner, position_id_le_bytes]\n */\nexport async function getPositionAddress(\n pool: Address,\n owner: Address,\n positionId: bigint,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<ProgramDerivedAddress> {\n const positionIdBytes = new Uint8Array(8);\n const view = new DataView(positionIdBytes.buffer);\n view.setBigUint64(0, positionId, true); // little-endian\n\n return getProgramDerivedAddress({\n programAddress: programId,\n seeds: [\n textEncoder.encode(SEED_POSITION),\n addressCodec.encode(pool),\n addressCodec.encode(owner),\n positionIdBytes,\n ],\n });\n}\n\n/**\n * Derive the OracleState PDA address\n * Seeds: ['oracle', pool]\n */\nexport async function getOracleAddress(\n pool: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<ProgramDerivedAddress> {\n return getProgramDerivedAddress({\n programAddress: programId,\n seeds: [textEncoder.encode(SEED_ORACLE), addressCodec.encode(pool)],\n });\n}\n\n/**\n * Derive the Protocol Position PDA address\n * Seeds: ['protocol_position', pool]\n */\nexport async function getProtocolPositionAddress(\n pool: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<ProgramDerivedAddress> {\n return getProgramDerivedAddress({\n programAddress: programId,\n seeds: [\n textEncoder.encode(SEED_PROTOCOL_POSITION),\n addressCodec.encode(pool),\n ],\n });\n}\n\n// ============================================================================\n// Batch PDA Derivation\n// ============================================================================\n\n/**\n * Derive all PDAs needed for pool initialization\n */\nexport async function getPoolInitAddresses(\n mint0: Address,\n mint1: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<{\n token0: Address;\n token1: Address;\n pool: ProgramDerivedAddress;\n authority: ProgramDerivedAddress;\n config: ProgramDerivedAddress;\n protocolPosition: ProgramDerivedAddress;\n}> {\n const [token0, token1] = sortMints(mint0, mint1);\n const [config, pool] = await Promise.all([\n getConfigAddress(programId),\n getPoolAddress(token0, token1, programId),\n ]);\n const [authority, protocolPosition] = await Promise.all([\n getPoolAuthorityAddress(pool[0], programId),\n getProtocolPositionAddress(pool[0], programId),\n ]);\n\n return {\n token0,\n token1,\n pool,\n authority,\n config,\n protocolPosition,\n };\n}\n\n/**\n * Derive all PDAs needed for swap operation\n */\nexport async function getSwapAddresses(\n pool: Address,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<{\n config: ProgramDerivedAddress;\n authority: ProgramDerivedAddress;\n oracle: ProgramDerivedAddress;\n}> {\n const [config, authority, oracle] = await Promise.all([\n getConfigAddress(programId),\n getPoolAuthorityAddress(pool, programId),\n getOracleAddress(pool, programId),\n ]);\n\n return { config, authority, oracle };\n}\n\n/**\n * Derive all PDAs needed for liquidity operations\n */\nexport async function getLiquidityAddresses(\n pool: Address,\n owner: Address,\n positionId: bigint,\n programId: Address = CPMM_PROGRAM_ID,\n): Promise<{\n config: ProgramDerivedAddress;\n authority: ProgramDerivedAddress;\n position: ProgramDerivedAddress;\n protocolPosition: ProgramDerivedAddress;\n oracle: ProgramDerivedAddress;\n}> {\n const [config, authority, position, protocolPosition, oracle] =\n await Promise.all([\n getConfigAddress(programId),\n getPoolAuthorityAddress(pool, programId),\n getPositionAddress(pool, owner, positionId, programId),\n getProtocolPositionAddress(pool, programId),\n getOracleAddress(pool, programId),\n ]);\n\n return { config, authority, position, protocolPosition, oracle };\n}\n"]}