@liquid-af/sdk 0.4.0 → 0.5.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.
- package/dist/accounts/liquid-swap.d.ts +4 -6
- package/dist/accounts/liquid-swap.d.ts.map +1 -1
- package/dist/accounts/liquid.d.ts.map +1 -1
- package/dist/accounts/liquid.js.map +1 -1
- package/dist/client.d.ts +11 -8
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +10 -3
- package/dist/client.js.map +1 -1
- package/dist/events/parser.d.ts +8 -3
- package/dist/events/parser.d.ts.map +1 -1
- package/dist/events/parser.js +72 -17
- package/dist/events/parser.js.map +1 -1
- package/dist/helpers/preview.d.ts.map +1 -1
- package/dist/helpers/preview.js.map +1 -1
- package/dist/idl/index.d.ts +12 -1
- package/dist/idl/index.d.ts.map +1 -1
- package/dist/idl/index.js +12 -1
- package/dist/idl/index.js.map +1 -1
- package/dist/idl/liquid.d.ts +7116 -2254
- package/dist/idl/liquid.d.ts.map +1 -1
- package/dist/idl/liquid.json +7200 -2338
- package/dist/idl/liquid_events.d.ts +3118 -0
- package/dist/idl/liquid_events.d.ts.map +1 -0
- package/dist/idl/liquid_events.js +2 -0
- package/dist/idl/liquid_events.js.map +1 -0
- package/dist/idl/liquid_events.json +3111 -0
- package/dist/idl/liquid_fees.d.ts +539 -0
- package/dist/idl/liquid_fees.d.ts.map +1 -1
- package/dist/idl/liquid_fees.json +539 -0
- package/dist/idl/liquid_state.d.ts +350 -0
- package/dist/idl/liquid_state.d.ts.map +1 -1
- package/dist/idl/liquid_state.json +350 -0
- package/dist/idl/liquid_swap.d.ts +1329 -106
- package/dist/idl/liquid_swap.d.ts.map +1 -1
- package/dist/idl/liquid_swap.json +1335 -112
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/instructions/index.d.ts +4 -4
- package/dist/instructions/index.d.ts.map +1 -1
- package/dist/instructions/index.js +2 -2
- package/dist/instructions/index.js.map +1 -1
- package/dist/instructions/liquid-fees.d.ts +12 -0
- package/dist/instructions/liquid-fees.d.ts.map +1 -1
- package/dist/instructions/liquid-fees.js +17 -0
- package/dist/instructions/liquid-fees.js.map +1 -1
- package/dist/instructions/liquid.d.ts +15 -1
- package/dist/instructions/liquid.d.ts.map +1 -1
- package/dist/instructions/liquid.js +32 -2
- package/dist/instructions/liquid.js.map +1 -1
- package/dist/pda/index.d.ts +2 -2
- package/dist/pda/index.d.ts.map +1 -1
- package/dist/pda/index.js +2 -2
- package/dist/pda/index.js.map +1 -1
- package/dist/pda/liquid-fees.d.ts +7 -0
- package/dist/pda/liquid-fees.d.ts.map +1 -1
- package/dist/pda/liquid-fees.js +10 -0
- package/dist/pda/liquid-fees.js.map +1 -1
- package/dist/pda/liquid.d.ts +10 -3
- package/dist/pda/liquid.d.ts.map +1 -1
- package/dist/pda/liquid.js +14 -5
- package/dist/pda/liquid.js.map +1 -1
- package/package.json +1 -1
- package/src/accounts/liquid.ts +1 -4
- package/src/client.ts +17 -14
- package/src/events/parser.ts +88 -19
- package/src/helpers/preview.ts +1 -5
- package/src/idl/index.ts +26 -1
- package/src/idl/liquid.json +7200 -2338
- package/src/idl/liquid.ts +7200 -2338
- package/src/idl/liquid_events.json +3111 -0
- package/src/idl/liquid_events.ts +3117 -0
- package/src/idl/liquid_fees.json +539 -0
- package/src/idl/liquid_fees.ts +539 -0
- package/src/idl/liquid_state.json +350 -0
- package/src/idl/liquid_state.ts +350 -0
- package/src/idl/liquid_swap.json +1335 -112
- package/src/idl/liquid_swap.ts +1335 -112
- package/src/index.ts +3 -0
- package/src/instructions/index.ts +4 -0
- package/src/instructions/liquid-fees.ts +26 -0
- package/src/instructions/liquid.ts +70 -1
- package/src/pda/index.ts +2 -0
- package/src/pda/liquid-fees.ts +13 -0
- package/src/pda/liquid.ts +21 -9
package/src/client.ts
CHANGED
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
buildMigrateNative,
|
|
29
29
|
buildInitializeNativeReferralVault,
|
|
30
30
|
buildWithdrawReferralRewards,
|
|
31
|
+
buildWithdrawReferralTokenRewards,
|
|
31
32
|
buildExecuteBuybackNative,
|
|
32
33
|
buildExecuteBuybackToken,
|
|
33
34
|
buildCreateStableCurve,
|
|
@@ -250,13 +251,24 @@ export class LiquidClient {
|
|
|
250
251
|
});
|
|
251
252
|
}
|
|
252
253
|
|
|
253
|
-
/** Builds a withdrawReferralRewards instruction. */
|
|
254
|
+
/** Builds a withdrawReferralRewards instruction (native SOL). */
|
|
254
255
|
buildWithdrawReferralRewards(params: {
|
|
255
256
|
user: PublicKey;
|
|
256
257
|
}): Promise<TransactionInstruction> {
|
|
257
258
|
return buildWithdrawReferralRewards({ ...params, config: this.config });
|
|
258
259
|
}
|
|
259
260
|
|
|
261
|
+
/** Builds a withdrawReferralTokenRewards instruction (SPL token). */
|
|
262
|
+
buildWithdrawReferralTokenRewards(params: {
|
|
263
|
+
user: PublicKey;
|
|
264
|
+
quoteMint: PublicKey;
|
|
265
|
+
}): Promise<TransactionInstruction> {
|
|
266
|
+
return buildWithdrawReferralTokenRewards({
|
|
267
|
+
...params,
|
|
268
|
+
config: this.config,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
260
272
|
/** Builds an executeBuybackNative instruction for native SOL curves. Permissionless; requires revoked fee config. */
|
|
261
273
|
buildExecuteBuybackNative(params: {
|
|
262
274
|
payer: PublicKey;
|
|
@@ -372,7 +384,7 @@ export class LiquidClient {
|
|
|
372
384
|
return buildMigrateStable({ ...params, config: this.config });
|
|
373
385
|
}
|
|
374
386
|
|
|
375
|
-
/** Builds an initializeReferralTokenVault instruction (creates a
|
|
387
|
+
/** Builds an initializeReferralTokenVault instruction (creates a PDA token vault for stable curve referral rewards). */
|
|
376
388
|
buildInitializeReferralTokenVault(params: {
|
|
377
389
|
user: PublicKey;
|
|
378
390
|
quoteMint: PublicKey;
|
|
@@ -758,11 +770,7 @@ export class LiquidClient {
|
|
|
758
770
|
|
|
759
771
|
/** Fetches a stable bonding curve account by token mint. */
|
|
760
772
|
fetchStableBondingCurve(mint: PublicKey) {
|
|
761
|
-
return fetchStableBondingCurveFn(
|
|
762
|
-
this.connection,
|
|
763
|
-
mint,
|
|
764
|
-
this.config,
|
|
765
|
-
);
|
|
773
|
+
return fetchStableBondingCurveFn(this.connection, mint, this.config);
|
|
766
774
|
}
|
|
767
775
|
|
|
768
776
|
/** Fetches a stable bonding curve account by its PDA address directly. */
|
|
@@ -932,13 +940,8 @@ export class LiquidClient {
|
|
|
932
940
|
}
|
|
933
941
|
|
|
934
942
|
/** Derives the stable bonding curve PDA for a token mint. */
|
|
935
|
-
getStableBondingCurvePDA(
|
|
936
|
-
mint
|
|
937
|
-
): [PublicKey, number] {
|
|
938
|
-
return getStableBondingCurvePDA(
|
|
939
|
-
mint,
|
|
940
|
-
this.config.liquidProgramId,
|
|
941
|
-
);
|
|
943
|
+
getStableBondingCurvePDA(mint: PublicKey): [PublicKey, number] {
|
|
944
|
+
return getStableBondingCurvePDA(mint, this.config.liquidProgramId);
|
|
942
945
|
}
|
|
943
946
|
|
|
944
947
|
/** Derives the SOL vault PDA for a bonding curve. */
|
package/src/events/parser.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BorshCoder,
|
|
1
|
+
import { BorshCoder, type Program } from "@coral-xyz/anchor";
|
|
2
2
|
|
|
3
3
|
/** Parsed event with name and data */
|
|
4
4
|
export interface ParsedEvent<T = unknown> {
|
|
@@ -6,8 +6,15 @@ export interface ParsedEvent<T = unknown> {
|
|
|
6
6
|
data: T;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
const PROGRAM_DATA = "Program data: ";
|
|
10
|
+
const PROGRAM_LOG = "Program log: ";
|
|
11
|
+
const INVOKE_RE = /^Program ([1-9A-HJ-NP-Za-km-z]+) invoke \[(\d+)\]$/;
|
|
12
|
+
const EXIT_RE = /^Program ([1-9A-HJ-NP-Za-km-z]+) (success|failed)/;
|
|
13
|
+
|
|
9
14
|
/**
|
|
10
15
|
* Parses events from transaction logs for a given program.
|
|
16
|
+
* Unlike Anchor's built-in EventParser, this correctly identifies events
|
|
17
|
+
* from programs invoked via CPI (not just top-level invocations).
|
|
11
18
|
*
|
|
12
19
|
* @param logs - Transaction log messages
|
|
13
20
|
* @param program - The Anchor program to parse events for
|
|
@@ -17,16 +24,11 @@ export const parseTransactionEvents = (
|
|
|
17
24
|
logs: string[],
|
|
18
25
|
program: Program,
|
|
19
26
|
): ParsedEvent[] => {
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
return parseCpiLogs(
|
|
28
|
+
logs,
|
|
29
|
+
program.programId.toString(),
|
|
22
30
|
new BorshCoder(program.idl),
|
|
23
31
|
);
|
|
24
|
-
const events: ParsedEvent[] = [];
|
|
25
|
-
const generator = parser.parseLogs(logs);
|
|
26
|
-
for (const event of generator) {
|
|
27
|
-
events.push({ name: event.name, data: event.data });
|
|
28
|
-
}
|
|
29
|
-
return events;
|
|
30
32
|
};
|
|
31
33
|
|
|
32
34
|
/**
|
|
@@ -49,10 +51,13 @@ export const addEventListener = <T>(
|
|
|
49
51
|
};
|
|
50
52
|
|
|
51
53
|
/**
|
|
52
|
-
*
|
|
53
|
-
* arrives or the timeout is exceeded.
|
|
54
|
+
* Subscribes to logs via WebSocket, fires a trigger, and polls until the
|
|
55
|
+
* event arrives or the timeout is exceeded.
|
|
54
56
|
*
|
|
55
|
-
*
|
|
57
|
+
* 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.
|
|
59
|
+
*
|
|
60
|
+
* @param program - The Anchor program whose events to listen for
|
|
56
61
|
* @param eventName - Name of the event to wait for
|
|
57
62
|
* @param trigger - Async function to trigger the event (e.g., sendAndConfirm)
|
|
58
63
|
* @param timeoutMs - Maximum wait time in milliseconds (default: 5000)
|
|
@@ -67,24 +72,88 @@ export async function waitForEvent<TEvent, TResult = string>(
|
|
|
67
72
|
timeoutMs = 5000,
|
|
68
73
|
): Promise<{ result: TResult; event: TEvent }> {
|
|
69
74
|
let eventData: TEvent | null = null;
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
const programId = program.programId.toString();
|
|
76
|
+
const coder = new BorshCoder(program.idl);
|
|
77
|
+
|
|
78
|
+
// Subscribe to logs mentioning this program (includes CPI invocations)
|
|
79
|
+
const subscriptionId = program.provider.connection.onLogs(
|
|
80
|
+
program.programId,
|
|
81
|
+
(logs) => {
|
|
82
|
+
if (logs.err) return;
|
|
83
|
+
const events = parseCpiLogs(logs.logs, programId, coder);
|
|
84
|
+
for (const event of events) {
|
|
85
|
+
if (event.name === eventName) {
|
|
86
|
+
eventData = event.data as TEvent;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
|
|
73
92
|
try {
|
|
74
93
|
const result = await trigger();
|
|
75
94
|
const start = Date.now();
|
|
76
95
|
while (!eventData && Date.now() - start < timeoutMs) {
|
|
77
96
|
await new Promise((r) => setTimeout(r, 200));
|
|
78
97
|
}
|
|
79
|
-
await program.removeEventListener(listenerId);
|
|
80
98
|
if (!eventData) {
|
|
81
99
|
throw new Error(
|
|
82
100
|
`Event '${eventName}' not received within ${timeoutMs}ms`,
|
|
83
101
|
);
|
|
84
102
|
}
|
|
85
103
|
return { result, event: eventData };
|
|
86
|
-
}
|
|
87
|
-
await program.
|
|
88
|
-
throw err;
|
|
104
|
+
} finally {
|
|
105
|
+
await program.provider.connection.removeOnLogsListener(subscriptionId);
|
|
89
106
|
}
|
|
90
107
|
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Parses events from transaction logs, properly handling CPI contexts.
|
|
111
|
+
*
|
|
112
|
+
* Anchor's built-in EventParser pushes a generic "cpi" string onto its
|
|
113
|
+
* execution stack for nested invocations, so it cannot identify events
|
|
114
|
+
* emitted by a CPI-only program. This parser extracts the actual program
|
|
115
|
+
* ID from each `invoke` log line, enabling correct attribution of events
|
|
116
|
+
* to programs at any CPI depth.
|
|
117
|
+
*/
|
|
118
|
+
function parseCpiLogs(
|
|
119
|
+
logs: string[],
|
|
120
|
+
programId: string,
|
|
121
|
+
coder: BorshCoder,
|
|
122
|
+
): ParsedEvent[] {
|
|
123
|
+
const events: ParsedEvent[] = [];
|
|
124
|
+
const stack: string[] = [];
|
|
125
|
+
|
|
126
|
+
for (const log of logs) {
|
|
127
|
+
if (!log.startsWith("Program ")) continue;
|
|
128
|
+
|
|
129
|
+
const invokeMatch = INVOKE_RE.exec(log);
|
|
130
|
+
if (invokeMatch) {
|
|
131
|
+
stack.push(invokeMatch[1]);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const exitMatch = EXIT_RE.exec(log);
|
|
136
|
+
if (exitMatch) {
|
|
137
|
+
stack.pop();
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Only try to decode events when inside the target program's context
|
|
142
|
+
if (stack.length > 0 && stack[stack.length - 1] === programId) {
|
|
143
|
+
let logStr: string | null = null;
|
|
144
|
+
if (log.startsWith(PROGRAM_DATA)) {
|
|
145
|
+
logStr = log.slice(PROGRAM_DATA.length);
|
|
146
|
+
} else if (log.startsWith(PROGRAM_LOG)) {
|
|
147
|
+
logStr = log.slice(PROGRAM_LOG.length);
|
|
148
|
+
}
|
|
149
|
+
if (logStr) {
|
|
150
|
+
const event = coder.events.decode(logStr);
|
|
151
|
+
if (event) {
|
|
152
|
+
events.push({ name: event.name, data: event.data });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return events;
|
|
159
|
+
}
|
package/src/helpers/preview.ts
CHANGED
|
@@ -464,11 +464,7 @@ async function resolveStableCurve(
|
|
|
464
464
|
pre?.globalConfig ?? fetchCurveGlobalConfig(connection, config),
|
|
465
465
|
pre?.bondingCurve
|
|
466
466
|
? null
|
|
467
|
-
: fetchStableBondingCurve(
|
|
468
|
-
connection,
|
|
469
|
-
mint,
|
|
470
|
-
config,
|
|
471
|
-
),
|
|
467
|
+
: fetchStableBondingCurve(connection, mint, config),
|
|
472
468
|
]);
|
|
473
469
|
|
|
474
470
|
const bondingCurve = pre?.bondingCurve ?? {
|
package/src/idl/index.ts
CHANGED
|
@@ -8,8 +8,15 @@ import liquidIdl from "./liquid.json" with { type: "json" };
|
|
|
8
8
|
import liquidSwapIdl from "./liquid_swap.json" with { type: "json" };
|
|
9
9
|
import liquidFeesIdl from "./liquid_fees.json" with { type: "json" };
|
|
10
10
|
import liquidStateIdl from "./liquid_state.json" with { type: "json" };
|
|
11
|
+
import liquidEventsIdl from "./liquid_events.json" with { type: "json" };
|
|
11
12
|
|
|
12
|
-
export {
|
|
13
|
+
export {
|
|
14
|
+
liquidIdl,
|
|
15
|
+
liquidSwapIdl,
|
|
16
|
+
liquidFeesIdl,
|
|
17
|
+
liquidStateIdl,
|
|
18
|
+
liquidEventsIdl,
|
|
19
|
+
};
|
|
13
20
|
export { patchIdl } from "./patch-idl.js";
|
|
14
21
|
|
|
15
22
|
// Re-export Anchor-generated IDL types
|
|
@@ -17,11 +24,13 @@ export type { Liquid } from "./liquid.js";
|
|
|
17
24
|
export type { LiquidSwap } from "./liquid_swap.js";
|
|
18
25
|
export type { LiquidFees } from "./liquid_fees.js";
|
|
19
26
|
export type { LiquidState } from "./liquid_state.js";
|
|
27
|
+
export type { LiquidEvents } from "./liquid_events.js";
|
|
20
28
|
|
|
21
29
|
import type { Liquid } from "./liquid.js";
|
|
22
30
|
import type { LiquidSwap } from "./liquid_swap.js";
|
|
23
31
|
import type { LiquidFees } from "./liquid_fees.js";
|
|
24
32
|
import type { LiquidState } from "./liquid_state.js";
|
|
33
|
+
import type { LiquidEvents } from "./liquid_events.js";
|
|
25
34
|
|
|
26
35
|
/**
|
|
27
36
|
* Creates a typed Program instance for the Liquid bonding curve program.
|
|
@@ -115,3 +124,19 @@ export function getLiquidStateProgram(
|
|
|
115
124
|
: liquidStateIdl;
|
|
116
125
|
return new Program(idl as LiquidState, createReadonlyProvider(connection));
|
|
117
126
|
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Creates a typed Program instance for the Liquid Events program.
|
|
130
|
+
* Uses a readonly provider — no signing capability.
|
|
131
|
+
*
|
|
132
|
+
* @param connection - Solana RPC connection
|
|
133
|
+
* @returns Read-only Program instance typed as LiquidEvents
|
|
134
|
+
*/
|
|
135
|
+
export function getLiquidEventsProgram(
|
|
136
|
+
connection: Connection,
|
|
137
|
+
): Program<LiquidEvents> {
|
|
138
|
+
return new Program(
|
|
139
|
+
liquidEventsIdl as LiquidEvents,
|
|
140
|
+
createReadonlyProvider(connection),
|
|
141
|
+
);
|
|
142
|
+
}
|