@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
package/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # @omnipair/dusk-sdk
2
+
3
+ TypeScript SDK for Omnipair Dusk (v2).
4
+
5
+ A `Dusk` instance is an enriched Anchor program facade. It exposes the raw
6
+ Anchor program through `dusk.program`, alongside typed on-chain reads and
7
+ previews through `dusk.get`, transaction builders through `dusk.write`, and
8
+ indexed historical data through `dusk.fetch`.
9
+
10
+ The package exports the generated Anchor IDL/types, PDA helpers, typed preview
11
+ decoders, a small write/read facade over the Dusk program, and an indexer client
12
+ for historical API data.
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ npm install @omnipair/dusk-sdk
18
+ # or
19
+ yarn add @omnipair/dusk-sdk
20
+ ```
21
+
22
+ ## Dusk Client
23
+
24
+ ```typescript
25
+ import { AnchorProvider } from "@coral-xyz/anchor";
26
+ import { Connection } from "@solana/web3.js";
27
+ import { Dusk } from "@omnipair/dusk-sdk";
28
+
29
+ const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");
30
+ const provider = new AnchorProvider(connection, wallet, { commitment: "confirmed" });
31
+
32
+ const dusk = new Dusk({
33
+ provider,
34
+ indexerBaseUrl: "https://api.indexer.omnipair.fi/api/v1",
35
+ });
36
+ ```
37
+
38
+ The client is intentionally split by source of truth:
39
+
40
+ - `dusk.write`: Anchor instruction, transaction, and RPC builders.
41
+ - `dusk.get`: PDA helpers, direct RPC account fetches, and typed simulation previews.
42
+ - `dusk.fetch`: historical/indexed HTTP API methods.
43
+
44
+ ## Write Instructions
45
+
46
+ ```typescript
47
+ const ix = await dusk.write.instruction(
48
+ "swap",
49
+ {
50
+ exactAssetIn: amountIn,
51
+ minAssetOut: minAmountOut,
52
+ },
53
+ {
54
+ accounts: {
55
+ market,
56
+ futarchyAuthority,
57
+ trader,
58
+ assetInMint,
59
+ assetOutMint,
60
+ reserveInVault,
61
+ reserveOutVault,
62
+ feeInVault,
63
+ traderAssetInAccount,
64
+ traderAssetOutAccount,
65
+ tokenProgram,
66
+ token2022Program,
67
+ eventAuthority,
68
+ program: dusk.program.programId,
69
+ },
70
+ }
71
+ );
72
+ ```
73
+
74
+ `write.builder(...)`, `write.transaction(...)`, and `write.rpc(...)` expose the
75
+ same generic path for every Dusk instruction in the IDL.
76
+
77
+ ## Get On-Chain State
78
+
79
+ ```typescript
80
+ const [market] = dusk.get.pda.market(baseMint, quoteMint, paramsHash);
81
+ const account = await dusk.get.market(market);
82
+
83
+ const swap = await dusk.get.previewSwap({
84
+ market,
85
+ assetInMint: baseMint,
86
+ assetOutMint: quoteMint,
87
+ exactAssetIn: amountIn,
88
+ });
89
+ ```
90
+
91
+ Preview methods use Solana `simulateTransaction` and decode typed Anchor return
92
+ data. They replace the old log-parsing getter workaround.
93
+
94
+ Available typed previews:
95
+
96
+ - `previewMarket(market)`.
97
+ - `previewSwap({ market, assetInMint, assetOutMint, exactAssetIn })`.
98
+ - `previewBorrowCapacity({ market, collateralAssetMint, debtAssetMint, collateralAmount, projectedBorrowAmount, withReferral })`.
99
+ - `previewBorrowPosition({ market, borrowPosition })`.
100
+
101
+ `previewBorrowCapacity` exposes both the health-limited result of the on-chain
102
+ binary search and the final limit after cash and daily-borrow constraints:
103
+
104
+ ```typescript
105
+ const capacity = await dusk.get.previewBorrowCapacity({
106
+ market,
107
+ collateralAssetMint: baseMint,
108
+ debtAssetMint: quoteMint,
109
+ collateralAmount,
110
+ // Optional: quote the fee, CF, and health terms for this requested principal.
111
+ projectedBorrowAmount,
112
+ withReferral: true,
113
+ maxAcceptableReferralFeeBps: 10,
114
+ });
115
+
116
+ capacity.maxDebtByHealth;
117
+ capacity.maxDebtByCash;
118
+ capacity.maxDebtByDailyLimit;
119
+ capacity.maxDebt;
120
+ capacity.maxBorrowAmount;
121
+ capacity.referralOriginationFeeBps;
122
+ capacity.projectedReferralFeeDebit;
123
+ capacity.projectedReferralVaultCredit;
124
+ capacity.maxCfBps;
125
+ capacity.liquidationCfBps;
126
+ capacity.projectedGlobalHealthContribution;
127
+ capacity.projectedGlobalMarketHealthBps;
128
+ capacity.projectedEffectiveExistingDebtNad;
129
+ ```
130
+
131
+ ## Fetch Historical Data
132
+
133
+ ```typescript
134
+ const pools = await dusk.fetch.pools({ limit: 50, sortBy: "tvl", sortOrder: "desc" });
135
+ const activity = await dusk.fetch.poolActivity(market, {
136
+ categories: ["swaps", "liquidity", "lending"],
137
+ limit: 100,
138
+ });
139
+ const snapshots = await dusk.fetch.userPortfolioSnapshots(owner, "30D");
140
+ ```
141
+
142
+ The indexer client wraps the Omnipair `/api/v1` routes for pools, stats, users,
143
+ positions, GeckoTerminal, CoinGecko, and CMC-compatible data. Use
144
+ `dusk.fetch.request(path, options)` for new or unwrapped endpoints.
145
+
146
+ ## Raw Program Exports
147
+
148
+ ```typescript
149
+ import {
150
+ createDuskProgram,
151
+ deriveMarketAddress,
152
+ IDL,
153
+ PROGRAM_ID,
154
+ type DuskIdl,
155
+ } from "@omnipair/dusk-sdk";
156
+
157
+ const program = createDuskProgram({ provider });
158
+ ```
159
+
160
+ `DUSK_PROGRAM_ID` is exported for integrations that prefer an explicit program
161
+ name over the generic `PROGRAM_ID` constant.
162
+
163
+ ## ESM Compatibility
164
+
165
+ This package ships strict ESM-compatible output. Relative module specifiers
166
+ include `.js` extensions in emitted files.
167
+
168
+ ## License
169
+
170
+ MIT
@@ -0,0 +1,7 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export type AddressLike = PublicKey | string;
3
+ export declare const DEFAULT_READONLY_PUBLIC_KEY: PublicKey;
4
+ export declare function address(value: AddressLike): PublicKey;
5
+ export declare function addressString(value: AddressLike): string;
6
+ export declare function normalizeAccountKeys<T>(value: T): T;
7
+ //# sourceMappingURL=address.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,MAAM,CAAC;AAE7C,eAAO,MAAM,2BAA2B,WAEvC,CAAC;AAEF,wBAAgB,OAAO,CAAC,KAAK,EAAE,WAAW,GAAG,SAAS,CAErD;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAExD;AAED,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAqBnD"}
@@ -0,0 +1,29 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export const DEFAULT_READONLY_PUBLIC_KEY = new PublicKey("8tF4uYMBXqGhCUGRZL3AmPqRzbX8JJ1TpYnY3uJKN4kt");
3
+ export function address(value) {
4
+ return value instanceof PublicKey ? value : new PublicKey(value);
5
+ }
6
+ export function addressString(value) {
7
+ return address(value).toBase58();
8
+ }
9
+ export function normalizeAccountKeys(value) {
10
+ if (typeof value === "string") {
11
+ try {
12
+ return new PublicKey(value);
13
+ }
14
+ catch {
15
+ return value;
16
+ }
17
+ }
18
+ if (value instanceof PublicKey || value === null || typeof value !== "object") {
19
+ return value;
20
+ }
21
+ if (Array.isArray(value)) {
22
+ return value.map((child) => normalizeAccountKeys(child));
23
+ }
24
+ return Object.fromEntries(Object.entries(value).map(([key, child]) => [
25
+ key,
26
+ normalizeAccountKeys(child),
27
+ ]));
28
+ }
29
+ //# sourceMappingURL=address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.js","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,CAAC,MAAM,2BAA2B,GAAG,IAAI,SAAS,CACtD,8CAA8C,CAC/C,CAAC;AAEF,MAAM,UAAU,OAAO,CAAC,KAAkB;IACxC,OAAO,KAAK,YAAY,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,CAAC;AACnE,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAkB;IAC9C,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAI,KAAQ;IAC9C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,OAAO,IAAI,SAAS,CAAC,KAAK,CAAM,CAAC;QACnC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IACD,IAAI,KAAK,YAAY,SAAS,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9E,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAM,CAAC;IAChE,CAAC;IAED,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;QACrE,GAAG;QACH,oBAAoB,CAAC,KAAK,CAAC;KAC5B,CAAC,CACE,CAAC;AACT,CAAC"}
@@ -0,0 +1,132 @@
1
+ import { AccountMeta, PublicKey } from "@solana/web3.js";
2
+ /**
3
+ * Omnipair Dusk (v2) program ID.
4
+ * Reads from env DUSK_PROGRAM_ID.
5
+ */
6
+ export declare const DUSK_PROGRAM_ID: PublicKey;
7
+ export declare const PROGRAM_ID: PublicKey;
8
+ export declare const TOKEN_METADATA_PROGRAM_ID: PublicKey;
9
+ /**
10
+ * PDA seeds used by the program
11
+ */
12
+ export declare const SEEDS: {
13
+ readonly MARKET_V2: Buffer<ArrayBuffer>;
14
+ readonly MARKET_RESERVE_VAULT: Buffer<ArrayBuffer>;
15
+ readonly MARKET_COLLATERAL_VAULT: Buffer<ArrayBuffer>;
16
+ readonly MARKET_FEE_VAULT: Buffer<ArrayBuffer>;
17
+ readonly MARKET_INTEREST_VAULT: Buffer<ArrayBuffer>;
18
+ readonly BORROW_POSITION: Buffer<ArrayBuffer>;
19
+ readonly LEVERAGE_POSITION: Buffer<ArrayBuffer>;
20
+ readonly YIELD_ACCOUNT: Buffer<ArrayBuffer>;
21
+ readonly HLP_YLP_VAULT: Buffer<ArrayBuffer>;
22
+ readonly INSURANCE: Buffer<ArrayBuffer>;
23
+ readonly FUTARCHY_AUTHORITY: Buffer<ArrayBuffer>;
24
+ readonly REFERRAL_PROFILE: Buffer<ArrayBuffer>;
25
+ readonly METADATA: Buffer<ArrayBuffer>;
26
+ };
27
+ /**
28
+ * Derive Futarchy Authority PDA address
29
+ */
30
+ export declare function deriveFutarchyAuthorityAddress(): [PublicKey, number];
31
+ /** Derive the protocol-wide referral profile for a referrer authority. */
32
+ export declare function deriveReferralProfileAddress(authority: PublicKey): [PublicKey, number];
33
+ /**
34
+ * Derive Dusk market PDA address
35
+ */
36
+ export declare function deriveMarketAddress(baseMint: PublicKey, quoteMint: PublicKey, paramsHash: Uint8Array | Buffer | number[]): [PublicKey, number];
37
+ /**
38
+ * Derive a Metaplex Token Metadata PDA for a mint.
39
+ */
40
+ export declare function deriveTokenMetadataAddress(mint: PublicKey): [PublicKey, number];
41
+ /**
42
+ * Derive market reserve vault PDA address
43
+ */
44
+ export declare function deriveMarketReserveVaultAddress(market: PublicKey, reserveMint: PublicKey): [PublicKey, number];
45
+ /**
46
+ * Derive market collateral vault PDA address
47
+ */
48
+ export declare function deriveMarketCollateralVaultAddress(market: PublicKey, collateralMint: PublicKey): [PublicKey, number];
49
+ /**
50
+ * Derive market fee vault PDA address
51
+ */
52
+ export declare function deriveMarketFeeVaultAddress(market: PublicKey, feeMint: PublicKey): [PublicKey, number];
53
+ /**
54
+ * Derive market interest vault PDA address
55
+ */
56
+ export declare function deriveMarketInterestVaultAddress(market: PublicKey, interestMint: PublicKey): [PublicKey, number];
57
+ /**
58
+ * Derive borrow position PDA address
59
+ */
60
+ export declare function deriveBorrowPositionAddress(market: PublicKey, positionId: PublicKey): [PublicKey, number];
61
+ /**
62
+ * Derive leverage position PDA address
63
+ */
64
+ export declare function deriveLeveragePositionAddress(market: PublicKey, positionId: PublicKey): [PublicKey, number];
65
+ export type YieldTokenKind = "ylp" | "hlp" | 0 | 1;
66
+ /**
67
+ * Derive yield-account PDA address for yLP/hLP revenue checkpoints.
68
+ */
69
+ export declare function deriveYieldAccountAddress(market: PublicKey, owner: PublicKey, assetMint: PublicKey, tokenKind: YieldTokenKind): [PublicKey, number];
70
+ export interface YieldTransferHookAccountsArgs {
71
+ lpMint: PublicKey;
72
+ market: PublicKey;
73
+ sourceOwner: PublicKey;
74
+ destinationOwner: PublicKey;
75
+ assetMint: PublicKey;
76
+ tokenKind: YieldTokenKind;
77
+ }
78
+ export declare const TRANSFER_HOOK_EXECUTE_DISCRIMINATOR: Buffer<ArrayBuffer>;
79
+ export interface YieldTransferHookValidationArgs {
80
+ market: PublicKey;
81
+ assetMint: PublicKey;
82
+ tokenKind: YieldTokenKind;
83
+ }
84
+ export interface YlpTransferHookValidationArgs {
85
+ market: PublicKey;
86
+ baseMint: PublicKey;
87
+ quoteMint: PublicKey;
88
+ }
89
+ /**
90
+ * Derive the standard Token-2022 transfer-hook validation PDA for a Dusk LP mint.
91
+ */
92
+ export declare function deriveYieldTransferHookValidationAddress(lpMint: PublicKey): [PublicKey, number];
93
+ /**
94
+ * Encode a static Token-2022 transfer-hook validation account for Execute.
95
+ *
96
+ * The returned bytes use the SPL TLV shape expected by Token-2022:
97
+ * execute discriminator, pod-slice length, count, then static account metas.
98
+ */
99
+ export declare function buildYieldTransferHookValidationAccountData(extraMetas: AccountMeta[]): Buffer;
100
+ /**
101
+ * Encode the production Token-2022 transfer-hook validation account for Dusk
102
+ * yLP/hLP mints.
103
+ *
104
+ * The validation state resolves static market/asset-mint accounts and derives
105
+ * source/destination YieldAccount PDAs from the source and destination token
106
+ * account owners observed by Token-2022 during Execute.
107
+ */
108
+ export declare function buildYieldTransferHookYieldValidationAccountData({ market, assetMint, tokenKind, }: YieldTransferHookValidationArgs): Buffer;
109
+ export declare function buildYlpTransferHookValidationAccountData({ market, baseMint, quoteMint, }: YlpTransferHookValidationArgs): Buffer;
110
+ /**
111
+ * Build Dusk yLP/hLP transfer-hook extra account metas.
112
+ *
113
+ * Token-2022 passes the source token account, LP mint, destination token
114
+ * account, and transfer authority as the base hook accounts. Omnipair Dusk (v2) needs
115
+ * the market, underlying asset mint, canonical source/destination yield
116
+ * accounts, hook program, and standard validation PDA as extra metas so the
117
+ * hook can checkpoint revenue before the balance move is finalized.
118
+ */
119
+ export declare function buildYieldTransferHookAccountMetas({ lpMint, market, sourceOwner, destinationOwner, assetMint, tokenKind, }: YieldTransferHookAccountsArgs): AccountMeta[];
120
+ export declare function buildYlpTransferHookAccountMetas({ lpMint, market, sourceOwner, destinationOwner, baseMint, quoteMint, }: Omit<YieldTransferHookAccountsArgs, "assetMint" | "tokenKind"> & {
121
+ baseMint: PublicKey;
122
+ quoteMint: PublicKey;
123
+ }): AccountMeta[];
124
+ /**
125
+ * Derive canonical aggregate hLP vault yLP token-account PDA.
126
+ */
127
+ export declare function deriveHlpYlpVaultAddress(market: PublicKey, targetHlpMint: PublicKey, ylpMint: PublicKey): [PublicKey, number];
128
+ /**
129
+ * Derive insurance PDA address
130
+ */
131
+ export declare function deriveInsuranceAddress(market: PublicKey, assetMint: PublicKey): [PublicKey, number];
132
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAUzD;;;GAGG;AACH,eAAO,MAAM,eAAe,WAAyD,CAAC;AAEtF,eAAO,MAAM,UAAU,WAAkB,CAAC;AAC1C,eAAO,MAAM,yBAAyB,WAA+C,CAAC;AAEtF;;GAEG;AACH,eAAO,MAAM,KAAK;;;;;;;;;;;;;;CAcR,CAAC;AAUX;;GAEG;AACH,wBAAgB,8BAA8B,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,CAEpE;AAED,0EAA0E;AAC1E,wBAAgB,4BAA4B,CAAC,SAAS,EAAE,SAAS,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAKtF;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,SAAS,EACnB,SAAS,EAAE,SAAS,EACpB,UAAU,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,EAAE,GACzC,CAAC,SAAS,EAAE,MAAM,CAAC,CAUrB;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,SAAS,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAK/E;AAED;;GAEG;AACH,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,SAAS,EACjB,WAAW,EAAE,SAAS,GACrB,CA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EACjB,SAAS,EAAE,SAAS,GACnB,CAAC,SAAS,EAAE,MAAM,CAAC,CAKrB"}
@@ -0,0 +1,330 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ const DEFAULT_PROGRAM_ID = "358bjJKXWxeAXAzteX1xTgyd9JNnjtzW8fnwCS8Da1mv";
3
+ const MPL_TOKEN_METADATA_PROGRAM_ID = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s";
4
+ function getProgramIdFromEnv(fallback) {
5
+ if (typeof process === "undefined" || !process.env)
6
+ return fallback;
7
+ return process.env.DUSK_PROGRAM_ID ?? fallback;
8
+ }
9
+ /**
10
+ * Omnipair Dusk (v2) program ID.
11
+ * Reads from env DUSK_PROGRAM_ID.
12
+ */
13
+ export const DUSK_PROGRAM_ID = new PublicKey(getProgramIdFromEnv(DEFAULT_PROGRAM_ID));
14
+ export const PROGRAM_ID = DUSK_PROGRAM_ID;
15
+ export const TOKEN_METADATA_PROGRAM_ID = new PublicKey(MPL_TOKEN_METADATA_PROGRAM_ID);
16
+ /**
17
+ * PDA seeds used by the program
18
+ */
19
+ export const SEEDS = {
20
+ MARKET_V2: Buffer.from("market_v2"),
21
+ MARKET_RESERVE_VAULT: Buffer.from("market_reserve"),
22
+ MARKET_COLLATERAL_VAULT: Buffer.from("market_collateral"),
23
+ MARKET_FEE_VAULT: Buffer.from("market_fee"),
24
+ MARKET_INTEREST_VAULT: Buffer.from("market_interest"),
25
+ BORROW_POSITION: Buffer.from("borrow_position_v2"),
26
+ LEVERAGE_POSITION: Buffer.from("leverage_position_v2"),
27
+ YIELD_ACCOUNT: Buffer.from("yield"),
28
+ HLP_YLP_VAULT: Buffer.from("hlp_ylp_vault"),
29
+ INSURANCE: Buffer.from("insurance"),
30
+ FUTARCHY_AUTHORITY: Buffer.from("futarchy_authority"),
31
+ REFERRAL_PROFILE: Buffer.from("referral_profile"),
32
+ METADATA: Buffer.from("metadata"),
33
+ };
34
+ function normalizeParamsHash(paramsHash) {
35
+ const hash = Buffer.from(paramsHash);
36
+ if (hash.length !== 32) {
37
+ throw new Error(`paramsHash must be 32 bytes, got ${hash.length}`);
38
+ }
39
+ return hash;
40
+ }
41
+ /**
42
+ * Derive Futarchy Authority PDA address
43
+ */
44
+ export function deriveFutarchyAuthorityAddress() {
45
+ return PublicKey.findProgramAddressSync([SEEDS.FUTARCHY_AUTHORITY], DUSK_PROGRAM_ID);
46
+ }
47
+ /** Derive the protocol-wide referral profile for a referrer authority. */
48
+ export function deriveReferralProfileAddress(authority) {
49
+ return PublicKey.findProgramAddressSync([SEEDS.REFERRAL_PROFILE, authority.toBuffer()], DUSK_PROGRAM_ID);
50
+ }
51
+ /**
52
+ * Derive Dusk market PDA address
53
+ */
54
+ export function deriveMarketAddress(baseMint, quoteMint, paramsHash) {
55
+ return PublicKey.findProgramAddressSync([
56
+ SEEDS.MARKET_V2,
57
+ baseMint.toBuffer(),
58
+ quoteMint.toBuffer(),
59
+ normalizeParamsHash(paramsHash),
60
+ ], DUSK_PROGRAM_ID);
61
+ }
62
+ /**
63
+ * Derive a Metaplex Token Metadata PDA for a mint.
64
+ */
65
+ export function deriveTokenMetadataAddress(mint) {
66
+ return PublicKey.findProgramAddressSync([SEEDS.METADATA, TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer()], TOKEN_METADATA_PROGRAM_ID);
67
+ }
68
+ /**
69
+ * Derive market reserve vault PDA address
70
+ */
71
+ export function deriveMarketReserveVaultAddress(market, reserveMint) {
72
+ return PublicKey.findProgramAddressSync([SEEDS.MARKET_RESERVE_VAULT, market.toBuffer(), reserveMint.toBuffer()], DUSK_PROGRAM_ID);
73
+ }
74
+ /**
75
+ * Derive market collateral vault PDA address
76
+ */
77
+ export function deriveMarketCollateralVaultAddress(market, collateralMint) {
78
+ return PublicKey.findProgramAddressSync([SEEDS.MARKET_COLLATERAL_VAULT, market.toBuffer(), collateralMint.toBuffer()], DUSK_PROGRAM_ID);
79
+ }
80
+ /**
81
+ * Derive market fee vault PDA address
82
+ */
83
+ export function deriveMarketFeeVaultAddress(market, feeMint) {
84
+ return PublicKey.findProgramAddressSync([SEEDS.MARKET_FEE_VAULT, market.toBuffer(), feeMint.toBuffer()], DUSK_PROGRAM_ID);
85
+ }
86
+ /**
87
+ * Derive market interest vault PDA address
88
+ */
89
+ export function deriveMarketInterestVaultAddress(market, interestMint) {
90
+ return PublicKey.findProgramAddressSync([SEEDS.MARKET_INTEREST_VAULT, market.toBuffer(), interestMint.toBuffer()], DUSK_PROGRAM_ID);
91
+ }
92
+ /**
93
+ * Derive borrow position PDA address
94
+ */
95
+ export function deriveBorrowPositionAddress(market, positionId) {
96
+ return PublicKey.findProgramAddressSync([SEEDS.BORROW_POSITION, market.toBuffer(), positionId.toBuffer()], DUSK_PROGRAM_ID);
97
+ }
98
+ /**
99
+ * Derive leverage position PDA address
100
+ */
101
+ export function deriveLeveragePositionAddress(market, positionId) {
102
+ return PublicKey.findProgramAddressSync([SEEDS.LEVERAGE_POSITION, market.toBuffer(), positionId.toBuffer()], DUSK_PROGRAM_ID);
103
+ }
104
+ function yieldTokenKindCode(tokenKind) {
105
+ if (tokenKind === "ylp" || tokenKind === 0)
106
+ return 0;
107
+ if (tokenKind === "hlp" || tokenKind === 1)
108
+ return 1;
109
+ throw new Error(`Unsupported yield token kind: ${tokenKind}`);
110
+ }
111
+ /**
112
+ * Derive yield-account PDA address for yLP/hLP revenue checkpoints.
113
+ */
114
+ export function deriveYieldAccountAddress(market, owner, assetMint, tokenKind) {
115
+ return PublicKey.findProgramAddressSync([
116
+ SEEDS.YIELD_ACCOUNT,
117
+ market.toBuffer(),
118
+ owner.toBuffer(),
119
+ assetMint.toBuffer(),
120
+ Buffer.from([yieldTokenKindCode(tokenKind)]),
121
+ ], DUSK_PROGRAM_ID);
122
+ }
123
+ export const TRANSFER_HOOK_EXECUTE_DISCRIMINATOR = Buffer.from([
124
+ 105, 37, 101, 197, 75, 251, 102, 26,
125
+ ]);
126
+ const TRANSFER_HOOK_EXTRA_ACCOUNT_META_SIZE = 35;
127
+ const TRANSFER_HOOK_TOKEN_ACCOUNT_OWNER_OFFSET = 32;
128
+ const TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX = 0;
129
+ const TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX = 2;
130
+ const TRANSFER_HOOK_MARKET_ACCOUNT_INDEX = 5;
131
+ const TRANSFER_HOOK_ASSET_MINT_ACCOUNT_INDEX = 6;
132
+ const TRANSFER_HOOK_QUOTE_ASSET_MINT_ACCOUNT_INDEX = 7;
133
+ /**
134
+ * Derive the standard Token-2022 transfer-hook validation PDA for a Dusk LP mint.
135
+ */
136
+ export function deriveYieldTransferHookValidationAddress(lpMint) {
137
+ return PublicKey.findProgramAddressSync([Buffer.from("extra-account-metas"), lpMint.toBuffer()], DUSK_PROGRAM_ID);
138
+ }
139
+ function assertU8(value, label) {
140
+ if (!Number.isInteger(value) || value < 0 || value > 255) {
141
+ throw new Error(`${label} must fit in a u8, got ${value}`);
142
+ }
143
+ }
144
+ function packTransferHookSeedConfig(seeds) {
145
+ const config = Buffer.alloc(32);
146
+ let offset = 0;
147
+ const writeByte = (value, label) => {
148
+ assertU8(value, label);
149
+ if (offset >= config.length) {
150
+ throw new Error("transfer-hook seed config exceeds 32 bytes");
151
+ }
152
+ config[offset] = value;
153
+ offset += 1;
154
+ };
155
+ for (const seed of seeds) {
156
+ switch (seed.kind) {
157
+ case "literal": {
158
+ const bytes = Buffer.from(seed.bytes);
159
+ assertU8(bytes.length, "literal seed length");
160
+ writeByte(1, "literal seed discriminator");
161
+ writeByte(bytes.length, "literal seed length");
162
+ if (offset + bytes.length > config.length) {
163
+ throw new Error("transfer-hook seed config exceeds 32 bytes");
164
+ }
165
+ bytes.copy(config, offset);
166
+ offset += bytes.length;
167
+ break;
168
+ }
169
+ case "instructionData":
170
+ writeByte(2, "instruction-data seed discriminator");
171
+ writeByte(seed.index, "instruction-data seed index");
172
+ writeByte(seed.length, "instruction-data seed length");
173
+ break;
174
+ case "accountKey":
175
+ writeByte(3, "account-key seed discriminator");
176
+ writeByte(seed.index, "account-key seed index");
177
+ break;
178
+ case "accountData":
179
+ writeByte(4, "account-data seed discriminator");
180
+ writeByte(seed.accountIndex, "account-data seed account index");
181
+ writeByte(seed.dataIndex, "account-data seed data index");
182
+ writeByte(seed.length, "account-data seed length");
183
+ break;
184
+ }
185
+ }
186
+ return config;
187
+ }
188
+ function staticTransferHookMeta(meta) {
189
+ return {
190
+ discriminator: 0,
191
+ addressConfig: meta.pubkey.toBuffer(),
192
+ isSigner: meta.isSigner,
193
+ isWritable: meta.isWritable,
194
+ };
195
+ }
196
+ function pdaTransferHookMeta(seeds, isWritable) {
197
+ return {
198
+ discriminator: 1,
199
+ addressConfig: packTransferHookSeedConfig(seeds),
200
+ isSigner: false,
201
+ isWritable,
202
+ };
203
+ }
204
+ function yieldAccountTransferHookSeeds(ownerTokenAccountIndex, tokenKind, assetMintAccountIndex = TRANSFER_HOOK_ASSET_MINT_ACCOUNT_INDEX) {
205
+ return [
206
+ { kind: "literal", bytes: SEEDS.YIELD_ACCOUNT },
207
+ { kind: "accountKey", index: TRANSFER_HOOK_MARKET_ACCOUNT_INDEX },
208
+ {
209
+ kind: "accountData",
210
+ accountIndex: ownerTokenAccountIndex,
211
+ dataIndex: TRANSFER_HOOK_TOKEN_ACCOUNT_OWNER_OFFSET,
212
+ length: 32,
213
+ },
214
+ { kind: "accountKey", index: assetMintAccountIndex },
215
+ { kind: "literal", bytes: [yieldTokenKindCode(tokenKind)] },
216
+ ];
217
+ }
218
+ function encodeTransferHookValidationAccountData(extraMetas) {
219
+ const podSliceLength = 4 + extraMetas.length * TRANSFER_HOOK_EXTRA_ACCOUNT_META_SIZE;
220
+ const data = Buffer.alloc(8 + 4 + podSliceLength);
221
+ TRANSFER_HOOK_EXECUTE_DISCRIMINATOR.copy(data, 0);
222
+ data.writeUInt32LE(podSliceLength, 8);
223
+ data.writeUInt32LE(extraMetas.length, 12);
224
+ let offset = 16;
225
+ for (const meta of extraMetas) {
226
+ data[offset] = meta.discriminator;
227
+ offset += 1;
228
+ meta.addressConfig.copy(data, offset);
229
+ offset += 32;
230
+ data[offset] = meta.isSigner ? 1 : 0;
231
+ offset += 1;
232
+ data[offset] = meta.isWritable ? 1 : 0;
233
+ offset += 1;
234
+ }
235
+ return data;
236
+ }
237
+ /**
238
+ * Encode a static Token-2022 transfer-hook validation account for Execute.
239
+ *
240
+ * The returned bytes use the SPL TLV shape expected by Token-2022:
241
+ * execute discriminator, pod-slice length, count, then static account metas.
242
+ */
243
+ export function buildYieldTransferHookValidationAccountData(extraMetas) {
244
+ return encodeTransferHookValidationAccountData(extraMetas.map(staticTransferHookMeta));
245
+ }
246
+ /**
247
+ * Encode the production Token-2022 transfer-hook validation account for Dusk
248
+ * yLP/hLP mints.
249
+ *
250
+ * The validation state resolves static market/asset-mint accounts and derives
251
+ * source/destination YieldAccount PDAs from the source and destination token
252
+ * account owners observed by Token-2022 during Execute.
253
+ */
254
+ export function buildYieldTransferHookYieldValidationAccountData({ market, assetMint, tokenKind, }) {
255
+ return encodeTransferHookValidationAccountData([
256
+ staticTransferHookMeta({ pubkey: market, isSigner: false, isWritable: false }),
257
+ staticTransferHookMeta({ pubkey: assetMint, isSigner: false, isWritable: false }),
258
+ pdaTransferHookMeta(yieldAccountTransferHookSeeds(TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX, tokenKind), true),
259
+ pdaTransferHookMeta(yieldAccountTransferHookSeeds(TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX, tokenKind), true),
260
+ ]);
261
+ }
262
+ export function buildYlpTransferHookValidationAccountData({ market, baseMint, quoteMint, }) {
263
+ return encodeTransferHookValidationAccountData([
264
+ staticTransferHookMeta({ pubkey: market, isSigner: false, isWritable: false }),
265
+ staticTransferHookMeta({ pubkey: baseMint, isSigner: false, isWritable: false }),
266
+ staticTransferHookMeta({ pubkey: quoteMint, isSigner: false, isWritable: false }),
267
+ pdaTransferHookMeta(yieldAccountTransferHookSeeds(TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX, "ylp"), true),
268
+ pdaTransferHookMeta(yieldAccountTransferHookSeeds(TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX, "ylp"), true),
269
+ pdaTransferHookMeta(yieldAccountTransferHookSeeds(TRANSFER_HOOK_SOURCE_ACCOUNT_INDEX, "ylp", TRANSFER_HOOK_QUOTE_ASSET_MINT_ACCOUNT_INDEX), true),
270
+ pdaTransferHookMeta(yieldAccountTransferHookSeeds(TRANSFER_HOOK_DESTINATION_ACCOUNT_INDEX, "ylp", TRANSFER_HOOK_QUOTE_ASSET_MINT_ACCOUNT_INDEX), true),
271
+ ]);
272
+ }
273
+ /**
274
+ * Build Dusk yLP/hLP transfer-hook extra account metas.
275
+ *
276
+ * Token-2022 passes the source token account, LP mint, destination token
277
+ * account, and transfer authority as the base hook accounts. Omnipair Dusk (v2) needs
278
+ * the market, underlying asset mint, canonical source/destination yield
279
+ * accounts, hook program, and standard validation PDA as extra metas so the
280
+ * hook can checkpoint revenue before the balance move is finalized.
281
+ */
282
+ export function buildYieldTransferHookAccountMetas({ lpMint, market, sourceOwner, destinationOwner, assetMint, tokenKind, }) {
283
+ const sourceYieldAccount = deriveYieldAccountAddress(market, sourceOwner, assetMint, tokenKind)[0];
284
+ const destinationYieldAccount = deriveYieldAccountAddress(market, destinationOwner, assetMint, tokenKind)[0];
285
+ const validationAccount = deriveYieldTransferHookValidationAddress(lpMint)[0];
286
+ return [
287
+ { pubkey: market, isSigner: false, isWritable: false },
288
+ { pubkey: assetMint, isSigner: false, isWritable: false },
289
+ { pubkey: sourceYieldAccount, isSigner: false, isWritable: true },
290
+ { pubkey: destinationYieldAccount, isSigner: false, isWritable: true },
291
+ { pubkey: DUSK_PROGRAM_ID, isSigner: false, isWritable: false },
292
+ { pubkey: validationAccount, isSigner: false, isWritable: false },
293
+ ];
294
+ }
295
+ export function buildYlpTransferHookAccountMetas({ lpMint, market, sourceOwner, destinationOwner, baseMint, quoteMint, }) {
296
+ const sourceBaseYieldAccount = deriveYieldAccountAddress(market, sourceOwner, baseMint, "ylp")[0];
297
+ const destinationBaseYieldAccount = deriveYieldAccountAddress(market, destinationOwner, baseMint, "ylp")[0];
298
+ const sourceQuoteYieldAccount = deriveYieldAccountAddress(market, sourceOwner, quoteMint, "ylp")[0];
299
+ const destinationQuoteYieldAccount = deriveYieldAccountAddress(market, destinationOwner, quoteMint, "ylp")[0];
300
+ const validationAccount = deriveYieldTransferHookValidationAddress(lpMint)[0];
301
+ return [
302
+ { pubkey: market, isSigner: false, isWritable: false },
303
+ { pubkey: baseMint, isSigner: false, isWritable: false },
304
+ { pubkey: quoteMint, isSigner: false, isWritable: false },
305
+ { pubkey: sourceBaseYieldAccount, isSigner: false, isWritable: true },
306
+ { pubkey: destinationBaseYieldAccount, isSigner: false, isWritable: true },
307
+ { pubkey: sourceQuoteYieldAccount, isSigner: false, isWritable: true },
308
+ { pubkey: destinationQuoteYieldAccount, isSigner: false, isWritable: true },
309
+ { pubkey: DUSK_PROGRAM_ID, isSigner: false, isWritable: false },
310
+ { pubkey: validationAccount, isSigner: false, isWritable: false },
311
+ ];
312
+ }
313
+ /**
314
+ * Derive canonical aggregate hLP vault yLP token-account PDA.
315
+ */
316
+ export function deriveHlpYlpVaultAddress(market, targetHlpMint, ylpMint) {
317
+ return PublicKey.findProgramAddressSync([
318
+ SEEDS.HLP_YLP_VAULT,
319
+ market.toBuffer(),
320
+ targetHlpMint.toBuffer(),
321
+ ylpMint.toBuffer(),
322
+ ], DUSK_PROGRAM_ID);
323
+ }
324
+ /**
325
+ * Derive insurance PDA address
326
+ */
327
+ export function deriveInsuranceAddress(market, assetMint) {
328
+ return PublicKey.findProgramAddressSync([SEEDS.INSURANCE, market.toBuffer(), assetMint.toBuffer()], DUSK_PROGRAM_ID);
329
+ }
330
+ //# sourceMappingURL=constants.js.map