@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/write.ts
CHANGED
|
@@ -1,16 +1,48 @@
|
|
|
1
|
+
import { deriveJuniorCohortAddress } from "./constants.js";
|
|
2
|
+
import { oracleComputeBudgetInstructions, oraclePolicy, oracleReadAccounts, type OraclePolicyInput } from "./oracle.js";
|
|
1
3
|
import type { Program } from "@coral-xyz/anchor";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
+
import {
|
|
5
|
+
getAssociatedTokenAddressSync,
|
|
6
|
+
getMint,
|
|
7
|
+
TOKEN_2022_PROGRAM_ID,
|
|
8
|
+
TOKEN_PROGRAM_ID,
|
|
9
|
+
} from "@solana/spl-token";
|
|
10
|
+
import {
|
|
11
|
+
SystemProgram,
|
|
12
|
+
SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
13
|
+
Transaction,
|
|
14
|
+
type AccountMeta,
|
|
15
|
+
type PublicKey,
|
|
16
|
+
type TransactionInstruction,
|
|
17
|
+
} from "@solana/web3.js";
|
|
4
18
|
|
|
5
19
|
import { address, normalizeAccountKeys, type AddressLike } from "./address.js";
|
|
20
|
+
import { type MarketLaunchConfig } from "./market-bootstrap.js";
|
|
21
|
+
import type { Market } from "./type-aliases.js";
|
|
6
22
|
import {
|
|
23
|
+
deriveBorrowPositionAddress,
|
|
24
|
+
deriveEventAuthorityAddress,
|
|
7
25
|
deriveFutarchyAuthorityAddress,
|
|
26
|
+
deriveLeverageCollateralVaultAddress,
|
|
27
|
+
deriveLeverageDelegationAddress,
|
|
28
|
+
deriveLeveragePositionAddress,
|
|
29
|
+
deriveMarketAddress,
|
|
30
|
+
deriveMarketCollateralVaultAddress,
|
|
8
31
|
deriveMarketInterestVaultAddress,
|
|
32
|
+
deriveMarketReserveVaultAddress,
|
|
33
|
+
deriveTokenMetadataAddress,
|
|
34
|
+
TOKEN_METADATA_PROGRAM_ID,
|
|
35
|
+
deriveReferralAccrualAddress,
|
|
9
36
|
deriveYieldAccountAddress,
|
|
10
37
|
deriveYieldTransferHookValidationAddress,
|
|
38
|
+
deriveHlpCohortAddress,
|
|
11
39
|
deriveReferralPartnerAddress,
|
|
12
40
|
type YieldTokenKind,
|
|
13
41
|
} from "./constants.js";
|
|
42
|
+
import {
|
|
43
|
+
integerBN,
|
|
44
|
+
type IntegerLike,
|
|
45
|
+
} from "./parameters.js";
|
|
14
46
|
import {
|
|
15
47
|
assertReferralInterestShareBps,
|
|
16
48
|
referralAccrualAddresses,
|
|
@@ -25,6 +57,8 @@ export type DuskInstructionArgs = unknown[] | unknown | undefined;
|
|
|
25
57
|
export type DuskAccounts = Record<string, unknown>;
|
|
26
58
|
|
|
27
59
|
export interface DuskBuildOptions {
|
|
60
|
+
/** Explicit source policy for offline builders; async builders resolve it from the market. */
|
|
61
|
+
oraclePolicy?: OraclePolicyInput;
|
|
28
62
|
accounts?: DuskAccounts;
|
|
29
63
|
remainingAccounts?: AccountMeta[];
|
|
30
64
|
}
|
|
@@ -74,6 +108,7 @@ export interface HlpLiquidityBuild {
|
|
|
74
108
|
type AnchorMethodBuilder = {
|
|
75
109
|
accounts(accounts: DuskAccounts): AnchorMethodBuilder;
|
|
76
110
|
remainingAccounts(accounts: AccountMeta[]): AnchorMethodBuilder;
|
|
111
|
+
preInstructions(instructions: TransactionInstruction[]): AnchorMethodBuilder;
|
|
77
112
|
instruction(): Promise<TransactionInstruction>;
|
|
78
113
|
transaction(): Promise<Transaction>;
|
|
79
114
|
rpc(): Promise<string>;
|
|
@@ -84,6 +119,12 @@ type AnchorMethods = Record<string, (...args: unknown[]) => AnchorMethodBuilder>
|
|
|
84
119
|
export class DuskWrite {
|
|
85
120
|
constructor(readonly program: Program<Dusk>) {}
|
|
86
121
|
|
|
122
|
+
private fetchMarketAccount(market: PublicKey): Promise<unknown> {
|
|
123
|
+
return (this.program as unknown as {
|
|
124
|
+
account: { market: { fetch(address: PublicKey): Promise<unknown> } };
|
|
125
|
+
}).account.market.fetch(market);
|
|
126
|
+
}
|
|
127
|
+
|
|
87
128
|
method(name: DuskInstructionName, args?: DuskInstructionArgs): AnchorMethodBuilder {
|
|
88
129
|
const method = (this.program.methods as unknown as AnchorMethods)[name];
|
|
89
130
|
if (!method) {
|
|
@@ -98,33 +139,119 @@ export class DuskWrite {
|
|
|
98
139
|
options: DuskBuildOptions = {}
|
|
99
140
|
): AnchorMethodBuilder {
|
|
100
141
|
let builder = this.method(name, args);
|
|
101
|
-
|
|
102
|
-
|
|
142
|
+
const instructionAccounts = this.program.idl.instructions?.find(
|
|
143
|
+
(instruction) => instruction.name === name
|
|
144
|
+
)?.accounts;
|
|
145
|
+
const usesEventCpi =
|
|
146
|
+
instructionAccounts?.some((account) => account.name === "eventAuthority") === true &&
|
|
147
|
+
instructionAccounts.some((account) => account.name === "program") === true;
|
|
148
|
+
if (options.accounts || usesEventCpi) {
|
|
149
|
+
const accounts = usesEventCpi
|
|
150
|
+
? {
|
|
151
|
+
...options.accounts,
|
|
152
|
+
eventAuthority: deriveEventAuthorityAddress(this.program.programId)[0],
|
|
153
|
+
program: this.program.programId,
|
|
154
|
+
}
|
|
155
|
+
: options.accounts!;
|
|
156
|
+
builder = builder.accounts(normalizeAccountKeys(accounts));
|
|
103
157
|
}
|
|
104
|
-
|
|
105
|
-
|
|
158
|
+
const remaining = [...(options.remainingAccounts ?? [])];
|
|
159
|
+
for (const meta of oracleReadAccounts(options.oraclePolicy)) {
|
|
160
|
+
if (!remaining.some(existing => existing.pubkey.equals(meta.pubkey))) remaining.push(meta);
|
|
106
161
|
}
|
|
162
|
+
if (remaining.length) builder = builder.remainingAccounts(remaining);
|
|
163
|
+
const budgets = oracleComputeBudgetInstructions(options.oraclePolicy);
|
|
164
|
+
if (budgets.length) builder = builder.preInstructions(budgets);
|
|
107
165
|
return builder;
|
|
108
166
|
}
|
|
109
167
|
|
|
110
|
-
instruction(
|
|
168
|
+
async instruction(
|
|
111
169
|
name: DuskInstructionName,
|
|
112
170
|
args?: DuskInstructionArgs,
|
|
113
171
|
options?: DuskBuildOptions
|
|
114
172
|
): Promise<TransactionInstruction> {
|
|
115
|
-
return this.builder(name, args, options).instruction();
|
|
173
|
+
return this.builder(name, args, await this.withOraclePolicy(name, options)).instruction();
|
|
116
174
|
}
|
|
117
175
|
|
|
118
|
-
transaction(
|
|
176
|
+
async transaction(
|
|
119
177
|
name: DuskInstructionName,
|
|
120
178
|
args?: DuskInstructionArgs,
|
|
121
179
|
options?: DuskBuildOptions
|
|
122
180
|
): Promise<Transaction> {
|
|
123
|
-
return this.builder(name, args, options).transaction();
|
|
181
|
+
return this.builder(name, args, await this.withOraclePolicy(name, options)).transaction();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async rpc(name: DuskInstructionName, args?: DuskInstructionArgs, options?: DuskBuildOptions) {
|
|
185
|
+
return this.builder(name, args, await this.withOraclePolicy(name, options)).rpc();
|
|
124
186
|
}
|
|
125
187
|
|
|
126
|
-
|
|
127
|
-
|
|
188
|
+
private async withOraclePolicy(name: DuskInstructionName, options: DuskBuildOptions = {}): Promise<DuskBuildOptions> {
|
|
189
|
+
if (!ORACLE_READ_INSTRUCTIONS.has(name) || options.oraclePolicy) return options;
|
|
190
|
+
const nested = options.accounts?.accounts as DuskAccounts | undefined;
|
|
191
|
+
const preview = options.accounts?.preview as DuskAccounts | undefined;
|
|
192
|
+
const market = options.accounts?.market ?? nested?.market ?? preview?.market;
|
|
193
|
+
if (!market) return options;
|
|
194
|
+
|
|
195
|
+
const state = await this.fetchMarketAccount(address(market as AddressLike)) as { oracle?: {policy: OraclePolicyInput} };
|
|
196
|
+
return {...options, oraclePolicy: state.oracle?.policy};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The hLP accounts an instruction must carry when hedged liquidity is live.
|
|
201
|
+
*
|
|
202
|
+
* Swap and openLeverage both require them, and their order is
|
|
203
|
+
* consensus-visible, so both take them from here rather than each assembling
|
|
204
|
+
* its own list.
|
|
205
|
+
*/
|
|
206
|
+
/**
|
|
207
|
+
* The hLP settlement prefix an instruction must carry when the market has
|
|
208
|
+
* active hLP.
|
|
209
|
+
*
|
|
210
|
+
* Required by every leverage instruction that settles the market —
|
|
211
|
+
* open, close, increase and decrease — not only by open. Omitting it fails
|
|
212
|
+
* with `NotEnoughAccounts` at the point the program tries to read the
|
|
213
|
+
* prefix, which names neither leverage nor hLP and reads as a malformed
|
|
214
|
+
* call.
|
|
215
|
+
*/
|
|
216
|
+
async hlpRemainingAccounts(
|
|
217
|
+
marketAddress: AddressLike,
|
|
218
|
+
marketSnapshot?: Market
|
|
219
|
+
): Promise<AccountMeta[]> {
|
|
220
|
+
const market = address(marketAddress);
|
|
221
|
+
const state = (marketSnapshot ?? await this.fetchMarketAccount(market)) as {
|
|
222
|
+
srlpMint: AccountMeta["pubkey"];
|
|
223
|
+
baseSide: { interestVault: AccountMeta["pubkey"]; assetMint: AccountMeta["pubkey"]; reserveVault: AccountMeta["pubkey"] };
|
|
224
|
+
quoteSide: { interestVault: AccountMeta["pubkey"]; assetMint: AccountMeta["pubkey"]; reserveVault: AccountMeta["pubkey"] };
|
|
225
|
+
baseHlpVault: {
|
|
226
|
+
hlpSupply: { toString(): string };
|
|
227
|
+
residualExposure: { toString(): string };
|
|
228
|
+
srlpVault: AccountMeta["pubkey"];
|
|
229
|
+
};
|
|
230
|
+
quoteHlpVault: {
|
|
231
|
+
hlpSupply: { toString(): string };
|
|
232
|
+
residualExposure: { toString(): string };
|
|
233
|
+
srlpVault: AccountMeta["pubkey"];
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
const hlpActive =
|
|
237
|
+
BigInt(state.baseHlpVault.hlpSupply.toString()) !== 0n ||
|
|
238
|
+
BigInt(state.quoteHlpVault.hlpSupply.toString()) !== 0n ||
|
|
239
|
+
BigInt(state.baseHlpVault.residualExposure.toString()) !== 0n ||
|
|
240
|
+
BigInt(state.quoteHlpVault.residualExposure.toString()) !== 0n;
|
|
241
|
+
if (!hlpActive) return [];
|
|
242
|
+
return [
|
|
243
|
+
{ pubkey: state.srlpMint, isSigner: false, isWritable: true },
|
|
244
|
+
{ pubkey: state.baseHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
245
|
+
{ pubkey: state.quoteHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
246
|
+
{ pubkey: state.baseSide.interestVault, isSigner: false, isWritable: true },
|
|
247
|
+
{ pubkey: state.quoteSide.interestVault, isSigner: false, isWritable: true },
|
|
248
|
+
// Fresh public-interest settlements rebalance both vaults. These keys
|
|
249
|
+
// usually duplicate fixed swap accounts; repayments need them explicitly.
|
|
250
|
+
{ pubkey: state.baseSide.assetMint, isSigner: false, isWritable: false },
|
|
251
|
+
{ pubkey: state.quoteSide.assetMint, isSigner: false, isWritable: false },
|
|
252
|
+
{ pubkey: state.baseSide.reserveVault, isSigner: false, isWritable: true },
|
|
253
|
+
{ pubkey: state.quoteSide.reserveVault, isSigner: false, isWritable: true },
|
|
254
|
+
];
|
|
128
255
|
}
|
|
129
256
|
|
|
130
257
|
async swapBuilder(
|
|
@@ -132,19 +259,19 @@ export class DuskWrite {
|
|
|
132
259
|
options: SwapBuildOptions
|
|
133
260
|
): Promise<AnchorMethodBuilder> {
|
|
134
261
|
const market = address(options.market);
|
|
135
|
-
const state = (await this.
|
|
136
|
-
|
|
262
|
+
const state = (await this.fetchMarketAccount(market)) as {
|
|
263
|
+
srlpMint: AccountMeta["pubkey"];
|
|
137
264
|
baseSide: { interestVault: AccountMeta["pubkey"] };
|
|
138
265
|
quoteSide: { interestVault: AccountMeta["pubkey"] };
|
|
139
266
|
baseHlpVault: {
|
|
140
267
|
hlpSupply: { toString(): string };
|
|
141
268
|
residualExposure: { toString(): string };
|
|
142
|
-
|
|
269
|
+
srlpVault: AccountMeta["pubkey"];
|
|
143
270
|
};
|
|
144
271
|
quoteHlpVault: {
|
|
145
272
|
hlpSupply: { toString(): string };
|
|
146
273
|
residualExposure: { toString(): string };
|
|
147
|
-
|
|
274
|
+
srlpVault: AccountMeta["pubkey"];
|
|
148
275
|
};
|
|
149
276
|
};
|
|
150
277
|
const hlpActive =
|
|
@@ -154,16 +281,20 @@ export class DuskWrite {
|
|
|
154
281
|
BigInt(state.quoteHlpVault.residualExposure.toString()) !== 0n;
|
|
155
282
|
const prefix: AccountMeta[] = hlpActive
|
|
156
283
|
? [
|
|
157
|
-
{ pubkey: state.
|
|
158
|
-
{ pubkey: state.baseHlpVault.
|
|
159
|
-
{ pubkey: state.quoteHlpVault.
|
|
284
|
+
{ pubkey: state.srlpMint, isSigner: false, isWritable: true },
|
|
285
|
+
{ pubkey: state.baseHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
286
|
+
{ pubkey: state.quoteHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
160
287
|
{ pubkey: state.baseSide.interestVault, isSigner: false, isWritable: true },
|
|
161
288
|
{ pubkey: state.quoteSide.interestVault, isSigner: false, isWritable: true },
|
|
162
289
|
]
|
|
163
290
|
: [];
|
|
164
291
|
|
|
165
292
|
return this.builder("swap", args, {
|
|
166
|
-
|
|
293
|
+
oraclePolicy: options.oraclePolicy ?? (state as typeof state & {oracle?: {policy: OraclePolicyInput}}).oracle?.policy,
|
|
294
|
+
accounts: {
|
|
295
|
+
...options.accounts,
|
|
296
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
297
|
+
},
|
|
167
298
|
// Prefix order is consensus-visible. Hook accounts must remain a tail,
|
|
168
299
|
// including any deliberate duplicate pubkeys required by a hook meta list.
|
|
169
300
|
remainingAccounts: [...prefix, ...(options.remainingAccounts ?? [])],
|
|
@@ -182,6 +313,318 @@ export class DuskWrite {
|
|
|
182
313
|
return (await this.swapBuilder(args, options)).rpc();
|
|
183
314
|
}
|
|
184
315
|
|
|
316
|
+
/**
|
|
317
|
+
* Swap, with accounts resolved from the market and the two mints.
|
|
318
|
+
*
|
|
319
|
+
* `swapInstruction` takes a prepared account map, which makes callers
|
|
320
|
+
* responsible for reconstructing the program's account set. This resolves it
|
|
321
|
+
* instead: reserve vaults derive from the market and mint, trader accounts
|
|
322
|
+
* default to the associated token account for the correct token program, and
|
|
323
|
+
* the hLP remaining-account prefix is still assembled by `swapBuilder`,
|
|
324
|
+
* whose ordering is consensus-visible.
|
|
325
|
+
*/
|
|
326
|
+
async buildSwapInstruction(
|
|
327
|
+
params: SwapParams
|
|
328
|
+
): Promise<TransactionInstruction> {
|
|
329
|
+
return (await this.buildSwapBuilder(params)).instruction();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async buildSwapTransaction(params: SwapParams): Promise<Transaction> {
|
|
333
|
+
return (await this.buildSwapBuilder(params)).transaction();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Borrow against a position, with accounts resolved from the market and the
|
|
338
|
+
* two mints.
|
|
339
|
+
*
|
|
340
|
+
* The referral accounts are optional on chain and are omitted here: a borrow
|
|
341
|
+
* with no referrer should not have to name accounts it does not use. Use
|
|
342
|
+
* `referredBorrow` when a referrer is present.
|
|
343
|
+
*/
|
|
344
|
+
async buildBorrowInstruction(
|
|
345
|
+
params: BorrowParams
|
|
346
|
+
): Promise<TransactionInstruction> {
|
|
347
|
+
const market = address(params.market);
|
|
348
|
+
const owner = address(params.owner);
|
|
349
|
+
const debtAssetMint = address(params.debtAssetMint);
|
|
350
|
+
const collateralAssetMint = address(params.collateralAssetMint);
|
|
351
|
+
|
|
352
|
+
if (debtAssetMint.equals(collateralAssetMint)) {
|
|
353
|
+
throw new Error("Borrow debt and collateral mints must differ");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const debtTokenProgram = await tokenProgramForMint(
|
|
357
|
+
this.program.provider.connection,
|
|
358
|
+
debtAssetMint
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
return this.instruction(
|
|
362
|
+
"borrow" as DuskInstructionName,
|
|
363
|
+
{
|
|
364
|
+
borrowAmount: integerBN(params.borrowAmount, "borrowAmount"),
|
|
365
|
+
minDebtAmountOut: integerBN(
|
|
366
|
+
params.minDebtAmountOut,
|
|
367
|
+
"minDebtAmountOut"
|
|
368
|
+
),
|
|
369
|
+
minLiquidationCfBps: Number(params.minLiquidationCfBps ?? 0),
|
|
370
|
+
referrer: null,
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
accounts: {
|
|
374
|
+
market,
|
|
375
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
376
|
+
owner,
|
|
377
|
+
debtAssetMint,
|
|
378
|
+
collateralAssetMint,
|
|
379
|
+
reserveVault: address(
|
|
380
|
+
params.reserveVault ??
|
|
381
|
+
deriveMarketReserveVaultAddress(market, debtAssetMint)[0]
|
|
382
|
+
),
|
|
383
|
+
ownerDebtAccount: address(
|
|
384
|
+
params.ownerDebtAccount ??
|
|
385
|
+
getAssociatedTokenAddressSync(
|
|
386
|
+
debtAssetMint,
|
|
387
|
+
owner,
|
|
388
|
+
true,
|
|
389
|
+
debtTokenProgram
|
|
390
|
+
)
|
|
391
|
+
),
|
|
392
|
+
borrowPosition: address(
|
|
393
|
+
params.borrowPosition ??
|
|
394
|
+
deriveBorrowPositionAddress(market, owner, address(params.positionId))[0]
|
|
395
|
+
),
|
|
396
|
+
referralPartner: null,
|
|
397
|
+
referralAccrual: null,
|
|
398
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
399
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
400
|
+
},
|
|
401
|
+
remainingAccounts: params.remainingAccounts,
|
|
402
|
+
}
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async buildBorrowTransaction(params: BorrowParams): Promise<Transaction> {
|
|
407
|
+
const options = await this.withOraclePolicy("borrow", { accounts: {market: address(params.market)} });
|
|
408
|
+
return new Transaction().add(...oracleComputeBudgetInstructions(options.oraclePolicy),
|
|
409
|
+
await this.buildBorrowInstruction(params));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Open a leverage position, with accounts resolved from the market, the
|
|
414
|
+
* position id and the two mints.
|
|
415
|
+
*
|
|
416
|
+
* Referral accounts are optional on chain and omitted here; use
|
|
417
|
+
* `referredOpenLeverage` when a referrer is present.
|
|
418
|
+
*/
|
|
419
|
+
async buildOpenLeverageInstruction(
|
|
420
|
+
params: OpenLeverageParams
|
|
421
|
+
): Promise<TransactionInstruction> {
|
|
422
|
+
const market = address(params.market);
|
|
423
|
+
const owner = address(params.owner);
|
|
424
|
+
const debtMint = address(params.debtMint);
|
|
425
|
+
const collateralMint = address(params.collateralMint);
|
|
426
|
+
|
|
427
|
+
if (debtMint.equals(collateralMint)) {
|
|
428
|
+
throw new Error("Leverage debt and collateral mints must differ");
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if ("fundingAsset" in params && params.fundingAsset !== undefined && params.fundingAsset !== "debt")
|
|
432
|
+
throw new Error("Leverage funding must use the debt asset; swap other tokens before opening");
|
|
433
|
+
const positionId = address(params.positionId);
|
|
434
|
+
const debtTokenProgram = await tokenProgramForMint(
|
|
435
|
+
this.program.provider.connection,
|
|
436
|
+
debtMint
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
const openArgs = {
|
|
440
|
+
positionId,
|
|
441
|
+
debtAsset: params.debtAsset === "quote" ? 1 : 0,
|
|
442
|
+
marginAmount: integerBN(params.marginAmount, "marginAmount"),
|
|
443
|
+
multiplierBps: integerBN(
|
|
444
|
+
params.multiplierBps,
|
|
445
|
+
"multiplierBps"
|
|
446
|
+
),
|
|
447
|
+
minCollateralOut: integerBN(
|
|
448
|
+
params.minCollateralOut,
|
|
449
|
+
"minCollateralOut"
|
|
450
|
+
),
|
|
451
|
+
referrer: null,
|
|
452
|
+
positionOwner: null,
|
|
453
|
+
limitPriceNad: integerBN(
|
|
454
|
+
params.limitPriceNad ?? 0,
|
|
455
|
+
"limitPriceNad"
|
|
456
|
+
),
|
|
457
|
+
};
|
|
458
|
+
return this.instruction(
|
|
459
|
+
"openLeverage",
|
|
460
|
+
openArgs,
|
|
461
|
+
{
|
|
462
|
+
accounts: {
|
|
463
|
+
market,
|
|
464
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
465
|
+
owner,
|
|
466
|
+
payer: address(params.payer ?? owner),
|
|
467
|
+
leveragePosition: address(
|
|
468
|
+
params.leveragePosition ??
|
|
469
|
+
deriveLeveragePositionAddress(market, owner, positionId)[0]
|
|
470
|
+
),
|
|
471
|
+
debtMint,
|
|
472
|
+
collateralMint,
|
|
473
|
+
debtReserveVault: address(
|
|
474
|
+
params.debtReserveVault ??
|
|
475
|
+
deriveMarketReserveVaultAddress(market, debtMint)[0]
|
|
476
|
+
),
|
|
477
|
+
collateralReserveVault: address(
|
|
478
|
+
params.collateralReserveVault ??
|
|
479
|
+
deriveMarketReserveVaultAddress(market, collateralMint)[0]
|
|
480
|
+
),
|
|
481
|
+
leverageCollateralVault: address(
|
|
482
|
+
params.leverageCollateralVault ??
|
|
483
|
+
deriveLeverageCollateralVaultAddress(market, collateralMint)[0]
|
|
484
|
+
),
|
|
485
|
+
ownerDebtAccount: address(
|
|
486
|
+
params.ownerDebtAccount ??
|
|
487
|
+
getAssociatedTokenAddressSync(
|
|
488
|
+
debtMint,
|
|
489
|
+
owner,
|
|
490
|
+
true,
|
|
491
|
+
debtTokenProgram
|
|
492
|
+
)
|
|
493
|
+
),
|
|
494
|
+
referralPartner: null,
|
|
495
|
+
referralAccrual: null,
|
|
496
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
497
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
498
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
499
|
+
systemProgram: SystemProgram.programId,
|
|
500
|
+
},
|
|
501
|
+
remainingAccounts: [
|
|
502
|
+
...(await this.hlpRemainingAccounts(market)),
|
|
503
|
+
...(params.remainingAccounts ?? []),
|
|
504
|
+
],
|
|
505
|
+
}
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
async buildOpenLeverageTransaction(
|
|
510
|
+
params: OpenLeverageParams
|
|
511
|
+
): Promise<Transaction> {
|
|
512
|
+
const options = await this.withOraclePolicy("openLeverage", { accounts: {market: address(params.market)} });
|
|
513
|
+
return new Transaction().add(...oracleComputeBudgetInstructions(options.oraclePolicy),
|
|
514
|
+
await this.buildOpenLeverageInstruction(params)
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Delegate a leverage position to a program that may act on it, which is how
|
|
520
|
+
* conditional orders (take-profit, stop-loss) are authorised.
|
|
521
|
+
*/
|
|
522
|
+
async buildCreateLeverageDelegationInstruction(
|
|
523
|
+
params: CreateLeverageDelegationParams
|
|
524
|
+
): Promise<TransactionInstruction> {
|
|
525
|
+
const market = address(params.market);
|
|
526
|
+
const leveragePosition = address(
|
|
527
|
+
params.leveragePosition ??
|
|
528
|
+
deriveLeveragePositionAddress(market, address(params.owner), address(params.positionId), address(params.namespaceAuthority ?? params.owner))[0]
|
|
529
|
+
);
|
|
530
|
+
|
|
531
|
+
return this.instruction(
|
|
532
|
+
"createLeverageDelegation" as DuskInstructionName,
|
|
533
|
+
{
|
|
534
|
+
debtAsset: params.debtAsset === "quote" ? 1 : 0,
|
|
535
|
+
delegatedProgram: address(params.delegatedProgram),
|
|
536
|
+
approvedActions: Number(params.approvedActions),
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
accounts: {
|
|
540
|
+
market,
|
|
541
|
+
leveragePosition,
|
|
542
|
+
leverageDelegation: address(
|
|
543
|
+
params.leverageDelegation ??
|
|
544
|
+
deriveLeverageDelegationAddress(leveragePosition)[0]
|
|
545
|
+
),
|
|
546
|
+
owner: address(params.owner),
|
|
547
|
+
systemProgram: SystemProgram.programId,
|
|
548
|
+
},
|
|
549
|
+
remainingAccounts: params.remainingAccounts,
|
|
550
|
+
}
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
async buildCreateLeverageDelegationTransaction(
|
|
555
|
+
params: CreateLeverageDelegationParams
|
|
556
|
+
): Promise<Transaction> {
|
|
557
|
+
return new Transaction().add(
|
|
558
|
+
await this.buildCreateLeverageDelegationInstruction(params)
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
private async buildSwapBuilder(params: SwapParams) {
|
|
563
|
+
const market = address(params.market);
|
|
564
|
+
const trader = address(params.trader);
|
|
565
|
+
const assetInMint = address(params.assetInMint);
|
|
566
|
+
const assetOutMint = address(params.assetOutMint);
|
|
567
|
+
|
|
568
|
+
if (assetInMint.equals(assetOutMint)) {
|
|
569
|
+
throw new Error("Swap input and output mints must differ");
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const connection = this.program.provider.connection;
|
|
573
|
+
const [assetInProgram, assetOutProgram] = await Promise.all([
|
|
574
|
+
tokenProgramForMint(connection, assetInMint),
|
|
575
|
+
tokenProgramForMint(connection, assetOutMint),
|
|
576
|
+
]);
|
|
577
|
+
|
|
578
|
+
return this.swapBuilder(
|
|
579
|
+
// A single positional SwapArgs struct: the generic builder spreads a
|
|
580
|
+
// non-array into one argument, so wrapping it in an outer object would
|
|
581
|
+
// serialize a struct of zeros and the program would reject the amount.
|
|
582
|
+
{
|
|
583
|
+
exactAssetIn: integerBN(params.exactAssetIn, "exactAssetIn"),
|
|
584
|
+
minAssetOut: integerBN(params.minAssetOut, "minAssetOut"),
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
market,
|
|
588
|
+
accounts: {
|
|
589
|
+
market,
|
|
590
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
591
|
+
trader,
|
|
592
|
+
assetInMint,
|
|
593
|
+
assetOutMint,
|
|
594
|
+
reserveInVault: address(
|
|
595
|
+
params.reserveInVault ??
|
|
596
|
+
deriveMarketReserveVaultAddress(market, assetInMint)[0]
|
|
597
|
+
),
|
|
598
|
+
reserveOutVault: address(
|
|
599
|
+
params.reserveOutVault ??
|
|
600
|
+
deriveMarketReserveVaultAddress(market, assetOutMint)[0]
|
|
601
|
+
),
|
|
602
|
+
traderAssetInAccount: address(
|
|
603
|
+
params.traderAssetInAccount ??
|
|
604
|
+
getAssociatedTokenAddressSync(
|
|
605
|
+
assetInMint,
|
|
606
|
+
trader,
|
|
607
|
+
true,
|
|
608
|
+
assetInProgram
|
|
609
|
+
)
|
|
610
|
+
),
|
|
611
|
+
traderAssetOutAccount: address(
|
|
612
|
+
params.traderAssetOutAccount ??
|
|
613
|
+
getAssociatedTokenAddressSync(
|
|
614
|
+
assetOutMint,
|
|
615
|
+
trader,
|
|
616
|
+
true,
|
|
617
|
+
assetOutProgram
|
|
618
|
+
)
|
|
619
|
+
),
|
|
620
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
621
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
622
|
+
},
|
|
623
|
+
remainingAccounts: params.remainingAccounts,
|
|
624
|
+
}
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
|
|
185
628
|
async initializeYieldAccountsInstruction(params: {
|
|
186
629
|
payer: AddressLike;
|
|
187
630
|
owner: AddressLike;
|
|
@@ -190,6 +633,8 @@ export class DuskWrite {
|
|
|
190
633
|
baseMint: AddressLike;
|
|
191
634
|
quoteMint: AddressLike;
|
|
192
635
|
tokenKind: YieldTokenKind;
|
|
636
|
+
/** Supply the retired mint's cohort when initializing claims after rotation. */
|
|
637
|
+
retired?: boolean;
|
|
193
638
|
}): Promise<TransactionInstruction> {
|
|
194
639
|
const payer = address(params.payer);
|
|
195
640
|
const owner = address(params.owner);
|
|
@@ -197,7 +642,7 @@ export class DuskWrite {
|
|
|
197
642
|
const lpMint = address(params.lpMint);
|
|
198
643
|
const baseMint = address(params.baseMint);
|
|
199
644
|
const quoteMint = address(params.quoteMint);
|
|
200
|
-
const tokenKind = params.tokenKind === "
|
|
645
|
+
const tokenKind = params.tokenKind === "srlp" || params.tokenKind === 0 ? { srlp: {} } : params.tokenKind === "junior" || params.tokenKind === 2 ? { junior: {} } : { hlp: {} };
|
|
201
646
|
return this.instruction("initializeYieldAccounts" as DuskInstructionName, { owner, tokenKind }, {
|
|
202
647
|
accounts: {
|
|
203
648
|
payer,
|
|
@@ -205,10 +650,27 @@ export class DuskWrite {
|
|
|
205
650
|
lpMint,
|
|
206
651
|
baseMint,
|
|
207
652
|
quoteMint,
|
|
208
|
-
baseYieldAccount: deriveYieldAccountAddress(
|
|
209
|
-
|
|
653
|
+
baseYieldAccount: deriveYieldAccountAddress(
|
|
654
|
+
market,
|
|
655
|
+
owner,
|
|
656
|
+
lpMint,
|
|
657
|
+
baseMint,
|
|
658
|
+
params.tokenKind,
|
|
659
|
+
this.program.programId
|
|
660
|
+
)[0],
|
|
661
|
+
quoteYieldAccount: deriveYieldAccountAddress(
|
|
662
|
+
market,
|
|
663
|
+
owner,
|
|
664
|
+
lpMint,
|
|
665
|
+
quoteMint,
|
|
666
|
+
params.tokenKind,
|
|
667
|
+
this.program.programId
|
|
668
|
+
)[0],
|
|
210
669
|
systemProgram: SystemProgram.programId,
|
|
211
670
|
},
|
|
671
|
+
remainingAccounts: params.retired
|
|
672
|
+
? [{ pubkey: (params.tokenKind === "junior" || params.tokenKind === 2 ? deriveJuniorCohortAddress : deriveHlpCohortAddress)(market, lpMint, this.program.programId)[0], isSigner: false, isWritable: false }]
|
|
673
|
+
: [],
|
|
212
674
|
});
|
|
213
675
|
}
|
|
214
676
|
|
|
@@ -222,16 +684,31 @@ export class DuskWrite {
|
|
|
222
684
|
payer: AddressLike;
|
|
223
685
|
market: AddressLike;
|
|
224
686
|
lpMint: AddressLike;
|
|
687
|
+
/** Pass this when composing market creation and hook setup in one transaction. */
|
|
688
|
+
tokenKind?: YieldTokenKind;
|
|
225
689
|
}): Promise<TransactionInstruction> {
|
|
226
690
|
const lpMint = address(params.lpMint);
|
|
691
|
+
const market = address(params.market);
|
|
692
|
+
let tokenKind = params.tokenKind;
|
|
693
|
+
if (tokenKind === undefined) {
|
|
694
|
+
const state = await this.fetchMarketAccount(market) as Market;
|
|
695
|
+
if (lpMint.equals(state.srlpMint)) tokenKind = "srlp";
|
|
696
|
+
else if (lpMint.equals(state.juniorMint)) tokenKind = "junior";
|
|
697
|
+
else if (lpMint.equals(state.baseSide.hlpMint) || lpMint.equals(state.quoteSide.hlpMint)) tokenKind = "hlp";
|
|
698
|
+
else throw new Error("LP mint does not belong to the market");
|
|
699
|
+
}
|
|
700
|
+
const isHlp = tokenKind === "hlp" || tokenKind === 1;
|
|
227
701
|
return this.instruction("initializeLpTransferHook" as DuskInstructionName, undefined, {
|
|
228
702
|
accounts: {
|
|
229
703
|
payer: address(params.payer),
|
|
230
|
-
market
|
|
704
|
+
market,
|
|
231
705
|
lpMint,
|
|
232
|
-
validationAccount: deriveYieldTransferHookValidationAddress(lpMint)[0],
|
|
706
|
+
validationAccount: deriveYieldTransferHookValidationAddress(lpMint, this.program.programId)[0],
|
|
233
707
|
systemProgram: SystemProgram.programId,
|
|
234
708
|
},
|
|
709
|
+
remainingAccounts: isHlp
|
|
710
|
+
? [{ pubkey: deriveHlpCohortAddress(market, lpMint, this.program.programId)[0], isSigner: false, isWritable: true }]
|
|
711
|
+
: [],
|
|
235
712
|
});
|
|
236
713
|
}
|
|
237
714
|
|
|
@@ -241,6 +718,106 @@ export class DuskWrite {
|
|
|
241
718
|
return new Transaction().add(await this.initializeLpTransferHookInstruction(params));
|
|
242
719
|
}
|
|
243
720
|
|
|
721
|
+
/**
|
|
722
|
+
* Bring a market into existence.
|
|
723
|
+
*
|
|
724
|
+
* The three LP mints must already exist and carry the Dusk transfer hook —
|
|
725
|
+
* see `createHookedLpMintInstructions`. Every PDA is derived here rather
|
|
726
|
+
* than left to Anchor, because the market address depends on `paramsHash`,
|
|
727
|
+
* which is an argument rather than an account, and the vaults hang off the
|
|
728
|
+
* market in turn.
|
|
729
|
+
*
|
|
730
|
+
* `teamTreasury` and its wrapped-SOL account come from the futarchy
|
|
731
|
+
* authority, which must already be initialized on the deployment.
|
|
732
|
+
*/
|
|
733
|
+
async initializeMarketInstruction(params: {
|
|
734
|
+
payer: AddressLike;
|
|
735
|
+
baseMint: AddressLike;
|
|
736
|
+
quoteMint: AddressLike;
|
|
737
|
+
srlpMint: AddressLike;
|
|
738
|
+
baseHlpMint: AddressLike;
|
|
739
|
+
quoteHlpMint: AddressLike;
|
|
740
|
+
teamTreasury: AddressLike;
|
|
741
|
+
teamTreasuryWsolAccount: AddressLike;
|
|
742
|
+
paramsHash: Uint8Array | Buffer | number[];
|
|
743
|
+
config: MarketLaunchConfig;
|
|
744
|
+
/** Share of base interest retained by the operator, 0–5000 bps. */
|
|
745
|
+
operatorInterestShareBps?: number;
|
|
746
|
+
/** Installed atomically at creation. Omit or pass null for native pricing.
|
|
747
|
+
* Guided first liquidity derives its ratio from the validated current feed. */
|
|
748
|
+
oracle?: OraclePolicyInput | null;
|
|
749
|
+
/** Native launch-adapter price in NAD. Guided creation requires zero. */
|
|
750
|
+
bootstrapPriceNad?: IntegerLike;
|
|
751
|
+
launchFeeProgressOffset?: number;
|
|
752
|
+
lpMetadata: {
|
|
753
|
+
srlp: { name: string; symbol: string; uri: string };
|
|
754
|
+
baseHlp: { name: string; symbol: string; uri: string };
|
|
755
|
+
quoteHlp: { name: string; symbol: string; uri: string };
|
|
756
|
+
};
|
|
757
|
+
}): Promise<TransactionInstruction> {
|
|
758
|
+
const operatorShare=params.operatorInterestShareBps??0;
|
|
759
|
+
if(!Number.isInteger(operatorShare)||operatorShare<0||operatorShare>5_000)throw new Error("operatorInterestShareBps must be 0–5000");
|
|
760
|
+
const baseMint = address(params.baseMint);
|
|
761
|
+
const quoteMint = address(params.quoteMint);
|
|
762
|
+
const paramsHash = Uint8Array.from(params.paramsHash);
|
|
763
|
+
if (paramsHash.length !== 32) {
|
|
764
|
+
throw new Error(`paramsHash must be 32 bytes, received ${paramsHash.length}`);
|
|
765
|
+
}
|
|
766
|
+
const [market] = deriveMarketAddress(baseMint, quoteMint, paramsHash, this.program.programId);
|
|
767
|
+
const oracle = params.oracle == null ? null : oraclePolicy(params.oracle);
|
|
768
|
+
const bootstrapPriceNad = integerBN(params.bootstrapPriceNad ?? 0, "bootstrapPriceNad");
|
|
769
|
+
if (oracle && oracle.lambdaBps > 0 && !bootstrapPriceNad.isZero())
|
|
770
|
+
throw new Error("Oracle-guided markets derive the initial price from the feed; omit bootstrapPriceNad");
|
|
771
|
+
return this.instruction(
|
|
772
|
+
"initializeMarket" as DuskInstructionName,
|
|
773
|
+
{
|
|
774
|
+
operatorInterestShareBps:operatorShare,
|
|
775
|
+
config: anchorMarketConfig(params.config),
|
|
776
|
+
oracle,
|
|
777
|
+
paramsHash: [...paramsHash],
|
|
778
|
+
bootstrapPriceNad,
|
|
779
|
+
launchFeeProgressOffset: params.launchFeeProgressOffset ?? 0,
|
|
780
|
+
srlpMetadata: params.lpMetadata.srlp,
|
|
781
|
+
baseHlpMetadata: params.lpMetadata.baseHlp,
|
|
782
|
+
quoteHlpMetadata: params.lpMetadata.quoteHlp,
|
|
783
|
+
},
|
|
784
|
+
{
|
|
785
|
+
accounts: {
|
|
786
|
+
payer: address(params.payer),
|
|
787
|
+
baseMint,
|
|
788
|
+
quoteMint,
|
|
789
|
+
srlpMint: address(params.srlpMint),
|
|
790
|
+
baseHlpMint: address(params.baseHlpMint),
|
|
791
|
+
quoteHlpMint: address(params.quoteHlpMint),
|
|
792
|
+
srlpTokenMetadata: deriveTokenMetadataAddress(address(params.srlpMint))[0],
|
|
793
|
+
baseHlpTokenMetadata: deriveTokenMetadataAddress(address(params.baseHlpMint))[0],
|
|
794
|
+
quoteHlpTokenMetadata: deriveTokenMetadataAddress(address(params.quoteHlpMint))[0],
|
|
795
|
+
market,
|
|
796
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
797
|
+
baseReserveVault: deriveMarketReserveVaultAddress(market, baseMint)[0],
|
|
798
|
+
quoteReserveVault: deriveMarketReserveVaultAddress(market, quoteMint)[0],
|
|
799
|
+
baseCollateralVault: deriveMarketCollateralVaultAddress(market, baseMint)[0],
|
|
800
|
+
quoteCollateralVault: deriveMarketCollateralVaultAddress(market, quoteMint)[0],
|
|
801
|
+
baseInterestVault: deriveMarketInterestVaultAddress(market, baseMint)[0],
|
|
802
|
+
quoteInterestVault: deriveMarketInterestVaultAddress(market, quoteMint)[0],
|
|
803
|
+
teamTreasury: address(params.teamTreasury),
|
|
804
|
+
teamTreasuryWsolAccount: address(params.teamTreasuryWsolAccount),
|
|
805
|
+
systemProgram: SystemProgram.programId,
|
|
806
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
807
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
808
|
+
sysvarInstructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
809
|
+
tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
|
|
810
|
+
},
|
|
811
|
+
}
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
async initializeMarketTransaction(
|
|
816
|
+
params: Parameters<DuskWrite["initializeMarketInstruction"]>[0]
|
|
817
|
+
): Promise<Transaction> {
|
|
818
|
+
return new Transaction().add(await this.initializeMarketInstruction(params));
|
|
819
|
+
}
|
|
820
|
+
|
|
244
821
|
async hlpLiquidityAction(
|
|
245
822
|
name: "depositSingleSided" | "withdrawSingleSided",
|
|
246
823
|
args: Record<string, unknown>,
|
|
@@ -252,8 +829,22 @@ export class DuskWrite {
|
|
|
252
829
|
const lpMint = address(options.targetHlpMint);
|
|
253
830
|
const baseMint = address(options.baseMint);
|
|
254
831
|
const quoteMint = address(options.quoteMint);
|
|
255
|
-
const baseYieldAccount = deriveYieldAccountAddress(
|
|
256
|
-
|
|
832
|
+
const baseYieldAccount = deriveYieldAccountAddress(
|
|
833
|
+
market,
|
|
834
|
+
owner,
|
|
835
|
+
lpMint,
|
|
836
|
+
baseMint,
|
|
837
|
+
"hlp",
|
|
838
|
+
this.program.programId
|
|
839
|
+
)[0];
|
|
840
|
+
const quoteYieldAccount = deriveYieldAccountAddress(
|
|
841
|
+
market,
|
|
842
|
+
owner,
|
|
843
|
+
lpMint,
|
|
844
|
+
quoteMint,
|
|
845
|
+
"hlp",
|
|
846
|
+
this.program.programId
|
|
847
|
+
)[0];
|
|
257
848
|
const [baseYieldInfo, quoteYieldInfo] = await Promise.all([
|
|
258
849
|
this.program.provider.connection.getAccountInfo(baseYieldAccount),
|
|
259
850
|
this.program.provider.connection.getAccountInfo(quoteYieldAccount),
|
|
@@ -299,6 +890,7 @@ export class DuskWrite {
|
|
|
299
890
|
quoteYieldAccount,
|
|
300
891
|
},
|
|
301
892
|
remainingAccounts: options.remainingAccounts,
|
|
893
|
+
oraclePolicy: options.oraclePolicy,
|
|
302
894
|
});
|
|
303
895
|
return {
|
|
304
896
|
baseYieldAccount,
|
|
@@ -317,6 +909,72 @@ export class DuskWrite {
|
|
|
317
909
|
return this.hlpLiquidityAction("withdrawSingleSided", args, options);
|
|
318
910
|
}
|
|
319
911
|
|
|
912
|
+
/** Begin a fresh hLP receipt cohort after the current vault has terminally closed. */
|
|
913
|
+
rotateHlpCohortInstruction(params: {
|
|
914
|
+
payer: AddressLike;
|
|
915
|
+
market: AddressLike;
|
|
916
|
+
oldHlpMint: AddressLike;
|
|
917
|
+
newHlpMint: AddressLike;
|
|
918
|
+
targetAsset: "base" | "quote";
|
|
919
|
+
metadata: { name: string; symbol: string; uri: string };
|
|
920
|
+
}): Promise<TransactionInstruction> {
|
|
921
|
+
const market = address(params.market);
|
|
922
|
+
const oldHlpMint = address(params.oldHlpMint);
|
|
923
|
+
const newHlpMint = address(params.newHlpMint);
|
|
924
|
+
return this.instruction("rotateHlpCohort" as DuskInstructionName, {
|
|
925
|
+
targetAsset: params.targetAsset === "base" ? 0 : 1,
|
|
926
|
+
metadata: params.metadata,
|
|
927
|
+
}, {
|
|
928
|
+
accounts: {
|
|
929
|
+
payer: address(params.payer),
|
|
930
|
+
market,
|
|
931
|
+
oldHlpMint,
|
|
932
|
+
newHlpMint,
|
|
933
|
+
oldCohort: deriveHlpCohortAddress(market, oldHlpMint, this.program.programId)[0],
|
|
934
|
+
newTokenMetadata: deriveTokenMetadataAddress(newHlpMint)[0],
|
|
935
|
+
sysvarInstructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
936
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
937
|
+
tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
|
|
938
|
+
systemProgram: SystemProgram.programId,
|
|
939
|
+
},
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/** Burn old hLP receipts after terminal closure while preserving earned yield. */
|
|
944
|
+
burnRetiredHlpReceiptInstruction(params: {
|
|
945
|
+
market: AddressLike;
|
|
946
|
+
owner: AddressLike;
|
|
947
|
+
retiredMint: AddressLike;
|
|
948
|
+
baseMint: AddressLike;
|
|
949
|
+
quoteMint: AddressLike;
|
|
950
|
+
amount: IntegerLike;
|
|
951
|
+
ownerHlpAccount?: AddressLike;
|
|
952
|
+
}): Promise<TransactionInstruction> {
|
|
953
|
+
const market = address(params.market);
|
|
954
|
+
const owner = address(params.owner);
|
|
955
|
+
const retiredMint = address(params.retiredMint);
|
|
956
|
+
return this.instruction("burnRetiredHlpReceipt" as DuskInstructionName, {
|
|
957
|
+
amount: integerBN(params.amount),
|
|
958
|
+
}, {
|
|
959
|
+
accounts: {
|
|
960
|
+
market,
|
|
961
|
+
owner,
|
|
962
|
+
retiredMint,
|
|
963
|
+
cohort: deriveHlpCohortAddress(market, retiredMint, this.program.programId)[0],
|
|
964
|
+
ownerHlpAccount: params.ownerHlpAccount
|
|
965
|
+
? address(params.ownerHlpAccount)
|
|
966
|
+
: getAssociatedTokenAddressSync(retiredMint, owner, true, TOKEN_2022_PROGRAM_ID),
|
|
967
|
+
baseYieldAccount: deriveYieldAccountAddress(
|
|
968
|
+
market, owner, retiredMint, address(params.baseMint), "hlp", this.program.programId
|
|
969
|
+
)[0],
|
|
970
|
+
quoteYieldAccount: deriveYieldAccountAddress(
|
|
971
|
+
market, owner, retiredMint, address(params.quoteMint), "hlp", this.program.programId
|
|
972
|
+
)[0],
|
|
973
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
974
|
+
},
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
|
|
320
978
|
async configureReferralPartnerInstruction(params: {
|
|
321
979
|
authoritySigner: AddressLike;
|
|
322
980
|
referrer: AddressLike;
|
|
@@ -411,6 +1069,9 @@ export class DuskWrite {
|
|
|
411
1069
|
...options.accounts,
|
|
412
1070
|
referralPartner: referral.referralPartner,
|
|
413
1071
|
referralAccrual: referral.referralAccrual,
|
|
1072
|
+
...(name === "openLeverage"
|
|
1073
|
+
? { instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY }
|
|
1074
|
+
: {}),
|
|
414
1075
|
},
|
|
415
1076
|
remainingAccounts: mergeAccountMetas(options.remainingAccounts ?? [], hookAccounts),
|
|
416
1077
|
}
|
|
@@ -480,8 +1141,15 @@ export class DuskWrite {
|
|
|
480
1141
|
this.program.provider.connection,
|
|
481
1142
|
mintKey
|
|
482
1143
|
);
|
|
1144
|
+
const referralAccrual = (this.program as unknown as {
|
|
1145
|
+
account: {
|
|
1146
|
+
referralAccrual: {
|
|
1147
|
+
fetch(address: PublicKey): Promise<{ amount: { toString(): string } }>;
|
|
1148
|
+
};
|
|
1149
|
+
};
|
|
1150
|
+
}).account.referralAccrual;
|
|
483
1151
|
const [accrual, mint] = await Promise.all([
|
|
484
|
-
|
|
1152
|
+
referralAccrual.fetch(referral.referralAccrual),
|
|
485
1153
|
getMint(
|
|
486
1154
|
this.program.provider.connection,
|
|
487
1155
|
mintKey,
|
|
@@ -526,6 +1194,895 @@ export class DuskWrite {
|
|
|
526
1194
|
await this.claimReferralInterestInstruction(params)
|
|
527
1195
|
);
|
|
528
1196
|
}
|
|
1197
|
+
|
|
1198
|
+
/**
|
|
1199
|
+
* Deposit collateral into a borrow position, creating it when `positionId` is
|
|
1200
|
+
* new. Callers pass amounts in raw base units.
|
|
1201
|
+
*/
|
|
1202
|
+
async depositCollateralInstruction(
|
|
1203
|
+
params: DepositCollateralParams
|
|
1204
|
+
): Promise<TransactionInstruction> {
|
|
1205
|
+
const market = address(params.market);
|
|
1206
|
+
const owner = address(params.owner);
|
|
1207
|
+
const assetMint = address(params.assetMint);
|
|
1208
|
+
const positionId = address(params.positionId);
|
|
1209
|
+
return this.instruction(
|
|
1210
|
+
"depositCollateral" as DuskInstructionName,
|
|
1211
|
+
{ positionId, depositAmount: integerBN(params.depositAmount, "depositAmount") },
|
|
1212
|
+
{
|
|
1213
|
+
accounts: {
|
|
1214
|
+
market,
|
|
1215
|
+
owner,
|
|
1216
|
+
assetMint,
|
|
1217
|
+
collateralVault: address(
|
|
1218
|
+
params.collateralVault ??
|
|
1219
|
+
deriveMarketCollateralVaultAddress(market, assetMint)[0]
|
|
1220
|
+
),
|
|
1221
|
+
ownerAssetAccount: address(params.ownerAssetAccount),
|
|
1222
|
+
borrowPosition: address(
|
|
1223
|
+
params.borrowPosition ??
|
|
1224
|
+
deriveBorrowPositionAddress(market, owner, positionId)[0]
|
|
1225
|
+
),
|
|
1226
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1227
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1228
|
+
},
|
|
1229
|
+
remainingAccounts: params.remainingAccounts,
|
|
1230
|
+
}
|
|
1231
|
+
);
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
/** Donate to an existing borrow position. `owner` is the token payer. */
|
|
1235
|
+
async donateCollateralInstruction(params: DepositCollateralParams): Promise<TransactionInstruction> {
|
|
1236
|
+
const market = address(params.market);
|
|
1237
|
+
const assetMint = address(params.assetMint);
|
|
1238
|
+
return this.instruction("donateCollateral" as DuskInstructionName,
|
|
1239
|
+
{ depositAmount: integerBN(params.depositAmount, "depositAmount") }, {
|
|
1240
|
+
accounts: {
|
|
1241
|
+
market, owner: address(params.owner), assetMint,
|
|
1242
|
+
collateralVault: address(params.collateralVault ?? deriveMarketCollateralVaultAddress(market, assetMint)[0]),
|
|
1243
|
+
ownerAssetAccount: address(params.ownerAssetAccount),
|
|
1244
|
+
borrowPosition: address(params.borrowPosition ?? deriveBorrowPositionAddress(market, address(params.positionOwner ?? params.owner), address(params.positionId))[0]),
|
|
1245
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1246
|
+
}, remainingAccounts: params.remainingAccounts,
|
|
1247
|
+
});
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
/** Sweep both collateral balances and close a debt-free borrow position. */
|
|
1251
|
+
async withdrawAllCollateralInstruction(params: WithdrawAllCollateralParams): Promise<TransactionInstruction> {
|
|
1252
|
+
const market = address(params.market);
|
|
1253
|
+
const baseMint = address(params.baseMint);
|
|
1254
|
+
const quoteMint = address(params.quoteMint);
|
|
1255
|
+
return this.instruction("withdrawAllCollateral" as DuskInstructionName, {
|
|
1256
|
+
minBaseOut: integerBN(params.minBaseOut, "minBaseOut"),
|
|
1257
|
+
minQuoteOut: integerBN(params.minQuoteOut, "minQuoteOut"),
|
|
1258
|
+
}, { accounts: {
|
|
1259
|
+
market, owner: address(params.owner), baseMint, quoteMint,
|
|
1260
|
+
borrowPosition: address(params.borrowPosition ?? deriveBorrowPositionAddress(market, address(params.owner), address(params.positionId))[0]),
|
|
1261
|
+
baseCollateralVault: deriveMarketCollateralVaultAddress(market, baseMint)[0],
|
|
1262
|
+
quoteCollateralVault: deriveMarketCollateralVaultAddress(market, quoteMint)[0],
|
|
1263
|
+
ownerBaseAccount: address(params.ownerBaseAccount), ownerQuoteAccount: address(params.ownerQuoteAccount),
|
|
1264
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1265
|
+
}, remainingAccounts: params.remainingAccounts });
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
/** Return all collateral after a complete leverage repayment and close the position. */
|
|
1269
|
+
async withdrawRepaidLeverageInstruction(params: WithdrawRepaidLeverageParams): Promise<TransactionInstruction> {
|
|
1270
|
+
const market = address(params.market);
|
|
1271
|
+
const collateralMint = address(params.collateralMint);
|
|
1272
|
+
return this.instruction("withdrawRepaidLeverage" as DuskInstructionName,
|
|
1273
|
+
{ minCollateralOut: integerBN(params.minCollateralOut, "minCollateralOut") }, {
|
|
1274
|
+
accounts: {
|
|
1275
|
+
market, owner: address(params.owner), collateralMint,
|
|
1276
|
+
leveragePosition: address(params.leveragePosition ?? deriveLeveragePositionAddress(market, address(params.owner), address(params.positionId), address(params.namespaceAuthority ?? params.owner))[0]),
|
|
1277
|
+
collateralVault: deriveLeverageCollateralVaultAddress(market, collateralMint)[0],
|
|
1278
|
+
ownerCollateralAccount: address(params.ownerCollateralAccount),
|
|
1279
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1280
|
+
}, remainingAccounts: params.remainingAccounts,
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
async depositCollateralTransaction(
|
|
1285
|
+
params: DepositCollateralParams
|
|
1286
|
+
): Promise<Transaction> {
|
|
1287
|
+
return new Transaction().add(
|
|
1288
|
+
await this.depositCollateralInstruction(params)
|
|
1289
|
+
);
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
/**
|
|
1293
|
+
* Withdraw collateral. `minAssetAmountOut` and `minLiquidationCfBps` are the
|
|
1294
|
+
* caller's slippage and health floors; the program rejects the withdrawal
|
|
1295
|
+
* rather than silently returning less.
|
|
1296
|
+
*/
|
|
1297
|
+
async withdrawCollateralInstruction(
|
|
1298
|
+
params: WithdrawCollateralParams
|
|
1299
|
+
): Promise<TransactionInstruction> {
|
|
1300
|
+
const market = address(params.market);
|
|
1301
|
+
const owner = address(params.owner);
|
|
1302
|
+
const assetMint = address(params.assetMint);
|
|
1303
|
+
const positionId = address(params.positionId);
|
|
1304
|
+
return this.instruction(
|
|
1305
|
+
"withdrawCollateral" as DuskInstructionName,
|
|
1306
|
+
{
|
|
1307
|
+
withdrawAmount: integerBN(params.withdrawAmount, "withdrawAmount"),
|
|
1308
|
+
minAssetAmountOut: integerBN(params.minAssetAmountOut, "minAssetAmountOut"),
|
|
1309
|
+
minLiquidationCfBps: params.minLiquidationCfBps,
|
|
1310
|
+
},
|
|
1311
|
+
{
|
|
1312
|
+
accounts: {
|
|
1313
|
+
market,
|
|
1314
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
1315
|
+
owner,
|
|
1316
|
+
assetMint,
|
|
1317
|
+
collateralVault: address(
|
|
1318
|
+
params.collateralVault ??
|
|
1319
|
+
deriveMarketCollateralVaultAddress(market, assetMint)[0]
|
|
1320
|
+
),
|
|
1321
|
+
ownerAssetAccount: address(params.ownerAssetAccount),
|
|
1322
|
+
borrowPosition: address(
|
|
1323
|
+
params.borrowPosition ??
|
|
1324
|
+
deriveBorrowPositionAddress(market, owner, positionId)[0]
|
|
1325
|
+
),
|
|
1326
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1327
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1328
|
+
},
|
|
1329
|
+
remainingAccounts: params.remainingAccounts,
|
|
1330
|
+
}
|
|
1331
|
+
);
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
async withdrawCollateralTransaction(
|
|
1335
|
+
params: WithdrawCollateralParams
|
|
1336
|
+
): Promise<Transaction> {
|
|
1337
|
+
return new Transaction().add(
|
|
1338
|
+
await this.withdrawCollateralInstruction(params)
|
|
1339
|
+
);
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* Repay anyone’s borrowed debt using `owner`’s tokens, without collateral rights.
|
|
1344
|
+
* Pass the wallet balance as `repayAmount` to clear a
|
|
1345
|
+
* position; the program repays at most the outstanding debt.
|
|
1346
|
+
*/
|
|
1347
|
+
async repayInstruction(params: RepayParams): Promise<TransactionInstruction> {
|
|
1348
|
+
const market = address(params.market);
|
|
1349
|
+
const owner = address(params.owner);
|
|
1350
|
+
const debtAssetMint = address(params.debtAssetMint);
|
|
1351
|
+
const positionId = address(params.positionId);
|
|
1352
|
+
const referralPartner = params.referralPartner
|
|
1353
|
+
? address(params.referralPartner)
|
|
1354
|
+
: null;
|
|
1355
|
+
return this.instruction(
|
|
1356
|
+
"repay" as DuskInstructionName,
|
|
1357
|
+
{ repayAmount: integerBN(params.repayAmount, "repayAmount") },
|
|
1358
|
+
{
|
|
1359
|
+
accounts: {
|
|
1360
|
+
market,
|
|
1361
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
1362
|
+
owner,
|
|
1363
|
+
debtAssetMint,
|
|
1364
|
+
collateralAssetMint: address(params.collateralAssetMint),
|
|
1365
|
+
reserveVault: address(
|
|
1366
|
+
params.reserveVault ??
|
|
1367
|
+
deriveMarketReserveVaultAddress(market, debtAssetMint)[0]
|
|
1368
|
+
),
|
|
1369
|
+
interestVault: address(
|
|
1370
|
+
params.interestVault ??
|
|
1371
|
+
deriveMarketInterestVaultAddress(market, debtAssetMint)[0]
|
|
1372
|
+
),
|
|
1373
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
1374
|
+
borrowPosition: address(
|
|
1375
|
+
params.borrowPosition ??
|
|
1376
|
+
deriveBorrowPositionAddress(market, address(params.positionOwner ?? params.owner), positionId)[0]
|
|
1377
|
+
),
|
|
1378
|
+
referralPartner,
|
|
1379
|
+
referralAccrual: referralPartner
|
|
1380
|
+
? deriveReferralAccrualAddress(
|
|
1381
|
+
referralPartner,
|
|
1382
|
+
market,
|
|
1383
|
+
debtAssetMint
|
|
1384
|
+
)[0]
|
|
1385
|
+
: null,
|
|
1386
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1387
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1388
|
+
},
|
|
1389
|
+
remainingAccounts: [...(await this.hlpRemainingAccounts(market)), ...(params.remainingAccounts ?? [])],
|
|
1390
|
+
}
|
|
1391
|
+
);
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
async repayTransaction(params: RepayParams): Promise<Transaction> {
|
|
1395
|
+
return new Transaction().add(await this.repayInstruction(params));
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
/**
|
|
1399
|
+
* Add balanced srLP liquidity. `minSrlpAmount` is the caller's slippage floor;
|
|
1400
|
+
* the program rejects the deposit rather than minting fewer shares.
|
|
1401
|
+
*/
|
|
1402
|
+
async addLiquidityInstruction(
|
|
1403
|
+
params: AddLiquidityParams
|
|
1404
|
+
): Promise<TransactionInstruction> {
|
|
1405
|
+
const common = await this.resolveSrlpAccounts(params);
|
|
1406
|
+
return this.instruction(
|
|
1407
|
+
"addLiquidity" as DuskInstructionName,
|
|
1408
|
+
{
|
|
1409
|
+
baseDepositAmount: integerBN(
|
|
1410
|
+
params.baseDepositAmount,
|
|
1411
|
+
"baseDepositAmount"
|
|
1412
|
+
),
|
|
1413
|
+
quoteDepositAmount: integerBN(
|
|
1414
|
+
params.quoteDepositAmount,
|
|
1415
|
+
"quoteDepositAmount"
|
|
1416
|
+
),
|
|
1417
|
+
minSrlpAmount: integerBN(params.minSrlpAmount, "minSrlpAmount"),
|
|
1418
|
+
},
|
|
1419
|
+
{
|
|
1420
|
+
accounts: {
|
|
1421
|
+
...common,
|
|
1422
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
1423
|
+
},
|
|
1424
|
+
remainingAccounts: params.remainingAccounts,
|
|
1425
|
+
}
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
async addLiquidityTransaction(
|
|
1430
|
+
params: AddLiquidityParams
|
|
1431
|
+
): Promise<Transaction> {
|
|
1432
|
+
return new Transaction().add(await this.addLiquidityInstruction(params));
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* Burn srLP for the underlying pair. Both minimums are enforced by the
|
|
1437
|
+
* program, so a partially-filled withdrawal fails instead of silently
|
|
1438
|
+
* returning less of one side.
|
|
1439
|
+
*/
|
|
1440
|
+
async removeLiquidityInstruction(
|
|
1441
|
+
params: RemoveLiquidityParams
|
|
1442
|
+
): Promise<TransactionInstruction> {
|
|
1443
|
+
const common = await this.resolveSrlpAccounts(params);
|
|
1444
|
+
return this.instruction(
|
|
1445
|
+
"removeLiquidity" as DuskInstructionName,
|
|
1446
|
+
{
|
|
1447
|
+
srlpAmount: integerBN(params.srlpAmount, "srlpAmount"),
|
|
1448
|
+
minBaseAmountOut: integerBN(
|
|
1449
|
+
params.minBaseAmountOut,
|
|
1450
|
+
"minBaseAmountOut"
|
|
1451
|
+
),
|
|
1452
|
+
minQuoteAmountOut: integerBN(
|
|
1453
|
+
params.minQuoteAmountOut,
|
|
1454
|
+
"minQuoteAmountOut"
|
|
1455
|
+
),
|
|
1456
|
+
},
|
|
1457
|
+
{ accounts: common, remainingAccounts: params.remainingAccounts }
|
|
1458
|
+
);
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
async removeLiquidityTransaction(
|
|
1462
|
+
params: RemoveLiquidityParams
|
|
1463
|
+
): Promise<Transaction> {
|
|
1464
|
+
return new Transaction().add(await this.removeLiquidityInstruction(params));
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
/**
|
|
1468
|
+
* Accounts shared by both balanced srLP instructions. Reserve vaults and srLP
|
|
1469
|
+
* yield accounts derive from the market; token programs resolve per mint so
|
|
1470
|
+
* a Token-2022 side needs no caller branching.
|
|
1471
|
+
*/
|
|
1472
|
+
/** Borrow more against an open position and receive collateral. */
|
|
1473
|
+
async increaseLeverageInstruction(params: IncreaseLeverageParams): Promise<TransactionInstruction> {
|
|
1474
|
+
return this.fundLeverageInstruction(params, 0n);
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/** Add debt-token equity and optional borrowing to the same position. */
|
|
1478
|
+
async addToLeverageInstruction(params: AddToLeverageParams): Promise<TransactionInstruction> {
|
|
1479
|
+
if (BigInt(params.marginAmount.toString()) <= 0n) throw new RangeError("marginAmount must be positive");
|
|
1480
|
+
return this.fundLeverageInstruction(params, params.marginAmount, params.ownerDebtAccount);
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
async addToLeverageTransaction(params: AddToLeverageParams): Promise<Transaction> {
|
|
1484
|
+
return new Transaction().add(await this.addToLeverageInstruction(params));
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
private async fundLeverageInstruction(
|
|
1488
|
+
params: IncreaseLeverageParams, marginAmount: RawAmount, ownerDebtAccount?: AddressLike
|
|
1489
|
+
): Promise<TransactionInstruction> {
|
|
1490
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
1491
|
+
return this.instruction(
|
|
1492
|
+
"increaseLeverage" as DuskInstructionName,
|
|
1493
|
+
{
|
|
1494
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
1495
|
+
debtAmount: integerBN(params.debtAmount, "debtAmount"),
|
|
1496
|
+
marginAmount: integerBN(marginAmount, "marginAmount"),
|
|
1497
|
+
minCollateralOut: integerBN(
|
|
1498
|
+
params.minCollateralOut,
|
|
1499
|
+
"minCollateralOut"
|
|
1500
|
+
),
|
|
1501
|
+
},
|
|
1502
|
+
{
|
|
1503
|
+
accounts: {
|
|
1504
|
+
market: core.market,
|
|
1505
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
1506
|
+
positionOwner: core.positionOwner,
|
|
1507
|
+
leveragePosition: core.leveragePosition,
|
|
1508
|
+
debtMint: core.debtMint,
|
|
1509
|
+
collateralMint: core.collateralMint,
|
|
1510
|
+
debtReserveVault: core.debtReserveVault,
|
|
1511
|
+
collateralReserveVault: core.collateralReserveVault,
|
|
1512
|
+
leverageCollateralVault: core.leverageCollateralVault,
|
|
1513
|
+
owner: core.owner,
|
|
1514
|
+
ownerDebtAccount: ownerDebtAccount ? address(ownerDebtAccount) : null,
|
|
1515
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
1516
|
+
tokenProgram: core.tokenProgram,
|
|
1517
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1518
|
+
},
|
|
1519
|
+
remainingAccounts: [
|
|
1520
|
+
...(await this.hlpRemainingAccounts(core.market)),
|
|
1521
|
+
...(params.remainingAccounts ?? []),
|
|
1522
|
+
],
|
|
1523
|
+
}
|
|
1524
|
+
);
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
async increaseLeverageTransaction(
|
|
1528
|
+
params: IncreaseLeverageParams
|
|
1529
|
+
): Promise<Transaction> {
|
|
1530
|
+
return new Transaction().add(
|
|
1531
|
+
await this.increaseLeverageInstruction(params)
|
|
1532
|
+
);
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
/** Sell collateral to repay debt without closing the position. */
|
|
1536
|
+
async decreaseLeverageInstruction(
|
|
1537
|
+
params: DecreaseLeverageParams
|
|
1538
|
+
): Promise<TransactionInstruction> {
|
|
1539
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
1540
|
+
return this.instruction(
|
|
1541
|
+
"decreaseLeverage" as DuskInstructionName,
|
|
1542
|
+
{
|
|
1543
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
1544
|
+
collateralAmount: integerBN(
|
|
1545
|
+
params.collateralAmount,
|
|
1546
|
+
"collateralAmount"
|
|
1547
|
+
),
|
|
1548
|
+
minRepayOut: integerBN(params.minRepayOut, "minRepayOut"),
|
|
1549
|
+
maxSurplusOut: integerBN(params.maxSurplusOut ?? 0n, "maxSurplusOut"),
|
|
1550
|
+
},
|
|
1551
|
+
{
|
|
1552
|
+
accounts: {
|
|
1553
|
+
market: core.market,
|
|
1554
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
1555
|
+
positionOwner: core.positionOwner,
|
|
1556
|
+
leveragePosition: core.leveragePosition,
|
|
1557
|
+
debtMint: core.debtMint,
|
|
1558
|
+
collateralMint: core.collateralMint,
|
|
1559
|
+
debtReserveVault: core.debtReserveVault,
|
|
1560
|
+
collateralReserveVault: core.collateralReserveVault,
|
|
1561
|
+
debtInterestVault: core.debtInterestVault,
|
|
1562
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
1563
|
+
leverageCollateralVault: core.leverageCollateralVault,
|
|
1564
|
+
referralPartner: core.referralPartner,
|
|
1565
|
+
referralAccrual: core.referralAccrual,
|
|
1566
|
+
owner: core.owner,
|
|
1567
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
1568
|
+
tokenProgram: core.tokenProgram,
|
|
1569
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1570
|
+
},
|
|
1571
|
+
remainingAccounts: [
|
|
1572
|
+
...(await this.hlpRemainingAccounts(core.market)),
|
|
1573
|
+
...(params.remainingAccounts ?? []),
|
|
1574
|
+
],
|
|
1575
|
+
}
|
|
1576
|
+
);
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
async decreaseLeverageTransaction(
|
|
1580
|
+
params: DecreaseLeverageParams
|
|
1581
|
+
): Promise<Transaction> {
|
|
1582
|
+
return new Transaction().add(
|
|
1583
|
+
await this.decreaseLeverageInstruction(params)
|
|
1584
|
+
);
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
/** Repay anyone’s leverage debt from `owner`’s tokens; collateral stays with the position owner. */
|
|
1588
|
+
async addLeverageMarginInstruction(params: LeverageMarginParams): Promise<TransactionInstruction> {
|
|
1589
|
+
return this.repayLeverageInstruction(params);
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
/** Permissionless repayment without an unnecessary collateral-sale quote. */
|
|
1593
|
+
async repayLeverageInstruction(params: LeverageMarginParams): Promise<TransactionInstruction> {
|
|
1594
|
+
return this.leverageRepaymentInstruction(params, "repayLeverage");
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
private async leverageRepaymentInstruction(
|
|
1598
|
+
params: LeverageMarginParams, method: "addLeverageMargin" | "repayLeverage"
|
|
1599
|
+
): Promise<TransactionInstruction> {
|
|
1600
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
1601
|
+
return this.instruction(
|
|
1602
|
+
method as DuskInstructionName,
|
|
1603
|
+
{
|
|
1604
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
1605
|
+
amount: integerBN(params.amount, "amount"),
|
|
1606
|
+
},
|
|
1607
|
+
{
|
|
1608
|
+
accounts: {
|
|
1609
|
+
market: core.market,
|
|
1610
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
1611
|
+
positionOwner: core.positionOwner,
|
|
1612
|
+
leveragePosition: core.leveragePosition,
|
|
1613
|
+
debtMint: core.debtMint,
|
|
1614
|
+
collateralMint: core.collateralMint,
|
|
1615
|
+
debtReserveVault: core.debtReserveVault,
|
|
1616
|
+
debtInterestVault: core.debtInterestVault,
|
|
1617
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
1618
|
+
referralPartner: core.referralPartner,
|
|
1619
|
+
referralAccrual: core.referralAccrual,
|
|
1620
|
+
owner: core.owner,
|
|
1621
|
+
tokenProgram: core.tokenProgram,
|
|
1622
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1623
|
+
},
|
|
1624
|
+
remainingAccounts: [...(await this.hlpRemainingAccounts(core.market)), ...(params.remainingAccounts ?? [])],
|
|
1625
|
+
}
|
|
1626
|
+
);
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
async repayLeverageTransaction(params: LeverageMarginParams): Promise<Transaction> {
|
|
1630
|
+
return new Transaction().add(await this.repayLeverageInstruction(params));
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
async addLeverageMarginTransaction(
|
|
1634
|
+
params: LeverageMarginParams
|
|
1635
|
+
): Promise<Transaction> {
|
|
1636
|
+
return new Transaction().add(
|
|
1637
|
+
await this.repayLeverageInstruction(params)
|
|
1638
|
+
);
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
/**
|
|
1642
|
+
* Owner-signed proportional partial/full close. Delegated closes use the delegate client.
|
|
1643
|
+
*/
|
|
1644
|
+
async closeLeverageInstruction(
|
|
1645
|
+
params: CloseLeverageParams,
|
|
1646
|
+
marketSnapshot?: Market
|
|
1647
|
+
): Promise<TransactionInstruction> {
|
|
1648
|
+
if ("collateralFunded" in params && params.collateralFunded === true)
|
|
1649
|
+
throw new Error("Collateral-funded leverage closes are no longer supported");
|
|
1650
|
+
const closeBps = params.closeBps ?? 10_000;
|
|
1651
|
+
if (!Number.isSafeInteger(closeBps) || closeBps <= 0 || closeBps > 10_000)
|
|
1652
|
+
throw new RangeError("closeBps must be between 1 and 10000");
|
|
1653
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
1654
|
+
return this.instruction(
|
|
1655
|
+
"closeLeverage",
|
|
1656
|
+
{
|
|
1657
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
1658
|
+
minAmountOut: integerBN(params.minAmountOut, "minAmountOut"),
|
|
1659
|
+
closeBps,
|
|
1660
|
+
},
|
|
1661
|
+
{
|
|
1662
|
+
accounts: {
|
|
1663
|
+
market: core.market,
|
|
1664
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
1665
|
+
positionOwner: core.positionOwner,
|
|
1666
|
+
leveragePosition: core.leveragePosition,
|
|
1667
|
+
debtMint: core.debtMint,
|
|
1668
|
+
collateralMint: core.collateralMint,
|
|
1669
|
+
debtReserveVault: core.debtReserveVault,
|
|
1670
|
+
collateralReserveVault: core.collateralReserveVault,
|
|
1671
|
+
debtInterestVault: core.debtInterestVault,
|
|
1672
|
+
leverageCollateralVault: core.leverageCollateralVault,
|
|
1673
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
1674
|
+
delegateFeeRecipient: params.delegateFeeRecipient
|
|
1675
|
+
? address(params.delegateFeeRecipient)
|
|
1676
|
+
: null,
|
|
1677
|
+
delegateExecutorAccount: params.delegateExecutorAccount
|
|
1678
|
+
? address(params.delegateExecutorAccount)
|
|
1679
|
+
: null,
|
|
1680
|
+
referralPartner: core.referralPartner,
|
|
1681
|
+
referralAccrual: core.referralAccrual,
|
|
1682
|
+
leverageDelegation: params.leverageDelegation
|
|
1683
|
+
? address(params.leverageDelegation)
|
|
1684
|
+
: null,
|
|
1685
|
+
delegatedProgram: params.delegatedProgram
|
|
1686
|
+
? address(params.delegatedProgram)
|
|
1687
|
+
: null,
|
|
1688
|
+
authority: address(params.authority ?? params.positionOwner),
|
|
1689
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
1690
|
+
tokenProgram: core.tokenProgram,
|
|
1691
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1692
|
+
},
|
|
1693
|
+
remainingAccounts: [
|
|
1694
|
+
...(await this.hlpRemainingAccounts(core.market, marketSnapshot)),
|
|
1695
|
+
...(params.remainingAccounts ?? []),
|
|
1696
|
+
],
|
|
1697
|
+
}
|
|
1698
|
+
);
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
async closeLeverageTransaction(
|
|
1702
|
+
params: CloseLeverageParams
|
|
1703
|
+
): Promise<Transaction> {
|
|
1704
|
+
return new Transaction().add(await this.closeLeverageInstruction(params));
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
/**
|
|
1708
|
+
* Accounts shared across the leverage lifecycle. Individual builders pass
|
|
1709
|
+
* through only the subset their instruction declares.
|
|
1710
|
+
*/
|
|
1711
|
+
private async resolveLeverageAccounts(params: LeverageAccounts) {
|
|
1712
|
+
const market = address(params.market);
|
|
1713
|
+
const positionOwner = address(params.positionOwner);
|
|
1714
|
+
const debtMint = address(params.debtMint);
|
|
1715
|
+
const collateralMint = address(params.collateralMint);
|
|
1716
|
+
const positionId = address(params.positionId);
|
|
1717
|
+
const referralPartner = params.referralPartner
|
|
1718
|
+
? address(params.referralPartner)
|
|
1719
|
+
: null;
|
|
1720
|
+
return {
|
|
1721
|
+
market,
|
|
1722
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
1723
|
+
positionOwner,
|
|
1724
|
+
owner: address(params.owner ?? params.positionOwner),
|
|
1725
|
+
leveragePosition: address(
|
|
1726
|
+
params.leveragePosition ??
|
|
1727
|
+
deriveLeveragePositionAddress(market, positionOwner, positionId, address(params.namespaceAuthority ?? params.positionOwner))[0]
|
|
1728
|
+
),
|
|
1729
|
+
debtMint,
|
|
1730
|
+
collateralMint,
|
|
1731
|
+
debtReserveVault: address(
|
|
1732
|
+
params.debtReserveVault ??
|
|
1733
|
+
deriveMarketReserveVaultAddress(market, debtMint)[0]
|
|
1734
|
+
),
|
|
1735
|
+
collateralReserveVault: address(
|
|
1736
|
+
params.collateralReserveVault ??
|
|
1737
|
+
deriveMarketReserveVaultAddress(market, collateralMint)[0]
|
|
1738
|
+
),
|
|
1739
|
+
debtInterestVault: address(
|
|
1740
|
+
params.debtInterestVault ??
|
|
1741
|
+
deriveMarketInterestVaultAddress(market, debtMint)[0]
|
|
1742
|
+
),
|
|
1743
|
+
leverageCollateralVault: address(
|
|
1744
|
+
params.leverageCollateralVault ??
|
|
1745
|
+
deriveLeverageCollateralVaultAddress(market, collateralMint)[0]
|
|
1746
|
+
),
|
|
1747
|
+
referralPartner,
|
|
1748
|
+
referralAccrual: referralPartner
|
|
1749
|
+
? deriveReferralAccrualAddress(referralPartner, market, debtMint)[0]
|
|
1750
|
+
: null,
|
|
1751
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1752
|
+
};
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
private async resolveSrlpAccounts(params: SrlpLiquidityAccounts) {
|
|
1756
|
+
const market = address(params.market);
|
|
1757
|
+
const owner = address(params.owner);
|
|
1758
|
+
const baseMint = address(params.baseMint);
|
|
1759
|
+
const quoteMint = address(params.quoteMint);
|
|
1760
|
+
const srlpMint = address(params.srlpMint);
|
|
1761
|
+
return {
|
|
1762
|
+
market,
|
|
1763
|
+
owner,
|
|
1764
|
+
baseMint,
|
|
1765
|
+
quoteMint,
|
|
1766
|
+
srlpMint,
|
|
1767
|
+
baseReserveVault: address(
|
|
1768
|
+
params.baseReserveVault ??
|
|
1769
|
+
deriveMarketReserveVaultAddress(market, baseMint)[0]
|
|
1770
|
+
),
|
|
1771
|
+
quoteReserveVault: address(
|
|
1772
|
+
params.quoteReserveVault ??
|
|
1773
|
+
deriveMarketReserveVaultAddress(market, quoteMint)[0]
|
|
1774
|
+
),
|
|
1775
|
+
ownerBaseAccount: address(params.ownerBaseAccount),
|
|
1776
|
+
ownerQuoteAccount: address(params.ownerQuoteAccount),
|
|
1777
|
+
ownerSrlpAccount: address(params.ownerSrlpAccount),
|
|
1778
|
+
baseYieldAccount: address(
|
|
1779
|
+
params.baseYieldAccount ??
|
|
1780
|
+
deriveYieldAccountAddress(
|
|
1781
|
+
market,
|
|
1782
|
+
owner,
|
|
1783
|
+
srlpMint,
|
|
1784
|
+
baseMint,
|
|
1785
|
+
"srlp",
|
|
1786
|
+
this.program.programId
|
|
1787
|
+
)[0]
|
|
1788
|
+
),
|
|
1789
|
+
quoteYieldAccount: address(
|
|
1790
|
+
params.quoteYieldAccount ??
|
|
1791
|
+
deriveYieldAccountAddress(
|
|
1792
|
+
market,
|
|
1793
|
+
owner,
|
|
1794
|
+
srlpMint,
|
|
1795
|
+
quoteMint,
|
|
1796
|
+
"srlp",
|
|
1797
|
+
this.program.programId
|
|
1798
|
+
)[0]
|
|
1799
|
+
),
|
|
1800
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1801
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1802
|
+
};
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
/** Raw base-unit amount. Accepts bigint to keep callers off floating point. */
|
|
1807
|
+
export type RawAmount = bigint | number | string;
|
|
1808
|
+
|
|
1809
|
+
interface LendingPositionAccounts {
|
|
1810
|
+
market: AddressLike;
|
|
1811
|
+
owner: AddressLike;
|
|
1812
|
+
/** Beneficiary when a different wallet pays for this position. */
|
|
1813
|
+
positionOwner?: AddressLike;
|
|
1814
|
+
/** Position discriminator; the borrow position PDA derives from it. */
|
|
1815
|
+
positionId: AddressLike;
|
|
1816
|
+
borrowPosition?: AddressLike;
|
|
1817
|
+
remainingAccounts?: AccountMeta[];
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
export interface DepositCollateralParams extends LendingPositionAccounts {
|
|
1821
|
+
assetMint: AddressLike;
|
|
1822
|
+
ownerAssetAccount: AddressLike;
|
|
1823
|
+
depositAmount: RawAmount;
|
|
1824
|
+
collateralVault?: AddressLike;
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
export interface WithdrawCollateralParams extends LendingPositionAccounts {
|
|
1828
|
+
assetMint: AddressLike;
|
|
1829
|
+
ownerAssetAccount: AddressLike;
|
|
1830
|
+
withdrawAmount: RawAmount;
|
|
1831
|
+
minAssetAmountOut: RawAmount;
|
|
1832
|
+
/** Health floor enforced by the program, in basis points. */
|
|
1833
|
+
minLiquidationCfBps: number;
|
|
1834
|
+
collateralVault?: AddressLike;
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
export interface RepayParams extends LendingPositionAccounts {
|
|
1838
|
+
debtAssetMint: AddressLike;
|
|
1839
|
+
collateralAssetMint: AddressLike;
|
|
1840
|
+
ownerDebtAccount: AddressLike;
|
|
1841
|
+
repayAmount: RawAmount;
|
|
1842
|
+
/** Omit when the position has no referrer. */
|
|
1843
|
+
referralPartner?: AddressLike | null;
|
|
1844
|
+
reserveVault?: AddressLike;
|
|
1845
|
+
interestVault?: AddressLike;
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
/** Which side of the pair carries the debt. */
|
|
1849
|
+
export type MarketAssetSide = "base" | "quote";
|
|
1850
|
+
|
|
1851
|
+
function marketAssetIndex(side: MarketAssetSide): number {
|
|
1852
|
+
if (side !== "base" && side !== "quote") {
|
|
1853
|
+
throw new Error(`debtAsset must be "base" or "quote", got ${String(side)}`);
|
|
1854
|
+
}
|
|
1855
|
+
return side === "base" ? 0 : 1;
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
interface LeverageAccounts {
|
|
1859
|
+
market: AddressLike;
|
|
1860
|
+
positionOwner: AddressLike;
|
|
1861
|
+
/** Position discriminator; the leverage position PDA derives from it. */
|
|
1862
|
+
positionId: AddressLike;
|
|
1863
|
+
debtMint: AddressLike;
|
|
1864
|
+
collateralMint: AddressLike;
|
|
1865
|
+
debtAsset: MarketAssetSide;
|
|
1866
|
+
/** Signer when it differs from the position owner. */
|
|
1867
|
+
owner?: AddressLike;
|
|
1868
|
+
/** Signer that originally funded the position, if different from its owner. */
|
|
1869
|
+
namespaceAuthority?: AddressLike;
|
|
1870
|
+
leveragePosition?: AddressLike;
|
|
1871
|
+
debtReserveVault?: AddressLike;
|
|
1872
|
+
collateralReserveVault?: AddressLike;
|
|
1873
|
+
debtInterestVault?: AddressLike;
|
|
1874
|
+
leverageCollateralVault?: AddressLike;
|
|
1875
|
+
referralPartner?: AddressLike | null;
|
|
1876
|
+
remainingAccounts?: AccountMeta[];
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
export interface IncreaseLeverageParams extends LeverageAccounts {
|
|
1880
|
+
debtAmount: RawAmount;
|
|
1881
|
+
minCollateralOut: RawAmount;
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
export interface AddToLeverageParams extends IncreaseLeverageParams {
|
|
1885
|
+
/** Gross wallet funding in the borrowed token. */
|
|
1886
|
+
marginAmount: RawAmount;
|
|
1887
|
+
ownerDebtAccount: AddressLike;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
export interface DecreaseLeverageParams extends LeverageAccounts {
|
|
1891
|
+
collateralAmount: RawAmount;
|
|
1892
|
+
minRepayOut: RawAmount;
|
|
1893
|
+
/** Owner-owned debt-token account for unused repayment output. */
|
|
1894
|
+
ownerDebtAccount: AddressLike;
|
|
1895
|
+
/** Gross surplus limit; defaults to zero. Use a bounded value for 1x rounding. */
|
|
1896
|
+
maxSurplusOut?: RawAmount;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
export interface LeverageMarginParams extends LeverageAccounts {
|
|
1900
|
+
amount: RawAmount;
|
|
1901
|
+
ownerDebtAccount: AddressLike;
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
export interface CloseLeverageParams extends LeverageAccounts {
|
|
1905
|
+
/** Minimum net wallet payout after all transfer fees. */
|
|
1906
|
+
minAmountOut: RawAmount;
|
|
1907
|
+
/** Percentage of the entire position to remove. Default 10000 closes all. */
|
|
1908
|
+
closeBps?: number;
|
|
1909
|
+
/** Payout account owned by the position owner. */
|
|
1910
|
+
ownerDebtAccount: AddressLike;
|
|
1911
|
+
/** Required for delegated closes; the two accounts receive bounded fees. */
|
|
1912
|
+
delegateFeeRecipient?: AddressLike | null;
|
|
1913
|
+
delegateExecutorAccount?: AddressLike | null;
|
|
1914
|
+
/** Delegated settlement; omit these for an owner-signed close. */
|
|
1915
|
+
leverageDelegation?: AddressLike | null;
|
|
1916
|
+
delegatedProgram?: AddressLike | null;
|
|
1917
|
+
authority?: AddressLike;
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
interface SrlpLiquidityAccounts {
|
|
1921
|
+
market: AddressLike;
|
|
1922
|
+
owner: AddressLike;
|
|
1923
|
+
baseMint: AddressLike;
|
|
1924
|
+
quoteMint: AddressLike;
|
|
1925
|
+
srlpMint: AddressLike;
|
|
1926
|
+
ownerBaseAccount: AddressLike;
|
|
1927
|
+
ownerQuoteAccount: AddressLike;
|
|
1928
|
+
ownerSrlpAccount: AddressLike;
|
|
1929
|
+
baseReserveVault?: AddressLike;
|
|
1930
|
+
quoteReserveVault?: AddressLike;
|
|
1931
|
+
baseYieldAccount?: AddressLike;
|
|
1932
|
+
quoteYieldAccount?: AddressLike;
|
|
1933
|
+
remainingAccounts?: AccountMeta[];
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
/** Which side of the market a leverage position borrows. */
|
|
1937
|
+
export type LeverageDebtAsset = "base" | "quote";
|
|
1938
|
+
|
|
1939
|
+
/** Opening leverage funded in the debt asset; margin plus borrowing buys collateral. */
|
|
1940
|
+
export interface OpenLeverageParams {
|
|
1941
|
+
market: AddressLike;
|
|
1942
|
+
owner: AddressLike;
|
|
1943
|
+
positionId: AddressLike;
|
|
1944
|
+
debtAsset: LeverageDebtAsset;
|
|
1945
|
+
debtMint: AddressLike;
|
|
1946
|
+
collateralMint: AddressLike;
|
|
1947
|
+
marginAmount: RawAmount;
|
|
1948
|
+
/** Leverage multiplier in basis points; 20000 is two times. */
|
|
1949
|
+
multiplierBps: RawAmount;
|
|
1950
|
+
minCollateralOut: RawAmount;
|
|
1951
|
+
/** Zero means no limit price. */
|
|
1952
|
+
limitPriceNad?: RawAmount;
|
|
1953
|
+
payer?: AddressLike;
|
|
1954
|
+
ownerDebtAccount?: AddressLike;
|
|
1955
|
+
leveragePosition?: AddressLike;
|
|
1956
|
+
debtReserveVault?: AddressLike;
|
|
1957
|
+
collateralReserveVault?: AddressLike;
|
|
1958
|
+
leverageCollateralVault?: AddressLike;
|
|
1959
|
+
remainingAccounts?: AccountMeta[];
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
/** Authorising a program to act on a leverage position. */
|
|
1963
|
+
export interface CreateLeverageDelegationParams {
|
|
1964
|
+
market: AddressLike;
|
|
1965
|
+
owner: AddressLike;
|
|
1966
|
+
namespaceAuthority?: AddressLike;
|
|
1967
|
+
positionId: AddressLike;
|
|
1968
|
+
debtAsset: LeverageDebtAsset;
|
|
1969
|
+
delegatedProgram: AddressLike;
|
|
1970
|
+
/** Bit flags for the actions the delegate may perform. */
|
|
1971
|
+
approvedActions: number;
|
|
1972
|
+
leveragePosition?: AddressLike;
|
|
1973
|
+
leverageDelegation?: AddressLike;
|
|
1974
|
+
remainingAccounts?: AccountMeta[];
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
/** A borrow described by its market, position and mints. */
|
|
1978
|
+
export interface BorrowParams {
|
|
1979
|
+
market: AddressLike;
|
|
1980
|
+
owner: AddressLike;
|
|
1981
|
+
/** Position discriminator; the borrow position PDA derives from it. */
|
|
1982
|
+
positionId: AddressLike;
|
|
1983
|
+
debtAssetMint: AddressLike;
|
|
1984
|
+
collateralAssetMint: AddressLike;
|
|
1985
|
+
borrowAmount: RawAmount;
|
|
1986
|
+
/** Slippage floor on the debt actually opened. */
|
|
1987
|
+
minDebtAmountOut: RawAmount;
|
|
1988
|
+
minLiquidationCfBps?: number;
|
|
1989
|
+
ownerDebtAccount?: AddressLike;
|
|
1990
|
+
reserveVault?: AddressLike;
|
|
1991
|
+
borrowPosition?: AddressLike;
|
|
1992
|
+
remainingAccounts?: AccountMeta[];
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
/** A swap described by its market and mints rather than by an account map. */
|
|
1996
|
+
export interface SwapParams {
|
|
1997
|
+
market: AddressLike;
|
|
1998
|
+
trader: AddressLike;
|
|
1999
|
+
assetInMint: AddressLike;
|
|
2000
|
+
assetOutMint: AddressLike;
|
|
2001
|
+
exactAssetIn: RawAmount;
|
|
2002
|
+
/** Slippage floor; the program rejects a fill below it. */
|
|
2003
|
+
minAssetOut: RawAmount;
|
|
2004
|
+
traderAssetInAccount?: AddressLike;
|
|
2005
|
+
traderAssetOutAccount?: AddressLike;
|
|
2006
|
+
reserveInVault?: AddressLike;
|
|
2007
|
+
reserveOutVault?: AddressLike;
|
|
2008
|
+
remainingAccounts?: AccountMeta[];
|
|
2009
|
+
}
|
|
2010
|
+
|
|
2011
|
+
export interface AddLiquidityParams extends SrlpLiquidityAccounts {
|
|
2012
|
+
/** Maximum gross spend. Guided first deposits pair at the current validated
|
|
2013
|
+
* oracle price; unused amounts remain with the owner. Native seeding uses
|
|
2014
|
+
* both amounts to establish the initial ratio. */
|
|
2015
|
+
baseDepositAmount: RawAmount;
|
|
2016
|
+
quoteDepositAmount: RawAmount;
|
|
2017
|
+
/** Slippage floor on minted srLP shares. */
|
|
2018
|
+
minSrlpAmount: RawAmount;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
export interface RemoveLiquidityParams extends SrlpLiquidityAccounts {
|
|
2022
|
+
srlpAmount: RawAmount;
|
|
2023
|
+
minBaseAmountOut: RawAmount;
|
|
2024
|
+
minQuoteAmountOut: RawAmount;
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
/**
|
|
2028
|
+
* Widen a launch config into the shape Anchor expects: every `u64` as a BN,
|
|
2029
|
+
* every `u16` left as a number. Written out field by field rather than mapped,
|
|
2030
|
+
* so a field added to the program fails to compile here instead of silently
|
|
2031
|
+
* going missing.
|
|
2032
|
+
*/
|
|
2033
|
+
function anchorMarketConfig(config: MarketLaunchConfig): Record<string, unknown> {
|
|
2034
|
+
const bn = (value: IntegerLike, label: string) => integerBN(value, label);
|
|
2035
|
+
if (bn(config.amm.peakAmplificationNad as IntegerLike, "peakAmplificationNad").gtn(1_000_000_000)
|
|
2036
|
+
&& bn(config.amm.adjustmentStepNad as IntegerLike, "adjustmentStepNad").isZero()) {
|
|
2037
|
+
throw new Error("concentrated markets require an enabled center controller");
|
|
2038
|
+
}
|
|
2039
|
+
return {
|
|
2040
|
+
swapFeeBps: config.swapFeeBps,
|
|
2041
|
+
divergenceFeeShareCapBps: config.divergenceFeeShareCapBps,
|
|
2042
|
+
volatilityFeeShareCapBps: config.volatilityFeeShareCapBps,
|
|
2043
|
+
targetHlpLeverageBps: config.targetHlpLeverageBps,
|
|
2044
|
+
settlementDivergenceBps: config.settlementDivergenceBps,
|
|
2045
|
+
emaHalfLifeMs: bn(config.emaHalfLifeMs, "emaHalfLifeMs"),
|
|
2046
|
+
directionalEmaHalfLifeMs: bn(
|
|
2047
|
+
config.directionalEmaHalfLifeMs,
|
|
2048
|
+
"directionalEmaHalfLifeMs"
|
|
2049
|
+
),
|
|
2050
|
+
curveDepthEmaHalfLifeMs: bn(config.curveDepthEmaHalfLifeMs, "curveDepthEmaHalfLifeMs"),
|
|
2051
|
+
maxDailyBorrowBps: config.maxDailyBorrowBps,
|
|
2052
|
+
globalHealthContributionCapBps: config.globalHealthContributionCapBps,
|
|
2053
|
+
borrowMarketHealthFloorBps: config.borrowMarketHealthFloorBps,
|
|
2054
|
+
amm: anchorIntegerFields(config.amm, AMM_U64_FIELDS),
|
|
2055
|
+
irm: anchorIntegerFields(config.irm, IRM_U64_FIELDS),
|
|
2056
|
+
startTime: bn(config.startTime, "startTime"),
|
|
2057
|
+
};
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
const AMM_U64_FIELDS = [
|
|
2061
|
+
"peakAmplificationNad",
|
|
2062
|
+
"centerEmaHalfLifeMs",
|
|
2063
|
+
"volatilityHalfLifeMs",
|
|
2064
|
+
"adjustmentStepNad",
|
|
2065
|
+
"volatilityShockCapNad",
|
|
2066
|
+
"volatilityCapNad",
|
|
2067
|
+
"divergenceFeeCoefficientNad",
|
|
2068
|
+
"volatilityFeeCoefficientNad",
|
|
2069
|
+
"launchFeeDurationSeconds",
|
|
2070
|
+
"launchRateLimitReferenceNad",
|
|
2071
|
+
"launchRateLimitDurationSeconds",
|
|
2072
|
+
] as const;
|
|
2073
|
+
|
|
2074
|
+
const IRM_U64_FIELDS = ["lowerSignalNad", "upperSignalNad", "halfLifeMs", "initialAprNad"] as const;
|
|
2075
|
+
|
|
2076
|
+
function anchorIntegerFields(
|
|
2077
|
+
source: Record<string, unknown>,
|
|
2078
|
+
wide: readonly string[]
|
|
2079
|
+
): Record<string, unknown> {
|
|
2080
|
+
const out: Record<string, unknown> = { ...source };
|
|
2081
|
+
for (const field of wide) {
|
|
2082
|
+
if (out[field] === undefined) continue;
|
|
2083
|
+
out[field] = integerBN(out[field] as IntegerLike, field);
|
|
2084
|
+
}
|
|
2085
|
+
return out;
|
|
529
2086
|
}
|
|
530
2087
|
|
|
531
2088
|
function normalizeArgs(args: DuskInstructionArgs): unknown[] {
|
|
@@ -550,3 +2107,68 @@ function mergeAccountMetas(...groups: readonly AccountMeta[][]): AccountMeta[] {
|
|
|
550
2107
|
}
|
|
551
2108
|
return [...merged.values()];
|
|
552
2109
|
}
|
|
2110
|
+
|
|
2111
|
+
export interface WithdrawAllCollateralParams extends LendingPositionAccounts {
|
|
2112
|
+
baseMint: AddressLike;
|
|
2113
|
+
quoteMint: AddressLike;
|
|
2114
|
+
ownerBaseAccount: AddressLike;
|
|
2115
|
+
ownerQuoteAccount: AddressLike;
|
|
2116
|
+
minBaseOut: RawAmount;
|
|
2117
|
+
minQuoteOut: RawAmount;
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
export interface WithdrawRepaidLeverageParams {
|
|
2121
|
+
market: AddressLike;
|
|
2122
|
+
owner: AddressLike;
|
|
2123
|
+
namespaceAuthority?: AddressLike;
|
|
2124
|
+
positionId: AddressLike;
|
|
2125
|
+
leveragePosition?: AddressLike;
|
|
2126
|
+
collateralMint: AddressLike;
|
|
2127
|
+
ownerCollateralAccount: AddressLike;
|
|
2128
|
+
minCollateralOut: RawAmount;
|
|
2129
|
+
remainingAccounts?: AccountMeta[];
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
// Kept in step with the on-chain instruction binding boundary.
|
|
2133
|
+
const ORACLE_READ_INSTRUCTIONS = new Set<string>([
|
|
2134
|
+
"addLiquidity",
|
|
2135
|
+
"openLiquidityGates",
|
|
2136
|
+
"removeLiquidity",
|
|
2137
|
+
"fundInterest",
|
|
2138
|
+
"harvest",
|
|
2139
|
+
"swap",
|
|
2140
|
+
"previewHlpFlash",
|
|
2141
|
+
"beginHlpFlash",
|
|
2142
|
+
"settleHlpFlash",
|
|
2143
|
+
"closeInsolventHlp",
|
|
2144
|
+
"beginFlashLiquidation",
|
|
2145
|
+
"previewFlashLiquidation",
|
|
2146
|
+
"previewFlashAmmLiquidation",
|
|
2147
|
+
"emergencyLiquidation",
|
|
2148
|
+
"previewEmergencyLiquidation",
|
|
2149
|
+
"previewLiquidationHealth",
|
|
2150
|
+
"settleFlashLiquidation",
|
|
2151
|
+
"depositCollateral",
|
|
2152
|
+
"withdrawCollateral",
|
|
2153
|
+
"borrow",
|
|
2154
|
+
"repay",
|
|
2155
|
+
"donateCollateral",
|
|
2156
|
+
"openLeverage",
|
|
2157
|
+
"closeLeverage",
|
|
2158
|
+
"delegatedCloseLeverage",
|
|
2159
|
+
"increaseLeverage",
|
|
2160
|
+
"decreaseLeverage",
|
|
2161
|
+
"addLeverageMargin",
|
|
2162
|
+
"repayLeverage",
|
|
2163
|
+
"previewHlpDepositCapacity",
|
|
2164
|
+
"previewMarket",
|
|
2165
|
+
"previewHlpOrderTrigger",
|
|
2166
|
+
"previewAddLiquidity",
|
|
2167
|
+
"previewSwap",
|
|
2168
|
+
"previewBorrowCapacity",
|
|
2169
|
+
"previewBorrowPositionCapacity",
|
|
2170
|
+
"previewBorrowPosition",
|
|
2171
|
+
"depositSingleSided",
|
|
2172
|
+
"withdrawSingleSided",
|
|
2173
|
+
"executeOperatorChange",
|
|
2174
|
+
]);
|