@liquid-af/sdk 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
1
  import { BorshCoder, type Program } from "@coral-xyz/anchor";
2
+ import type { LiquidEvents } from "../idl/liquid_events.js";
3
+ import type { LiquidEventMap, LiquidEventName } from "./types.js";
2
4
 
3
5
  /** Parsed event with name and data */
4
6
  export interface ParsedEvent<T = unknown> {
@@ -12,17 +14,17 @@ const INVOKE_RE = /^Program ([1-9A-HJ-NP-Za-km-z]+) invoke \[(\d+)\]$/;
12
14
  const EXIT_RE = /^Program ([1-9A-HJ-NP-Za-km-z]+) (success|failed)/;
13
15
 
14
16
  /**
15
- * Parses events from transaction logs for a given program.
17
+ * Parses events from transaction logs for the liquid-events program.
16
18
  * Unlike Anchor's built-in EventParser, this correctly identifies events
17
19
  * from programs invoked via CPI (not just top-level invocations).
18
20
  *
19
21
  * @param logs - Transaction log messages
20
- * @param program - The Anchor program to parse events for
22
+ * @param program - The liquid-events program instance
21
23
  * @returns Array of parsed events
22
24
  */
23
25
  export const parseTransactionEvents = (
24
26
  logs: string[],
25
- program: Program,
27
+ program: Program<LiquidEvents>,
26
28
  ): ParsedEvent[] => {
27
29
  return parseCpiLogs(
28
30
  logs,
@@ -32,21 +34,24 @@ export const parseTransactionEvents = (
32
34
  };
33
35
 
34
36
  /**
35
- * Registers an event listener on a program.
37
+ * Registers an event listener on the liquid-events program.
36
38
  * Returns the listener ID (use program.removeEventListener to unsubscribe).
37
39
  *
38
- * @param program - The Anchor program
40
+ * @param program - The liquid-events program instance
39
41
  * @param eventName - Name of the event to listen for
40
42
  * @param callback - Handler called when event is received
41
43
  * @returns Listener ID for removal
42
44
  */
43
- export const addEventListener = <T>(
44
- program: Program,
45
- eventName: string,
46
- callback: (event: T) => void,
45
+ export const addEventListener = <N extends LiquidEventName>(
46
+ program: Program<LiquidEvents>,
47
+ eventName: N,
48
+ callback: (event: LiquidEventMap[N]) => void,
47
49
  ): number => {
48
- return program.addEventListener(eventName, (data: T) => {
49
- callback(data);
50
+ // Cast needed: Anchor's IdlEvents types differ structurally from our
51
+ // hand-written interfaces (e.g. enum variant wrapping), but the runtime
52
+ // data is identical.
53
+ return program.addEventListener(eventName, (data: unknown) => {
54
+ callback(data as LiquidEventMap[N]);
50
55
  });
51
56
  };
52
57
 
@@ -55,23 +60,22 @@ export const addEventListener = <T>(
55
60
  * event arrives or the timeout is exceeded.
56
61
  *
57
62
  * Uses CPI-aware log parsing so events emitted by programs that are only
58
- * invoked via CPI (e.g., a centralized events program) are correctly detected.
63
+ * invoked via CPI (e.g., the centralized events program) are correctly detected.
59
64
  *
60
- * @param program - The Anchor program whose events to listen for
65
+ * @param program - The liquid-events program instance
61
66
  * @param eventName - Name of the event to wait for
62
67
  * @param trigger - Async function to trigger the event (e.g., sendAndConfirm)
63
68
  * @param timeoutMs - Maximum wait time in milliseconds (default: 5000)
64
69
  * @returns Object containing the trigger result and the captured event data
65
70
  * @throws If the event is not received within the timeout period
66
71
  */
67
- export async function waitForEvent<TEvent, TResult = string>(
68
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
- program: Program<any>,
70
- eventName: string,
72
+ export async function waitForEvent<N extends LiquidEventName, TResult = string>(
73
+ program: Program<LiquidEvents>,
74
+ eventName: N,
71
75
  trigger: () => Promise<TResult>,
72
76
  timeoutMs = 5000,
73
- ): Promise<{ result: TResult; event: TEvent }> {
74
- let eventData: TEvent | null = null;
77
+ ): Promise<{ result: TResult; event: LiquidEventMap[N] }> {
78
+ let eventData: LiquidEventMap[N] | null = null;
75
79
  const programId = program.programId.toString();
76
80
  const coder = new BorshCoder(program.idl);
77
81
 
@@ -83,7 +87,7 @@ export async function waitForEvent<TEvent, TResult = string>(
83
87
  const events = parseCpiLogs(logs.logs, programId, coder);
84
88
  for (const event of events) {
85
89
  if (event.name === eventName) {
86
- eventData = event.data as TEvent;
90
+ eventData = event.data as LiquidEventMap[N];
87
91
  }
88
92
  }
89
93
  },
@@ -224,6 +224,15 @@ export interface FeesDistributedEvent {
224
224
  timestamp: BN;
225
225
  }
226
226
 
227
+ /** Event emitted when a recipient claims accumulated fees */
228
+ export interface FeesClaimedEvent {
229
+ feeConfig: PublicKey;
230
+ mint: PublicKey;
231
+ amount: BN;
232
+ recipient: PublicKey;
233
+ timestamp: BN;
234
+ }
235
+
227
236
  /** Event emitted when a fee configuration is revoked */
228
237
  export interface FeeConfigRevokedEvent {
229
238
  feeConfig: PublicKey;
@@ -308,3 +317,37 @@ export interface CashbackSpentEvent {
308
317
  remainingBalance: BN;
309
318
  timestamp: BN;
310
319
  }
320
+
321
+ /** Maps camelCase event names (as Anchor uses in JS) to their typed data interfaces */
322
+ export interface LiquidEventMap {
323
+ tokenCreated: TokenCreatedEvent;
324
+ tradeEvent: TradeEvent;
325
+ migrationCompleted: MigrationCompletedEvent;
326
+ curveCompleted: CurveCompletedEvent;
327
+ curveBuybackExecuted: BondingCurveBuybackExecutedEvent;
328
+ referralRewardsWithdrawn: ReferralRewardsWithdrawnEvent;
329
+ curveProtocolPaused: BondingCurveProtocolPausedEvent;
330
+ curveProtocolUnpaused: BondingCurveProtocolUnpausedEvent;
331
+ swapEvent: SwapEvent;
332
+ lpChangeEvent: LpChangeEvent;
333
+ poolCreated: PoolCreatedEvent;
334
+ ammBuybackExecuted: AmmBuybackExecutedEvent;
335
+ ammProtocolPaused: AmmProtocolPausedEvent;
336
+ ammProtocolUnpaused: AmmProtocolUnpausedEvent;
337
+ feesDistributed: FeesDistributedEvent;
338
+ feesClaimed: FeesClaimedEvent;
339
+ feeConfigRevoked: FeeConfigRevokedEvent;
340
+ dealCreated: DealCreatedEvent;
341
+ dealUpdated: DealUpdatedEvent;
342
+ dealClosed: DealClosedEvent;
343
+ dealRedeemed: DealRedeemedEvent;
344
+ snapshotTaken: SnapshotTakenEvent;
345
+ userUpdatedByAdmin: UserUpdatedByAdminEvent;
346
+ referrerSet: ReferrerSetEvent;
347
+ cashbackModeActivated: CashbackModeActivatedEvent;
348
+ cashbackEarned: CashbackEarnedEvent;
349
+ cashbackSpent: CashbackSpentEvent;
350
+ }
351
+
352
+ /** All valid event names (camelCase, as Anchor uses in JS) */
353
+ export type LiquidEventName = keyof LiquidEventMap;