@omnipair/dusk-sdk 2.0.2 → 2.16.0-next.20261102

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 (187) hide show
  1. package/README.md +1011 -16
  2. package/dist/amounts.d.ts +9 -0
  3. package/dist/amounts.d.ts.map +1 -0
  4. package/dist/amounts.js +10 -0
  5. package/dist/amounts.js.map +1 -0
  6. package/dist/constants.d.ts +71 -14
  7. package/dist/constants.d.ts.map +1 -1
  8. package/dist/constants.js +155 -28
  9. package/dist/constants.js.map +1 -1
  10. package/dist/delegate.d.ts +94 -0
  11. package/dist/delegate.d.ts.map +1 -0
  12. package/dist/delegate.js +131 -0
  13. package/dist/delegate.js.map +1 -0
  14. package/dist/dusk.d.ts +14 -0
  15. package/dist/dusk.d.ts.map +1 -1
  16. package/dist/dusk.js +22 -0
  17. package/dist/dusk.js.map +1 -1
  18. package/dist/get.d.ts +3253 -378
  19. package/dist/get.d.ts.map +1 -1
  20. package/dist/get.js +130 -70
  21. package/dist/get.js.map +1 -1
  22. package/dist/governance-client.d.ts +169 -0
  23. package/dist/governance-client.d.ts.map +1 -0
  24. package/dist/governance-client.js +146 -0
  25. package/dist/governance-client.js.map +1 -0
  26. package/dist/governance.d.ts +313 -0
  27. package/dist/governance.d.ts.map +1 -0
  28. package/dist/governance.js +902 -0
  29. package/dist/governance.js.map +1 -0
  30. package/dist/hash.d.ts +9 -0
  31. package/dist/hash.d.ts.map +1 -0
  32. package/dist/hash.js +16 -0
  33. package/dist/hash.js.map +1 -0
  34. package/dist/hlp-liquidation-client.d.ts +106 -0
  35. package/dist/hlp-liquidation-client.d.ts.map +1 -0
  36. package/dist/hlp-liquidation-client.js +150 -0
  37. package/dist/hlp-liquidation-client.js.map +1 -0
  38. package/dist/idl_delegate.d.ts +3 -0
  39. package/dist/idl_delegate.d.ts.map +1 -0
  40. package/dist/idl_delegate.js +3 -0
  41. package/dist/idl_delegate.js.map +1 -0
  42. package/dist/idl_delegate.json +6119 -0
  43. package/dist/idl_v2.json +17175 -5862
  44. package/dist/index.d.ts +23 -0
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +23 -0
  47. package/dist/index.js.map +1 -1
  48. package/dist/interest-accounting.d.ts +49 -0
  49. package/dist/interest-accounting.d.ts.map +1 -0
  50. package/dist/interest-accounting.js +72 -0
  51. package/dist/interest-accounting.js.map +1 -0
  52. package/dist/leverage-actions.d.ts +122 -0
  53. package/dist/leverage-actions.d.ts.map +1 -0
  54. package/dist/leverage-actions.js +228 -0
  55. package/dist/leverage-actions.js.map +1 -0
  56. package/dist/leverage-margins.d.ts +12 -0
  57. package/dist/leverage-margins.d.ts.map +1 -0
  58. package/dist/leverage-margins.js +45 -0
  59. package/dist/leverage-margins.js.map +1 -0
  60. package/dist/leverage-preview.d.ts +587 -0
  61. package/dist/leverage-preview.d.ts.map +1 -0
  62. package/dist/leverage-preview.js +89 -0
  63. package/dist/leverage-preview.js.map +1 -0
  64. package/dist/liquidation-client.d.ts +230 -0
  65. package/dist/liquidation-client.d.ts.map +1 -0
  66. package/dist/liquidation-client.js +239 -0
  67. package/dist/liquidation-client.js.map +1 -0
  68. package/dist/liquidation.d.ts +78 -0
  69. package/dist/liquidation.d.ts.map +1 -0
  70. package/dist/liquidation.js +122 -0
  71. package/dist/liquidation.js.map +1 -0
  72. package/dist/lp-naming.d.ts +43 -0
  73. package/dist/lp-naming.d.ts.map +1 -0
  74. package/dist/lp-naming.js +81 -0
  75. package/dist/lp-naming.js.map +1 -0
  76. package/dist/lp-vanity.d.ts +33 -0
  77. package/dist/lp-vanity.d.ts.map +1 -0
  78. package/dist/lp-vanity.js +94 -0
  79. package/dist/lp-vanity.js.map +1 -0
  80. package/dist/market-bootstrap.d.ts +109 -0
  81. package/dist/market-bootstrap.d.ts.map +1 -0
  82. package/dist/market-bootstrap.js +158 -0
  83. package/dist/market-bootstrap.js.map +1 -0
  84. package/dist/market-launch.d.ts +45 -0
  85. package/dist/market-launch.d.ts.map +1 -0
  86. package/dist/market-launch.js +76 -0
  87. package/dist/market-launch.js.map +1 -0
  88. package/dist/oracle.d.ts +46 -0
  89. package/dist/oracle.d.ts.map +1 -0
  90. package/dist/oracle.js +103 -0
  91. package/dist/oracle.js.map +1 -0
  92. package/dist/preview.d.ts +39 -1
  93. package/dist/preview.d.ts.map +1 -1
  94. package/dist/preview.js +12 -0
  95. package/dist/preview.js.map +1 -1
  96. package/dist/protection.d.ts +48 -0
  97. package/dist/protection.d.ts.map +1 -0
  98. package/dist/protection.js +88 -0
  99. package/dist/protection.js.map +1 -0
  100. package/dist/risk-pricing.d.ts +14 -0
  101. package/dist/risk-pricing.d.ts.map +1 -0
  102. package/dist/risk-pricing.js +25 -0
  103. package/dist/risk-pricing.js.map +1 -0
  104. package/dist/simulation.d.ts +45 -0
  105. package/dist/simulation.d.ts.map +1 -0
  106. package/dist/simulation.js +106 -0
  107. package/dist/simulation.js.map +1 -0
  108. package/dist/tranche-client.d.ts +121 -0
  109. package/dist/tranche-client.d.ts.map +1 -0
  110. package/dist/tranche-client.js +185 -0
  111. package/dist/tranche-client.js.map +1 -0
  112. package/dist/transfer-fees.d.ts +46 -0
  113. package/dist/transfer-fees.d.ts.map +1 -0
  114. package/dist/transfer-fees.js +76 -0
  115. package/dist/transfer-fees.js.map +1 -0
  116. package/dist/type-aliases.d.ts +271 -31
  117. package/dist/type-aliases.d.ts.map +1 -1
  118. package/dist/types_delegate.d.ts +6126 -0
  119. package/dist/types_delegate.d.ts.map +1 -0
  120. package/dist/types_delegate.js +2 -0
  121. package/dist/types_delegate.js.map +1 -0
  122. package/dist/types_v2.d.ts +17235 -5922
  123. package/dist/types_v2.d.ts.map +1 -1
  124. package/dist/virtual-book/index.d.ts +4 -0
  125. package/dist/virtual-book/index.d.ts.map +1 -0
  126. package/dist/virtual-book/index.js +4 -0
  127. package/dist/virtual-book/index.js.map +1 -0
  128. package/dist/virtual-book/market.d.ts +608 -0
  129. package/dist/virtual-book/market.d.ts.map +1 -0
  130. package/dist/virtual-book/market.js +59 -0
  131. package/dist/virtual-book/market.js.map +1 -0
  132. package/dist/virtual-book/projection.d.ts +44 -0
  133. package/dist/virtual-book/projection.d.ts.map +1 -0
  134. package/dist/virtual-book/projection.js +229 -0
  135. package/dist/virtual-book/projection.js.map +1 -0
  136. package/dist/virtual-book/quotes.d.ts +1241 -0
  137. package/dist/virtual-book/quotes.d.ts.map +1 -0
  138. package/dist/virtual-book/quotes.js +233 -0
  139. package/dist/virtual-book/quotes.js.map +1 -0
  140. package/dist/write.d.ts +517 -1
  141. package/dist/write.d.ts.map +1 -1
  142. package/dist/write.js +1172 -23
  143. package/dist/write.js.map +1 -1
  144. package/dist/yield-client.d.ts +51 -0
  145. package/dist/yield-client.d.ts.map +1 -0
  146. package/dist/yield-client.js +98 -0
  147. package/dist/yield-client.js.map +1 -0
  148. package/package.json +9 -2
  149. package/src/amounts.ts +13 -0
  150. package/src/constants.ts +236 -26
  151. package/src/delegate.ts +238 -0
  152. package/src/dusk.ts +28 -0
  153. package/src/get.ts +247 -82
  154. package/src/governance-client.ts +234 -0
  155. package/src/governance.ts +1422 -0
  156. package/src/hash.ts +15 -0
  157. package/src/hlp-liquidation-client.ts +195 -0
  158. package/src/idl_delegate.json +6119 -0
  159. package/src/idl_delegate.ts +3 -0
  160. package/src/idl_v2.json +17175 -5862
  161. package/src/index.ts +31 -0
  162. package/src/interest-accounting.ts +85 -0
  163. package/src/leverage-actions.ts +216 -0
  164. package/src/leverage-margins.ts +48 -0
  165. package/src/leverage-preview.ts +86 -0
  166. package/src/liquidation-client.ts +297 -0
  167. package/src/liquidation.ts +137 -0
  168. package/src/lp-naming.ts +127 -0
  169. package/src/lp-vanity.ts +135 -0
  170. package/src/market-bootstrap.ts +273 -0
  171. package/src/market-launch.ts +103 -0
  172. package/src/oracle.ts +120 -0
  173. package/src/preview.ts +53 -1
  174. package/src/protection.ts +112 -0
  175. package/src/risk-pricing.ts +28 -0
  176. package/src/simulation.ts +169 -0
  177. package/src/tranche-client.ts +187 -0
  178. package/src/transfer-fees.ts +99 -0
  179. package/src/type-aliases.ts +280 -31
  180. package/src/types_delegate.ts +6125 -0
  181. package/src/types_v2.ts +17174 -5861
  182. package/src/virtual-book/index.ts +3 -0
  183. package/src/virtual-book/market.ts +76 -0
  184. package/src/virtual-book/projection.ts +265 -0
  185. package/src/virtual-book/quotes.ts +308 -0
  186. package/src/write.ts +2073 -34
  187. package/src/yield-client.ts +117 -0
