aftermath-ts-sdk 1.3.26-perps.13 → 1.3.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/general/apiHelpers/transactionsApiHelpers.d.ts +0 -3
- package/dist/general/apiHelpers/transactionsApiHelpers.d.ts.map +1 -1
- package/dist/general/apiHelpers/transactionsApiHelpers.js +0 -12
- package/dist/general/providers/aftermath.d.ts +5 -0
- package/dist/general/providers/aftermath.d.ts.map +1 -1
- package/dist/general/providers/aftermath.js +6 -1
- package/dist/general/providers/aftermathApi.d.ts +5 -0
- package/dist/general/providers/aftermathApi.d.ts.map +1 -1
- package/dist/general/providers/aftermathApi.js +5 -0
- package/dist/general/types/castingTypes.d.ts +2 -2
- package/dist/general/types/castingTypes.d.ts.map +1 -1
- package/dist/general/types/configTypes.d.ts +10 -6
- package/dist/general/types/configTypes.d.ts.map +1 -1
- package/dist/general/types/generalTypes.d.ts +0 -8
- package/dist/general/types/generalTypes.d.ts.map +1 -1
- package/dist/general/types/moveErrorsInterface.d.ts +1 -1
- package/dist/general/types/moveErrorsInterface.d.ts.map +1 -1
- package/dist/general/utils/caller.d.ts +2 -34
- package/dist/general/utils/caller.d.ts.map +1 -1
- package/dist/general/utils/caller.js +12 -111
- package/dist/general/utils/casting.d.ts +5 -4
- package/dist/general/utils/casting.d.ts.map +1 -1
- package/dist/general/utils/casting.js +5 -4
- package/dist/general/utils/helpers.d.ts +0 -1
- package/dist/general/utils/helpers.d.ts.map +1 -1
- package/dist/general/utils/helpers.js +6 -38
- package/dist/general/wallet/wallet.d.ts +1 -1
- package/dist/general/wallet/wallet.d.ts.map +1 -1
- package/dist/packages/coin/coin.d.ts +1 -5
- package/dist/packages/coin/coin.d.ts.map +1 -1
- package/dist/packages/coin/coin.js +1 -7
- package/dist/packages/farms/api/farmsApiCasting.js +27 -27
- package/dist/packages/farms/farms.d.ts +1 -1
- package/dist/packages/farms/farms.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakedPosition.d.ts +1 -1
- package/dist/packages/farms/farmsStakedPosition.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakingPool.d.ts +1 -1
- package/dist/packages/farms/farmsStakingPool.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApi.d.ts +17 -9
- package/dist/packages/faucet/api/faucetApi.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApi.js +49 -44
- package/dist/packages/faucet/api/faucetApiCasting.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApiCasting.js +7 -10
- package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts +4 -1
- package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts.map +1 -1
- package/dist/packages/faucet/faucet.d.ts +1 -1
- package/dist/packages/faucet/faucet.d.ts.map +1 -1
- package/dist/packages/faucet/faucet.js +1 -1
- package/dist/packages/faucet/faucetTypes.d.ts +3 -2
- package/dist/packages/faucet/faucetTypes.d.ts.map +1 -1
- package/dist/packages/leveragedStaking/api/leveragedStakingApiCasting.js +3 -3
- package/dist/packages/multisig/multisig.d.ts +1 -1
- package/dist/packages/multisig/multisig.d.ts.map +1 -1
- package/dist/packages/nftAmm/nftAmm.d.ts +1 -1
- package/dist/packages/nftAmm/nftAmm.d.ts.map +1 -1
- package/dist/packages/oracle/api/oracleApi.d.ts +21 -0
- package/dist/packages/oracle/api/oracleApi.d.ts.map +1 -0
- package/dist/packages/oracle/api/oracleApi.js +76 -0
- package/dist/packages/oracle/api/oracleApiCasting.d.ts +6 -0
- package/dist/packages/oracle/api/oracleApiCasting.d.ts.map +1 -0
- package/dist/packages/oracle/api/oracleApiCasting.js +25 -0
- package/dist/packages/oracle/oracle.d.ts +15 -0
- package/dist/packages/oracle/oracle.d.ts.map +1 -0
- package/dist/packages/oracle/oracle.js +46 -0
- package/dist/packages/oracle/oracleCastingTypes.d.ts +11 -0
- package/dist/packages/oracle/oracleCastingTypes.d.ts.map +1 -0
- package/dist/packages/oracle/oracleCastingTypes.js +2 -0
- package/dist/packages/oracle/oracleTypes.d.ts +10 -0
- package/dist/packages/oracle/oracleTypes.d.ts.map +1 -0
- package/dist/packages/oracle/oracleTypes.js +2 -0
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts +203 -12
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApi.js +615 -926
- package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts +21 -10
- package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApiCasting.js +238 -260
- package/dist/packages/perpetuals/index.d.ts +0 -1
- package/dist/packages/perpetuals/index.d.ts.map +1 -1
- package/dist/packages/perpetuals/index.js +0 -1
- package/dist/packages/perpetuals/perpetuals.d.ts +28 -813
- package/dist/packages/perpetuals/perpetuals.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetuals.js +70 -984
- package/dist/packages/perpetuals/perpetualsAccount.d.ts +170 -754
- package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsAccount.js +312 -1267
- package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts +149 -82
- package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsMarket.d.ts +27 -341
- package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsMarket.js +78 -411
- package/dist/packages/perpetuals/perpetualsTypes.d.ts +319 -2575
- package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsTypes.js +82 -92
- package/dist/packages/pools/api/poolsApiCasting.js +3 -3
- package/dist/packages/pools/pool.d.ts +1 -1
- package/dist/packages/pools/pool.d.ts.map +1 -1
- package/dist/packages/pools/pools.d.ts +1 -1
- package/dist/packages/pools/pools.d.ts.map +1 -1
- package/dist/packages/router/api/routerApiCasting.js +1 -1
- package/dist/packages/router/routerTypes.d.ts +1 -1
- package/dist/packages/router/routerTypes.d.ts.map +1 -1
- package/dist/packages/staking/api/stakingApiCasting.js +3 -3
- package/dist/packages/staking/staking.d.ts +1 -1
- package/dist/packages/staking/staking.d.ts.map +1 -1
- package/dist/packages/sui/sui.d.ts +1 -1
- package/dist/packages/sui/sui.d.ts.map +1 -1
- package/dist/packages/suiFrens/api/suiFrensApiCasting.js +4 -4
- package/dist/packages/suiFrens/stakedSuiFren.d.ts +1 -1
- package/dist/packages/suiFrens/stakedSuiFren.d.ts.map +1 -1
- package/dist/packages/suiFrens/suiFren.d.ts +1 -1
- package/dist/packages/suiFrens/suiFren.d.ts.map +1 -1
- package/dist/packages/suiFrens/suiFrens.d.ts +1 -1
- package/dist/packages/suiFrens/suiFrens.d.ts.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +1 -1
- package/dist/packages/perpetuals/perpetualsVault.d.ts +0 -438
- package/dist/packages/perpetuals/perpetualsVault.d.ts.map +0 -1
- package/dist/packages/perpetuals/perpetualsVault.js +0 -601
|
@@ -12,155 +12,77 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.PerpetualsMarket = void 0;
|
|
13
13
|
const __1 = require("../..");
|
|
14
14
|
const caller_1 = require("../../general/utils/caller");
|
|
15
|
+
const iFixedUtils_1 = require("../../general/utils/iFixedUtils");
|
|
15
16
|
const perpetuals_1 = require("./perpetuals");
|
|
16
|
-
|
|
17
|
-
* High-level wrapper around a single perpetuals market.
|
|
18
|
-
*
|
|
19
|
-
* This class provides:
|
|
20
|
-
*
|
|
21
|
-
* - Lightweight accessors for immutable market properties:
|
|
22
|
-
* - `marketId`, `indexPrice`, `collateralPrice`, `collateralCoinType`
|
|
23
|
-
* - `marketParams`, `marketState`
|
|
24
|
-
* - Read endpoints for:
|
|
25
|
-
* - Orderbook snapshots
|
|
26
|
-
* - 24h stats and order history
|
|
27
|
-
* - Market prices and derived funding metrics
|
|
28
|
-
* - Helpers for:
|
|
29
|
-
* - Order sizing (max size, lot/tick rounding)
|
|
30
|
-
* - Margin and collateral calculations
|
|
31
|
-
* - Constructing an “empty” position for a market
|
|
32
|
-
*
|
|
33
|
-
* Typical usage:
|
|
34
|
-
*
|
|
35
|
-
* ```ts
|
|
36
|
-
* const perps = new Perpetuals(config);
|
|
37
|
-
* const { markets } = await perps.getMarkets({ marketIds: ["0x..."] });
|
|
38
|
-
* const market = markets[0];
|
|
39
|
-
*
|
|
40
|
-
* const { orderbook } = await market.getOrderbook();
|
|
41
|
-
* const stats = await market.get24hrStats();
|
|
42
|
-
* const { basePrice, collateralPrice } = await market.getPrices();
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
17
|
+
const utils_1 = require("./utils");
|
|
45
18
|
class PerpetualsMarket extends caller_1.Caller {
|
|
46
19
|
// =========================================================================
|
|
47
20
|
// Constructor
|
|
48
21
|
// =========================================================================
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
*
|
|
52
|
-
* @param marketData - Snapshot of market configuration and state.
|
|
53
|
-
* @param config - Optional {@link CallerConfig} (network, base URL, etc.).
|
|
54
|
-
* @param Provider - Optional shared {@link AftermathApi} provider instance.
|
|
55
|
-
*
|
|
56
|
-
* @remarks
|
|
57
|
-
* This class extends {@link Caller} with the `"perpetuals"` route prefix, meaning
|
|
58
|
-
* all HTTP requests resolve under `/perpetuals/...`.
|
|
59
|
-
*/
|
|
60
|
-
constructor(marketData, config, Provider) {
|
|
61
|
-
super(config, "perpetuals");
|
|
22
|
+
constructor(marketData, config) {
|
|
23
|
+
super(config, `perpetuals/${marketData.collateralCoinType}/markets/${marketData.objectId}`);
|
|
62
24
|
this.marketData = marketData;
|
|
63
|
-
this.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
* the account's collateral and positions.
|
|
74
|
-
*
|
|
75
|
-
* @param inputs.accountId - Perpetuals account ID.
|
|
76
|
-
* @param inputs.side - Order side (Bid/Ask).
|
|
77
|
-
* @param inputs.leverage - Optional assumed leverage.
|
|
78
|
-
* @param inputs.price - Optional assumed price (e.g. for limit orders).
|
|
79
|
-
*
|
|
80
|
-
* @returns `{ maxOrderSize }` in base units (scaled integer as `bigint`).
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```ts
|
|
84
|
-
* const { maxOrderSize } = await market.getMaxOrderSize({
|
|
85
|
-
* accountId: 123n,
|
|
86
|
-
* side: PerpetualsOrderSide.Bid,
|
|
87
|
-
* leverage: 5,
|
|
88
|
-
* });
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
// TODO: move/add to account ?
|
|
92
|
-
this.getMaxOrderSize = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
93
|
-
return this.fetchApi("account/max-order-size", Object.assign(Object.assign({}, inputs), { marketId: this.marketId }));
|
|
25
|
+
this.getMaxOrderSizeUsd = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const { side, price, account, indexPrice, leverage } = inputs;
|
|
27
|
+
const maxSize = yield this.fetchApi("max-order-size", {
|
|
28
|
+
accountId: account.accountCap.accountId,
|
|
29
|
+
collateral: account.collateralBalance(),
|
|
30
|
+
side,
|
|
31
|
+
price,
|
|
32
|
+
leverage,
|
|
33
|
+
});
|
|
34
|
+
return Number(maxSize) * this.lotSize() * indexPrice;
|
|
94
35
|
});
|
|
95
36
|
// =========================================================================
|
|
96
|
-
//
|
|
37
|
+
// Calculations
|
|
97
38
|
// =========================================================================
|
|
98
|
-
/**
|
|
99
|
-
* Compute the remaining time until the next funding event, in milliseconds.
|
|
100
|
-
*
|
|
101
|
-
* @returns `nextFundingTimeMs() - Date.now()`.
|
|
102
|
-
*
|
|
103
|
-
* @remarks
|
|
104
|
-
* If the next funding timestamp does not fit safely into a JS `number`,
|
|
105
|
-
* {@link nextFundingTimeMs} returns `Number.MAX_SAFE_INTEGER`, and the
|
|
106
|
-
* difference may be very large.
|
|
107
|
-
*/
|
|
108
39
|
this.timeUntilNextFundingMs = () => {
|
|
109
40
|
return this.nextFundingTimeMs() - Date.now();
|
|
110
41
|
};
|
|
111
|
-
/**
|
|
112
|
-
* Get the scheduled timestamp for the next funding event, in milliseconds.
|
|
113
|
-
*
|
|
114
|
-
* Safety behavior:
|
|
115
|
-
* - If `marketData.nextFundingTimestampMs` exceeds `Number.MAX_SAFE_INTEGER`,
|
|
116
|
-
* this returns `Number.MAX_SAFE_INTEGER`.
|
|
117
|
-
*
|
|
118
|
-
* @returns Next funding timestamp (ms) as a JS `number`.
|
|
119
|
-
*/
|
|
120
42
|
this.nextFundingTimeMs = () => {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
: Number(this.marketData.nextFundingTimestampMs);
|
|
43
|
+
const fundingFrequencyMs = Number(this.marketParams.fundingFrequencyMs);
|
|
44
|
+
const lastFundingIntervalNumber = Math.floor(this.marketState.fundingLastUpdateMs / fundingFrequencyMs);
|
|
45
|
+
return (lastFundingIntervalNumber + 1) * fundingFrequencyMs;
|
|
125
46
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
47
|
+
// The funding rate as the difference between book and index TWAPs relative to the index price,
|
|
48
|
+
// scaled by the funding period adjustment:
|
|
49
|
+
// (bookTwap - indexTwap) / indexPrice * (fundingFrequency / fundingPeriod)
|
|
50
|
+
//
|
|
51
|
+
// To get the rate as a percentage, multiply the output by 100.
|
|
52
|
+
this.estimatedFundingRate = (inputs) => {
|
|
53
|
+
const { indexPrice } = inputs;
|
|
54
|
+
const premiumTwap = iFixedUtils_1.IFixedUtils.numberFromIFixed(this.marketState.premiumTwap);
|
|
55
|
+
const relativePremium = premiumTwap / indexPrice;
|
|
56
|
+
const periodAdjustment = Number(this.marketParams.fundingFrequencyMs) /
|
|
57
|
+
Number(this.marketParams.fundingPeriodMs);
|
|
58
|
+
return relativePremium * periodAdjustment;
|
|
59
|
+
};
|
|
60
|
+
this.priceToOrderPrice = (inputs) => {
|
|
61
|
+
const { price } = inputs;
|
|
62
|
+
const lotSize = this.marketParams.lotSize;
|
|
63
|
+
const tickSize = this.marketParams.tickSize;
|
|
64
|
+
return perpetuals_1.Perpetuals.priceToOrderPrice({
|
|
65
|
+
price,
|
|
66
|
+
lotSize,
|
|
67
|
+
tickSize,
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
this.orderPriceToPrice = (inputs) => {
|
|
71
|
+
const { orderPrice } = inputs;
|
|
72
|
+
const lotSize = this.marketParams.lotSize;
|
|
73
|
+
const tickSize = this.marketParams.tickSize;
|
|
74
|
+
return perpetuals_1.Perpetuals.orderPriceToPrice({
|
|
75
|
+
orderPrice,
|
|
76
|
+
lotSize,
|
|
77
|
+
tickSize,
|
|
78
|
+
});
|
|
135
79
|
};
|
|
136
|
-
// =========================================================================
|
|
137
|
-
// Margin / Collateral Calculations
|
|
138
|
-
// =========================================================================
|
|
139
|
-
/**
|
|
140
|
-
* Calculate the collateral required to support an order given leverage and prices.
|
|
141
|
-
*
|
|
142
|
-
* The computed collateral is based on the *remaining* unfilled size:
|
|
143
|
-
* `remaining = initialSize - filledSize`.
|
|
144
|
-
*
|
|
145
|
-
* USD requirement:
|
|
146
|
-
* ```text
|
|
147
|
-
* remainingBase * indexPrice * initialMarginRatio
|
|
148
|
-
* ```
|
|
149
|
-
* where `initialMarginRatio = 1 / leverage` (or 1 if leverage is falsy).
|
|
150
|
-
*
|
|
151
|
-
* @param inputs.leverage - Target leverage for the order (>= 1).
|
|
152
|
-
* @param inputs.orderData - Order data containing `initialSize` and `filledSize`.
|
|
153
|
-
* @param inputs.indexPrice - Index/oracle price of the base asset.
|
|
154
|
-
* @param inputs.collateralPrice - Price of the collateral asset.
|
|
155
|
-
*
|
|
156
|
-
* @returns Object with:
|
|
157
|
-
* - `collateralUsd`: required collateral in USD
|
|
158
|
-
* - `collateral`: required collateral in collateral coin units
|
|
159
|
-
*/
|
|
160
80
|
this.calcCollateralUsedForOrder = (inputs) => {
|
|
161
81
|
const { leverage, orderData, indexPrice, collateralPrice } = inputs;
|
|
162
|
-
const imr = 1 /
|
|
163
|
-
const
|
|
82
|
+
const imr = 1 / leverage;
|
|
83
|
+
// const imr = this.initialMarginRatio();
|
|
84
|
+
const collateralUsd = Number(orderData.initialSize - orderData.filledSize) *
|
|
85
|
+
this.lotSize() *
|
|
164
86
|
indexPrice *
|
|
165
87
|
imr;
|
|
166
88
|
const collateral = collateralUsd / collateralPrice;
|
|
@@ -169,64 +91,9 @@ class PerpetualsMarket extends caller_1.Caller {
|
|
|
169
91
|
collateral,
|
|
170
92
|
};
|
|
171
93
|
};
|
|
172
|
-
// =========================================================================
|
|
173
|
-
// Helpers
|
|
174
|
-
// =========================================================================
|
|
175
|
-
/**
|
|
176
|
-
* Round a price to the nearest valid tick for this market.
|
|
177
|
-
*
|
|
178
|
-
* Rounding mode:
|
|
179
|
-
* - `floor: true` => round down
|
|
180
|
-
* - `ceil: true` => round up
|
|
181
|
-
* - neither => nearest tick (`Math.round`)
|
|
182
|
-
*
|
|
183
|
-
* @param inputs.price - Raw price to round.
|
|
184
|
-
* @param inputs.floor - Force floor rounding.
|
|
185
|
-
* @param inputs.ceil - Force ceil rounding.
|
|
186
|
-
* @returns Price snapped to the market tick size.
|
|
187
|
-
*/
|
|
188
94
|
this.roundToValidPrice = (inputs) => {
|
|
189
|
-
|
|
190
|
-
return ((inputs.floor
|
|
191
|
-
? Math.floor(ticks)
|
|
192
|
-
: inputs.ceil
|
|
193
|
-
? Math.ceil(ticks)
|
|
194
|
-
: Math.round(ticks)) * this.tickSize());
|
|
95
|
+
return Math.round(inputs.price / this.tickSize()) * this.tickSize();
|
|
195
96
|
};
|
|
196
|
-
/**
|
|
197
|
-
* Round a price to the nearest valid tick as a fixed-point `bigint` (1e9 precision).
|
|
198
|
-
*
|
|
199
|
-
* This is helpful when you need the on-chain representation directly
|
|
200
|
-
* (e.g. order price fields stored in 9-decimal fixed).
|
|
201
|
-
*
|
|
202
|
-
* @param inputs.price - Raw price as a JS number.
|
|
203
|
-
* @param inputs.floor - Force floor rounding.
|
|
204
|
-
* @param inputs.ceil - Force ceil rounding.
|
|
205
|
-
* @returns Tick-snapped price scaled by `1e9`.
|
|
206
|
-
*/
|
|
207
|
-
this.roundToValidPriceBigInt = (inputs) => {
|
|
208
|
-
const scaledPrice = Number(inputs.price * __1.Casting.Fixed.fixedOneN9);
|
|
209
|
-
return ((BigInt(inputs.floor
|
|
210
|
-
? Math.floor(scaledPrice)
|
|
211
|
-
: inputs.ceil
|
|
212
|
-
? Math.ceil(scaledPrice)
|
|
213
|
-
: Math.round(scaledPrice)) /
|
|
214
|
-
this.marketParams.tickSize) *
|
|
215
|
-
this.marketParams.tickSize);
|
|
216
|
-
};
|
|
217
|
-
/**
|
|
218
|
-
* Round a base-asset size to the nearest valid lot size for this market.
|
|
219
|
-
*
|
|
220
|
-
* Rounding mode:
|
|
221
|
-
* - `floor: true` => round down
|
|
222
|
-
* - `ceil: true` => round up
|
|
223
|
-
* - neither => nearest lot (`Math.round`)
|
|
224
|
-
*
|
|
225
|
-
* @param inputs.size - Raw size in base asset units.
|
|
226
|
-
* @param inputs.floor - Force floor rounding.
|
|
227
|
-
* @param inputs.ceil - Force ceil rounding.
|
|
228
|
-
* @returns Size snapped to the market lot size.
|
|
229
|
-
*/
|
|
230
97
|
this.roundToValidSize = (inputs) => {
|
|
231
98
|
const lots = inputs.size / this.lotSize();
|
|
232
99
|
return ((inputs.floor
|
|
@@ -235,59 +102,7 @@ class PerpetualsMarket extends caller_1.Caller {
|
|
|
235
102
|
? Math.ceil(lots)
|
|
236
103
|
: Math.round(lots)) * this.lotSize());
|
|
237
104
|
};
|
|
238
|
-
/**
|
|
239
|
-
* Round a base-asset size to the nearest valid lot as a fixed-point `bigint` (1e9 precision).
|
|
240
|
-
*
|
|
241
|
-
* @param inputs.size - Raw base size as a JS number.
|
|
242
|
-
* @param inputs.floor - Force floor rounding.
|
|
243
|
-
* @param inputs.ceil - Force ceil rounding.
|
|
244
|
-
* @returns Lot-snapped size scaled by `1e9`.
|
|
245
|
-
*/
|
|
246
|
-
this.roundToValidSizeBigInt = (inputs) => {
|
|
247
|
-
const scaledSize = Number(inputs.size * __1.Casting.Fixed.fixedOneN9);
|
|
248
|
-
return ((BigInt(inputs.floor
|
|
249
|
-
? Math.floor(scaledSize)
|
|
250
|
-
: inputs.ceil
|
|
251
|
-
? Math.ceil(scaledSize)
|
|
252
|
-
: Math.round(scaledSize)) /
|
|
253
|
-
this.marketParams.lotSize) *
|
|
254
|
-
this.marketParams.lotSize);
|
|
255
|
-
};
|
|
256
|
-
/**
|
|
257
|
-
* Construct an "empty" position object for this market.
|
|
258
|
-
*
|
|
259
|
-
* Useful when an account has no open position but downstream UI/calculations
|
|
260
|
-
* expect a {@link PerpetualsPosition}-shaped object.
|
|
261
|
-
*
|
|
262
|
-
* @returns A zeroed-out {@link PerpetualsPosition} for `this.marketId`.
|
|
263
|
-
*/
|
|
264
|
-
this.emptyPosition = () => {
|
|
265
|
-
return {
|
|
266
|
-
marketId: this.marketId,
|
|
267
|
-
collateral: 0,
|
|
268
|
-
collateralUsd: 0,
|
|
269
|
-
baseAssetAmount: 0,
|
|
270
|
-
quoteAssetNotionalAmount: 0,
|
|
271
|
-
cumFundingRateLong: this.marketData.marketState.cumFundingRateLong,
|
|
272
|
-
cumFundingRateShort: this.marketData.marketState.cumFundingRateShort,
|
|
273
|
-
asksQuantity: 0,
|
|
274
|
-
bidsQuantity: 0,
|
|
275
|
-
pendingOrders: [],
|
|
276
|
-
makerFee: 1, // 100% (placeholder default)
|
|
277
|
-
takerFee: 1, // 100% (placeholder default)
|
|
278
|
-
leverage: 1,
|
|
279
|
-
entryPrice: 0,
|
|
280
|
-
freeCollateral: 0,
|
|
281
|
-
freeMarginUsd: 0,
|
|
282
|
-
liquidationPrice: 0,
|
|
283
|
-
marginRatio: 1,
|
|
284
|
-
unrealizedFundingsUsd: 0,
|
|
285
|
-
unrealizedPnlUsd: 0,
|
|
286
|
-
};
|
|
287
|
-
};
|
|
288
105
|
this.marketId = marketData.objectId;
|
|
289
|
-
this.indexPrice = marketData.indexPrice;
|
|
290
|
-
this.collateralPrice = marketData.collateralPrice;
|
|
291
106
|
this.collateralCoinType = marketData.collateralCoinType;
|
|
292
107
|
this.marketParams = marketData.marketParams;
|
|
293
108
|
this.marketState = marketData.marketState;
|
|
@@ -295,201 +110,53 @@ class PerpetualsMarket extends caller_1.Caller {
|
|
|
295
110
|
// =========================================================================
|
|
296
111
|
// Inspections
|
|
297
112
|
// =========================================================================
|
|
298
|
-
/**
|
|
299
|
-
* Fetch the mid price for this market’s orderbook.
|
|
300
|
-
*
|
|
301
|
-
* This is a convenience endpoint that returns only:
|
|
302
|
-
* - `midPrice`: midpoint between best bid and best ask, or `undefined`
|
|
303
|
-
* if the orderbook is empty or unavailable.
|
|
304
|
-
*
|
|
305
|
-
* @returns `{ midPrice }`.
|
|
306
|
-
*
|
|
307
|
-
* @example
|
|
308
|
-
* ```ts
|
|
309
|
-
* const { midPrice } = await market.getOrderbookMidPrice();
|
|
310
|
-
* ```
|
|
311
|
-
*/
|
|
312
113
|
// NOTE: should this be entirely removed since data already in orderbook function ?
|
|
313
|
-
|
|
314
|
-
return this.fetchApi("
|
|
315
|
-
marketId: this.marketId,
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
319
|
-
* Fetch the 24-hour statistics for this specific market.
|
|
320
|
-
*
|
|
321
|
-
* Under the hood, this calls {@link Perpetuals.getMarkets24hrStats} and
|
|
322
|
-
* returns the first (and only) entry.
|
|
323
|
-
*
|
|
324
|
-
* @returns {@link PerpetualsMarket24hrStats}.
|
|
325
|
-
*
|
|
326
|
-
* @remarks
|
|
327
|
-
* This method creates a new {@link Perpetuals} instance using `this.config`.
|
|
328
|
-
* If you need shared Provider behavior, prefer calling `perps.getMarkets24hrStats`
|
|
329
|
-
* directly with the same Provider you initialized.
|
|
330
|
-
*/
|
|
331
|
-
get24hrStats() {
|
|
332
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
333
|
-
const res = yield new perpetuals_1.Perpetuals(this.config).getMarkets24hrStats({
|
|
334
|
-
marketIds: [this.marketId],
|
|
335
|
-
});
|
|
336
|
-
return res.marketsStats[0];
|
|
337
|
-
});
|
|
114
|
+
getOrderbookPrice() {
|
|
115
|
+
return this.fetchApi("orderbook-price");
|
|
338
116
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
*
|
|
342
|
-
* @returns Object containing `orderbook`.
|
|
343
|
-
*
|
|
344
|
-
* @example
|
|
345
|
-
* ```ts
|
|
346
|
-
* const { orderbook } = await market.getOrderbook();
|
|
347
|
-
* console.log(orderbook.bids[0], orderbook.asks[0]);
|
|
348
|
-
* ```
|
|
349
|
-
*/
|
|
350
|
-
// TODO: move to `Perpetuals` class ?
|
|
351
|
-
getOrderbook() {
|
|
352
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
353
|
-
const { orderbooks } = yield this.fetchApi("markets/orderbooks", {
|
|
354
|
-
marketIds: [this.marketId],
|
|
355
|
-
});
|
|
356
|
-
return {
|
|
357
|
-
orderbook: orderbooks[0].orderbook,
|
|
358
|
-
};
|
|
359
|
-
});
|
|
117
|
+
get24hrVolume() {
|
|
118
|
+
return this.fetchApi("24hr-volume");
|
|
360
119
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
*
|
|
364
|
-
* Unlike {@link PerpetualsAccount.getPlaceMarketOrderPreview}, this version:
|
|
365
|
-
* - Calls `account/previews/place-market-order`
|
|
366
|
-
* - Explicitly sets `accountId: undefined`, allowing a “generic” preview that
|
|
367
|
-
* doesn’t rely on a specific account’s on-chain positions/collateral.
|
|
368
|
-
*
|
|
369
|
-
* @param inputs - {@link SdkPerpetualsPlaceMarketOrderPreviewInputs}.
|
|
370
|
-
* @param abortSignal - Optional abort signal to cancel the request.
|
|
371
|
-
*
|
|
372
|
-
* @returns Either `{ error }` or a preview containing the simulated updated position,
|
|
373
|
-
* slippage, filled/posted sizes, collateral change, and execution price.
|
|
374
|
-
*/
|
|
375
|
-
getPlaceMarketOrderPreview(inputs, abortSignal) {
|
|
376
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
377
|
-
return this.fetchApi("account/previews/place-market-order", Object.assign(Object.assign({}, inputs), { accountId: undefined }), abortSignal);
|
|
378
|
-
});
|
|
120
|
+
getPrice24hrsAgo() {
|
|
121
|
+
return this.fetchApi("price-24hrs-ago");
|
|
379
122
|
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
*
|
|
383
|
-
* Similar to {@link getPlaceMarketOrderPreview}, this uses:
|
|
384
|
-
* - `account/previews/place-limit-order`
|
|
385
|
-
* - `accountId: undefined`
|
|
386
|
-
*
|
|
387
|
-
* @param inputs - {@link SdkPerpetualsPlaceLimitOrderPreviewInputs}.
|
|
388
|
-
* @param abortSignal - Optional abort signal to cancel the request.
|
|
389
|
-
*
|
|
390
|
-
* @returns Either `{ error }` or a preview describing the simulated post-order state.
|
|
391
|
-
*/
|
|
392
|
-
getPlaceLimitOrderPreview(inputs, abortSignal) {
|
|
393
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
394
|
-
return this.fetchApi("account/previews/place-limit-order", Object.assign(Object.assign({}, inputs), { accountId: undefined }), abortSignal);
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
// =========================================================================
|
|
398
|
-
// Order History
|
|
399
|
-
// =========================================================================
|
|
400
|
-
/**
|
|
401
|
-
* Fetch paginated order history for this market.
|
|
402
|
-
*
|
|
403
|
-
* This is market-wide (public) history, not scoped to any account.
|
|
404
|
-
*
|
|
405
|
-
* @param inputs.beforeTimestampCursor - Optional pagination cursor.
|
|
406
|
-
* @param inputs.limit - Optional page size.
|
|
407
|
-
*
|
|
408
|
-
* @returns {@link ApiPerpetualsMarketOrderHistoryResponse} containing:
|
|
409
|
-
* - `orders`
|
|
410
|
-
* - `nextBeforeTimestampCursor`
|
|
411
|
-
*/
|
|
412
|
-
getOrderHistory(inputs) {
|
|
413
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
414
|
-
return this.fetchApi("market/order-history", Object.assign(Object.assign({}, inputs), { marketId: this.marketId }));
|
|
415
|
-
});
|
|
123
|
+
getOrderbook() {
|
|
124
|
+
return this.fetchApi("orderbook");
|
|
416
125
|
}
|
|
417
126
|
// =========================================================================
|
|
418
|
-
//
|
|
127
|
+
// Trade History
|
|
419
128
|
// =========================================================================
|
|
420
|
-
|
|
421
|
-
* Fetch the current base and collateral prices for this market.
|
|
422
|
-
*
|
|
423
|
-
* Internally calls {@link Perpetuals.getPrices} and returns the first result.
|
|
424
|
-
*
|
|
425
|
-
* @returns `{ basePrice, collateralPrice }`.
|
|
426
|
-
*
|
|
427
|
-
* @remarks
|
|
428
|
-
* This method instantiates a new {@link Perpetuals} client using `this.config`.
|
|
429
|
-
* If you rely on a shared Provider, call `perps.getPrices(...)` directly instead.
|
|
430
|
-
*/
|
|
431
|
-
getPrices() {
|
|
129
|
+
getTradeHistory(inputs) {
|
|
432
130
|
return __awaiter(this, void 0, void 0, function* () {
|
|
433
|
-
return (
|
|
434
|
-
marketIds: [this.marketId],
|
|
435
|
-
})).marketsPrices[0];
|
|
131
|
+
return this.fetchApi(`trade-history`, inputs);
|
|
436
132
|
});
|
|
437
133
|
}
|
|
438
134
|
// =========================================================================
|
|
439
135
|
// Value Conversions
|
|
440
136
|
// =========================================================================
|
|
441
|
-
/**
|
|
442
|
-
* Get the base-asset lot size for this market as a `number`.
|
|
443
|
-
*
|
|
444
|
-
* Order sizes must be multiples of this lot size.
|
|
445
|
-
*
|
|
446
|
-
* @returns Lot size in base asset units.
|
|
447
|
-
*/
|
|
448
137
|
lotSize() {
|
|
449
138
|
return perpetuals_1.Perpetuals.lotOrTickSizeToNumber(this.marketParams.lotSize);
|
|
450
139
|
}
|
|
451
|
-
/**
|
|
452
|
-
* Get the minimal price tick size for this market as a `number`.
|
|
453
|
-
*
|
|
454
|
-
* Limit prices must be multiples of this tick size.
|
|
455
|
-
*
|
|
456
|
-
* @returns Tick size in quote units (e.g. USD).
|
|
457
|
-
*/
|
|
458
140
|
tickSize() {
|
|
459
141
|
return perpetuals_1.Perpetuals.lotOrTickSizeToNumber(this.marketParams.tickSize);
|
|
460
142
|
}
|
|
461
|
-
/**
|
|
462
|
-
* Get the maximum theoretical leverage for this market.
|
|
463
|
-
*
|
|
464
|
-
* Computed as:
|
|
465
|
-
* ```ts
|
|
466
|
-
* 1 / marginRatioInitial
|
|
467
|
-
* ```
|
|
468
|
-
*
|
|
469
|
-
* @returns Maximum leverage.
|
|
470
|
-
*/
|
|
471
143
|
maxLeverage() {
|
|
472
|
-
return 1 /
|
|
144
|
+
return (1 /
|
|
145
|
+
__1.Casting.IFixed.numberFromIFixed(this.marketParams.marginRatioInitial));
|
|
473
146
|
}
|
|
474
|
-
/**
|
|
475
|
-
* Get the initial margin ratio for this market.
|
|
476
|
-
*
|
|
477
|
-
* This is the minimum margin required when opening a position.
|
|
478
|
-
*
|
|
479
|
-
* @returns Initial margin ratio as a fraction (e.g. 0.05 = 20x).
|
|
480
|
-
*/
|
|
481
147
|
initialMarginRatio() {
|
|
482
|
-
return this.marketParams.marginRatioInitial;
|
|
148
|
+
return __1.Casting.IFixed.numberFromIFixed(this.marketParams.marginRatioInitial);
|
|
483
149
|
}
|
|
484
|
-
/**
|
|
485
|
-
* Get the maintenance margin ratio for this market.
|
|
486
|
-
*
|
|
487
|
-
* Falling below this ratio may trigger liquidation.
|
|
488
|
-
*
|
|
489
|
-
* @returns Maintenance margin ratio as a fraction.
|
|
490
|
-
*/
|
|
491
150
|
maintenanceMarginRatio() {
|
|
492
|
-
return this.marketParams.marginRatioMaintenance;
|
|
151
|
+
return __1.Casting.IFixed.numberFromIFixed(this.marketParams.marginRatioMaintenance);
|
|
152
|
+
}
|
|
153
|
+
// =========================================================================
|
|
154
|
+
// Helpers
|
|
155
|
+
// =========================================================================
|
|
156
|
+
orderPrice(inputs) {
|
|
157
|
+
const { orderId } = inputs;
|
|
158
|
+
const orderPrice = utils_1.PerpetualsOrderUtils.price(orderId);
|
|
159
|
+
return this.orderPriceToPrice({ orderPrice });
|
|
493
160
|
}
|
|
494
161
|
}
|
|
495
162
|
exports.PerpetualsMarket = PerpetualsMarket;
|