@omnipair/dusk-sdk 2.0.1 → 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 (192) hide show
  1. package/README.md +1085 -26
  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 +80 -33
  7. package/dist/constants.d.ts.map +1 -1
  8. package/dist/constants.js +175 -64
  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 +3285 -237
  19. package/dist/get.d.ts.map +1 -1
  20. package/dist/get.js +140 -71
  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 +18372 -6121
  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/referral.d.ts +28 -28
  101. package/dist/referral.d.ts.map +1 -1
  102. package/dist/referral.js +41 -33
  103. package/dist/referral.js.map +1 -1
  104. package/dist/risk-pricing.d.ts +14 -0
  105. package/dist/risk-pricing.d.ts.map +1 -0
  106. package/dist/risk-pricing.js +25 -0
  107. package/dist/risk-pricing.js.map +1 -0
  108. package/dist/simulation.d.ts +45 -0
  109. package/dist/simulation.d.ts.map +1 -0
  110. package/dist/simulation.js +106 -0
  111. package/dist/simulation.js.map +1 -0
  112. package/dist/tranche-client.d.ts +121 -0
  113. package/dist/tranche-client.d.ts.map +1 -0
  114. package/dist/tranche-client.js +185 -0
  115. package/dist/tranche-client.js.map +1 -0
  116. package/dist/transfer-fees.d.ts +46 -0
  117. package/dist/transfer-fees.d.ts.map +1 -0
  118. package/dist/transfer-fees.js +76 -0
  119. package/dist/transfer-fees.js.map +1 -0
  120. package/dist/type-aliases.d.ts +271 -30
  121. package/dist/type-aliases.d.ts.map +1 -1
  122. package/dist/types_delegate.d.ts +6126 -0
  123. package/dist/types_delegate.d.ts.map +1 -0
  124. package/dist/types_delegate.js +2 -0
  125. package/dist/types_delegate.js.map +1 -0
  126. package/dist/types_v2.d.ts +18105 -5854
  127. package/dist/types_v2.d.ts.map +1 -1
  128. package/dist/virtual-book/index.d.ts +4 -0
  129. package/dist/virtual-book/index.d.ts.map +1 -0
  130. package/dist/virtual-book/index.js +4 -0
  131. package/dist/virtual-book/index.js.map +1 -0
  132. package/dist/virtual-book/market.d.ts +608 -0
  133. package/dist/virtual-book/market.d.ts.map +1 -0
  134. package/dist/virtual-book/market.js +59 -0
  135. package/dist/virtual-book/market.js.map +1 -0
  136. package/dist/virtual-book/projection.d.ts +44 -0
  137. package/dist/virtual-book/projection.d.ts.map +1 -0
  138. package/dist/virtual-book/projection.js +229 -0
  139. package/dist/virtual-book/projection.js.map +1 -0
  140. package/dist/virtual-book/quotes.d.ts +1241 -0
  141. package/dist/virtual-book/quotes.d.ts.map +1 -0
  142. package/dist/virtual-book/quotes.js +233 -0
  143. package/dist/virtual-book/quotes.js.map +1 -0
  144. package/dist/write.d.ts +584 -14
  145. package/dist/write.d.ts.map +1 -1
  146. package/dist/write.js +1371 -47
  147. package/dist/write.js.map +1 -1
  148. package/dist/yield-client.d.ts +51 -0
  149. package/dist/yield-client.d.ts.map +1 -0
  150. package/dist/yield-client.js +98 -0
  151. package/dist/yield-client.js.map +1 -0
  152. package/package.json +9 -2
  153. package/src/amounts.ts +13 -0
  154. package/src/constants.ts +276 -111
  155. package/src/delegate.ts +238 -0
  156. package/src/dusk.ts +28 -0
  157. package/src/get.ts +267 -90
  158. package/src/governance-client.ts +234 -0
  159. package/src/governance.ts +1422 -0
  160. package/src/hash.ts +15 -0
  161. package/src/hlp-liquidation-client.ts +195 -0
  162. package/src/idl_delegate.json +6119 -0
  163. package/src/idl_delegate.ts +3 -0
  164. package/src/idl_v2.json +18372 -6121
  165. package/src/index.ts +31 -0
  166. package/src/interest-accounting.ts +85 -0
  167. package/src/leverage-actions.ts +216 -0
  168. package/src/leverage-margins.ts +48 -0
  169. package/src/leverage-preview.ts +86 -0
  170. package/src/liquidation-client.ts +297 -0
  171. package/src/liquidation.ts +137 -0
  172. package/src/lp-naming.ts +127 -0
  173. package/src/lp-vanity.ts +135 -0
  174. package/src/market-bootstrap.ts +273 -0
  175. package/src/market-launch.ts +103 -0
  176. package/src/oracle.ts +120 -0
  177. package/src/preview.ts +53 -1
  178. package/src/protection.ts +112 -0
  179. package/src/referral.ts +71 -71
  180. package/src/risk-pricing.ts +28 -0
  181. package/src/simulation.ts +169 -0
  182. package/src/tranche-client.ts +187 -0
  183. package/src/transfer-fees.ts +99 -0
  184. package/src/type-aliases.ts +280 -30
  185. package/src/types_delegate.ts +6125 -0
  186. package/src/types_v2.ts +18372 -6121
  187. package/src/virtual-book/index.ts +3 -0
  188. package/src/virtual-book/market.ts +76 -0
  189. package/src/virtual-book/projection.ts +265 -0
  190. package/src/virtual-book/quotes.ts +308 -0
  191. package/src/write.ts +2394 -67
  192. package/src/yield-client.ts +117 -0
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,
@@ -14,24 +31,32 @@ import {
14
31
  deriveLeveragePositionAddress,
15
32
  deriveMarketAddress,
16
33
  deriveMarketCollateralVaultAddress,
17
- deriveMarketFeeVaultAddress,
18
34
  deriveMarketInterestVaultAddress,
19
35
  deriveMarketReserveVaultAddress,
20
- deriveReferralProfileAddress,
36
+ deriveParameterProposalAddress,
37
+ deriveProposalSupportAddress,
38
+ deriveReferralAccrualAddress,
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,10 @@ import type {
42
67
  LeverageDelegation,
43
68
  LeveragePosition,
44
69
  Market,
70
+ ParameterProposal,
71
+ ProposalSupport,
72
+ ReferralAccrual,
73
+ ReferralPartner,
45
74
  YieldAccount,
46
75
  } from "./type-aliases.js";
47
76
  import type { Dusk } from "./types_v2.js";
@@ -52,7 +81,6 @@ export const pda = {
52
81
  tokenMetadata: deriveTokenMetadataAddress,
53
82
  marketReserveVault: deriveMarketReserveVaultAddress,
54
83
  marketCollateralVault: deriveMarketCollateralVaultAddress,
55
- marketFeeVault: deriveMarketFeeVaultAddress,
56
84
  marketInterestVault: deriveMarketInterestVaultAddress,
57
85
  borrowPosition: deriveBorrowPositionAddress,
58
86
  leveragePosition: deriveLeveragePositionAddress,
@@ -60,16 +88,23 @@ export const pda = {
60
88
  yieldTransferHookValidation: deriveYieldTransferHookValidationAddress,
61
89
  hlpYlpVault: deriveHlpYlpVaultAddress,
62
90
  insurance: deriveInsuranceAddress,
63
- referralProfile: deriveReferralProfileAddress,
91
+ referralPartner: deriveReferralPartnerAddress,
92
+ referralAccrual: deriveReferralAccrualAddress,
93
+ parameterProposal: deriveParameterProposalAddress,
94
+ proposalSupport: deriveProposalSupportAddress,
64
95
  } as const;
65
96
 
66
- export interface SimulateOptions {
67
- feePayer?: AddressLike;
68
- commitment?: Commitment;
97
+ export interface PreviewHlpDepositCapacityParams extends SimulateOptions {
98
+ market: AddressLike;
99
+ futarchyAuthority?: AddressLike;
100
+ baseMint: AddressLike;
101
+ quoteMint: AddressLike;
102
+ targetHlpMint: AddressLike;
69
103
  }
70
104
 
71
105
  export interface PreviewSwapParams extends SimulateOptions {
72
106
  market: AddressLike;
107
+ futarchyAuthority?: AddressLike;
73
108
  assetInMint: AddressLike;
74
109
  assetOutMint: AddressLike;
75
110
  exactAssetIn: BN;
@@ -85,32 +120,36 @@ export interface PreviewAddLiquidityParams extends SimulateOptions {
85
120
 
86
121
  export interface PreviewBorrowCapacityParams extends SimulateOptions {
87
122
  market: AddressLike;
88
- futarchyAuthority?: AddressLike;
89
123
  collateralAssetMint: AddressLike;
90
124
  debtAssetMint: AddressLike;
125
+ /** Net collateral amount credited to the vault after any deposit transfer fee. */
91
126
  collateralAmount: BN;
92
127
  /**
93
- * Candidate requested principal used for the returned fee, CF, and health
94
- * fields. When omitted, the program quotes at the final net capacity.
128
+ * Candidate debt amount used for the returned CF and health fields. When
129
+ * omitted, the program quotes at maximum borrow capacity.
95
130
  */
96
131
  projectedBorrowAmount?: BN | null;
97
- withReferral?: boolean;
98
- maxAcceptableReferralFeeBps?: number;
99
132
  }
100
133
 
101
- export interface PreviewBorrowPositionParams extends SimulateOptions {
134
+ export interface PreviewBorrowPositionCapacityParams extends SimulateOptions {
135
+ capacityKind: "borrow" | "withdraw";
102
136
  market: AddressLike;
103
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;
104
144
  }
105
145
 
106
- export class DuskSimulationError extends Error {
107
- constructor(
108
- message: string,
109
- readonly simulation: Awaited<ReturnType<Program<Dusk>["provider"]["connection"]["simulateTransaction"]>>
110
- ) {
111
- super(message);
112
- this.name = "DuskSimulationError";
113
- }
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;
114
153
  }
115
154
 
116
155
  export class DuskGet {
@@ -135,65 +174,163 @@ export class DuskGet {
135
174
  return client.fetch(address(account));
136
175
  }
137
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
+
138
189
  market(account: AddressLike): Promise<Market> {
139
- return this.program.account.market.fetch(address(account));
190
+ return this.programAccount<Market>("market", account);
140
191
  }
141
192
 
142
193
  borrowPosition(account: AddressLike): Promise<BorrowPosition> {
143
- return this.program.account.borrowPosition.fetch(address(account));
194
+ return this.programAccount<BorrowPosition>("borrowPosition", account);
144
195
  }
145
196
 
146
197
  leveragePosition(account: AddressLike): Promise<LeveragePosition> {
147
- 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);
148
208
  }
149
209
 
150
210
  leverageDelegation(account: AddressLike): Promise<LeverageDelegation> {
151
- return this.program.account.leverageDelegation.fetch(address(account));
211
+ return this.programAccount<LeverageDelegation>("leverageDelegation", account);
152
212
  }
153
213
 
154
214
  yieldAccount(account: AddressLike): Promise<YieldAccount> {
155
- return this.program.account.yieldAccount.fetch(address(account));
215
+ return this.programAccount<YieldAccount>("yieldAccount", account);
156
216
  }
157
217
 
158
218
  futarchyAuthority(account: AddressLike = deriveFutarchyAuthorityAddress()[0]): Promise<FutarchyAuthority> {
159
- return this.program.account.futarchyAuthority.fetch(address(account));
219
+ return this.programAccount<FutarchyAuthority>("futarchyAuthority", account);
220
+ }
221
+
222
+ referralPartner(account: AddressLike): Promise<ReferralPartner> {
223
+ return this.programAccount<ReferralPartner>("referralPartner", account);
224
+ }
225
+
226
+ referralAccrual(account: AddressLike): Promise<ReferralAccrual> {
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);
160
262
  }
161
263
 
162
264
  allMarkets() {
163
- return this.program.account.market.all();
265
+ return this.allProgramAccounts<Market>("market");
164
266
  }
165
267
 
166
268
  allBorrowPositions() {
167
- return this.program.account.borrowPosition.all();
269
+ return this.allProgramAccounts<BorrowPosition>("borrowPosition");
168
270
  }
169
271
 
170
272
  allLeveragePositions() {
171
- return this.program.account.leveragePosition.all();
273
+ return this.allProgramAccounts<LeveragePosition>("leveragePosition");
274
+ }
275
+
276
+ allReferralPartners() {
277
+ return this.allProgramAccounts<ReferralPartner>("referralPartner");
278
+ }
279
+
280
+ allReferralAccruals() {
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 });
172
298
  }
173
299
 
174
300
  async previewMarket(market: AddressLike, options: SimulateOptions = {}): Promise<MarketPreview> {
175
- const instruction = await this.program.methods
176
- .previewMarket()
177
- .accounts(normalizeAccountKeys({ market }))
178
- .instruction();
301
+ const instruction = await this.previewInstruction("previewMarket", [], normalizeAccountKeys({ market }));
179
302
 
180
303
  return decodePreviewMarketReturnData(await this.simulateReturnData(instruction, options));
181
304
  }
182
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
+
183
321
  async previewAddLiquidity(params: PreviewAddLiquidityParams): Promise<AddLiquidityPreview> {
184
- const instruction = await this.program.methods
185
- .previewAddLiquidity({
322
+ const instruction = await this.previewInstruction(
323
+ "previewAddLiquidity",
324
+ [{
186
325
  baseDepositAmount: params.baseDepositAmount,
187
326
  quoteDepositAmount: params.quoteDepositAmount,
188
- })
189
- .accounts(
190
- normalizeAccountKeys({
327
+ }],
328
+ normalizeAccountKeys({
191
329
  market: params.market,
192
330
  baseMint: params.baseMint,
193
331
  quoteMint: params.quoteMint,
194
- })
195
- )
196
- .instruction();
332
+ })
333
+ );
197
334
 
198
335
  return decodePreviewAddLiquidityReturnData(
199
336
  await this.simulateReturnData(instruction, params)
@@ -201,18 +338,19 @@ export class DuskGet {
201
338
  }
202
339
 
203
340
  async previewSwap(params: PreviewSwapParams): Promise<SwapPreview> {
204
- const instruction = await this.program.methods
205
- .previewSwap({
341
+ const instruction = await this.previewInstruction(
342
+ "previewSwap",
343
+ [{
206
344
  exactAssetIn: params.exactAssetIn,
207
- })
208
- .accounts(
209
- normalizeAccountKeys({
345
+ }],
346
+ normalizeAccountKeys({
210
347
  market: params.market,
348
+ futarchyAuthority:
349
+ params.futarchyAuthority ?? deriveFutarchyAuthorityAddress()[0],
211
350
  assetInMint: params.assetInMint,
212
351
  assetOutMint: params.assetOutMint,
213
- })
214
- )
215
- .instruction();
352
+ })
353
+ );
216
354
 
217
355
  return decodePreviewSwapReturnData(await this.simulateReturnData(instruction, params));
218
356
  }
@@ -220,60 +358,99 @@ export class DuskGet {
220
358
  async previewBorrowCapacity(
221
359
  params: PreviewBorrowCapacityParams
222
360
  ): Promise<BorrowCapacityPreview> {
223
- const instruction = await this.program.methods
224
- .previewBorrowCapacity({
361
+ const instruction = await this.previewInstruction(
362
+ "previewBorrowCapacity",
363
+ [{
225
364
  collateralAmount: params.collateralAmount,
226
365
  projectedBorrowAmount: params.projectedBorrowAmount ?? null,
227
- withReferral: params.withReferral ?? false,
228
- maxAcceptableReferralFeeBps: params.maxAcceptableReferralFeeBps ?? 0,
229
- })
230
- .accounts(
231
- normalizeAccountKeys({
366
+ }],
367
+ normalizeAccountKeys({
232
368
  market: params.market,
233
- futarchyAuthority: params.futarchyAuthority ?? deriveFutarchyAuthorityAddress()[0],
234
369
  collateralAssetMint: params.collateralAssetMint,
235
370
  debtAssetMint: params.debtAssetMint,
236
- })
237
- )
238
- .instruction();
371
+ })
372
+ );
239
373
 
240
374
  return decodePreviewBorrowCapacityReturnData(await this.simulateReturnData(instruction, params));
241
375
  }
242
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
+
243
398
  async previewBorrowPosition(params: PreviewBorrowPositionParams): Promise<BorrowPositionPreview> {
244
- const instruction = await this.program.methods
245
- .previewBorrowPosition()
246
- .accounts(
247
- normalizeAccountKeys({
399
+ const instruction = await this.previewInstruction(
400
+ "previewBorrowPosition",
401
+ [],
402
+ normalizeAccountKeys({
248
403
  market: params.market,
249
404
  borrowPosition: params.borrowPosition,
250
- })
251
- )
252
- .instruction();
405
+ baseMint: params.baseMint,
406
+ quoteMint: params.quoteMint,
407
+ })
408
+ );
253
409
 
254
410
  return decodePreviewBorrowPositionReturnData(await this.simulateReturnData(instruction, params));
255
411
  }
256
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
+
257
424
  async simulateReturnData(
258
425
  instruction: TransactionInstruction,
259
426
  options: SimulateOptions = {}
260
427
  ): Promise<PreviewReturnData> {
261
- const tx = new Transaction().add(instruction);
262
- tx.feePayer = address(options.feePayer ?? this.program.provider.publicKey ?? this.defaultFeePayer);
263
- tx.recentBlockhash = (
264
- await this.program.provider.connection.getLatestBlockhash(options.commitment)
265
- ).blockhash;
266
-
267
- const simulation = await this.program.provider.connection.simulateTransaction(tx);
268
- if (simulation.value.err) {
269
- throw new DuskSimulationError("Dusk simulation failed", simulation);
270
- }
271
- if (!simulation.value.returnData) {
272
- throw new DuskSimulationError("Dusk simulation did not return data", simulation);
273
- }
274
- if (simulation.value.returnData.programId !== this.program.programId.toBase58()) {
275
- throw new DuskSimulationError("Dusk simulation returned data from a different program", simulation);
276
- }
277
- 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)! };
278
455
  }
279
456
  }