@omnipair/dusk-sdk 2.0.1 → 2.16.0-next.20261102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/README.md +1085 -26
  2. package/dist/amounts.d.ts +9 -0
  3. package/dist/amounts.d.ts.map +1 -0
  4. package/dist/amounts.js +10 -0
  5. package/dist/amounts.js.map +1 -0
  6. package/dist/constants.d.ts +80 -33
  7. package/dist/constants.d.ts.map +1 -1
  8. package/dist/constants.js +175 -64
  9. package/dist/constants.js.map +1 -1
  10. package/dist/delegate.d.ts +94 -0
  11. package/dist/delegate.d.ts.map +1 -0
  12. package/dist/delegate.js +131 -0
  13. package/dist/delegate.js.map +1 -0
  14. package/dist/dusk.d.ts +14 -0
  15. package/dist/dusk.d.ts.map +1 -1
  16. package/dist/dusk.js +22 -0
  17. package/dist/dusk.js.map +1 -1
  18. package/dist/get.d.ts +3285 -237
  19. package/dist/get.d.ts.map +1 -1
  20. package/dist/get.js +140 -71
  21. package/dist/get.js.map +1 -1
  22. package/dist/governance-client.d.ts +169 -0
  23. package/dist/governance-client.d.ts.map +1 -0
  24. package/dist/governance-client.js +146 -0
  25. package/dist/governance-client.js.map +1 -0
  26. package/dist/governance.d.ts +313 -0
  27. package/dist/governance.d.ts.map +1 -0
  28. package/dist/governance.js +902 -0
  29. package/dist/governance.js.map +1 -0
  30. package/dist/hash.d.ts +9 -0
  31. package/dist/hash.d.ts.map +1 -0
  32. package/dist/hash.js +16 -0
  33. package/dist/hash.js.map +1 -0
  34. package/dist/hlp-liquidation-client.d.ts +106 -0
  35. package/dist/hlp-liquidation-client.d.ts.map +1 -0
  36. package/dist/hlp-liquidation-client.js +150 -0
  37. package/dist/hlp-liquidation-client.js.map +1 -0
  38. package/dist/idl_delegate.d.ts +3 -0
  39. package/dist/idl_delegate.d.ts.map +1 -0
  40. package/dist/idl_delegate.js +3 -0
  41. package/dist/idl_delegate.js.map +1 -0
  42. package/dist/idl_delegate.json +6119 -0
  43. package/dist/idl_v2.json +18372 -6121
  44. package/dist/index.d.ts +23 -0
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +23 -0
  47. package/dist/index.js.map +1 -1
  48. package/dist/interest-accounting.d.ts +49 -0
  49. package/dist/interest-accounting.d.ts.map +1 -0
  50. package/dist/interest-accounting.js +72 -0
  51. package/dist/interest-accounting.js.map +1 -0
  52. package/dist/leverage-actions.d.ts +122 -0
  53. package/dist/leverage-actions.d.ts.map +1 -0
  54. package/dist/leverage-actions.js +228 -0
  55. package/dist/leverage-actions.js.map +1 -0
  56. package/dist/leverage-margins.d.ts +12 -0
  57. package/dist/leverage-margins.d.ts.map +1 -0
  58. package/dist/leverage-margins.js +45 -0
  59. package/dist/leverage-margins.js.map +1 -0
  60. package/dist/leverage-preview.d.ts +587 -0
  61. package/dist/leverage-preview.d.ts.map +1 -0
  62. package/dist/leverage-preview.js +89 -0
  63. package/dist/leverage-preview.js.map +1 -0
  64. package/dist/liquidation-client.d.ts +230 -0
  65. package/dist/liquidation-client.d.ts.map +1 -0
  66. package/dist/liquidation-client.js +239 -0
  67. package/dist/liquidation-client.js.map +1 -0
  68. package/dist/liquidation.d.ts +78 -0
  69. package/dist/liquidation.d.ts.map +1 -0
  70. package/dist/liquidation.js +122 -0
  71. package/dist/liquidation.js.map +1 -0
  72. package/dist/lp-naming.d.ts +43 -0
  73. package/dist/lp-naming.d.ts.map +1 -0
  74. package/dist/lp-naming.js +81 -0
  75. package/dist/lp-naming.js.map +1 -0
  76. package/dist/lp-vanity.d.ts +33 -0
  77. package/dist/lp-vanity.d.ts.map +1 -0
  78. package/dist/lp-vanity.js +94 -0
  79. package/dist/lp-vanity.js.map +1 -0
  80. package/dist/market-bootstrap.d.ts +109 -0
  81. package/dist/market-bootstrap.d.ts.map +1 -0
  82. package/dist/market-bootstrap.js +158 -0
  83. package/dist/market-bootstrap.js.map +1 -0
  84. package/dist/market-launch.d.ts +45 -0
  85. package/dist/market-launch.d.ts.map +1 -0
  86. package/dist/market-launch.js +76 -0
  87. package/dist/market-launch.js.map +1 -0
  88. package/dist/oracle.d.ts +46 -0
  89. package/dist/oracle.d.ts.map +1 -0
  90. package/dist/oracle.js +103 -0
  91. package/dist/oracle.js.map +1 -0
  92. package/dist/preview.d.ts +39 -1
  93. package/dist/preview.d.ts.map +1 -1
  94. package/dist/preview.js +12 -0
  95. package/dist/preview.js.map +1 -1
  96. package/dist/protection.d.ts +48 -0
  97. package/dist/protection.d.ts.map +1 -0
  98. package/dist/protection.js +88 -0
  99. package/dist/protection.js.map +1 -0
  100. package/dist/referral.d.ts +28 -28
  101. package/dist/referral.d.ts.map +1 -1
  102. package/dist/referral.js +41 -33
  103. package/dist/referral.js.map +1 -1
  104. package/dist/risk-pricing.d.ts +14 -0
  105. package/dist/risk-pricing.d.ts.map +1 -0
  106. package/dist/risk-pricing.js +25 -0
  107. package/dist/risk-pricing.js.map +1 -0
  108. package/dist/simulation.d.ts +45 -0
  109. package/dist/simulation.d.ts.map +1 -0
  110. package/dist/simulation.js +106 -0
  111. package/dist/simulation.js.map +1 -0
  112. package/dist/tranche-client.d.ts +121 -0
  113. package/dist/tranche-client.d.ts.map +1 -0
  114. package/dist/tranche-client.js +185 -0
  115. package/dist/tranche-client.js.map +1 -0
  116. package/dist/transfer-fees.d.ts +46 -0
  117. package/dist/transfer-fees.d.ts.map +1 -0
  118. package/dist/transfer-fees.js +76 -0
  119. package/dist/transfer-fees.js.map +1 -0
  120. package/dist/type-aliases.d.ts +271 -30
  121. package/dist/type-aliases.d.ts.map +1 -1
  122. package/dist/types_delegate.d.ts +6126 -0
  123. package/dist/types_delegate.d.ts.map +1 -0
  124. package/dist/types_delegate.js +2 -0
  125. package/dist/types_delegate.js.map +1 -0
  126. package/dist/types_v2.d.ts +18105 -5854
  127. package/dist/types_v2.d.ts.map +1 -1
  128. package/dist/virtual-book/index.d.ts +4 -0
  129. package/dist/virtual-book/index.d.ts.map +1 -0
  130. package/dist/virtual-book/index.js +4 -0
  131. package/dist/virtual-book/index.js.map +1 -0
  132. package/dist/virtual-book/market.d.ts +608 -0
  133. package/dist/virtual-book/market.d.ts.map +1 -0
  134. package/dist/virtual-book/market.js +59 -0
  135. package/dist/virtual-book/market.js.map +1 -0
  136. package/dist/virtual-book/projection.d.ts +44 -0
  137. package/dist/virtual-book/projection.d.ts.map +1 -0
  138. package/dist/virtual-book/projection.js +229 -0
  139. package/dist/virtual-book/projection.js.map +1 -0
  140. package/dist/virtual-book/quotes.d.ts +1241 -0
  141. package/dist/virtual-book/quotes.d.ts.map +1 -0
  142. package/dist/virtual-book/quotes.js +233 -0
  143. package/dist/virtual-book/quotes.js.map +1 -0
  144. package/dist/write.d.ts +584 -14
  145. package/dist/write.d.ts.map +1 -1
  146. package/dist/write.js +1371 -47
  147. package/dist/write.js.map +1 -1
  148. package/dist/yield-client.d.ts +51 -0
  149. package/dist/yield-client.d.ts.map +1 -0
  150. package/dist/yield-client.js +98 -0
  151. package/dist/yield-client.js.map +1 -0
  152. package/package.json +9 -2
  153. package/src/amounts.ts +13 -0
  154. package/src/constants.ts +276 -111
  155. package/src/delegate.ts +238 -0
  156. package/src/dusk.ts +28 -0
  157. package/src/get.ts +267 -90
  158. package/src/governance-client.ts +234 -0
  159. package/src/governance.ts +1422 -0
  160. package/src/hash.ts +15 -0
  161. package/src/hlp-liquidation-client.ts +195 -0
  162. package/src/idl_delegate.json +6119 -0
  163. package/src/idl_delegate.ts +3 -0
  164. package/src/idl_v2.json +18372 -6121
  165. package/src/index.ts +31 -0
  166. package/src/interest-accounting.ts +85 -0
  167. package/src/leverage-actions.ts +216 -0
  168. package/src/leverage-margins.ts +48 -0
  169. package/src/leverage-preview.ts +86 -0
  170. package/src/liquidation-client.ts +297 -0
  171. package/src/liquidation.ts +137 -0
  172. package/src/lp-naming.ts +127 -0
  173. package/src/lp-vanity.ts +135 -0
  174. package/src/market-bootstrap.ts +273 -0
  175. package/src/market-launch.ts +103 -0
  176. package/src/oracle.ts +120 -0
  177. package/src/preview.ts +53 -1
  178. package/src/protection.ts +112 -0
  179. package/src/referral.ts +71 -71
  180. package/src/risk-pricing.ts +28 -0
  181. package/src/simulation.ts +169 -0
  182. package/src/tranche-client.ts +187 -0
  183. package/src/transfer-fees.ts +99 -0
  184. package/src/type-aliases.ts +280 -30
  185. package/src/types_delegate.ts +6125 -0
  186. package/src/types_v2.ts +18372 -6121
  187. package/src/virtual-book/index.ts +3 -0
  188. package/src/virtual-book/market.ts +76 -0
  189. package/src/virtual-book/projection.ts +265 -0
  190. package/src/virtual-book/quotes.ts +308 -0
  191. package/src/write.ts +2394 -67
  192. package/src/yield-client.ts +117 -0
