@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/idl_v2.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Re-export Dusk IDL
|
|
2
|
+
export { default as IDL } from "./idl_v2.js";
|
|
3
|
+
export type { Dusk as DuskIdl } from "./types_v2.js";
|
|
4
|
+
|
|
5
|
+
// Re-export types
|
|
6
|
+
export * from "./type-aliases.js";
|
|
7
|
+
|
|
8
|
+
// Re-export constants and utilities
|
|
9
|
+
export * from "./address.js";
|
|
10
|
+
export * from "./constants.js";
|
|
11
|
+
export { Dusk } from "./dusk.js";
|
|
12
|
+
export type { DuskOptions } from "./dusk.js";
|
|
13
|
+
export * from "./get.js";
|
|
14
|
+
export * from "./indexer.js";
|
|
15
|
+
export * from "./preview.js";
|
|
16
|
+
export * from "./program.js";
|
|
17
|
+
export * from "./referral.js";
|
|
18
|
+
export * from "./write.js";
|
package/src/indexer.ts
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { addressString, type AddressLike } from "./address.js";
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_INDEXER_BASE_URL = "https://api.indexer.omnipair.fi/api/v1";
|
|
4
|
+
|
|
5
|
+
export type FetchLike = typeof fetch;
|
|
6
|
+
export type ActivityCategory = "swaps" | "liquidity" | "lending";
|
|
7
|
+
export type ActivitySort = "recent" | "oldest";
|
|
8
|
+
export type HistoryRange = "1H" | "2H" | "4H" | "12H" | "24H" | "7D" | "30D";
|
|
9
|
+
export type PortfolioRange = "7D" | "30D" | "90D" | "ALL";
|
|
10
|
+
export type Visibility = "visible" | "all";
|
|
11
|
+
|
|
12
|
+
export interface ApiResponse<T = unknown> {
|
|
13
|
+
success: boolean;
|
|
14
|
+
data?: T;
|
|
15
|
+
error?: string;
|
|
16
|
+
message?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RequestOptions {
|
|
20
|
+
params?: Record<string, string | number | boolean | undefined | null>;
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
headers?: HeadersInit;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PaginationOptions {
|
|
26
|
+
limit?: number;
|
|
27
|
+
offset?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface PoolListOptions extends PaginationOptions {
|
|
31
|
+
token0?: AddressLike;
|
|
32
|
+
token1?: AddressLike;
|
|
33
|
+
sortBy?: string;
|
|
34
|
+
sortOrder?: "asc" | "desc" | string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface WindowHoursOptions {
|
|
38
|
+
windowHours?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface CandleOptions {
|
|
42
|
+
resolution: number;
|
|
43
|
+
from: number;
|
|
44
|
+
to: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ActivityOptions extends PaginationOptions {
|
|
48
|
+
categories?: ActivityCategory[];
|
|
49
|
+
poolAddress?: AddressLike;
|
|
50
|
+
sort?: ActivitySort;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface PositionOptions extends PaginationOptions {
|
|
54
|
+
userAddress?: AddressLike;
|
|
55
|
+
poolAddress?: AddressLike;
|
|
56
|
+
type?: "liquidity" | "lending" | "borrow" | "long" | "short" | "all" | string;
|
|
57
|
+
status?: "open" | "closed" | "all" | string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class DuskIndexerError extends Error {
|
|
61
|
+
constructor(
|
|
62
|
+
message: string,
|
|
63
|
+
readonly status: number,
|
|
64
|
+
readonly body: unknown
|
|
65
|
+
) {
|
|
66
|
+
super(message);
|
|
67
|
+
this.name = "DuskIndexerError";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class DuskIndexerClient {
|
|
72
|
+
readonly baseUrl: string;
|
|
73
|
+
private readonly fetchImpl: FetchLike;
|
|
74
|
+
|
|
75
|
+
constructor(options: { baseUrl?: string; fetch?: FetchLike } = {}) {
|
|
76
|
+
this.baseUrl = normalizeBaseUrl(options.baseUrl ?? DEFAULT_INDEXER_BASE_URL);
|
|
77
|
+
this.fetchImpl = options.fetch ?? globalThis.fetch;
|
|
78
|
+
if (!this.fetchImpl) {
|
|
79
|
+
throw new Error("Dusk indexer client requires a fetch implementation.");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async request<T = unknown>(path: string, options: RequestOptions = {}): Promise<T> {
|
|
84
|
+
const response = await this.fetchImpl(this.url(path, options.params), {
|
|
85
|
+
headers: options.headers,
|
|
86
|
+
signal: options.signal,
|
|
87
|
+
});
|
|
88
|
+
const body = await readResponseBody(response);
|
|
89
|
+
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
throw new DuskIndexerError(
|
|
92
|
+
`Dusk indexer request failed with HTTP ${response.status}`,
|
|
93
|
+
response.status,
|
|
94
|
+
body
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return body as T;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
url(path: string, params?: RequestOptions["params"]): string {
|
|
102
|
+
const url = new URL(path.replace(/^\/+/, ""), `${this.baseUrl}/`);
|
|
103
|
+
for (const [key, value] of Object.entries(params ?? {})) {
|
|
104
|
+
if (value !== undefined && value !== null) {
|
|
105
|
+
url.searchParams.set(key, String(value));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return url.toString();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
stats<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
112
|
+
return this.request("/stats", options);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
volumeChart<T = unknown>(timeframe?: string, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
116
|
+
return this.request("/stats/volume-chart", withParams(options, { timeframe }));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
feesChart<T = unknown>(timeframe?: string, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
120
|
+
return this.request("/stats/fees-chart", withParams(options, { timeframe }));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
interestChart<T = unknown>(timeframe?: string, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
124
|
+
return this.request("/stats/interest-chart", withParams(options, { timeframe }));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
swapCountChart<T = unknown>(timeframe?: string, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
128
|
+
return this.request("/stats/swap-count-chart", withParams(options, { timeframe }));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
pools<T = unknown>(params: PoolListOptions = {}, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
132
|
+
return this.request("/pools", withParams(options, serializeParams(params)));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
pool<T = unknown>(poolAddress: AddressLike, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
136
|
+
return this.request(`/pools/${addressString(poolAddress)}`, options);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
poolTvl<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
140
|
+
return this.request("/pools/tvl", options);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
poolValueBaselines<T = unknown>(
|
|
144
|
+
params: { range?: HistoryRange; visibility?: Visibility } = {},
|
|
145
|
+
options?: RequestOptions
|
|
146
|
+
): Promise<ApiResponse<T>> {
|
|
147
|
+
return this.request("/pools/value-baselines", withParams(options, params));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
pairedTokens<T = unknown>(
|
|
151
|
+
tokenAddress: AddressLike,
|
|
152
|
+
options?: RequestOptions
|
|
153
|
+
): Promise<ApiResponse<T>> {
|
|
154
|
+
return this.request(`/pools/paired-tokens/${addressString(tokenAddress)}`, options);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
poolStats<T = unknown>(
|
|
158
|
+
poolAddress: AddressLike,
|
|
159
|
+
params: WindowHoursOptions = {},
|
|
160
|
+
options?: RequestOptions
|
|
161
|
+
): Promise<ApiResponse<T>> {
|
|
162
|
+
return this.request(`/pools/${addressString(poolAddress)}/stats`, withParams(options, params));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
poolRateHistory<T = unknown>(
|
|
166
|
+
poolAddress: AddressLike,
|
|
167
|
+
params: { range?: HistoryRange } = {},
|
|
168
|
+
options?: RequestOptions
|
|
169
|
+
): Promise<ApiResponse<T>> {
|
|
170
|
+
return this.request(
|
|
171
|
+
`/pools/${addressString(poolAddress)}/rate-history`,
|
|
172
|
+
withParams(options, params)
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
poolVolume<T = unknown>(
|
|
177
|
+
poolAddress: AddressLike,
|
|
178
|
+
params: WindowHoursOptions = {},
|
|
179
|
+
options?: RequestOptions
|
|
180
|
+
): Promise<ApiResponse<T>> {
|
|
181
|
+
return this.request(`/pools/${addressString(poolAddress)}/volume`, withParams(options, params));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
poolFees<T = unknown>(
|
|
185
|
+
poolAddress: AddressLike,
|
|
186
|
+
params: WindowHoursOptions = {},
|
|
187
|
+
options?: RequestOptions
|
|
188
|
+
): Promise<ApiResponse<T>> {
|
|
189
|
+
return this.request(`/pools/${addressString(poolAddress)}/fees`, withParams(options, params));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
poolPriceChart<T = unknown>(
|
|
193
|
+
poolAddress: AddressLike,
|
|
194
|
+
params: WindowHoursOptions = {},
|
|
195
|
+
options?: RequestOptions
|
|
196
|
+
): Promise<ApiResponse<T>> {
|
|
197
|
+
return this.request(
|
|
198
|
+
`/pools/${addressString(poolAddress)}/price-chart`,
|
|
199
|
+
withParams(options, params)
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
poolCandles<T = unknown>(
|
|
204
|
+
poolAddress: AddressLike,
|
|
205
|
+
params: CandleOptions,
|
|
206
|
+
options?: RequestOptions
|
|
207
|
+
): Promise<ApiResponse<T>> {
|
|
208
|
+
return this.request(`/pools/${addressString(poolAddress)}/candles`, withParams(options, params));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
poolSwaps<T = unknown>(
|
|
212
|
+
poolAddress: AddressLike,
|
|
213
|
+
params: PaginationOptions = {},
|
|
214
|
+
options?: RequestOptions
|
|
215
|
+
): Promise<ApiResponse<T>> {
|
|
216
|
+
return this.request(`/pools/${addressString(poolAddress)}/swaps`, withParams(options, params));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
poolActivity<T = unknown>(
|
|
220
|
+
poolAddress: AddressLike,
|
|
221
|
+
params: Omit<ActivityOptions, "poolAddress"> = {},
|
|
222
|
+
options?: RequestOptions
|
|
223
|
+
): Promise<ApiResponse<T>> {
|
|
224
|
+
return this.request(
|
|
225
|
+
`/pools/${addressString(poolAddress)}/activity`,
|
|
226
|
+
withParams(options, serializeActivityParams(params))
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
poolLiquidityEvents<T = unknown>(
|
|
231
|
+
poolAddress: AddressLike,
|
|
232
|
+
params: { userAddress: AddressLike } & PaginationOptions,
|
|
233
|
+
options?: RequestOptions
|
|
234
|
+
): Promise<ApiResponse<T>> {
|
|
235
|
+
return this.request(
|
|
236
|
+
`/pools/${addressString(poolAddress)}/liquidity-events`,
|
|
237
|
+
withParams(options, serializeParams(params))
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
userSwaps<T = unknown>(
|
|
242
|
+
userAddress: AddressLike,
|
|
243
|
+
params: PaginationOptions & { poolAddress?: AddressLike } = {},
|
|
244
|
+
options?: RequestOptions
|
|
245
|
+
): Promise<ApiResponse<T>> {
|
|
246
|
+
return this.request(`/users/${addressString(userAddress)}/swaps`, withParams(options, serializeParams(params)));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
userLiquidityEvents<T = unknown>(
|
|
250
|
+
userAddress: AddressLike,
|
|
251
|
+
params: { poolAddress: AddressLike } & PaginationOptions,
|
|
252
|
+
options?: RequestOptions
|
|
253
|
+
): Promise<ApiResponse<T>> {
|
|
254
|
+
return this.request(
|
|
255
|
+
`/users/${addressString(userAddress)}/liquidity-events`,
|
|
256
|
+
withParams(options, serializeParams(params))
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
userLendingEvents<T = unknown>(
|
|
261
|
+
userAddress: AddressLike,
|
|
262
|
+
params: PaginationOptions & { poolAddress?: AddressLike } = {},
|
|
263
|
+
options?: RequestOptions
|
|
264
|
+
): Promise<ApiResponse<T>> {
|
|
265
|
+
return this.request(
|
|
266
|
+
`/users/${addressString(userAddress)}/lending-events`,
|
|
267
|
+
withParams(options, serializeParams(params))
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
userActivity<T = unknown>(
|
|
272
|
+
userAddress: AddressLike,
|
|
273
|
+
params: ActivityOptions = {},
|
|
274
|
+
options?: RequestOptions
|
|
275
|
+
): Promise<ApiResponse<T>> {
|
|
276
|
+
return this.request(
|
|
277
|
+
`/users/${addressString(userAddress)}/activity`,
|
|
278
|
+
withParams(options, serializeActivityParams(params))
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
userPortfolioSnapshots<T = unknown>(
|
|
283
|
+
userAddress: AddressLike,
|
|
284
|
+
range: PortfolioRange,
|
|
285
|
+
options?: RequestOptions
|
|
286
|
+
): Promise<ApiResponse<T>> {
|
|
287
|
+
return this.request(
|
|
288
|
+
`/users/${addressString(userAddress)}/portfolio-snapshots`,
|
|
289
|
+
withParams(options, { range })
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
userLpEarnings<T = unknown>(
|
|
294
|
+
userAddress: AddressLike,
|
|
295
|
+
params: { range?: PortfolioRange; poolAddress?: AddressLike } = {},
|
|
296
|
+
options?: RequestOptions
|
|
297
|
+
): Promise<ApiResponse<T>> {
|
|
298
|
+
return this.request(
|
|
299
|
+
`/users/${addressString(userAddress)}/lp-earnings`,
|
|
300
|
+
withParams(options, serializeParams(params))
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
userPositions<T = unknown>(
|
|
305
|
+
userAddress: AddressLike,
|
|
306
|
+
params: Omit<PositionOptions, "userAddress"> = {},
|
|
307
|
+
options?: RequestOptions
|
|
308
|
+
): Promise<ApiResponse<T>> {
|
|
309
|
+
return this.request(
|
|
310
|
+
`/users/${addressString(userAddress)}/positions`,
|
|
311
|
+
withParams(options, serializeParams(params))
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
positions<T = unknown>(
|
|
316
|
+
params: PositionOptions = {},
|
|
317
|
+
options?: RequestOptions
|
|
318
|
+
): Promise<ApiResponse<T>> {
|
|
319
|
+
return this.request("/positions", withParams(options, serializeParams(params)));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
liquidityPositions<T = unknown>(
|
|
323
|
+
params: PositionOptions = {},
|
|
324
|
+
options?: RequestOptions
|
|
325
|
+
): Promise<ApiResponse<T>> {
|
|
326
|
+
return this.request("/positions/liquidity", withParams(options, serializeParams(params)));
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
position<T = unknown>(positionId: string, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
330
|
+
return this.request(`/positions/${positionId}`, options);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
coingeckoTickers<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
334
|
+
return this.request("/coingecko/tickers", options);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
geckoLatestBlock<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
338
|
+
return this.request("/gecko/latest-block", options);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
geckoAsset<T = unknown>(id: AddressLike, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
342
|
+
return this.request("/gecko/asset", withParams(options, { id: addressString(id) }));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
geckoPair<T = unknown>(id: AddressLike, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
346
|
+
return this.request("/gecko/pair", withParams(options, { id: addressString(id) }));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
geckoEvents<T = unknown>(
|
|
350
|
+
params: { fromBlock: number; toBlock: number },
|
|
351
|
+
options?: RequestOptions
|
|
352
|
+
): Promise<ApiResponse<T>> {
|
|
353
|
+
return this.request("/gecko/events", withParams(options, params));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
cmcFactory<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
357
|
+
return this.request("/cmc/factory", options);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
cmcSummary<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
361
|
+
return this.request("/cmc/summary", options);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
cmcAssets<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
365
|
+
return this.request("/cmc/assets", options);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
cmcTicker<T = unknown>(options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
369
|
+
return this.request("/cmc/ticker", options);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
cmcTrades<T = unknown>(marketPair: string, options?: RequestOptions): Promise<ApiResponse<T>> {
|
|
373
|
+
return this.request(`/cmc/trades/${encodeURIComponent(marketPair)}`, options);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function normalizeBaseUrl(baseUrl: string): string {
|
|
378
|
+
return baseUrl.replace(/\/+$/, "");
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function withParams(options: RequestOptions | undefined, params?: object): RequestOptions {
|
|
382
|
+
return {
|
|
383
|
+
...options,
|
|
384
|
+
params: {
|
|
385
|
+
...options?.params,
|
|
386
|
+
...serializeParams(params ?? {}),
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function serializeParams(params: object): RequestOptions["params"] {
|
|
392
|
+
return Object.fromEntries(
|
|
393
|
+
Object.entries(params as Record<string, unknown>).map(([key, value]) => [key, serializeParam(value)])
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function serializeActivityParams(params: ActivityOptions): RequestOptions["params"] {
|
|
398
|
+
return {
|
|
399
|
+
...serializeParams(params),
|
|
400
|
+
categories: params.categories?.join(","),
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async function readResponseBody(response: Response): Promise<unknown> {
|
|
405
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
406
|
+
if (contentType.includes("application/json")) {
|
|
407
|
+
return response.json();
|
|
408
|
+
}
|
|
409
|
+
return response.text();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function serializeParam(value: unknown): string | number | boolean | undefined | null {
|
|
413
|
+
if (value === undefined || value === null || typeof value !== "object") {
|
|
414
|
+
return value as string | number | boolean | undefined | null;
|
|
415
|
+
}
|
|
416
|
+
if (Array.isArray(value)) {
|
|
417
|
+
return value.join(",");
|
|
418
|
+
}
|
|
419
|
+
try {
|
|
420
|
+
return addressString(value as AddressLike);
|
|
421
|
+
} catch {
|
|
422
|
+
return String(value);
|
|
423
|
+
}
|
|
424
|
+
}
|
package/src/preview.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { BorshCoder, type Idl, type IdlTypes } from "@coral-xyz/anchor";
|
|
2
|
+
|
|
3
|
+
import IDL from "./idl_v2.js";
|
|
4
|
+
import type { Dusk } from "./types_v2.js";
|
|
5
|
+
|
|
6
|
+
const coder = new BorshCoder(IDL as unknown as Idl);
|
|
7
|
+
|
|
8
|
+
type DuskPreviewTypes = IdlTypes<Dusk>;
|
|
9
|
+
|
|
10
|
+
export type MarketPreview = DuskPreviewTypes["marketPreview"];
|
|
11
|
+
export type AddLiquidityPreview = DuskPreviewTypes["addLiquidityPreview"];
|
|
12
|
+
export type SwapPreview = DuskPreviewTypes["swapPreview"];
|
|
13
|
+
export type BorrowCapacityPreview = DuskPreviewTypes["borrowCapacityPreview"];
|
|
14
|
+
export type BorrowPositionPreview = DuskPreviewTypes["borrowPositionPreview"];
|
|
15
|
+
|
|
16
|
+
export const PREVIEW_RETURN_TYPES = {
|
|
17
|
+
previewMarket: "MarketPreview",
|
|
18
|
+
previewAddLiquidity: "AddLiquidityPreview",
|
|
19
|
+
previewSwap: "SwapPreview",
|
|
20
|
+
previewBorrowCapacity: "BorrowCapacityPreview",
|
|
21
|
+
previewBorrowPosition: "BorrowPositionPreview",
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
export type PreviewInstructionName = keyof typeof PREVIEW_RETURN_TYPES;
|
|
25
|
+
export type PreviewReturnTypeName = (typeof PREVIEW_RETURN_TYPES)[PreviewInstructionName];
|
|
26
|
+
|
|
27
|
+
type PreviewReturnByIdlType = {
|
|
28
|
+
MarketPreview: MarketPreview;
|
|
29
|
+
AddLiquidityPreview: AddLiquidityPreview;
|
|
30
|
+
SwapPreview: SwapPreview;
|
|
31
|
+
BorrowCapacityPreview: BorrowCapacityPreview;
|
|
32
|
+
BorrowPositionPreview: BorrowPositionPreview;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type PreviewReturnForType<T extends PreviewReturnTypeName> = PreviewReturnByIdlType[T];
|
|
36
|
+
export type PreviewReturnForInstruction<T extends PreviewInstructionName> =
|
|
37
|
+
PreviewReturnByIdlType[(typeof PREVIEW_RETURN_TYPES)[T]];
|
|
38
|
+
|
|
39
|
+
export type PreviewReturnData =
|
|
40
|
+
| Buffer
|
|
41
|
+
| Uint8Array
|
|
42
|
+
| string
|
|
43
|
+
| readonly [string, BufferEncoding]
|
|
44
|
+
| {
|
|
45
|
+
data:
|
|
46
|
+
| Buffer
|
|
47
|
+
| Uint8Array
|
|
48
|
+
| string
|
|
49
|
+
| readonly [string, BufferEncoding];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function decodePreviewReturnData<T extends PreviewReturnTypeName>(
|
|
53
|
+
typeName: T,
|
|
54
|
+
returnData: PreviewReturnData
|
|
55
|
+
): PreviewReturnForType<T> {
|
|
56
|
+
const decoded = coder.types.decode(typeName, previewReturnDataBytes(returnData));
|
|
57
|
+
return camelizeKeys(decoded) as PreviewReturnForType<T>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function decodePreviewInstructionReturnData<T extends PreviewInstructionName>(
|
|
61
|
+
instructionName: T,
|
|
62
|
+
returnData: PreviewReturnData
|
|
63
|
+
): PreviewReturnForInstruction<T> {
|
|
64
|
+
return decodePreviewReturnData(
|
|
65
|
+
PREVIEW_RETURN_TYPES[instructionName],
|
|
66
|
+
returnData
|
|
67
|
+
) as PreviewReturnForInstruction<T>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function decodePreviewMarketReturnData(returnData: PreviewReturnData): MarketPreview {
|
|
71
|
+
return decodePreviewReturnData("MarketPreview", returnData);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function decodePreviewAddLiquidityReturnData(
|
|
75
|
+
returnData: PreviewReturnData
|
|
76
|
+
): AddLiquidityPreview {
|
|
77
|
+
return decodePreviewReturnData("AddLiquidityPreview", returnData);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function decodePreviewSwapReturnData(returnData: PreviewReturnData): SwapPreview {
|
|
81
|
+
return decodePreviewReturnData("SwapPreview", returnData);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function decodePreviewBorrowCapacityReturnData(
|
|
85
|
+
returnData: PreviewReturnData
|
|
86
|
+
): BorrowCapacityPreview {
|
|
87
|
+
return decodePreviewReturnData("BorrowCapacityPreview", returnData);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function decodePreviewBorrowPositionReturnData(
|
|
91
|
+
returnData: PreviewReturnData
|
|
92
|
+
): BorrowPositionPreview {
|
|
93
|
+
return decodePreviewReturnData("BorrowPositionPreview", returnData);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function previewReturnDataBytes(returnData: PreviewReturnData): Buffer {
|
|
97
|
+
if (returnData instanceof Uint8Array) {
|
|
98
|
+
return Buffer.from(returnData);
|
|
99
|
+
}
|
|
100
|
+
if (typeof returnData === "string") {
|
|
101
|
+
return Buffer.from(returnData, "base64");
|
|
102
|
+
}
|
|
103
|
+
if (isEncodedReturnTuple(returnData)) {
|
|
104
|
+
return Buffer.from(returnData[0], returnData[1]);
|
|
105
|
+
}
|
|
106
|
+
return previewReturnDataBytes(returnData.data);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isEncodedReturnTuple(
|
|
110
|
+
returnData: PreviewReturnData
|
|
111
|
+
): returnData is readonly [string, BufferEncoding] {
|
|
112
|
+
return Array.isArray(returnData);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function camelizeKeys(value: unknown): unknown {
|
|
116
|
+
if (Array.isArray(value)) {
|
|
117
|
+
return value.map(camelizeKeys);
|
|
118
|
+
}
|
|
119
|
+
if (!isPlainRecord(value)) {
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return Object.fromEntries(
|
|
124
|
+
Object.entries(value).map(([key, child]) => [camelizeKey(key), camelizeKeys(child)])
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
129
|
+
if (value === null || typeof value !== "object") {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
const prototype = Object.getPrototypeOf(value);
|
|
133
|
+
return prototype === Object.prototype || prototype === null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function camelizeKey(key: string): string {
|
|
137
|
+
const snakeToCamel = key.replace(/_([a-z0-9])/g, (_, char: string) => char.toUpperCase());
|
|
138
|
+
return snakeToCamel.charAt(0).toLowerCase() + snakeToCamel.slice(1);
|
|
139
|
+
}
|
package/src/program.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { AnchorProvider, Program } from "@coral-xyz/anchor";
|
|
2
|
+
import type { Connection, Transaction, VersionedTransaction } from "@solana/web3.js";
|
|
3
|
+
|
|
4
|
+
import { address, DEFAULT_READONLY_PUBLIC_KEY, type AddressLike } from "./address.js";
|
|
5
|
+
import IDL from "./idl_v2.js";
|
|
6
|
+
import type { Dusk } from "./types_v2.js";
|
|
7
|
+
import { PROGRAM_ID } from "./constants.js";
|
|
8
|
+
|
|
9
|
+
export type DuskProgram = Program<Dusk>;
|
|
10
|
+
|
|
11
|
+
export interface ReadonlyWallet {
|
|
12
|
+
publicKey: ReturnType<typeof address>;
|
|
13
|
+
signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T>;
|
|
14
|
+
signAllTransactions<T extends Transaction | VersionedTransaction>(transactions: T[]): Promise<T[]>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DuskProgramOptions {
|
|
18
|
+
provider?: AnchorProvider;
|
|
19
|
+
connection?: Connection;
|
|
20
|
+
programId?: AddressLike;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function duskIdl(programId: AddressLike = PROGRAM_ID): Dusk {
|
|
24
|
+
const idl = JSON.parse(JSON.stringify(IDL)) as Dusk;
|
|
25
|
+
idl.address = address(programId).toBase58() as Dusk["address"];
|
|
26
|
+
return idl;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createReadonlyProvider(connection: Connection): AnchorProvider {
|
|
30
|
+
const wallet: ReadonlyWallet = {
|
|
31
|
+
publicKey: DEFAULT_READONLY_PUBLIC_KEY,
|
|
32
|
+
async signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T> {
|
|
33
|
+
return transaction;
|
|
34
|
+
},
|
|
35
|
+
async signAllTransactions<T extends Transaction | VersionedTransaction>(
|
|
36
|
+
transactions: T[]
|
|
37
|
+
): Promise<T[]> {
|
|
38
|
+
return transactions;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return new AnchorProvider(connection, wallet, { commitment: "confirmed" });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function createDuskProgram(options: DuskProgramOptions): DuskProgram {
|
|
46
|
+
const provider =
|
|
47
|
+
options.provider ?? (options.connection ? createReadonlyProvider(options.connection) : undefined);
|
|
48
|
+
|
|
49
|
+
if (!provider) {
|
|
50
|
+
throw new Error("Dusk SDK requires either an Anchor provider or a Solana connection.");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return new Program<Dusk>(duskIdl(options.programId), provider);
|
|
54
|
+
}
|