aftermath-ts-sdk 1.3.27 → 1.3.28
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/objectsApiHelpers.d.ts.map +1 -1
- package/dist/general/apiHelpers/objectsApiHelpers.js +5 -4
- package/dist/general/apiHelpers/transactionsApiHelpers.d.ts +3 -0
- package/dist/general/apiHelpers/transactionsApiHelpers.d.ts.map +1 -1
- package/dist/general/apiHelpers/transactionsApiHelpers.js +14 -2
- package/dist/general/providers/aftermath.d.ts +19 -19
- package/dist/general/providers/aftermath.d.ts.map +1 -1
- package/dist/general/providers/aftermath.js +20 -20
- package/dist/general/providers/aftermathApi.d.ts +0 -5
- package/dist/general/providers/aftermathApi.d.ts.map +1 -1
- package/dist/general/providers/aftermathApi.js +0 -5
- 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 +6 -10
- package/dist/general/types/configTypes.d.ts.map +1 -1
- package/dist/general/types/generalTypes.d.ts +8 -0
- 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 +34 -2
- package/dist/general/utils/caller.d.ts.map +1 -1
- package/dist/general/utils/caller.js +111 -12
- package/dist/general/utils/casting.d.ts +4 -5
- package/dist/general/utils/casting.d.ts.map +1 -1
- package/dist/general/utils/casting.js +4 -5
- package/dist/general/utils/helpers.d.ts +1 -0
- package/dist/general/utils/helpers.d.ts.map +1 -1
- package/dist/general/utils/helpers.js +40 -9
- 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 +5 -1
- package/dist/packages/coin/coin.d.ts.map +1 -1
- package/dist/packages/coin/coin.js +7 -1
- 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 +9 -17
- package/dist/packages/faucet/api/faucetApi.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApi.js +44 -49
- package/dist/packages/faucet/api/faucetApiCasting.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApiCasting.js +10 -7
- package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts +1 -4
- 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 +2 -3
- 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/perpetuals/api/perpetualsApi.d.ts +12 -203
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApi.js +926 -615
- package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts +10 -21
- package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApiCasting.js +260 -238
- package/dist/packages/perpetuals/index.d.ts +1 -0
- package/dist/packages/perpetuals/index.d.ts.map +1 -1
- package/dist/packages/perpetuals/index.js +1 -0
- package/dist/packages/perpetuals/perpetuals.d.ts +830 -28
- package/dist/packages/perpetuals/perpetuals.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetuals.js +1013 -70
- package/dist/packages/perpetuals/perpetualsAccount.d.ts +754 -170
- package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsAccount.js +1270 -312
- package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts +82 -149
- package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsMarket.d.ts +327 -27
- package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsMarket.js +392 -79
- package/dist/packages/perpetuals/perpetualsTypes.d.ts +2662 -318
- package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsTypes.js +92 -82
- package/dist/packages/perpetuals/perpetualsVault.d.ts +438 -0
- package/dist/packages/perpetuals/perpetualsVault.d.ts.map +1 -0
- package/dist/packages/perpetuals/perpetualsVault.js +601 -0
- 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/rewards/index.d.ts +2 -0
- package/dist/packages/rewards/index.d.ts.map +1 -0
- package/dist/packages/rewards/index.js +17 -0
- package/dist/packages/rewards/rewards.d.ts +21 -0
- package/dist/packages/rewards/rewards.d.ts.map +1 -0
- package/dist/packages/rewards/rewards.js +65 -0
- package/dist/packages/rewards/rewardsTypes.d.ts +169 -0
- package/dist/packages/rewards/rewardsTypes.d.ts.map +1 -0
- package/dist/packages/router/api/routerApiCasting.js +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 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/package.json +1 -1
- package/dist/packages/oracle/api/oracleApi.d.ts +0 -21
- package/dist/packages/oracle/api/oracleApi.d.ts.map +0 -1
- package/dist/packages/oracle/api/oracleApi.js +0 -76
- package/dist/packages/oracle/api/oracleApiCasting.d.ts +0 -6
- package/dist/packages/oracle/api/oracleApiCasting.d.ts.map +0 -1
- package/dist/packages/oracle/api/oracleApiCasting.js +0 -25
- package/dist/packages/oracle/oracle.d.ts +0 -15
- package/dist/packages/oracle/oracle.d.ts.map +0 -1
- package/dist/packages/oracle/oracle.js +0 -46
- package/dist/packages/oracle/oracleCastingTypes.d.ts +0 -11
- package/dist/packages/oracle/oracleCastingTypes.d.ts.map +0 -1
- package/dist/packages/oracle/oracleTypes.d.ts +0 -10
- package/dist/packages/oracle/oracleTypes.d.ts.map +0 -1
- package/dist/packages/oracle/oracleTypes.js +0 -2
- /package/dist/packages/{oracle/oracleCastingTypes.js → rewards/rewardsTypes.js} +0 -0
|
@@ -12,77 +12,155 @@ 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");
|
|
16
15
|
const perpetuals_1 = require("./perpetuals");
|
|
17
|
-
|
|
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
|
+
*/
|
|
18
45
|
class PerpetualsMarket extends caller_1.Caller {
|
|
19
46
|
// =========================================================================
|
|
20
47
|
// Constructor
|
|
21
48
|
// =========================================================================
|
|
22
|
-
|
|
23
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Create a new {@link PerpetualsMarket} wrapper from raw market data.
|
|
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");
|
|
24
62
|
this.marketData = marketData;
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
63
|
+
this.Provider = Provider;
|
|
64
|
+
/**
|
|
65
|
+
* Compute the maximum order size that can be placed by a given account
|
|
66
|
+
* in this market, under optional leverage and price assumptions.
|
|
67
|
+
*
|
|
68
|
+
* This is a common frontend helper for:
|
|
69
|
+
* - "max size" buttons
|
|
70
|
+
* - input validation against risk limits
|
|
71
|
+
*
|
|
72
|
+
* **Note:** This is routed through the `account` namespace because it depends on
|
|
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 }));
|
|
35
94
|
});
|
|
36
95
|
// =========================================================================
|
|
37
|
-
//
|
|
96
|
+
// Funding / Timing
|
|
38
97
|
// =========================================================================
|
|
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
|
+
*/
|
|
39
108
|
this.timeUntilNextFundingMs = () => {
|
|
40
109
|
return this.nextFundingTimeMs() - Date.now();
|
|
41
110
|
};
|
|
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
|
+
*/
|
|
42
120
|
this.nextFundingTimeMs = () => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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;
|
|
121
|
+
return this.marketData.nextFundingTimestampMs >
|
|
122
|
+
BigInt(Number.MAX_SAFE_INTEGER)
|
|
123
|
+
? Number.MAX_SAFE_INTEGER
|
|
124
|
+
: Number(this.marketData.nextFundingTimestampMs);
|
|
59
125
|
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
});
|
|
126
|
+
/**
|
|
127
|
+
* Estimated funding rate per period for this market.
|
|
128
|
+
*
|
|
129
|
+
* This is read directly from `marketData.estimatedFundingRate`.
|
|
130
|
+
*
|
|
131
|
+
* @returns Estimated funding rate as a fraction (e.g. `0.01` = 1%).
|
|
132
|
+
*/
|
|
133
|
+
this.estimatedFundingRate = () => {
|
|
134
|
+
return this.marketData.estimatedFundingRate;
|
|
79
135
|
};
|
|
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
|
+
*/
|
|
80
160
|
this.calcCollateralUsedForOrder = (inputs) => {
|
|
81
161
|
const { leverage, orderData, indexPrice, collateralPrice } = inputs;
|
|
82
|
-
const imr = 1 / leverage;
|
|
83
|
-
|
|
84
|
-
const collateralUsd = Number(orderData.initialSize - orderData.filledSize) *
|
|
85
|
-
this.lotSize() *
|
|
162
|
+
const imr = 1 / (leverage || 1);
|
|
163
|
+
const collateralUsd = (Number(orderData.currentSize) / __1.Casting.Fixed.fixedOneN9) *
|
|
86
164
|
indexPrice *
|
|
87
165
|
imr;
|
|
88
166
|
const collateral = collateralUsd / collateralPrice;
|
|
@@ -91,9 +169,64 @@ class PerpetualsMarket extends caller_1.Caller {
|
|
|
91
169
|
collateral,
|
|
92
170
|
};
|
|
93
171
|
};
|
|
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
|
+
*/
|
|
94
188
|
this.roundToValidPrice = (inputs) => {
|
|
95
|
-
|
|
189
|
+
const ticks = inputs.price / this.tickSize();
|
|
190
|
+
return ((inputs.floor
|
|
191
|
+
? Math.floor(ticks)
|
|
192
|
+
: inputs.ceil
|
|
193
|
+
? Math.ceil(ticks)
|
|
194
|
+
: Math.round(ticks)) * this.tickSize());
|
|
195
|
+
};
|
|
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);
|
|
96
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
|
+
*/
|
|
97
230
|
this.roundToValidSize = (inputs) => {
|
|
98
231
|
const lots = inputs.size / this.lotSize();
|
|
99
232
|
return ((inputs.floor
|
|
@@ -102,7 +235,59 @@ class PerpetualsMarket extends caller_1.Caller {
|
|
|
102
235
|
? Math.ceil(lots)
|
|
103
236
|
: Math.round(lots)) * this.lotSize());
|
|
104
237
|
};
|
|
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
|
+
};
|
|
105
288
|
this.marketId = marketData.objectId;
|
|
289
|
+
this.indexPrice = marketData.indexPrice;
|
|
290
|
+
this.collateralPrice = marketData.collateralPrice;
|
|
106
291
|
this.collateralCoinType = marketData.collateralCoinType;
|
|
107
292
|
this.marketParams = marketData.marketParams;
|
|
108
293
|
this.marketState = marketData.marketState;
|
|
@@ -110,53 +295,181 @@ class PerpetualsMarket extends caller_1.Caller {
|
|
|
110
295
|
// =========================================================================
|
|
111
296
|
// Inspections
|
|
112
297
|
// =========================================================================
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Fetch the 24-hour volume and price change statistics for this market.
|
|
300
|
+
*
|
|
301
|
+
* Under the hood, this calls {@link Perpetuals.getMarkets24hrStats} and
|
|
302
|
+
* returns the first (and only) entry.
|
|
303
|
+
*
|
|
304
|
+
* @returns {@link PerpetualsMarket24hrStats}.
|
|
305
|
+
*
|
|
306
|
+
* @remarks
|
|
307
|
+
* This method creates a new {@link Perpetuals} instance using `this.config`.
|
|
308
|
+
* If you need shared Provider behavior, prefer calling `perps.getMarkets24hrStats`
|
|
309
|
+
* directly with the same Provider you initialized.
|
|
310
|
+
*/
|
|
311
|
+
get24hrStats() {
|
|
312
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
313
|
+
const res = yield new perpetuals_1.Perpetuals(this.config).getMarkets24hrStats({
|
|
314
|
+
marketIds: [this.marketId],
|
|
315
|
+
});
|
|
316
|
+
return res.marketsStats[0];
|
|
317
|
+
});
|
|
116
318
|
}
|
|
117
|
-
|
|
118
|
-
|
|
319
|
+
/**
|
|
320
|
+
* Fetch the full orderbook snapshot for this market.
|
|
321
|
+
*
|
|
322
|
+
* @returns Object containing `orderbook`.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* const { orderbook } = await market.getOrderbook();
|
|
327
|
+
* console.log(orderbook.bids[0], orderbook.asks[0]);
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
// TODO: move to `Perpetuals` class ?
|
|
331
|
+
getOrderbook() {
|
|
332
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
333
|
+
const { orderbooks } = yield this.fetchApi("markets/orderbooks", {
|
|
334
|
+
marketIds: [this.marketId],
|
|
335
|
+
});
|
|
336
|
+
return {
|
|
337
|
+
orderbook: orderbooks[0].orderbook,
|
|
338
|
+
};
|
|
339
|
+
});
|
|
119
340
|
}
|
|
120
|
-
|
|
121
|
-
|
|
341
|
+
/**
|
|
342
|
+
* Market-level preview of placing a market order.
|
|
343
|
+
*
|
|
344
|
+
* Unlike {@link PerpetualsAccount.getPlaceMarketOrderPreview}, this version:
|
|
345
|
+
* - Calls `account/previews/place-market-order`
|
|
346
|
+
* - Explicitly sets `accountId: undefined`, allowing a “generic” preview that
|
|
347
|
+
* doesn’t rely on a specific account’s on-chain positions/collateral.
|
|
348
|
+
*
|
|
349
|
+
* @param inputs - {@link SdkPerpetualsPlaceMarketOrderPreviewInputs}.
|
|
350
|
+
* @param abortSignal - Optional abort signal to cancel the request.
|
|
351
|
+
*
|
|
352
|
+
* @returns Either `{ error }` or a preview containing the simulated updated position,
|
|
353
|
+
* slippage, filled/posted sizes, collateral change, and execution price.
|
|
354
|
+
*/
|
|
355
|
+
getPlaceMarketOrderPreview(inputs, abortSignal) {
|
|
356
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
357
|
+
return this.fetchApi("account/previews/place-market-order", Object.assign(Object.assign({}, inputs), { accountId: undefined }), abortSignal);
|
|
358
|
+
});
|
|
122
359
|
}
|
|
123
|
-
|
|
124
|
-
|
|
360
|
+
/**
|
|
361
|
+
* Market-level preview of placing a limit order.
|
|
362
|
+
*
|
|
363
|
+
* Similar to {@link getPlaceMarketOrderPreview}, this uses:
|
|
364
|
+
* - `account/previews/place-limit-order`
|
|
365
|
+
* - `accountId: undefined`
|
|
366
|
+
*
|
|
367
|
+
* @param inputs - {@link SdkPerpetualsPlaceLimitOrderPreviewInputs}.
|
|
368
|
+
* @param abortSignal - Optional abort signal to cancel the request.
|
|
369
|
+
*
|
|
370
|
+
* @returns Either `{ error }` or a preview describing the simulated post-order state.
|
|
371
|
+
*/
|
|
372
|
+
getPlaceLimitOrderPreview(inputs, abortSignal) {
|
|
373
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
374
|
+
return this.fetchApi("account/previews/place-limit-order", Object.assign(Object.assign({}, inputs), { accountId: undefined }), abortSignal);
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
// =========================================================================
|
|
378
|
+
// Order History
|
|
379
|
+
// =========================================================================
|
|
380
|
+
/**
|
|
381
|
+
* Fetch paginated order history for this market.
|
|
382
|
+
*
|
|
383
|
+
* This is market-wide (public) history, not scoped to any account.
|
|
384
|
+
*
|
|
385
|
+
* @param inputs.beforeTimestampCursor - Optional pagination cursor.
|
|
386
|
+
* @param inputs.limit - Optional page size.
|
|
387
|
+
*
|
|
388
|
+
* @returns {@link ApiPerpetualsMarketOrderHistoryResponse} containing:
|
|
389
|
+
* - `orders`
|
|
390
|
+
* - `nextBeforeTimestampCursor`
|
|
391
|
+
*/
|
|
392
|
+
getOrderHistory(inputs) {
|
|
393
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
394
|
+
return this.fetchApi("market/order-history", Object.assign(Object.assign({}, inputs), { marketId: this.marketId }));
|
|
395
|
+
});
|
|
125
396
|
}
|
|
126
397
|
// =========================================================================
|
|
127
|
-
//
|
|
398
|
+
// Prices
|
|
128
399
|
// =========================================================================
|
|
129
|
-
|
|
400
|
+
/**
|
|
401
|
+
* Fetch the current prices for this market.
|
|
402
|
+
*
|
|
403
|
+
* Internally calls {@link Perpetuals.getPrices} and returns the first result.
|
|
404
|
+
*
|
|
405
|
+
* @returns `{ marketId, basePrice, collateralPrice, midPrice, markPrice }`.
|
|
406
|
+
*
|
|
407
|
+
* @remarks
|
|
408
|
+
* This method instantiates a new {@link Perpetuals} client using `this.config`.
|
|
409
|
+
* If you rely on a shared Provider, call `perps.getPrices(...)` directly instead.
|
|
410
|
+
*/
|
|
411
|
+
getPrices() {
|
|
130
412
|
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
-
return this.
|
|
413
|
+
return (yield new perpetuals_1.Perpetuals(this.config).getPrices({
|
|
414
|
+
marketIds: [this.marketId],
|
|
415
|
+
})).marketsPrices[0];
|
|
132
416
|
});
|
|
133
417
|
}
|
|
134
418
|
// =========================================================================
|
|
135
419
|
// Value Conversions
|
|
136
420
|
// =========================================================================
|
|
421
|
+
/**
|
|
422
|
+
* Get the base-asset lot size for this market as a `number`.
|
|
423
|
+
*
|
|
424
|
+
* Order sizes must be multiples of this lot size.
|
|
425
|
+
*
|
|
426
|
+
* @returns Lot size in base asset units.
|
|
427
|
+
*/
|
|
137
428
|
lotSize() {
|
|
138
429
|
return perpetuals_1.Perpetuals.lotOrTickSizeToNumber(this.marketParams.lotSize);
|
|
139
430
|
}
|
|
431
|
+
/**
|
|
432
|
+
* Get the minimal price tick size for this market as a `number`.
|
|
433
|
+
*
|
|
434
|
+
* Limit prices must be multiples of this tick size.
|
|
435
|
+
*
|
|
436
|
+
* @returns Tick size in quote units (e.g. USD).
|
|
437
|
+
*/
|
|
140
438
|
tickSize() {
|
|
141
439
|
return perpetuals_1.Perpetuals.lotOrTickSizeToNumber(this.marketParams.tickSize);
|
|
142
440
|
}
|
|
441
|
+
/**
|
|
442
|
+
* Get the maximum theoretical leverage for this market.
|
|
443
|
+
*
|
|
444
|
+
* Computed as:
|
|
445
|
+
* ```ts
|
|
446
|
+
* 1 / marginRatioInitial
|
|
447
|
+
* ```
|
|
448
|
+
*
|
|
449
|
+
* @returns Maximum leverage.
|
|
450
|
+
*/
|
|
143
451
|
maxLeverage() {
|
|
144
|
-
return
|
|
145
|
-
__1.Casting.IFixed.numberFromIFixed(this.marketParams.marginRatioInitial));
|
|
452
|
+
return 1 / this.marketParams.marginRatioInitial;
|
|
146
453
|
}
|
|
454
|
+
/**
|
|
455
|
+
* Get the initial margin ratio for this market.
|
|
456
|
+
*
|
|
457
|
+
* This is the minimum margin required when opening a position.
|
|
458
|
+
*
|
|
459
|
+
* @returns Initial margin ratio as a fraction (e.g. 0.05 = 20x).
|
|
460
|
+
*/
|
|
147
461
|
initialMarginRatio() {
|
|
148
|
-
return
|
|
462
|
+
return this.marketParams.marginRatioInitial;
|
|
149
463
|
}
|
|
464
|
+
/**
|
|
465
|
+
* Get the maintenance margin ratio for this market.
|
|
466
|
+
*
|
|
467
|
+
* Falling below this ratio may trigger liquidation.
|
|
468
|
+
*
|
|
469
|
+
* @returns Maintenance margin ratio as a fraction.
|
|
470
|
+
*/
|
|
150
471
|
maintenanceMarginRatio() {
|
|
151
|
-
return
|
|
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 });
|
|
472
|
+
return this.marketParams.marginRatioMaintenance;
|
|
160
473
|
}
|
|
161
474
|
}
|
|
162
475
|
exports.PerpetualsMarket = PerpetualsMarket;
|