@omnipair/dusk-sdk 2.0.0
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 +170 -0
- package/dist/address.d.ts +7 -0
- package/dist/address.d.ts.map +1 -0
- package/dist/address.js +29 -0
- package/dist/address.js.map +1 -0
- package/dist/constants.d.ts +132 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +330 -0
- package/dist/constants.js.map +1 -0
- package/dist/dusk.d.ts +24 -0
- package/dist/dusk.d.ts.map +1 -0
- package/dist/dusk.js +26 -0
- package/dist/dusk.js.map +1 -0
- package/dist/get.d.ts +340 -0
- package/dist/get.d.ts.map +1 -0
- package/dist/get.js +152 -0
- package/dist/get.js.map +1 -0
- package/dist/idl_v2.d.ts +3 -0
- package/dist/idl_v2.d.ts.map +1 -0
- package/dist/idl_v2.js +3 -0
- package/dist/idl_v2.js.map +1 -0
- package/dist/idl_v2.json +12630 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/indexer.d.ts +122 -0
- package/dist/indexer.d.ts.map +1 -0
- package/dist/indexer.js +203 -0
- package/dist/indexer.js.map +1 -0
- package/dist/preview.d.ts +39 -0
- package/dist/preview.d.ts.map +1 -0
- package/dist/preview.js +68 -0
- package/dist/preview.js.map +1 -0
- package/dist/program.d.ts +19 -0
- package/dist/program.d.ts.map +1 -0
- package/dist/program.js +29 -0
- package/dist/program.js.map +1 -0
- package/dist/referral.d.ts +51 -0
- package/dist/referral.d.ts.map +1 -0
- package/dist/referral.js +80 -0
- package/dist/referral.js.map +1 -0
- package/dist/type-aliases.d.ts +32 -0
- package/dist/type-aliases.d.ts.map +1 -0
- package/dist/type-aliases.js +2 -0
- package/dist/type-aliases.js.map +1 -0
- package/dist/types_v2.d.ts +12637 -0
- package/dist/types_v2.d.ts.map +1 -0
- package/dist/types_v2.js +2 -0
- package/dist/types_v2.js.map +1 -0
- package/dist/write.d.ts +61 -0
- package/dist/write.d.ts.map +1 -0
- package/dist/write.js +129 -0
- package/dist/write.js.map +1 -0
- package/package.json +76 -0
- package/src/address.ts +38 -0
- package/src/constants.ts +562 -0
- package/src/dusk.ts +43 -0
- package/src/get.ts +279 -0
- package/src/idl_v2.json +12630 -0
- package/src/idl_v2.ts +3 -0
- package/src/index.ts +18 -0
- package/src/indexer.ts +424 -0
- package/src/preview.ts +139 -0
- package/src/program.ts +54 -0
- package/src/referral.ts +172 -0
- package/src/type-aliases.ts +33 -0
- package/src/types_v2.ts +12636 -0
- package/src/write.ts +219 -0
package/src/get.ts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type { BN, Program } from "@coral-xyz/anchor";
|
|
2
|
+
import {
|
|
3
|
+
PublicKey,
|
|
4
|
+
Transaction,
|
|
5
|
+
type Commitment,
|
|
6
|
+
type TransactionInstruction,
|
|
7
|
+
} from "@solana/web3.js";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
deriveBorrowPositionAddress,
|
|
11
|
+
deriveFutarchyAuthorityAddress,
|
|
12
|
+
deriveHlpYlpVaultAddress,
|
|
13
|
+
deriveInsuranceAddress,
|
|
14
|
+
deriveLeveragePositionAddress,
|
|
15
|
+
deriveMarketAddress,
|
|
16
|
+
deriveMarketCollateralVaultAddress,
|
|
17
|
+
deriveMarketFeeVaultAddress,
|
|
18
|
+
deriveMarketInterestVaultAddress,
|
|
19
|
+
deriveMarketReserveVaultAddress,
|
|
20
|
+
deriveReferralProfileAddress,
|
|
21
|
+
deriveTokenMetadataAddress,
|
|
22
|
+
deriveYieldAccountAddress,
|
|
23
|
+
deriveYieldTransferHookValidationAddress,
|
|
24
|
+
} from "./constants.js";
|
|
25
|
+
import { address, DEFAULT_READONLY_PUBLIC_KEY, normalizeAccountKeys, type AddressLike } from "./address.js";
|
|
26
|
+
import {
|
|
27
|
+
decodePreviewAddLiquidityReturnData,
|
|
28
|
+
decodePreviewBorrowCapacityReturnData,
|
|
29
|
+
decodePreviewBorrowPositionReturnData,
|
|
30
|
+
decodePreviewMarketReturnData,
|
|
31
|
+
decodePreviewSwapReturnData,
|
|
32
|
+
type AddLiquidityPreview,
|
|
33
|
+
type BorrowCapacityPreview,
|
|
34
|
+
type BorrowPositionPreview,
|
|
35
|
+
type MarketPreview,
|
|
36
|
+
type PreviewReturnData,
|
|
37
|
+
type SwapPreview,
|
|
38
|
+
} from "./preview.js";
|
|
39
|
+
import type {
|
|
40
|
+
BorrowPosition,
|
|
41
|
+
FutarchyAuthority,
|
|
42
|
+
LeverageDelegation,
|
|
43
|
+
LeveragePosition,
|
|
44
|
+
Market,
|
|
45
|
+
YieldAccount,
|
|
46
|
+
} from "./type-aliases.js";
|
|
47
|
+
import type { Dusk } from "./types_v2.js";
|
|
48
|
+
|
|
49
|
+
export const pda = {
|
|
50
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress,
|
|
51
|
+
market: deriveMarketAddress,
|
|
52
|
+
tokenMetadata: deriveTokenMetadataAddress,
|
|
53
|
+
marketReserveVault: deriveMarketReserveVaultAddress,
|
|
54
|
+
marketCollateralVault: deriveMarketCollateralVaultAddress,
|
|
55
|
+
marketFeeVault: deriveMarketFeeVaultAddress,
|
|
56
|
+
marketInterestVault: deriveMarketInterestVaultAddress,
|
|
57
|
+
borrowPosition: deriveBorrowPositionAddress,
|
|
58
|
+
leveragePosition: deriveLeveragePositionAddress,
|
|
59
|
+
yieldAccount: deriveYieldAccountAddress,
|
|
60
|
+
yieldTransferHookValidation: deriveYieldTransferHookValidationAddress,
|
|
61
|
+
hlpYlpVault: deriveHlpYlpVaultAddress,
|
|
62
|
+
insurance: deriveInsuranceAddress,
|
|
63
|
+
referralProfile: deriveReferralProfileAddress,
|
|
64
|
+
} as const;
|
|
65
|
+
|
|
66
|
+
export interface SimulateOptions {
|
|
67
|
+
feePayer?: AddressLike;
|
|
68
|
+
commitment?: Commitment;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface PreviewSwapParams extends SimulateOptions {
|
|
72
|
+
market: AddressLike;
|
|
73
|
+
assetInMint: AddressLike;
|
|
74
|
+
assetOutMint: AddressLike;
|
|
75
|
+
exactAssetIn: BN;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface PreviewAddLiquidityParams extends SimulateOptions {
|
|
79
|
+
market: AddressLike;
|
|
80
|
+
baseMint: AddressLike;
|
|
81
|
+
quoteMint: AddressLike;
|
|
82
|
+
baseDepositAmount: BN;
|
|
83
|
+
quoteDepositAmount: BN;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface PreviewBorrowCapacityParams extends SimulateOptions {
|
|
87
|
+
market: AddressLike;
|
|
88
|
+
futarchyAuthority?: AddressLike;
|
|
89
|
+
collateralAssetMint: AddressLike;
|
|
90
|
+
debtAssetMint: AddressLike;
|
|
91
|
+
collateralAmount: BN;
|
|
92
|
+
/**
|
|
93
|
+
* Candidate requested principal used for the returned fee, CF, and health
|
|
94
|
+
* fields. When omitted, the program quotes at the final net capacity.
|
|
95
|
+
*/
|
|
96
|
+
projectedBorrowAmount?: BN | null;
|
|
97
|
+
withReferral?: boolean;
|
|
98
|
+
maxAcceptableReferralFeeBps?: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface PreviewBorrowPositionParams extends SimulateOptions {
|
|
102
|
+
market: AddressLike;
|
|
103
|
+
borrowPosition: AddressLike;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class DuskSimulationError extends Error {
|
|
107
|
+
constructor(
|
|
108
|
+
message: string,
|
|
109
|
+
readonly simulation: Awaited<ReturnType<Program<Dusk>["provider"]["connection"]["simulateTransaction"]>>
|
|
110
|
+
) {
|
|
111
|
+
super(message);
|
|
112
|
+
this.name = "DuskSimulationError";
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export class DuskGet {
|
|
117
|
+
readonly pda = pda;
|
|
118
|
+
|
|
119
|
+
constructor(
|
|
120
|
+
readonly program: Program<Dusk>,
|
|
121
|
+
private readonly defaultFeePayer: AddressLike = DEFAULT_READONLY_PUBLIC_KEY
|
|
122
|
+
) {}
|
|
123
|
+
|
|
124
|
+
async accountInfo(account: AddressLike, commitment?: Commitment) {
|
|
125
|
+
return this.program.provider.connection.getAccountInfo(address(account), commitment);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async programAccount<T = unknown>(name: string, account: AddressLike): Promise<T> {
|
|
129
|
+
const client = (
|
|
130
|
+
this.program.account as unknown as Record<string, { fetch(address: PublicKey): Promise<T> }>
|
|
131
|
+
)[name];
|
|
132
|
+
if (!client) {
|
|
133
|
+
throw new Error(`Unknown Dusk account type: ${name}`);
|
|
134
|
+
}
|
|
135
|
+
return client.fetch(address(account));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
market(account: AddressLike): Promise<Market> {
|
|
139
|
+
return this.program.account.market.fetch(address(account));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
borrowPosition(account: AddressLike): Promise<BorrowPosition> {
|
|
143
|
+
return this.program.account.borrowPosition.fetch(address(account));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
leveragePosition(account: AddressLike): Promise<LeveragePosition> {
|
|
147
|
+
return this.program.account.leveragePosition.fetch(address(account));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
leverageDelegation(account: AddressLike): Promise<LeverageDelegation> {
|
|
151
|
+
return this.program.account.leverageDelegation.fetch(address(account));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
yieldAccount(account: AddressLike): Promise<YieldAccount> {
|
|
155
|
+
return this.program.account.yieldAccount.fetch(address(account));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
futarchyAuthority(account: AddressLike = deriveFutarchyAuthorityAddress()[0]): Promise<FutarchyAuthority> {
|
|
159
|
+
return this.program.account.futarchyAuthority.fetch(address(account));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
allMarkets() {
|
|
163
|
+
return this.program.account.market.all();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
allBorrowPositions() {
|
|
167
|
+
return this.program.account.borrowPosition.all();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
allLeveragePositions() {
|
|
171
|
+
return this.program.account.leveragePosition.all();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async previewMarket(market: AddressLike, options: SimulateOptions = {}): Promise<MarketPreview> {
|
|
175
|
+
const instruction = await this.program.methods
|
|
176
|
+
.previewMarket()
|
|
177
|
+
.accounts(normalizeAccountKeys({ market }))
|
|
178
|
+
.instruction();
|
|
179
|
+
|
|
180
|
+
return decodePreviewMarketReturnData(await this.simulateReturnData(instruction, options));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async previewAddLiquidity(params: PreviewAddLiquidityParams): Promise<AddLiquidityPreview> {
|
|
184
|
+
const instruction = await this.program.methods
|
|
185
|
+
.previewAddLiquidity({
|
|
186
|
+
baseDepositAmount: params.baseDepositAmount,
|
|
187
|
+
quoteDepositAmount: params.quoteDepositAmount,
|
|
188
|
+
})
|
|
189
|
+
.accounts(
|
|
190
|
+
normalizeAccountKeys({
|
|
191
|
+
market: params.market,
|
|
192
|
+
baseMint: params.baseMint,
|
|
193
|
+
quoteMint: params.quoteMint,
|
|
194
|
+
})
|
|
195
|
+
)
|
|
196
|
+
.instruction();
|
|
197
|
+
|
|
198
|
+
return decodePreviewAddLiquidityReturnData(
|
|
199
|
+
await this.simulateReturnData(instruction, params)
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async previewSwap(params: PreviewSwapParams): Promise<SwapPreview> {
|
|
204
|
+
const instruction = await this.program.methods
|
|
205
|
+
.previewSwap({
|
|
206
|
+
exactAssetIn: params.exactAssetIn,
|
|
207
|
+
})
|
|
208
|
+
.accounts(
|
|
209
|
+
normalizeAccountKeys({
|
|
210
|
+
market: params.market,
|
|
211
|
+
assetInMint: params.assetInMint,
|
|
212
|
+
assetOutMint: params.assetOutMint,
|
|
213
|
+
})
|
|
214
|
+
)
|
|
215
|
+
.instruction();
|
|
216
|
+
|
|
217
|
+
return decodePreviewSwapReturnData(await this.simulateReturnData(instruction, params));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async previewBorrowCapacity(
|
|
221
|
+
params: PreviewBorrowCapacityParams
|
|
222
|
+
): Promise<BorrowCapacityPreview> {
|
|
223
|
+
const instruction = await this.program.methods
|
|
224
|
+
.previewBorrowCapacity({
|
|
225
|
+
collateralAmount: params.collateralAmount,
|
|
226
|
+
projectedBorrowAmount: params.projectedBorrowAmount ?? null,
|
|
227
|
+
withReferral: params.withReferral ?? false,
|
|
228
|
+
maxAcceptableReferralFeeBps: params.maxAcceptableReferralFeeBps ?? 0,
|
|
229
|
+
})
|
|
230
|
+
.accounts(
|
|
231
|
+
normalizeAccountKeys({
|
|
232
|
+
market: params.market,
|
|
233
|
+
futarchyAuthority: params.futarchyAuthority ?? deriveFutarchyAuthorityAddress()[0],
|
|
234
|
+
collateralAssetMint: params.collateralAssetMint,
|
|
235
|
+
debtAssetMint: params.debtAssetMint,
|
|
236
|
+
})
|
|
237
|
+
)
|
|
238
|
+
.instruction();
|
|
239
|
+
|
|
240
|
+
return decodePreviewBorrowCapacityReturnData(await this.simulateReturnData(instruction, params));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async previewBorrowPosition(params: PreviewBorrowPositionParams): Promise<BorrowPositionPreview> {
|
|
244
|
+
const instruction = await this.program.methods
|
|
245
|
+
.previewBorrowPosition()
|
|
246
|
+
.accounts(
|
|
247
|
+
normalizeAccountKeys({
|
|
248
|
+
market: params.market,
|
|
249
|
+
borrowPosition: params.borrowPosition,
|
|
250
|
+
})
|
|
251
|
+
)
|
|
252
|
+
.instruction();
|
|
253
|
+
|
|
254
|
+
return decodePreviewBorrowPositionReturnData(await this.simulateReturnData(instruction, params));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async simulateReturnData(
|
|
258
|
+
instruction: TransactionInstruction,
|
|
259
|
+
options: SimulateOptions = {}
|
|
260
|
+
): Promise<PreviewReturnData> {
|
|
261
|
+
const tx = new Transaction().add(instruction);
|
|
262
|
+
tx.feePayer = address(options.feePayer ?? this.program.provider.publicKey ?? this.defaultFeePayer);
|
|
263
|
+
tx.recentBlockhash = (
|
|
264
|
+
await this.program.provider.connection.getLatestBlockhash(options.commitment)
|
|
265
|
+
).blockhash;
|
|
266
|
+
|
|
267
|
+
const simulation = await this.program.provider.connection.simulateTransaction(tx);
|
|
268
|
+
if (simulation.value.err) {
|
|
269
|
+
throw new DuskSimulationError("Dusk simulation failed", simulation);
|
|
270
|
+
}
|
|
271
|
+
if (!simulation.value.returnData) {
|
|
272
|
+
throw new DuskSimulationError("Dusk simulation did not return data", simulation);
|
|
273
|
+
}
|
|
274
|
+
if (simulation.value.returnData.programId !== this.program.programId.toBase58()) {
|
|
275
|
+
throw new DuskSimulationError("Dusk simulation returned data from a different program", simulation);
|
|
276
|
+
}
|
|
277
|
+
return simulation.value.returnData;
|
|
278
|
+
}
|
|
279
|
+
}
|