@@ -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
+ }
package/src/dusk.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { DuskTranches } from "./tranche-client.js";
1
2
  import type { AnchorProvider, Program } from "@coral-xyz/anchor";
2
3
  import type { Connection } from "@solana/web3.js";
3
4
 
@@ -6,7 +7,15 @@ import { DuskGet } from "./get.js";
6
7
  import { DuskIndexerClient, type FetchLike } from "./indexer.js";
7
8
  import { createDuskProgram } from "./program.js";
8
9
  import type { Dusk as DuskIdl } from "./types_v2.js";
10
+ import { DuskGovernance } from "./governance-client.js";
9
11
  import { DuskWrite } from "./write.js";
12
+ import { DuskHlpLiquidations } from "./hlp-liquidation-client.js";
13
+ import { DuskYield } from "./yield-client.js";
14
+ import { DuskLiquidations } from "./liquidation-client.js";
15
+ import {
16
+ createLeverageDelegateProgram,
17
+ DuskLeverageOrders,
18
+ } from "./delegate.js";
10
19
 
11
20
  export interface DuskOptions {
12
21
  program?: Program<DuskIdl>;
@@ -16,13 +25,21 @@ export interface DuskOptions {
16
25
  feePayer?: AddressLike;
17
26
  indexerBaseUrl?: string;
18
27
  fetch?: FetchLike;
28
+ leverageDelegateProgramId?: AddressLike;
19
29
  }
20
30
 
21
31
  export class Dusk {
22
32
  readonly program: Program<DuskIdl>;
23
33
  readonly get: DuskGet;
24
34
  readonly write: DuskWrite;
35
+ readonly governance: DuskGovernance;
36
+ readonly liquidations: DuskLiquidations;
37
+ readonly yield: DuskYield;
38
+ readonly tranches: DuskTranches;
39
+ readonly hlpLiquidations: DuskHlpLiquidations;
25
40
  readonly fetch: DuskIndexerClient;
41
+ /** Conditional orders, which live in the delegate program. */
42
+ readonly orders: DuskLeverageOrders;
26
43
 
27
44
  constructor(options: DuskOptions = {}) {
28
45
  this.program =
@@ -35,9 +52,20 @@ export class Dusk {
35
52
 
36
53
  this.get = new DuskGet(this.program, options.feePayer);
37
54
  this.write = new DuskWrite(this.program);
55
+ this.governance = new DuskGovernance(this.program);
56
+ this.liquidations = new DuskLiquidations(this.program, options.feePayer);
57
+ this.hlpLiquidations = new DuskHlpLiquidations(this.program, options.feePayer);
58
+ this.yield = new DuskYield(this.program, options.feePayer);
59
+ this.tranches = new DuskTranches(this.program);
38
60
  this.fetch = new DuskIndexerClient({
39
61
  baseUrl: options.indexerBaseUrl,
40
62
  fetch: options.fetch,
41
63
  });
64
+ this.orders = new DuskLeverageOrders(
65
+ createLeverageDelegateProgram({
66
+ provider: this.program.provider as AnchorProvider,
67
+ programId: options.leverageDelegateProgramId,
68
+ })
69
+ );
42
70
  }
43
71
  }