@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/dist/write.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { deriveJuniorCohortAddress } from "./constants.js";
|
|
2
|
+
import { oracleComputeBudgetInstructions, oraclePolicy, oracleReadAccounts } from "./oracle.js";
|
|
3
|
+
import { getAssociatedTokenAddressSync, getMint, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, } from "@solana/spl-token";
|
|
4
|
+
import { SystemProgram, SYSVAR_INSTRUCTIONS_PUBKEY, Transaction, } from "@solana/web3.js";
|
|
3
5
|
import { address, normalizeAccountKeys } from "./address.js";
|
|
4
|
-
import { deriveFutarchyAuthorityAddress, deriveMarketInterestVaultAddress, deriveYieldAccountAddress, deriveYieldTransferHookValidationAddress, deriveReferralPartnerAddress, } from "./constants.js";
|
|
6
|
+
import { deriveBorrowPositionAddress, deriveEventAuthorityAddress, deriveFutarchyAuthorityAddress, deriveLeverageCollateralVaultAddress, deriveLeverageDelegationAddress, deriveLeveragePositionAddress, deriveMarketAddress, deriveMarketCollateralVaultAddress, deriveMarketInterestVaultAddress, deriveMarketReserveVaultAddress, deriveTokenMetadataAddress, TOKEN_METADATA_PROGRAM_ID, deriveReferralAccrualAddress, deriveYieldAccountAddress, deriveYieldTransferHookValidationAddress, deriveHlpCohortAddress, deriveReferralPartnerAddress, } from "./constants.js";
|
|
7
|
+
import { integerBN, } from "./parameters.js";
|
|
5
8
|
import { assertReferralInterestShareBps, referralAccrualAddresses, resolveTransferHookAccountMetas, tokenProgramForMint, } from "./referral.js";
|
|
6
9
|
export class DuskWrite {
|
|
7
10
|
program;
|
|
8
11
|
constructor(program) {
|
|
9
12
|
this.program = program;
|
|
10
13
|
}
|
|
14
|
+
fetchMarketAccount(market) {
|
|
15
|
+
return this.program.account.market.fetch(market);
|
|
16
|
+
}
|
|
11
17
|
method(name, args) {
|
|
12
18
|
const method = this.program.methods[name];
|
|
13
19
|
if (!method) {
|
|
@@ -17,41 +23,113 @@ export class DuskWrite {
|
|
|
17
23
|
}
|
|
18
24
|
builder(name, args, options = {}) {
|
|
19
25
|
let builder = this.method(name, args);
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
const instructionAccounts = this.program.idl.instructions?.find((instruction) => instruction.name === name)?.accounts;
|
|
27
|
+
const usesEventCpi = instructionAccounts?.some((account) => account.name === "eventAuthority") === true &&
|
|
28
|
+
instructionAccounts.some((account) => account.name === "program") === true;
|
|
29
|
+
if (options.accounts || usesEventCpi) {
|
|
30
|
+
const accounts = usesEventCpi
|
|
31
|
+
? {
|
|
32
|
+
...options.accounts,
|
|
33
|
+
eventAuthority: deriveEventAuthorityAddress(this.program.programId)[0],
|
|
34
|
+
program: this.program.programId,
|
|
35
|
+
}
|
|
36
|
+
: options.accounts;
|
|
37
|
+
builder = builder.accounts(normalizeAccountKeys(accounts));
|
|
22
38
|
}
|
|
23
|
-
|
|
24
|
-
|
|
39
|
+
const remaining = [...(options.remainingAccounts ?? [])];
|
|
40
|
+
for (const meta of oracleReadAccounts(options.oraclePolicy)) {
|
|
41
|
+
if (!remaining.some(existing => existing.pubkey.equals(meta.pubkey)))
|
|
42
|
+
remaining.push(meta);
|
|
25
43
|
}
|
|
44
|
+
if (remaining.length)
|
|
45
|
+
builder = builder.remainingAccounts(remaining);
|
|
46
|
+
const budgets = oracleComputeBudgetInstructions(options.oraclePolicy);
|
|
47
|
+
if (budgets.length)
|
|
48
|
+
builder = builder.preInstructions(budgets);
|
|
26
49
|
return builder;
|
|
27
50
|
}
|
|
28
|
-
instruction(name, args, options) {
|
|
29
|
-
return this.builder(name, args, options).instruction();
|
|
51
|
+
async instruction(name, args, options) {
|
|
52
|
+
return this.builder(name, args, await this.withOraclePolicy(name, options)).instruction();
|
|
53
|
+
}
|
|
54
|
+
async transaction(name, args, options) {
|
|
55
|
+
return this.builder(name, args, await this.withOraclePolicy(name, options)).transaction();
|
|
30
56
|
}
|
|
31
|
-
|
|
32
|
-
return this.builder(name, args, options).
|
|
57
|
+
async rpc(name, args, options) {
|
|
58
|
+
return this.builder(name, args, await this.withOraclePolicy(name, options)).rpc();
|
|
33
59
|
}
|
|
34
|
-
|
|
35
|
-
|
|
60
|
+
async withOraclePolicy(name, options = {}) {
|
|
61
|
+
if (!ORACLE_READ_INSTRUCTIONS.has(name) || options.oraclePolicy)
|
|
62
|
+
return options;
|
|
63
|
+
const nested = options.accounts?.accounts;
|
|
64
|
+
const preview = options.accounts?.preview;
|
|
65
|
+
const market = options.accounts?.market ?? nested?.market ?? preview?.market;
|
|
66
|
+
if (!market)
|
|
67
|
+
return options;
|
|
68
|
+
const state = await this.fetchMarketAccount(address(market));
|
|
69
|
+
return { ...options, oraclePolicy: state.oracle?.policy };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The hLP accounts an instruction must carry when hedged liquidity is live.
|
|
73
|
+
*
|
|
74
|
+
* Swap and openLeverage both require them, and their order is
|
|
75
|
+
* consensus-visible, so both take them from here rather than each assembling
|
|
76
|
+
* its own list.
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* The hLP settlement prefix an instruction must carry when the market has
|
|
80
|
+
* active hLP.
|
|
81
|
+
*
|
|
82
|
+
* Required by every leverage instruction that settles the market —
|
|
83
|
+
* open, close, increase and decrease — not only by open. Omitting it fails
|
|
84
|
+
* with `NotEnoughAccounts` at the point the program tries to read the
|
|
85
|
+
* prefix, which names neither leverage nor hLP and reads as a malformed
|
|
86
|
+
* call.
|
|
87
|
+
*/
|
|
88
|
+
async hlpRemainingAccounts(marketAddress, marketSnapshot) {
|
|
89
|
+
const market = address(marketAddress);
|
|
90
|
+
const state = (marketSnapshot ?? await this.fetchMarketAccount(market));
|
|
91
|
+
const hlpActive = BigInt(state.baseHlpVault.hlpSupply.toString()) !== 0n ||
|
|
92
|
+
BigInt(state.quoteHlpVault.hlpSupply.toString()) !== 0n ||
|
|
93
|
+
BigInt(state.baseHlpVault.residualExposure.toString()) !== 0n ||
|
|
94
|
+
BigInt(state.quoteHlpVault.residualExposure.toString()) !== 0n;
|
|
95
|
+
if (!hlpActive)
|
|
96
|
+
return [];
|
|
97
|
+
return [
|
|
98
|
+
{ pubkey: state.srlpMint, isSigner: false, isWritable: true },
|
|
99
|
+
{ pubkey: state.baseHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
100
|
+
{ pubkey: state.quoteHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
101
|
+
{ pubkey: state.baseSide.interestVault, isSigner: false, isWritable: true },
|
|
102
|
+
{ pubkey: state.quoteSide.interestVault, isSigner: false, isWritable: true },
|
|
103
|
+
// Fresh public-interest settlements rebalance both vaults. These keys
|
|
104
|
+
// usually duplicate fixed swap accounts; repayments need them explicitly.
|
|
105
|
+
{ pubkey: state.baseSide.assetMint, isSigner: false, isWritable: false },
|
|
106
|
+
{ pubkey: state.quoteSide.assetMint, isSigner: false, isWritable: false },
|
|
107
|
+
{ pubkey: state.baseSide.reserveVault, isSigner: false, isWritable: true },
|
|
108
|
+
{ pubkey: state.quoteSide.reserveVault, isSigner: false, isWritable: true },
|
|
109
|
+
];
|
|
36
110
|
}
|
|
37
111
|
async swapBuilder(args, options) {
|
|
38
112
|
const market = address(options.market);
|
|
39
|
-
const state = (await this.
|
|
113
|
+
const state = (await this.fetchMarketAccount(market));
|
|
40
114
|
const hlpActive = BigInt(state.baseHlpVault.hlpSupply.toString()) !== 0n ||
|
|
41
115
|
BigInt(state.quoteHlpVault.hlpSupply.toString()) !== 0n ||
|
|
42
116
|
BigInt(state.baseHlpVault.residualExposure.toString()) !== 0n ||
|
|
43
117
|
BigInt(state.quoteHlpVault.residualExposure.toString()) !== 0n;
|
|
44
118
|
const prefix = hlpActive
|
|
45
119
|
? [
|
|
46
|
-
{ pubkey: state.
|
|
47
|
-
{ pubkey: state.baseHlpVault.
|
|
48
|
-
{ pubkey: state.quoteHlpVault.
|
|
120
|
+
{ pubkey: state.srlpMint, isSigner: false, isWritable: true },
|
|
121
|
+
{ pubkey: state.baseHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
122
|
+
{ pubkey: state.quoteHlpVault.srlpVault, isSigner: false, isWritable: true },
|
|
49
123
|
{ pubkey: state.baseSide.interestVault, isSigner: false, isWritable: true },
|
|
50
124
|
{ pubkey: state.quoteSide.interestVault, isSigner: false, isWritable: true },
|
|
51
125
|
]
|
|
52
126
|
: [];
|
|
53
127
|
return this.builder("swap", args, {
|
|
54
|
-
|
|
128
|
+
oraclePolicy: options.oraclePolicy ?? state.oracle?.policy,
|
|
129
|
+
accounts: {
|
|
130
|
+
...options.accounts,
|
|
131
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
132
|
+
},
|
|
55
133
|
// Prefix order is consensus-visible. Hook accounts must remain a tail,
|
|
56
134
|
// including any deliberate duplicate pubkeys required by a hook meta list.
|
|
57
135
|
remainingAccounts: [...prefix, ...(options.remainingAccounts ?? [])],
|
|
@@ -66,6 +144,202 @@ export class DuskWrite {
|
|
|
66
144
|
async swapRpc(args, options) {
|
|
67
145
|
return (await this.swapBuilder(args, options)).rpc();
|
|
68
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Swap, with accounts resolved from the market and the two mints.
|
|
149
|
+
*
|
|
150
|
+
* `swapInstruction` takes a prepared account map, which makes callers
|
|
151
|
+
* responsible for reconstructing the program's account set. This resolves it
|
|
152
|
+
* instead: reserve vaults derive from the market and mint, trader accounts
|
|
153
|
+
* default to the associated token account for the correct token program, and
|
|
154
|
+
* the hLP remaining-account prefix is still assembled by `swapBuilder`,
|
|
155
|
+
* whose ordering is consensus-visible.
|
|
156
|
+
*/
|
|
157
|
+
async buildSwapInstruction(params) {
|
|
158
|
+
return (await this.buildSwapBuilder(params)).instruction();
|
|
159
|
+
}
|
|
160
|
+
async buildSwapTransaction(params) {
|
|
161
|
+
return (await this.buildSwapBuilder(params)).transaction();
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Borrow against a position, with accounts resolved from the market and the
|
|
165
|
+
* two mints.
|
|
166
|
+
*
|
|
167
|
+
* The referral accounts are optional on chain and are omitted here: a borrow
|
|
168
|
+
* with no referrer should not have to name accounts it does not use. Use
|
|
169
|
+
* `referredBorrow` when a referrer is present.
|
|
170
|
+
*/
|
|
171
|
+
async buildBorrowInstruction(params) {
|
|
172
|
+
const market = address(params.market);
|
|
173
|
+
const owner = address(params.owner);
|
|
174
|
+
const debtAssetMint = address(params.debtAssetMint);
|
|
175
|
+
const collateralAssetMint = address(params.collateralAssetMint);
|
|
176
|
+
if (debtAssetMint.equals(collateralAssetMint)) {
|
|
177
|
+
throw new Error("Borrow debt and collateral mints must differ");
|
|
178
|
+
}
|
|
179
|
+
const debtTokenProgram = await tokenProgramForMint(this.program.provider.connection, debtAssetMint);
|
|
180
|
+
return this.instruction("borrow", {
|
|
181
|
+
borrowAmount: integerBN(params.borrowAmount, "borrowAmount"),
|
|
182
|
+
minDebtAmountOut: integerBN(params.minDebtAmountOut, "minDebtAmountOut"),
|
|
183
|
+
minLiquidationCfBps: Number(params.minLiquidationCfBps ?? 0),
|
|
184
|
+
referrer: null,
|
|
185
|
+
}, {
|
|
186
|
+
accounts: {
|
|
187
|
+
market,
|
|
188
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
189
|
+
owner,
|
|
190
|
+
debtAssetMint,
|
|
191
|
+
collateralAssetMint,
|
|
192
|
+
reserveVault: address(params.reserveVault ??
|
|
193
|
+
deriveMarketReserveVaultAddress(market, debtAssetMint)[0]),
|
|
194
|
+
ownerDebtAccount: address(params.ownerDebtAccount ??
|
|
195
|
+
getAssociatedTokenAddressSync(debtAssetMint, owner, true, debtTokenProgram)),
|
|
196
|
+
borrowPosition: address(params.borrowPosition ??
|
|
197
|
+
deriveBorrowPositionAddress(market, owner, address(params.positionId))[0]),
|
|
198
|
+
referralPartner: null,
|
|
199
|
+
referralAccrual: null,
|
|
200
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
201
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
202
|
+
},
|
|
203
|
+
remainingAccounts: params.remainingAccounts,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
async buildBorrowTransaction(params) {
|
|
207
|
+
const options = await this.withOraclePolicy("borrow", { accounts: { market: address(params.market) } });
|
|
208
|
+
return new Transaction().add(...oracleComputeBudgetInstructions(options.oraclePolicy), await this.buildBorrowInstruction(params));
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Open a leverage position, with accounts resolved from the market, the
|
|
212
|
+
* position id and the two mints.
|
|
213
|
+
*
|
|
214
|
+
* Referral accounts are optional on chain and omitted here; use
|
|
215
|
+
* `referredOpenLeverage` when a referrer is present.
|
|
216
|
+
*/
|
|
217
|
+
async buildOpenLeverageInstruction(params) {
|
|
218
|
+
const market = address(params.market);
|
|
219
|
+
const owner = address(params.owner);
|
|
220
|
+
const debtMint = address(params.debtMint);
|
|
221
|
+
const collateralMint = address(params.collateralMint);
|
|
222
|
+
if (debtMint.equals(collateralMint)) {
|
|
223
|
+
throw new Error("Leverage debt and collateral mints must differ");
|
|
224
|
+
}
|
|
225
|
+
if ("fundingAsset" in params && params.fundingAsset !== undefined && params.fundingAsset !== "debt")
|
|
226
|
+
throw new Error("Leverage funding must use the debt asset; swap other tokens before opening");
|
|
227
|
+
const positionId = address(params.positionId);
|
|
228
|
+
const debtTokenProgram = await tokenProgramForMint(this.program.provider.connection, debtMint);
|
|
229
|
+
const openArgs = {
|
|
230
|
+
positionId,
|
|
231
|
+
debtAsset: params.debtAsset === "quote" ? 1 : 0,
|
|
232
|
+
marginAmount: integerBN(params.marginAmount, "marginAmount"),
|
|
233
|
+
multiplierBps: integerBN(params.multiplierBps, "multiplierBps"),
|
|
234
|
+
minCollateralOut: integerBN(params.minCollateralOut, "minCollateralOut"),
|
|
235
|
+
referrer: null,
|
|
236
|
+
positionOwner: null,
|
|
237
|
+
limitPriceNad: integerBN(params.limitPriceNad ?? 0, "limitPriceNad"),
|
|
238
|
+
};
|
|
239
|
+
return this.instruction("openLeverage", openArgs, {
|
|
240
|
+
accounts: {
|
|
241
|
+
market,
|
|
242
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
243
|
+
owner,
|
|
244
|
+
payer: address(params.payer ?? owner),
|
|
245
|
+
leveragePosition: address(params.leveragePosition ??
|
|
246
|
+
deriveLeveragePositionAddress(market, owner, positionId)[0]),
|
|
247
|
+
debtMint,
|
|
248
|
+
collateralMint,
|
|
249
|
+
debtReserveVault: address(params.debtReserveVault ??
|
|
250
|
+
deriveMarketReserveVaultAddress(market, debtMint)[0]),
|
|
251
|
+
collateralReserveVault: address(params.collateralReserveVault ??
|
|
252
|
+
deriveMarketReserveVaultAddress(market, collateralMint)[0]),
|
|
253
|
+
leverageCollateralVault: address(params.leverageCollateralVault ??
|
|
254
|
+
deriveLeverageCollateralVaultAddress(market, collateralMint)[0]),
|
|
255
|
+
ownerDebtAccount: address(params.ownerDebtAccount ??
|
|
256
|
+
getAssociatedTokenAddressSync(debtMint, owner, true, debtTokenProgram)),
|
|
257
|
+
referralPartner: null,
|
|
258
|
+
referralAccrual: null,
|
|
259
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
260
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
261
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
262
|
+
systemProgram: SystemProgram.programId,
|
|
263
|
+
},
|
|
264
|
+
remainingAccounts: [
|
|
265
|
+
...(await this.hlpRemainingAccounts(market)),
|
|
266
|
+
...(params.remainingAccounts ?? []),
|
|
267
|
+
],
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
async buildOpenLeverageTransaction(params) {
|
|
271
|
+
const options = await this.withOraclePolicy("openLeverage", { accounts: { market: address(params.market) } });
|
|
272
|
+
return new Transaction().add(...oracleComputeBudgetInstructions(options.oraclePolicy), await this.buildOpenLeverageInstruction(params));
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Delegate a leverage position to a program that may act on it, which is how
|
|
276
|
+
* conditional orders (take-profit, stop-loss) are authorised.
|
|
277
|
+
*/
|
|
278
|
+
async buildCreateLeverageDelegationInstruction(params) {
|
|
279
|
+
const market = address(params.market);
|
|
280
|
+
const leveragePosition = address(params.leveragePosition ??
|
|
281
|
+
deriveLeveragePositionAddress(market, address(params.owner), address(params.positionId), address(params.namespaceAuthority ?? params.owner))[0]);
|
|
282
|
+
return this.instruction("createLeverageDelegation", {
|
|
283
|
+
debtAsset: params.debtAsset === "quote" ? 1 : 0,
|
|
284
|
+
delegatedProgram: address(params.delegatedProgram),
|
|
285
|
+
approvedActions: Number(params.approvedActions),
|
|
286
|
+
}, {
|
|
287
|
+
accounts: {
|
|
288
|
+
market,
|
|
289
|
+
leveragePosition,
|
|
290
|
+
leverageDelegation: address(params.leverageDelegation ??
|
|
291
|
+
deriveLeverageDelegationAddress(leveragePosition)[0]),
|
|
292
|
+
owner: address(params.owner),
|
|
293
|
+
systemProgram: SystemProgram.programId,
|
|
294
|
+
},
|
|
295
|
+
remainingAccounts: params.remainingAccounts,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
async buildCreateLeverageDelegationTransaction(params) {
|
|
299
|
+
return new Transaction().add(await this.buildCreateLeverageDelegationInstruction(params));
|
|
300
|
+
}
|
|
301
|
+
async buildSwapBuilder(params) {
|
|
302
|
+
const market = address(params.market);
|
|
303
|
+
const trader = address(params.trader);
|
|
304
|
+
const assetInMint = address(params.assetInMint);
|
|
305
|
+
const assetOutMint = address(params.assetOutMint);
|
|
306
|
+
if (assetInMint.equals(assetOutMint)) {
|
|
307
|
+
throw new Error("Swap input and output mints must differ");
|
|
308
|
+
}
|
|
309
|
+
const connection = this.program.provider.connection;
|
|
310
|
+
const [assetInProgram, assetOutProgram] = await Promise.all([
|
|
311
|
+
tokenProgramForMint(connection, assetInMint),
|
|
312
|
+
tokenProgramForMint(connection, assetOutMint),
|
|
313
|
+
]);
|
|
314
|
+
return this.swapBuilder(
|
|
315
|
+
// A single positional SwapArgs struct: the generic builder spreads a
|
|
316
|
+
// non-array into one argument, so wrapping it in an outer object would
|
|
317
|
+
// serialize a struct of zeros and the program would reject the amount.
|
|
318
|
+
{
|
|
319
|
+
exactAssetIn: integerBN(params.exactAssetIn, "exactAssetIn"),
|
|
320
|
+
minAssetOut: integerBN(params.minAssetOut, "minAssetOut"),
|
|
321
|
+
}, {
|
|
322
|
+
market,
|
|
323
|
+
accounts: {
|
|
324
|
+
market,
|
|
325
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
326
|
+
trader,
|
|
327
|
+
assetInMint,
|
|
328
|
+
assetOutMint,
|
|
329
|
+
reserveInVault: address(params.reserveInVault ??
|
|
330
|
+
deriveMarketReserveVaultAddress(market, assetInMint)[0]),
|
|
331
|
+
reserveOutVault: address(params.reserveOutVault ??
|
|
332
|
+
deriveMarketReserveVaultAddress(market, assetOutMint)[0]),
|
|
333
|
+
traderAssetInAccount: address(params.traderAssetInAccount ??
|
|
334
|
+
getAssociatedTokenAddressSync(assetInMint, trader, true, assetInProgram)),
|
|
335
|
+
traderAssetOutAccount: address(params.traderAssetOutAccount ??
|
|
336
|
+
getAssociatedTokenAddressSync(assetOutMint, trader, true, assetOutProgram)),
|
|
337
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
338
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
339
|
+
},
|
|
340
|
+
remainingAccounts: params.remainingAccounts,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
69
343
|
async initializeYieldAccountsInstruction(params) {
|
|
70
344
|
const payer = address(params.payer);
|
|
71
345
|
const owner = address(params.owner);
|
|
@@ -73,7 +347,7 @@ export class DuskWrite {
|
|
|
73
347
|
const lpMint = address(params.lpMint);
|
|
74
348
|
const baseMint = address(params.baseMint);
|
|
75
349
|
const quoteMint = address(params.quoteMint);
|
|
76
|
-
const tokenKind = params.tokenKind === "
|
|
350
|
+
const tokenKind = params.tokenKind === "srlp" || params.tokenKind === 0 ? { srlp: {} } : params.tokenKind === "junior" || params.tokenKind === 2 ? { junior: {} } : { hlp: {} };
|
|
77
351
|
return this.instruction("initializeYieldAccounts", { owner, tokenKind }, {
|
|
78
352
|
accounts: {
|
|
79
353
|
payer,
|
|
@@ -81,10 +355,13 @@ export class DuskWrite {
|
|
|
81
355
|
lpMint,
|
|
82
356
|
baseMint,
|
|
83
357
|
quoteMint,
|
|
84
|
-
baseYieldAccount: deriveYieldAccountAddress(market, owner, lpMint, baseMint, params.tokenKind)[0],
|
|
85
|
-
quoteYieldAccount: deriveYieldAccountAddress(market, owner, lpMint, quoteMint, params.tokenKind)[0],
|
|
358
|
+
baseYieldAccount: deriveYieldAccountAddress(market, owner, lpMint, baseMint, params.tokenKind, this.program.programId)[0],
|
|
359
|
+
quoteYieldAccount: deriveYieldAccountAddress(market, owner, lpMint, quoteMint, params.tokenKind, this.program.programId)[0],
|
|
86
360
|
systemProgram: SystemProgram.programId,
|
|
87
361
|
},
|
|
362
|
+
remainingAccounts: params.retired
|
|
363
|
+
? [{ pubkey: (params.tokenKind === "junior" || params.tokenKind === 2 ? deriveJuniorCohortAddress : deriveHlpCohortAddress)(market, lpMint, this.program.programId)[0], isSigner: false, isWritable: false }]
|
|
364
|
+
: [],
|
|
88
365
|
});
|
|
89
366
|
}
|
|
90
367
|
async initializeYieldAccountsTransaction(params) {
|
|
@@ -92,19 +369,105 @@ export class DuskWrite {
|
|
|
92
369
|
}
|
|
93
370
|
async initializeLpTransferHookInstruction(params) {
|
|
94
371
|
const lpMint = address(params.lpMint);
|
|
372
|
+
const market = address(params.market);
|
|
373
|
+
let tokenKind = params.tokenKind;
|
|
374
|
+
if (tokenKind === undefined) {
|
|
375
|
+
const state = await this.fetchMarketAccount(market);
|
|
376
|
+
if (lpMint.equals(state.srlpMint))
|
|
377
|
+
tokenKind = "srlp";
|
|
378
|
+
else if (lpMint.equals(state.juniorMint))
|
|
379
|
+
tokenKind = "junior";
|
|
380
|
+
else if (lpMint.equals(state.baseSide.hlpMint) || lpMint.equals(state.quoteSide.hlpMint))
|
|
381
|
+
tokenKind = "hlp";
|
|
382
|
+
else
|
|
383
|
+
throw new Error("LP mint does not belong to the market");
|
|
384
|
+
}
|
|
385
|
+
const isHlp = tokenKind === "hlp" || tokenKind === 1;
|
|
95
386
|
return this.instruction("initializeLpTransferHook", undefined, {
|
|
96
387
|
accounts: {
|
|
97
388
|
payer: address(params.payer),
|
|
98
|
-
market
|
|
389
|
+
market,
|
|
99
390
|
lpMint,
|
|
100
|
-
validationAccount: deriveYieldTransferHookValidationAddress(lpMint)[0],
|
|
391
|
+
validationAccount: deriveYieldTransferHookValidationAddress(lpMint, this.program.programId)[0],
|
|
101
392
|
systemProgram: SystemProgram.programId,
|
|
102
393
|
},
|
|
394
|
+
remainingAccounts: isHlp
|
|
395
|
+
? [{ pubkey: deriveHlpCohortAddress(market, lpMint, this.program.programId)[0], isSigner: false, isWritable: true }]
|
|
396
|
+
: [],
|
|
103
397
|
});
|
|
104
398
|
}
|
|
105
399
|
async initializeLpTransferHookTransaction(params) {
|
|
106
400
|
return new Transaction().add(await this.initializeLpTransferHookInstruction(params));
|
|
107
401
|
}
|
|
402
|
+
/**
|
|
403
|
+
* Bring a market into existence.
|
|
404
|
+
*
|
|
405
|
+
* The three LP mints must already exist and carry the Dusk transfer hook —
|
|
406
|
+
* see `createHookedLpMintInstructions`. Every PDA is derived here rather
|
|
407
|
+
* than left to Anchor, because the market address depends on `paramsHash`,
|
|
408
|
+
* which is an argument rather than an account, and the vaults hang off the
|
|
409
|
+
* market in turn.
|
|
410
|
+
*
|
|
411
|
+
* `teamTreasury` and its wrapped-SOL account come from the futarchy
|
|
412
|
+
* authority, which must already be initialized on the deployment.
|
|
413
|
+
*/
|
|
414
|
+
async initializeMarketInstruction(params) {
|
|
415
|
+
const operatorShare = params.operatorInterestShareBps ?? 0;
|
|
416
|
+
if (!Number.isInteger(operatorShare) || operatorShare < 0 || operatorShare > 5_000)
|
|
417
|
+
throw new Error("operatorInterestShareBps must be 0–5000");
|
|
418
|
+
const baseMint = address(params.baseMint);
|
|
419
|
+
const quoteMint = address(params.quoteMint);
|
|
420
|
+
const paramsHash = Uint8Array.from(params.paramsHash);
|
|
421
|
+
if (paramsHash.length !== 32) {
|
|
422
|
+
throw new Error(`paramsHash must be 32 bytes, received ${paramsHash.length}`);
|
|
423
|
+
}
|
|
424
|
+
const [market] = deriveMarketAddress(baseMint, quoteMint, paramsHash, this.program.programId);
|
|
425
|
+
const oracle = params.oracle == null ? null : oraclePolicy(params.oracle);
|
|
426
|
+
const bootstrapPriceNad = integerBN(params.bootstrapPriceNad ?? 0, "bootstrapPriceNad");
|
|
427
|
+
if (oracle && oracle.lambdaBps > 0 && !bootstrapPriceNad.isZero())
|
|
428
|
+
throw new Error("Oracle-guided markets derive the initial price from the feed; omit bootstrapPriceNad");
|
|
429
|
+
return this.instruction("initializeMarket", {
|
|
430
|
+
operatorInterestShareBps: operatorShare,
|
|
431
|
+
config: anchorMarketConfig(params.config),
|
|
432
|
+
oracle,
|
|
433
|
+
paramsHash: [...paramsHash],
|
|
434
|
+
bootstrapPriceNad,
|
|
435
|
+
launchFeeProgressOffset: params.launchFeeProgressOffset ?? 0,
|
|
436
|
+
srlpMetadata: params.lpMetadata.srlp,
|
|
437
|
+
baseHlpMetadata: params.lpMetadata.baseHlp,
|
|
438
|
+
quoteHlpMetadata: params.lpMetadata.quoteHlp,
|
|
439
|
+
}, {
|
|
440
|
+
accounts: {
|
|
441
|
+
payer: address(params.payer),
|
|
442
|
+
baseMint,
|
|
443
|
+
quoteMint,
|
|
444
|
+
srlpMint: address(params.srlpMint),
|
|
445
|
+
baseHlpMint: address(params.baseHlpMint),
|
|
446
|
+
quoteHlpMint: address(params.quoteHlpMint),
|
|
447
|
+
srlpTokenMetadata: deriveTokenMetadataAddress(address(params.srlpMint))[0],
|
|
448
|
+
baseHlpTokenMetadata: deriveTokenMetadataAddress(address(params.baseHlpMint))[0],
|
|
449
|
+
quoteHlpTokenMetadata: deriveTokenMetadataAddress(address(params.quoteHlpMint))[0],
|
|
450
|
+
market,
|
|
451
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
452
|
+
baseReserveVault: deriveMarketReserveVaultAddress(market, baseMint)[0],
|
|
453
|
+
quoteReserveVault: deriveMarketReserveVaultAddress(market, quoteMint)[0],
|
|
454
|
+
baseCollateralVault: deriveMarketCollateralVaultAddress(market, baseMint)[0],
|
|
455
|
+
quoteCollateralVault: deriveMarketCollateralVaultAddress(market, quoteMint)[0],
|
|
456
|
+
baseInterestVault: deriveMarketInterestVaultAddress(market, baseMint)[0],
|
|
457
|
+
quoteInterestVault: deriveMarketInterestVaultAddress(market, quoteMint)[0],
|
|
458
|
+
teamTreasury: address(params.teamTreasury),
|
|
459
|
+
teamTreasuryWsolAccount: address(params.teamTreasuryWsolAccount),
|
|
460
|
+
systemProgram: SystemProgram.programId,
|
|
461
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
462
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
463
|
+
sysvarInstructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
464
|
+
tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
|
|
465
|
+
},
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
async initializeMarketTransaction(params) {
|
|
469
|
+
return new Transaction().add(await this.initializeMarketInstruction(params));
|
|
470
|
+
}
|
|
108
471
|
async hlpLiquidityAction(name, args, options) {
|
|
109
472
|
const payer = address(options.payer);
|
|
110
473
|
const owner = address(options.owner);
|
|
@@ -112,8 +475,8 @@ export class DuskWrite {
|
|
|
112
475
|
const lpMint = address(options.targetHlpMint);
|
|
113
476
|
const baseMint = address(options.baseMint);
|
|
114
477
|
const quoteMint = address(options.quoteMint);
|
|
115
|
-
const baseYieldAccount = deriveYieldAccountAddress(market, owner, lpMint, baseMint, "hlp")[0];
|
|
116
|
-
const quoteYieldAccount = deriveYieldAccountAddress(market, owner, lpMint, quoteMint, "hlp")[0];
|
|
478
|
+
const baseYieldAccount = deriveYieldAccountAddress(market, owner, lpMint, baseMint, "hlp", this.program.programId)[0];
|
|
479
|
+
const quoteYieldAccount = deriveYieldAccountAddress(market, owner, lpMint, quoteMint, "hlp", this.program.programId)[0];
|
|
117
480
|
const [baseYieldInfo, quoteYieldInfo] = await Promise.all([
|
|
118
481
|
this.program.provider.connection.getAccountInfo(baseYieldAccount),
|
|
119
482
|
this.program.provider.connection.getAccountInfo(quoteYieldAccount),
|
|
@@ -153,6 +516,7 @@ export class DuskWrite {
|
|
|
153
516
|
quoteYieldAccount,
|
|
154
517
|
},
|
|
155
518
|
remainingAccounts: options.remainingAccounts,
|
|
519
|
+
oraclePolicy: options.oraclePolicy,
|
|
156
520
|
});
|
|
157
521
|
return {
|
|
158
522
|
baseYieldAccount,
|
|
@@ -168,6 +532,51 @@ export class DuskWrite {
|
|
|
168
532
|
withdrawSingleSided(args, options) {
|
|
169
533
|
return this.hlpLiquidityAction("withdrawSingleSided", args, options);
|
|
170
534
|
}
|
|
535
|
+
/** Begin a fresh hLP receipt cohort after the current vault has terminally closed. */
|
|
536
|
+
rotateHlpCohortInstruction(params) {
|
|
537
|
+
const market = address(params.market);
|
|
538
|
+
const oldHlpMint = address(params.oldHlpMint);
|
|
539
|
+
const newHlpMint = address(params.newHlpMint);
|
|
540
|
+
return this.instruction("rotateHlpCohort", {
|
|
541
|
+
targetAsset: params.targetAsset === "base" ? 0 : 1,
|
|
542
|
+
metadata: params.metadata,
|
|
543
|
+
}, {
|
|
544
|
+
accounts: {
|
|
545
|
+
payer: address(params.payer),
|
|
546
|
+
market,
|
|
547
|
+
oldHlpMint,
|
|
548
|
+
newHlpMint,
|
|
549
|
+
oldCohort: deriveHlpCohortAddress(market, oldHlpMint, this.program.programId)[0],
|
|
550
|
+
newTokenMetadata: deriveTokenMetadataAddress(newHlpMint)[0],
|
|
551
|
+
sysvarInstructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
552
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
553
|
+
tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
|
|
554
|
+
systemProgram: SystemProgram.programId,
|
|
555
|
+
},
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
/** Burn old hLP receipts after terminal closure while preserving earned yield. */
|
|
559
|
+
burnRetiredHlpReceiptInstruction(params) {
|
|
560
|
+
const market = address(params.market);
|
|
561
|
+
const owner = address(params.owner);
|
|
562
|
+
const retiredMint = address(params.retiredMint);
|
|
563
|
+
return this.instruction("burnRetiredHlpReceipt", {
|
|
564
|
+
amount: integerBN(params.amount),
|
|
565
|
+
}, {
|
|
566
|
+
accounts: {
|
|
567
|
+
market,
|
|
568
|
+
owner,
|
|
569
|
+
retiredMint,
|
|
570
|
+
cohort: deriveHlpCohortAddress(market, retiredMint, this.program.programId)[0],
|
|
571
|
+
ownerHlpAccount: params.ownerHlpAccount
|
|
572
|
+
? address(params.ownerHlpAccount)
|
|
573
|
+
: getAssociatedTokenAddressSync(retiredMint, owner, true, TOKEN_2022_PROGRAM_ID),
|
|
574
|
+
baseYieldAccount: deriveYieldAccountAddress(market, owner, retiredMint, address(params.baseMint), "hlp", this.program.programId)[0],
|
|
575
|
+
quoteYieldAccount: deriveYieldAccountAddress(market, owner, retiredMint, address(params.quoteMint), "hlp", this.program.programId)[0],
|
|
576
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
577
|
+
},
|
|
578
|
+
});
|
|
579
|
+
}
|
|
171
580
|
async configureReferralPartnerInstruction(params) {
|
|
172
581
|
assertReferralInterestShareBps(params.interestShareBps);
|
|
173
582
|
const referrer = address(params.referrer);
|
|
@@ -226,6 +635,9 @@ export class DuskWrite {
|
|
|
226
635
|
...options.accounts,
|
|
227
636
|
referralPartner: referral.referralPartner,
|
|
228
637
|
referralAccrual: referral.referralAccrual,
|
|
638
|
+
...(name === "openLeverage"
|
|
639
|
+
? { instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY }
|
|
640
|
+
: {}),
|
|
229
641
|
},
|
|
230
642
|
remainingAccounts: mergeAccountMetas(options.remainingAccounts ?? [], hookAccounts),
|
|
231
643
|
});
|
|
@@ -267,8 +679,9 @@ export class DuskWrite {
|
|
|
267
679
|
const interestVault = address(params.interestVault ?? deriveMarketInterestVaultAddress(market, mintKey)[0]);
|
|
268
680
|
const recipientTokenAccount = address(params.recipientTokenAccount);
|
|
269
681
|
const tokenProgram = await tokenProgramForMint(this.program.provider.connection, mintKey);
|
|
682
|
+
const referralAccrual = this.program.account.referralAccrual;
|
|
270
683
|
const [accrual, mint] = await Promise.all([
|
|
271
|
-
|
|
684
|
+
referralAccrual.fetch(referral.referralAccrual),
|
|
272
685
|
getMint(this.program.provider.connection, mintKey, undefined, tokenProgram),
|
|
273
686
|
]);
|
|
274
687
|
const hookAccounts = await resolveTransferHookAccountMetas(this.program.provider.connection, [
|
|
@@ -300,6 +713,494 @@ export class DuskWrite {
|
|
|
300
713
|
async claimReferralInterestTransaction(params) {
|
|
301
714
|
return new Transaction().add(await this.claimReferralInterestInstruction(params));
|
|
302
715
|
}
|
|
716
|
+
/**
|
|
717
|
+
* Deposit collateral into a borrow position, creating it when `positionId` is
|
|
718
|
+
* new. Callers pass amounts in raw base units.
|
|
719
|
+
*/
|
|
720
|
+
async depositCollateralInstruction(params) {
|
|
721
|
+
const market = address(params.market);
|
|
722
|
+
const owner = address(params.owner);
|
|
723
|
+
const assetMint = address(params.assetMint);
|
|
724
|
+
const positionId = address(params.positionId);
|
|
725
|
+
return this.instruction("depositCollateral", { positionId, depositAmount: integerBN(params.depositAmount, "depositAmount") }, {
|
|
726
|
+
accounts: {
|
|
727
|
+
market,
|
|
728
|
+
owner,
|
|
729
|
+
assetMint,
|
|
730
|
+
collateralVault: address(params.collateralVault ??
|
|
731
|
+
deriveMarketCollateralVaultAddress(market, assetMint)[0]),
|
|
732
|
+
ownerAssetAccount: address(params.ownerAssetAccount),
|
|
733
|
+
borrowPosition: address(params.borrowPosition ??
|
|
734
|
+
deriveBorrowPositionAddress(market, owner, positionId)[0]),
|
|
735
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
736
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
737
|
+
},
|
|
738
|
+
remainingAccounts: params.remainingAccounts,
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
/** Donate to an existing borrow position. `owner` is the token payer. */
|
|
742
|
+
async donateCollateralInstruction(params) {
|
|
743
|
+
const market = address(params.market);
|
|
744
|
+
const assetMint = address(params.assetMint);
|
|
745
|
+
return this.instruction("donateCollateral", { depositAmount: integerBN(params.depositAmount, "depositAmount") }, {
|
|
746
|
+
accounts: {
|
|
747
|
+
market, owner: address(params.owner), assetMint,
|
|
748
|
+
collateralVault: address(params.collateralVault ?? deriveMarketCollateralVaultAddress(market, assetMint)[0]),
|
|
749
|
+
ownerAssetAccount: address(params.ownerAssetAccount),
|
|
750
|
+
borrowPosition: address(params.borrowPosition ?? deriveBorrowPositionAddress(market, address(params.positionOwner ?? params.owner), address(params.positionId))[0]),
|
|
751
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
752
|
+
}, remainingAccounts: params.remainingAccounts,
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
/** Sweep both collateral balances and close a debt-free borrow position. */
|
|
756
|
+
async withdrawAllCollateralInstruction(params) {
|
|
757
|
+
const market = address(params.market);
|
|
758
|
+
const baseMint = address(params.baseMint);
|
|
759
|
+
const quoteMint = address(params.quoteMint);
|
|
760
|
+
return this.instruction("withdrawAllCollateral", {
|
|
761
|
+
minBaseOut: integerBN(params.minBaseOut, "minBaseOut"),
|
|
762
|
+
minQuoteOut: integerBN(params.minQuoteOut, "minQuoteOut"),
|
|
763
|
+
}, { accounts: {
|
|
764
|
+
market, owner: address(params.owner), baseMint, quoteMint,
|
|
765
|
+
borrowPosition: address(params.borrowPosition ?? deriveBorrowPositionAddress(market, address(params.owner), address(params.positionId))[0]),
|
|
766
|
+
baseCollateralVault: deriveMarketCollateralVaultAddress(market, baseMint)[0],
|
|
767
|
+
quoteCollateralVault: deriveMarketCollateralVaultAddress(market, quoteMint)[0],
|
|
768
|
+
ownerBaseAccount: address(params.ownerBaseAccount), ownerQuoteAccount: address(params.ownerQuoteAccount),
|
|
769
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
770
|
+
}, remainingAccounts: params.remainingAccounts });
|
|
771
|
+
}
|
|
772
|
+
/** Return all collateral after a complete leverage repayment and close the position. */
|
|
773
|
+
async withdrawRepaidLeverageInstruction(params) {
|
|
774
|
+
const market = address(params.market);
|
|
775
|
+
const collateralMint = address(params.collateralMint);
|
|
776
|
+
return this.instruction("withdrawRepaidLeverage", { minCollateralOut: integerBN(params.minCollateralOut, "minCollateralOut") }, {
|
|
777
|
+
accounts: {
|
|
778
|
+
market, owner: address(params.owner), collateralMint,
|
|
779
|
+
leveragePosition: address(params.leveragePosition ?? deriveLeveragePositionAddress(market, address(params.owner), address(params.positionId), address(params.namespaceAuthority ?? params.owner))[0]),
|
|
780
|
+
collateralVault: deriveLeverageCollateralVaultAddress(market, collateralMint)[0],
|
|
781
|
+
ownerCollateralAccount: address(params.ownerCollateralAccount),
|
|
782
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
783
|
+
}, remainingAccounts: params.remainingAccounts,
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
async depositCollateralTransaction(params) {
|
|
787
|
+
return new Transaction().add(await this.depositCollateralInstruction(params));
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Withdraw collateral. `minAssetAmountOut` and `minLiquidationCfBps` are the
|
|
791
|
+
* caller's slippage and health floors; the program rejects the withdrawal
|
|
792
|
+
* rather than silently returning less.
|
|
793
|
+
*/
|
|
794
|
+
async withdrawCollateralInstruction(params) {
|
|
795
|
+
const market = address(params.market);
|
|
796
|
+
const owner = address(params.owner);
|
|
797
|
+
const assetMint = address(params.assetMint);
|
|
798
|
+
const positionId = address(params.positionId);
|
|
799
|
+
return this.instruction("withdrawCollateral", {
|
|
800
|
+
withdrawAmount: integerBN(params.withdrawAmount, "withdrawAmount"),
|
|
801
|
+
minAssetAmountOut: integerBN(params.minAssetAmountOut, "minAssetAmountOut"),
|
|
802
|
+
minLiquidationCfBps: params.minLiquidationCfBps,
|
|
803
|
+
}, {
|
|
804
|
+
accounts: {
|
|
805
|
+
market,
|
|
806
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
807
|
+
owner,
|
|
808
|
+
assetMint,
|
|
809
|
+
collateralVault: address(params.collateralVault ??
|
|
810
|
+
deriveMarketCollateralVaultAddress(market, assetMint)[0]),
|
|
811
|
+
ownerAssetAccount: address(params.ownerAssetAccount),
|
|
812
|
+
borrowPosition: address(params.borrowPosition ??
|
|
813
|
+
deriveBorrowPositionAddress(market, owner, positionId)[0]),
|
|
814
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
815
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
816
|
+
},
|
|
817
|
+
remainingAccounts: params.remainingAccounts,
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
async withdrawCollateralTransaction(params) {
|
|
821
|
+
return new Transaction().add(await this.withdrawCollateralInstruction(params));
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Repay anyone’s borrowed debt using `owner`’s tokens, without collateral rights.
|
|
825
|
+
* Pass the wallet balance as `repayAmount` to clear a
|
|
826
|
+
* position; the program repays at most the outstanding debt.
|
|
827
|
+
*/
|
|
828
|
+
async repayInstruction(params) {
|
|
829
|
+
const market = address(params.market);
|
|
830
|
+
const owner = address(params.owner);
|
|
831
|
+
const debtAssetMint = address(params.debtAssetMint);
|
|
832
|
+
const positionId = address(params.positionId);
|
|
833
|
+
const referralPartner = params.referralPartner
|
|
834
|
+
? address(params.referralPartner)
|
|
835
|
+
: null;
|
|
836
|
+
return this.instruction("repay", { repayAmount: integerBN(params.repayAmount, "repayAmount") }, {
|
|
837
|
+
accounts: {
|
|
838
|
+
market,
|
|
839
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
840
|
+
owner,
|
|
841
|
+
debtAssetMint,
|
|
842
|
+
collateralAssetMint: address(params.collateralAssetMint),
|
|
843
|
+
reserveVault: address(params.reserveVault ??
|
|
844
|
+
deriveMarketReserveVaultAddress(market, debtAssetMint)[0]),
|
|
845
|
+
interestVault: address(params.interestVault ??
|
|
846
|
+
deriveMarketInterestVaultAddress(market, debtAssetMint)[0]),
|
|
847
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
848
|
+
borrowPosition: address(params.borrowPosition ??
|
|
849
|
+
deriveBorrowPositionAddress(market, address(params.positionOwner ?? params.owner), positionId)[0]),
|
|
850
|
+
referralPartner,
|
|
851
|
+
referralAccrual: referralPartner
|
|
852
|
+
? deriveReferralAccrualAddress(referralPartner, market, debtAssetMint)[0]
|
|
853
|
+
: null,
|
|
854
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
855
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
856
|
+
},
|
|
857
|
+
remainingAccounts: [...(await this.hlpRemainingAccounts(market)), ...(params.remainingAccounts ?? [])],
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
async repayTransaction(params) {
|
|
861
|
+
return new Transaction().add(await this.repayInstruction(params));
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Add balanced srLP liquidity. `minSrlpAmount` is the caller's slippage floor;
|
|
865
|
+
* the program rejects the deposit rather than minting fewer shares.
|
|
866
|
+
*/
|
|
867
|
+
async addLiquidityInstruction(params) {
|
|
868
|
+
const common = await this.resolveSrlpAccounts(params);
|
|
869
|
+
return this.instruction("addLiquidity", {
|
|
870
|
+
baseDepositAmount: integerBN(params.baseDepositAmount, "baseDepositAmount"),
|
|
871
|
+
quoteDepositAmount: integerBN(params.quoteDepositAmount, "quoteDepositAmount"),
|
|
872
|
+
minSrlpAmount: integerBN(params.minSrlpAmount, "minSrlpAmount"),
|
|
873
|
+
}, {
|
|
874
|
+
accounts: {
|
|
875
|
+
...common,
|
|
876
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
877
|
+
},
|
|
878
|
+
remainingAccounts: params.remainingAccounts,
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
async addLiquidityTransaction(params) {
|
|
882
|
+
return new Transaction().add(await this.addLiquidityInstruction(params));
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* Burn srLP for the underlying pair. Both minimums are enforced by the
|
|
886
|
+
* program, so a partially-filled withdrawal fails instead of silently
|
|
887
|
+
* returning less of one side.
|
|
888
|
+
*/
|
|
889
|
+
async removeLiquidityInstruction(params) {
|
|
890
|
+
const common = await this.resolveSrlpAccounts(params);
|
|
891
|
+
return this.instruction("removeLiquidity", {
|
|
892
|
+
srlpAmount: integerBN(params.srlpAmount, "srlpAmount"),
|
|
893
|
+
minBaseAmountOut: integerBN(params.minBaseAmountOut, "minBaseAmountOut"),
|
|
894
|
+
minQuoteAmountOut: integerBN(params.minQuoteAmountOut, "minQuoteAmountOut"),
|
|
895
|
+
}, { accounts: common, remainingAccounts: params.remainingAccounts });
|
|
896
|
+
}
|
|
897
|
+
async removeLiquidityTransaction(params) {
|
|
898
|
+
return new Transaction().add(await this.removeLiquidityInstruction(params));
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Accounts shared by both balanced srLP instructions. Reserve vaults and srLP
|
|
902
|
+
* yield accounts derive from the market; token programs resolve per mint so
|
|
903
|
+
* a Token-2022 side needs no caller branching.
|
|
904
|
+
*/
|
|
905
|
+
/** Borrow more against an open position and receive collateral. */
|
|
906
|
+
async increaseLeverageInstruction(params) {
|
|
907
|
+
return this.fundLeverageInstruction(params, 0n);
|
|
908
|
+
}
|
|
909
|
+
/** Add debt-token equity and optional borrowing to the same position. */
|
|
910
|
+
async addToLeverageInstruction(params) {
|
|
911
|
+
if (BigInt(params.marginAmount.toString()) <= 0n)
|
|
912
|
+
throw new RangeError("marginAmount must be positive");
|
|
913
|
+
return this.fundLeverageInstruction(params, params.marginAmount, params.ownerDebtAccount);
|
|
914
|
+
}
|
|
915
|
+
async addToLeverageTransaction(params) {
|
|
916
|
+
return new Transaction().add(await this.addToLeverageInstruction(params));
|
|
917
|
+
}
|
|
918
|
+
async fundLeverageInstruction(params, marginAmount, ownerDebtAccount) {
|
|
919
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
920
|
+
return this.instruction("increaseLeverage", {
|
|
921
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
922
|
+
debtAmount: integerBN(params.debtAmount, "debtAmount"),
|
|
923
|
+
marginAmount: integerBN(marginAmount, "marginAmount"),
|
|
924
|
+
minCollateralOut: integerBN(params.minCollateralOut, "minCollateralOut"),
|
|
925
|
+
}, {
|
|
926
|
+
accounts: {
|
|
927
|
+
market: core.market,
|
|
928
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
929
|
+
positionOwner: core.positionOwner,
|
|
930
|
+
leveragePosition: core.leveragePosition,
|
|
931
|
+
debtMint: core.debtMint,
|
|
932
|
+
collateralMint: core.collateralMint,
|
|
933
|
+
debtReserveVault: core.debtReserveVault,
|
|
934
|
+
collateralReserveVault: core.collateralReserveVault,
|
|
935
|
+
leverageCollateralVault: core.leverageCollateralVault,
|
|
936
|
+
owner: core.owner,
|
|
937
|
+
ownerDebtAccount: ownerDebtAccount ? address(ownerDebtAccount) : null,
|
|
938
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
939
|
+
tokenProgram: core.tokenProgram,
|
|
940
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
941
|
+
},
|
|
942
|
+
remainingAccounts: [
|
|
943
|
+
...(await this.hlpRemainingAccounts(core.market)),
|
|
944
|
+
...(params.remainingAccounts ?? []),
|
|
945
|
+
],
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
async increaseLeverageTransaction(params) {
|
|
949
|
+
return new Transaction().add(await this.increaseLeverageInstruction(params));
|
|
950
|
+
}
|
|
951
|
+
/** Sell collateral to repay debt without closing the position. */
|
|
952
|
+
async decreaseLeverageInstruction(params) {
|
|
953
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
954
|
+
return this.instruction("decreaseLeverage", {
|
|
955
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
956
|
+
collateralAmount: integerBN(params.collateralAmount, "collateralAmount"),
|
|
957
|
+
minRepayOut: integerBN(params.minRepayOut, "minRepayOut"),
|
|
958
|
+
maxSurplusOut: integerBN(params.maxSurplusOut ?? 0n, "maxSurplusOut"),
|
|
959
|
+
}, {
|
|
960
|
+
accounts: {
|
|
961
|
+
market: core.market,
|
|
962
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
963
|
+
positionOwner: core.positionOwner,
|
|
964
|
+
leveragePosition: core.leveragePosition,
|
|
965
|
+
debtMint: core.debtMint,
|
|
966
|
+
collateralMint: core.collateralMint,
|
|
967
|
+
debtReserveVault: core.debtReserveVault,
|
|
968
|
+
collateralReserveVault: core.collateralReserveVault,
|
|
969
|
+
debtInterestVault: core.debtInterestVault,
|
|
970
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
971
|
+
leverageCollateralVault: core.leverageCollateralVault,
|
|
972
|
+
referralPartner: core.referralPartner,
|
|
973
|
+
referralAccrual: core.referralAccrual,
|
|
974
|
+
owner: core.owner,
|
|
975
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
976
|
+
tokenProgram: core.tokenProgram,
|
|
977
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
978
|
+
},
|
|
979
|
+
remainingAccounts: [
|
|
980
|
+
...(await this.hlpRemainingAccounts(core.market)),
|
|
981
|
+
...(params.remainingAccounts ?? []),
|
|
982
|
+
],
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
async decreaseLeverageTransaction(params) {
|
|
986
|
+
return new Transaction().add(await this.decreaseLeverageInstruction(params));
|
|
987
|
+
}
|
|
988
|
+
/** Repay anyone’s leverage debt from `owner`’s tokens; collateral stays with the position owner. */
|
|
989
|
+
async addLeverageMarginInstruction(params) {
|
|
990
|
+
return this.repayLeverageInstruction(params);
|
|
991
|
+
}
|
|
992
|
+
/** Permissionless repayment without an unnecessary collateral-sale quote. */
|
|
993
|
+
async repayLeverageInstruction(params) {
|
|
994
|
+
return this.leverageRepaymentInstruction(params, "repayLeverage");
|
|
995
|
+
}
|
|
996
|
+
async leverageRepaymentInstruction(params, method) {
|
|
997
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
998
|
+
return this.instruction(method, {
|
|
999
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
1000
|
+
amount: integerBN(params.amount, "amount"),
|
|
1001
|
+
}, {
|
|
1002
|
+
accounts: {
|
|
1003
|
+
market: core.market,
|
|
1004
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
1005
|
+
positionOwner: core.positionOwner,
|
|
1006
|
+
leveragePosition: core.leveragePosition,
|
|
1007
|
+
debtMint: core.debtMint,
|
|
1008
|
+
collateralMint: core.collateralMint,
|
|
1009
|
+
debtReserveVault: core.debtReserveVault,
|
|
1010
|
+
debtInterestVault: core.debtInterestVault,
|
|
1011
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
1012
|
+
referralPartner: core.referralPartner,
|
|
1013
|
+
referralAccrual: core.referralAccrual,
|
|
1014
|
+
owner: core.owner,
|
|
1015
|
+
tokenProgram: core.tokenProgram,
|
|
1016
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1017
|
+
},
|
|
1018
|
+
remainingAccounts: [...(await this.hlpRemainingAccounts(core.market)), ...(params.remainingAccounts ?? [])],
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
async repayLeverageTransaction(params) {
|
|
1022
|
+
return new Transaction().add(await this.repayLeverageInstruction(params));
|
|
1023
|
+
}
|
|
1024
|
+
async addLeverageMarginTransaction(params) {
|
|
1025
|
+
return new Transaction().add(await this.repayLeverageInstruction(params));
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* Owner-signed proportional partial/full close. Delegated closes use the delegate client.
|
|
1029
|
+
*/
|
|
1030
|
+
async closeLeverageInstruction(params, marketSnapshot) {
|
|
1031
|
+
if ("collateralFunded" in params && params.collateralFunded === true)
|
|
1032
|
+
throw new Error("Collateral-funded leverage closes are no longer supported");
|
|
1033
|
+
const closeBps = params.closeBps ?? 10_000;
|
|
1034
|
+
if (!Number.isSafeInteger(closeBps) || closeBps <= 0 || closeBps > 10_000)
|
|
1035
|
+
throw new RangeError("closeBps must be between 1 and 10000");
|
|
1036
|
+
const core = await this.resolveLeverageAccounts(params);
|
|
1037
|
+
return this.instruction("closeLeverage", {
|
|
1038
|
+
debtAsset: marketAssetIndex(params.debtAsset),
|
|
1039
|
+
minAmountOut: integerBN(params.minAmountOut, "minAmountOut"),
|
|
1040
|
+
closeBps,
|
|
1041
|
+
}, {
|
|
1042
|
+
accounts: {
|
|
1043
|
+
market: core.market,
|
|
1044
|
+
futarchyAuthority: core.futarchyAuthority,
|
|
1045
|
+
positionOwner: core.positionOwner,
|
|
1046
|
+
leveragePosition: core.leveragePosition,
|
|
1047
|
+
debtMint: core.debtMint,
|
|
1048
|
+
collateralMint: core.collateralMint,
|
|
1049
|
+
debtReserveVault: core.debtReserveVault,
|
|
1050
|
+
collateralReserveVault: core.collateralReserveVault,
|
|
1051
|
+
debtInterestVault: core.debtInterestVault,
|
|
1052
|
+
leverageCollateralVault: core.leverageCollateralVault,
|
|
1053
|
+
ownerDebtAccount: address(params.ownerDebtAccount),
|
|
1054
|
+
delegateFeeRecipient: params.delegateFeeRecipient
|
|
1055
|
+
? address(params.delegateFeeRecipient)
|
|
1056
|
+
: null,
|
|
1057
|
+
delegateExecutorAccount: params.delegateExecutorAccount
|
|
1058
|
+
? address(params.delegateExecutorAccount)
|
|
1059
|
+
: null,
|
|
1060
|
+
referralPartner: core.referralPartner,
|
|
1061
|
+
referralAccrual: core.referralAccrual,
|
|
1062
|
+
leverageDelegation: params.leverageDelegation
|
|
1063
|
+
? address(params.leverageDelegation)
|
|
1064
|
+
: null,
|
|
1065
|
+
delegatedProgram: params.delegatedProgram
|
|
1066
|
+
? address(params.delegatedProgram)
|
|
1067
|
+
: null,
|
|
1068
|
+
authority: address(params.authority ?? params.positionOwner),
|
|
1069
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
1070
|
+
tokenProgram: core.tokenProgram,
|
|
1071
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1072
|
+
},
|
|
1073
|
+
remainingAccounts: [
|
|
1074
|
+
...(await this.hlpRemainingAccounts(core.market, marketSnapshot)),
|
|
1075
|
+
...(params.remainingAccounts ?? []),
|
|
1076
|
+
],
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
async closeLeverageTransaction(params) {
|
|
1080
|
+
return new Transaction().add(await this.closeLeverageInstruction(params));
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Accounts shared across the leverage lifecycle. Individual builders pass
|
|
1084
|
+
* through only the subset their instruction declares.
|
|
1085
|
+
*/
|
|
1086
|
+
async resolveLeverageAccounts(params) {
|
|
1087
|
+
const market = address(params.market);
|
|
1088
|
+
const positionOwner = address(params.positionOwner);
|
|
1089
|
+
const debtMint = address(params.debtMint);
|
|
1090
|
+
const collateralMint = address(params.collateralMint);
|
|
1091
|
+
const positionId = address(params.positionId);
|
|
1092
|
+
const referralPartner = params.referralPartner
|
|
1093
|
+
? address(params.referralPartner)
|
|
1094
|
+
: null;
|
|
1095
|
+
return {
|
|
1096
|
+
market,
|
|
1097
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress()[0],
|
|
1098
|
+
positionOwner,
|
|
1099
|
+
owner: address(params.owner ?? params.positionOwner),
|
|
1100
|
+
leveragePosition: address(params.leveragePosition ??
|
|
1101
|
+
deriveLeveragePositionAddress(market, positionOwner, positionId, address(params.namespaceAuthority ?? params.positionOwner))[0]),
|
|
1102
|
+
debtMint,
|
|
1103
|
+
collateralMint,
|
|
1104
|
+
debtReserveVault: address(params.debtReserveVault ??
|
|
1105
|
+
deriveMarketReserveVaultAddress(market, debtMint)[0]),
|
|
1106
|
+
collateralReserveVault: address(params.collateralReserveVault ??
|
|
1107
|
+
deriveMarketReserveVaultAddress(market, collateralMint)[0]),
|
|
1108
|
+
debtInterestVault: address(params.debtInterestVault ??
|
|
1109
|
+
deriveMarketInterestVaultAddress(market, debtMint)[0]),
|
|
1110
|
+
leverageCollateralVault: address(params.leverageCollateralVault ??
|
|
1111
|
+
deriveLeverageCollateralVaultAddress(market, collateralMint)[0]),
|
|
1112
|
+
referralPartner,
|
|
1113
|
+
referralAccrual: referralPartner
|
|
1114
|
+
? deriveReferralAccrualAddress(referralPartner, market, debtMint)[0]
|
|
1115
|
+
: null,
|
|
1116
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
async resolveSrlpAccounts(params) {
|
|
1120
|
+
const market = address(params.market);
|
|
1121
|
+
const owner = address(params.owner);
|
|
1122
|
+
const baseMint = address(params.baseMint);
|
|
1123
|
+
const quoteMint = address(params.quoteMint);
|
|
1124
|
+
const srlpMint = address(params.srlpMint);
|
|
1125
|
+
return {
|
|
1126
|
+
market,
|
|
1127
|
+
owner,
|
|
1128
|
+
baseMint,
|
|
1129
|
+
quoteMint,
|
|
1130
|
+
srlpMint,
|
|
1131
|
+
baseReserveVault: address(params.baseReserveVault ??
|
|
1132
|
+
deriveMarketReserveVaultAddress(market, baseMint)[0]),
|
|
1133
|
+
quoteReserveVault: address(params.quoteReserveVault ??
|
|
1134
|
+
deriveMarketReserveVaultAddress(market, quoteMint)[0]),
|
|
1135
|
+
ownerBaseAccount: address(params.ownerBaseAccount),
|
|
1136
|
+
ownerQuoteAccount: address(params.ownerQuoteAccount),
|
|
1137
|
+
ownerSrlpAccount: address(params.ownerSrlpAccount),
|
|
1138
|
+
baseYieldAccount: address(params.baseYieldAccount ??
|
|
1139
|
+
deriveYieldAccountAddress(market, owner, srlpMint, baseMint, "srlp", this.program.programId)[0]),
|
|
1140
|
+
quoteYieldAccount: address(params.quoteYieldAccount ??
|
|
1141
|
+
deriveYieldAccountAddress(market, owner, srlpMint, quoteMint, "srlp", this.program.programId)[0]),
|
|
1142
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1143
|
+
token2022Program: TOKEN_2022_PROGRAM_ID,
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
function marketAssetIndex(side) {
|
|
1148
|
+
if (side !== "base" && side !== "quote") {
|
|
1149
|
+
throw new Error(`debtAsset must be "base" or "quote", got ${String(side)}`);
|
|
1150
|
+
}
|
|
1151
|
+
return side === "base" ? 0 : 1;
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* Widen a launch config into the shape Anchor expects: every `u64` as a BN,
|
|
1155
|
+
* every `u16` left as a number. Written out field by field rather than mapped,
|
|
1156
|
+
* so a field added to the program fails to compile here instead of silently
|
|
1157
|
+
* going missing.
|
|
1158
|
+
*/
|
|
1159
|
+
function anchorMarketConfig(config) {
|
|
1160
|
+
const bn = (value, label) => integerBN(value, label);
|
|
1161
|
+
if (bn(config.amm.peakAmplificationNad, "peakAmplificationNad").gtn(1_000_000_000)
|
|
1162
|
+
&& bn(config.amm.adjustmentStepNad, "adjustmentStepNad").isZero()) {
|
|
1163
|
+
throw new Error("concentrated markets require an enabled center controller");
|
|
1164
|
+
}
|
|
1165
|
+
return {
|
|
1166
|
+
swapFeeBps: config.swapFeeBps,
|
|
1167
|
+
divergenceFeeShareCapBps: config.divergenceFeeShareCapBps,
|
|
1168
|
+
volatilityFeeShareCapBps: config.volatilityFeeShareCapBps,
|
|
1169
|
+
targetHlpLeverageBps: config.targetHlpLeverageBps,
|
|
1170
|
+
settlementDivergenceBps: config.settlementDivergenceBps,
|
|
1171
|
+
emaHalfLifeMs: bn(config.emaHalfLifeMs, "emaHalfLifeMs"),
|
|
1172
|
+
directionalEmaHalfLifeMs: bn(config.directionalEmaHalfLifeMs, "directionalEmaHalfLifeMs"),
|
|
1173
|
+
curveDepthEmaHalfLifeMs: bn(config.curveDepthEmaHalfLifeMs, "curveDepthEmaHalfLifeMs"),
|
|
1174
|
+
maxDailyBorrowBps: config.maxDailyBorrowBps,
|
|
1175
|
+
globalHealthContributionCapBps: config.globalHealthContributionCapBps,
|
|
1176
|
+
borrowMarketHealthFloorBps: config.borrowMarketHealthFloorBps,
|
|
1177
|
+
amm: anchorIntegerFields(config.amm, AMM_U64_FIELDS),
|
|
1178
|
+
irm: anchorIntegerFields(config.irm, IRM_U64_FIELDS),
|
|
1179
|
+
startTime: bn(config.startTime, "startTime"),
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
const AMM_U64_FIELDS = [
|
|
1183
|
+
"peakAmplificationNad",
|
|
1184
|
+
"centerEmaHalfLifeMs",
|
|
1185
|
+
"volatilityHalfLifeMs",
|
|
1186
|
+
"adjustmentStepNad",
|
|
1187
|
+
"volatilityShockCapNad",
|
|
1188
|
+
"volatilityCapNad",
|
|
1189
|
+
"divergenceFeeCoefficientNad",
|
|
1190
|
+
"volatilityFeeCoefficientNad",
|
|
1191
|
+
"launchFeeDurationSeconds",
|
|
1192
|
+
"launchRateLimitReferenceNad",
|
|
1193
|
+
"launchRateLimitDurationSeconds",
|
|
1194
|
+
];
|
|
1195
|
+
const IRM_U64_FIELDS = ["lowerSignalNad", "upperSignalNad", "halfLifeMs", "initialAprNad"];
|
|
1196
|
+
function anchorIntegerFields(source, wide) {
|
|
1197
|
+
const out = { ...source };
|
|
1198
|
+
for (const field of wide) {
|
|
1199
|
+
if (out[field] === undefined)
|
|
1200
|
+
continue;
|
|
1201
|
+
out[field] = integerBN(out[field], field);
|
|
1202
|
+
}
|
|
1203
|
+
return out;
|
|
303
1204
|
}
|
|
304
1205
|
function normalizeArgs(args) {
|
|
305
1206
|
if (args === undefined) {
|
|
@@ -322,4 +1223,47 @@ function mergeAccountMetas(...groups) {
|
|
|
322
1223
|
}
|
|
323
1224
|
return [...merged.values()];
|
|
324
1225
|
}
|
|
1226
|
+
// Kept in step with the on-chain instruction binding boundary.
|
|
1227
|
+
const ORACLE_READ_INSTRUCTIONS = new Set([
|
|
1228
|
+
"addLiquidity",
|
|
1229
|
+
"openLiquidityGates",
|
|
1230
|
+
"removeLiquidity",
|
|
1231
|
+
"fundInterest",
|
|
1232
|
+
"harvest",
|
|
1233
|
+
"swap",
|
|
1234
|
+
"previewHlpFlash",
|
|
1235
|
+
"beginHlpFlash",
|
|
1236
|
+
"settleHlpFlash",
|
|
1237
|
+
"closeInsolventHlp",
|
|
1238
|
+
"beginFlashLiquidation",
|
|
1239
|
+
"previewFlashLiquidation",
|
|
1240
|
+
"previewFlashAmmLiquidation",
|
|
1241
|
+
"emergencyLiquidation",
|
|
1242
|
+
"previewEmergencyLiquidation",
|
|
1243
|
+
"previewLiquidationHealth",
|
|
1244
|
+
"settleFlashLiquidation",
|
|
1245
|
+
"depositCollateral",
|
|
1246
|
+
"withdrawCollateral",
|
|
1247
|
+
"borrow",
|
|
1248
|
+
"repay",
|
|
1249
|
+
"donateCollateral",
|
|
1250
|
+
"openLeverage",
|
|
1251
|
+
"closeLeverage",
|
|
1252
|
+
"delegatedCloseLeverage",
|
|
1253
|
+
"increaseLeverage",
|
|
1254
|
+
"decreaseLeverage",
|
|
1255
|
+
"addLeverageMargin",
|
|
1256
|
+
"repayLeverage",
|
|
1257
|
+
"previewHlpDepositCapacity",
|
|
1258
|
+
"previewMarket",
|
|
1259
|
+
"previewHlpOrderTrigger",
|
|
1260
|
+
"previewAddLiquidity",
|
|
1261
|
+
"previewSwap",
|
|
1262
|
+
"previewBorrowCapacity",
|
|
1263
|
+
"previewBorrowPositionCapacity",
|
|
1264
|
+
"previewBorrowPosition",
|
|
1265
|
+
"depositSingleSided",
|
|
1266
|
+
"withdrawSingleSided",
|
|
1267
|
+
"executeOperatorChange",
|
|
1268
|
+
]);
|
|
325
1269
|
//# sourceMappingURL=write.js.map
|