aftermath-ts-sdk 1.3.23-perps.26 → 1.3.23-perps.27
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/dist/packages/perpetuals/perpetuals.d.ts +539 -14
- package/dist/packages/perpetuals/perpetuals.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetuals.js +540 -57
- package/dist/packages/perpetuals/perpetualsAccount.d.ts +474 -8
- package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsAccount.js +489 -25
- package/dist/packages/perpetuals/perpetualsMarket.d.ts +337 -3
- package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsMarket.js +321 -1
- package/dist/packages/perpetuals/perpetualsTypes.d.ts +907 -30
- package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsTypes.js +70 -0
- package/dist/packages/perpetuals/perpetualsVault.d.ts +3 -2
- package/dist/packages/perpetuals/perpetualsVault.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsVault.js +29 -24
- package/package.json +1 -1
|
@@ -28,10 +28,62 @@ const perpetualsAccount_1 = require("./perpetualsAccount");
|
|
|
28
28
|
const fixedUtils_1 = require("../../general/utils/fixedUtils");
|
|
29
29
|
const utils_1 = require("./utils");
|
|
30
30
|
const perpetualsVault_1 = require("./perpetualsVault");
|
|
31
|
+
/**
|
|
32
|
+
* High-level client for interacting with Aftermath Perpetuals.
|
|
33
|
+
*
|
|
34
|
+
* This class exposes a typed, ergonomic interface over the Perpetuals HTTP API
|
|
35
|
+
* and websocket endpoints, including:
|
|
36
|
+
*
|
|
37
|
+
* - Market discovery (`getAllMarkets`, `getMarkets`, `getMarket`)
|
|
38
|
+
* - Vault discovery (`getAllVaults`, `getVaults`, `getVault`)
|
|
39
|
+
* - Account & position data (`getAccount`, `getAccounts`, `getAccountObjects`)
|
|
40
|
+
* - Ownership queries (`getOwnedAccountCaps`, `getOwnedVaultCaps`)
|
|
41
|
+
* - Historical data & stats (`getMarketHistoricalData`, `getMarkets24hrStats`)
|
|
42
|
+
* - Pricing helpers (`getPrices`, `getLpCoinPrices`)
|
|
43
|
+
* - Transaction builders (`getCreateAccountTx`, `getCreateVaultCapTx`, `getCreateVaultTx`)
|
|
44
|
+
* - Websocket feeds (`openUpdatesWebsocketStream`, `openMarketCandlesWebsocketStream`)
|
|
45
|
+
*
|
|
46
|
+
* Typical usage via the root SDK:
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { Aftermath } from "@aftermath/sdk";
|
|
50
|
+
*
|
|
51
|
+
* const afSdk = new Aftermath("MAINNET");
|
|
52
|
+
* await afSdk.init();
|
|
53
|
+
*
|
|
54
|
+
* const perps = afSdk.Perpetuals();
|
|
55
|
+
*
|
|
56
|
+
* // Fetch markets for a given collateral coin type
|
|
57
|
+
* const markets = await perps.getAllMarkets({
|
|
58
|
+
* collateralCoinType: "0x2::sui::SUI",
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Fetch account + positions for a given account cap
|
|
62
|
+
* const [accountCap] = await perps.getOwnedAccountCaps({
|
|
63
|
+
* walletAddress: "0x...",
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* const account = await perps.getAccount({ accountCap });
|
|
67
|
+
*
|
|
68
|
+
* // Build a create-account transaction (not signed or sent)
|
|
69
|
+
* const createAccountTx = await perps.getCreateAccountTx({
|
|
70
|
+
* walletAddress: "0x...",
|
|
71
|
+
* collateralCoinType: "0x2::sui::SUI",
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
31
75
|
class Perpetuals extends caller_1.Caller {
|
|
32
76
|
// =========================================================================
|
|
33
77
|
// Constructor
|
|
34
78
|
// =========================================================================
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new Perpetuals client.
|
|
81
|
+
*
|
|
82
|
+
* @param config - Optional caller configuration (network, auth token, etc.).
|
|
83
|
+
* @param Provider - Optional shared {@link AftermathApi} provider instance. When
|
|
84
|
+
* provided, transaction-building helpers (e.g. `getCreateAccountTx`) can
|
|
85
|
+
* derive serialized `txKind` from a `Transaction` object.
|
|
86
|
+
*/
|
|
35
87
|
constructor(config, Provider) {
|
|
36
88
|
super(config, "perpetuals");
|
|
37
89
|
this.Provider = Provider;
|
|
@@ -39,12 +91,38 @@ class Perpetuals extends caller_1.Caller {
|
|
|
39
91
|
// =========================================================================
|
|
40
92
|
// Class Objects
|
|
41
93
|
// =========================================================================
|
|
94
|
+
/**
|
|
95
|
+
* Fetch all perpetual markets for a given collateral coin type.
|
|
96
|
+
*
|
|
97
|
+
* @param inputs.collateralCoinType - Coin type used as collateral, e.g. `"0x2::sui::SUI"`.
|
|
98
|
+
* @returns Array of {@link PerpetualsMarket} instances, each wrapping the raw market data.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const markets = await perps.getAllMarkets({
|
|
103
|
+
* collateralCoinType: "0x2::sui::SUI",
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
42
107
|
getAllMarkets(inputs) {
|
|
43
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
109
|
const marketDatas = yield this.fetchApi("all-markets", inputs);
|
|
45
110
|
return marketDatas.map((marketData) => new perpetualsMarket_1.PerpetualsMarket(marketData, this.config));
|
|
46
111
|
});
|
|
47
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Fetch a single market by ID.
|
|
115
|
+
*
|
|
116
|
+
* Internally calls {@link getMarkets} and returns the first entry.
|
|
117
|
+
*
|
|
118
|
+
* @param inputs.marketId - The market (clearing house) object ID.
|
|
119
|
+
* @returns A {@link PerpetualsMarket} instance corresponding to the given ID.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* const market = await perps.getMarket({ marketId: "0x..." });
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
48
126
|
getMarket(inputs) {
|
|
49
127
|
return __awaiter(this, void 0, void 0, function* () {
|
|
50
128
|
const markets = yield this.getMarkets({
|
|
@@ -53,6 +131,23 @@ class Perpetuals extends caller_1.Caller {
|
|
|
53
131
|
return markets[0];
|
|
54
132
|
});
|
|
55
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Fetch multiple markets by ID.
|
|
136
|
+
*
|
|
137
|
+
* NOTE: the backend currently always returns market data together with an
|
|
138
|
+
* orderbook object, but this SDK helper ignores the orderbook and constructs
|
|
139
|
+
* {@link PerpetualsMarket} instances from the `market` field only.
|
|
140
|
+
*
|
|
141
|
+
* @param inputs.marketIds - Array of market object IDs to fetch.
|
|
142
|
+
* @returns Array of {@link PerpetualsMarket} objects in the same order as `marketIds`.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const [marketA, marketB] = await perps.getMarkets({
|
|
147
|
+
* marketIds: ["0x..A", "0x..B"],
|
|
148
|
+
* });
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
56
151
|
getMarkets(inputs) {
|
|
57
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
153
|
const marketDatas = yield this.fetchApi("markets", Object.assign(Object.assign({}, inputs), { withOrderbook: false }));
|
|
@@ -61,12 +156,35 @@ class Perpetuals extends caller_1.Caller {
|
|
|
61
156
|
new perpetualsMarket_1.PerpetualsMarket(marketData.market, this.config));
|
|
62
157
|
});
|
|
63
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Fetch all vaults on the current network.
|
|
161
|
+
*
|
|
162
|
+
* @returns Array of {@link PerpetualsVault} objects, each wrapping a vault on-chain object.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* const vaults = await perps.getAllVaults();
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
64
169
|
getAllVaults() {
|
|
65
170
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
171
|
const vaultObjects = yield this.fetchApi("vaults", {});
|
|
67
172
|
return vaultObjects.map((vaultObject) => new perpetualsVault_1.PerpetualsVault(vaultObject, this.config));
|
|
68
173
|
});
|
|
69
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Fetch a single vault by ID.
|
|
177
|
+
*
|
|
178
|
+
* Internally calls {@link getVaults} and returns the first entry.
|
|
179
|
+
*
|
|
180
|
+
* @param inputs.marketId - The vault object ID (note: named `marketId` for historical reasons).
|
|
181
|
+
* @returns A {@link PerpetualsVault} instance.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* const vault = await perps.getVault({ marketId: "0x..." });
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
70
188
|
getVault(inputs) {
|
|
71
189
|
return __awaiter(this, void 0, void 0, function* () {
|
|
72
190
|
const vaults = yield this.getVaults({
|
|
@@ -75,12 +193,40 @@ class Perpetuals extends caller_1.Caller {
|
|
|
75
193
|
return vaults[0];
|
|
76
194
|
});
|
|
77
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Fetch multiple vaults by ID.
|
|
198
|
+
*
|
|
199
|
+
* @param inputs.vaultIds - Array of vault object IDs.
|
|
200
|
+
* @returns Array of {@link PerpetualsVault} objects in the same order as `vaultIds`.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* const [vaultA, vaultB] = await perps.getVaults({
|
|
205
|
+
* vaultIds: ["0x..A", "0x..B"],
|
|
206
|
+
* });
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
78
209
|
getVaults(inputs) {
|
|
79
210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
211
|
const vaultObjects = yield this.fetchApi("vaults", inputs);
|
|
81
212
|
return vaultObjects.map((vaultObject) => new perpetualsVault_1.PerpetualsVault(vaultObject, this.config));
|
|
82
213
|
});
|
|
83
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Convenience helper to fetch a single account (positions + account object) from an account cap.
|
|
217
|
+
*
|
|
218
|
+
* Internally calls {@link getAccounts} and returns the first entry.
|
|
219
|
+
*
|
|
220
|
+
* @param inputs.accountCap - Account-cap or vault-cap-extended object to derive account metadata from.
|
|
221
|
+
* @param inputs.marketIds - Optional list of markets to filter positions by.
|
|
222
|
+
* @returns A {@link PerpetualsAccount} instance.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* const [accountCap] = await perps.getOwnedAccountCaps({ walletAddress: "0x..." });
|
|
227
|
+
* const account = await perps.getAccount({ accountCap });
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
84
230
|
// TODO: merge this with `getAccountObjects` as an option ?
|
|
85
231
|
getAccount(inputs) {
|
|
86
232
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -91,6 +237,26 @@ class Perpetuals extends caller_1.Caller {
|
|
|
91
237
|
}))[0];
|
|
92
238
|
});
|
|
93
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Fetch one or more accounts (positions + account objects) from account caps.
|
|
242
|
+
*
|
|
243
|
+
* This composes two API calls:
|
|
244
|
+
* - `/perpetuals/accounts/positions` to fetch {@link PerpetualsAccountObject}s
|
|
245
|
+
* - Local pairing with the provided `accountCaps`
|
|
246
|
+
*
|
|
247
|
+
* The resulting {@link PerpetualsAccount} objects wrap both the on-chain account
|
|
248
|
+
* data and the cap metadata in a single helper.
|
|
249
|
+
*
|
|
250
|
+
* @param inputs.accountCaps - Array of account caps or vault-cap-extended objects.
|
|
251
|
+
* @param inputs.marketIds - Optional list of market IDs to filter positions by.
|
|
252
|
+
* @returns Array of {@link PerpetualsAccount} instances in the same order as `accountCaps`.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* const accountCaps = await perps.getOwnedAccountCaps({ walletAddress: "0x..." });
|
|
257
|
+
* const accounts = await perps.getAccounts({ accountCaps });
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
94
260
|
// TODO: make account fetching get positions and account cap data all at once ?
|
|
95
261
|
getAccounts(inputs) {
|
|
96
262
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -106,6 +272,22 @@ class Perpetuals extends caller_1.Caller {
|
|
|
106
272
|
return accountObjects.map((account, index) => new perpetualsAccount_1.PerpetualsAccount(account, accountCaps[index], this.config, this.Provider));
|
|
107
273
|
});
|
|
108
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Fetch raw account objects (including positions) for one or more account IDs.
|
|
277
|
+
*
|
|
278
|
+
* @param inputs.accountIds - List of account IDs to query.
|
|
279
|
+
* @param inputs.collateralCoinType - Collateral coin type to use for valuation.
|
|
280
|
+
* @param inputs.marketIds - Optional list of market IDs to filter positions by.
|
|
281
|
+
* @returns Array of {@link PerpetualsAccountObject} in the same order as `accountIds`.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```ts
|
|
285
|
+
* const accountObjects = await perps.getAccountObjects({
|
|
286
|
+
* accountIds: [123n, 456n],
|
|
287
|
+
* collateralCoinType: "0x2::sui::SUI",
|
|
288
|
+
* });
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
109
291
|
// TODO: handle different collateral coin types ?
|
|
110
292
|
getAccountObjects(inputs) {
|
|
111
293
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -119,6 +301,22 @@ class Perpetuals extends caller_1.Caller {
|
|
|
119
301
|
});
|
|
120
302
|
});
|
|
121
303
|
}
|
|
304
|
+
/**
|
|
305
|
+
* Fetch all account caps (perpetuals accounts) owned by a wallet, optionally
|
|
306
|
+
* filtered by collateral coin types.
|
|
307
|
+
*
|
|
308
|
+
* @param inputs.walletAddress - Owner wallet address.
|
|
309
|
+
* @param inputs.collateralCoinTypes - Optional list of collateral coin types to filter by.
|
|
310
|
+
* @returns Array of {@link PerpetualsAccountCap} objects.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```ts
|
|
314
|
+
* const caps = await perps.getOwnedAccountCaps({
|
|
315
|
+
* walletAddress: "0x...",
|
|
316
|
+
* collateralCoinTypes: ["0x2::sui::SUI"],
|
|
317
|
+
* });
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
122
320
|
getOwnedAccountCaps(inputs) {
|
|
123
321
|
return __awaiter(this, void 0, void 0, function* () {
|
|
124
322
|
const { walletAddress, collateralCoinTypes } = inputs;
|
|
@@ -128,16 +326,61 @@ class Perpetuals extends caller_1.Caller {
|
|
|
128
326
|
});
|
|
129
327
|
});
|
|
130
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* Fetch all vault caps owned by a wallet.
|
|
331
|
+
*
|
|
332
|
+
* @param inputs.walletAddress - Owner wallet address.
|
|
333
|
+
* @returns Array of {@link PerpetualsVaultCap} objects.
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```ts
|
|
337
|
+
* const vaultCaps = await perps.getOwnedVaultCaps({
|
|
338
|
+
* walletAddress: "0x...",
|
|
339
|
+
* });
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
131
342
|
getOwnedVaultCaps(inputs) {
|
|
132
343
|
return __awaiter(this, void 0, void 0, function* () {
|
|
133
344
|
return this.fetchApi("vaults/owned-vault-caps", inputs);
|
|
134
345
|
});
|
|
135
346
|
}
|
|
136
|
-
|
|
347
|
+
/**
|
|
348
|
+
* Fetch all pending vault withdrawal requests created by a given wallet.
|
|
349
|
+
*
|
|
350
|
+
* @param inputs.walletAddress - Wallet address that created the withdraw requests.
|
|
351
|
+
* @returns Array of {@link PerpetualsVaultWithdrawRequest}.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* const withdrawRequests = await perps.getOwnedVaultWithdrawRequests({
|
|
356
|
+
* walletAddress: "0x...",
|
|
357
|
+
* });
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
getOwnedVaultWithdrawRequests(inputs) {
|
|
137
361
|
return __awaiter(this, void 0, void 0, function* () {
|
|
138
362
|
return this.fetchApi("vaults/owned-withdraw-requests", Object.assign({}, inputs));
|
|
139
363
|
});
|
|
140
364
|
}
|
|
365
|
+
// TODO: docs
|
|
366
|
+
getOwnedVaultLpCoins(inputs) {
|
|
367
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
368
|
+
return this.fetchApi("vaults/owned-lp-coins", inputs);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Fetch account caps by their cap object IDs.
|
|
373
|
+
*
|
|
374
|
+
* @param inputs.accountCapIds - List of account cap object IDs.
|
|
375
|
+
* @returns Array of {@link PerpetualsAccountCap}.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```ts
|
|
379
|
+
* const caps = await perps.getAccountCaps({
|
|
380
|
+
* accountCapIds: ["0xcap1", "0xcap2"],
|
|
381
|
+
* });
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
141
384
|
getAccountCaps(inputs) {
|
|
142
385
|
return __awaiter(this, void 0, void 0, function* () {
|
|
143
386
|
return this.fetchApi("accounts", inputs);
|
|
@@ -146,6 +389,25 @@ class Perpetuals extends caller_1.Caller {
|
|
|
146
389
|
// =========================================================================
|
|
147
390
|
// Data
|
|
148
391
|
// =========================================================================
|
|
392
|
+
/**
|
|
393
|
+
* Fetch historical OHLCV candle data for a single market.
|
|
394
|
+
*
|
|
395
|
+
* @param inputs.marketId - Market ID to query.
|
|
396
|
+
* @param inputs.fromTimestamp - Start timestamp (inclusive).
|
|
397
|
+
* @param inputs.toTimestamp - End timestamp (exclusive).
|
|
398
|
+
* @param inputs.intervalMs - Candle interval in milliseconds.
|
|
399
|
+
* @returns Array of {@link PerpetualsMarketCandleDataPoint}.
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```ts
|
|
403
|
+
* const candles = await perps.getMarketHistoricalData({
|
|
404
|
+
* marketId: "0x...",
|
|
405
|
+
* fromTimestamp: Date.now() - 24 * 60 * 60 * 1000,
|
|
406
|
+
* toTimestamp: Date.now(),
|
|
407
|
+
* intervalMs: 60_000, // 1 minute
|
|
408
|
+
* });
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
149
411
|
getMarketHistoricalData(inputs) {
|
|
150
412
|
const { marketId, fromTimestamp, toTimestamp, intervalMs } = inputs;
|
|
151
413
|
return this.fetchApi("market/candle-history", {
|
|
@@ -155,12 +417,38 @@ class Perpetuals extends caller_1.Caller {
|
|
|
155
417
|
intervalMs,
|
|
156
418
|
});
|
|
157
419
|
}
|
|
420
|
+
/**
|
|
421
|
+
* Fetch 24-hour stats for multiple markets.
|
|
422
|
+
*
|
|
423
|
+
* @param inputs.marketIds - Market IDs to query.
|
|
424
|
+
* @returns Array of 24hr stats aligned with `marketIds`.
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* ```ts
|
|
428
|
+
* const stats = await perps.getMarkets24hrStats({
|
|
429
|
+
* marketIds: ["0x...", "0x..."],
|
|
430
|
+
* });
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
158
433
|
getMarkets24hrStats(inputs) {
|
|
159
434
|
return this.fetchApi("markets/24hr-stats", inputs);
|
|
160
435
|
}
|
|
161
436
|
// =========================================================================
|
|
162
437
|
// Prices
|
|
163
438
|
// =========================================================================
|
|
439
|
+
/**
|
|
440
|
+
* Fetch the latest oracle prices (base & collateral) for one or more markets.
|
|
441
|
+
*
|
|
442
|
+
* @param inputs.marketIds - List of market IDs to query.
|
|
443
|
+
* @returns Array of `{ basePrice, collateralPrice }` objects in the same order as `marketIds`.
|
|
444
|
+
* Returns `[]` if `marketIds` is empty.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```ts
|
|
448
|
+
* const prices = await perps.getPrices({ marketIds: ["0x..."] });
|
|
449
|
+
* const { basePrice, collateralPrice } = prices[0];
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
164
452
|
getPrices(inputs) {
|
|
165
453
|
return __awaiter(this, void 0, void 0, function* () {
|
|
166
454
|
if (inputs.marketIds.length <= 0)
|
|
@@ -168,6 +456,17 @@ class Perpetuals extends caller_1.Caller {
|
|
|
168
456
|
return this.fetchApi("markets/prices", inputs);
|
|
169
457
|
});
|
|
170
458
|
}
|
|
459
|
+
/**
|
|
460
|
+
* Fetch LP coin prices (in collateral units) for a set of vaults.
|
|
461
|
+
*
|
|
462
|
+
* @param inputs.vaultIds - List of vault IDs to query.
|
|
463
|
+
* @returns Array of LP prices corresponding to each vault ID; returns `[]` if none are provided.
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* ```ts
|
|
467
|
+
* const [price] = await perps.getLpCoinPrices({ vaultIds: ["0x..."] });
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
171
470
|
getLpCoinPrices(inputs) {
|
|
172
471
|
return __awaiter(this, void 0, void 0, function* () {
|
|
173
472
|
if (inputs.vaultIds.length <= 0)
|
|
@@ -178,6 +477,31 @@ class Perpetuals extends caller_1.Caller {
|
|
|
178
477
|
// =========================================================================
|
|
179
478
|
// Transactions
|
|
180
479
|
// =========================================================================
|
|
480
|
+
/**
|
|
481
|
+
* Build a `create-account` transaction for Aftermath Perpetuals.
|
|
482
|
+
*
|
|
483
|
+
* This helper:
|
|
484
|
+
* - Optionally converts a {@link Transaction} into a serialized `txKind`
|
|
485
|
+
* via the shared `Provider` (if present).
|
|
486
|
+
* - Calls the `/perpetuals/transactions/create-account` endpoint.
|
|
487
|
+
* - Returns a serialized transaction (`txKind`) that you can sign and execute.
|
|
488
|
+
*
|
|
489
|
+
* @param inputs.walletAddress - The wallet address that will own the new account.
|
|
490
|
+
* @param inputs.collateralCoinType - Collateral coin type to be used with this account.
|
|
491
|
+
* @param inputs.tx - Optional {@link Transaction} to extend; if provided,
|
|
492
|
+
* the create-account commands are appended to this transaction.
|
|
493
|
+
*
|
|
494
|
+
* @returns API transaction response containing `txKind`.
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* ```ts
|
|
498
|
+
* const { txKind } = await perps.getCreateAccountTx({
|
|
499
|
+
* walletAddress: "0x...",
|
|
500
|
+
* collateralCoinType: "0x2::sui::SUI",
|
|
501
|
+
* });
|
|
502
|
+
* // sign + execute txKind with your wallet adapter
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
181
505
|
getCreateAccountTx(inputs) {
|
|
182
506
|
return __awaiter(this, void 0, void 0, function* () {
|
|
183
507
|
var _a;
|
|
@@ -191,30 +515,23 @@ class Perpetuals extends caller_1.Caller {
|
|
|
191
515
|
});
|
|
192
516
|
});
|
|
193
517
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
// },
|
|
212
|
-
// undefined,
|
|
213
|
-
// {
|
|
214
|
-
// txKind: true,
|
|
215
|
-
// }
|
|
216
|
-
// );
|
|
217
|
-
// }
|
|
518
|
+
/**
|
|
519
|
+
* Build a `create-vault-cap` transaction.
|
|
520
|
+
*
|
|
521
|
+
* This helper directly forwards the body through to the backend. If you wish
|
|
522
|
+
* to extend an existing {@link Transaction}, build the `txKind` yourself
|
|
523
|
+
* and pass it as part of {@link ApiPerpetualsCreateVaultCapBody}.
|
|
524
|
+
*
|
|
525
|
+
* @param inputs - Request body for the create-vault-cap endpoint.
|
|
526
|
+
* @returns API transaction response containing `txKind`.
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* const { txKind } = await perps.getCreateVaultCapTx({
|
|
531
|
+
* walletAddress: "0x...",
|
|
532
|
+
* });
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
218
535
|
getCreateVaultCapTx(
|
|
219
536
|
// TODO: add tx support ?
|
|
220
537
|
inputs) {
|
|
@@ -224,31 +541,47 @@ class Perpetuals extends caller_1.Caller {
|
|
|
224
541
|
});
|
|
225
542
|
});
|
|
226
543
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
544
|
+
/**
|
|
545
|
+
* Build a `create-vault` transaction.
|
|
546
|
+
*
|
|
547
|
+
* This helper:
|
|
548
|
+
* - Optionally converts a {@link Transaction} into a serialized `txKind`
|
|
549
|
+
* via the shared `Provider` (if present).
|
|
550
|
+
* - Calls `/perpetuals/vault/transactions/create-vault`.
|
|
551
|
+
* - Returns a serialized transaction (`txKind`) that you can sign and execute.
|
|
552
|
+
*
|
|
553
|
+
* You can specify the initial deposit either as an explicit amount or as a
|
|
554
|
+
* `depositCoinArg` referring to an existing transaction argument.
|
|
555
|
+
*
|
|
556
|
+
* @param inputs.name - Human-readable vault name.
|
|
557
|
+
* @param inputs.walletAddress - Address of vault owner.
|
|
558
|
+
* @param inputs.lpCoinType - Coin type for the LP token.
|
|
559
|
+
* @param inputs.collateralCoinType - Collateral coin type for the vault.
|
|
560
|
+
* @param inputs.lockPeriodMs - Lock-in period for deposits in milliseconds.
|
|
561
|
+
* @param inputs.ownerFeePercentage - Percentage of user profits taken as owner fee.
|
|
562
|
+
* @param inputs.forceWithdrawDelayMs - Delay before forced withdrawals are processed.
|
|
563
|
+
* @param inputs.isSponsoredTx - Whether this transaction is sponsored (fees paid by another party).
|
|
564
|
+
* @param inputs.initialDepositAmount - Initial deposit amount (mutually exclusive with `initialDepositCoinArg`).
|
|
565
|
+
* @param inputs.initialDepositCoinArg - Transaction object argument referencing the deposit coin.
|
|
566
|
+
* @param inputs.tx - Optional {@link Transaction} to extend.
|
|
567
|
+
*
|
|
568
|
+
* @returns API transaction response containing `txKind`.
|
|
569
|
+
*
|
|
570
|
+
* @example
|
|
571
|
+
* ```ts
|
|
572
|
+
* const { txKind } = await perps.getCreateVaultTx({
|
|
573
|
+
* name: "My Vault",
|
|
574
|
+
* walletAddress: "0x...",
|
|
575
|
+
* lpCoinType: "0x...::lp::LP",
|
|
576
|
+
* collateralCoinType: "0x2::sui::SUI",
|
|
577
|
+
* lockPeriodMs: BigInt(7 * 24 * 60 * 60 * 1000),
|
|
578
|
+
* ownerFeePercentage: 0.2,
|
|
579
|
+
* forceWithdrawDelayMs: BigInt(24 * 60 * 60 * 1000),
|
|
580
|
+
* initialDepositAmount: BigInt("1000000000"),
|
|
581
|
+
* });
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
getCreateVaultTx(inputs) {
|
|
252
585
|
return __awaiter(this, void 0, void 0, function* () {
|
|
253
586
|
var _a;
|
|
254
587
|
const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
|
|
@@ -263,24 +596,65 @@ class Perpetuals extends caller_1.Caller {
|
|
|
263
596
|
// =========================================================================
|
|
264
597
|
// Helpers
|
|
265
598
|
// =========================================================================
|
|
599
|
+
/**
|
|
600
|
+
* Determine the logical order side (Bid/Ask) from a signed base asset amount.
|
|
601
|
+
*
|
|
602
|
+
* @param inputs.baseAssetAmount - Position base size. Positive => Bid (long), negative => Ask (short).
|
|
603
|
+
* @returns Corresponding {@link PerpetualsOrderSide}.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* const side = Perpetuals.positionSide({ baseAssetAmount: -1 });
|
|
608
|
+
* // side === PerpetualsOrderSide.Ask
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
266
611
|
static positionSide(inputs) {
|
|
267
612
|
const baseAmount = inputs.baseAssetAmount;
|
|
268
613
|
const isLong = Math.sign(baseAmount);
|
|
269
614
|
const side = isLong >= 0 ? types_1.PerpetualsOrderSide.Bid : types_1.PerpetualsOrderSide.Ask;
|
|
270
615
|
return side;
|
|
271
616
|
}
|
|
617
|
+
/**
|
|
618
|
+
* Compute the effective price from a {@link FilledTakerOrderEvent}.
|
|
619
|
+
*
|
|
620
|
+
* Uses `quoteAssetDelta / baseAssetDelta`.
|
|
621
|
+
*
|
|
622
|
+
* @param inputs.orderEvent - Filled taker order event.
|
|
623
|
+
* @returns Trade price as a `number`.
|
|
624
|
+
*/
|
|
272
625
|
static orderPriceFromEvent(inputs) {
|
|
273
626
|
const { orderEvent } = inputs;
|
|
274
627
|
return orderEvent.quoteAssetDelta / orderEvent.baseAssetDelta;
|
|
275
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* Extract the price (as floating-point) from an encoded order ID.
|
|
631
|
+
*
|
|
632
|
+
* Internally uses {@link PerpetualsOrderUtils.price} and converts the
|
|
633
|
+
* fixed-point `PerpetualsOrderPrice` into a `number`.
|
|
634
|
+
*
|
|
635
|
+
* @param inputs.orderId - Encoded order ID.
|
|
636
|
+
* @returns Floating-point price.
|
|
637
|
+
*/
|
|
276
638
|
static orderPriceFromOrderId(inputs) {
|
|
277
639
|
const { orderId } = inputs;
|
|
278
640
|
const orderPrice = utils_1.PerpetualsOrderUtils.price(orderId);
|
|
279
641
|
return this.orderPriceToPrice({ orderPrice });
|
|
280
642
|
}
|
|
643
|
+
/**
|
|
644
|
+
* Convert a fixed-point lot or tick size (9 decimals) to a `number`.
|
|
645
|
+
*
|
|
646
|
+
* @param lotOrTickSize - Fixed-point size as `bigint`.
|
|
647
|
+
* @returns Floating-point representation.
|
|
648
|
+
*/
|
|
281
649
|
static lotOrTickSizeToNumber(lotOrTickSize) {
|
|
282
650
|
return Number(lotOrTickSize) / fixedUtils_1.FixedUtils.fixedOneN9;
|
|
283
651
|
}
|
|
652
|
+
/**
|
|
653
|
+
* Convert a floating-point lot or tick size to its fixed-point representation (9 decimals).
|
|
654
|
+
*
|
|
655
|
+
* @param lotOrTickSize - Floating-point size.
|
|
656
|
+
* @returns Fixed-point representation as `bigint`.
|
|
657
|
+
*/
|
|
284
658
|
static lotOrTickSizeToBigInt(lotOrTickSize) {
|
|
285
659
|
return BigInt(Math.round(lotOrTickSize * fixedUtils_1.FixedUtils.fixedOneN9));
|
|
286
660
|
}
|
|
@@ -300,9 +674,50 @@ class Perpetuals extends caller_1.Caller {
|
|
|
300
674
|
// Websocket
|
|
301
675
|
// =========================================================================
|
|
302
676
|
/**
|
|
303
|
-
* Open the main updates websocket:
|
|
677
|
+
* Open the main updates websocket: `/perpetuals/ws/updates`.
|
|
678
|
+
*
|
|
679
|
+
* This stream can deliver:
|
|
680
|
+
* - Market updates
|
|
681
|
+
* - User account + stop order updates
|
|
682
|
+
* - Oracle price updates
|
|
683
|
+
* - Orderbook deltas
|
|
684
|
+
* - Market trades
|
|
685
|
+
* - User trades
|
|
686
|
+
* - User collateral changes
|
|
687
|
+
*
|
|
688
|
+
* The returned controller object includes a set of convenient subscribe /
|
|
689
|
+
* unsubscribe helpers for each stream type.
|
|
304
690
|
*
|
|
305
|
-
* @
|
|
691
|
+
* @param args.onMessage - Handler for incoming messages from the ws.
|
|
692
|
+
* @param args.onOpen - Optional hook called when the websocket is opened.
|
|
693
|
+
* @param args.onError - Optional hook called on websocket error.
|
|
694
|
+
* @param args.onClose - Optional hook called when the websocket closes.
|
|
695
|
+
*
|
|
696
|
+
* @returns An object containing:
|
|
697
|
+
* - `ws`: the underlying `WebSocket` instance
|
|
698
|
+
* - subscribe/unsubscribe helpers:
|
|
699
|
+
* - `subscribeMarket` / `unsubscribeMarket`
|
|
700
|
+
* - `subscribeUser` / `unsubscribeUser`
|
|
701
|
+
* - `subscribeOracle` / `unsubscribeOracle`
|
|
702
|
+
* - `subscribeOrderbook` / `unsubscribeOrderbook`
|
|
703
|
+
* - `subscribeMarketTrades` / `unsubscribeMarketTrades`
|
|
704
|
+
* - `subscribeUserTrades` / `unsubscribeUserTrades`
|
|
705
|
+
* - `subscribeUserCollateralChanges` / `unsubscribeUserCollateralChanges`
|
|
706
|
+
* - `close`: function to close the websocket
|
|
707
|
+
*
|
|
708
|
+
* @example
|
|
709
|
+
* ```ts
|
|
710
|
+
* const stream = perps.openUpdatesWebsocketStream({
|
|
711
|
+
* onMessage: (msg) => {
|
|
712
|
+
* if ("market" in msg) {
|
|
713
|
+
* console.log("Market update", msg.market);
|
|
714
|
+
* }
|
|
715
|
+
* },
|
|
716
|
+
* });
|
|
717
|
+
*
|
|
718
|
+
* stream.subscribeMarket({ marketId: "0x..." });
|
|
719
|
+
* stream.subscribeUser({ accountId: 123n, withStopOrders: undefined });
|
|
720
|
+
* ```
|
|
306
721
|
*/
|
|
307
722
|
openUpdatesWebsocketStream(args) {
|
|
308
723
|
const { onMessage, onOpen, onError, onClose } = args;
|
|
@@ -390,8 +805,33 @@ class Perpetuals extends caller_1.Caller {
|
|
|
390
805
|
};
|
|
391
806
|
}
|
|
392
807
|
/**
|
|
393
|
-
* Open market-candles websocket for a single market/interval:
|
|
394
|
-
*
|
|
808
|
+
* Open a market-candles websocket stream for a single market/interval:
|
|
809
|
+
* `/perpetuals/ws/market-candles/{market_id}/{interval_ms}`.
|
|
810
|
+
*
|
|
811
|
+
* The stream emits {@link PerpetualsWsCandleResponseMessage} messages,
|
|
812
|
+
* typically containing the latest candle for the specified interval.
|
|
813
|
+
*
|
|
814
|
+
* @param args.marketId - Market ID to subscribe to.
|
|
815
|
+
* @param args.intervalMs - Candle interval in milliseconds.
|
|
816
|
+
* @param args.onMessage - Handler for incoming candle updates.
|
|
817
|
+
* @param args.onOpen - Optional hook called when the websocket opens.
|
|
818
|
+
* @param args.onError - Optional hook called on websocket error.
|
|
819
|
+
* @param args.onClose - Optional hook called when the websocket closes.
|
|
820
|
+
*
|
|
821
|
+
* @returns An object containing:
|
|
822
|
+
* - `ws`: the underlying `WebSocket` instance
|
|
823
|
+
* - `close`: function to close the websocket
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* ```ts
|
|
827
|
+
* const stream = perps.openMarketCandlesWebsocketStream({
|
|
828
|
+
* marketId: "0x...",
|
|
829
|
+
* intervalMs: 60_000,
|
|
830
|
+
* onMessage: ({ lastCandle }) => {
|
|
831
|
+
* console.log("New candle:", lastCandle);
|
|
832
|
+
* },
|
|
833
|
+
* });
|
|
834
|
+
* ```
|
|
395
835
|
*/
|
|
396
836
|
openMarketCandlesWebsocketStream(args) {
|
|
397
837
|
const { marketId, intervalMs, onMessage, onOpen, onError, onClose } = args;
|
|
@@ -414,23 +854,66 @@ exports.Perpetuals = Perpetuals;
|
|
|
414
854
|
// =========================================================================
|
|
415
855
|
// Constants
|
|
416
856
|
// =========================================================================
|
|
857
|
+
/**
|
|
858
|
+
* Helper namespace for order-specific utilities such as parsing order IDs,
|
|
859
|
+
* extracting price bits, etc.
|
|
860
|
+
*
|
|
861
|
+
* This is a direct alias of {@link PerpetualsOrderUtils}.
|
|
862
|
+
*/
|
|
417
863
|
Perpetuals.OrderUtils = utils_1.PerpetualsOrderUtils;
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
864
|
+
/**
|
|
865
|
+
* Convert a floating-point price into a fixed-point {@link PerpetualsOrderPrice}
|
|
866
|
+
* using 9 decimal places of precision.
|
|
867
|
+
*
|
|
868
|
+
* @param inputs.price - Floating-point price.
|
|
869
|
+
* @returns Encoded {@link PerpetualsOrderPrice} as `bigint`.
|
|
870
|
+
*/
|
|
421
871
|
Perpetuals.priceToOrderPrice = (inputs) => {
|
|
422
872
|
const { price } = inputs;
|
|
423
873
|
return BigInt(Math.round(price * fixedUtils_1.FixedUtils.fixedOneN9));
|
|
424
874
|
};
|
|
875
|
+
/**
|
|
876
|
+
* Convert a fixed-point {@link PerpetualsOrderPrice} to a human-friendly price.
|
|
877
|
+
*
|
|
878
|
+
* @param inputs.orderPrice - Encoded order price as `bigint`.
|
|
879
|
+
* @returns Floating-point price value.
|
|
880
|
+
*/
|
|
425
881
|
Perpetuals.orderPriceToPrice = (inputs) => {
|
|
426
882
|
const { orderPrice } = inputs;
|
|
427
883
|
return Number(orderPrice) / fixedUtils_1.FixedUtils.fixedOneN9;
|
|
428
884
|
};
|
|
885
|
+
/**
|
|
886
|
+
* Infer the order side from an order ID.
|
|
887
|
+
*
|
|
888
|
+
* Uses {@link PerpetualsOrderUtils.isAsk} under the hood.
|
|
889
|
+
*
|
|
890
|
+
* @param orderId - Encoded order ID.
|
|
891
|
+
* @returns {@link PerpetualsOrderSide.Ask} if ask, otherwise {@link PerpetualsOrderSide.Bid}.
|
|
892
|
+
*/
|
|
429
893
|
Perpetuals.orderIdToSide = (orderId) => {
|
|
430
894
|
return Perpetuals.OrderUtils.isAsk(orderId)
|
|
431
895
|
? types_1.PerpetualsOrderSide.Ask
|
|
432
896
|
: types_1.PerpetualsOrderSide.Bid;
|
|
433
897
|
};
|
|
898
|
+
/**
|
|
899
|
+
* Construct a full event type string for a collateral-specific event.
|
|
900
|
+
*
|
|
901
|
+
* Many Move events are generic over a collateral coin type. This helper
|
|
902
|
+
* appends `<collateralCoinType>` to a base `eventType`.
|
|
903
|
+
*
|
|
904
|
+
* @param inputs.eventType - Base event type without type parameters.
|
|
905
|
+
* @param inputs.collateralCoinType - Collateral coin type, e.g. `"0x2::sui::SUI"`.
|
|
906
|
+
* @returns Fully-qualified event type string.
|
|
907
|
+
*
|
|
908
|
+
* @example
|
|
909
|
+
* ```ts
|
|
910
|
+
* const fullType = Perpetuals.eventTypeForCollateral({
|
|
911
|
+
* eventType: "0x1::perps::DepositedCollateral",
|
|
912
|
+
* collateralCoinType: "0x2::sui::SUI",
|
|
913
|
+
* });
|
|
914
|
+
* // "0x1::perps::DepositedCollateral<0x2::sui::SUI>"
|
|
915
|
+
* ```
|
|
916
|
+
*/
|
|
434
917
|
Perpetuals.eventTypeForCollateral = (inputs) => {
|
|
435
918
|
return `${inputs.eventType}<${inputs.collateralCoinType}>`;
|
|
436
919
|
};
|