@omnipair/dusk-sdk 2.0.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 (69) hide show
  1. package/README.md +170 -0
  2. package/dist/address.d.ts +7 -0
  3. package/dist/address.d.ts.map +1 -0
  4. package/dist/address.js +29 -0
  5. package/dist/address.js.map +1 -0
  6. package/dist/constants.d.ts +132 -0
  7. package/dist/constants.d.ts.map +1 -0
  8. package/dist/constants.js +330 -0
  9. package/dist/constants.js.map +1 -0
  10. package/dist/dusk.d.ts +24 -0
  11. package/dist/dusk.d.ts.map +1 -0
  12. package/dist/dusk.js +26 -0
  13. package/dist/dusk.js.map +1 -0
  14. package/dist/get.d.ts +340 -0
  15. package/dist/get.d.ts.map +1 -0
  16. package/dist/get.js +152 -0
  17. package/dist/get.js.map +1 -0
  18. package/dist/idl_v2.d.ts +3 -0
  19. package/dist/idl_v2.d.ts.map +1 -0
  20. package/dist/idl_v2.js +3 -0
  21. package/dist/idl_v2.js.map +1 -0
  22. package/dist/idl_v2.json +12630 -0
  23. package/dist/index.d.ts +14 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +15 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/indexer.d.ts +122 -0
  28. package/dist/indexer.d.ts.map +1 -0
  29. package/dist/indexer.js +203 -0
  30. package/dist/indexer.js.map +1 -0
  31. package/dist/preview.d.ts +39 -0
  32. package/dist/preview.d.ts.map +1 -0
  33. package/dist/preview.js +68 -0
  34. package/dist/preview.js.map +1 -0
  35. package/dist/program.d.ts +19 -0
  36. package/dist/program.d.ts.map +1 -0
  37. package/dist/program.js +29 -0
  38. package/dist/program.js.map +1 -0
  39. package/dist/referral.d.ts +51 -0
  40. package/dist/referral.d.ts.map +1 -0
  41. package/dist/referral.js +80 -0
  42. package/dist/referral.js.map +1 -0
  43. package/dist/type-aliases.d.ts +32 -0
  44. package/dist/type-aliases.d.ts.map +1 -0
  45. package/dist/type-aliases.js +2 -0
  46. package/dist/type-aliases.js.map +1 -0
  47. package/dist/types_v2.d.ts +12637 -0
  48. package/dist/types_v2.d.ts.map +1 -0
  49. package/dist/types_v2.js +2 -0
  50. package/dist/types_v2.js.map +1 -0
  51. package/dist/write.d.ts +61 -0
  52. package/dist/write.d.ts.map +1 -0
  53. package/dist/write.js +129 -0
  54. package/dist/write.js.map +1 -0
  55. package/package.json +76 -0
  56. package/src/address.ts +38 -0
  57. package/src/constants.ts +562 -0
  58. package/src/dusk.ts +43 -0
  59. package/src/get.ts +279 -0
  60. package/src/idl_v2.json +12630 -0
  61. package/src/idl_v2.ts +3 -0
  62. package/src/index.ts +18 -0
  63. package/src/indexer.ts +424 -0
  64. package/src/preview.ts +139 -0
  65. package/src/program.ts +54 -0
  66. package/src/referral.ts +172 -0
  67. package/src/type-aliases.ts +33 -0
  68. package/src/types_v2.ts +12636 -0
  69. package/src/write.ts +219 -0
