@omnipair/dusk-sdk 2.0.2 → 2.16.0-next.20261102
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1011 -16
- 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 +71 -14
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +155 -28
- package/dist/constants.js.map +1 -1
- package/dist/delegate.d.ts +94 -0
- package/dist/delegate.d.ts.map +1 -0
- package/dist/delegate.js +131 -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 +3253 -378
- package/dist/get.d.ts.map +1 -1
- package/dist/get.js +130 -70
- package/dist/get.js.map +1 -1
- package/dist/governance-client.d.ts +169 -0
- package/dist/governance-client.d.ts.map +1 -0
- package/dist/governance-client.js +146 -0
- package/dist/governance-client.js.map +1 -0
- package/dist/governance.d.ts +313 -0
- package/dist/governance.d.ts.map +1 -0
- package/dist/governance.js +902 -0
- package/dist/governance.js.map +1 -0
- 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 +106 -0
- package/dist/hlp-liquidation-client.d.ts.map +1 -0
- package/dist/hlp-liquidation-client.js +150 -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 +6119 -0
- package/dist/idl_v2.json +17175 -5862
- 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 +49 -0
- package/dist/interest-accounting.d.ts.map +1 -0
- package/dist/interest-accounting.js +72 -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 +587 -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 +230 -0
- package/dist/liquidation-client.d.ts.map +1 -0
- package/dist/liquidation-client.js +239 -0
- package/dist/liquidation-client.js.map +1 -0
- package/dist/liquidation.d.ts +78 -0
- package/dist/liquidation.d.ts.map +1 -0
- package/dist/liquidation.js +122 -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 +158 -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/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/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 +14 -0
- package/dist/risk-pricing.d.ts.map +1 -0
- package/dist/risk-pricing.js +25 -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 +271 -31
- package/dist/type-aliases.d.ts.map +1 -1
- package/dist/types_delegate.d.ts +6126 -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 +17235 -5922
- 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 +608 -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 +1241 -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 +517 -1
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +1172 -23
- 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 +9 -2
- package/src/amounts.ts +13 -0
- package/src/constants.ts +236 -26
- package/src/delegate.ts +238 -0
- package/src/dusk.ts +28 -0
- package/src/get.ts +247 -82
- package/src/governance-client.ts +234 -0
- package/src/governance.ts +1422 -0
- package/src/hash.ts +15 -0
- package/src/hlp-liquidation-client.ts +195 -0
- package/src/idl_delegate.json +6119 -0
- package/src/idl_delegate.ts +3 -0
- package/src/idl_v2.json +17175 -5862
- package/src/index.ts +31 -0
- package/src/interest-accounting.ts +85 -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 +297 -0
- package/src/liquidation.ts +137 -0
- package/src/lp-naming.ts +127 -0
- package/src/lp-vanity.ts +135 -0
- package/src/market-bootstrap.ts +273 -0
- package/src/market-launch.ts +103 -0
- package/src/oracle.ts +120 -0
- package/src/preview.ts +53 -1
- package/src/protection.ts +112 -0
- package/src/risk-pricing.ts +28 -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 +280 -31
- package/src/types_delegate.ts +6125 -0
- package/src/types_v2.ts +17174 -5861
- 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 +2073 -34
- package/src/yield-client.ts +117 -0
package/src/constants.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { AccountMeta, PublicKey } from "@solana/web3.js";
|
|
2
|
+
import IDL from "./idl_v2.js";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
/** Shared on-chain default; regenerated from the program instead of duplicated here. */
|
|
5
|
+
export const DEFAULT_PROTOCOL_REVENUE_SHARE_BPS = Number(
|
|
6
|
+
IDL.constants.find(({ name }) => name === "DEFAULT_PROTOCOL_REVENUE_SHARE_BPS")!.value
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
const DEFAULT_PROGRAM_ID = "5EuU8artxWthfER6GTUY52TfL1aRsQH2TH8DsdGCa8cJ";
|
|
4
10
|
const MPL_TOKEN_METADATA_PROGRAM_ID = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s";
|
|
5
11
|
|
|
6
12
|
function getProgramIdFromEnv(fallback: string): string {
|
|
@@ -9,14 +15,35 @@ function getProgramIdFromEnv(fallback: string): string {
|
|
|
9
15
|
}
|
|
10
16
|
|
|
11
17
|
/**
|
|
12
|
-
* Omnipair Dusk
|
|
18
|
+
* Omnipair Dusk program ID.
|
|
13
19
|
* Reads from env DUSK_PROGRAM_ID.
|
|
14
20
|
*/
|
|
15
21
|
export const DUSK_PROGRAM_ID = new PublicKey(getProgramIdFromEnv(DEFAULT_PROGRAM_ID));
|
|
16
22
|
|
|
17
23
|
export const PROGRAM_ID = DUSK_PROGRAM_ID;
|
|
24
|
+
/** Current development Market account layout. */
|
|
25
|
+
export const MARKET_LAYOUT_VERSION = Number(
|
|
26
|
+
IDL.constants.find(({ name }) => name === "MARKET_LAYOUT_VERSION")!.value
|
|
27
|
+
);
|
|
18
28
|
export const TOKEN_METADATA_PROGRAM_ID = new PublicKey(MPL_TOKEN_METADATA_PROGRAM_ID);
|
|
19
29
|
|
|
30
|
+
/** Direct-yLP parameter-governance constants. These values are immutable for Dusk market layout v1. */
|
|
31
|
+
export const GOVERNANCE_BPS_DENOMINATOR = 10_000;
|
|
32
|
+
export const PARAMETER_PROPOSAL_SPONSOR_BPS = 100;
|
|
33
|
+
export const PARAMETER_PROPOSAL_SUPPORT_BPS = 5_000;
|
|
34
|
+
export const PARAMETER_PROPOSAL_TIMELOCK_SECONDS = 7 * 24 * 60 * 60;
|
|
35
|
+
export const PARAMETER_PROPOSAL_EXECUTION_WINDOW_SECONDS = 7 * 24 * 60 * 60;
|
|
36
|
+
export const PARAMETER_EXECUTION_MAX_UTILIZATION_BPS = 8_000;
|
|
37
|
+
export const PROPOSAL_METADATA_VERSION = 1;
|
|
38
|
+
export const MAX_PROPOSAL_TITLE_BYTES = 96;
|
|
39
|
+
export const MAX_PROPOSAL_DESCRIPTION_URI_BYTES = 200;
|
|
40
|
+
export const MAX_PROPOSAL_DESCRIPTION_BYTES = 32_768;
|
|
41
|
+
// Nominal 1-7 day ramp at Solana's current 200 ms scheduled slot cadence.
|
|
42
|
+
// Update with the on-chain timing constant if the network cadence changes.
|
|
43
|
+
export const DEFAULT_CONCENTRATION_RAMP_DURATION_SLOTS = 432_000;
|
|
44
|
+
export const MIN_CONCENTRATION_RAMP_DURATION_SLOTS = 432_000;
|
|
45
|
+
export const MAX_CONCENTRATION_RAMP_DURATION_SLOTS = 3_024_000;
|
|
46
|
+
|
|
20
47
|
/**
|
|
21
48
|
* PDA seeds used by the program
|
|
22
49
|
*/
|
|
@@ -27,15 +54,28 @@ export const SEEDS = {
|
|
|
27
54
|
MARKET_INTEREST_VAULT: Buffer.from("market_interest"),
|
|
28
55
|
BORROW_POSITION: Buffer.from("borrow_position_v2"),
|
|
29
56
|
LEVERAGE_POSITION: Buffer.from("leverage_position_v2"),
|
|
57
|
+
LEVERAGE_COLLATERAL_VAULT: Buffer.from("leverage_collateral"),
|
|
58
|
+
LEVERAGE_DELEGATION: Buffer.from("leverage_delegation_v2"),
|
|
30
59
|
YIELD_ACCOUNT: Buffer.from("yield"),
|
|
31
60
|
HLP_YLP_VAULT: Buffer.from("hlp_ylp_vault"),
|
|
61
|
+
HLP_COHORT: Buffer.from("hlp_cohort"),
|
|
32
62
|
INSURANCE: Buffer.from("insurance"),
|
|
63
|
+
EVENT_AUTHORITY: Buffer.from("__event_authority"),
|
|
33
64
|
FUTARCHY_AUTHORITY: Buffer.from("futarchy_authority"),
|
|
34
65
|
REFERRAL_PARTNER: Buffer.from("referral_partner"),
|
|
35
66
|
REFERRAL_ACCRUAL: Buffer.from("referral_accrual"),
|
|
67
|
+
PARAMETER_PROPOSAL: Buffer.from("parameter_proposal"),
|
|
68
|
+
PROPOSAL_SUPPORT: Buffer.from("proposal_support"),
|
|
36
69
|
METADATA: Buffer.from("metadata"),
|
|
37
70
|
} as const;
|
|
38
71
|
|
|
72
|
+
/** Derive the canonical Anchor event-CPI authority for a Dusk program. */
|
|
73
|
+
export function deriveEventAuthorityAddress(
|
|
74
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
75
|
+
): [PublicKey, number] {
|
|
76
|
+
return PublicKey.findProgramAddressSync([SEEDS.EVENT_AUTHORITY], programId);
|
|
77
|
+
}
|
|
78
|
+
|
|
39
79
|
function normalizeParamsHash(paramsHash: Uint8Array | Buffer | number[]): Buffer {
|
|
40
80
|
const hash = Buffer.from(paramsHash);
|
|
41
81
|
if (hash.length !== 32) {
|
|
@@ -47,8 +87,8 @@ function normalizeParamsHash(paramsHash: Uint8Array | Buffer | number[]): Buffer
|
|
|
47
87
|
/**
|
|
48
88
|
* Derive Futarchy Authority PDA address
|
|
49
89
|
*/
|
|
50
|
-
export function deriveFutarchyAuthorityAddress(): [PublicKey, number] {
|
|
51
|
-
return PublicKey.findProgramAddressSync([SEEDS.FUTARCHY_AUTHORITY],
|
|
90
|
+
export function deriveFutarchyAuthorityAddress(programId: PublicKey = DUSK_PROGRAM_ID): [PublicKey, number] {
|
|
91
|
+
return PublicKey.findProgramAddressSync([SEEDS.FUTARCHY_AUTHORITY], programId);
|
|
52
92
|
}
|
|
53
93
|
|
|
54
94
|
/** Derive the protocol-wide referral partner for a referrer authority. */
|
|
@@ -63,7 +103,8 @@ export function deriveReferralPartnerAddress(authority: PublicKey): [PublicKey,
|
|
|
63
103
|
export function deriveReferralAccrualAddress(
|
|
64
104
|
referralPartner: PublicKey,
|
|
65
105
|
market: PublicKey,
|
|
66
|
-
assetMint: PublicKey
|
|
106
|
+
assetMint: PublicKey,
|
|
107
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
67
108
|
): [PublicKey, number] {
|
|
68
109
|
return PublicKey.findProgramAddressSync(
|
|
69
110
|
[
|
|
@@ -72,7 +113,52 @@ export function deriveReferralAccrualAddress(
|
|
|
72
113
|
market.toBuffer(),
|
|
73
114
|
assetMint.toBuffer(),
|
|
74
115
|
],
|
|
75
|
-
|
|
116
|
+
programId
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type U64SeedLike = bigint | number | string | { toString(): string };
|
|
121
|
+
|
|
122
|
+
function u64Seed(value: U64SeedLike, label: string): Buffer {
|
|
123
|
+
if (typeof value === "number" && !Number.isSafeInteger(value)) {
|
|
124
|
+
throw new Error(`${label} must be a safe integer when supplied as a number`);
|
|
125
|
+
}
|
|
126
|
+
let normalized: bigint;
|
|
127
|
+
try {
|
|
128
|
+
normalized = BigInt(value.toString());
|
|
129
|
+
} catch {
|
|
130
|
+
throw new Error(`${label} must be an unsigned 64-bit integer`);
|
|
131
|
+
}
|
|
132
|
+
if (normalized < 0n || normalized > 0xffff_ffff_ffff_ffffn) {
|
|
133
|
+
throw new Error(`${label} must be an unsigned 64-bit integer`);
|
|
134
|
+
}
|
|
135
|
+
const encoded = Buffer.alloc(8);
|
|
136
|
+
encoded.writeBigUInt64LE(normalized);
|
|
137
|
+
return encoded;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Derive one immutable typed parameter proposal. The nonce is Borsh u64 LE. */
|
|
141
|
+
export function deriveParameterProposalAddress(
|
|
142
|
+
market: PublicKey,
|
|
143
|
+
proposer: PublicKey,
|
|
144
|
+
nonce: U64SeedLike,
|
|
145
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
146
|
+
): [PublicKey, number] {
|
|
147
|
+
return PublicKey.findProgramAddressSync(
|
|
148
|
+
[SEEDS.PARAMETER_PROPOSAL, market.toBuffer(), proposer.toBuffer(), u64Seed(nonce, "nonce")],
|
|
149
|
+
programId
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Derive one support position. Locked yLP cannot be shared across proposals. */
|
|
154
|
+
export function deriveProposalSupportAddress(
|
|
155
|
+
proposal: PublicKey,
|
|
156
|
+
supporter: PublicKey,
|
|
157
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
158
|
+
): [PublicKey, number] {
|
|
159
|
+
return PublicKey.findProgramAddressSync(
|
|
160
|
+
[SEEDS.PROPOSAL_SUPPORT, proposal.toBuffer(), supporter.toBuffer()],
|
|
161
|
+
programId
|
|
76
162
|
);
|
|
77
163
|
}
|
|
78
164
|
|
|
@@ -82,7 +168,8 @@ export function deriveReferralAccrualAddress(
|
|
|
82
168
|
export function deriveMarketAddress(
|
|
83
169
|
baseMint: PublicKey,
|
|
84
170
|
quoteMint: PublicKey,
|
|
85
|
-
paramsHash: Uint8Array | Buffer | number[]
|
|
171
|
+
paramsHash: Uint8Array | Buffer | number[],
|
|
172
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
86
173
|
): [PublicKey, number] {
|
|
87
174
|
return PublicKey.findProgramAddressSync(
|
|
88
175
|
[
|
|
@@ -91,7 +178,7 @@ export function deriveMarketAddress(
|
|
|
91
178
|
quoteMint.toBuffer(),
|
|
92
179
|
normalizeParamsHash(paramsHash),
|
|
93
180
|
],
|
|
94
|
-
|
|
181
|
+
programId
|
|
95
182
|
);
|
|
96
183
|
}
|
|
97
184
|
|
|
@@ -121,6 +208,18 @@ export function deriveMarketReserveVaultAddress(
|
|
|
121
208
|
/**
|
|
122
209
|
* Derive market collateral vault PDA address
|
|
123
210
|
*/
|
|
211
|
+
/** Per-market collateral custody for leverage positions. */
|
|
212
|
+
export function deriveLeverageCollateralVaultAddress(
|
|
213
|
+
market: PublicKey,
|
|
214
|
+
collateralMint: PublicKey,
|
|
215
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
216
|
+
): [PublicKey, number] {
|
|
217
|
+
return PublicKey.findProgramAddressSync(
|
|
218
|
+
[SEEDS.LEVERAGE_COLLATERAL_VAULT, market.toBuffer(), collateralMint.toBuffer()],
|
|
219
|
+
programId
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
124
223
|
export function deriveMarketCollateralVaultAddress(
|
|
125
224
|
market: PublicKey,
|
|
126
225
|
collateralMint: PublicKey
|
|
@@ -149,10 +248,11 @@ export function deriveMarketInterestVaultAddress(
|
|
|
149
248
|
*/
|
|
150
249
|
export function deriveBorrowPositionAddress(
|
|
151
250
|
market: PublicKey,
|
|
251
|
+
owner: PublicKey,
|
|
152
252
|
positionId: PublicKey
|
|
153
253
|
): [PublicKey, number] {
|
|
154
254
|
return PublicKey.findProgramAddressSync(
|
|
155
|
-
[SEEDS.BORROW_POSITION, market.toBuffer(), positionId.toBuffer()],
|
|
255
|
+
[SEEDS.BORROW_POSITION, market.toBuffer(), owner.toBuffer(), positionId.toBuffer()],
|
|
156
256
|
DUSK_PROGRAM_ID
|
|
157
257
|
);
|
|
158
258
|
}
|
|
@@ -160,21 +260,40 @@ export function deriveBorrowPositionAddress(
|
|
|
160
260
|
/**
|
|
161
261
|
* Derive leverage position PDA address
|
|
162
262
|
*/
|
|
263
|
+
/** Delegation is keyed by the position it delegates, not by the owner. */
|
|
264
|
+
export function deriveLeverageDelegationAddress(
|
|
265
|
+
leveragePosition: PublicKey
|
|
266
|
+
): [PublicKey, number] {
|
|
267
|
+
return PublicKey.findProgramAddressSync(
|
|
268
|
+
[SEEDS.LEVERAGE_DELEGATION, leveragePosition.toBuffer()],
|
|
269
|
+
DUSK_PROGRAM_ID
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
163
273
|
export function deriveLeveragePositionAddress(
|
|
164
274
|
market: PublicKey,
|
|
165
|
-
|
|
275
|
+
positionOwner: PublicKey,
|
|
276
|
+
positionId: PublicKey,
|
|
277
|
+
namespaceAuthority: PublicKey = positionOwner
|
|
166
278
|
): [PublicKey, number] {
|
|
167
279
|
return PublicKey.findProgramAddressSync(
|
|
168
|
-
[
|
|
280
|
+
[
|
|
281
|
+
SEEDS.LEVERAGE_POSITION,
|
|
282
|
+
market.toBuffer(),
|
|
283
|
+
positionOwner.toBuffer(),
|
|
284
|
+
namespaceAuthority.toBuffer(),
|
|
285
|
+
positionId.toBuffer(),
|
|
286
|
+
],
|
|
169
287
|
DUSK_PROGRAM_ID
|
|
170
288
|
);
|
|
171
289
|
}
|
|
172
290
|
|
|
173
|
-
export type YieldTokenKind = "ylp" | "hlp" | 0 | 1;
|
|
291
|
+
export type YieldTokenKind = "ylp" | "hlp" | "junior" | 0 | 1 | 2;
|
|
174
292
|
|
|
175
293
|
function yieldTokenKindCode(tokenKind: YieldTokenKind): number {
|
|
176
294
|
if (tokenKind === "ylp" || tokenKind === 0) return 0;
|
|
177
295
|
if (tokenKind === "hlp" || tokenKind === 1) return 1;
|
|
296
|
+
if (tokenKind === "junior" || tokenKind === 2) return 2;
|
|
178
297
|
throw new Error(`Unsupported yield token kind: ${tokenKind}`);
|
|
179
298
|
}
|
|
180
299
|
|
|
@@ -186,7 +305,8 @@ export function deriveYieldAccountAddress(
|
|
|
186
305
|
owner: PublicKey,
|
|
187
306
|
lpMint: PublicKey,
|
|
188
307
|
assetMint: PublicKey,
|
|
189
|
-
tokenKind: YieldTokenKind
|
|
308
|
+
tokenKind: YieldTokenKind,
|
|
309
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
190
310
|
): [PublicKey, number] {
|
|
191
311
|
return PublicKey.findProgramAddressSync(
|
|
192
312
|
[
|
|
@@ -197,7 +317,7 @@ export function deriveYieldAccountAddress(
|
|
|
197
317
|
assetMint.toBuffer(),
|
|
198
318
|
Buffer.from([yieldTokenKindCode(tokenKind)]),
|
|
199
319
|
],
|
|
200
|
-
|
|
320
|
+
programId
|
|
201
321
|
);
|
|
202
322
|
}
|
|
203
323
|
|
|
@@ -209,6 +329,7 @@ export interface YieldTransferHookAccountsArgs {
|
|
|
209
329
|
baseMint: PublicKey;
|
|
210
330
|
quoteMint: PublicKey;
|
|
211
331
|
tokenKind: YieldTokenKind;
|
|
332
|
+
programId?: PublicKey;
|
|
212
333
|
}
|
|
213
334
|
|
|
214
335
|
export const TRANSFER_HOOK_EXECUTE_DISCRIMINATOR = Buffer.from([
|
|
@@ -241,15 +362,37 @@ export interface LpTransferHookValidationArgs {
|
|
|
241
362
|
baseMint: PublicKey;
|
|
242
363
|
quoteMint: PublicKey;
|
|
243
364
|
tokenKind: YieldTokenKind;
|
|
365
|
+
/**
|
|
366
|
+
* Encode the seven-account layout of mints initialized before the event
|
|
367
|
+
* authority and Dusk program were added. Those mints publish no
|
|
368
|
+
* `LpTransferred` receipt.
|
|
369
|
+
*/
|
|
370
|
+
legacyLayout?: boolean;
|
|
371
|
+
programId?: PublicKey;
|
|
244
372
|
}
|
|
245
373
|
|
|
246
374
|
/**
|
|
247
375
|
* Derive the standard Token-2022 transfer-hook validation PDA for a Dusk LP mint.
|
|
248
376
|
*/
|
|
249
|
-
export function deriveYieldTransferHookValidationAddress(
|
|
377
|
+
export function deriveYieldTransferHookValidationAddress(
|
|
378
|
+
lpMint: PublicKey,
|
|
379
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
380
|
+
): [PublicKey, number] {
|
|
250
381
|
return PublicKey.findProgramAddressSync(
|
|
251
382
|
[Buffer.from("extra-account-metas"), lpMint.toBuffer()],
|
|
252
|
-
|
|
383
|
+
programId
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Mint-specific hLP cohort; retired mints retain their frozen yield state. */
|
|
388
|
+
export function deriveHlpCohortAddress(
|
|
389
|
+
market: PublicKey,
|
|
390
|
+
hlpMint: PublicKey,
|
|
391
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
392
|
+
): [PublicKey, number] {
|
|
393
|
+
return PublicKey.findProgramAddressSync(
|
|
394
|
+
[SEEDS.HLP_COHORT, market.toBuffer(), hlpMint.toBuffer()],
|
|
395
|
+
programId
|
|
253
396
|
);
|
|
254
397
|
}
|
|
255
398
|
|
|
@@ -391,7 +534,19 @@ export function buildLpTransferHookValidationAccountData({
|
|
|
391
534
|
baseMint,
|
|
392
535
|
quoteMint,
|
|
393
536
|
tokenKind,
|
|
537
|
+
legacyLayout = false,
|
|
538
|
+
programId = DUSK_PROGRAM_ID,
|
|
394
539
|
}: LpTransferHookValidationArgs): Buffer {
|
|
540
|
+
const eventMetas = legacyLayout
|
|
541
|
+
? []
|
|
542
|
+
: [
|
|
543
|
+
staticTransferHookMeta({
|
|
544
|
+
pubkey: deriveEventAuthorityAddress(programId)[0],
|
|
545
|
+
isSigner: false,
|
|
546
|
+
isWritable: false,
|
|
547
|
+
}),
|
|
548
|
+
staticTransferHookMeta({ pubkey: programId, isSigner: false, isWritable: false }),
|
|
549
|
+
];
|
|
395
550
|
return encodeTransferHookValidationAccountData([
|
|
396
551
|
staticTransferHookMeta({ pubkey: market, isSigner: false, isWritable: true }),
|
|
397
552
|
staticTransferHookMeta({ pubkey: baseMint, isSigner: false, isWritable: false }),
|
|
@@ -420,6 +575,19 @@ export function buildLpTransferHookValidationAccountData({
|
|
|
420
575
|
),
|
|
421
576
|
true
|
|
422
577
|
),
|
|
578
|
+
...eventMetas,
|
|
579
|
+
...(yieldTokenKindCode(tokenKind) === 1 && !legacyLayout
|
|
580
|
+
? [
|
|
581
|
+
pdaTransferHookMeta(
|
|
582
|
+
[
|
|
583
|
+
{ kind: "literal", bytes: SEEDS.HLP_COHORT },
|
|
584
|
+
{ kind: "accountKey", index: TRANSFER_HOOK_MARKET_ACCOUNT_INDEX },
|
|
585
|
+
{ kind: "accountKey", index: 1 },
|
|
586
|
+
],
|
|
587
|
+
false
|
|
588
|
+
),
|
|
589
|
+
]
|
|
590
|
+
: []),
|
|
423
591
|
]);
|
|
424
592
|
}
|
|
425
593
|
|
|
@@ -427,10 +595,13 @@ export function buildLpTransferHookValidationAccountData({
|
|
|
427
595
|
* Build Dusk yLP/hLP transfer-hook extra account metas.
|
|
428
596
|
*
|
|
429
597
|
* Token-2022 passes the source token account, LP mint, destination token
|
|
430
|
-
* account, and transfer authority as the base hook accounts. Omnipair Dusk
|
|
598
|
+
* account, and transfer authority as the base hook accounts. Omnipair Dusk needs
|
|
431
599
|
* the market, underlying asset mint, canonical source/destination yield
|
|
432
600
|
* accounts, hook program, and standard validation PDA as extra metas so the
|
|
433
|
-
* hook can checkpoint revenue before the balance move is finalized.
|
|
601
|
+
* hook can checkpoint revenue before the balance move is finalized. The event
|
|
602
|
+
* authority lets the hook publish `LpTransferred`; Token-2022 ignores it for
|
|
603
|
+
* mints that still use the legacy seven-account layout, so the list works for
|
|
604
|
+
* both layouts.
|
|
434
605
|
*/
|
|
435
606
|
export function buildLpTransferHookAccountMetas({
|
|
436
607
|
lpMint,
|
|
@@ -440,36 +611,41 @@ export function buildLpTransferHookAccountMetas({
|
|
|
440
611
|
baseMint,
|
|
441
612
|
quoteMint,
|
|
442
613
|
tokenKind,
|
|
614
|
+
programId = DUSK_PROGRAM_ID,
|
|
443
615
|
}: YieldTransferHookAccountsArgs): AccountMeta[] {
|
|
444
616
|
const sourceBaseYieldAccount = deriveYieldAccountAddress(
|
|
445
617
|
market,
|
|
446
618
|
sourceOwner,
|
|
447
619
|
lpMint,
|
|
448
620
|
baseMint,
|
|
449
|
-
tokenKind
|
|
621
|
+
tokenKind,
|
|
622
|
+
programId
|
|
450
623
|
)[0];
|
|
451
624
|
const destinationBaseYieldAccount = deriveYieldAccountAddress(
|
|
452
625
|
market,
|
|
453
626
|
destinationOwner,
|
|
454
627
|
lpMint,
|
|
455
628
|
baseMint,
|
|
456
|
-
tokenKind
|
|
629
|
+
tokenKind,
|
|
630
|
+
programId
|
|
457
631
|
)[0];
|
|
458
632
|
const sourceQuoteYieldAccount = deriveYieldAccountAddress(
|
|
459
633
|
market,
|
|
460
634
|
sourceOwner,
|
|
461
635
|
lpMint,
|
|
462
636
|
quoteMint,
|
|
463
|
-
tokenKind
|
|
637
|
+
tokenKind,
|
|
638
|
+
programId
|
|
464
639
|
)[0];
|
|
465
640
|
const destinationQuoteYieldAccount = deriveYieldAccountAddress(
|
|
466
641
|
market,
|
|
467
642
|
destinationOwner,
|
|
468
643
|
lpMint,
|
|
469
644
|
quoteMint,
|
|
470
|
-
tokenKind
|
|
645
|
+
tokenKind,
|
|
646
|
+
programId
|
|
471
647
|
)[0];
|
|
472
|
-
const validationAccount = deriveYieldTransferHookValidationAddress(lpMint)[0];
|
|
648
|
+
const validationAccount = deriveYieldTransferHookValidationAddress(lpMint, programId)[0];
|
|
473
649
|
|
|
474
650
|
return [
|
|
475
651
|
{ pubkey: market, isSigner: false, isWritable: true },
|
|
@@ -479,7 +655,13 @@ export function buildLpTransferHookAccountMetas({
|
|
|
479
655
|
{ pubkey: destinationBaseYieldAccount, isSigner: false, isWritable: true },
|
|
480
656
|
{ pubkey: sourceQuoteYieldAccount, isSigner: false, isWritable: true },
|
|
481
657
|
{ pubkey: destinationQuoteYieldAccount, isSigner: false, isWritable: true },
|
|
482
|
-
{ pubkey:
|
|
658
|
+
{ pubkey: deriveEventAuthorityAddress(programId)[0], isSigner: false, isWritable: false },
|
|
659
|
+
{ pubkey: programId, isSigner: false, isWritable: false },
|
|
660
|
+
...(yieldTokenKindCode(tokenKind) === 1
|
|
661
|
+
? [{ pubkey: deriveHlpCohortAddress(market, lpMint, programId)[0], isSigner: false, isWritable: false }]
|
|
662
|
+
: yieldTokenKindCode(tokenKind) === 2
|
|
663
|
+
? [{ pubkey: deriveJuniorCohortAddress(market, lpMint, programId)[0], isSigner: false, isWritable: false }]
|
|
664
|
+
: []),
|
|
483
665
|
{ pubkey: validationAccount, isSigner: false, isWritable: false },
|
|
484
666
|
];
|
|
485
667
|
}
|
|
@@ -508,10 +690,38 @@ export function deriveHlpYlpVaultAddress(
|
|
|
508
690
|
*/
|
|
509
691
|
export function deriveInsuranceAddress(
|
|
510
692
|
market: PublicKey,
|
|
511
|
-
assetMint: PublicKey
|
|
693
|
+
assetMint: PublicKey,
|
|
694
|
+
programId: PublicKey = DUSK_PROGRAM_ID
|
|
512
695
|
): [PublicKey, number] {
|
|
513
696
|
return PublicKey.findProgramAddressSync(
|
|
514
697
|
[SEEDS.INSURANCE, market.toBuffer(), assetMint.toBuffer()],
|
|
515
|
-
|
|
698
|
+
programId
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/** Owner- and LP-mint-specific reusable governance balance. */
|
|
703
|
+
export function deriveGovernanceBalanceAddress(market: PublicKey, owner: PublicKey, lpMint: PublicKey, programId: PublicKey = DUSK_PROGRAM_ID): [PublicKey, number] {
|
|
704
|
+
return PublicKey.findProgramAddressSync(
|
|
705
|
+
[Buffer.from("governance_balance"), market.toBuffer(), owner.toBuffer(), lpMint.toBuffer()],
|
|
706
|
+
programId
|
|
516
707
|
);
|
|
517
708
|
}
|
|
709
|
+
|
|
710
|
+
export function deriveGovernanceSupportAddress(proposal: PublicKey, balance: PublicKey, programId: PublicKey = DUSK_PROGRAM_ID): [PublicKey, number] {
|
|
711
|
+
return PublicKey.findProgramAddressSync([Buffer.from("governance_support"), proposal.toBuffer(), balance.toBuffer()], programId);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
export function deriveJuniorMintAddress(market: PublicKey, generation: U64SeedLike, programId: PublicKey = DUSK_PROGRAM_ID): [PublicKey, number] {
|
|
715
|
+
return PublicKey.findProgramAddressSync([Buffer.from("junior_mint"), market.toBuffer(), u64Seed(generation, "generation")], programId);
|
|
716
|
+
}
|
|
717
|
+
export function deriveJuniorCohortAddress(market: PublicKey, mint: PublicKey, programId: PublicKey = DUSK_PROGRAM_ID): [PublicKey, number] {
|
|
718
|
+
return PublicKey.findProgramAddressSync([Buffer.from("junior_cohort"), market.toBuffer(), mint.toBuffer()], programId);
|
|
719
|
+
}
|
|
720
|
+
export function deriveJuniorExitAddress(market: PublicKey, owner: PublicKey, mint: PublicKey, programId: PublicKey = DUSK_PROGRAM_ID): [PublicKey, number] {
|
|
721
|
+
return PublicKey.findProgramAddressSync([Buffer.from("junior_exit"), market.toBuffer(), owner.toBuffer(), mint.toBuffer()], programId);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
export function deriveIncomeEpochAddress(market: PublicKey, bookIndex: number, epoch: U64SeedLike, programId: PublicKey = DUSK_PROGRAM_ID): [PublicKey, number] {
|
|
725
|
+
if (!Number.isInteger(bookIndex) || bookIndex < 0 || bookIndex > 3) throw new Error("bookIndex must be 0–3");
|
|
726
|
+
return PublicKey.findProgramAddressSync([Buffer.from("income_epoch"), market.toBuffer(), Buffer.from([bookIndex]), u64Seed(epoch, "epoch")], programId);
|
|
727
|
+
}
|
package/src/delegate.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { AnchorProvider, Program } from "@coral-xyz/anchor";
|
|
2
|
+
import {
|
|
3
|
+
PublicKey,
|
|
4
|
+
SystemProgram,
|
|
5
|
+
Transaction,
|
|
6
|
+
type AccountMeta,
|
|
7
|
+
type TransactionInstruction,
|
|
8
|
+
} from "@solana/web3.js";
|
|
9
|
+
|
|
10
|
+
import { address, type AddressLike } from "./address.js";
|
|
11
|
+
import { deriveLeveragePositionAddress } from "./constants.js";
|
|
12
|
+
import { toBN } from "./governance.js";
|
|
13
|
+
import DELEGATE_IDL from "./idl_delegate.js";
|
|
14
|
+
import type { LeverageDelegate } from "./types_delegate.js";
|
|
15
|
+
import { getAssociatedTokenAddressSync, TOKEN_PROGRAM_ID } from "@solana/spl-token";
|
|
16
|
+
import { governanceIntegerBN } from "./governance.js";
|
|
17
|
+
import type { RawAmount } from "./write.js";
|
|
18
|
+
|
|
19
|
+
/** Hard-coded additional fee on successful built-in order execution. */
|
|
20
|
+
export const ORDER_PROTOCOL_FEE_BPS = 10n;
|
|
21
|
+
|
|
22
|
+
/** Net protocol fee in raw units, rounded up. Token-2022 transfer fees are additional. */
|
|
23
|
+
export function calculateOrderProtocolFee(executedValue: RawAmount): bigint {
|
|
24
|
+
const value = BigInt(governanceIntegerBN(executedValue, "executedValue").toString());
|
|
25
|
+
if (value < 0n || value > 0xffffffffffffffffn) throw new RangeError("executedValue must fit u64");
|
|
26
|
+
return (value * ORDER_PROTOCOL_FEE_BPS + 9_999n) / 10_000n;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Supply under `protocolFee` when executing any built-in order. Create this
|
|
30
|
+
* ATA if needed. Treasury comes from Dusk's canonical FutarchyAuthority.
|
|
31
|
+
*/
|
|
32
|
+
export function orderProtocolFeeAccounts(treasury: PublicKey, mint: PublicKey,
|
|
33
|
+
tokenProgram = TOKEN_PROGRAM_ID) {
|
|
34
|
+
return { feeRecipient: getAssociatedTokenAddressSync(mint, treasury, true, tokenProgram) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const LEVERAGE_DELEGATE_PROGRAM_ID = address(
|
|
38
|
+
"3ChYvxNwJfr49PznzYKPmXLFUw5P2vUQZmbqqmyFJCYi"
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
export type LeverageDelegateProgram = Program<LeverageDelegate>;
|
|
42
|
+
|
|
43
|
+
export function leverageDelegateIdl(
|
|
44
|
+
programId: AddressLike = LEVERAGE_DELEGATE_PROGRAM_ID
|
|
45
|
+
): LeverageDelegate {
|
|
46
|
+
const idl = JSON.parse(JSON.stringify(DELEGATE_IDL)) as LeverageDelegate;
|
|
47
|
+
idl.address = address(programId).toBase58() as LeverageDelegate["address"];
|
|
48
|
+
return idl;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createLeverageDelegateProgram(options: {
|
|
52
|
+
provider: AnchorProvider;
|
|
53
|
+
programId?: AddressLike;
|
|
54
|
+
}): LeverageDelegateProgram {
|
|
55
|
+
return new Program<LeverageDelegate>(
|
|
56
|
+
leverageDelegateIdl(options.programId),
|
|
57
|
+
options.provider
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Conditional order kinds.
|
|
63
|
+
*
|
|
64
|
+
* The program stores `kind` as an opaque u8 and does not define an enum for
|
|
65
|
+
* it, so the meaning is a caller convention. These are the values the protocol
|
|
66
|
+
* already uses; changing them silently reinterprets every existing order.
|
|
67
|
+
*/
|
|
68
|
+
export const LEVERAGE_ORDER_KIND = {
|
|
69
|
+
takeProfit: 1,
|
|
70
|
+
stopLoss: 2,
|
|
71
|
+
} as const;
|
|
72
|
+
|
|
73
|
+
export type LeverageOrderKind = keyof typeof LEVERAGE_ORDER_KIND;
|
|
74
|
+
|
|
75
|
+
const ORDER_SEED = Buffer.from("leverage_order");
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A conditional order's address.
|
|
79
|
+
*
|
|
80
|
+
* Keyed by position, owner and order id, so one position can carry several
|
|
81
|
+
* orders and two owners cannot collide on the same id.
|
|
82
|
+
*/
|
|
83
|
+
export function deriveLeverageOrderAddress(
|
|
84
|
+
leveragePosition: PublicKey,
|
|
85
|
+
owner: PublicKey,
|
|
86
|
+
orderId: bigint | number | string,
|
|
87
|
+
programId: PublicKey = LEVERAGE_DELEGATE_PROGRAM_ID
|
|
88
|
+
): [PublicKey, number] {
|
|
89
|
+
const id = Buffer.alloc(8);
|
|
90
|
+
id.writeBigUInt64LE(BigInt(orderId));
|
|
91
|
+
return PublicKey.findProgramAddressSync(
|
|
92
|
+
[ORDER_SEED, leveragePosition.toBuffer(), owner.toBuffer(), id],
|
|
93
|
+
programId
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface CreateLeverageOrderParams {
|
|
98
|
+
market: AddressLike;
|
|
99
|
+
owner: AddressLike;
|
|
100
|
+
namespaceAuthority?: AddressLike;
|
|
101
|
+
/** Identifies the leverage position the order acts on. */
|
|
102
|
+
positionId: AddressLike;
|
|
103
|
+
orderId: bigint | number | string;
|
|
104
|
+
kind: LeverageOrderKind;
|
|
105
|
+
/** Trigger price in NAD; the order closes out when it is crossed. */
|
|
106
|
+
triggerCloseoutPriceNad: bigint | number | string;
|
|
107
|
+
/** How much of the position to close, in basis points. */
|
|
108
|
+
closeBps: number;
|
|
109
|
+
leveragePosition?: AddressLike;
|
|
110
|
+
order?: AddressLike;
|
|
111
|
+
remainingAccounts?: AccountMeta[];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface CancelLeverageOrderParams {
|
|
115
|
+
owner: AddressLike;
|
|
116
|
+
namespaceAuthority?: AddressLike;
|
|
117
|
+
orderId: bigint | number | string;
|
|
118
|
+
/** Identifies the leverage position the order acts on. */
|
|
119
|
+
positionId?: AddressLike;
|
|
120
|
+
/** Required when `positionId` is not given. */
|
|
121
|
+
market?: AddressLike;
|
|
122
|
+
leveragePosition?: AddressLike;
|
|
123
|
+
/** Supply the order address directly to skip derivation entirely. */
|
|
124
|
+
order?: AddressLike;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Conditional orders on leverage positions.
|
|
129
|
+
*
|
|
130
|
+
* These live in the delegate program rather than in Dusk itself, so they need
|
|
131
|
+
* their own client. The position must already have delegated to this program
|
|
132
|
+
* — see `buildCreateLeverageDelegationInstruction` — or the order has no
|
|
133
|
+
* authority to act.
|
|
134
|
+
*/
|
|
135
|
+
export class DuskLeverageOrders {
|
|
136
|
+
constructor(readonly program: LeverageDelegateProgram) {}
|
|
137
|
+
|
|
138
|
+
async createOrderInstruction(
|
|
139
|
+
params: CreateLeverageOrderParams
|
|
140
|
+
): Promise<TransactionInstruction> {
|
|
141
|
+
const market = address(params.market);
|
|
142
|
+
const owner = address(params.owner);
|
|
143
|
+
const leveragePosition = address(
|
|
144
|
+
params.leveragePosition ??
|
|
145
|
+
deriveLeveragePositionAddress(market, owner, address(params.positionId), address(params.namespaceAuthority ?? params.owner))[0]
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
if (!Number.isInteger(params.closeBps) || params.closeBps <= 0 || params.closeBps > 10_000) {
|
|
149
|
+
throw new Error("Leverage order closeBps must be between 1 and 10000");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const order = address(
|
|
153
|
+
params.order ??
|
|
154
|
+
deriveLeverageOrderAddress(
|
|
155
|
+
leveragePosition,
|
|
156
|
+
owner,
|
|
157
|
+
params.orderId,
|
|
158
|
+
this.program.programId
|
|
159
|
+
)[0]
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
return this.program.methods
|
|
163
|
+
.createLeverageOrder({
|
|
164
|
+
orderId: toBN(BigInt(params.orderId.toString())),
|
|
165
|
+
kind: LEVERAGE_ORDER_KIND[params.kind],
|
|
166
|
+
triggerCloseoutPriceNad: toBN(
|
|
167
|
+
BigInt(params.triggerCloseoutPriceNad.toString())
|
|
168
|
+
),
|
|
169
|
+
closeBps: params.closeBps,
|
|
170
|
+
} as never)
|
|
171
|
+
.accounts({
|
|
172
|
+
market,
|
|
173
|
+
leveragePosition,
|
|
174
|
+
order,
|
|
175
|
+
owner,
|
|
176
|
+
systemProgram: SystemProgram.programId,
|
|
177
|
+
} as never)
|
|
178
|
+
.remainingAccounts(params.remainingAccounts ?? [])
|
|
179
|
+
.instruction();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async createOrderTransaction(
|
|
183
|
+
params: CreateLeverageOrderParams
|
|
184
|
+
): Promise<Transaction> {
|
|
185
|
+
return new Transaction().add(await this.createOrderInstruction(params));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Cancel a conditional order and return its rent to the owner.
|
|
190
|
+
*
|
|
191
|
+
* The program resolves the order's position from the account itself, so
|
|
192
|
+
* cancelling needs neither the market nor the position — but the address
|
|
193
|
+
* still has to be derived from them unless `order` is supplied.
|
|
194
|
+
*/
|
|
195
|
+
async cancelOrderInstruction(
|
|
196
|
+
params: CancelLeverageOrderParams
|
|
197
|
+
): Promise<TransactionInstruction> {
|
|
198
|
+
const owner = address(params.owner);
|
|
199
|
+
const order = address(params.order ?? this.resolveOrderAddress(params));
|
|
200
|
+
|
|
201
|
+
return this.program.methods
|
|
202
|
+
.cancelLeverageOrder({
|
|
203
|
+
orderId: toBN(BigInt(params.orderId.toString())),
|
|
204
|
+
} as never)
|
|
205
|
+
.accounts({ order, owner } as never)
|
|
206
|
+
.instruction();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async cancelOrderTransaction(
|
|
210
|
+
params: CancelLeverageOrderParams
|
|
211
|
+
): Promise<Transaction> {
|
|
212
|
+
return new Transaction().add(await this.cancelOrderInstruction(params));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private resolveOrderAddress(params: CancelLeverageOrderParams): PublicKey {
|
|
216
|
+
const leveragePosition = params.leveragePosition
|
|
217
|
+
? address(params.leveragePosition)
|
|
218
|
+
: (() => {
|
|
219
|
+
if (!params.market || !params.positionId) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
"Cancelling an order needs `order`, or `leveragePosition`, or both `market` and `positionId`"
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
return deriveLeveragePositionAddress(
|
|
225
|
+
address(params.market),
|
|
226
|
+
address(params.owner),
|
|
227
|
+
address(params.positionId),
|
|
228
|
+
address(params.namespaceAuthority ?? params.owner)
|
|
229
|
+
)[0];
|
|
230
|
+
})();
|
|
231
|
+
return deriveLeverageOrderAddress(
|
|
232
|
+
leveragePosition,
|
|
233
|
+
address(params.owner),
|
|
234
|
+
params.orderId,
|
|
235
|
+
this.program.programId
|
|
236
|
+
)[0];
|
|
237
|
+
}
|
|
238
|
+
}
|