package/src/dusk.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { DuskTranches } from "./tranche-client.js";
1
2
  import type { AnchorProvider, Program } from "@coral-xyz/anchor";
2
3
  import type { Connection } from "@solana/web3.js";
3
4
 
@@ -6,7 +7,15 @@ import { DuskGet } from "./get.js";
6
7
  import { DuskIndexerClient, type FetchLike } from "./indexer.js";
7
8
  import { createDuskProgram } from "./program.js";
8
9
  import type { Dusk as DuskIdl } from "./types_v2.js";
10
+ import { DuskGovernance } from "./governance-client.js";
9
11
  import { DuskWrite } from "./write.js";
12
+ import { DuskHlpLiquidations } from "./hlp-liquidation-client.js";
13
+ import { DuskYield } from "./yield-client.js";
14
+ import { DuskLiquidations } from "./liquidation-client.js";
15
+ import {
16
+ createLeverageDelegateProgram,
17
+ DuskLeverageOrders,
18
+ } from "./delegate.js";
10
19
 
11
20
  export interface DuskOptions {
12
21
  program?: Program<DuskIdl>;
@@ -16,13 +25,21 @@ export interface DuskOptions {
16
25
  feePayer?: AddressLike;
17
26
  indexerBaseUrl?: string;
18
27
  fetch?: FetchLike;
28
+ leverageDelegateProgramId?: AddressLike;
19
29
  }
20
30
 
21
31
  export class Dusk {
22
32
  readonly program: Program<DuskIdl>;
23
33
  readonly get: DuskGet;
24
34
  readonly write: DuskWrite;
35
+ readonly governance: DuskGovernance;
36
+ readonly liquidations: DuskLiquidations;
37
+ readonly yield: DuskYield;
38
+ readonly tranches: DuskTranches;
39
+ readonly hlpLiquidations: DuskHlpLiquidations;
25
40
  readonly fetch: DuskIndexerClient;
41
+ /** Conditional orders, which live in the delegate program. */
42
+ readonly orders: DuskLeverageOrders;
26
43
 
27
44
  constructor(options: DuskOptions = {}) {
28
45
  this.program =
@@ -35,9 +52,20 @@ export class Dusk {
35
52
 
36
53
  this.get = new DuskGet(this.program, options.feePayer);
37
54
  this.write = new DuskWrite(this.program);
55
+ this.governance = new DuskGovernance(this.program);
56
+ this.liquidations = new DuskLiquidations(this.program, options.feePayer);
57
+ this.hlpLiquidations = new DuskHlpLiquidations(this.program, options.feePayer);
58
+ this.yield = new DuskYield(this.program, options.feePayer);
59
+ this.tranches = new DuskTranches(this.program);
38
60
  this.fetch = new DuskIndexerClient({
39
61
  baseUrl: options.indexerBaseUrl,
40
62
  fetch: options.fetch,
41
63
  });
64
+ this.orders = new DuskLeverageOrders(
65
+ createLeverageDelegateProgram({
66
+ provider: this.program.provider as AnchorProvider,
67
+ programId: options.leverageDelegateProgramId,
68
+ })
69
+ );
42
70
  }
43
71
  }
package/src/get.ts CHANGED
@@ -1,10 +1,27 @@
1
+ import { DuskWrite, type DuskInstructionName } from "./write.js";
1
2
  import type { BN, Program } from "@coral-xyz/anchor";
3
+ import { previewLeverageAction, type PreviewLeverageActionParams } from "./leverage-preview.js";
4
+ import { PublicKey, TransactionInstruction, type Commitment } from "@solana/web3.js";
2
5
  import {
3
- PublicKey,
4
- Transaction,
5
- type Commitment,
6
- type TransactionInstruction,
7
- } from "@solana/web3.js";
6
+ simulatePreviewWithContext,
7
+ type PreviewSimulationOptions,
8
+ type SimulateOptions,
9
+ } from "./simulation.js";
10
+ export { DuskSimulationError, DuskPreviewTimeoutError } from "./simulation.js";
11
+ export type {
12
+ SimulateOptions,
13
+ PreviewSimulationOptions,
14
+ DuskPreviewSimulation,
15
+ } from "./simulation.js";
16
+ import {
17
+ previewVirtualBookSnapshot,
18
+ previewVirtualBookQuotes,
19
+ previewVirtualBookBatch,
20
+ projectDuskVirtualBook,
21
+ type DuskVirtualBookSnapshot,
22
+ type VirtualBookQuoteOptions,
23
+ type VirtualBookQuoteRequest,
24
+ } from "./virtual-book/index.js";
8
25
 
9
26
  import {
10
27
  deriveBorrowPositionAddress,
@@ -16,22 +33,30 @@ import {
16
33
  deriveMarketCollateralVaultAddress,
17
34
  deriveMarketInterestVaultAddress,
18
35
  deriveMarketReserveVaultAddress,
36
+ deriveParameterProposalAddress,
37
+ deriveProposalSupportAddress,
19
38
  deriveReferralAccrualAddress,
20
39
  deriveReferralPartnerAddress,
21
40
  deriveTokenMetadataAddress,
22
41
  deriveYieldAccountAddress,
23
42
  deriveYieldTransferHookValidationAddress,
43
+ type U64SeedLike,
24
44
  } from "./constants.js";
25
45
  import { address, DEFAULT_READONLY_PUBLIC_KEY, normalizeAccountKeys, type AddressLike } from "./address.js";
46
+ import { getStoredLeverageMargins } from "./leverage-margins.js";
26
47
  import {
48
+ decodePreviewHlpDepositCapacityReturnData,
49
+ type HlpDepositCapacityPreview,
27
50
  decodePreviewAddLiquidityReturnData,
28
51
  decodePreviewBorrowCapacityReturnData,
29
52
  decodePreviewBorrowPositionReturnData,
53
+ decodePreviewBorrowPositionCapacityReturnData,
30
54
  decodePreviewMarketReturnData,
31
55
  decodePreviewSwapReturnData,
32
56
  type AddLiquidityPreview,
33
57
  type BorrowCapacityPreview,
34
58
  type BorrowPositionPreview,
59
+ type BorrowPositionCapacityPreview,
35
60
  type MarketPreview,
36
61
  type PreviewReturnData,
37
62
  type SwapPreview,
@@ -42,6 +67,8 @@ import type {
42
67
  LeverageDelegation,
43
68
  LeveragePosition,
44
69
  Market,
70
+ ParameterProposal,
71
+ ProposalSupport,
45
72
  ReferralAccrual,
46
73
  ReferralPartner,
47
74
  YieldAccount,
@@ -63,15 +90,21 @@ export const pda = {
63
90
  insurance: deriveInsuranceAddress,
64
91
  referralPartner: deriveReferralPartnerAddress,
65
92
  referralAccrual: deriveReferralAccrualAddress,
93
+ parameterProposal: deriveParameterProposalAddress,
94
+ proposalSupport: deriveProposalSupportAddress,
66
95
  } as const;
67
96
 
68
- export interface SimulateOptions {
69
- feePayer?: AddressLike;
70
- commitment?: Commitment;
97
+ export interface PreviewHlpDepositCapacityParams extends SimulateOptions {
98
+ market: AddressLike;
99
+ futarchyAuthority?: AddressLike;
100
+ baseMint: AddressLike;
101
+ quoteMint: AddressLike;
102
+ targetHlpMint: AddressLike;
71
103
  }
72
104
 
73
105
  export interface PreviewSwapParams extends SimulateOptions {
74
106
  market: AddressLike;
107
+ futarchyAuthority?: AddressLike;
75
108
  assetInMint: AddressLike;
76
109
  assetOutMint: AddressLike;
77
110
  exactAssetIn: BN;
@@ -89,6 +122,7 @@ export interface PreviewBorrowCapacityParams extends SimulateOptions {
89
122
  market: AddressLike;
90
123
  collateralAssetMint: AddressLike;
91
124
  debtAssetMint: AddressLike;
125
+ /** Net collateral amount credited to the vault after any deposit transfer fee. */
92
126
  collateralAmount: BN;
93
127
  /**
94
128
  * Candidate debt amount used for the returned CF and health fields. When
@@ -97,19 +131,25 @@ export interface PreviewBorrowCapacityParams extends SimulateOptions {
97
131
  projectedBorrowAmount?: BN | null;
98
132
  }
99
133
 
100
- export interface PreviewBorrowPositionParams extends SimulateOptions {
134
+ export interface PreviewBorrowPositionCapacityParams extends SimulateOptions {
135
+ capacityKind: "borrow" | "withdraw";
101
136
  market: AddressLike;
102
137
  borrowPosition: AddressLike;
138
+ collateralAssetMint: AddressLike;
139
+ debtAssetMint: AddressLike;
140
+ /** Net credit after transfer fees, or negative collateral vault debit. */
141
+ collateralChange: BN;
142
+ /** Additional debt; omit for maximum additional capacity. */
143
+ projectedBorrowAmount?: BN | null;
103
144
  }
104
145
 
105
- export class DuskSimulationError extends Error {
106
- constructor(
107
- message: string,
108
- readonly simulation: Awaited<ReturnType<Program<Dusk>["provider"]["connection"]["simulateTransaction"]>>
109
- ) {
110
- super(message);
111
- this.name = "DuskSimulationError";
112
- }
146
+ export interface PreviewBorrowPositionParams extends SimulateOptions {
147
+ market: AddressLike;
148
+ borrowPosition: AddressLike;
149
+ /** Market base mint; liquidation fields value collateral after its transfer fee. */
150
+ baseMint: AddressLike;
151
+ /** Market quote mint; liquidation fields value collateral after its transfer fee. */
152
+ quoteMint: AddressLike;
113
153
  }
114
154
 
115
155
  export class DuskGet {
@@ -134,81 +174,163 @@ export class DuskGet {
134
174
  return client.fetch(address(account));
135
175
  }
136
176
 
177
+ private allProgramAccounts<T>(name: string): Promise<Array<{ publicKey: PublicKey; account: T }>> {
178
+ const client = (
179
+ this.program as unknown as {
180
+ account: Record<string, { all(): Promise<Array<{ publicKey: PublicKey; account: T }>> }>;
181
+ }
182
+ ).account[name];
183
+ if (!client) {
184
+ throw new Error(`Unknown Dusk account type: ${name}`);
185
+ }
186
+ return client.all();
187
+ }
188
+
137
189
  market(account: AddressLike): Promise<Market> {
138
- return this.program.account.market.fetch(address(account));
190
+ return this.programAccount<Market>("market", account);
139
191
  }
140
192
 
141
193
  borrowPosition(account: AddressLike): Promise<BorrowPosition> {
142
- return this.program.account.borrowPosition.fetch(address(account));
194
+ return this.programAccount<BorrowPosition>("borrowPosition", account);
143
195
  }
144
196
 
145
197
  leveragePosition(account: AddressLike): Promise<LeveragePosition> {
146
- return this.program.account.leveragePosition.fetch(address(account));
198
+ return this.programAccount<LeveragePosition>("leveragePosition", account);
199
+ }
200
+
201
+ /** Fetch saved margin terms; does not quote an opening or an increase. */
202
+ async leverageMargins(account: AddressLike) {
203
+ return getStoredLeverageMargins(await this.leveragePosition(account));
204
+ }
205
+
206
+ previewLeverageAction(params: PreviewLeverageActionParams) {
207
+ return previewLeverageAction(this.program, params);
147
208
  }
148
209
 
149
210
  leverageDelegation(account: AddressLike): Promise<LeverageDelegation> {
150
- return this.program.account.leverageDelegation.fetch(address(account));
211
+ return this.programAccount<LeverageDelegation>("leverageDelegation", account);
151
212
  }
152
213
 
153
214
  yieldAccount(account: AddressLike): Promise<YieldAccount> {
154
- return this.program.account.yieldAccount.fetch(address(account));
215
+ return this.programAccount<YieldAccount>("yieldAccount", account);
155
216
  }
156
217
 
157
218
  futarchyAuthority(account: AddressLike = deriveFutarchyAuthorityAddress()[0]): Promise<FutarchyAuthority> {
158
- return this.program.account.futarchyAuthority.fetch(address(account));
219
+ return this.programAccount<FutarchyAuthority>("futarchyAuthority", account);
159
220
  }
160
221
 
161
222
  referralPartner(account: AddressLike): Promise<ReferralPartner> {
162
- return this.program.account.referralPartner.fetch(address(account));
223
+ return this.programAccount<ReferralPartner>("referralPartner", account);
163
224
  }
164
225
 
165
226
  referralAccrual(account: AddressLike): Promise<ReferralAccrual> {
166
- return this.program.account.referralAccrual.fetch(address(account));
227
+ return this.programAccount<ReferralAccrual>("referralAccrual", account);
228
+ }
229
+
230
+ parameterProposal(account: AddressLike): Promise<ParameterProposal> {
231
+ return this.programAccount<ParameterProposal>("parameterProposal", account);
232
+ }
233
+
234
+ proposalSupport(account: AddressLike): Promise<ProposalSupport> {
235
+ return this.programAccount<ProposalSupport>("proposalSupport", account);
236
+ }
237
+
238
+ parameterProposalFor(
239
+ market: AddressLike,
240
+ proposer: AddressLike,
241
+ nonce: U64SeedLike
242
+ ): Promise<ParameterProposal> {
243
+ const [proposal] = deriveParameterProposalAddress(
244
+ address(market),
245
+ address(proposer),
246
+ nonce,
247
+ this.program.programId
248
+ );
249
+ return this.parameterProposal(proposal);
250
+ }
251
+
252
+ proposalSupportFor(
253
+ proposal: AddressLike,
254
+ supporter: AddressLike
255
+ ): Promise<ProposalSupport> {
256
+ const [support] = deriveProposalSupportAddress(
257
+ address(proposal),
258
+ address(supporter),
259
+ this.program.programId
260
+ );
261
+ return this.proposalSupport(support);
167
262
  }
168
263
 
169
264
  allMarkets() {
170
- return this.program.account.market.all();
265
+ return this.allProgramAccounts<Market>("market");
171
266
  }
172
267
 
173
268
  allBorrowPositions() {
174
- return this.program.account.borrowPosition.all();
269
+ return this.allProgramAccounts<BorrowPosition>("borrowPosition");
175
270
  }
176
271
 
177
272
  allLeveragePositions() {
178
- return this.program.account.leveragePosition.all();
273
+ return this.allProgramAccounts<LeveragePosition>("leveragePosition");
179
274
  }
180
275
 
181
276
  allReferralPartners() {
182
- return this.program.account.referralPartner.all();
277
+ return this.allProgramAccounts<ReferralPartner>("referralPartner");
183
278
  }
184
279
 
185
280
  allReferralAccruals() {
186
- return this.program.account.referralAccrual.all();
281
+ return this.allProgramAccounts<ReferralAccrual>("referralAccrual");
282
+ }
283
+
284
+ allParameterProposals() {
285
+ return this.allProgramAccounts<ParameterProposal>("parameterProposal");
286
+ }
287
+
288
+ allProposalSupports() {
289
+ return this.allProgramAccounts<ProposalSupport>("proposalSupport");
290
+ }
291
+
292
+ private previewInstruction(
293
+ name: string,
294
+ args: unknown[],
295
+ accounts: Record<string, unknown>
296
+ ): Promise<TransactionInstruction> {
297
+ return new DuskWrite(this.program).instruction(name as DuskInstructionName, args, { accounts });
187
298
  }
188
299
 
189
300
  async previewMarket(market: AddressLike, options: SimulateOptions = {}): Promise<MarketPreview> {
190
- const instruction = await this.program.methods
191
- .previewMarket()
192
- .accounts(normalizeAccountKeys({ market }))
193
- .instruction();
301
+ const instruction = await this.previewInstruction("previewMarket", [], normalizeAccountKeys({ market }));
194
302
 
195
303
  return decodePreviewMarketReturnData(await this.simulateReturnData(instruction, options));
196
304
  }
197
305
 
306
+ /** Current full-value hedge admission and funding ceiling, including transfer fees.
307
+ * A Ready status is not an amount-specific execution guarantee; simulate the
308
+ * completed deposit transaction to check rounding, slippage and custody.
309
+ */
310
+ async previewHlpDepositCapacity(params: PreviewHlpDepositCapacityParams): Promise<HlpDepositCapacityPreview> {
311
+ const instruction = await this.previewInstruction("previewHlpDepositCapacity", [], normalizeAccountKeys({
312
+ market: params.market,
313
+ futarchyAuthority: params.futarchyAuthority ?? deriveFutarchyAuthorityAddress(this.program.programId)[0],
314
+ baseMint: params.baseMint,
315
+ quoteMint: params.quoteMint,
316
+ targetHlpMint: params.targetHlpMint,
317
+ }));
318
+ return decodePreviewHlpDepositCapacityReturnData(await this.simulateReturnData(instruction, params));
319
+ }
320
+
198
321
  async previewAddLiquidity(params: PreviewAddLiquidityParams): Promise<AddLiquidityPreview> {
199
- const instruction = await this.program.methods
200
- .previewAddLiquidity({
322
+ const instruction = await this.previewInstruction(
323
+ "previewAddLiquidity",
324
+ [{
201
325
  baseDepositAmount: params.baseDepositAmount,
202
326
  quoteDepositAmount: params.quoteDepositAmount,
203
- })
204
- .accounts(
205
- normalizeAccountKeys({
327
+ }],
328
+ normalizeAccountKeys({
206
329
  market: params.market,
207
330
  baseMint: params.baseMint,
208
331
  quoteMint: params.quoteMint,
209
- })
210
- )
211
- .instruction();
332
+ })
333
+ );
212
334
 
213
335
  return decodePreviewAddLiquidityReturnData(
214
336
  await this.simulateReturnData(instruction, params)
@@ -216,18 +338,19 @@ export class DuskGet {
216
338
  }
217
339
 
218
340
  async previewSwap(params: PreviewSwapParams): Promise<SwapPreview> {
219
- const instruction = await this.program.methods
220
- .previewSwap({
341
+ const instruction = await this.previewInstruction(
342
+ "previewSwap",
343
+ [{
221
344
  exactAssetIn: params.exactAssetIn,
222
- })
223
- .accounts(
224
- normalizeAccountKeys({
345
+ }],
346
+ normalizeAccountKeys({
225
347
  market: params.market,
348
+ futarchyAuthority:
349
+ params.futarchyAuthority ?? deriveFutarchyAuthorityAddress()[0],
226
350
  assetInMint: params.assetInMint,
227
351
  assetOutMint: params.assetOutMint,
228
- })
229
- )
230
- .instruction();
352
+ })
353
+ );
231
354
 
232
355
  return decodePreviewSwapReturnData(await this.simulateReturnData(instruction, params));
233
356
  }
@@ -235,57 +358,99 @@ export class DuskGet {
235
358
  async previewBorrowCapacity(
236
359
  params: PreviewBorrowCapacityParams
237
360
  ): Promise<BorrowCapacityPreview> {
238
- const instruction = await this.program.methods
239
- .previewBorrowCapacity({
361
+ const instruction = await this.previewInstruction(
362
+ "previewBorrowCapacity",
363
+ [{
240
364
  collateralAmount: params.collateralAmount,
241
365
  projectedBorrowAmount: params.projectedBorrowAmount ?? null,
242
- })
243
- .accounts(
244
- normalizeAccountKeys({
366
+ }],
367
+ normalizeAccountKeys({
245
368
  market: params.market,
246
369
  collateralAssetMint: params.collateralAssetMint,
247
370
  debtAssetMint: params.debtAssetMint,
248
- })
249
- )
250
- .instruction();
371
+ })
372
+ );
251
373
 
252
374
  return decodePreviewBorrowCapacityReturnData(await this.simulateReturnData(instruction, params));
253
375
  }
254
376
 
377
+ async previewBorrowPositionCapacity(
378
+ params: PreviewBorrowPositionCapacityParams
379
+ ): Promise<BorrowPositionCapacityPreview> {
380
+ const instruction = await this.previewInstruction(
381
+ "previewBorrowPositionCapacity",
382
+ [{
383
+ capacityKind: params.capacityKind === "borrow" ? { borrow: {} } : { withdraw: {} },
384
+ collateralChange: params.collateralChange,
385
+ projectedBorrowAmount: params.projectedBorrowAmount ?? null,
386
+ }],
387
+ normalizeAccountKeys({
388
+ market: params.market,
389
+ borrowPosition: params.borrowPosition,
390
+ collateralAssetMint: params.collateralAssetMint,
391
+ debtAssetMint: params.debtAssetMint,
392
+ })
393
+ );
394
+
395
+ return decodePreviewBorrowPositionCapacityReturnData(await this.simulateReturnData(instruction, params));
396
+ }
397
+
255
398
  async previewBorrowPosition(params: PreviewBorrowPositionParams): Promise<BorrowPositionPreview> {
256
- const instruction = await this.program.methods
257
- .previewBorrowPosition()
258
- .accounts(
259
- normalizeAccountKeys({
399
+ const instruction = await this.previewInstruction(
400
+ "previewBorrowPosition",
401
+ [],
402
+ normalizeAccountKeys({
260
403
  market: params.market,
261
404
  borrowPosition: params.borrowPosition,
262
- })
263
- )
264
- .instruction();
405
+ baseMint: params.baseMint,
406
+ quoteMint: params.quoteMint,
407
+ })
408
+ );
265
409
 
266
410
  return decodePreviewBorrowPositionReturnData(await this.simulateReturnData(instruction, params));
267
411
  }
268
412
 
413
+ /** Simulate one or more instructions while retaining bank and account provenance. */
414
+ simulateWithContext(
415
+ instructions: readonly TransactionInstruction[],
416
+ options: PreviewSimulationOptions = {}
417
+ ) {
418
+ return simulatePreviewWithContext(this.program, instructions, {
419
+ ...options,
420
+ feePayer: options.feePayer ?? this.program.provider.publicKey ?? this.defaultFeePayer,
421
+ });
422
+ }
423
+
269
424
  async simulateReturnData(
270
425
  instruction: TransactionInstruction,
271
426
  options: SimulateOptions = {}
272
427
  ): Promise<PreviewReturnData> {
273
- const tx = new Transaction().add(instruction);
274
- tx.feePayer = address(options.feePayer ?? this.program.provider.publicKey ?? this.defaultFeePayer);
275
- tx.recentBlockhash = (
276
- await this.program.provider.connection.getLatestBlockhash(options.commitment)
277
- ).blockhash;
278
-
279
- const simulation = await this.program.provider.connection.simulateTransaction(tx);
280
- if (simulation.value.err) {
281
- throw new DuskSimulationError("Dusk simulation failed", simulation);
282
- }
283
- if (!simulation.value.returnData) {
284
- throw new DuskSimulationError("Dusk simulation did not return data", simulation);
285
- }
286
- if (simulation.value.returnData.programId !== this.program.programId.toBase58()) {
287
- throw new DuskSimulationError("Dusk simulation returned data from a different program", simulation);
288
- }
289
- return simulation.value.returnData;
428
+ return (await this.simulateWithContext([instruction], options)).value.returnData!;
429
+ }
430
+
431
+ previewVirtualBookSnapshot(market: AddressLike, options: PreviewSimulationOptions = {}) {
432
+ return previewVirtualBookSnapshot(this.program, market, options);
433
+ }
434
+
435
+ previewVirtualBookBatch(
436
+ snapshot: DuskVirtualBookSnapshot,
437
+ requests: VirtualBookQuoteRequest[],
438
+ options: PreviewSimulationOptions = {}
439
+ ) {
440
+ return previewVirtualBookBatch(this.program, snapshot, requests, options);
441
+ }
442
+
443
+ previewVirtualBookQuotes(
444
+ snapshot: DuskVirtualBookSnapshot,
445
+ options: VirtualBookQuoteOptions = {}
446
+ ) {
447
+ return previewVirtualBookQuotes(this.program, snapshot, options);
448
+ }
449
+
450
+ /** Fee-inclusive display depth. Transaction quotes must still use the exact requested input. */
451
+ async previewVirtualBook(market: AddressLike, options: VirtualBookQuoteOptions = {}) {
452
+ const snapshot = await this.previewVirtualBookSnapshot(market, options);
453
+ const quotes = await this.previewVirtualBookQuotes(snapshot, options);
454
+ return { ...quotes, book: projectDuskVirtualBook(quotes)! };
290
455
  }
291
456
  }