@@ -0,0 +1,562 @@
1
+ import { AccountMeta, PublicKey } from "@solana/web3.js";
2
+
3
+ const DEFAULT_PROGRAM_ID = "358bjJKXWxeAXAzteX1xTgyd9JNnjtzW8fnwCS8Da1mv";
4
+ const MPL_TOKEN_METADATA_PROGRAM_ID = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s";
5
+
6
+ function getProgramIdFromEnv(fallback: string): string {
7
+ if (typeof process === "undefined" || !process.env) return fallback;
8
+ return process.env.DUSK_PROGRAM_ID ?? fallback;
9
+ }
10
+
11
+ /**
12
+ * Omnipair Dusk (v2) program ID.
13
+ * Reads from env DUSK_PROGRAM_ID.
14
+ */
15
+ export const DUSK_PROGRAM_ID = new PublicKey(getProgramIdFromEnv(DEFAULT_PROGRAM_ID));
16
+
17
+ export const PROGRAM_ID = DUSK_PROGRAM_ID;
18
+ export const TOKEN_METADATA_PROGRAM_ID = new PublicKey(MPL_TOKEN_METADATA_PROGRAM_ID);
19
+
20
+ /**
21
+ * PDA seeds used by the program
22
+ */
23
+ export const SEEDS = {
24
+ MARKET_V2: Buffer.from("market_v2"),
25
+ MARKET_RESERVE_VAULT: Buffer.from("market_reserve"),
26
+ MARKET_COLLATERAL_VAULT: Buffer.from("market_collateral"),
27
+ MARKET_FEE_VAULT: Buffer.from("market_fee"),
28
+ MARKET_INTEREST_VAULT: Buffer.from("market_interest"),
29
+ BORROW_POSITION: Buffer.from("borrow_position_v2"),
30
+ LEVERAGE_POSITION: Buffer.from("leverage_position_v2"),
31
+ YIELD_ACCOUNT: Buffer.from("yield"),
32
+ HLP_YLP_VAULT: Buffer.from("hlp_ylp_vault"),
33
+ INSURANCE: Buffer.from("insurance"),
34
+ FUTARCHY_AUTHORITY: Buffer.from("futarchy_authority"),
35
+ REFERRAL_PROFILE: Buffer.from("referral_profile"),
36
+ METADATA: Buffer.from("metadata"),
37
+ } as const;
38
+
39
+ function normalizeParamsHash(paramsHash: Uint8Array | Buffer | number[]): Buffer {
40
+ const hash = Buffer.from(paramsHash);
41
+ if (hash.length !== 32) {
42
+ throw new Error(`paramsHash must be 32 bytes, got ${hash.length}`);
43
+ }
44
+ return hash;
45
+ }
46
+
47
+ /**
48
+ * Derive Futarchy Authority PDA address
49
+ */
50
+ export function deriveFutarchyAuthorityAddress(): [PublicKey, number] {
51
+ return PublicKey.findProgramAddressSync([SEEDS.FUTARCHY_AUTHORITY], DUSK_PROGRAM_ID);
52
+ }
53
+
54
+ /** Derive the protocol-wide referral profile for a referrer authority. */
55
+ export function deriveReferralProfileAddress(authority: PublicKey): [PublicKey, number] {
56
+ return PublicKey.findProgramAddressSync(
57
+ [SEEDS.REFERRAL_PROFILE, authority.toBuffer()],
58
+ DUSK_PROGRAM_ID
59
+ );
60
+ }
61
+
62
+ /**
63
+ * Derive Dusk market PDA address
64
+ */
65
+ export function deriveMarketAddress(
66
+ baseMint: PublicKey,
67
+ quoteMint: PublicKey,
68
+ paramsHash: Uint8Array | Buffer | number[]
69
+ ): [PublicKey, number] {
70
+ return PublicKey.findProgramAddressSync(
71
+ [
72
+ SEEDS.MARKET_V2,
73
+ baseMint.toBuffer(),
74
+ quoteMint.toBuffer(),
75
+ normalizeParamsHash(paramsHash),
76
+ ],
77
+ DUSK_PROGRAM_ID
78
+ );
79
+ }
80
+
81
+ /**
82
+ * Derive a Metaplex Token Metadata PDA for a mint.
83
+ */
84
+ export function deriveTokenMetadataAddress(mint: PublicKey): [PublicKey, number] {
85
+ return PublicKey.findProgramAddressSync(
86
+ [SEEDS.METADATA, TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer()],
87
+ TOKEN_METADATA_PROGRAM_ID
88
+ );
89
+ }
90
+
91
+ /**
92
+ * Derive market reserve vault PDA address
93
+ */
94
+ export function deriveMarketReserveVaultAddress(
95
+ market: PublicKey,
96
+ reserveMint: PublicKey
97
+ ): [PublicKey, number] {
98
+ return PublicKey.findProgramAddressSync(
99
+ [SEEDS.MARKET_RESERVE_VAULT, market.toBuffer(), reserveMint.toBuffer()],
100
+ DUSK_PROGRAM_ID
101
+ );
102
+ }
103
+
104
+ /**
105
+ * Derive market collateral vault PDA address
106
+ */
107
+ export function deriveMarketCollateralVaultAddress(
108
+ market: PublicKey,
109
+ collateralMint: PublicKey
110
+ ): [PublicKey, number] {
111
+ return PublicKey.findProgramAddressSync(
112
+ [SEEDS.MARKET_COLLATERAL_VAULT, market.toBuffer(), collateralMint.toBuffer()],
113
+ DUSK_PROGRAM_ID
114
+ );
115
+ }
116
+
117
+ /**
118
+ * Derive market fee vault PDA address
119
+ */
120
+ export function deriveMarketFeeVaultAddress(
121
+ market: PublicKey,
122
+ feeMint: PublicKey
123
+ ): [PublicKey, number] {
124
+ return PublicKey.findProgramAddressSync(
125
+ [SEEDS.MARKET_FEE_VAULT, market.toBuffer(), feeMint.toBuffer()],
126
+ DUSK_PROGRAM_ID
127
+ );
128
+ }
129
+
130
+ /**
131
+ * Derive market interest vault PDA address
132
+ */
133
+ export function deriveMarketInterestVaultAddress(
134
+ market: PublicKey,
135
+ interestMint: PublicKey
136
+ ): [PublicKey, number] {
137
+ return PublicKey.findProgramAddressSync(
138
+ [SEEDS.MARKET_INTEREST_VAULT, market.toBuffer(), interestMint.toBuffer()],
139
+ DUSK_PROGRAM_ID
140
+ );
141
+ }
142
+
143
+ /**
144
+ * Derive borrow position PDA address
145
+ */
146
+ export function deriveBorrowPositionAddress(
147
+ market: PublicKey,
148
+ positionId: PublicKey
149
+ ): [PublicKey, number] {
150
+ return PublicKey.findProgramAddressSync(
151
+ [SEEDS.BORROW_POSITION, market.toBuffer(), positionId.toBuffer()],
152
+ DUSK_PROGRAM_ID
153
+ );
154
+ }
155
+
156
+ /**
157
+ * Derive leverage position PDA address
158
+ */
159
+ export function deriveLeveragePositionAddress(
160
+ market: PublicKey,
161
+ positionId: PublicKey
162
+ ): [PublicKey, number] {
163
+ return PublicKey.findProgramAddressSync(
164
+ [SEEDS.LEVERAGE_POSITION, market.toBuffer(), positionId.toBuffer()],
165
+ DUSK_PROGRAM_ID
166
+ );
167
+ }
168
+
169
+ export type YieldTokenKind = "ylp" | "hlp" | 0 | 1;
170
+
171
+ function yieldTokenKindCode(tokenKind: YieldTokenKind): number {
172
+ if (tokenKind === "ylp" || tokenKind === 0) return 0;
173
+ if (tokenKind === "hlp" || tokenKind === 1) return 1;
174
+ throw new Error(`Unsupported yield token kind: ${tokenKind}`);
175
+ }
176
+
177
+ /**
178
+ * Derive yield-account PDA address for yLP/hLP revenue checkpoints.
179
+ */
180
+ export function deriveYieldAccountAddress(
181
+ market: PublicKey,
182
+ owner: PublicKey,
183
+ assetMint: PublicKey,
184
+ tokenKind: YieldTokenKind
185
+ ): [PublicKey, number] {
186
+ return PublicKey.findProgramAddressSync(
187
+ [
188
+ SEEDS.YIELD_ACCOUNT,
189
+ market.toBuffer(),
190
+ owner.toBuffer(),
191
+ assetMint.toBuffer(),
192
+ Buffer.from([yieldTokenKindCode(tokenKind)]),
193
+ ],
194
+ DUSK_PROGRAM_ID
195
+ );
196
+ }
197
+
198
+ export interface YieldTransferHookAccountsArgs {
199
+ lpMint: PublicKey;
200
+ market: PublicKey;
201
+ sourceOwner: PublicKey;
202
+ destinationOwner: PublicKey;
203
+ assetMint: PublicKey;
204
+ tokenKind: YieldTokenKind;
205
+ }
206
+
207
+ export const TRANSFER_HOOK_EXECUTE_DISCRIMINATOR = Buffer.from([
208
+ 105, 37, 101, 197, 75, 251, 102, 26,
209
+ ]);
210
+
211
+ const TRANSFER_HOOK_EXTRA_ACCOUNT_META_SIZE = 35;
212
+ const TRANSFER_HOOK_TOKEN_ACCOUNT_OWNER_OFFSET = 32;
213
+ const TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX = 0;
214
+ const TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX = 2;
215
+ const TRANSFER_HOOK_MARKET_ACCOUNT_INDEX = 5;
216
+ const TRANSFER_HOOK_ASSET_MINT_ACCOUNT_INDEX = 6;
217
+ const TRANSFER_HOOK_QUOTE_ASSET_MINT_ACCOUNT_INDEX = 7;
218
+
219
+ type TransferHookSeed =
220
+ | { kind: "literal"; bytes: Uint8Array | Buffer | number[] }
221
+ | { kind: "instructionData"; index: number; length: number }
222
+ | { kind: "accountKey"; index: number }
223
+ | { kind: "accountData"; accountIndex: number; dataIndex: number; length: number };
224
+
225
+ interface TransferHookValidationMeta {
226
+ discriminator: number;
227
+ addressConfig: Buffer;
228
+ isSigner: boolean;
229
+ isWritable: boolean;
230
+ }
231
+
232
+ export interface YieldTransferHookValidationArgs {
233
+ market: PublicKey;
234
+ assetMint: PublicKey;
235
+ tokenKind: YieldTokenKind;
236
+ }
237
+
238
+ export interface YlpTransferHookValidationArgs {
239
+ market: PublicKey;
240
+ baseMint: PublicKey;
241
+ quoteMint: PublicKey;
242
+ }
243
+
244
+ /**
245
+ * Derive the standard Token-2022 transfer-hook validation PDA for a Dusk LP mint.
246
+ */
247
+ export function deriveYieldTransferHookValidationAddress(lpMint: PublicKey): [PublicKey, number] {
248
+ return PublicKey.findProgramAddressSync(
249
+ [Buffer.from("extra-account-metas"), lpMint.toBuffer()],
250
+ DUSK_PROGRAM_ID
251
+ );
252
+ }
253
+
254
+ function assertU8(value: number, label: string): void {
255
+ if (!Number.isInteger(value) || value < 0 || value > 255) {
256
+ throw new Error(`${label} must fit in a u8, got ${value}`);
257
+ }
258
+ }
259
+
260
+ function packTransferHookSeedConfig(seeds: TransferHookSeed[]): Buffer {
261
+ const config = Buffer.alloc(32);
262
+ let offset = 0;
263
+ const writeByte = (value: number, label: string) => {
264
+ assertU8(value, label);
265
+ if (offset >= config.length) {
266
+ throw new Error("transfer-hook seed config exceeds 32 bytes");
267
+ }
268
+ config[offset] = value;
269
+ offset += 1;
270
+ };
271
+
272
+ for (const seed of seeds) {
273
+ switch (seed.kind) {
274
+ case "literal": {
275
+ const bytes = Buffer.from(seed.bytes);
276
+ assertU8(bytes.length, "literal seed length");
277
+ writeByte(1, "literal seed discriminator");
278
+ writeByte(bytes.length, "literal seed length");
279
+ if (offset + bytes.length > config.length) {
280
+ throw new Error("transfer-hook seed config exceeds 32 bytes");
281
+ }
282
+ bytes.copy(config, offset);
283
+ offset += bytes.length;
284
+ break;
285
+ }
286
+ case "instructionData":
287
+ writeByte(2, "instruction-data seed discriminator");
288
+ writeByte(seed.index, "instruction-data seed index");
289
+ writeByte(seed.length, "instruction-data seed length");
290
+ break;
291
+ case "accountKey":
292
+ writeByte(3, "account-key seed discriminator");
293
+ writeByte(seed.index, "account-key seed index");
294
+ break;
295
+ case "accountData":
296
+ writeByte(4, "account-data seed discriminator");
297
+ writeByte(seed.accountIndex, "account-data seed account index");
298
+ writeByte(seed.dataIndex, "account-data seed data index");
299
+ writeByte(seed.length, "account-data seed length");
300
+ break;
301
+ }
302
+ }
303
+
304
+ return config;
305
+ }
306
+
307
+ function staticTransferHookMeta(meta: AccountMeta): TransferHookValidationMeta {
308
+ return {
309
+ discriminator: 0,
310
+ addressConfig: meta.pubkey.toBuffer(),
311
+ isSigner: meta.isSigner,
312
+ isWritable: meta.isWritable,
313
+ };
314
+ }
315
+
316
+ function pdaTransferHookMeta(
317
+ seeds: TransferHookSeed[],
318
+ isWritable: boolean
319
+ ): TransferHookValidationMeta {
320
+ return {
321
+ discriminator: 1,
322
+ addressConfig: packTransferHookSeedConfig(seeds),
323
+ isSigner: false,
324
+ isWritable,
325
+ };
326
+ }
327
+
328
+ function yieldAccountTransferHookSeeds(
329
+ ownerTokenAccountIndex: number,
330
+ tokenKind: YieldTokenKind,
331
+ assetMintAccountIndex = TRANSFER_HOOK_ASSET_MINT_ACCOUNT_INDEX
332
+ ): TransferHookSeed[] {
333
+ return [
334
+ { kind: "literal", bytes: SEEDS.YIELD_ACCOUNT },
335
+ { kind: "accountKey", index: TRANSFER_HOOK_MARKET_ACCOUNT_INDEX },
336
+ {
337
+ kind: "accountData",
338
+ accountIndex: ownerTokenAccountIndex,
339
+ dataIndex: TRANSFER_HOOK_TOKEN_ACCOUNT_OWNER_OFFSET,
340
+ length: 32,
341
+ },
342
+ { kind: "accountKey", index: assetMintAccountIndex },
343
+ { kind: "literal", bytes: [yieldTokenKindCode(tokenKind)] },
344
+ ];
345
+ }
346
+
347
+ function encodeTransferHookValidationAccountData(extraMetas: TransferHookValidationMeta[]): Buffer {
348
+ const podSliceLength = 4 + extraMetas.length * TRANSFER_HOOK_EXTRA_ACCOUNT_META_SIZE;
349
+ const data = Buffer.alloc(8 + 4 + podSliceLength);
350
+ TRANSFER_HOOK_EXECUTE_DISCRIMINATOR.copy(data, 0);
351
+ data.writeUInt32LE(podSliceLength, 8);
352
+ data.writeUInt32LE(extraMetas.length, 12);
353
+
354
+ let offset = 16;
355
+ for (const meta of extraMetas) {
356
+ data[offset] = meta.discriminator;
357
+ offset += 1;
358
+ meta.addressConfig.copy(data, offset);
359
+ offset += 32;
360
+ data[offset] = meta.isSigner ? 1 : 0;
361
+ offset += 1;
362
+ data[offset] = meta.isWritable ? 1 : 0;
363
+ offset += 1;
364
+ }
365
+ return data;
366
+ }
367
+
368
+ /**
369
+ * Encode a static Token-2022 transfer-hook validation account for Execute.
370
+ *
371
+ * The returned bytes use the SPL TLV shape expected by Token-2022:
372
+ * execute discriminator, pod-slice length, count, then static account metas.
373
+ */
374
+ export function buildYieldTransferHookValidationAccountData(extraMetas: AccountMeta[]): Buffer {
375
+ return encodeTransferHookValidationAccountData(extraMetas.map(staticTransferHookMeta));
376
+ }
377
+
378
+ /**
379
+ * Encode the production Token-2022 transfer-hook validation account for Dusk
380
+ * yLP/hLP mints.
381
+ *
382
+ * The validation state resolves static market/asset-mint accounts and derives
383
+ * source/destination YieldAccount PDAs from the source and destination token
384
+ * account owners observed by Token-2022 during Execute.
385
+ */
386
+ export function buildYieldTransferHookYieldValidationAccountData({
387
+ market,
388
+ assetMint,
389
+ tokenKind,
390
+ }: YieldTransferHookValidationArgs): Buffer {
391
+ return encodeTransferHookValidationAccountData([
392
+ staticTransferHookMeta({ pubkey: market, isSigner: false, isWritable: false }),
393
+ staticTransferHookMeta({ pubkey: assetMint, isSigner: false, isWritable: false }),
394
+ pdaTransferHookMeta(
395
+ yieldAccountTransferHookSeeds(TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX, tokenKind),
396
+ true
397
+ ),
398
+ pdaTransferHookMeta(
399
+ yieldAccountTransferHookSeeds(TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX, tokenKind),
400
+ true
401
+ ),
402
+ ]);
403
+ }
404
+
405
+ export function buildYlpTransferHookValidationAccountData({
406
+ market,
407
+ baseMint,
408
+ quoteMint,
409
+ }: YlpTransferHookValidationArgs): Buffer {
410
+ return encodeTransferHookValidationAccountData([
411
+ staticTransferHookMeta({ pubkey: market, isSigner: false, isWritable: false }),
412
+ staticTransferHookMeta({ pubkey: baseMint, isSigner: false, isWritable: false }),
413
+ staticTransferHookMeta({ pubkey: quoteMint, isSigner: false, isWritable: false }),
414
+ pdaTransferHookMeta(
415
+ yieldAccountTransferHookSeeds(TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX, "ylp"),
416
+ true
417
+ ),
418
+ pdaTransferHookMeta(
419
+ yieldAccountTransferHookSeeds(TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX, "ylp"),
420
+ true
421
+ ),
422
+ pdaTransferHookMeta(
423
+ yieldAccountTransferHookSeeds(
424
+ TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX,
425
+ "ylp",
426
+ TRANSFER_HOOK_QUOTE_ASSET_MINT_ACCOUNT_INDEX
427
+ ),
428
+ true
429
+ ),
430
+ pdaTransferHookMeta(
431
+ yieldAccountTransferHookSeeds(
432
+ TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX,
433
+ "ylp",
434
+ TRANSFER_HOOK_QUOTE_ASSET_MINT_ACCOUNT_INDEX
435
+ ),
436
+ true
437
+ ),
438
+ ]);
439
+ }
440
+
441
+ /**
442
+ * Build Dusk yLP/hLP transfer-hook extra account metas.
443
+ *
444
+ * Token-2022 passes the source token account, LP mint, destination token
445
+ * account, and transfer authority as the base hook accounts. Omnipair Dusk (v2) needs
446
+ * the market, underlying asset mint, canonical source/destination yield
447
+ * accounts, hook program, and standard validation PDA as extra metas so the
448
+ * hook can checkpoint revenue before the balance move is finalized.
449
+ */
450
+ export function buildYieldTransferHookAccountMetas({
451
+ lpMint,
452
+ market,
453
+ sourceOwner,
454
+ destinationOwner,
455
+ assetMint,
456
+ tokenKind,
457
+ }: YieldTransferHookAccountsArgs): AccountMeta[] {
458
+ const sourceYieldAccount = deriveYieldAccountAddress(
459
+ market,
460
+ sourceOwner,
461
+ assetMint,
462
+ tokenKind
463
+ )[0];
464
+ const destinationYieldAccount = deriveYieldAccountAddress(
465
+ market,
466
+ destinationOwner,
467
+ assetMint,
468
+ tokenKind
469
+ )[0];
470
+ const validationAccount = deriveYieldTransferHookValidationAddress(lpMint)[0];
471
+
472
+ return [
473
+ { pubkey: market, isSigner: false, isWritable: false },
474
+ { pubkey: assetMint, isSigner: false, isWritable: false },
475
+ { pubkey: sourceYieldAccount, isSigner: false, isWritable: true },
476
+ { pubkey: destinationYieldAccount, isSigner: false, isWritable: true },
477
+ { pubkey: DUSK_PROGRAM_ID, isSigner: false, isWritable: false },
478
+ { pubkey: validationAccount, isSigner: false, isWritable: false },
479
+ ];
480
+ }
481
+
482
+ export function buildYlpTransferHookAccountMetas({
483
+ lpMint,
484
+ market,
485
+ sourceOwner,
486
+ destinationOwner,
487
+ baseMint,
488
+ quoteMint,
489
+ }: Omit<YieldTransferHookAccountsArgs, "assetMint" | "tokenKind"> & {
490
+ baseMint: PublicKey;
491
+ quoteMint: PublicKey;
492
+ }): AccountMeta[] {
493
+ const sourceBaseYieldAccount = deriveYieldAccountAddress(
494
+ market,
495
+ sourceOwner,
496
+ baseMint,
497
+ "ylp"
498
+ )[0];
499
+ const destinationBaseYieldAccount = deriveYieldAccountAddress(
500
+ market,
501
+ destinationOwner,
502
+ baseMint,
503
+ "ylp"
504
+ )[0];
505
+ const sourceQuoteYieldAccount = deriveYieldAccountAddress(
506
+ market,
507
+ sourceOwner,
508
+ quoteMint,
509
+ "ylp"
510
+ )[0];
511
+ const destinationQuoteYieldAccount = deriveYieldAccountAddress(
512
+ market,
513
+ destinationOwner,
514
+ quoteMint,
515
+ "ylp"
516
+ )[0];
517
+ const validationAccount = deriveYieldTransferHookValidationAddress(lpMint)[0];
518
+
519
+ return [
520
+ { pubkey: market, isSigner: false, isWritable: false },
521
+ { pubkey: baseMint, isSigner: false, isWritable: false },
522
+ { pubkey: quoteMint, isSigner: false, isWritable: false },
523
+ { pubkey: sourceBaseYieldAccount, isSigner: false, isWritable: true },
524
+ { pubkey: destinationBaseYieldAccount, isSigner: false, isWritable: true },
525
+ { pubkey: sourceQuoteYieldAccount, isSigner: false, isWritable: true },
526
+ { pubkey: destinationQuoteYieldAccount, isSigner: false, isWritable: true },
527
+ { pubkey: DUSK_PROGRAM_ID, isSigner: false, isWritable: false },
528
+ { pubkey: validationAccount, isSigner: false, isWritable: false },
529
+ ];
530
+ }
531
+
532
+ /**
533
+ * Derive canonical aggregate hLP vault yLP token-account PDA.
534
+ */
535
+ export function deriveHlpYlpVaultAddress(
536
+ market: PublicKey,
537
+ targetHlpMint: PublicKey,
538
+ ylpMint: PublicKey
539
+ ): [PublicKey, number] {
540
+ return PublicKey.findProgramAddressSync(
541
+ [
542
+ SEEDS.HLP_YLP_VAULT,
543
+ market.toBuffer(),
544
+ targetHlpMint.toBuffer(),
545
+ ylpMint.toBuffer(),
546
+ ],
547
+ DUSK_PROGRAM_ID
548
+ );
549
+ }
550
+
551
+ /**
552
+ * Derive insurance PDA address
553
+ */
554
+ export function deriveInsuranceAddress(
555
+ market: PublicKey,
556
+ assetMint: PublicKey
557
+ ): [PublicKey, number] {
558
+ return PublicKey.findProgramAddressSync(
559
+ [SEEDS.INSURANCE, market.toBuffer(), assetMint.toBuffer()],
560
+ DUSK_PROGRAM_ID
561
+ );
562
+ }
package/src/dusk.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { AnchorProvider, Program } from "@coral-xyz/anchor";
2
+ import type { Connection } from "@solana/web3.js";
3
+
4
+ import { type AddressLike } from "./address.js";
5
+ import { DuskGet } from "./get.js";
6
+ import { DuskIndexerClient, type FetchLike } from "./indexer.js";
7
+ import { createDuskProgram } from "./program.js";
8
+ import type { Dusk as DuskIdl } from "./types_v2.js";
9
+ import { DuskWrite } from "./write.js";
10
+
11
+ export interface DuskOptions {
12
+ program?: Program<DuskIdl>;
13
+ provider?: AnchorProvider;
14
+ connection?: Connection;
15
+ programId?: AddressLike;
16
+ feePayer?: AddressLike;
17
+ indexerBaseUrl?: string;
18
+ fetch?: FetchLike;
19
+ }
20
+
21
+ export class Dusk {
22
+ readonly program: Program<DuskIdl>;
23
+ readonly get: DuskGet;
24
+ readonly write: DuskWrite;
25
+ readonly fetch: DuskIndexerClient;
26
+
27
+ constructor(options: DuskOptions = {}) {
28
+ this.program =
29
+ options.program ??
30
+ createDuskProgram({
31
+ provider: options.provider,
32
+ connection: options.connection,
33
+ programId: options.programId,
34
+ });
35
+
36
+ this.get = new DuskGet(this.program, options.feePayer);
37
+ this.write = new DuskWrite(this.program);
38
+ this.fetch = new DuskIndexerClient({
39
+ baseUrl: options.indexerBaseUrl,
40
+ fetch: options.fetch,
41
+ });
42
+ }
43
+ }