@omnipair/dusk-sdk 2.0.2 → 2.18.0-next.20261106
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +863 -42
- package/dist/amounts.d.ts +9 -0
- package/dist/amounts.d.ts.map +1 -0
- package/dist/amounts.js +10 -0
- package/dist/amounts.js.map +1 -0
- package/dist/constants.d.ts +68 -27
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +153 -41
- package/dist/constants.js.map +1 -1
- package/dist/delegate.d.ts +97 -0
- package/dist/delegate.d.ts.map +1 -0
- package/dist/delegate.js +152 -0
- package/dist/delegate.js.map +1 -0
- package/dist/dusk.d.ts +14 -0
- package/dist/dusk.d.ts.map +1 -1
- package/dist/dusk.js +22 -0
- package/dist/dusk.js.map +1 -1
- package/dist/get.d.ts +3033 -385
- package/dist/get.d.ts.map +1 -1
- package/dist/get.js +109 -72
- package/dist/get.js.map +1 -1
- package/dist/hash.d.ts +9 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +16 -0
- package/dist/hash.js.map +1 -0
- package/dist/hlp-liquidation-client.d.ts +104 -0
- package/dist/hlp-liquidation-client.d.ts.map +1 -0
- package/dist/hlp-liquidation-client.js +146 -0
- package/dist/hlp-liquidation-client.js.map +1 -0
- package/dist/idl_delegate.d.ts +3 -0
- package/dist/idl_delegate.d.ts.map +1 -0
- package/dist/idl_delegate.js +3 -0
- package/dist/idl_delegate.js.map +1 -0
- package/dist/idl_delegate.json +6268 -0
- package/dist/idl_v2.json +16746 -6676
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -1
- package/dist/interest-accounting.d.ts +45 -0
- package/dist/interest-accounting.d.ts.map +1 -0
- package/dist/interest-accounting.js +59 -0
- package/dist/interest-accounting.js.map +1 -0
- package/dist/leverage-actions.d.ts +122 -0
- package/dist/leverage-actions.d.ts.map +1 -0
- package/dist/leverage-actions.js +228 -0
- package/dist/leverage-actions.js.map +1 -0
- package/dist/leverage-margins.d.ts +12 -0
- package/dist/leverage-margins.d.ts.map +1 -0
- package/dist/leverage-margins.js +45 -0
- package/dist/leverage-margins.js.map +1 -0
- package/dist/leverage-preview.d.ts +598 -0
- package/dist/leverage-preview.d.ts.map +1 -0
- package/dist/leverage-preview.js +89 -0
- package/dist/leverage-preview.js.map +1 -0
- package/dist/liquidation-client.d.ts +226 -0
- package/dist/liquidation-client.d.ts.map +1 -0
- package/dist/liquidation-client.js +237 -0
- package/dist/liquidation-client.js.map +1 -0
- package/dist/liquidation.d.ts +72 -0
- package/dist/liquidation.d.ts.map +1 -0
- package/dist/liquidation.js +104 -0
- package/dist/liquidation.js.map +1 -0
- package/dist/lp-naming.d.ts +43 -0
- package/dist/lp-naming.d.ts.map +1 -0
- package/dist/lp-naming.js +81 -0
- package/dist/lp-naming.js.map +1 -0
- package/dist/lp-vanity.d.ts +33 -0
- package/dist/lp-vanity.d.ts.map +1 -0
- package/dist/lp-vanity.js +94 -0
- package/dist/lp-vanity.js.map +1 -0
- package/dist/market-bootstrap.d.ts +109 -0
- package/dist/market-bootstrap.d.ts.map +1 -0
- package/dist/market-bootstrap.js +156 -0
- package/dist/market-bootstrap.js.map +1 -0
- package/dist/market-launch.d.ts +45 -0
- package/dist/market-launch.d.ts.map +1 -0
- package/dist/market-launch.js +76 -0
- package/dist/market-launch.js.map +1 -0
- package/dist/operator-client.d.ts +99 -0
- package/dist/operator-client.d.ts.map +1 -0
- package/dist/operator-client.js +153 -0
- package/dist/operator-client.js.map +1 -0
- package/dist/oracle.d.ts +46 -0
- package/dist/oracle.d.ts.map +1 -0
- package/dist/oracle.js +103 -0
- package/dist/oracle.js.map +1 -0
- package/dist/parameters.d.ts +231 -0
- package/dist/parameters.d.ts.map +1 -0
- package/dist/parameters.js +683 -0
- package/dist/parameters.js.map +1 -0
- package/dist/preview.d.ts +39 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +12 -0
- package/dist/preview.js.map +1 -1
- package/dist/protection.d.ts +48 -0
- package/dist/protection.d.ts.map +1 -0
- package/dist/protection.js +88 -0
- package/dist/protection.js.map +1 -0
- package/dist/risk-pricing.d.ts +12 -0
- package/dist/risk-pricing.d.ts.map +1 -0
- package/dist/risk-pricing.js +23 -0
- package/dist/risk-pricing.js.map +1 -0
- package/dist/simulation.d.ts +45 -0
- package/dist/simulation.d.ts.map +1 -0
- package/dist/simulation.js +106 -0
- package/dist/simulation.js.map +1 -0
- package/dist/tranche-client.d.ts +121 -0
- package/dist/tranche-client.d.ts.map +1 -0
- package/dist/tranche-client.js +185 -0
- package/dist/tranche-client.js.map +1 -0
- package/dist/transfer-fees.d.ts +46 -0
- package/dist/transfer-fees.d.ts.map +1 -0
- package/dist/transfer-fees.js +76 -0
- package/dist/transfer-fees.js.map +1 -0
- package/dist/type-aliases.d.ts +265 -31
- package/dist/type-aliases.d.ts.map +1 -1
- package/dist/types_delegate.d.ts +6275 -0
- package/dist/types_delegate.d.ts.map +1 -0
- package/dist/types_delegate.js +2 -0
- package/dist/types_delegate.js.map +1 -0
- package/dist/types_v2.d.ts +16654 -6584
- package/dist/types_v2.d.ts.map +1 -1
- package/dist/virtual-book/index.d.ts +4 -0
- package/dist/virtual-book/index.d.ts.map +1 -0
- package/dist/virtual-book/index.js +4 -0
- package/dist/virtual-book/index.js.map +1 -0
- package/dist/virtual-book/market.d.ts +619 -0
- package/dist/virtual-book/market.d.ts.map +1 -0
- package/dist/virtual-book/market.js +59 -0
- package/dist/virtual-book/market.js.map +1 -0
- package/dist/virtual-book/projection.d.ts +44 -0
- package/dist/virtual-book/projection.d.ts.map +1 -0
- package/dist/virtual-book/projection.js +229 -0
- package/dist/virtual-book/projection.js.map +1 -0
- package/dist/virtual-book/quotes.d.ts +1263 -0
- package/dist/virtual-book/quotes.d.ts.map +1 -0
- package/dist/virtual-book/quotes.js +233 -0
- package/dist/virtual-book/quotes.js.map +1 -0
- package/dist/write.d.ts +427 -0
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +970 -26
- package/dist/write.js.map +1 -1
- package/dist/yield-client.d.ts +51 -0
- package/dist/yield-client.d.ts.map +1 -0
- package/dist/yield-client.js +98 -0
- package/dist/yield-client.js.map +1 -0
- package/package.json +11 -4
- package/src/amounts.ts +13 -0
- package/src/constants.ts +220 -46
- package/src/delegate.ts +259 -0
- package/src/dusk.ts +28 -0
- package/src/get.ts +201 -86
- package/src/hash.ts +15 -0
- package/src/hlp-liquidation-client.ts +189 -0
- package/src/idl_delegate.json +6268 -0
- package/src/idl_delegate.ts +3 -0
- package/src/idl_v2.json +16746 -6676
- package/src/index.ts +31 -0
- package/src/interest-accounting.ts +73 -0
- package/src/leverage-actions.ts +216 -0
- package/src/leverage-margins.ts +48 -0
- package/src/leverage-preview.ts +86 -0
- package/src/liquidation-client.ts +295 -0
- package/src/liquidation.ts +119 -0
- package/src/lp-naming.ts +127 -0
- package/src/lp-vanity.ts +135 -0
- package/src/market-bootstrap.ts +271 -0
- package/src/market-launch.ts +103 -0
- package/src/operator-client.ts +169 -0
- package/src/oracle.ts +120 -0
- package/src/parameters.ts +1019 -0
- package/src/preview.ts +53 -1
- package/src/protection.ts +112 -0
- package/src/risk-pricing.ts +26 -0
- package/src/simulation.ts +169 -0
- package/src/tranche-client.ts +187 -0
- package/src/transfer-fees.ts +99 -0
- package/src/type-aliases.ts +294 -32
- package/src/types_delegate.ts +6274 -0
- package/src/types_v2.ts +16745 -6675
- package/src/virtual-book/index.ts +3 -0
- package/src/virtual-book/market.ts +76 -0
- package/src/virtual-book/projection.ts +265 -0
- package/src/virtual-book/quotes.ts +308 -0
- package/src/write.ts +1650 -28
- package/src/yield-client.ts +117 -0
package/src/get.ts
CHANGED
|
@@ -1,16 +1,32 @@
|
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
} from "
|
|
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,
|
|
11
28
|
deriveFutarchyAuthorityAddress,
|
|
12
|
-
|
|
13
|
-
deriveInsuranceAddress,
|
|
29
|
+
deriveHlpSrlpVaultAddress,
|
|
14
30
|
deriveLeveragePositionAddress,
|
|
15
31
|
deriveMarketAddress,
|
|
16
32
|
deriveMarketCollateralVaultAddress,
|
|
@@ -21,17 +37,23 @@ import {
|
|
|
21
37
|
deriveTokenMetadataAddress,
|
|
22
38
|
deriveYieldAccountAddress,
|
|
23
39
|
deriveYieldTransferHookValidationAddress,
|
|
40
|
+
type U64SeedLike,
|
|
24
41
|
} from "./constants.js";
|
|
25
42
|
import { address, DEFAULT_READONLY_PUBLIC_KEY, normalizeAccountKeys, type AddressLike } from "./address.js";
|
|
43
|
+
import { getStoredLeverageMargins } from "./leverage-margins.js";
|
|
26
44
|
import {
|
|
45
|
+
decodePreviewHlpDepositCapacityReturnData,
|
|
46
|
+
type HlpDepositCapacityPreview,
|
|
27
47
|
decodePreviewAddLiquidityReturnData,
|
|
28
48
|
decodePreviewBorrowCapacityReturnData,
|
|
29
49
|
decodePreviewBorrowPositionReturnData,
|
|
50
|
+
decodePreviewBorrowPositionCapacityReturnData,
|
|
30
51
|
decodePreviewMarketReturnData,
|
|
31
52
|
decodePreviewSwapReturnData,
|
|
32
53
|
type AddLiquidityPreview,
|
|
33
54
|
type BorrowCapacityPreview,
|
|
34
55
|
type BorrowPositionPreview,
|
|
56
|
+
type BorrowPositionCapacityPreview,
|
|
35
57
|
type MarketPreview,
|
|
36
58
|
type PreviewReturnData,
|
|
37
59
|
type SwapPreview,
|
|
@@ -59,19 +81,22 @@ export const pda = {
|
|
|
59
81
|
leveragePosition: deriveLeveragePositionAddress,
|
|
60
82
|
yieldAccount: deriveYieldAccountAddress,
|
|
61
83
|
yieldTransferHookValidation: deriveYieldTransferHookValidationAddress,
|
|
62
|
-
|
|
63
|
-
insurance: deriveInsuranceAddress,
|
|
84
|
+
hlpSrlpVault: deriveHlpSrlpVaultAddress,
|
|
64
85
|
referralPartner: deriveReferralPartnerAddress,
|
|
65
86
|
referralAccrual: deriveReferralAccrualAddress,
|
|
66
87
|
} as const;
|
|
67
88
|
|
|
68
|
-
export interface SimulateOptions {
|
|
69
|
-
|
|
70
|
-
|
|
89
|
+
export interface PreviewHlpDepositCapacityParams extends SimulateOptions {
|
|
90
|
+
market: AddressLike;
|
|
91
|
+
futarchyAuthority?: AddressLike;
|
|
92
|
+
baseMint: AddressLike;
|
|
93
|
+
quoteMint: AddressLike;
|
|
94
|
+
targetHlpMint: AddressLike;
|
|
71
95
|
}
|
|
72
96
|
|
|
73
97
|
export interface PreviewSwapParams extends SimulateOptions {
|
|
74
98
|
market: AddressLike;
|
|
99
|
+
futarchyAuthority?: AddressLike;
|
|
75
100
|
assetInMint: AddressLike;
|
|
76
101
|
assetOutMint: AddressLike;
|
|
77
102
|
exactAssetIn: BN;
|
|
@@ -89,6 +114,7 @@ export interface PreviewBorrowCapacityParams extends SimulateOptions {
|
|
|
89
114
|
market: AddressLike;
|
|
90
115
|
collateralAssetMint: AddressLike;
|
|
91
116
|
debtAssetMint: AddressLike;
|
|
117
|
+
/** Net collateral amount credited to the vault after any deposit transfer fee. */
|
|
92
118
|
collateralAmount: BN;
|
|
93
119
|
/**
|
|
94
120
|
* Candidate debt amount used for the returned CF and health fields. When
|
|
@@ -97,19 +123,25 @@ export interface PreviewBorrowCapacityParams extends SimulateOptions {
|
|
|
97
123
|
projectedBorrowAmount?: BN | null;
|
|
98
124
|
}
|
|
99
125
|
|
|
100
|
-
export interface
|
|
126
|
+
export interface PreviewBorrowPositionCapacityParams extends SimulateOptions {
|
|
127
|
+
capacityKind: "borrow" | "withdraw";
|
|
101
128
|
market: AddressLike;
|
|
102
129
|
borrowPosition: AddressLike;
|
|
130
|
+
collateralAssetMint: AddressLike;
|
|
131
|
+
debtAssetMint: AddressLike;
|
|
132
|
+
/** Net credit after transfer fees, or negative collateral vault debit. */
|
|
133
|
+
collateralChange: BN;
|
|
134
|
+
/** Additional debt; omit for maximum additional capacity. */
|
|
135
|
+
projectedBorrowAmount?: BN | null;
|
|
103
136
|
}
|
|
104
137
|
|
|
105
|
-
export
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
138
|
+
export interface PreviewBorrowPositionParams extends SimulateOptions {
|
|
139
|
+
market: AddressLike;
|
|
140
|
+
borrowPosition: AddressLike;
|
|
141
|
+
/** Market base mint; liquidation fields value collateral after its transfer fee. */
|
|
142
|
+
baseMint: AddressLike;
|
|
143
|
+
/** Market quote mint; liquidation fields value collateral after its transfer fee. */
|
|
144
|
+
quoteMint: AddressLike;
|
|
113
145
|
}
|
|
114
146
|
|
|
115
147
|
export class DuskGet {
|
|
@@ -134,81 +166,121 @@ export class DuskGet {
|
|
|
134
166
|
return client.fetch(address(account));
|
|
135
167
|
}
|
|
136
168
|
|
|
169
|
+
private allProgramAccounts<T>(name: string): Promise<Array<{ publicKey: PublicKey; account: T }>> {
|
|
170
|
+
const client = (
|
|
171
|
+
this.program as unknown as {
|
|
172
|
+
account: Record<string, { all(): Promise<Array<{ publicKey: PublicKey; account: T }>> }>;
|
|
173
|
+
}
|
|
174
|
+
).account[name];
|
|
175
|
+
if (!client) {
|
|
176
|
+
throw new Error(`Unknown Dusk account type: ${name}`);
|
|
177
|
+
}
|
|
178
|
+
return client.all();
|
|
179
|
+
}
|
|
180
|
+
|
|
137
181
|
market(account: AddressLike): Promise<Market> {
|
|
138
|
-
return this.
|
|
182
|
+
return this.programAccount<Market>("market", account);
|
|
139
183
|
}
|
|
140
184
|
|
|
141
185
|
borrowPosition(account: AddressLike): Promise<BorrowPosition> {
|
|
142
|
-
return this.
|
|
186
|
+
return this.programAccount<BorrowPosition>("borrowPosition", account);
|
|
143
187
|
}
|
|
144
188
|
|
|
145
189
|
leveragePosition(account: AddressLike): Promise<LeveragePosition> {
|
|
146
|
-
return this.
|
|
190
|
+
return this.programAccount<LeveragePosition>("leveragePosition", account);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Fetch saved margin terms; does not quote an opening or an increase. */
|
|
194
|
+
async leverageMargins(account: AddressLike) {
|
|
195
|
+
return getStoredLeverageMargins(await this.leveragePosition(account));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
previewLeverageAction(params: PreviewLeverageActionParams) {
|
|
199
|
+
return previewLeverageAction(this.program, params);
|
|
147
200
|
}
|
|
148
201
|
|
|
149
202
|
leverageDelegation(account: AddressLike): Promise<LeverageDelegation> {
|
|
150
|
-
return this.
|
|
203
|
+
return this.programAccount<LeverageDelegation>("leverageDelegation", account);
|
|
151
204
|
}
|
|
152
205
|
|
|
153
206
|
yieldAccount(account: AddressLike): Promise<YieldAccount> {
|
|
154
|
-
return this.
|
|
207
|
+
return this.programAccount<YieldAccount>("yieldAccount", account);
|
|
155
208
|
}
|
|
156
209
|
|
|
157
210
|
futarchyAuthority(account: AddressLike = deriveFutarchyAuthorityAddress()[0]): Promise<FutarchyAuthority> {
|
|
158
|
-
return this.
|
|
211
|
+
return this.programAccount<FutarchyAuthority>("futarchyAuthority", account);
|
|
159
212
|
}
|
|
160
213
|
|
|
161
214
|
referralPartner(account: AddressLike): Promise<ReferralPartner> {
|
|
162
|
-
return this.
|
|
215
|
+
return this.programAccount<ReferralPartner>("referralPartner", account);
|
|
163
216
|
}
|
|
164
217
|
|
|
165
218
|
referralAccrual(account: AddressLike): Promise<ReferralAccrual> {
|
|
166
|
-
return this.
|
|
219
|
+
return this.programAccount<ReferralAccrual>("referralAccrual", account);
|
|
167
220
|
}
|
|
168
221
|
|
|
169
222
|
allMarkets() {
|
|
170
|
-
return this.
|
|
223
|
+
return this.allProgramAccounts<Market>("market");
|
|
171
224
|
}
|
|
172
225
|
|
|
173
226
|
allBorrowPositions() {
|
|
174
|
-
return this.
|
|
227
|
+
return this.allProgramAccounts<BorrowPosition>("borrowPosition");
|
|
175
228
|
}
|
|
176
229
|
|
|
177
230
|
allLeveragePositions() {
|
|
178
|
-
return this.
|
|
231
|
+
return this.allProgramAccounts<LeveragePosition>("leveragePosition");
|
|
179
232
|
}
|
|
180
233
|
|
|
181
234
|
allReferralPartners() {
|
|
182
|
-
return this.
|
|
235
|
+
return this.allProgramAccounts<ReferralPartner>("referralPartner");
|
|
183
236
|
}
|
|
184
237
|
|
|
185
238
|
allReferralAccruals() {
|
|
186
|
-
return this.
|
|
239
|
+
return this.allProgramAccounts<ReferralAccrual>("referralAccrual");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private previewInstruction(
|
|
243
|
+
name: string,
|
|
244
|
+
args: unknown[],
|
|
245
|
+
accounts: Record<string, unknown>
|
|
246
|
+
): Promise<TransactionInstruction> {
|
|
247
|
+
return new DuskWrite(this.program).instruction(name as DuskInstructionName, args, { accounts });
|
|
187
248
|
}
|
|
188
249
|
|
|
189
250
|
async previewMarket(market: AddressLike, options: SimulateOptions = {}): Promise<MarketPreview> {
|
|
190
|
-
const instruction = await this.
|
|
191
|
-
.previewMarket()
|
|
192
|
-
.accounts(normalizeAccountKeys({ market }))
|
|
193
|
-
.instruction();
|
|
251
|
+
const instruction = await this.previewInstruction("previewMarket", [], normalizeAccountKeys({ market }));
|
|
194
252
|
|
|
195
253
|
return decodePreviewMarketReturnData(await this.simulateReturnData(instruction, options));
|
|
196
254
|
}
|
|
197
255
|
|
|
256
|
+
/** Current full-value hedge admission and funding ceiling, including transfer fees.
|
|
257
|
+
* A Ready status is not an amount-specific execution guarantee; simulate the
|
|
258
|
+
* completed deposit transaction to check rounding, slippage and custody.
|
|
259
|
+
*/
|
|
260
|
+
async previewHlpDepositCapacity(params: PreviewHlpDepositCapacityParams): Promise<HlpDepositCapacityPreview> {
|
|
261
|
+
const instruction = await this.previewInstruction("previewHlpDepositCapacity", [], normalizeAccountKeys({
|
|
262
|
+
market: params.market,
|
|
263
|
+
futarchyAuthority: params.futarchyAuthority ?? deriveFutarchyAuthorityAddress(this.program.programId)[0],
|
|
264
|
+
baseMint: params.baseMint,
|
|
265
|
+
quoteMint: params.quoteMint,
|
|
266
|
+
targetHlpMint: params.targetHlpMint,
|
|
267
|
+
}));
|
|
268
|
+
return decodePreviewHlpDepositCapacityReturnData(await this.simulateReturnData(instruction, params));
|
|
269
|
+
}
|
|
270
|
+
|
|
198
271
|
async previewAddLiquidity(params: PreviewAddLiquidityParams): Promise<AddLiquidityPreview> {
|
|
199
|
-
const instruction = await this.
|
|
200
|
-
|
|
272
|
+
const instruction = await this.previewInstruction(
|
|
273
|
+
"previewAddLiquidity",
|
|
274
|
+
[{
|
|
201
275
|
baseDepositAmount: params.baseDepositAmount,
|
|
202
276
|
quoteDepositAmount: params.quoteDepositAmount,
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
normalizeAccountKeys({
|
|
277
|
+
}],
|
|
278
|
+
normalizeAccountKeys({
|
|
206
279
|
market: params.market,
|
|
207
280
|
baseMint: params.baseMint,
|
|
208
281
|
quoteMint: params.quoteMint,
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
.instruction();
|
|
282
|
+
})
|
|
283
|
+
);
|
|
212
284
|
|
|
213
285
|
return decodePreviewAddLiquidityReturnData(
|
|
214
286
|
await this.simulateReturnData(instruction, params)
|
|
@@ -216,18 +288,19 @@ export class DuskGet {
|
|
|
216
288
|
}
|
|
217
289
|
|
|
218
290
|
async previewSwap(params: PreviewSwapParams): Promise<SwapPreview> {
|
|
219
|
-
const instruction = await this.
|
|
220
|
-
|
|
291
|
+
const instruction = await this.previewInstruction(
|
|
292
|
+
"previewSwap",
|
|
293
|
+
[{
|
|
221
294
|
exactAssetIn: params.exactAssetIn,
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
normalizeAccountKeys({
|
|
295
|
+
}],
|
|
296
|
+
normalizeAccountKeys({
|
|
225
297
|
market: params.market,
|
|
298
|
+
futarchyAuthority:
|
|
299
|
+
params.futarchyAuthority ?? deriveFutarchyAuthorityAddress()[0],
|
|
226
300
|
assetInMint: params.assetInMint,
|
|
227
301
|
assetOutMint: params.assetOutMint,
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
.instruction();
|
|
302
|
+
})
|
|
303
|
+
);
|
|
231
304
|
|
|
232
305
|
return decodePreviewSwapReturnData(await this.simulateReturnData(instruction, params));
|
|
233
306
|
}
|
|
@@ -235,57 +308,99 @@ export class DuskGet {
|
|
|
235
308
|
async previewBorrowCapacity(
|
|
236
309
|
params: PreviewBorrowCapacityParams
|
|
237
310
|
): Promise<BorrowCapacityPreview> {
|
|
238
|
-
const instruction = await this.
|
|
239
|
-
|
|
311
|
+
const instruction = await this.previewInstruction(
|
|
312
|
+
"previewBorrowCapacity",
|
|
313
|
+
[{
|
|
240
314
|
collateralAmount: params.collateralAmount,
|
|
241
315
|
projectedBorrowAmount: params.projectedBorrowAmount ?? null,
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
normalizeAccountKeys({
|
|
316
|
+
}],
|
|
317
|
+
normalizeAccountKeys({
|
|
245
318
|
market: params.market,
|
|
246
319
|
collateralAssetMint: params.collateralAssetMint,
|
|
247
320
|
debtAssetMint: params.debtAssetMint,
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
.instruction();
|
|
321
|
+
})
|
|
322
|
+
);
|
|
251
323
|
|
|
252
324
|
return decodePreviewBorrowCapacityReturnData(await this.simulateReturnData(instruction, params));
|
|
253
325
|
}
|
|
254
326
|
|
|
327
|
+
async previewBorrowPositionCapacity(
|
|
328
|
+
params: PreviewBorrowPositionCapacityParams
|
|
329
|
+
): Promise<BorrowPositionCapacityPreview> {
|
|
330
|
+
const instruction = await this.previewInstruction(
|
|
331
|
+
"previewBorrowPositionCapacity",
|
|
332
|
+
[{
|
|
333
|
+
capacityKind: params.capacityKind === "borrow" ? { borrow: {} } : { withdraw: {} },
|
|
334
|
+
collateralChange: params.collateralChange,
|
|
335
|
+
projectedBorrowAmount: params.projectedBorrowAmount ?? null,
|
|
336
|
+
}],
|
|
337
|
+
normalizeAccountKeys({
|
|
338
|
+
market: params.market,
|
|
339
|
+
borrowPosition: params.borrowPosition,
|
|
340
|
+
collateralAssetMint: params.collateralAssetMint,
|
|
341
|
+
debtAssetMint: params.debtAssetMint,
|
|
342
|
+
})
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
return decodePreviewBorrowPositionCapacityReturnData(await this.simulateReturnData(instruction, params));
|
|
346
|
+
}
|
|
347
|
+
|
|
255
348
|
async previewBorrowPosition(params: PreviewBorrowPositionParams): Promise<BorrowPositionPreview> {
|
|
256
|
-
const instruction = await this.
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
349
|
+
const instruction = await this.previewInstruction(
|
|
350
|
+
"previewBorrowPosition",
|
|
351
|
+
[],
|
|
352
|
+
normalizeAccountKeys({
|
|
260
353
|
market: params.market,
|
|
261
354
|
borrowPosition: params.borrowPosition,
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
355
|
+
baseMint: params.baseMint,
|
|
356
|
+
quoteMint: params.quoteMint,
|
|
357
|
+
})
|
|
358
|
+
);
|
|
265
359
|
|
|
266
360
|
return decodePreviewBorrowPositionReturnData(await this.simulateReturnData(instruction, params));
|
|
267
361
|
}
|
|
268
362
|
|
|
363
|
+
/** Simulate one or more instructions while retaining bank and account provenance. */
|
|
364
|
+
simulateWithContext(
|
|
365
|
+
instructions: readonly TransactionInstruction[],
|
|
366
|
+
options: PreviewSimulationOptions = {}
|
|
367
|
+
) {
|
|
368
|
+
return simulatePreviewWithContext(this.program, instructions, {
|
|
369
|
+
...options,
|
|
370
|
+
feePayer: options.feePayer ?? this.program.provider.publicKey ?? this.defaultFeePayer,
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
269
374
|
async simulateReturnData(
|
|
270
375
|
instruction: TransactionInstruction,
|
|
271
376
|
options: SimulateOptions = {}
|
|
272
377
|
): Promise<PreviewReturnData> {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
)
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
378
|
+
return (await this.simulateWithContext([instruction], options)).value.returnData!;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
previewVirtualBookSnapshot(market: AddressLike, options: PreviewSimulationOptions = {}) {
|
|
382
|
+
return previewVirtualBookSnapshot(this.program, market, options);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
previewVirtualBookBatch(
|
|
386
|
+
snapshot: DuskVirtualBookSnapshot,
|
|
387
|
+
requests: VirtualBookQuoteRequest[],
|
|
388
|
+
options: PreviewSimulationOptions = {}
|
|
389
|
+
) {
|
|
390
|
+
return previewVirtualBookBatch(this.program, snapshot, requests, options);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
previewVirtualBookQuotes(
|
|
394
|
+
snapshot: DuskVirtualBookSnapshot,
|
|
395
|
+
options: VirtualBookQuoteOptions = {}
|
|
396
|
+
) {
|
|
397
|
+
return previewVirtualBookQuotes(this.program, snapshot, options);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Fee-inclusive display depth. Transaction quotes must still use the exact requested input. */
|
|
401
|
+
async previewVirtualBook(market: AddressLike, options: VirtualBookQuoteOptions = {}) {
|
|
402
|
+
const snapshot = await this.previewVirtualBookSnapshot(market, options);
|
|
403
|
+
const quotes = await this.previewVirtualBookQuotes(snapshot, options);
|
|
404
|
+
return { ...quotes, book: projectDuskVirtualBook(quotes)! };
|
|
290
405
|
}
|
|
291
406
|
}
|
package/src/hash.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-256 that works in a browser and in Node.
|
|
3
|
+
*
|
|
4
|
+
* WebCrypto is the only digest a browser bundle can rely on and it is async,
|
|
5
|
+
* so every caller of this is async too. `node:crypto` is imported lazily so a
|
|
6
|
+
* bundler targeting the browser never has to resolve it.
|
|
7
|
+
*/
|
|
8
|
+
export async function sha256(bytes: Uint8Array): Promise<Uint8Array> {
|
|
9
|
+
if (globalThis.crypto?.subtle) {
|
|
10
|
+
const stableBytes = Uint8Array.from(bytes);
|
|
11
|
+
return new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", stableBytes));
|
|
12
|
+
}
|
|
13
|
+
const { createHash } = await import("node:crypto");
|
|
14
|
+
return Uint8Array.from(createHash("sha256").update(bytes).digest());
|
|
15
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { Program } from "@coral-xyz/anchor";
|
|
2
|
+
import { createAssociatedTokenAccountIdempotentInstruction, getAssociatedTokenAddressSync, TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
|
|
3
|
+
import { ComputeBudgetProgram, PublicKey, SystemProgram, SYSVAR_INSTRUCTIONS_PUBKEY, type AccountMeta, type TransactionInstruction } from "@solana/web3.js";
|
|
4
|
+
import { address, type AddressLike } from "./address.js";
|
|
5
|
+
import { deriveFutarchyAuthorityAddress } from "./constants.js";
|
|
6
|
+
import { DuskGet } from "./get.js";
|
|
7
|
+
import { integerBN, type IntegerLike } from "./parameters.js";
|
|
8
|
+
import { previewReturnDataBytes } from "./preview.js";
|
|
9
|
+
import { tokenProgramForMint } from "./referral.js";
|
|
10
|
+
import type { Dusk as DuskIdl } from "./types_v2.js";
|
|
11
|
+
import type { HlpFlashQuote } from "./type-aliases.js";
|
|
12
|
+
import { DuskWrite, type HlpLiquidityBuildOptions } from "./write.js";
|
|
13
|
+
|
|
14
|
+
export type { HlpFlashQuote };
|
|
15
|
+
export interface HlpRecoveryParams { market: AddressLike; targetAsset: "base" | "quote" }
|
|
16
|
+
export interface HlpFlashRouteContext {
|
|
17
|
+
quote: HlpFlashQuote;
|
|
18
|
+
session: PublicKey;
|
|
19
|
+
repaymentVault: PublicKey;
|
|
20
|
+
buyerCollateralAccount: PublicKey;
|
|
21
|
+
buyerDebtAccount: PublicKey;
|
|
22
|
+
collateralMint: PublicKey;
|
|
23
|
+
debtMint: PublicKey;
|
|
24
|
+
collateralTokenProgram: PublicKey;
|
|
25
|
+
debtTokenProgram: PublicKey;
|
|
26
|
+
/** Net spendable atoms required in repaymentVault. A route must gross up
|
|
27
|
+
* its incoming transfer; this already includes the vault-to-reserve fee. */
|
|
28
|
+
requiredPaymentCredit: bigint;
|
|
29
|
+
}
|
|
30
|
+
export interface HlpFlashParams extends HlpRecoveryParams {
|
|
31
|
+
buyer: AddressLike;
|
|
32
|
+
/** Explicit opt-in to closing an insolvent vault's entire backing. */
|
|
33
|
+
allowFull?: boolean;
|
|
34
|
+
maxPayment?: IntegerLike;
|
|
35
|
+
maxCollateralDebit?: IntegerLike;
|
|
36
|
+
minCollateralCredit?: IntegerLike;
|
|
37
|
+
maxSocializedLoss?: IntegerLike;
|
|
38
|
+
prefixInstructions?: readonly TransactionInstruction[];
|
|
39
|
+
settleRemainingAccounts?: readonly AccountMeta[];
|
|
40
|
+
route: (context: HlpFlashRouteContext) => Promise<readonly TransactionInstruction[]> | readonly TransactionInstruction[];
|
|
41
|
+
/** Append an owner-signed withdrawal after funded recovery or terminal
|
|
42
|
+
* settlement. A terminal close pays zero principal. */
|
|
43
|
+
exit?: HlpRecoveryExit;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface HlpRecoveryExit {
|
|
47
|
+
hlpAmount: IntegerLike;
|
|
48
|
+
minTargetAmountOut?: IntegerLike;
|
|
49
|
+
options: HlpLiquidityBuildOptions;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface HlpNativeCloseAndExitParams extends HlpRecoveryParams {
|
|
53
|
+
exit: HlpRecoveryExit;
|
|
54
|
+
maxSocializedLoss?: IntegerLike;
|
|
55
|
+
minCallerBountyOut?: IntegerLike;
|
|
56
|
+
closeRemainingAccounts?: readonly AccountMeta[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Atomic vault recovery. A solvent funding gap can be rescued before the
|
|
60
|
+
* native critical-stress boundary; a full quote still requires insolvency.
|
|
61
|
+
* Keeps a fixed payment obligation and permits an external route or inventory
|
|
62
|
+
* payment. A direct Dusk swap may be used when the complete transaction fits
|
|
63
|
+
* Solana's compute limit; callers must simulate their chosen route. Native
|
|
64
|
+
* Dusk recovery remains available through an ordinary swap. Never signs or
|
|
65
|
+
* submits. */
|
|
66
|
+
export class DuskHlpLiquidations {
|
|
67
|
+
readonly write: DuskWrite;
|
|
68
|
+
readonly get: DuskGet;
|
|
69
|
+
constructor(readonly program: Program<DuskIdl>, feePayer?: AddressLike) {
|
|
70
|
+
this.write = new DuskWrite(program);
|
|
71
|
+
this.get = new DuskGet(program, feePayer);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private async resolve(params: HlpRecoveryParams) {
|
|
75
|
+
if (params.targetAsset !== "base" && params.targetAsset !== "quote") throw new Error("Invalid hLP target asset");
|
|
76
|
+
const market = address(params.market), target = params.targetAsset === "base" ? 0 : 1;
|
|
77
|
+
const state = await this.get.market(market);
|
|
78
|
+
const collateral = target === 0 ? state.baseSide : state.quoteSide;
|
|
79
|
+
const debt = target === 0 ? state.quoteSide : state.baseSide;
|
|
80
|
+
const futarchyAuthority = deriveFutarchyAuthorityAddress(this.program.programId)[0];
|
|
81
|
+
return { market, target, state, collateral, debt, futarchyAuthority };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Uses the program's full-value eligibility, EMA payment quote, transfer
|
|
85
|
+
* fees and affordable incentive. A positive funding gap or frozen vault can
|
|
86
|
+
* quote a solvent rescue; inspect quote.full before building a transaction.
|
|
87
|
+
* It does not reserve liquidity or payment. */
|
|
88
|
+
async preview(params: HlpRecoveryParams): Promise<HlpFlashQuote> {
|
|
89
|
+
const r = await this.resolve(params);
|
|
90
|
+
const instruction = await this.write.instruction("previewHlpFlash", [r.target], {
|
|
91
|
+
accounts: { market: r.market, futarchyAuthority: r.futarchyAuthority, targetMint: r.collateral.assetMint, borrowedMint: r.debt.assetMint },
|
|
92
|
+
});
|
|
93
|
+
return this.program.coder.types.decode("hlpFlashQuote", previewReturnDataBytes(await this.get.simulateReturnData(instruction))) as HlpFlashQuote;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async isFrozen(params: HlpRecoveryParams): Promise<boolean> {
|
|
97
|
+
const r = await this.resolve(params);
|
|
98
|
+
return (r.state.amm.frozenHlpMask & (r.target === 0 ? 1 : 2)) !== 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private async exitInstruction(r: Awaited<ReturnType<DuskHlpLiquidations["resolve"]>>, exit: HlpRecoveryExit) {
|
|
102
|
+
if (!address(exit.options.market).equals(r.market)) throw new Error("Exit market differs from liquidation market");
|
|
103
|
+
if (!address(exit.options.targetHlpMint).equals(r.collateral.hlpMint)) throw new Error("Exit hLP mint differs from liquidation target");
|
|
104
|
+
if (!address(exit.options.baseMint).equals(r.state.baseSide.assetMint) ||
|
|
105
|
+
!address(exit.options.quoteMint).equals(r.state.quoteSide.assetMint)) throw new Error("Exit asset mints differ from market");
|
|
106
|
+
const built = await this.write.withdrawSingleSided({
|
|
107
|
+
hlpAmount: integerBN(exit.hlpAmount, "hlpAmount"),
|
|
108
|
+
minTargetAmountOut: integerBN(exit.minTargetAmountOut ?? 0, "minTargetAmountOut"),
|
|
109
|
+
}, exit.options);
|
|
110
|
+
return built;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** One signed transaction: permissionless native junior/senior loss close,
|
|
114
|
+
* then the holder burns receipts. Eligibility uses current-slot symmetric
|
|
115
|
+
* EMA health; native recovery uses EMA price, smoothed depth, concentration,
|
|
116
|
+
* and the configured fee schedule. The receipt burn pays zero principal. */
|
|
117
|
+
async buildNativeCloseAndExit(params: HlpNativeCloseAndExitParams) {
|
|
118
|
+
const r = await this.resolve(params);
|
|
119
|
+
const exit = await this.exitInstruction(r, params.exit);
|
|
120
|
+
const caller = address(params.exit.options.owner);
|
|
121
|
+
const debtTokenProgram = await tokenProgramForMint(this.program.provider.connection, r.debt.assetMint);
|
|
122
|
+
const callerBountyAccount = getAssociatedTokenAddressSync(r.debt.assetMint, caller, true, debtTokenProgram);
|
|
123
|
+
const targetVault = r.target === 0 ? r.state.baseHlpVault : r.state.quoteHlpVault;
|
|
124
|
+
const close = await this.write.instruction("closeInsolventHlp", {
|
|
125
|
+
targetAsset: r.target,
|
|
126
|
+
maxSocializedLoss: integerBN(params.maxSocializedLoss ?? 0xffffffffffffffffn, "maxSocializedLoss"),
|
|
127
|
+
minCallerBountyOut: integerBN(params.minCallerBountyOut ?? 0, "minCallerBountyOut"),
|
|
128
|
+
}, { accounts: {
|
|
129
|
+
market: r.market, futarchyAuthority: r.futarchyAuthority, caller,
|
|
130
|
+
borrowedMint: r.debt.assetMint, borrowedReserveVault: r.debt.reserveVault,
|
|
131
|
+
borrowedInterestVault: r.debt.interestVault,
|
|
132
|
+
callerBountyAccount, srlpMint: r.state.srlpMint, targetHlpSrlpVault: targetVault.srlpVault,
|
|
133
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
134
|
+
}, remainingAccounts: params.closeRemainingAccounts ? [...params.closeRemainingAccounts] : undefined });
|
|
135
|
+
const instructions = [
|
|
136
|
+
createAssociatedTokenAccountIdempotentInstruction(caller, callerBountyAccount, caller, r.debt.assetMint, debtTokenProgram),
|
|
137
|
+
...exit.setupInstructions, close, exit.actionInstruction,
|
|
138
|
+
];
|
|
139
|
+
return { closeInstruction: close, exitInstruction: exit.actionInstruction, instructions };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async buildFlash(params: HlpFlashParams) {
|
|
143
|
+
if (typeof params.route !== "function") throw new Error("An explicit payment route is required");
|
|
144
|
+
const r = await this.resolve(params), buyer = address(params.buyer);
|
|
145
|
+
const [quote, collateralTokenProgram, debtTokenProgram] = await Promise.all([
|
|
146
|
+
this.preview(params), tokenProgramForMint(this.program.provider.connection, r.collateral.assetMint),
|
|
147
|
+
tokenProgramForMint(this.program.provider.connection, r.debt.assetMint),
|
|
148
|
+
]);
|
|
149
|
+
if (quote.full && !params.allowFull) throw new Error("This vault needs full recovery; set allowFull explicitly");
|
|
150
|
+
const exit = params.exit ? await this.exitInstruction(r, params.exit) : undefined;
|
|
151
|
+
const [session] = PublicKey.findProgramAddressSync([Buffer.from("hlp-flash-session"), r.market.toBuffer(), Buffer.from([r.target])], this.program.programId);
|
|
152
|
+
const [repaymentVault] = PublicKey.findProgramAddressSync([Buffer.from("hlp-flash-payment"), session.toBuffer()], this.program.programId);
|
|
153
|
+
const buyerCollateralAccount = getAssociatedTokenAddressSync(r.collateral.assetMint, buyer, true, collateralTokenProgram);
|
|
154
|
+
const buyerDebtAccount = getAssociatedTokenAddressSync(r.debt.assetMint, buyer, true, debtTokenProgram);
|
|
155
|
+
const prefix = [...(params.prefixInstructions ?? []), ...(exit?.setupInstructions ?? [])];
|
|
156
|
+
for (const [tag, instruction] of [[2, ComputeBudgetProgram.setComputeUnitLimit({ units: 1_400_000 })],
|
|
157
|
+
[1, ComputeBudgetProgram.requestHeapFrame({ bytes: 256 * 1024 })]] as const) {
|
|
158
|
+
const matching = prefix.filter(ix => ix.programId.equals(ComputeBudgetProgram.programId) && ix.data[0] === tag);
|
|
159
|
+
if (matching.length > 1) throw new Error("Duplicate compute budget instruction");
|
|
160
|
+
if (!matching.length) prefix.unshift(instruction);
|
|
161
|
+
}
|
|
162
|
+
prefix.push(createAssociatedTokenAccountIdempotentInstruction(buyer, buyerCollateralAccount, buyer, r.collateral.assetMint, collateralTokenProgram),
|
|
163
|
+
createAssociatedTokenAccountIdempotentInstruction(buyer, buyerDebtAccount, buyer, r.debt.assetMint, debtTokenProgram));
|
|
164
|
+
const context: HlpFlashRouteContext = { quote, session, repaymentVault, buyerCollateralAccount, buyerDebtAccount,
|
|
165
|
+
collateralMint: r.collateral.assetMint, debtMint: r.debt.assetMint, collateralTokenProgram, debtTokenProgram,
|
|
166
|
+
requiredPaymentCredit: BigInt(quote.payment.toString()) };
|
|
167
|
+
const route = [...await params.route(context)];
|
|
168
|
+
if (route.some(ix => ix.programId.equals(ComputeBudgetProgram.programId))) throw new Error("Compute budgets must precede flash begin");
|
|
169
|
+
const beginIndex = prefix.length, settleIndex = beginIndex + 1 + route.length;
|
|
170
|
+
if (settleIndex > 65_535) throw new RangeError("Too many instructions for the bound settle index");
|
|
171
|
+
const accounts = { market: r.market, futarchyAuthority: r.futarchyAuthority, buyer, targetMint: r.collateral.assetMint,
|
|
172
|
+
borrowedMint: r.debt.assetMint, targetReserve: r.collateral.reserveVault, borrowedReserve: r.debt.reserveVault,
|
|
173
|
+
buyerCollateralAccount, buyerRefundAccount: buyerDebtAccount,
|
|
174
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID, borrowedTokenProgram: debtTokenProgram };
|
|
175
|
+
const common = { accounts, session, repaymentVault, instructions: SYSVAR_INSTRUCTIONS_PUBKEY };
|
|
176
|
+
const begin = await this.write.instruction("beginHlpFlash", {
|
|
177
|
+
targetAsset: r.target, allowFull: params.allowFull ?? false,
|
|
178
|
+
maxPayment: integerBN(params.maxPayment ?? BigInt(quote.payment.toString()), "maxPayment"),
|
|
179
|
+
maxCollateralDebit: integerBN(params.maxCollateralDebit ?? BigInt(quote.collateralDebit.toString()), "maxCollateralDebit"),
|
|
180
|
+
minCollateralCredit: integerBN(params.minCollateralCredit ?? BigInt(quote.collateralCredit.toString()), "minCollateralCredit"),
|
|
181
|
+
maxSocializedLoss: integerBN(params.maxSocializedLoss ?? 0xffffffffffffffffn, "maxSocializedLoss"), settleIndex,
|
|
182
|
+
}, { accounts: { ...common, systemProgram: SystemProgram.programId } });
|
|
183
|
+
const settle = await this.write.instruction("settleHlpFlash", [], { accounts: common,
|
|
184
|
+
remainingAccounts: params.settleRemainingAccounts ? [...params.settleRemainingAccounts] : await this.write.hlpRemainingAccounts(r.market, r.state) });
|
|
185
|
+
return { ...context, beginIndex, settleIndex, beginInstruction: begin, settleInstruction: settle,
|
|
186
|
+
exitInstruction: exit?.actionInstruction,
|
|
187
|
+
instructions: [...prefix, begin, ...route, settle, ...(exit ? [exit.actionInstruction] : [])] };
|
|
188
|
+
}
|
|
189
|
+
}
|