@pina-rs/amm 0.0.0 → 0.2.0

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.
Files changed (48) hide show
  1. package/README.md +27 -2
  2. package/dist/index.cjs +1744 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +2192 -0
  5. package/dist/index.d.ts +2192 -0
  6. package/dist/index.js +1507 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +55 -5
  9. package/src/generated/accounts/ammConfig.ts +197 -0
  10. package/src/generated/accounts/index.ts +10 -0
  11. package/src/generated/accounts/pool.ts +274 -0
  12. package/src/generated/errors/index.ts +9 -0
  13. package/src/generated/errors/pinaAmm.ts +66 -0
  14. package/src/generated/events/configCreated.ts +80 -0
  15. package/src/generated/events/configUpdated.ts +64 -0
  16. package/src/generated/events/feesCollected.ts +64 -0
  17. package/src/generated/events/index.ts +18 -0
  18. package/src/generated/events/liquidityChanged.ts +72 -0
  19. package/src/generated/events/logs.ts +732 -0
  20. package/src/generated/events/poolCreated.ts +80 -0
  21. package/src/generated/events/poolCreatorChanged.ts +56 -0
  22. package/src/generated/events/poolSynced.ts +78 -0
  23. package/src/generated/events/poolSyncedV0.ts +44 -0
  24. package/src/generated/events/swapped.ts +100 -0
  25. package/src/generated/index.ts +14 -0
  26. package/src/generated/instructions/collectCreatorFees.ts +119 -0
  27. package/src/generated/instructions/collectProtocolFees.ts +123 -0
  28. package/src/generated/instructions/createConfig.ts +142 -0
  29. package/src/generated/instructions/createPool.ts +291 -0
  30. package/src/generated/instructions/deposit.ts +139 -0
  31. package/src/generated/instructions/index.ts +20 -0
  32. package/src/generated/instructions/migrate.ts +155 -0
  33. package/src/generated/instructions/setPoolCreator.ts +90 -0
  34. package/src/generated/instructions/swapExactIn.ts +119 -0
  35. package/src/generated/instructions/swapExactOut.ts +119 -0
  36. package/src/generated/instructions/syncPool.ts +93 -0
  37. package/src/generated/instructions/updateConfig.ts +105 -0
  38. package/src/generated/instructions/withdraw.ts +139 -0
  39. package/src/generated/pdas/ammConfig.ts +18 -0
  40. package/src/generated/pdas/index.ts +12 -0
  41. package/src/generated/pdas/pool.ts +20 -0
  42. package/src/generated/pdas/poolLpMint.ts +18 -0
  43. package/src/generated/pdas/poolVault.ts +19 -0
  44. package/src/generated/pinaPodCodecs.ts +232 -0
  45. package/src/generated/programs/index.ts +9 -0
  46. package/src/generated/programs/pinaAmm.ts +124 -0
  47. package/src/index.ts +11 -0
  48. package/CHANGELOG.md +0 -5
