@whetstone-research/doppler-sdk 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -13
- package/dist/{chunk-AT33RY6T.cjs → chunk-3BW6IVP3.cjs} +46 -37
- package/dist/chunk-3BW6IVP3.cjs.map +1 -0
- package/dist/{chunk-ZQEEGBXT.js → chunk-NMTYI3TB.js} +23 -26
- package/dist/chunk-NMTYI3TB.js.map +1 -0
- package/dist/{chunk-2NFQKIOB.js → chunk-TVBSIQIX.js} +35 -26
- package/dist/chunk-TVBSIQIX.js.map +1 -0
- package/dist/{chunk-LL7TW3GU.cjs → chunk-W5TMS4RD.cjs} +56 -59
- package/dist/chunk-W5TMS4RD.cjs.map +1 -0
- package/dist/evm/index.cjs +226 -115
- package/dist/evm/index.cjs.map +1 -1
- package/dist/evm/index.d.cts +6 -0
- package/dist/evm/index.d.ts +6 -0
- package/dist/evm/index.js +227 -116
- package/dist/evm/index.js.map +1 -1
- package/dist/{oracle-CU-nZnja.d.cts → oracle-DN8BrGPh.d.cts} +28 -28
- package/dist/{oracle-CU-nZnja.d.ts → oracle-DN8BrGPh.d.ts} +28 -28
- package/dist/{pda-FLNTBIC5.js → pda-5D3GD5S6.js} +3 -3
- package/dist/pda-5D3GD5S6.js.map +1 -0
- package/dist/{pda-G25AJSXG.cjs → pda-GNZYNRUH.cjs} +17 -17
- package/dist/{pda-G25AJSXG.cjs.map → pda-GNZYNRUH.cjs.map} +1 -1
- package/dist/solana/index.cjs +875 -740
- package/dist/solana/index.cjs.map +1 -1
- package/dist/solana/index.d.cts +545 -471
- package/dist/solana/index.d.ts +545 -471
- package/dist/solana/index.js +738 -603
- package/dist/solana/index.js.map +1 -1
- package/dist/solana/react/index.cjs +57 -57
- package/dist/solana/react/index.cjs.map +1 -1
- package/dist/solana/react/index.d.cts +5 -5
- package/dist/solana/react/index.d.ts +5 -5
- package/dist/solana/react/index.js +15 -15
- package/dist/solana/react/index.js.map +1 -1
- package/package.json +4 -2
- package/dist/chunk-2NFQKIOB.js.map +0 -1
- package/dist/chunk-AT33RY6T.cjs.map +0 -1
- package/dist/chunk-LL7TW3GU.cjs.map +0 -1
- package/dist/chunk-ZQEEGBXT.js.map +0 -1
- package/dist/pda-FLNTBIC5.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunk3BW6IVP3_cjs = require('./chunk-3BW6IVP3.cjs');
|
|
4
4
|
var kit = require('@solana/kit');
|
|
5
5
|
|
|
6
6
|
var addressCodec = kit.getAddressCodec();
|
|
@@ -49,11 +49,8 @@ var ammConfigDataCodec = kit.getStructCodec([
|
|
|
49
49
|
["admin", addressCodec],
|
|
50
50
|
["paused", boolCodec],
|
|
51
51
|
["numeraireMint", addressCodec],
|
|
52
|
-
["
|
|
53
|
-
[
|
|
54
|
-
"sentinelAllowlist",
|
|
55
|
-
kit.getArrayCodec(addressCodec, { size: chunkAT33RY6T_cjs.MAX_SENTINEL_ALLOWLIST })
|
|
56
|
-
],
|
|
52
|
+
["hookAllowlistLen", u8Codec],
|
|
53
|
+
["hookAllowlist", kit.getArrayCodec(addressCodec, { size: chunk3BW6IVP3_cjs.MAX_HOOK_ALLOWLIST })],
|
|
57
54
|
["maxSwapFeeBps", u16Codec],
|
|
58
55
|
["maxFeeSplitBps", u16Codec],
|
|
59
56
|
["maxRouteHops", u8Codec],
|
|
@@ -79,14 +76,14 @@ var poolDataCodec = kit.getStructCodec([
|
|
|
79
76
|
["feeGrowthGlobal1Q64", u128Codec],
|
|
80
77
|
["feesUnclaimed0", u64Codec],
|
|
81
78
|
["feesUnclaimed1", u64Codec],
|
|
82
|
-
["
|
|
83
|
-
["
|
|
79
|
+
["hookProgram", addressCodec],
|
|
80
|
+
["hookFlags", u32Codec],
|
|
84
81
|
["numeraireMint", addressCodec],
|
|
85
|
-
["
|
|
82
|
+
["liquidityMeasureTokenIndex", u8Codec],
|
|
86
83
|
["routeNextPool", addressCodec],
|
|
87
84
|
["routeBridgeMint", addressCodec],
|
|
88
85
|
["kLast", u128Codec],
|
|
89
|
-
["
|
|
86
|
+
["protocolFeePosition", addressCodec],
|
|
90
87
|
["locked", u8Codec],
|
|
91
88
|
["version", u8Codec],
|
|
92
89
|
["reserved", reservedBytesCodec]
|
|
@@ -120,22 +117,22 @@ var oracleStateDataCodec = kit.getStructCodec([
|
|
|
120
117
|
["observationIndex", u16Codec],
|
|
121
118
|
[
|
|
122
119
|
"observations",
|
|
123
|
-
kit.getArrayCodec(observationCodec, { size:
|
|
120
|
+
kit.getArrayCodec(observationCodec, { size: chunk3BW6IVP3_cjs.MAX_ORACLE_OBSERVATIONS })
|
|
124
121
|
],
|
|
125
122
|
["version", u8Codec],
|
|
126
123
|
["reserved", reservedBytesCodec]
|
|
127
124
|
]);
|
|
128
125
|
var ammConfigDecoder = kit.getHiddenPrefixDecoder(ammConfigDataCodec, [
|
|
129
|
-
kit.getConstantDecoder(
|
|
126
|
+
kit.getConstantDecoder(chunk3BW6IVP3_cjs.ACCOUNT_DISCRIMINATORS.AmmConfig)
|
|
130
127
|
]);
|
|
131
128
|
var poolDecoder = kit.getHiddenPrefixDecoder(poolDataCodec, [
|
|
132
|
-
kit.getConstantDecoder(
|
|
129
|
+
kit.getConstantDecoder(chunk3BW6IVP3_cjs.ACCOUNT_DISCRIMINATORS.Pool)
|
|
133
130
|
]);
|
|
134
131
|
var positionDecoder = kit.getHiddenPrefixDecoder(positionDataCodec, [
|
|
135
|
-
kit.getConstantDecoder(
|
|
132
|
+
kit.getConstantDecoder(chunk3BW6IVP3_cjs.ACCOUNT_DISCRIMINATORS.Position)
|
|
136
133
|
]);
|
|
137
134
|
var oracleStateDecoder = kit.getHiddenPrefixDecoder(oracleStateDataCodec, [
|
|
138
|
-
kit.getConstantDecoder(
|
|
135
|
+
kit.getConstantDecoder(chunk3BW6IVP3_cjs.ACCOUNT_DISCRIMINATORS.OracleState)
|
|
139
136
|
]);
|
|
140
137
|
function decodeAmmConfig(data) {
|
|
141
138
|
return ammConfigDecoder.decode(data);
|
|
@@ -169,7 +166,7 @@ var optionAddressCodec = kit.transformCodec(
|
|
|
169
166
|
var swapExactInArgsCodec = kit.getStructCodec([
|
|
170
167
|
["amountIn", u64Codec],
|
|
171
168
|
["minAmountOut", u64Codec],
|
|
172
|
-
["
|
|
169
|
+
["tradeDirection", u8Codec],
|
|
173
170
|
["updateOracle", boolCodec]
|
|
174
171
|
]);
|
|
175
172
|
var addLiquidityArgsWithOracleCodec = kit.getStructCodec([
|
|
@@ -209,14 +206,14 @@ var initializeConfigArgsCodec = kit.getStructCodec([
|
|
|
209
206
|
["maxRouteHops", u8Codec],
|
|
210
207
|
["protocolFeeEnabled", boolCodec],
|
|
211
208
|
["protocolFeeBps", u16Codec],
|
|
212
|
-
["
|
|
209
|
+
["hookAllowlist", kit.getArrayCodec(addressCodec, { size: u32Codec })]
|
|
213
210
|
]);
|
|
214
211
|
var initializePoolArgsCodec = kit.getStructCodec([
|
|
215
212
|
["mintA", addressCodec],
|
|
216
213
|
["mintB", addressCodec],
|
|
217
214
|
["initialSwapFeeBps", u16Codec],
|
|
218
215
|
["initialFeeSplitBps", u16Codec],
|
|
219
|
-
["
|
|
216
|
+
["liquidityMeasureTokenIndex", u8Codec],
|
|
220
217
|
["numeraireMintOverride", optionAddressCodec]
|
|
221
218
|
]);
|
|
222
219
|
var initializeOracleArgsCodec = kit.getStructCodec([
|
|
@@ -224,9 +221,9 @@ var initializeOracleArgsCodec = kit.getStructCodec([
|
|
|
224
221
|
["observationIntervalSec", u32Codec],
|
|
225
222
|
["numObservations", u16Codec]
|
|
226
223
|
]);
|
|
227
|
-
var
|
|
228
|
-
["
|
|
229
|
-
["
|
|
224
|
+
var setHookArgsCodec = kit.getStructCodec([
|
|
225
|
+
["hookProgram", addressCodec],
|
|
226
|
+
["hookFlags", u32Codec]
|
|
230
227
|
]);
|
|
231
228
|
var setFeesArgsCodec = kit.getStructCodec([
|
|
232
229
|
["swapFeeBps", u16Codec],
|
|
@@ -244,7 +241,7 @@ var oracleConsultArgsCodec = kit.getStructCodec([
|
|
|
244
241
|
]);
|
|
245
242
|
var quoteToNumeraireArgsCodec = kit.getStructCodec([
|
|
246
243
|
["amount", u128Codec],
|
|
247
|
-
["
|
|
244
|
+
["inputTokenIndex", u8Codec],
|
|
248
245
|
["maxHops", u8Codec],
|
|
249
246
|
["useTwap", boolCodec],
|
|
250
247
|
["windowSeconds", u32Codec]
|
|
@@ -276,8 +273,8 @@ function encodeInitializePoolArgs(args) {
|
|
|
276
273
|
function encodeInitializeOracleArgs(args) {
|
|
277
274
|
return new Uint8Array(initializeOracleArgsCodec.encode(args));
|
|
278
275
|
}
|
|
279
|
-
function
|
|
280
|
-
return new Uint8Array(
|
|
276
|
+
function encodeSetHookArgs(args) {
|
|
277
|
+
return new Uint8Array(setHookArgsCodec.encode(args));
|
|
281
278
|
}
|
|
282
279
|
function encodeSetFeesArgs(args) {
|
|
283
280
|
return new Uint8Array(setFeesArgsCodec.encode(args));
|
|
@@ -299,12 +296,12 @@ function encodeQuoteToNumeraireArgs(args) {
|
|
|
299
296
|
function q64ToNumber(q64) {
|
|
300
297
|
const intPart = q64 >> 64n;
|
|
301
298
|
const fracPart = q64 & (1n << 64n) - 1n;
|
|
302
|
-
return Number(intPart) + Number(fracPart) / Number(
|
|
299
|
+
return Number(intPart) + Number(fracPart) / Number(chunk3BW6IVP3_cjs.Q64_ONE);
|
|
303
300
|
}
|
|
304
301
|
function numberToQ64(n) {
|
|
305
302
|
const intPart = Math.floor(n);
|
|
306
303
|
const fracPart = n - intPart;
|
|
307
|
-
return (BigInt(intPart) << 64n) + BigInt(Math.round(fracPart * Number(
|
|
304
|
+
return (BigInt(intPart) << 64n) + BigInt(Math.round(fracPart * Number(chunk3BW6IVP3_cjs.Q64_ONE)));
|
|
308
305
|
}
|
|
309
306
|
function q64Mul(a, b) {
|
|
310
307
|
return a * b >> 64n;
|
|
@@ -345,7 +342,7 @@ function ratioToNumber(numerator, denominator) {
|
|
|
345
342
|
const q64 = (numerator << 64n) / denominator;
|
|
346
343
|
return q64ToNumber(q64);
|
|
347
344
|
}
|
|
348
|
-
function getSwapQuote(pool, amountIn,
|
|
345
|
+
function getSwapQuote(pool, amountIn, tradeDirection) {
|
|
349
346
|
if (amountIn === 0n) {
|
|
350
347
|
return {
|
|
351
348
|
amountOut: 0n,
|
|
@@ -356,12 +353,12 @@ function getSwapQuote(pool, amountIn, direction) {
|
|
|
356
353
|
executionPrice: 0
|
|
357
354
|
};
|
|
358
355
|
}
|
|
359
|
-
const [reserveIn, reserveOut] =
|
|
356
|
+
const [reserveIn, reserveOut] = tradeDirection === 0 ? [pool.reserve0, pool.reserve1] : [pool.reserve1, pool.reserve0];
|
|
360
357
|
if (reserveIn === 0n || reserveOut === 0n) {
|
|
361
358
|
throw new Error("Pool has zero liquidity");
|
|
362
359
|
}
|
|
363
|
-
const feeTotal = amountIn * BigInt(pool.swapFeeBps) /
|
|
364
|
-
const feeDist = feeTotal * BigInt(pool.feeSplitBps) /
|
|
360
|
+
const feeTotal = amountIn * BigInt(pool.swapFeeBps) / chunk3BW6IVP3_cjs.BPS_DENOM;
|
|
361
|
+
const feeDist = feeTotal * BigInt(pool.feeSplitBps) / chunk3BW6IVP3_cjs.BPS_DENOM;
|
|
365
362
|
const feeComp = feeTotal - feeDist;
|
|
366
363
|
const amountInEff = amountIn - feeTotal;
|
|
367
364
|
const amountOut = amountInEff * reserveOut / (reserveIn + amountInEff);
|
|
@@ -377,8 +374,8 @@ function getSwapQuote(pool, amountIn, direction) {
|
|
|
377
374
|
executionPrice
|
|
378
375
|
};
|
|
379
376
|
}
|
|
380
|
-
function getSwapQuoteExactOut(pool, amountOut,
|
|
381
|
-
const [reserveIn, reserveOut] =
|
|
377
|
+
function getSwapQuoteExactOut(pool, amountOut, tradeDirection) {
|
|
378
|
+
const [reserveIn, reserveOut] = tradeDirection === 0 ? [pool.reserve0, pool.reserve1] : [pool.reserve1, pool.reserve0];
|
|
382
379
|
if (reserveIn === 0n || reserveOut === 0n) {
|
|
383
380
|
throw new Error("Pool has zero liquidity");
|
|
384
381
|
}
|
|
@@ -387,8 +384,8 @@ function getSwapQuoteExactOut(pool, amountOut, direction) {
|
|
|
387
384
|
}
|
|
388
385
|
const amountInEff = ceilDiv(amountOut * reserveIn, reserveOut - amountOut);
|
|
389
386
|
const amountIn = ceilDiv(
|
|
390
|
-
amountInEff *
|
|
391
|
-
|
|
387
|
+
amountInEff * chunk3BW6IVP3_cjs.BPS_DENOM,
|
|
388
|
+
chunk3BW6IVP3_cjs.BPS_DENOM - BigInt(pool.swapFeeBps)
|
|
392
389
|
);
|
|
393
390
|
const feeTotal = amountIn - amountInEff;
|
|
394
391
|
return { amountIn, feeTotal };
|
|
@@ -516,11 +513,11 @@ async function fetchPool(rpc, address, config) {
|
|
|
516
513
|
return decodePool(base64ToBytes(response.value.data[0]));
|
|
517
514
|
}
|
|
518
515
|
async function fetchAllPools(rpc, config) {
|
|
519
|
-
const programId = config?.programId ??
|
|
516
|
+
const programId = config?.programId ?? chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID;
|
|
520
517
|
const discriminatorFilter = {
|
|
521
518
|
memcmp: {
|
|
522
519
|
offset: 0n,
|
|
523
|
-
bytes: bytesToBase64(
|
|
520
|
+
bytes: bytesToBase64(chunk3BW6IVP3_cjs.ACCOUNT_DISCRIMINATORS.Pool),
|
|
524
521
|
encoding: "base64"
|
|
525
522
|
}
|
|
526
523
|
};
|
|
@@ -545,8 +542,8 @@ async function fetchAllPools(rpc, config) {
|
|
|
545
542
|
return pools;
|
|
546
543
|
}
|
|
547
544
|
async function getPoolByMints(rpc, mint0, mint1, config) {
|
|
548
|
-
const programId = config?.programId ??
|
|
549
|
-
const [poolAddress] = await
|
|
545
|
+
const programId = config?.programId ?? chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID;
|
|
546
|
+
const [poolAddress] = await chunk3BW6IVP3_cjs.getPoolAddress(mint0, mint1, programId);
|
|
550
547
|
const pool = await fetchPool(rpc, poolAddress, config);
|
|
551
548
|
if (!pool) {
|
|
552
549
|
return null;
|
|
@@ -573,9 +570,9 @@ async function poolExists(rpc, mint0, mint1, config) {
|
|
|
573
570
|
const result = await getPoolByMints(rpc, mint0, mint1, config);
|
|
574
571
|
return result !== null;
|
|
575
572
|
}
|
|
576
|
-
async function getPoolAddressFromMints(mint0, mint1, programId =
|
|
577
|
-
const [token0, token1] =
|
|
578
|
-
const [poolAddress] = await
|
|
573
|
+
async function getPoolAddressFromMints(mint0, mint1, programId = chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID) {
|
|
574
|
+
const [token0, token1] = chunk3BW6IVP3_cjs.sortMints(mint0, mint1);
|
|
575
|
+
const [poolAddress] = await chunk3BW6IVP3_cjs.getPoolAddress(mint0, mint1, programId);
|
|
579
576
|
return {
|
|
580
577
|
poolAddress,
|
|
581
578
|
token0,
|
|
@@ -623,14 +620,14 @@ async function fetchPosition(rpc, address, config) {
|
|
|
623
620
|
return decodePosition(base64ToBytes2(response.value.data[0]));
|
|
624
621
|
}
|
|
625
622
|
async function fetchUserPositions(rpc, owner, pool, config) {
|
|
626
|
-
const programId = config?.programId ??
|
|
623
|
+
const programId = config?.programId ?? chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID;
|
|
627
624
|
const filters = [
|
|
628
625
|
// Discriminator filter (first 8 bytes)
|
|
629
626
|
{
|
|
630
627
|
memcmp: {
|
|
631
628
|
offset: 0n,
|
|
632
629
|
bytes: bytesToBase642(
|
|
633
|
-
|
|
630
|
+
chunk3BW6IVP3_cjs.ACCOUNT_DISCRIMINATORS.Position
|
|
634
631
|
),
|
|
635
632
|
encoding: "base64"
|
|
636
633
|
}
|
|
@@ -674,14 +671,14 @@ async function fetchUserPositions(rpc, owner, pool, config) {
|
|
|
674
671
|
return positions;
|
|
675
672
|
}
|
|
676
673
|
async function fetchPoolPositions(rpc, pool, config) {
|
|
677
|
-
const programId = config?.programId ??
|
|
674
|
+
const programId = config?.programId ?? chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID;
|
|
678
675
|
const filters = [
|
|
679
676
|
// Discriminator filter
|
|
680
677
|
{
|
|
681
678
|
memcmp: {
|
|
682
679
|
offset: 0n,
|
|
683
680
|
bytes: bytesToBase642(
|
|
684
|
-
|
|
681
|
+
chunk3BW6IVP3_cjs.ACCOUNT_DISCRIMINATORS.Position
|
|
685
682
|
),
|
|
686
683
|
encoding: "base64"
|
|
687
684
|
}
|
|
@@ -742,8 +739,8 @@ function getPositionValue(pool, position) {
|
|
|
742
739
|
};
|
|
743
740
|
}
|
|
744
741
|
async function fetchPositionByParams(rpc, pool, owner, positionId, config) {
|
|
745
|
-
const programId = config?.programId ??
|
|
746
|
-
const [address] = await
|
|
742
|
+
const programId = config?.programId ?? chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID;
|
|
743
|
+
const [address] = await chunk3BW6IVP3_cjs.getPositionAddress(
|
|
747
744
|
pool,
|
|
748
745
|
owner,
|
|
749
746
|
positionId,
|
|
@@ -758,8 +755,8 @@ async function fetchPositionByParams(rpc, pool, owner, positionId, config) {
|
|
|
758
755
|
account: position
|
|
759
756
|
};
|
|
760
757
|
}
|
|
761
|
-
async function getPositionAddressFromParams(pool, owner, positionId, programId =
|
|
762
|
-
const [address] = await
|
|
758
|
+
async function getPositionAddressFromParams(pool, owner, positionId, programId = chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID) {
|
|
759
|
+
const [address] = await chunk3BW6IVP3_cjs.getPositionAddress(
|
|
763
760
|
pool,
|
|
764
761
|
owner,
|
|
765
762
|
positionId,
|
|
@@ -810,8 +807,8 @@ async function fetchOracle(rpc, address, config) {
|
|
|
810
807
|
return decodeOracleState(base64ToBytes3(response.value.data[0]));
|
|
811
808
|
}
|
|
812
809
|
async function getOracleForPool(rpc, pool, config) {
|
|
813
|
-
const programId = config?.programId ??
|
|
814
|
-
const [oracleAddress] = await
|
|
810
|
+
const programId = config?.programId ?? chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID;
|
|
811
|
+
const [oracleAddress] = await chunk3BW6IVP3_cjs.getOracleAddress(pool, programId);
|
|
815
812
|
const oracle = await fetchOracle(rpc, oracleAddress, config);
|
|
816
813
|
if (!oracle) {
|
|
817
814
|
return null;
|
|
@@ -821,8 +818,8 @@ async function getOracleForPool(rpc, pool, config) {
|
|
|
821
818
|
account: oracle
|
|
822
819
|
};
|
|
823
820
|
}
|
|
824
|
-
async function getOracleAddressFromPool(pool, programId =
|
|
825
|
-
const [address] = await
|
|
821
|
+
async function getOracleAddressFromPool(pool, programId = chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID) {
|
|
822
|
+
const [address] = await chunk3BW6IVP3_cjs.getOracleAddress(pool, programId);
|
|
826
823
|
return address;
|
|
827
824
|
}
|
|
828
825
|
function consultTwap(oracle, windowSeconds, currentTimestamp) {
|
|
@@ -945,10 +942,10 @@ function getOracleBufferStats(oracle) {
|
|
|
945
942
|
};
|
|
946
943
|
}
|
|
947
944
|
async function fetchOraclesBatch(rpc, pools, config) {
|
|
948
|
-
const programId = config?.programId ??
|
|
945
|
+
const programId = config?.programId ?? chunk3BW6IVP3_cjs.CPMM_PROGRAM_ID;
|
|
949
946
|
const oracles = /* @__PURE__ */ new Map();
|
|
950
947
|
const oracleAddresses = await Promise.all(
|
|
951
|
-
pools.map((pool) =>
|
|
948
|
+
pools.map((pool) => chunk3BW6IVP3_cjs.getOracleAddress(pool, programId))
|
|
952
949
|
);
|
|
953
950
|
const results = await Promise.all(
|
|
954
951
|
oracleAddresses.map(([addr]) => fetchOracle(rpc, addr, config))
|
|
@@ -1004,8 +1001,8 @@ exports.encodeOracleConsultArgs = encodeOracleConsultArgs;
|
|
|
1004
1001
|
exports.encodeQuoteToNumeraireArgs = encodeQuoteToNumeraireArgs;
|
|
1005
1002
|
exports.encodeRemoveLiquidityArgs = encodeRemoveLiquidityArgs;
|
|
1006
1003
|
exports.encodeSetFeesArgs = encodeSetFeesArgs;
|
|
1004
|
+
exports.encodeSetHookArgs = encodeSetHookArgs;
|
|
1007
1005
|
exports.encodeSetRouteArgs = encodeSetRouteArgs;
|
|
1008
|
-
exports.encodeSetSentinelArgs = encodeSetSentinelArgs;
|
|
1009
1006
|
exports.encodeSwapExactInArgs = encodeSwapExactInArgs;
|
|
1010
1007
|
exports.encodeTransferAdminArgs = encodeTransferAdminArgs;
|
|
1011
1008
|
exports.fetchAllPools = fetchAllPools;
|
|
@@ -1060,11 +1057,11 @@ exports.quoteToNumeraireArgsCodec = quoteToNumeraireArgsCodec;
|
|
|
1060
1057
|
exports.ratioToNumber = ratioToNumber;
|
|
1061
1058
|
exports.removeLiquidityArgsCodec = removeLiquidityArgsCodec;
|
|
1062
1059
|
exports.setFeesArgsCodec = setFeesArgsCodec;
|
|
1060
|
+
exports.setHookArgsCodec = setHookArgsCodec;
|
|
1063
1061
|
exports.setRouteArgsCodec = setRouteArgsCodec;
|
|
1064
|
-
exports.setSentinelArgsCodec = setSentinelArgsCodec;
|
|
1065
1062
|
exports.sortPoolsByReserves = sortPoolsByReserves;
|
|
1066
1063
|
exports.sortPositionsByShares = sortPositionsByShares;
|
|
1067
1064
|
exports.swapExactInArgsCodec = swapExactInArgsCodec;
|
|
1068
1065
|
exports.transferAdminArgsCodec = transferAdminArgsCodec;
|
|
1069
|
-
//# sourceMappingURL=chunk-
|
|
1070
|
-
//# sourceMappingURL=chunk-
|
|
1066
|
+
//# sourceMappingURL=chunk-W5TMS4RD.cjs.map
|
|
1067
|
+
//# sourceMappingURL=chunk-W5TMS4RD.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/solana/core/codecs.ts","../src/solana/core/math.ts","../src/solana/client/pool.ts","../src/solana/client/position.ts","../src/solana/client/oracle.ts"],"names":["getAddressCodec","getBooleanCodec","getU8Codec","getU16Codec","getU32Codec","getU64Codec","getU128Codec","transformCodec","getArrayCodec","fixCodecSize","getBytesCodec","getStructCodec","MAX_HOOK_ALLOWLIST","MAX_ORACLE_OBSERVATIONS","getHiddenPrefixDecoder","getConstantDecoder","ACCOUNT_DISCRIMINATORS","mergeBytes","getOptionCodec","unwrapOption","Q64_ONE","BPS_DENOM","sharesOut","CPMM_PROGRAM_ID","getPoolAddress","sortMints","bytesToBase64","base64ToBytes","getPositionAddress","getOracleAddress"],"mappings":";;;;;AAwDA,IAAM,eAAeA,mBAAA,EAAgB;AACrC,IAAM,YAAYC,mBAAA,EAAgB;AAClC,IAAM,UAAUC,cAAA,EAAW;AAC3B,IAAM,WAAWC,eAAA,EAAY;AAC7B,IAAM,WAAWC,eAAA,EAAY;AAC7B,IAAM,WAAWC,eAAA,EAAY;AAC7B,IAAM,YAAYC,gBAAA,EAAa;AAC/B,IAAM,SAAA,GAA2BC,kBAAA;AAAA,EAC/BC,iBAAA,CAAc,QAAA,EAAU,EAAE,IAAA,EAAM,GAAG,CAAA;AAAA,EACnC,CAAC,KAAA,KAAU;AACT,IAAA,IAAI,QAAQ,EAAA,EAAI;AACd,MAAA,MAAM,IAAI,MAAM,yBAAyB,CAAA;AAAA,IAC3C;AACA,IAAA,IAAI,KAAA,IAAS,SAAS,EAAA,EAAI;AACxB,MAAA,MAAM,IAAI,MAAM,eAAe,CAAA;AAAA,IACjC;AACA,IAAA,MAAM,IAAA,GAAA,CAAQ,MAAM,GAAA,IAAO,EAAA;AAC3B,IAAA,OAAO;AAAA,MACL,KAAA,GAAQ,IAAA;AAAA,MACP,SAAS,GAAA,GAAO,IAAA;AAAA,MAChB,SAAS,IAAA,GAAQ,IAAA;AAAA,MACjB,SAAS,IAAA,GAAQ;AAAA,KACpB;AAAA,EACF,CAAA;AAAA,EACA,CAAC,KAAA,KAAU;AACT,IAAA,IAAI,GAAA,GAAM,EAAA;AACV,IAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,CAAA,EAAG,KAAK,CAAA,EAAG;AAC7B,MAAA,GA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,EAAS;AAAA,OACV,CAAA;AAAA,IACH;AAAA,EACF;AAEA,EAAA,OAAO,OAAA;AACT;AAWO,SAAS,0BAAA,CACd,MACA,MAAA,EAQA;AACA,EAAA,MAAM,UAAA,GACJ,IAAA,CAAK,QAAA,GAAW,EAAA,GAAK,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA,GAAI,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-W5TMS4RD.cjs","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_HOOK_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 SetHookArgs,\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 ['hookAllowlistLen', u8Codec],\n ['hookAllowlist', getArrayCodec(addressCodec, { size: MAX_HOOK_ALLOWLIST })],\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 ['hookProgram', addressCodec],\n ['hookFlags', u32Codec],\n ['numeraireMint', addressCodec],\n ['liquidityMeasureTokenIndex', u8Codec],\n ['routeNextPool', addressCodec],\n ['routeBridgeMint', addressCodec],\n ['kLast', u128Codec],\n ['protocolFeePosition', 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 ['tradeDirection', 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 ['hookAllowlist', 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 ['liquidityMeasureTokenIndex', 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 setHookArgsCodec: Codec<SetHookArgs> = getStructCodec([\n ['hookProgram', addressCodec],\n ['hookFlags', 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 ['inputTokenIndex', 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 SetHook args */\nexport function encodeSetHookArgs(args: SetHookArgs): Uint8Array {\n return new Uint8Array(setHookArgsCodec.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 TradeDirection,\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 tradeDirection: TradeDirection,\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 trade direction\n const [reserveIn, reserveOut] =\n tradeDirection === 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 tradeDirection: TradeDirection,\n): SwapQuoteExactOut {\n // Get reserves based on trade direction\n const [reserveIn, reserveOut] =\n tradeDirection === 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: TradeDirection = 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","/**\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"]}
|