@whetstone-research/doppler-sdk 1.0.2 → 1.0.4
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 +23 -19
- package/dist/chunk-BXATWUGJ.cjs +1111 -0
- package/dist/chunk-BXATWUGJ.cjs.map +1 -0
- package/dist/chunk-DPKVNI6Q.cjs +351 -0
- package/dist/chunk-DPKVNI6Q.cjs.map +1 -0
- package/dist/chunk-Q7SFCCGT.cjs +11 -0
- package/dist/chunk-Q7SFCCGT.cjs.map +1 -0
- package/dist/evm/index.cjs +15410 -0
- package/dist/evm/index.cjs.map +1 -0
- package/dist/evm/index.d.cts +8533 -0
- package/dist/oracle-CU-nZnja.d.cts +826 -0
- package/dist/pda-ZZMBZSFU.cjs +53 -0
- package/dist/pda-ZZMBZSFU.cjs.map +1 -0
- package/dist/solana/index.cjs +4981 -0
- package/dist/solana/index.cjs.map +1 -0
- package/dist/solana/index.d.cts +4584 -0
- package/dist/solana/react/index.cjs +1465 -0
- package/dist/solana/react/index.cjs.map +1 -0
- package/dist/solana/react/index.d.cts +1046 -0
- package/package.json +7 -4
|
@@ -0,0 +1,1111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkDPKVNI6Q_cjs = require('./chunk-DPKVNI6Q.cjs');
|
|
4
|
+
var kit = require('@solana/kit');
|
|
5
|
+
|
|
6
|
+
var addressCodec = kit.getAddressCodec();
|
|
7
|
+
var boolCodec = kit.getBooleanCodec();
|
|
8
|
+
var u8Codec = kit.getU8Codec();
|
|
9
|
+
var u16Codec = kit.getU16Codec();
|
|
10
|
+
var u32Codec = kit.getU32Codec();
|
|
11
|
+
var u64Codec = kit.getU64Codec();
|
|
12
|
+
var u128Codec = kit.getU128Codec();
|
|
13
|
+
var u256Codec = kit.transformCodec(
|
|
14
|
+
kit.getArrayCodec(u64Codec, { size: 4 }),
|
|
15
|
+
(value) => {
|
|
16
|
+
if (value < 0n) {
|
|
17
|
+
throw new Error("u256 cannot be negative");
|
|
18
|
+
}
|
|
19
|
+
if (value >> 256n !== 0n) {
|
|
20
|
+
throw new Error("u256 overflow");
|
|
21
|
+
}
|
|
22
|
+
const mask = (1n << 64n) - 1n;
|
|
23
|
+
return [
|
|
24
|
+
value & mask,
|
|
25
|
+
value >> 64n & mask,
|
|
26
|
+
value >> 128n & mask,
|
|
27
|
+
value >> 192n & mask
|
|
28
|
+
];
|
|
29
|
+
},
|
|
30
|
+
(value) => {
|
|
31
|
+
let out = 0n;
|
|
32
|
+
for (let i = 0; i < 4; i += 1) {
|
|
33
|
+
out |= value[i] << 64n * BigInt(i);
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
var reservedBytesCodec = kit.transformCodec(
|
|
39
|
+
kit.fixCodecSize(kit.getBytesCodec(), 7),
|
|
40
|
+
(value) => value,
|
|
41
|
+
(value) => new Uint8Array(value)
|
|
42
|
+
);
|
|
43
|
+
var observationCodec = kit.getStructCodec([
|
|
44
|
+
["timestamp", u32Codec],
|
|
45
|
+
["price0Cumulative", u256Codec],
|
|
46
|
+
["price1Cumulative", u256Codec]
|
|
47
|
+
]);
|
|
48
|
+
var ammConfigDataCodec = kit.getStructCodec([
|
|
49
|
+
["admin", addressCodec],
|
|
50
|
+
["paused", boolCodec],
|
|
51
|
+
["numeraireMint", addressCodec],
|
|
52
|
+
["sentinelAllowlistLen", u8Codec],
|
|
53
|
+
[
|
|
54
|
+
"sentinelAllowlist",
|
|
55
|
+
kit.getArrayCodec(addressCodec, { size: chunkDPKVNI6Q_cjs.MAX_SENTINEL_ALLOWLIST })
|
|
56
|
+
],
|
|
57
|
+
["maxSwapFeeBps", u16Codec],
|
|
58
|
+
["maxFeeSplitBps", u16Codec],
|
|
59
|
+
["maxRouteHops", u8Codec],
|
|
60
|
+
["protocolFeeEnabled", boolCodec],
|
|
61
|
+
["protocolFeeBps", u16Codec],
|
|
62
|
+
["version", u8Codec],
|
|
63
|
+
["reserved", reservedBytesCodec]
|
|
64
|
+
]);
|
|
65
|
+
var poolDataCodec = kit.getStructCodec([
|
|
66
|
+
["config", addressCodec],
|
|
67
|
+
["token0Mint", addressCodec],
|
|
68
|
+
["token1Mint", addressCodec],
|
|
69
|
+
["vault0", addressCodec],
|
|
70
|
+
["vault1", addressCodec],
|
|
71
|
+
["authority", addressCodec],
|
|
72
|
+
["bump", u8Codec],
|
|
73
|
+
["reserve0", u64Codec],
|
|
74
|
+
["reserve1", u64Codec],
|
|
75
|
+
["totalShares", u128Codec],
|
|
76
|
+
["swapFeeBps", u16Codec],
|
|
77
|
+
["feeSplitBps", u16Codec],
|
|
78
|
+
["feeGrowthGlobal0Q64", u128Codec],
|
|
79
|
+
["feeGrowthGlobal1Q64", u128Codec],
|
|
80
|
+
["feesUnclaimed0", u64Codec],
|
|
81
|
+
["feesUnclaimed1", u64Codec],
|
|
82
|
+
["sentinelProgram", addressCodec],
|
|
83
|
+
["sentinelFlags", u32Codec],
|
|
84
|
+
["numeraireMint", addressCodec],
|
|
85
|
+
["liquidityMeasureSide", u8Codec],
|
|
86
|
+
["routeNextPool", addressCodec],
|
|
87
|
+
["routeBridgeMint", addressCodec],
|
|
88
|
+
["kLast", u128Codec],
|
|
89
|
+
["protocolPosition", addressCodec],
|
|
90
|
+
["locked", u8Codec],
|
|
91
|
+
["version", u8Codec],
|
|
92
|
+
["reserved", reservedBytesCodec]
|
|
93
|
+
]);
|
|
94
|
+
var positionDataCodec = kit.getStructCodec([
|
|
95
|
+
["pool", addressCodec],
|
|
96
|
+
["owner", addressCodec],
|
|
97
|
+
["positionId", u64Codec],
|
|
98
|
+
["shares", u128Codec],
|
|
99
|
+
["feeGrowthLast0Q64", u128Codec],
|
|
100
|
+
["feeGrowthLast1Q64", u128Codec],
|
|
101
|
+
["feeOwed0", u64Codec],
|
|
102
|
+
["feeOwed1", u64Codec],
|
|
103
|
+
["version", u8Codec],
|
|
104
|
+
["reserved", reservedBytesCodec]
|
|
105
|
+
]);
|
|
106
|
+
var oracleStateDataCodec = kit.getStructCodec([
|
|
107
|
+
["pool", addressCodec],
|
|
108
|
+
["initialized", boolCodec],
|
|
109
|
+
["maxPriceChangeRatioQ64", u128Codec],
|
|
110
|
+
["lastSlot", u64Codec],
|
|
111
|
+
["truncPrice0Q64", u128Codec],
|
|
112
|
+
["truncPrice1Q64", u128Codec],
|
|
113
|
+
["deviation0Q64", u128Codec],
|
|
114
|
+
["deviation1Q64", u128Codec],
|
|
115
|
+
["price0Cumulative", u256Codec],
|
|
116
|
+
["price1Cumulative", u256Codec],
|
|
117
|
+
["lastTimestamp", u32Codec],
|
|
118
|
+
["lastObservationTimestamp", u32Codec],
|
|
119
|
+
["observationIntervalSec", u32Codec],
|
|
120
|
+
["observationIndex", u16Codec],
|
|
121
|
+
[
|
|
122
|
+
"observations",
|
|
123
|
+
kit.getArrayCodec(observationCodec, { size: chunkDPKVNI6Q_cjs.MAX_ORACLE_OBSERVATIONS })
|
|
124
|
+
],
|
|
125
|
+
["version", u8Codec],
|
|
126
|
+
["reserved", reservedBytesCodec]
|
|
127
|
+
]);
|
|
128
|
+
var ammConfigDecoder = kit.getHiddenPrefixDecoder(ammConfigDataCodec, [
|
|
129
|
+
kit.getConstantDecoder(chunkDPKVNI6Q_cjs.ACCOUNT_DISCRIMINATORS.AmmConfig)
|
|
130
|
+
]);
|
|
131
|
+
var poolDecoder = kit.getHiddenPrefixDecoder(poolDataCodec, [
|
|
132
|
+
kit.getConstantDecoder(chunkDPKVNI6Q_cjs.ACCOUNT_DISCRIMINATORS.Pool)
|
|
133
|
+
]);
|
|
134
|
+
var positionDecoder = kit.getHiddenPrefixDecoder(positionDataCodec, [
|
|
135
|
+
kit.getConstantDecoder(chunkDPKVNI6Q_cjs.ACCOUNT_DISCRIMINATORS.Position)
|
|
136
|
+
]);
|
|
137
|
+
var oracleStateDecoder = kit.getHiddenPrefixDecoder(oracleStateDataCodec, [
|
|
138
|
+
kit.getConstantDecoder(chunkDPKVNI6Q_cjs.ACCOUNT_DISCRIMINATORS.OracleState)
|
|
139
|
+
]);
|
|
140
|
+
function decodeAmmConfig(data) {
|
|
141
|
+
return ammConfigDecoder.decode(data);
|
|
142
|
+
}
|
|
143
|
+
function decodePool(data) {
|
|
144
|
+
return poolDecoder.decode(data);
|
|
145
|
+
}
|
|
146
|
+
function decodePosition(data) {
|
|
147
|
+
return positionDecoder.decode(data);
|
|
148
|
+
}
|
|
149
|
+
function decodeOracleState(data) {
|
|
150
|
+
return oracleStateDecoder.decode(data);
|
|
151
|
+
}
|
|
152
|
+
function encodeInstructionData(discriminator, codec, args) {
|
|
153
|
+
const encodedArgs = (() => {
|
|
154
|
+
if (!codec) {
|
|
155
|
+
return new Uint8Array();
|
|
156
|
+
}
|
|
157
|
+
if (args === void 0) {
|
|
158
|
+
throw new Error("Instruction args are required for codec encoders");
|
|
159
|
+
}
|
|
160
|
+
return new Uint8Array(codec.encode(args));
|
|
161
|
+
})();
|
|
162
|
+
return kit.mergeBytes([discriminator, encodedArgs]);
|
|
163
|
+
}
|
|
164
|
+
var optionAddressCodec = kit.transformCodec(
|
|
165
|
+
kit.getOptionCodec(addressCodec, { prefix: u8Codec }),
|
|
166
|
+
(value) => value,
|
|
167
|
+
(value) => kit.unwrapOption(value)
|
|
168
|
+
);
|
|
169
|
+
var swapExactInArgsCodec = kit.getStructCodec([
|
|
170
|
+
["amountIn", u64Codec],
|
|
171
|
+
["minAmountOut", u64Codec],
|
|
172
|
+
["direction", u8Codec],
|
|
173
|
+
["updateOracle", boolCodec]
|
|
174
|
+
]);
|
|
175
|
+
var addLiquidityArgsWithOracleCodec = kit.getStructCodec([
|
|
176
|
+
["amount0Max", u64Codec],
|
|
177
|
+
["amount1Max", u64Codec],
|
|
178
|
+
["minSharesOut", u128Codec],
|
|
179
|
+
["updateOracle", boolCodec]
|
|
180
|
+
]);
|
|
181
|
+
var addLiquidityArgsCodec = kit.transformCodec(
|
|
182
|
+
addLiquidityArgsWithOracleCodec,
|
|
183
|
+
(value) => ({
|
|
184
|
+
...value,
|
|
185
|
+
updateOracle: value.updateOracle ?? false
|
|
186
|
+
}),
|
|
187
|
+
(value) => value
|
|
188
|
+
);
|
|
189
|
+
var removeLiquidityArgsCodec = kit.getStructCodec([
|
|
190
|
+
["sharesIn", u128Codec],
|
|
191
|
+
["minAmount0Out", u64Codec],
|
|
192
|
+
["minAmount1Out", u64Codec],
|
|
193
|
+
["updateOracle", boolCodec]
|
|
194
|
+
]);
|
|
195
|
+
var collectFeesArgsCodec = kit.getStructCodec([
|
|
196
|
+
["max0", u64Codec],
|
|
197
|
+
["max1", u64Codec]
|
|
198
|
+
]);
|
|
199
|
+
var collectProtocolFeesArgsCodec = kit.getStructCodec([
|
|
200
|
+
["max0", u64Codec],
|
|
201
|
+
["max1", u64Codec]
|
|
202
|
+
]);
|
|
203
|
+
var createPositionArgsCodec = kit.getStructCodec([["positionId", u64Codec]]);
|
|
204
|
+
var initializeConfigArgsCodec = kit.getStructCodec([
|
|
205
|
+
["admin", addressCodec],
|
|
206
|
+
["numeraireMint", addressCodec],
|
|
207
|
+
["maxSwapFeeBps", u16Codec],
|
|
208
|
+
["maxFeeSplitBps", u16Codec],
|
|
209
|
+
["maxRouteHops", u8Codec],
|
|
210
|
+
["protocolFeeEnabled", boolCodec],
|
|
211
|
+
["protocolFeeBps", u16Codec],
|
|
212
|
+
["sentinelAllowlist", kit.getArrayCodec(addressCodec, { size: u32Codec })]
|
|
213
|
+
]);
|
|
214
|
+
var initializePoolArgsCodec = kit.getStructCodec([
|
|
215
|
+
["mintA", addressCodec],
|
|
216
|
+
["mintB", addressCodec],
|
|
217
|
+
["initialSwapFeeBps", u16Codec],
|
|
218
|
+
["initialFeeSplitBps", u16Codec],
|
|
219
|
+
["liquidityMeasureSide", u8Codec],
|
|
220
|
+
["numeraireMintOverride", optionAddressCodec]
|
|
221
|
+
]);
|
|
222
|
+
var initializeOracleArgsCodec = kit.getStructCodec([
|
|
223
|
+
["maxPriceChangeRatioQ64", u128Codec],
|
|
224
|
+
["observationIntervalSec", u32Codec],
|
|
225
|
+
["numObservations", u16Codec]
|
|
226
|
+
]);
|
|
227
|
+
var setSentinelArgsCodec = kit.getStructCodec([
|
|
228
|
+
["sentinelProgram", addressCodec],
|
|
229
|
+
["sentinelFlags", u32Codec]
|
|
230
|
+
]);
|
|
231
|
+
var setFeesArgsCodec = kit.getStructCodec([
|
|
232
|
+
["swapFeeBps", u16Codec],
|
|
233
|
+
["feeSplitBps", u16Codec]
|
|
234
|
+
]);
|
|
235
|
+
var setRouteArgsCodec = kit.getStructCodec([
|
|
236
|
+
["routeNextPool", addressCodec],
|
|
237
|
+
["routeBridgeMint", addressCodec]
|
|
238
|
+
]);
|
|
239
|
+
var transferAdminArgsCodec = kit.getStructCodec([
|
|
240
|
+
["newAdmin", addressCodec]
|
|
241
|
+
]);
|
|
242
|
+
var oracleConsultArgsCodec = kit.getStructCodec([
|
|
243
|
+
["windowSeconds", u32Codec]
|
|
244
|
+
]);
|
|
245
|
+
var quoteToNumeraireArgsCodec = kit.getStructCodec([
|
|
246
|
+
["amount", u128Codec],
|
|
247
|
+
["side", u8Codec],
|
|
248
|
+
["maxHops", u8Codec],
|
|
249
|
+
["useTwap", boolCodec],
|
|
250
|
+
["windowSeconds", u32Codec]
|
|
251
|
+
]);
|
|
252
|
+
function encodeSwapExactInArgs(args) {
|
|
253
|
+
return new Uint8Array(swapExactInArgsCodec.encode(args));
|
|
254
|
+
}
|
|
255
|
+
function encodeAddLiquidityArgs(args) {
|
|
256
|
+
return new Uint8Array(addLiquidityArgsCodec.encode(args));
|
|
257
|
+
}
|
|
258
|
+
function encodeRemoveLiquidityArgs(args) {
|
|
259
|
+
return new Uint8Array(removeLiquidityArgsCodec.encode(args));
|
|
260
|
+
}
|
|
261
|
+
function encodeCollectFeesArgs(args) {
|
|
262
|
+
return new Uint8Array(collectFeesArgsCodec.encode(args));
|
|
263
|
+
}
|
|
264
|
+
function encodeCollectProtocolFeesArgs(args) {
|
|
265
|
+
return new Uint8Array(collectProtocolFeesArgsCodec.encode(args));
|
|
266
|
+
}
|
|
267
|
+
function encodeCreatePositionArgs(args) {
|
|
268
|
+
return new Uint8Array(createPositionArgsCodec.encode(args));
|
|
269
|
+
}
|
|
270
|
+
function encodeInitializeConfigArgs(args) {
|
|
271
|
+
return new Uint8Array(initializeConfigArgsCodec.encode(args));
|
|
272
|
+
}
|
|
273
|
+
function encodeInitializePoolArgs(args) {
|
|
274
|
+
return new Uint8Array(initializePoolArgsCodec.encode(args));
|
|
275
|
+
}
|
|
276
|
+
function encodeInitializeOracleArgs(args) {
|
|
277
|
+
return new Uint8Array(initializeOracleArgsCodec.encode(args));
|
|
278
|
+
}
|
|
279
|
+
function encodeSetSentinelArgs(args) {
|
|
280
|
+
return new Uint8Array(setSentinelArgsCodec.encode(args));
|
|
281
|
+
}
|
|
282
|
+
function encodeSetFeesArgs(args) {
|
|
283
|
+
return new Uint8Array(setFeesArgsCodec.encode(args));
|
|
284
|
+
}
|
|
285
|
+
function encodeSetRouteArgs(args) {
|
|
286
|
+
return new Uint8Array(setRouteArgsCodec.encode(args));
|
|
287
|
+
}
|
|
288
|
+
function encodeTransferAdminArgs(args) {
|
|
289
|
+
return new Uint8Array(transferAdminArgsCodec.encode(args));
|
|
290
|
+
}
|
|
291
|
+
function encodeOracleConsultArgs(args) {
|
|
292
|
+
return new Uint8Array(oracleConsultArgsCodec.encode(args));
|
|
293
|
+
}
|
|
294
|
+
function encodeQuoteToNumeraireArgs(args) {
|
|
295
|
+
return new Uint8Array(quoteToNumeraireArgsCodec.encode(args));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// src/solana/core/math.ts
|
|
299
|
+
function q64ToNumber(q64) {
|
|
300
|
+
const intPart = q64 >> 64n;
|
|
301
|
+
const fracPart = q64 & (1n << 64n) - 1n;
|
|
302
|
+
return Number(intPart) + Number(fracPart) / Number(chunkDPKVNI6Q_cjs.Q64_ONE);
|
|
303
|
+
}
|
|
304
|
+
function numberToQ64(n) {
|
|
305
|
+
const intPart = Math.floor(n);
|
|
306
|
+
const fracPart = n - intPart;
|
|
307
|
+
return (BigInt(intPart) << 64n) + BigInt(Math.round(fracPart * Number(chunkDPKVNI6Q_cjs.Q64_ONE)));
|
|
308
|
+
}
|
|
309
|
+
function q64Mul(a, b) {
|
|
310
|
+
return a * b >> 64n;
|
|
311
|
+
}
|
|
312
|
+
function q64Div(a, b) {
|
|
313
|
+
return (a << 64n) / b;
|
|
314
|
+
}
|
|
315
|
+
function computePrice0Q64(reserve0, reserve1) {
|
|
316
|
+
if (reserve0 === 0n) return 0n;
|
|
317
|
+
return (reserve1 << 64n) / reserve0;
|
|
318
|
+
}
|
|
319
|
+
function computePrice1Q64(reserve0, reserve1) {
|
|
320
|
+
if (reserve1 === 0n) return 0n;
|
|
321
|
+
return (reserve0 << 64n) / reserve1;
|
|
322
|
+
}
|
|
323
|
+
function isqrt(n) {
|
|
324
|
+
if (n < 0n) throw new Error("isqrt: negative input");
|
|
325
|
+
if (n === 0n) return 0n;
|
|
326
|
+
let x = n;
|
|
327
|
+
let y = (x + 1n) / 2n;
|
|
328
|
+
while (y < x) {
|
|
329
|
+
x = y;
|
|
330
|
+
y = (x + n / x) / 2n;
|
|
331
|
+
}
|
|
332
|
+
return x;
|
|
333
|
+
}
|
|
334
|
+
function ceilDiv(a, b) {
|
|
335
|
+
return (a + b - 1n) / b;
|
|
336
|
+
}
|
|
337
|
+
function minBigInt(a, b) {
|
|
338
|
+
return a < b ? a : b;
|
|
339
|
+
}
|
|
340
|
+
function maxBigInt(a, b) {
|
|
341
|
+
return a > b ? a : b;
|
|
342
|
+
}
|
|
343
|
+
function ratioToNumber(numerator, denominator) {
|
|
344
|
+
if (denominator === 0n) return 0;
|
|
345
|
+
const q64 = (numerator << 64n) / denominator;
|
|
346
|
+
return q64ToNumber(q64);
|
|
347
|
+
}
|
|
348
|
+
function getSwapQuote(pool, amountIn, direction) {
|
|
349
|
+
if (amountIn === 0n) {
|
|
350
|
+
return {
|
|
351
|
+
amountOut: 0n,
|
|
352
|
+
feeTotal: 0n,
|
|
353
|
+
feeDist: 0n,
|
|
354
|
+
feeComp: 0n,
|
|
355
|
+
priceImpact: 0,
|
|
356
|
+
executionPrice: 0
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
const [reserveIn, reserveOut] = direction === 0 ? [pool.reserve0, pool.reserve1] : [pool.reserve1, pool.reserve0];
|
|
360
|
+
if (reserveIn === 0n || reserveOut === 0n) {
|
|
361
|
+
throw new Error("Pool has zero liquidity");
|
|
362
|
+
}
|
|
363
|
+
const feeTotal = amountIn * BigInt(pool.swapFeeBps) / chunkDPKVNI6Q_cjs.BPS_DENOM;
|
|
364
|
+
const feeDist = feeTotal * BigInt(pool.feeSplitBps) / chunkDPKVNI6Q_cjs.BPS_DENOM;
|
|
365
|
+
const feeComp = feeTotal - feeDist;
|
|
366
|
+
const amountInEff = amountIn - feeTotal;
|
|
367
|
+
const amountOut = amountInEff * reserveOut / (reserveIn + amountInEff);
|
|
368
|
+
const spotPrice = ratioToNumber(reserveOut, reserveIn);
|
|
369
|
+
const executionPrice = ratioToNumber(amountOut, amountIn);
|
|
370
|
+
const priceImpact = spotPrice === 0 ? 0 : Math.abs(spotPrice - executionPrice) / spotPrice;
|
|
371
|
+
return {
|
|
372
|
+
amountOut,
|
|
373
|
+
feeTotal,
|
|
374
|
+
feeDist,
|
|
375
|
+
feeComp,
|
|
376
|
+
priceImpact,
|
|
377
|
+
executionPrice
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function getSwapQuoteExactOut(pool, amountOut, direction) {
|
|
381
|
+
const [reserveIn, reserveOut] = direction === 0 ? [pool.reserve0, pool.reserve1] : [pool.reserve1, pool.reserve0];
|
|
382
|
+
if (reserveIn === 0n || reserveOut === 0n) {
|
|
383
|
+
throw new Error("Pool has zero liquidity");
|
|
384
|
+
}
|
|
385
|
+
if (amountOut >= reserveOut) {
|
|
386
|
+
throw new Error("Insufficient liquidity for output amount");
|
|
387
|
+
}
|
|
388
|
+
const amountInEff = ceilDiv(amountOut * reserveIn, reserveOut - amountOut);
|
|
389
|
+
const amountIn = ceilDiv(
|
|
390
|
+
amountInEff * chunkDPKVNI6Q_cjs.BPS_DENOM,
|
|
391
|
+
chunkDPKVNI6Q_cjs.BPS_DENOM - BigInt(pool.swapFeeBps)
|
|
392
|
+
);
|
|
393
|
+
const feeTotal = amountIn - amountInEff;
|
|
394
|
+
return { amountIn, feeTotal };
|
|
395
|
+
}
|
|
396
|
+
function getAddLiquidityQuote(pool, amount0Max, amount1Max) {
|
|
397
|
+
if (pool.totalShares === 0n) {
|
|
398
|
+
const rawShares = isqrt(amount0Max * amount1Max);
|
|
399
|
+
if (rawShares === 0n) {
|
|
400
|
+
throw new Error("Initial liquidity too small");
|
|
401
|
+
}
|
|
402
|
+
const sharesOut2 = rawShares;
|
|
403
|
+
return {
|
|
404
|
+
sharesOut: sharesOut2,
|
|
405
|
+
amount0: amount0Max,
|
|
406
|
+
amount1: amount1Max,
|
|
407
|
+
poolShare: ratioToNumber(sharesOut2, rawShares)
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
if (pool.reserve0 === 0n || pool.reserve1 === 0n) {
|
|
411
|
+
throw new Error("Invalid pool state: zero reserves with non-zero shares");
|
|
412
|
+
}
|
|
413
|
+
const shares0 = amount0Max * pool.totalShares / pool.reserve0;
|
|
414
|
+
const shares1 = amount1Max * pool.totalShares / pool.reserve1;
|
|
415
|
+
const sharesOut = minBigInt(shares0, shares1);
|
|
416
|
+
const amount0 = ceilDiv(sharesOut * pool.reserve0, pool.totalShares);
|
|
417
|
+
const amount1 = ceilDiv(sharesOut * pool.reserve1, pool.totalShares);
|
|
418
|
+
const newTotalShares = pool.totalShares + sharesOut;
|
|
419
|
+
const poolShare = ratioToNumber(sharesOut, newTotalShares);
|
|
420
|
+
return {
|
|
421
|
+
sharesOut,
|
|
422
|
+
amount0,
|
|
423
|
+
amount1,
|
|
424
|
+
poolShare
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function getRemoveLiquidityQuote(pool, sharesIn) {
|
|
428
|
+
if (pool.totalShares === 0n) {
|
|
429
|
+
throw new Error("Pool has no shares");
|
|
430
|
+
}
|
|
431
|
+
if (sharesIn > pool.totalShares) {
|
|
432
|
+
throw new Error("Shares exceed total supply");
|
|
433
|
+
}
|
|
434
|
+
const amount0 = sharesIn * pool.reserve0 / pool.totalShares;
|
|
435
|
+
const amount1 = sharesIn * pool.reserve1 / pool.totalShares;
|
|
436
|
+
return { amount0, amount1 };
|
|
437
|
+
}
|
|
438
|
+
function calculateAccruedFees(shares, feeGrowthLastQ64, feeGrowthGlobalQ64) {
|
|
439
|
+
const delta = feeGrowthGlobalQ64 - feeGrowthLastQ64;
|
|
440
|
+
return shares * delta >> 64n;
|
|
441
|
+
}
|
|
442
|
+
function getPendingFees(pool, position) {
|
|
443
|
+
const accrued0 = calculateAccruedFees(
|
|
444
|
+
position.shares,
|
|
445
|
+
position.feeGrowthLast0Q64,
|
|
446
|
+
pool.feeGrowthGlobal0Q64
|
|
447
|
+
);
|
|
448
|
+
const accrued1 = calculateAccruedFees(
|
|
449
|
+
position.shares,
|
|
450
|
+
position.feeGrowthLast1Q64,
|
|
451
|
+
pool.feeGrowthGlobal1Q64
|
|
452
|
+
);
|
|
453
|
+
return {
|
|
454
|
+
pending0: position.feeOwed0 + accrued0,
|
|
455
|
+
pending1: position.feeOwed1 + accrued1
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function getSpotPrice0(pool) {
|
|
459
|
+
if (pool.reserve0 === 0n) return 0;
|
|
460
|
+
return q64ToNumber(computePrice0Q64(pool.reserve0, pool.reserve1));
|
|
461
|
+
}
|
|
462
|
+
function getSpotPrice1(pool) {
|
|
463
|
+
if (pool.reserve1 === 0n) return 0;
|
|
464
|
+
return q64ToNumber(computePrice1Q64(pool.reserve0, pool.reserve1));
|
|
465
|
+
}
|
|
466
|
+
function getK(pool) {
|
|
467
|
+
return pool.reserve0 * pool.reserve1;
|
|
468
|
+
}
|
|
469
|
+
function getTvl(pool, side = 0) {
|
|
470
|
+
if (side === 0) {
|
|
471
|
+
return 2n * pool.reserve0;
|
|
472
|
+
} else {
|
|
473
|
+
return 2n * pool.reserve1;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function calculateTwap(cumulativeStart, cumulativeEnd, timestampStart, timestampEnd) {
|
|
477
|
+
const dt = BigInt(timestampEnd - timestampStart);
|
|
478
|
+
if (dt === 0n) return 0n;
|
|
479
|
+
const cumulativeDiff = cumulativeEnd >= cumulativeStart ? cumulativeEnd - cumulativeStart : (1n << 256n) - cumulativeStart + cumulativeEnd;
|
|
480
|
+
return cumulativeDiff / dt;
|
|
481
|
+
}
|
|
482
|
+
function calculateTwapNumber(cumulativeStart, cumulativeEnd, timestampStart, timestampEnd) {
|
|
483
|
+
const twapQ64 = calculateTwap(
|
|
484
|
+
cumulativeStart,
|
|
485
|
+
cumulativeEnd,
|
|
486
|
+
timestampStart,
|
|
487
|
+
timestampEnd
|
|
488
|
+
);
|
|
489
|
+
return q64ToNumber(twapQ64);
|
|
490
|
+
}
|
|
491
|
+
function createCollectFeesInstruction(accounts, args, programId = chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID) {
|
|
492
|
+
const {
|
|
493
|
+
pool,
|
|
494
|
+
position,
|
|
495
|
+
owner,
|
|
496
|
+
authority,
|
|
497
|
+
vault0,
|
|
498
|
+
vault1,
|
|
499
|
+
token0Mint,
|
|
500
|
+
token1Mint,
|
|
501
|
+
user0,
|
|
502
|
+
user1,
|
|
503
|
+
tokenProgram = chunkDPKVNI6Q_cjs.TOKEN_PROGRAM_ADDRESS
|
|
504
|
+
} = accounts;
|
|
505
|
+
const keys = [
|
|
506
|
+
{ address: pool, role: kit.AccountRole.WRITABLE },
|
|
507
|
+
{ address: position, role: kit.AccountRole.WRITABLE },
|
|
508
|
+
{ address: owner, role: kit.AccountRole.READONLY_SIGNER },
|
|
509
|
+
{ address: authority, role: kit.AccountRole.READONLY },
|
|
510
|
+
{ address: vault0, role: kit.AccountRole.WRITABLE },
|
|
511
|
+
{ address: vault1, role: kit.AccountRole.WRITABLE },
|
|
512
|
+
{ address: token0Mint, role: kit.AccountRole.READONLY },
|
|
513
|
+
{ address: token1Mint, role: kit.AccountRole.READONLY },
|
|
514
|
+
{ address: user0, role: kit.AccountRole.WRITABLE },
|
|
515
|
+
{ address: user1, role: kit.AccountRole.WRITABLE },
|
|
516
|
+
{ address: tokenProgram, role: kit.AccountRole.READONLY }
|
|
517
|
+
];
|
|
518
|
+
const data = encodeInstructionData(
|
|
519
|
+
chunkDPKVNI6Q_cjs.INSTRUCTION_DISCRIMINATORS.collectFees,
|
|
520
|
+
collectFeesArgsCodec,
|
|
521
|
+
args
|
|
522
|
+
);
|
|
523
|
+
return {
|
|
524
|
+
programAddress: programId,
|
|
525
|
+
accounts: keys,
|
|
526
|
+
data
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
var MAX_FEE_AMOUNT = BigInt("18446744073709551615");
|
|
530
|
+
|
|
531
|
+
// src/solana/client/pool.ts
|
|
532
|
+
function bytesToBase64(bytes) {
|
|
533
|
+
let binary = "";
|
|
534
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
535
|
+
binary += String.fromCharCode(bytes[i]);
|
|
536
|
+
}
|
|
537
|
+
return btoa(binary);
|
|
538
|
+
}
|
|
539
|
+
function base64ToBytes(base64) {
|
|
540
|
+
const binary = atob(base64);
|
|
541
|
+
const bytes = new Uint8Array(binary.length);
|
|
542
|
+
for (let i = 0; i < binary.length; i++) {
|
|
543
|
+
bytes[i] = binary.charCodeAt(i);
|
|
544
|
+
}
|
|
545
|
+
return bytes;
|
|
546
|
+
}
|
|
547
|
+
async function fetchPool(rpc, address, config) {
|
|
548
|
+
const response = await rpc.getAccountInfo(address, {
|
|
549
|
+
encoding: "base64",
|
|
550
|
+
commitment: config?.commitment
|
|
551
|
+
}).send();
|
|
552
|
+
if (!response.value) {
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
return decodePool(base64ToBytes(response.value.data[0]));
|
|
556
|
+
}
|
|
557
|
+
async function fetchAllPools(rpc, config) {
|
|
558
|
+
const programId = config?.programId ?? chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID;
|
|
559
|
+
const discriminatorFilter = {
|
|
560
|
+
memcmp: {
|
|
561
|
+
offset: 0n,
|
|
562
|
+
bytes: bytesToBase64(chunkDPKVNI6Q_cjs.ACCOUNT_DISCRIMINATORS.Pool),
|
|
563
|
+
encoding: "base64"
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
const response = await rpc.getProgramAccounts(programId, {
|
|
567
|
+
encoding: "base64",
|
|
568
|
+
commitment: config?.commitment,
|
|
569
|
+
filters: [discriminatorFilter]
|
|
570
|
+
}).send();
|
|
571
|
+
const accounts = Array.isArray(response) ? response : response.value;
|
|
572
|
+
const pools = [];
|
|
573
|
+
for (const account of accounts) {
|
|
574
|
+
try {
|
|
575
|
+
const pool = decodePool(base64ToBytes(account.account.data[0]));
|
|
576
|
+
pools.push({
|
|
577
|
+
address: account.pubkey,
|
|
578
|
+
account: pool
|
|
579
|
+
});
|
|
580
|
+
} catch {
|
|
581
|
+
console.warn(`Failed to decode pool account: ${account.pubkey}`);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return pools;
|
|
585
|
+
}
|
|
586
|
+
async function getPoolByMints(rpc, mint0, mint1, config) {
|
|
587
|
+
const programId = config?.programId ?? chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID;
|
|
588
|
+
const [poolAddress] = await chunkDPKVNI6Q_cjs.getPoolAddress(mint0, mint1, programId);
|
|
589
|
+
const pool = await fetchPool(rpc, poolAddress, config);
|
|
590
|
+
if (!pool) {
|
|
591
|
+
return null;
|
|
592
|
+
}
|
|
593
|
+
return {
|
|
594
|
+
address: poolAddress,
|
|
595
|
+
account: pool
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
async function fetchPoolsBatch(rpc, addresses, config) {
|
|
599
|
+
const pools = /* @__PURE__ */ new Map();
|
|
600
|
+
const results = await Promise.all(
|
|
601
|
+
addresses.map((addr) => fetchPool(rpc, addr, config))
|
|
602
|
+
);
|
|
603
|
+
for (let i = 0; i < addresses.length; i++) {
|
|
604
|
+
const pool = results[i];
|
|
605
|
+
if (pool) {
|
|
606
|
+
pools.set(addresses[i], pool);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return pools;
|
|
610
|
+
}
|
|
611
|
+
async function poolExists(rpc, mint0, mint1, config) {
|
|
612
|
+
const result = await getPoolByMints(rpc, mint0, mint1, config);
|
|
613
|
+
return result !== null;
|
|
614
|
+
}
|
|
615
|
+
async function getPoolAddressFromMints(mint0, mint1, programId = chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID) {
|
|
616
|
+
const [token0, token1] = chunkDPKVNI6Q_cjs.sortMints(mint0, mint1);
|
|
617
|
+
const [poolAddress] = await chunkDPKVNI6Q_cjs.getPoolAddress(mint0, mint1, programId);
|
|
618
|
+
return {
|
|
619
|
+
poolAddress,
|
|
620
|
+
token0,
|
|
621
|
+
token1
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
function filterPoolsByMint(pools, mint) {
|
|
625
|
+
return pools.filter(
|
|
626
|
+
({ account }) => account.token0Mint === mint || account.token1Mint === mint
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
function sortPoolsByReserves(pools, descending = true) {
|
|
630
|
+
return [...pools].sort((a, b) => {
|
|
631
|
+
const totalA = a.account.reserve0 + a.account.reserve1;
|
|
632
|
+
const totalB = b.account.reserve0 + b.account.reserve1;
|
|
633
|
+
const cmp = totalA < totalB ? -1 : totalA > totalB ? 1 : 0;
|
|
634
|
+
return descending ? -cmp : cmp;
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// src/solana/client/position.ts
|
|
639
|
+
function bytesToBase642(bytes) {
|
|
640
|
+
let binary = "";
|
|
641
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
642
|
+
binary += String.fromCharCode(bytes[i]);
|
|
643
|
+
}
|
|
644
|
+
return btoa(binary);
|
|
645
|
+
}
|
|
646
|
+
function base64ToBytes2(base64) {
|
|
647
|
+
const binary = atob(base64);
|
|
648
|
+
const bytes = new Uint8Array(binary.length);
|
|
649
|
+
for (let i = 0; i < binary.length; i++) {
|
|
650
|
+
bytes[i] = binary.charCodeAt(i);
|
|
651
|
+
}
|
|
652
|
+
return bytes;
|
|
653
|
+
}
|
|
654
|
+
async function fetchPosition(rpc, address, config) {
|
|
655
|
+
const response = await rpc.getAccountInfo(address, {
|
|
656
|
+
encoding: "base64",
|
|
657
|
+
commitment: config?.commitment
|
|
658
|
+
}).send();
|
|
659
|
+
if (!response.value) {
|
|
660
|
+
return null;
|
|
661
|
+
}
|
|
662
|
+
return decodePosition(base64ToBytes2(response.value.data[0]));
|
|
663
|
+
}
|
|
664
|
+
async function fetchUserPositions(rpc, owner, pool, config) {
|
|
665
|
+
const programId = config?.programId ?? chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID;
|
|
666
|
+
const filters = [
|
|
667
|
+
// Discriminator filter (first 8 bytes)
|
|
668
|
+
{
|
|
669
|
+
memcmp: {
|
|
670
|
+
offset: 0n,
|
|
671
|
+
bytes: bytesToBase642(
|
|
672
|
+
chunkDPKVNI6Q_cjs.ACCOUNT_DISCRIMINATORS.Position
|
|
673
|
+
),
|
|
674
|
+
encoding: "base64"
|
|
675
|
+
}
|
|
676
|
+
},
|
|
677
|
+
// Owner filter (after 8-byte discriminator + 32-byte pool = offset 40)
|
|
678
|
+
{
|
|
679
|
+
memcmp: {
|
|
680
|
+
offset: 40n,
|
|
681
|
+
bytes: owner,
|
|
682
|
+
encoding: "base58"
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
];
|
|
686
|
+
if (pool) {
|
|
687
|
+
filters.push({
|
|
688
|
+
memcmp: {
|
|
689
|
+
offset: 8n,
|
|
690
|
+
bytes: pool,
|
|
691
|
+
encoding: "base58"
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
const response = await rpc.getProgramAccounts(programId, {
|
|
696
|
+
encoding: "base64",
|
|
697
|
+
commitment: config?.commitment,
|
|
698
|
+
filters
|
|
699
|
+
}).send();
|
|
700
|
+
const accounts = Array.isArray(response) ? response : response.value;
|
|
701
|
+
const positions = [];
|
|
702
|
+
for (const account of accounts) {
|
|
703
|
+
try {
|
|
704
|
+
const position = decodePosition(base64ToBytes2(account.account.data[0]));
|
|
705
|
+
positions.push({
|
|
706
|
+
address: account.pubkey,
|
|
707
|
+
account: position
|
|
708
|
+
});
|
|
709
|
+
} catch {
|
|
710
|
+
console.warn(`Failed to decode position account: ${account.pubkey}`);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return positions;
|
|
714
|
+
}
|
|
715
|
+
async function fetchPoolPositions(rpc, pool, config) {
|
|
716
|
+
const programId = config?.programId ?? chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID;
|
|
717
|
+
const filters = [
|
|
718
|
+
// Discriminator filter
|
|
719
|
+
{
|
|
720
|
+
memcmp: {
|
|
721
|
+
offset: 0n,
|
|
722
|
+
bytes: bytesToBase642(
|
|
723
|
+
chunkDPKVNI6Q_cjs.ACCOUNT_DISCRIMINATORS.Position
|
|
724
|
+
),
|
|
725
|
+
encoding: "base64"
|
|
726
|
+
}
|
|
727
|
+
},
|
|
728
|
+
// Pool filter (after 8-byte discriminator = offset 8)
|
|
729
|
+
{
|
|
730
|
+
memcmp: {
|
|
731
|
+
offset: 8n,
|
|
732
|
+
bytes: pool,
|
|
733
|
+
encoding: "base58"
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
];
|
|
737
|
+
const response = await rpc.getProgramAccounts(programId, {
|
|
738
|
+
encoding: "base64",
|
|
739
|
+
commitment: config?.commitment,
|
|
740
|
+
filters
|
|
741
|
+
}).send();
|
|
742
|
+
const accounts = Array.isArray(response) ? response : response.value;
|
|
743
|
+
const positions = [];
|
|
744
|
+
for (const account of accounts) {
|
|
745
|
+
try {
|
|
746
|
+
const position = decodePosition(base64ToBytes2(account.account.data[0]));
|
|
747
|
+
positions.push({
|
|
748
|
+
address: account.pubkey,
|
|
749
|
+
account: position
|
|
750
|
+
});
|
|
751
|
+
} catch {
|
|
752
|
+
console.warn(`Failed to decode position account: ${account.pubkey}`);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
return positions;
|
|
756
|
+
}
|
|
757
|
+
function getPositionValue(pool, position) {
|
|
758
|
+
if (pool.totalShares === 0n) {
|
|
759
|
+
return {
|
|
760
|
+
amount0: 0n,
|
|
761
|
+
amount1: 0n,
|
|
762
|
+
pendingFees0: 0n,
|
|
763
|
+
pendingFees1: 0n,
|
|
764
|
+
totalValue0: 0n,
|
|
765
|
+
totalValue1: 0n,
|
|
766
|
+
poolShare: 0
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
const amount0 = position.shares * pool.reserve0 / pool.totalShares;
|
|
770
|
+
const amount1 = position.shares * pool.reserve1 / pool.totalShares;
|
|
771
|
+
const { pending0, pending1 } = getPendingFees(pool, position);
|
|
772
|
+
const poolShare = ratioToNumber(position.shares, pool.totalShares);
|
|
773
|
+
return {
|
|
774
|
+
amount0,
|
|
775
|
+
amount1,
|
|
776
|
+
pendingFees0: pending0,
|
|
777
|
+
pendingFees1: pending1,
|
|
778
|
+
totalValue0: amount0 + pending0,
|
|
779
|
+
totalValue1: amount1 + pending1,
|
|
780
|
+
poolShare
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
async function fetchPositionByParams(rpc, pool, owner, positionId, config) {
|
|
784
|
+
const programId = config?.programId ?? chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID;
|
|
785
|
+
const [address] = await chunkDPKVNI6Q_cjs.getPositionAddress(
|
|
786
|
+
pool,
|
|
787
|
+
owner,
|
|
788
|
+
positionId,
|
|
789
|
+
programId
|
|
790
|
+
);
|
|
791
|
+
const position = await fetchPosition(rpc, address, config);
|
|
792
|
+
if (!position) {
|
|
793
|
+
return null;
|
|
794
|
+
}
|
|
795
|
+
return {
|
|
796
|
+
address,
|
|
797
|
+
account: position
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
async function getPositionAddressFromParams(pool, owner, positionId, programId = chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID) {
|
|
801
|
+
const [address] = await chunkDPKVNI6Q_cjs.getPositionAddress(
|
|
802
|
+
pool,
|
|
803
|
+
owner,
|
|
804
|
+
positionId,
|
|
805
|
+
programId
|
|
806
|
+
);
|
|
807
|
+
return address;
|
|
808
|
+
}
|
|
809
|
+
async function fetchPositionsBatch(rpc, addresses, config) {
|
|
810
|
+
const positions = /* @__PURE__ */ new Map();
|
|
811
|
+
const results = await Promise.all(
|
|
812
|
+
addresses.map((addr) => fetchPosition(rpc, addr, config))
|
|
813
|
+
);
|
|
814
|
+
for (let i = 0; i < addresses.length; i++) {
|
|
815
|
+
const position = results[i];
|
|
816
|
+
if (position) {
|
|
817
|
+
positions.set(addresses[i], position);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
return positions;
|
|
821
|
+
}
|
|
822
|
+
function filterActivePositions(positions) {
|
|
823
|
+
return positions.filter(({ account }) => account.shares > 0n);
|
|
824
|
+
}
|
|
825
|
+
function sortPositionsByShares(positions, descending = true) {
|
|
826
|
+
return [...positions].sort((a, b) => {
|
|
827
|
+
const cmp = a.account.shares < b.account.shares ? -1 : a.account.shares > b.account.shares ? 1 : 0;
|
|
828
|
+
return descending ? -cmp : cmp;
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
// src/solana/client/oracle.ts
|
|
833
|
+
function base64ToBytes3(base64) {
|
|
834
|
+
const binary = atob(base64);
|
|
835
|
+
const bytes = new Uint8Array(binary.length);
|
|
836
|
+
for (let i = 0; i < binary.length; i++) {
|
|
837
|
+
bytes[i] = binary.charCodeAt(i);
|
|
838
|
+
}
|
|
839
|
+
return bytes;
|
|
840
|
+
}
|
|
841
|
+
async function fetchOracle(rpc, address, config) {
|
|
842
|
+
const response = await rpc.getAccountInfo(address, {
|
|
843
|
+
encoding: "base64",
|
|
844
|
+
commitment: config?.commitment
|
|
845
|
+
}).send();
|
|
846
|
+
if (!response.value) {
|
|
847
|
+
return null;
|
|
848
|
+
}
|
|
849
|
+
return decodeOracleState(base64ToBytes3(response.value.data[0]));
|
|
850
|
+
}
|
|
851
|
+
async function getOracleForPool(rpc, pool, config) {
|
|
852
|
+
const programId = config?.programId ?? chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID;
|
|
853
|
+
const [oracleAddress] = await chunkDPKVNI6Q_cjs.getOracleAddress(pool, programId);
|
|
854
|
+
const oracle = await fetchOracle(rpc, oracleAddress, config);
|
|
855
|
+
if (!oracle) {
|
|
856
|
+
return null;
|
|
857
|
+
}
|
|
858
|
+
return {
|
|
859
|
+
address: oracleAddress,
|
|
860
|
+
account: oracle
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
async function getOracleAddressFromPool(pool, programId = chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID) {
|
|
864
|
+
const [address] = await chunkDPKVNI6Q_cjs.getOracleAddress(pool, programId);
|
|
865
|
+
return address;
|
|
866
|
+
}
|
|
867
|
+
function consultTwap(oracle, windowSeconds, currentTimestamp) {
|
|
868
|
+
if (!oracle.initialized || oracle.lastTimestamp === 0) {
|
|
869
|
+
return null;
|
|
870
|
+
}
|
|
871
|
+
const nowTs = Math.max(0, Math.floor(currentTimestamp ?? Date.now() / 1e3));
|
|
872
|
+
const dtSinceLast = Math.max(0, nowTs - oracle.lastTimestamp);
|
|
873
|
+
const cum0Now = oracle.price0Cumulative + oracle.truncPrice0Q64 * BigInt(dtSinceLast);
|
|
874
|
+
const cum1Now = oracle.price1Cumulative + oracle.truncPrice1Q64 * BigInt(dtSinceLast);
|
|
875
|
+
if (windowSeconds === 0) {
|
|
876
|
+
return {
|
|
877
|
+
price0Q64: oracle.truncPrice0Q64,
|
|
878
|
+
price1Q64: oracle.truncPrice1Q64,
|
|
879
|
+
price0: q64ToNumber(oracle.truncPrice0Q64),
|
|
880
|
+
price1: q64ToNumber(oracle.truncPrice1Q64)
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
const targetTs = Math.max(0, nowTs - windowSeconds);
|
|
884
|
+
const sample = selectSample(oracle, targetTs);
|
|
885
|
+
if (!sample) {
|
|
886
|
+
return null;
|
|
887
|
+
}
|
|
888
|
+
const dt = Math.max(0, nowTs - sample.timestamp);
|
|
889
|
+
if (dt === 0) {
|
|
890
|
+
return null;
|
|
891
|
+
}
|
|
892
|
+
const price0Q64 = (cum0Now - sample.price0Cumulative) / BigInt(dt);
|
|
893
|
+
const price1Q64 = (cum1Now - sample.price1Cumulative) / BigInt(dt);
|
|
894
|
+
return {
|
|
895
|
+
price0Q64,
|
|
896
|
+
price1Q64,
|
|
897
|
+
price0: q64ToNumber(price0Q64),
|
|
898
|
+
price1: q64ToNumber(price1Q64)
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
function selectSample(oracle, targetTimestamp) {
|
|
902
|
+
let best = null;
|
|
903
|
+
const base = {
|
|
904
|
+
timestamp: oracle.lastTimestamp,
|
|
905
|
+
price0Cumulative: oracle.price0Cumulative,
|
|
906
|
+
price1Cumulative: oracle.price1Cumulative
|
|
907
|
+
};
|
|
908
|
+
if (base.timestamp !== 0 && base.timestamp <= targetTimestamp) {
|
|
909
|
+
best = base;
|
|
910
|
+
}
|
|
911
|
+
for (const obs of oracle.observations) {
|
|
912
|
+
if (obs.timestamp === 0) {
|
|
913
|
+
continue;
|
|
914
|
+
}
|
|
915
|
+
if (obs.timestamp <= targetTimestamp) {
|
|
916
|
+
if (!best || obs.timestamp > best.timestamp) {
|
|
917
|
+
best = obs;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
if (best) {
|
|
922
|
+
return best;
|
|
923
|
+
}
|
|
924
|
+
let oldest = base.timestamp !== 0 ? base : null;
|
|
925
|
+
for (const obs of oracle.observations) {
|
|
926
|
+
if (obs.timestamp === 0) {
|
|
927
|
+
continue;
|
|
928
|
+
}
|
|
929
|
+
if (!oldest || obs.timestamp < oldest.timestamp) {
|
|
930
|
+
oldest = obs;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
return oldest;
|
|
934
|
+
}
|
|
935
|
+
function getOracleSpotPrices(oracle) {
|
|
936
|
+
return {
|
|
937
|
+
price0Q64: oracle.truncPrice0Q64,
|
|
938
|
+
price1Q64: oracle.truncPrice1Q64,
|
|
939
|
+
price0: q64ToNumber(oracle.truncPrice0Q64),
|
|
940
|
+
price1: q64ToNumber(oracle.truncPrice1Q64)
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
function getOracleDeviation(oracle) {
|
|
944
|
+
return {
|
|
945
|
+
deviation0Q64: oracle.deviation0Q64,
|
|
946
|
+
deviation1Q64: oracle.deviation1Q64,
|
|
947
|
+
deviation0: q64ToNumber(oracle.deviation0Q64),
|
|
948
|
+
deviation1: q64ToNumber(oracle.deviation1Q64)
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
function getOracleAge(oracle, currentTimestamp) {
|
|
952
|
+
const now = currentTimestamp ?? Math.floor(Date.now() / 1e3);
|
|
953
|
+
return now - oracle.lastTimestamp;
|
|
954
|
+
}
|
|
955
|
+
function isOracleStale(oracle, maxAgeSeconds, currentTimestamp) {
|
|
956
|
+
return getOracleAge(oracle, currentTimestamp) > maxAgeSeconds;
|
|
957
|
+
}
|
|
958
|
+
function getOracleBufferStats(oracle) {
|
|
959
|
+
const observations = oracle.observations;
|
|
960
|
+
const capacity = observations.length;
|
|
961
|
+
const currentIndex = oracle.observationIndex;
|
|
962
|
+
let filledCount = 0;
|
|
963
|
+
let oldestTimestamp = 0;
|
|
964
|
+
let newestTimestamp = 0;
|
|
965
|
+
for (let i = 0; i < capacity; i++) {
|
|
966
|
+
const obs = observations[i];
|
|
967
|
+
if (obs.timestamp > 0) {
|
|
968
|
+
filledCount++;
|
|
969
|
+
if (oldestTimestamp === 0 || obs.timestamp < oldestTimestamp) {
|
|
970
|
+
oldestTimestamp = obs.timestamp;
|
|
971
|
+
}
|
|
972
|
+
if (obs.timestamp > newestTimestamp) {
|
|
973
|
+
newestTimestamp = obs.timestamp;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
return {
|
|
978
|
+
capacity,
|
|
979
|
+
filledCount,
|
|
980
|
+
currentIndex,
|
|
981
|
+
oldestTimestamp,
|
|
982
|
+
newestTimestamp,
|
|
983
|
+
timeSpanSeconds: newestTimestamp > oldestTimestamp ? newestTimestamp - oldestTimestamp : 0
|
|
984
|
+
};
|
|
985
|
+
}
|
|
986
|
+
async function fetchOraclesBatch(rpc, pools, config) {
|
|
987
|
+
const programId = config?.programId ?? chunkDPKVNI6Q_cjs.CPMM_PROGRAM_ID;
|
|
988
|
+
const oracles = /* @__PURE__ */ new Map();
|
|
989
|
+
const oracleAddresses = await Promise.all(
|
|
990
|
+
pools.map((pool) => chunkDPKVNI6Q_cjs.getOracleAddress(pool, programId))
|
|
991
|
+
);
|
|
992
|
+
const results = await Promise.all(
|
|
993
|
+
oracleAddresses.map(([addr]) => fetchOracle(rpc, addr, config))
|
|
994
|
+
);
|
|
995
|
+
for (let i = 0; i < pools.length; i++) {
|
|
996
|
+
const oracle = results[i];
|
|
997
|
+
if (oracle) {
|
|
998
|
+
oracles.set(pools[i], {
|
|
999
|
+
address: oracleAddresses[i][0],
|
|
1000
|
+
account: oracle
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
return oracles;
|
|
1005
|
+
}
|
|
1006
|
+
function comparePoolAndOraclePrices(pool, oracle) {
|
|
1007
|
+
const poolPrice0 = pool.reserve0 > 0n ? Number(pool.reserve1) / Number(pool.reserve0) : 0;
|
|
1008
|
+
const oraclePrice0 = q64ToNumber(oracle.truncPrice0Q64);
|
|
1009
|
+
const divergencePct = oraclePrice0 > 0 ? (poolPrice0 - oraclePrice0) / oraclePrice0 * 100 : 0;
|
|
1010
|
+
return {
|
|
1011
|
+
poolPrice0,
|
|
1012
|
+
oraclePrice0,
|
|
1013
|
+
divergencePct
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
exports.MAX_FEE_AMOUNT = MAX_FEE_AMOUNT;
|
|
1018
|
+
exports.addLiquidityArgsCodec = addLiquidityArgsCodec;
|
|
1019
|
+
exports.ammConfigDataCodec = ammConfigDataCodec;
|
|
1020
|
+
exports.calculateAccruedFees = calculateAccruedFees;
|
|
1021
|
+
exports.calculateTwap = calculateTwap;
|
|
1022
|
+
exports.calculateTwapNumber = calculateTwapNumber;
|
|
1023
|
+
exports.ceilDiv = ceilDiv;
|
|
1024
|
+
exports.collectFeesArgsCodec = collectFeesArgsCodec;
|
|
1025
|
+
exports.collectProtocolFeesArgsCodec = collectProtocolFeesArgsCodec;
|
|
1026
|
+
exports.comparePoolAndOraclePrices = comparePoolAndOraclePrices;
|
|
1027
|
+
exports.computePrice0Q64 = computePrice0Q64;
|
|
1028
|
+
exports.computePrice1Q64 = computePrice1Q64;
|
|
1029
|
+
exports.consultTwap = consultTwap;
|
|
1030
|
+
exports.createCollectFeesInstruction = createCollectFeesInstruction;
|
|
1031
|
+
exports.createPositionArgsCodec = createPositionArgsCodec;
|
|
1032
|
+
exports.decodeAmmConfig = decodeAmmConfig;
|
|
1033
|
+
exports.decodeOracleState = decodeOracleState;
|
|
1034
|
+
exports.decodePool = decodePool;
|
|
1035
|
+
exports.decodePosition = decodePosition;
|
|
1036
|
+
exports.encodeAddLiquidityArgs = encodeAddLiquidityArgs;
|
|
1037
|
+
exports.encodeCollectFeesArgs = encodeCollectFeesArgs;
|
|
1038
|
+
exports.encodeCollectProtocolFeesArgs = encodeCollectProtocolFeesArgs;
|
|
1039
|
+
exports.encodeCreatePositionArgs = encodeCreatePositionArgs;
|
|
1040
|
+
exports.encodeInitializeConfigArgs = encodeInitializeConfigArgs;
|
|
1041
|
+
exports.encodeInitializeOracleArgs = encodeInitializeOracleArgs;
|
|
1042
|
+
exports.encodeInitializePoolArgs = encodeInitializePoolArgs;
|
|
1043
|
+
exports.encodeInstructionData = encodeInstructionData;
|
|
1044
|
+
exports.encodeOracleConsultArgs = encodeOracleConsultArgs;
|
|
1045
|
+
exports.encodeQuoteToNumeraireArgs = encodeQuoteToNumeraireArgs;
|
|
1046
|
+
exports.encodeRemoveLiquidityArgs = encodeRemoveLiquidityArgs;
|
|
1047
|
+
exports.encodeSetFeesArgs = encodeSetFeesArgs;
|
|
1048
|
+
exports.encodeSetRouteArgs = encodeSetRouteArgs;
|
|
1049
|
+
exports.encodeSetSentinelArgs = encodeSetSentinelArgs;
|
|
1050
|
+
exports.encodeSwapExactInArgs = encodeSwapExactInArgs;
|
|
1051
|
+
exports.encodeTransferAdminArgs = encodeTransferAdminArgs;
|
|
1052
|
+
exports.fetchAllPools = fetchAllPools;
|
|
1053
|
+
exports.fetchOracle = fetchOracle;
|
|
1054
|
+
exports.fetchOraclesBatch = fetchOraclesBatch;
|
|
1055
|
+
exports.fetchPool = fetchPool;
|
|
1056
|
+
exports.fetchPoolPositions = fetchPoolPositions;
|
|
1057
|
+
exports.fetchPoolsBatch = fetchPoolsBatch;
|
|
1058
|
+
exports.fetchPosition = fetchPosition;
|
|
1059
|
+
exports.fetchPositionByParams = fetchPositionByParams;
|
|
1060
|
+
exports.fetchPositionsBatch = fetchPositionsBatch;
|
|
1061
|
+
exports.fetchUserPositions = fetchUserPositions;
|
|
1062
|
+
exports.filterActivePositions = filterActivePositions;
|
|
1063
|
+
exports.filterPoolsByMint = filterPoolsByMint;
|
|
1064
|
+
exports.getAddLiquidityQuote = getAddLiquidityQuote;
|
|
1065
|
+
exports.getK = getK;
|
|
1066
|
+
exports.getOracleAddressFromPool = getOracleAddressFromPool;
|
|
1067
|
+
exports.getOracleAge = getOracleAge;
|
|
1068
|
+
exports.getOracleBufferStats = getOracleBufferStats;
|
|
1069
|
+
exports.getOracleDeviation = getOracleDeviation;
|
|
1070
|
+
exports.getOracleForPool = getOracleForPool;
|
|
1071
|
+
exports.getOracleSpotPrices = getOracleSpotPrices;
|
|
1072
|
+
exports.getPendingFees = getPendingFees;
|
|
1073
|
+
exports.getPoolAddressFromMints = getPoolAddressFromMints;
|
|
1074
|
+
exports.getPoolByMints = getPoolByMints;
|
|
1075
|
+
exports.getPositionAddressFromParams = getPositionAddressFromParams;
|
|
1076
|
+
exports.getPositionValue = getPositionValue;
|
|
1077
|
+
exports.getRemoveLiquidityQuote = getRemoveLiquidityQuote;
|
|
1078
|
+
exports.getSpotPrice0 = getSpotPrice0;
|
|
1079
|
+
exports.getSpotPrice1 = getSpotPrice1;
|
|
1080
|
+
exports.getSwapQuote = getSwapQuote;
|
|
1081
|
+
exports.getSwapQuoteExactOut = getSwapQuoteExactOut;
|
|
1082
|
+
exports.getTvl = getTvl;
|
|
1083
|
+
exports.initializeConfigArgsCodec = initializeConfigArgsCodec;
|
|
1084
|
+
exports.initializeOracleArgsCodec = initializeOracleArgsCodec;
|
|
1085
|
+
exports.initializePoolArgsCodec = initializePoolArgsCodec;
|
|
1086
|
+
exports.isOracleStale = isOracleStale;
|
|
1087
|
+
exports.isqrt = isqrt;
|
|
1088
|
+
exports.maxBigInt = maxBigInt;
|
|
1089
|
+
exports.minBigInt = minBigInt;
|
|
1090
|
+
exports.numberToQ64 = numberToQ64;
|
|
1091
|
+
exports.observationCodec = observationCodec;
|
|
1092
|
+
exports.oracleConsultArgsCodec = oracleConsultArgsCodec;
|
|
1093
|
+
exports.oracleStateDataCodec = oracleStateDataCodec;
|
|
1094
|
+
exports.poolDataCodec = poolDataCodec;
|
|
1095
|
+
exports.poolExists = poolExists;
|
|
1096
|
+
exports.positionDataCodec = positionDataCodec;
|
|
1097
|
+
exports.q64Div = q64Div;
|
|
1098
|
+
exports.q64Mul = q64Mul;
|
|
1099
|
+
exports.q64ToNumber = q64ToNumber;
|
|
1100
|
+
exports.quoteToNumeraireArgsCodec = quoteToNumeraireArgsCodec;
|
|
1101
|
+
exports.ratioToNumber = ratioToNumber;
|
|
1102
|
+
exports.removeLiquidityArgsCodec = removeLiquidityArgsCodec;
|
|
1103
|
+
exports.setFeesArgsCodec = setFeesArgsCodec;
|
|
1104
|
+
exports.setRouteArgsCodec = setRouteArgsCodec;
|
|
1105
|
+
exports.setSentinelArgsCodec = setSentinelArgsCodec;
|
|
1106
|
+
exports.sortPoolsByReserves = sortPoolsByReserves;
|
|
1107
|
+
exports.sortPositionsByShares = sortPositionsByShares;
|
|
1108
|
+
exports.swapExactInArgsCodec = swapExactInArgsCodec;
|
|
1109
|
+
exports.transferAdminArgsCodec = transferAdminArgsCodec;
|
|
1110
|
+
//# sourceMappingURL=chunk-BXATWUGJ.cjs.map
|
|
1111
|
+
//# sourceMappingURL=chunk-BXATWUGJ.cjs.map
|