@@ -0,0 +1,732 @@
1
+ /**
2
+ * This code was AUTOGENERATED using the Codama library and Pina's migration
3
+ * manifest. Please DO NOT EDIT THIS FILE, instead rerun client generation.
4
+ *
5
+ * Event log read path: decode `Program data:` log lines into typed events.
6
+ * A migration-aware event is decoded with the layout of the version that
7
+ * emitted it: each earlier version is its own `<Event>V<n>` event, and a
8
+ * version no event here describes fails instead of being misread.
9
+ */
10
+
11
+ import { getBase64Encoder, type ReadonlyUint8Array } from "@solana/kit";
12
+ import {
13
+ getPoolCreatedEventDecoder,
14
+ getPoolCreatedEventDiscriminatorBytes,
15
+ type PoolCreatedEvent,
16
+ } from "./poolCreated.js";
17
+ import {
18
+ getSwappedEventDecoder,
19
+ getSwappedEventDiscriminatorBytes,
20
+ type SwappedEvent,
21
+ } from "./swapped.js";
22
+ import {
23
+ getLiquidityChangedEventDecoder,
24
+ getLiquidityChangedEventDiscriminatorBytes,
25
+ type LiquidityChangedEvent,
26
+ } from "./liquidityChanged.js";
27
+ import {
28
+ getFeesCollectedEventDecoder,
29
+ getFeesCollectedEventDiscriminatorBytes,
30
+ type FeesCollectedEvent,
31
+ } from "./feesCollected.js";
32
+ import {
33
+ getPoolSyncedEventDecoder,
34
+ getPoolSyncedEventDiscriminatorBytes,
35
+ type PoolSyncedEvent,
36
+ } from "./poolSynced.js";
37
+ import {
38
+ getPoolSyncedV0EventDecoder,
39
+ getPoolSyncedV0EventDiscriminatorBytes,
40
+ type PoolSyncedV0Event,
41
+ } from "./poolSyncedV0.js";
42
+ import {
43
+ getConfigCreatedEventDecoder,
44
+ getConfigCreatedEventDiscriminatorBytes,
45
+ type ConfigCreatedEvent,
46
+ } from "./configCreated.js";
47
+ import {
48
+ getConfigUpdatedEventDecoder,
49
+ getConfigUpdatedEventDiscriminatorBytes,
50
+ type ConfigUpdatedEvent,
51
+ } from "./configUpdated.js";
52
+ import {
53
+ getPoolCreatorChangedEventDecoder,
54
+ getPoolCreatorChangedEventDiscriminatorBytes,
55
+ type PoolCreatorChangedEvent,
56
+ } from "./poolCreatorChanged.js";
57
+
58
+ /**
59
+ * Decode one `poolCreated` record: only records carrying this event's migration version decode.
60
+ */
61
+ export function decodePoolCreatedEvent(
62
+ data: ReadonlyUint8Array | Uint8Array,
63
+ ): DecodedPoolCreatedEvent {
64
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
65
+ const discriminatorBytes = getPoolCreatedEventDiscriminatorBytes();
66
+ if (bytes.length < 2) {
67
+ throw new RangeError(
68
+ `the provided data is too short for the "PoolCreatedEvent" event envelope`,
69
+ );
70
+ }
71
+ for (let index = 0; index < 1; index += 1) {
72
+ if (bytes[index] !== discriminatorBytes[index]) {
73
+ throw new RangeError(
74
+ 'the provided data does not match the "PoolCreatedEvent" event discriminator.',
75
+ );
76
+ }
77
+ }
78
+ const sourceVersion = bytes[1];
79
+ if (sourceVersion !== 0) {
80
+ throw new RangeError(
81
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
82
+ );
83
+ }
84
+ return { name: "poolCreated", data: getPoolCreatedEventDecoder().decode(bytes) };
85
+ }
86
+
87
+ /** One log entry that named this event. */
88
+ export type DecodedPoolCreatedEvent = { name: "poolCreated"; data: PoolCreatedEvent };
89
+
90
+ /**
91
+ * Decode a `Program data:` log line, or return `null` when the line is not
92
+ * this event.
93
+ */
94
+ export function parsePoolCreatedEventFromLog(log: string): DecodedPoolCreatedEvent | null {
95
+ const prefix = "Program data: ";
96
+ if (!log.startsWith(prefix)) {
97
+ return null;
98
+ }
99
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
100
+ if (bytes.length < 2) {
101
+ return null;
102
+ }
103
+ const discriminatorBytes = getPoolCreatedEventDiscriminatorBytes();
104
+ for (let index = 0; index < 1; index += 1) {
105
+ if (bytes[index] !== discriminatorBytes[index]) {
106
+ return null;
107
+ }
108
+ }
109
+ if (bytes[1] !== 0) {
110
+ return null;
111
+ }
112
+ return decodePoolCreatedEvent(bytes);
113
+ }
114
+
115
+ /**
116
+ * Decode one `swapped` record: only records carrying this event's migration version decode.
117
+ */
118
+ export function decodeSwappedEvent(
119
+ data: ReadonlyUint8Array | Uint8Array,
120
+ ): DecodedSwappedEvent {
121
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
122
+ const discriminatorBytes = getSwappedEventDiscriminatorBytes();
123
+ if (bytes.length < 2) {
124
+ throw new RangeError(
125
+ `the provided data is too short for the "SwappedEvent" event envelope`,
126
+ );
127
+ }
128
+ for (let index = 0; index < 1; index += 1) {
129
+ if (bytes[index] !== discriminatorBytes[index]) {
130
+ throw new RangeError(
131
+ 'the provided data does not match the "SwappedEvent" event discriminator.',
132
+ );
133
+ }
134
+ }
135
+ const sourceVersion = bytes[1];
136
+ if (sourceVersion !== 0) {
137
+ throw new RangeError(
138
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
139
+ );
140
+ }
141
+ return { name: "swapped", data: getSwappedEventDecoder().decode(bytes) };
142
+ }
143
+
144
+ /** One log entry that named this event. */
145
+ export type DecodedSwappedEvent = { name: "swapped"; data: SwappedEvent };
146
+
147
+ /**
148
+ * Decode a `Program data:` log line, or return `null` when the line is not
149
+ * this event.
150
+ */
151
+ export function parseSwappedEventFromLog(log: string): DecodedSwappedEvent | null {
152
+ const prefix = "Program data: ";
153
+ if (!log.startsWith(prefix)) {
154
+ return null;
155
+ }
156
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
157
+ if (bytes.length < 2) {
158
+ return null;
159
+ }
160
+ const discriminatorBytes = getSwappedEventDiscriminatorBytes();
161
+ for (let index = 0; index < 1; index += 1) {
162
+ if (bytes[index] !== discriminatorBytes[index]) {
163
+ return null;
164
+ }
165
+ }
166
+ if (bytes[1] !== 0) {
167
+ return null;
168
+ }
169
+ return decodeSwappedEvent(bytes);
170
+ }
171
+
172
+ /**
173
+ * Decode one `liquidityChanged` record: only records carrying this event's migration version decode.
174
+ */
175
+ export function decodeLiquidityChangedEvent(
176
+ data: ReadonlyUint8Array | Uint8Array,
177
+ ): DecodedLiquidityChangedEvent {
178
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
179
+ const discriminatorBytes = getLiquidityChangedEventDiscriminatorBytes();
180
+ if (bytes.length < 2) {
181
+ throw new RangeError(
182
+ `the provided data is too short for the "LiquidityChangedEvent" event envelope`,
183
+ );
184
+ }
185
+ for (let index = 0; index < 1; index += 1) {
186
+ if (bytes[index] !== discriminatorBytes[index]) {
187
+ throw new RangeError(
188
+ 'the provided data does not match the "LiquidityChangedEvent" event discriminator.',
189
+ );
190
+ }
191
+ }
192
+ const sourceVersion = bytes[1];
193
+ if (sourceVersion !== 0) {
194
+ throw new RangeError(
195
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
196
+ );
197
+ }
198
+ return { name: "liquidityChanged", data: getLiquidityChangedEventDecoder().decode(bytes) };
199
+ }
200
+
201
+ /** One log entry that named this event. */
202
+ export type DecodedLiquidityChangedEvent = { name: "liquidityChanged"; data: LiquidityChangedEvent };
203
+
204
+ /**
205
+ * Decode a `Program data:` log line, or return `null` when the line is not
206
+ * this event.
207
+ */
208
+ export function parseLiquidityChangedEventFromLog(log: string): DecodedLiquidityChangedEvent | null {
209
+ const prefix = "Program data: ";
210
+ if (!log.startsWith(prefix)) {
211
+ return null;
212
+ }
213
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
214
+ if (bytes.length < 2) {
215
+ return null;
216
+ }
217
+ const discriminatorBytes = getLiquidityChangedEventDiscriminatorBytes();
218
+ for (let index = 0; index < 1; index += 1) {
219
+ if (bytes[index] !== discriminatorBytes[index]) {
220
+ return null;
221
+ }
222
+ }
223
+ if (bytes[1] !== 0) {
224
+ return null;
225
+ }
226
+ return decodeLiquidityChangedEvent(bytes);
227
+ }
228
+
229
+ /**
230
+ * Decode one `feesCollected` record: only records carrying this event's migration version decode.
231
+ */
232
+ export function decodeFeesCollectedEvent(
233
+ data: ReadonlyUint8Array | Uint8Array,
234
+ ): DecodedFeesCollectedEvent {
235
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
236
+ const discriminatorBytes = getFeesCollectedEventDiscriminatorBytes();
237
+ if (bytes.length < 2) {
238
+ throw new RangeError(
239
+ `the provided data is too short for the "FeesCollectedEvent" event envelope`,
240
+ );
241
+ }
242
+ for (let index = 0; index < 1; index += 1) {
243
+ if (bytes[index] !== discriminatorBytes[index]) {
244
+ throw new RangeError(
245
+ 'the provided data does not match the "FeesCollectedEvent" event discriminator.',
246
+ );
247
+ }
248
+ }
249
+ const sourceVersion = bytes[1];
250
+ if (sourceVersion !== 0) {
251
+ throw new RangeError(
252
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
253
+ );
254
+ }
255
+ return { name: "feesCollected", data: getFeesCollectedEventDecoder().decode(bytes) };
256
+ }
257
+
258
+ /** One log entry that named this event. */
259
+ export type DecodedFeesCollectedEvent = { name: "feesCollected"; data: FeesCollectedEvent };
260
+
261
+ /**
262
+ * Decode a `Program data:` log line, or return `null` when the line is not
263
+ * this event.
264
+ */
265
+ export function parseFeesCollectedEventFromLog(log: string): DecodedFeesCollectedEvent | null {
266
+ const prefix = "Program data: ";
267
+ if (!log.startsWith(prefix)) {
268
+ return null;
269
+ }
270
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
271
+ if (bytes.length < 2) {
272
+ return null;
273
+ }
274
+ const discriminatorBytes = getFeesCollectedEventDiscriminatorBytes();
275
+ for (let index = 0; index < 1; index += 1) {
276
+ if (bytes[index] !== discriminatorBytes[index]) {
277
+ return null;
278
+ }
279
+ }
280
+ if (bytes[1] !== 0) {
281
+ return null;
282
+ }
283
+ return decodeFeesCollectedEvent(bytes);
284
+ }
285
+
286
+ /**
287
+ * Decode one `poolSynced` record: only records carrying this event's migration version decode.
288
+ */
289
+ export function decodePoolSyncedEvent(
290
+ data: ReadonlyUint8Array | Uint8Array,
291
+ ): DecodedPoolSyncedEvent {
292
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
293
+ const discriminatorBytes = getPoolSyncedEventDiscriminatorBytes();
294
+ if (bytes.length < 2) {
295
+ throw new RangeError(
296
+ `the provided data is too short for the "PoolSyncedEvent" event envelope`,
297
+ );
298
+ }
299
+ for (let index = 0; index < 1; index += 1) {
300
+ if (bytes[index] !== discriminatorBytes[index]) {
301
+ throw new RangeError(
302
+ 'the provided data does not match the "PoolSyncedEvent" event discriminator.',
303
+ );
304
+ }
305
+ }
306
+ const sourceVersion = bytes[1];
307
+ if (sourceVersion !== 1) {
308
+ throw new RangeError(
309
+ `event migration version mismatch: expected 1, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
310
+ );
311
+ }
312
+ return { name: "poolSynced", data: getPoolSyncedEventDecoder().decode(bytes) };
313
+ }
314
+
315
+ /** One log entry that named this event. */
316
+ export type DecodedPoolSyncedEvent = { name: "poolSynced"; data: PoolSyncedEvent };
317
+
318
+ /**
319
+ * Decode a `Program data:` log line, or return `null` when the line is not
320
+ * this event.
321
+ */
322
+ export function parsePoolSyncedEventFromLog(log: string): DecodedPoolSyncedEvent | null {
323
+ const prefix = "Program data: ";
324
+ if (!log.startsWith(prefix)) {
325
+ return null;
326
+ }
327
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
328
+ if (bytes.length < 2) {
329
+ return null;
330
+ }
331
+ const discriminatorBytes = getPoolSyncedEventDiscriminatorBytes();
332
+ for (let index = 0; index < 1; index += 1) {
333
+ if (bytes[index] !== discriminatorBytes[index]) {
334
+ return null;
335
+ }
336
+ }
337
+ if (bytes[1] !== 1) {
338
+ return null;
339
+ }
340
+ return decodePoolSyncedEvent(bytes);
341
+ }
342
+
343
+ /**
344
+ * Decode one `poolSyncedV0` record: only records carrying this event's migration version decode.
345
+ */
346
+ export function decodePoolSyncedV0Event(
347
+ data: ReadonlyUint8Array | Uint8Array,
348
+ ): DecodedPoolSyncedV0Event {
349
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
350
+ const discriminatorBytes = getPoolSyncedV0EventDiscriminatorBytes();
351
+ if (bytes.length < 2) {
352
+ throw new RangeError(
353
+ `the provided data is too short for the "PoolSyncedV0Event" event envelope`,
354
+ );
355
+ }
356
+ for (let index = 0; index < 1; index += 1) {
357
+ if (bytes[index] !== discriminatorBytes[index]) {
358
+ throw new RangeError(
359
+ 'the provided data does not match the "PoolSyncedV0Event" event discriminator.',
360
+ );
361
+ }
362
+ }
363
+ const sourceVersion = bytes[1];
364
+ if (sourceVersion !== 0) {
365
+ throw new RangeError(
366
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
367
+ );
368
+ }
369
+ return { name: "poolSyncedV0", data: getPoolSyncedV0EventDecoder().decode(bytes) };
370
+ }
371
+
372
+ /** One log entry that named this event. */
373
+ export type DecodedPoolSyncedV0Event = { name: "poolSyncedV0"; data: PoolSyncedV0Event };
374
+
375
+ /**
376
+ * Decode a `Program data:` log line, or return `null` when the line is not
377
+ * this event.
378
+ */
379
+ export function parsePoolSyncedV0EventFromLog(log: string): DecodedPoolSyncedV0Event | null {
380
+ const prefix = "Program data: ";
381
+ if (!log.startsWith(prefix)) {
382
+ return null;
383
+ }
384
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
385
+ if (bytes.length < 2) {
386
+ return null;
387
+ }
388
+ const discriminatorBytes = getPoolSyncedV0EventDiscriminatorBytes();
389
+ for (let index = 0; index < 1; index += 1) {
390
+ if (bytes[index] !== discriminatorBytes[index]) {
391
+ return null;
392
+ }
393
+ }
394
+ if (bytes[1] !== 0) {
395
+ return null;
396
+ }
397
+ return decodePoolSyncedV0Event(bytes);
398
+ }
399
+
400
+ /**
401
+ * Decode one `configCreated` record: only records carrying this event's migration version decode.
402
+ */
403
+ export function decodeConfigCreatedEvent(
404
+ data: ReadonlyUint8Array | Uint8Array,
405
+ ): DecodedConfigCreatedEvent {
406
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
407
+ const discriminatorBytes = getConfigCreatedEventDiscriminatorBytes();
408
+ if (bytes.length < 2) {
409
+ throw new RangeError(
410
+ `the provided data is too short for the "ConfigCreatedEvent" event envelope`,
411
+ );
412
+ }
413
+ for (let index = 0; index < 1; index += 1) {
414
+ if (bytes[index] !== discriminatorBytes[index]) {
415
+ throw new RangeError(
416
+ 'the provided data does not match the "ConfigCreatedEvent" event discriminator.',
417
+ );
418
+ }
419
+ }
420
+ const sourceVersion = bytes[1];
421
+ if (sourceVersion !== 0) {
422
+ throw new RangeError(
423
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
424
+ );
425
+ }
426
+ return { name: "configCreated", data: getConfigCreatedEventDecoder().decode(bytes) };
427
+ }
428
+
429
+ /** One log entry that named this event. */
430
+ export type DecodedConfigCreatedEvent = { name: "configCreated"; data: ConfigCreatedEvent };
431
+
432
+ /**
433
+ * Decode a `Program data:` log line, or return `null` when the line is not
434
+ * this event.
435
+ */
436
+ export function parseConfigCreatedEventFromLog(log: string): DecodedConfigCreatedEvent | null {
437
+ const prefix = "Program data: ";
438
+ if (!log.startsWith(prefix)) {
439
+ return null;
440
+ }
441
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
442
+ if (bytes.length < 2) {
443
+ return null;
444
+ }
445
+ const discriminatorBytes = getConfigCreatedEventDiscriminatorBytes();
446
+ for (let index = 0; index < 1; index += 1) {
447
+ if (bytes[index] !== discriminatorBytes[index]) {
448
+ return null;
449
+ }
450
+ }
451
+ if (bytes[1] !== 0) {
452
+ return null;
453
+ }
454
+ return decodeConfigCreatedEvent(bytes);
455
+ }
456
+
457
+ /**
458
+ * Decode one `configUpdated` record: only records carrying this event's migration version decode.
459
+ */
460
+ export function decodeConfigUpdatedEvent(
461
+ data: ReadonlyUint8Array | Uint8Array,
462
+ ): DecodedConfigUpdatedEvent {
463
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
464
+ const discriminatorBytes = getConfigUpdatedEventDiscriminatorBytes();
465
+ if (bytes.length < 2) {
466
+ throw new RangeError(
467
+ `the provided data is too short for the "ConfigUpdatedEvent" event envelope`,
468
+ );
469
+ }
470
+ for (let index = 0; index < 1; index += 1) {
471
+ if (bytes[index] !== discriminatorBytes[index]) {
472
+ throw new RangeError(
473
+ 'the provided data does not match the "ConfigUpdatedEvent" event discriminator.',
474
+ );
475
+ }
476
+ }
477
+ const sourceVersion = bytes[1];
478
+ if (sourceVersion !== 0) {
479
+ throw new RangeError(
480
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
481
+ );
482
+ }
483
+ return { name: "configUpdated", data: getConfigUpdatedEventDecoder().decode(bytes) };
484
+ }
485
+
486
+ /** One log entry that named this event. */
487
+ export type DecodedConfigUpdatedEvent = { name: "configUpdated"; data: ConfigUpdatedEvent };
488
+
489
+ /**
490
+ * Decode a `Program data:` log line, or return `null` when the line is not
491
+ * this event.
492
+ */
493
+ export function parseConfigUpdatedEventFromLog(log: string): DecodedConfigUpdatedEvent | null {
494
+ const prefix = "Program data: ";
495
+ if (!log.startsWith(prefix)) {
496
+ return null;
497
+ }
498
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
499
+ if (bytes.length < 2) {
500
+ return null;
501
+ }
502
+ const discriminatorBytes = getConfigUpdatedEventDiscriminatorBytes();
503
+ for (let index = 0; index < 1; index += 1) {
504
+ if (bytes[index] !== discriminatorBytes[index]) {
505
+ return null;
506
+ }
507
+ }
508
+ if (bytes[1] !== 0) {
509
+ return null;
510
+ }
511
+ return decodeConfigUpdatedEvent(bytes);
512
+ }
513
+
514
+ /**
515
+ * Decode one `poolCreatorChanged` record: only records carrying this event's migration version decode.
516
+ */
517
+ export function decodePoolCreatorChangedEvent(
518
+ data: ReadonlyUint8Array | Uint8Array,
519
+ ): DecodedPoolCreatorChangedEvent {
520
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
521
+ const discriminatorBytes = getPoolCreatorChangedEventDiscriminatorBytes();
522
+ if (bytes.length < 2) {
523
+ throw new RangeError(
524
+ `the provided data is too short for the "PoolCreatorChangedEvent" event envelope`,
525
+ );
526
+ }
527
+ for (let index = 0; index < 1; index += 1) {
528
+ if (bytes[index] !== discriminatorBytes[index]) {
529
+ throw new RangeError(
530
+ 'the provided data does not match the "PoolCreatorChangedEvent" event discriminator.',
531
+ );
532
+ }
533
+ }
534
+ const sourceVersion = bytes[1];
535
+ if (sourceVersion !== 0) {
536
+ throw new RangeError(
537
+ `event migration version mismatch: expected 0, received ${sourceVersion} (decode it with the event for that version, or regenerate this client)`,
538
+ );
539
+ }
540
+ return { name: "poolCreatorChanged", data: getPoolCreatorChangedEventDecoder().decode(bytes) };
541
+ }
542
+
543
+ /** One log entry that named this event. */
544
+ export type DecodedPoolCreatorChangedEvent = { name: "poolCreatorChanged"; data: PoolCreatorChangedEvent };
545
+
546
+ /**
547
+ * Decode a `Program data:` log line, or return `null` when the line is not
548
+ * this event.
549
+ */
550
+ export function parsePoolCreatorChangedEventFromLog(log: string): DecodedPoolCreatorChangedEvent | null {
551
+ const prefix = "Program data: ";
552
+ if (!log.startsWith(prefix)) {
553
+ return null;
554
+ }
555
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
556
+ if (bytes.length < 2) {
557
+ return null;
558
+ }
559
+ const discriminatorBytes = getPoolCreatorChangedEventDiscriminatorBytes();
560
+ for (let index = 0; index < 1; index += 1) {
561
+ if (bytes[index] !== discriminatorBytes[index]) {
562
+ return null;
563
+ }
564
+ }
565
+ if (bytes[1] !== 0) {
566
+ return null;
567
+ }
568
+ return decodePoolCreatorChangedEvent(bytes);
569
+ }
570
+
571
+
572
+ /**
573
+ * Explain a `Program data:` line that names a migration-aware event but that
574
+ * no generated event claimed, or return `null` for an unrelated line.
575
+ */
576
+ function unrecognizedEventVersion(log: string): string | null {
577
+ const prefix = "Program data: ";
578
+ if (!log.startsWith(prefix)) {
579
+ return null;
580
+ }
581
+ const bytes = getBase64Encoder().encode(log.slice(prefix.length));
582
+ if (bytes.length >= 1 && bytes[0] === 1) {
583
+ return bytes.length < 2
584
+ ? 'event "poolCreated" log is too short for its version envelope'
585
+ : `event "poolCreated" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
586
+ }
587
+ if (bytes.length >= 1 && bytes[0] === 2) {
588
+ return bytes.length < 2
589
+ ? 'event "swapped" log is too short for its version envelope'
590
+ : `event "swapped" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
591
+ }
592
+ if (bytes.length >= 1 && bytes[0] === 3) {
593
+ return bytes.length < 2
594
+ ? 'event "liquidityChanged" log is too short for its version envelope'
595
+ : `event "liquidityChanged" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
596
+ }
597
+ if (bytes.length >= 1 && bytes[0] === 4) {
598
+ return bytes.length < 2
599
+ ? 'event "feesCollected" log is too short for its version envelope'
600
+ : `event "feesCollected" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
601
+ }
602
+ if (bytes.length >= 1 && bytes[0] === 5) {
603
+ return bytes.length < 2
604
+ ? 'event "poolSynced" log is too short for its version envelope'
605
+ : `event "poolSynced" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
606
+ }
607
+ if (bytes.length >= 1 && bytes[0] === 8) {
608
+ return bytes.length < 2
609
+ ? 'event "configCreated" log is too short for its version envelope'
610
+ : `event "configCreated" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
611
+ }
612
+ if (bytes.length >= 1 && bytes[0] === 6) {
613
+ return bytes.length < 2
614
+ ? 'event "configUpdated" log is too short for its version envelope'
615
+ : `event "configUpdated" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
616
+ }
617
+ if (bytes.length >= 1 && bytes[0] === 7) {
618
+ return bytes.length < 2
619
+ ? 'event "poolCreatorChanged" log is too short for its version envelope'
620
+ : `event "poolCreatorChanged" log carries migration version ${bytes[1]}, which this client cannot decode; regenerate it`;
621
+ }
622
+ return null;
623
+ }
624
+
625
+ /** Every event this program can emit, as decoded from a log line. */
626
+ export type DecodedPinaAmmEvent =
627
+ | DecodedPoolCreatedEvent
628
+ | DecodedSwappedEvent
629
+ | DecodedLiquidityChangedEvent
630
+ | DecodedFeesCollectedEvent
631
+ | DecodedPoolSyncedEvent
632
+ | DecodedPoolSyncedV0Event
633
+ | DecodedConfigCreatedEvent
634
+ | DecodedConfigUpdatedEvent
635
+ | DecodedPoolCreatorChangedEvent;
636
+
637
+ /** The program whose invocation frames emit the events decoded here. */
638
+ export const PINA_AMM_EVENT_SOURCE_ADDRESS = "pAMMzVPRwtrxQs2CjLTUUpfCAp9XdvVmwwVhpAuCobi";
639
+
640
+ const PROGRAM_INVOKE_LOG = /^Program (\S+) invoke \[\d+\]$/;
641
+ const PROGRAM_EXIT_LOG = /^Program (\S+) (?:success|failed: .*)$/;
642
+
643
+ /**
644
+ * Decode every `Program data:` line this program emitted in a transaction's
645
+ * logs.
646
+ *
647
+ * `logs` must be the complete, ordered log messages of one transaction. The
648
+ * parser follows the runtime's `Program <address> invoke [n]` and
649
+ * `Program <address> success` / `failed` frames and decodes a data line only
650
+ * while `programAddress` is the innermost invoked program. Any program can
651
+ * write a `Program data:` line with this program's discriminator, so data
652
+ * lines from other programs (including ones this program invokes through CPI)
653
+ * and lines outside any frame are skipped rather than trusted.
654
+ *
655
+ * Unrelated lines are skipped. A line this program emitted that names an event
656
+ * but carries a version no generated event describes throws instead of being
657
+ * silently dropped. The per-event `parse*FromLog` helpers decode one line
658
+ * without this attribution and are only safe for data already known to come
659
+ * from this program.
660
+ */
661
+ export function parsePinaAmmEventsFromLogs(
662
+ logs: readonly string[],
663
+ programAddress: string = PINA_AMM_EVENT_SOURCE_ADDRESS,
664
+ ): DecodedPinaAmmEvent[] {
665
+ const discovered: DecodedPinaAmmEvent[] = [];
666
+ const frames: string[] = [];
667
+ for (const log of logs) {
668
+ const invoke = PROGRAM_INVOKE_LOG.exec(log);
669
+ if (invoke !== null) {
670
+ frames.push(invoke[1] ?? "");
671
+ continue;
672
+ }
673
+ const exit = PROGRAM_EXIT_LOG.exec(log);
674
+ if (exit !== null) {
675
+ frames.pop();
676
+ continue;
677
+ }
678
+ if (frames.length === 0 || frames[frames.length - 1] !== programAddress) {
679
+ continue;
680
+ }
681
+ const poolCreated = parsePoolCreatedEventFromLog(log);
682
+ if (poolCreated !== null) {
683
+ discovered.push(poolCreated);
684
+ continue;
685
+ }
686
+ const swapped = parseSwappedEventFromLog(log);
687
+ if (swapped !== null) {
688
+ discovered.push(swapped);
689
+ continue;
690
+ }
691
+ const liquidityChanged = parseLiquidityChangedEventFromLog(log);
692
+ if (liquidityChanged !== null) {
693
+ discovered.push(liquidityChanged);
694
+ continue;
695
+ }
696
+ const feesCollected = parseFeesCollectedEventFromLog(log);
697
+ if (feesCollected !== null) {
698
+ discovered.push(feesCollected);
699
+ continue;
700
+ }
701
+ const poolSynced = parsePoolSyncedEventFromLog(log);
702
+ if (poolSynced !== null) {
703
+ discovered.push(poolSynced);
704
+ continue;
705
+ }
706
+ const poolSyncedV0 = parsePoolSyncedV0EventFromLog(log);
707
+ if (poolSyncedV0 !== null) {
708
+ discovered.push(poolSyncedV0);
709
+ continue;
710
+ }
711
+ const configCreated = parseConfigCreatedEventFromLog(log);
712
+ if (configCreated !== null) {
713
+ discovered.push(configCreated);
714
+ continue;
715
+ }
716
+ const configUpdated = parseConfigUpdatedEventFromLog(log);
717
+ if (configUpdated !== null) {
718
+ discovered.push(configUpdated);
719
+ continue;
720
+ }
721
+ const poolCreatorChanged = parsePoolCreatorChangedEventFromLog(log);
722
+ if (poolCreatorChanged !== null) {
723
+ discovered.push(poolCreatorChanged);
724
+ continue;
725
+ }
726
+ const unknownVersion = unrecognizedEventVersion(log);
727
+ if (unknownVersion !== null) {
728
+ throw new RangeError(unknownVersion);
729
+ }
730
+ }
731
+ return discovered;
732
+ }