@drift-labs/sdk 0.2.0-master.24 → 0.2.0-master.26
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/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +8 -6
- package/lib/addresses/pda.js +35 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +90 -54
- package/lib/clearingHouse.d.ts +71 -42
- package/lib/clearingHouse.js +765 -282
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +19 -17
- package/lib/clearingHouseUser.js +145 -123
- package/lib/config.d.ts +7 -7
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/types.d.ts +4 -2
- package/lib/events/types.js +2 -0
- package/lib/examples/makeTradeExample.js +18 -6
- package/lib/idl/clearing_house.json +1128 -347
- package/lib/index.d.ts +6 -3
- package/lib/index.js +6 -3
- package/lib/math/amm.d.ts +5 -3
- package/lib/math/amm.js +42 -4
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +148 -39
- package/lib/types.js +37 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +56 -42
- package/src/admin.js +1284 -0
- package/src/admin.ts +149 -75
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1097 -380
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +237 -172
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +30 -30
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.js +117 -0
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +7 -1
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +27 -6
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1128 -347
- package/src/index.js +75 -0
- package/src/index.ts +6 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +73 -5
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +131 -39
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
package/lib/clearingHouseUser.js
CHANGED
|
@@ -5,7 +5,7 @@ const types_1 = require("./types");
|
|
|
5
5
|
const position_1 = require("./math/position");
|
|
6
6
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
7
7
|
const _1 = require(".");
|
|
8
|
-
const
|
|
8
|
+
const spotBalance_1 = require("./math/spotBalance");
|
|
9
9
|
const margin_1 = require("./math/margin");
|
|
10
10
|
const pollingUserAccountSubscriber_1 = require("./accounts/pollingUserAccountSubscriber");
|
|
11
11
|
const webSocketUserAccountSubscriber_1 = require("./accounts/webSocketUserAccountSubscriber");
|
|
@@ -59,11 +59,12 @@ class ClearingHouseUser {
|
|
|
59
59
|
* @returns userPosition
|
|
60
60
|
*/
|
|
61
61
|
getUserPosition(marketIndex) {
|
|
62
|
-
return this.getUserAccount().
|
|
62
|
+
return this.getUserAccount().perpPositions.find((position) => position.marketIndex.eq(marketIndex));
|
|
63
63
|
}
|
|
64
64
|
getEmptyPosition(marketIndex) {
|
|
65
65
|
return {
|
|
66
66
|
baseAssetAmount: numericConstants_1.ZERO,
|
|
67
|
+
remainderBaseAssetAmount: numericConstants_1.ZERO,
|
|
67
68
|
lastCumulativeFundingRate: numericConstants_1.ZERO,
|
|
68
69
|
marketIndex,
|
|
69
70
|
quoteAssetAmount: numericConstants_1.ZERO,
|
|
@@ -105,12 +106,14 @@ class ClearingHouseUser {
|
|
|
105
106
|
}
|
|
106
107
|
/**
|
|
107
108
|
* calculates the market position if the lp position was settled
|
|
108
|
-
* @returns : userPosition
|
|
109
|
+
* @returns : the settled userPosition
|
|
110
|
+
* @returns : the dust base asset amount (ie, < stepsize)
|
|
111
|
+
* @returns : pnl from settle
|
|
109
112
|
*/
|
|
110
113
|
getSettledLPPosition(marketIndex) {
|
|
111
114
|
const _position = this.getUserPosition(marketIndex);
|
|
112
115
|
const position = this.getClonedPosition(_position);
|
|
113
|
-
const market = this.clearingHouse.
|
|
116
|
+
const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
|
|
114
117
|
const nShares = position.lpShares;
|
|
115
118
|
const deltaBaa = market.amm.marketPositionPerLp.baseAssetAmount
|
|
116
119
|
.sub(position.lastNetBaseAssetAmountPerLp)
|
|
@@ -124,24 +127,22 @@ class ClearingHouseUser {
|
|
|
124
127
|
const sign = { true: new _1.BN(1), false: new _1.BN(-1) }[v.gte(numericConstants_1.ZERO).toString()];
|
|
125
128
|
return sign;
|
|
126
129
|
}
|
|
127
|
-
|
|
128
|
-
.abs()
|
|
129
|
-
.
|
|
130
|
-
|
|
131
|
-
const _standardizedBaa = deltaBaa.sub(remainder);
|
|
132
|
-
let remainderBaa;
|
|
133
|
-
if (_standardizedBaa.abs().gte(market.amm.baseAssetAmountStepSize)) {
|
|
134
|
-
remainderBaa = remainder;
|
|
130
|
+
function standardize(amount, stepsize) {
|
|
131
|
+
const remainder = amount.abs().mod(stepsize).mul(sign(amount));
|
|
132
|
+
const standardizedAmount = amount.sub(remainder);
|
|
133
|
+
return [standardizedAmount, remainder];
|
|
135
134
|
}
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
const [standardizedBaa, remainderBaa] = standardize(deltaBaa, market.amm.baseAssetAmountStepSize);
|
|
136
|
+
position.remainderBaseAssetAmount =
|
|
137
|
+
position.remainderBaseAssetAmount.add(remainderBaa);
|
|
138
|
+
if (position.remainderBaseAssetAmount
|
|
139
|
+
.abs()
|
|
140
|
+
.gte(market.amm.baseAssetAmountStepSize)) {
|
|
141
|
+
const [newStandardizedBaa, newRemainderBaa] = standardize(position.remainderBaseAssetAmount, market.amm.baseAssetAmountStepSize);
|
|
142
|
+
position.baseAssetAmount =
|
|
143
|
+
position.baseAssetAmount.add(newStandardizedBaa);
|
|
144
|
+
position.remainderBaseAssetAmount = newRemainderBaa;
|
|
138
145
|
}
|
|
139
|
-
const standardizedBaa = deltaBaa.sub(remainderBaa);
|
|
140
|
-
const reaminderPerLP = remainderBaa.mul(numericConstants_1.AMM_RESERVE_PRECISION).div(nShares);
|
|
141
|
-
position.baseAssetAmount = position.baseAssetAmount.add(standardizedBaa);
|
|
142
|
-
position.quoteAssetAmount = position.quoteAssetAmount.add(deltaQaa);
|
|
143
|
-
position.lastNetBaseAssetAmountPerLp =
|
|
144
|
-
market.amm.marketPositionPerLp.baseAssetAmount.sub(reaminderPerLP);
|
|
145
146
|
let updateType;
|
|
146
147
|
if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
147
148
|
updateType = 'open';
|
|
@@ -168,13 +169,15 @@ class ClearingHouseUser {
|
|
|
168
169
|
newQuoteEntry = position.quoteEntryAmount.sub(position.quoteEntryAmount
|
|
169
170
|
.mul(deltaBaa.abs())
|
|
170
171
|
.div(position.baseAssetAmount.abs()));
|
|
171
|
-
pnl = position.quoteEntryAmount.sub(newQuoteEntry);
|
|
172
|
+
pnl = position.quoteEntryAmount.sub(newQuoteEntry).add(deltaQaa);
|
|
172
173
|
}
|
|
173
174
|
else {
|
|
174
175
|
newQuoteEntry = deltaQaa.sub(deltaQaa.mul(position.baseAssetAmount.abs()).div(deltaBaa.abs()));
|
|
175
176
|
pnl = position.quoteEntryAmount.add(deltaQaa.sub(newQuoteEntry));
|
|
176
177
|
}
|
|
177
178
|
position.quoteEntryAmount = newQuoteEntry;
|
|
179
|
+
position.baseAssetAmount = position.baseAssetAmount.add(standardizedBaa);
|
|
180
|
+
position.quoteAssetAmount = position.quoteAssetAmount.add(deltaQaa);
|
|
178
181
|
if (position.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
179
182
|
position.lastCumulativeFundingRate = market.amm.cumulativeFundingRateLong;
|
|
180
183
|
}
|
|
@@ -209,25 +212,25 @@ class ClearingHouseUser {
|
|
|
209
212
|
/**
|
|
210
213
|
* @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
|
|
211
214
|
*/
|
|
212
|
-
getMarginRequirement(type) {
|
|
215
|
+
getMarginRequirement(type, liquidationBuffer) {
|
|
213
216
|
return this.getUserAccount()
|
|
214
|
-
.
|
|
215
|
-
const market = this.clearingHouse.
|
|
216
|
-
if (
|
|
217
|
+
.perpPositions.reduce((marginRequirement, perpPosition) => {
|
|
218
|
+
const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
|
|
219
|
+
if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
|
|
217
220
|
// is an lp
|
|
218
221
|
// clone so we dont mutate the position
|
|
219
|
-
|
|
222
|
+
perpPosition = this.getClonedPosition(perpPosition);
|
|
220
223
|
// settle position
|
|
221
224
|
const [settledPosition, dustBaa, _] = this.getSettledLPPosition(market.marketIndex);
|
|
222
|
-
|
|
225
|
+
perpPosition.baseAssetAmount =
|
|
223
226
|
settledPosition.baseAssetAmount.add(dustBaa);
|
|
224
|
-
|
|
227
|
+
perpPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
|
|
225
228
|
// open orders
|
|
226
229
|
let openAsks;
|
|
227
230
|
if (market.amm.maxBaseAssetReserve > market.amm.baseAssetReserve) {
|
|
228
231
|
openAsks = market.amm.maxBaseAssetReserve
|
|
229
232
|
.sub(market.amm.baseAssetReserve)
|
|
230
|
-
.mul(
|
|
233
|
+
.mul(perpPosition.lpShares)
|
|
231
234
|
.div(market.amm.sqrtK)
|
|
232
235
|
.mul(new _1.BN(-1));
|
|
233
236
|
}
|
|
@@ -238,25 +241,33 @@ class ClearingHouseUser {
|
|
|
238
241
|
if (market.amm.minBaseAssetReserve < market.amm.baseAssetReserve) {
|
|
239
242
|
openBids = market.amm.baseAssetReserve
|
|
240
243
|
.sub(market.amm.minBaseAssetReserve)
|
|
241
|
-
.mul(
|
|
244
|
+
.mul(perpPosition.lpShares)
|
|
242
245
|
.div(market.amm.sqrtK);
|
|
243
246
|
}
|
|
244
247
|
else {
|
|
245
248
|
openBids = numericConstants_1.ZERO;
|
|
246
249
|
}
|
|
247
|
-
|
|
248
|
-
|
|
250
|
+
perpPosition.openAsks = perpPosition.openAsks.add(openAsks);
|
|
251
|
+
perpPosition.openBids = perpPosition.openBids.add(openBids);
|
|
252
|
+
}
|
|
253
|
+
let valuationPrice = this.getOracleDataForMarket(market.marketIndex).price;
|
|
254
|
+
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
255
|
+
valuationPrice = market.settlementPrice;
|
|
249
256
|
}
|
|
250
|
-
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(
|
|
257
|
+
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
|
|
251
258
|
const worstCaseAssetValue = worstCaseBaseAssetAmount
|
|
252
259
|
.abs()
|
|
253
|
-
.mul(
|
|
260
|
+
.mul(valuationPrice)
|
|
254
261
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
255
|
-
|
|
262
|
+
const positionMarginRequirement = worstCaseAssetValue
|
|
256
263
|
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), type)))
|
|
257
|
-
.div(numericConstants_1.MARGIN_PRECISION)
|
|
264
|
+
.div(numericConstants_1.MARGIN_PRECISION);
|
|
265
|
+
if (liquidationBuffer !== undefined) {
|
|
266
|
+
positionMarginRequirement.add(worstCaseAssetValue.mul(liquidationBuffer).div(numericConstants_1.MARGIN_PRECISION));
|
|
267
|
+
}
|
|
268
|
+
return marginRequirement.add(positionMarginRequirement);
|
|
258
269
|
}, numericConstants_1.ZERO)
|
|
259
|
-
.add(this.
|
|
270
|
+
.add(this.getSpotMarketLiabilityValue(undefined, type, liquidationBuffer));
|
|
260
271
|
}
|
|
261
272
|
/**
|
|
262
273
|
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
@@ -267,24 +278,26 @@ class ClearingHouseUser {
|
|
|
267
278
|
/**
|
|
268
279
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
269
280
|
*/
|
|
270
|
-
getMaintenanceMarginRequirement() {
|
|
271
|
-
return this.getMarginRequirement('Maintenance');
|
|
281
|
+
getMaintenanceMarginRequirement(liquidationBuffer) {
|
|
282
|
+
return this.getMarginRequirement('Maintenance', liquidationBuffer);
|
|
272
283
|
}
|
|
273
284
|
/**
|
|
274
285
|
* calculates unrealized position price pnl
|
|
275
286
|
* @returns : Precision QUOTE_PRECISION
|
|
276
287
|
*/
|
|
277
288
|
getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory) {
|
|
289
|
+
const quoteSpotMarket = this.clearingHouse.getQuoteSpotMarketAccount();
|
|
278
290
|
return this.getUserAccount()
|
|
279
|
-
.
|
|
280
|
-
.reduce((unrealizedPnl,
|
|
281
|
-
const market = this.clearingHouse.
|
|
282
|
-
|
|
291
|
+
.perpPositions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
|
|
292
|
+
.reduce((unrealizedPnl, perpPosition) => {
|
|
293
|
+
const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
|
|
294
|
+
const oraclePriceData = this.getOracleDataForMarket(market.marketIndex);
|
|
295
|
+
let positionUnrealizedPnl = (0, _1.calculatePositionPNL)(market, perpPosition, withFunding, oraclePriceData);
|
|
283
296
|
if (withWeightMarginCategory !== undefined) {
|
|
284
297
|
if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
285
298
|
positionUnrealizedPnl = positionUnrealizedPnl
|
|
286
|
-
.mul((0, _1.calculateUnrealizedAssetWeight)(market, positionUnrealizedPnl, withWeightMarginCategory))
|
|
287
|
-
.div(new _1.BN(numericConstants_1.
|
|
299
|
+
.mul((0, _1.calculateUnrealizedAssetWeight)(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
|
|
300
|
+
.div(new _1.BN(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION));
|
|
288
301
|
}
|
|
289
302
|
}
|
|
290
303
|
return unrealizedPnl.add(positionUnrealizedPnl);
|
|
@@ -296,74 +309,79 @@ class ClearingHouseUser {
|
|
|
296
309
|
*/
|
|
297
310
|
getUnrealizedFundingPNL(marketIndex) {
|
|
298
311
|
return this.getUserAccount()
|
|
299
|
-
.
|
|
300
|
-
.reduce((pnl,
|
|
301
|
-
const market = this.clearingHouse.
|
|
302
|
-
return pnl.add((0, _1.calculatePositionFundingPNL)(market,
|
|
312
|
+
.perpPositions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
|
|
313
|
+
.reduce((pnl, perpPosition) => {
|
|
314
|
+
const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
|
|
315
|
+
return pnl.add((0, _1.calculatePositionFundingPNL)(market, perpPosition));
|
|
303
316
|
}, numericConstants_1.ZERO);
|
|
304
317
|
}
|
|
305
|
-
|
|
306
|
-
return this.getUserAccount().
|
|
307
|
-
if (
|
|
308
|
-
(0, types_1.isVariant)(
|
|
309
|
-
(
|
|
318
|
+
getSpotMarketLiabilityValue(marketIndex, withWeightMarginCategory, liquidationBuffer) {
|
|
319
|
+
return this.getUserAccount().spotPositions.reduce((totalLiabilityValue, spotPosition) => {
|
|
320
|
+
if (spotPosition.balance.eq(numericConstants_1.ZERO) ||
|
|
321
|
+
(0, types_1.isVariant)(spotPosition.balanceType, 'deposit') ||
|
|
322
|
+
(marketIndex !== undefined &&
|
|
323
|
+
!spotPosition.marketIndex.eq(marketIndex))) {
|
|
310
324
|
return totalLiabilityValue;
|
|
311
325
|
}
|
|
312
326
|
// Todo this needs to account for whether it's based on initial or maintenance requirements
|
|
313
|
-
const
|
|
314
|
-
const tokenAmount = (0,
|
|
327
|
+
const spotMarketAccount = this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
|
|
328
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarketAccount, spotPosition.balanceType);
|
|
315
329
|
let liabilityValue = tokenAmount
|
|
316
|
-
.mul(this.
|
|
330
|
+
.mul(this.getOracleDataForSpotMarket(spotMarketAccount.marketIndex).price)
|
|
317
331
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
318
|
-
.div(new _1.BN(10).pow(new _1.BN(
|
|
332
|
+
.div(new _1.BN(10).pow(new _1.BN(spotMarketAccount.decimals).sub(numericConstants_1.SPOT_MARKET_BALANCE_PRECISION_EXP)));
|
|
319
333
|
if (withWeightMarginCategory !== undefined) {
|
|
320
|
-
|
|
334
|
+
let weight = (0, spotBalance_1.calculateLiabilityWeight)(tokenAmount, spotMarketAccount, withWeightMarginCategory);
|
|
335
|
+
if (liquidationBuffer !== undefined) {
|
|
336
|
+
weight = weight.add(liquidationBuffer);
|
|
337
|
+
}
|
|
321
338
|
liabilityValue = liabilityValue
|
|
322
339
|
.mul(weight)
|
|
323
|
-
.div(numericConstants_1.
|
|
340
|
+
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
324
341
|
}
|
|
325
342
|
return totalLiabilityValue.add(liabilityValue);
|
|
326
343
|
}, numericConstants_1.ZERO);
|
|
327
344
|
}
|
|
328
|
-
|
|
329
|
-
return this.getUserAccount().
|
|
330
|
-
if (
|
|
331
|
-
(0, types_1.isVariant)(
|
|
332
|
-
(
|
|
345
|
+
getSpotMarketAssetValue(marketIndex, withWeightMarginCategory) {
|
|
346
|
+
return this.getUserAccount().spotPositions.reduce((totalAssetValue, spotPosition) => {
|
|
347
|
+
if (spotPosition.balance.eq(numericConstants_1.ZERO) ||
|
|
348
|
+
(0, types_1.isVariant)(spotPosition.balanceType, 'borrow') ||
|
|
349
|
+
(marketIndex !== undefined &&
|
|
350
|
+
!spotPosition.marketIndex.eq(marketIndex))) {
|
|
333
351
|
return totalAssetValue;
|
|
334
352
|
}
|
|
335
353
|
// Todo this needs to account for whether it's based on initial or maintenance requirements
|
|
336
|
-
const
|
|
337
|
-
const tokenAmount = (0,
|
|
354
|
+
const spotMarketAccount = this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
|
|
355
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarketAccount, spotPosition.balanceType);
|
|
338
356
|
let assetValue = tokenAmount
|
|
339
|
-
.mul(this.
|
|
357
|
+
.mul(this.getOracleDataForSpotMarket(spotMarketAccount.marketIndex).price)
|
|
340
358
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
341
|
-
.div(new _1.BN(10).pow(new _1.BN(
|
|
359
|
+
.div(new _1.BN(10).pow(new _1.BN(spotMarketAccount.decimals).sub(numericConstants_1.SPOT_MARKET_BALANCE_PRECISION_EXP)));
|
|
342
360
|
if (withWeightMarginCategory !== undefined) {
|
|
343
|
-
const weight = (0,
|
|
344
|
-
assetValue = assetValue.mul(weight).div(numericConstants_1.
|
|
361
|
+
const weight = (0, spotBalance_1.calculateAssetWeight)(tokenAmount, spotMarketAccount, withWeightMarginCategory);
|
|
362
|
+
assetValue = assetValue.mul(weight).div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
345
363
|
}
|
|
346
364
|
return totalAssetValue.add(assetValue);
|
|
347
365
|
}, numericConstants_1.ZERO);
|
|
348
366
|
}
|
|
349
|
-
|
|
350
|
-
return this.
|
|
367
|
+
getNetSpotMarketValue(withWeightMarginCategory) {
|
|
368
|
+
return this.getSpotMarketAssetValue(undefined, withWeightMarginCategory).sub(this.getSpotMarketLiabilityValue(undefined, withWeightMarginCategory));
|
|
351
369
|
}
|
|
352
370
|
/**
|
|
353
371
|
* calculates TotalCollateral: collateral + unrealized pnl
|
|
354
372
|
* @returns : Precision QUOTE_PRECISION
|
|
355
373
|
*/
|
|
356
374
|
getTotalCollateral(marginCategory = 'Initial') {
|
|
357
|
-
return this.
|
|
375
|
+
return this.getSpotMarketAssetValue(undefined, marginCategory).add(this.getUnrealizedPNL(true, undefined, marginCategory));
|
|
358
376
|
}
|
|
359
377
|
/**
|
|
360
378
|
* calculates sum of position value across all positions in margin system
|
|
361
379
|
* @returns : Precision QUOTE_PRECISION
|
|
362
380
|
*/
|
|
363
381
|
getTotalPositionValue() {
|
|
364
|
-
return this.getUserAccount().
|
|
365
|
-
const market = this.clearingHouse.
|
|
366
|
-
const posVal = (0, margin_1.calculateBaseAssetValueWithOracle)(market,
|
|
382
|
+
return this.getUserAccount().perpPositions.reduce((positionValue, perpPosition) => {
|
|
383
|
+
const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
|
|
384
|
+
const posVal = (0, margin_1.calculateBaseAssetValueWithOracle)(market, perpPosition, this.getOracleDataForMarket(market.marketIndex));
|
|
367
385
|
return positionValue.add(posVal);
|
|
368
386
|
}, numericConstants_1.ZERO);
|
|
369
387
|
}
|
|
@@ -373,7 +391,7 @@ class ClearingHouseUser {
|
|
|
373
391
|
*/
|
|
374
392
|
getPositionValue(marketIndex, oraclePriceData) {
|
|
375
393
|
const userPosition = this.getUserPosition(marketIndex) || this.getEmptyPosition(marketIndex);
|
|
376
|
-
const market = this.clearingHouse.
|
|
394
|
+
const market = this.clearingHouse.getPerpMarketAccount(userPosition.marketIndex);
|
|
377
395
|
return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData);
|
|
378
396
|
}
|
|
379
397
|
getPositionSide(currentPosition) {
|
|
@@ -392,7 +410,7 @@ class ClearingHouseUser {
|
|
|
392
410
|
* @returns : Precision MARK_PRICE_PRECISION
|
|
393
411
|
*/
|
|
394
412
|
getPositionEstimatedExitPriceAndPnl(position, amountToClose, useAMMClose = false) {
|
|
395
|
-
const market = this.clearingHouse.
|
|
413
|
+
const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
|
|
396
414
|
const entryPrice = (0, position_1.calculateEntryPrice)(position);
|
|
397
415
|
const oraclePriceData = this.getOracleDataForMarket(position.marketIndex);
|
|
398
416
|
if (amountToClose) {
|
|
@@ -445,7 +463,7 @@ class ClearingHouseUser {
|
|
|
445
463
|
* @returns : Precision TEN_THOUSAND
|
|
446
464
|
*/
|
|
447
465
|
getMaxLeverage(marketIndex, category = 'Initial') {
|
|
448
|
-
const market = this.clearingHouse.
|
|
466
|
+
const market = this.clearingHouse.getPerpMarketAccount(marketIndex);
|
|
449
467
|
const marginRatioCategory = (0, _1.calculateMarketMarginRatio)(market,
|
|
450
468
|
// worstCaseBaseAssetAmount.abs(),
|
|
451
469
|
numericConstants_1.ZERO, // todo
|
|
@@ -466,21 +484,24 @@ class ClearingHouseUser {
|
|
|
466
484
|
}
|
|
467
485
|
canBeLiquidated() {
|
|
468
486
|
const totalCollateral = this.getTotalCollateral();
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
487
|
+
// if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
|
|
488
|
+
let liquidationBuffer = undefined;
|
|
489
|
+
if (this.getUserAccount().beingLiquidated) {
|
|
490
|
+
liquidationBuffer = new _1.BN(this.clearingHouse.getStateAccount().liquidationMarginBufferRatio);
|
|
491
|
+
}
|
|
492
|
+
const maintenanceRequirement = this.getMaintenanceMarginRequirement(liquidationBuffer);
|
|
493
|
+
return totalCollateral.lt(maintenanceRequirement);
|
|
473
494
|
}
|
|
474
495
|
/**
|
|
475
496
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
476
497
|
* @returns
|
|
477
498
|
*/
|
|
478
499
|
needsToSettleFundingPayment() {
|
|
479
|
-
for (const userPosition of this.getUserAccount().
|
|
500
|
+
for (const userPosition of this.getUserAccount().perpPositions) {
|
|
480
501
|
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
481
502
|
continue;
|
|
482
503
|
}
|
|
483
|
-
const market = this.clearingHouse.
|
|
504
|
+
const market = this.clearingHouse.getPerpMarketAccount(userPosition.marketIndex);
|
|
484
505
|
if (market.amm.cumulativeFundingRateLong.eq(userPosition.lastCumulativeFundingRate) ||
|
|
485
506
|
market.amm.cumulativeFundingRateShort.eq(userPosition.lastCumulativeFundingRate)) {
|
|
486
507
|
continue;
|
|
@@ -491,12 +512,12 @@ class ClearingHouseUser {
|
|
|
491
512
|
}
|
|
492
513
|
/**
|
|
493
514
|
* Calculate the liquidation price of a position, with optional parameter to calculate the liquidation price after a trade
|
|
494
|
-
* @param
|
|
515
|
+
* @param PerpPosition
|
|
495
516
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
496
517
|
* @param partial
|
|
497
518
|
* @returns Precision : MARK_PRICE_PRECISION
|
|
498
519
|
*/
|
|
499
|
-
liquidationPrice(
|
|
520
|
+
liquidationPrice(perpPosition, positionBaseSizeChange = numericConstants_1.ZERO) {
|
|
500
521
|
// solves formula for example canBeLiquidated below
|
|
501
522
|
/* example: assume BTC price is $40k (examine 10% up/down)
|
|
502
523
|
|
|
@@ -509,15 +530,16 @@ class ClearingHouseUser {
|
|
|
509
530
|
3. (10k - 4k) / (100k - 4k) = 6k/96k => .0625 */
|
|
510
531
|
const totalCollateral = this.getTotalCollateral();
|
|
511
532
|
// calculate the total position value ignoring any value from the target market of the trade
|
|
512
|
-
const totalPositionValueExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(
|
|
513
|
-
const
|
|
514
|
-
this.getEmptyPosition(
|
|
515
|
-
const
|
|
516
|
-
const proposedBaseAssetAmount =
|
|
533
|
+
const totalPositionValueExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(perpPosition.marketIndex);
|
|
534
|
+
const currentPerpPosition = this.getUserPosition(perpPosition.marketIndex) ||
|
|
535
|
+
this.getEmptyPosition(perpPosition.marketIndex);
|
|
536
|
+
const currentPerpPositionBaseSize = currentPerpPosition.baseAssetAmount;
|
|
537
|
+
const proposedBaseAssetAmount = currentPerpPositionBaseSize.add(positionBaseSizeChange);
|
|
517
538
|
// calculate position for current market after trade
|
|
518
|
-
const
|
|
519
|
-
marketIndex:
|
|
539
|
+
const proposedPerpPosition = {
|
|
540
|
+
marketIndex: perpPosition.marketIndex,
|
|
520
541
|
baseAssetAmount: proposedBaseAssetAmount,
|
|
542
|
+
remainderBaseAssetAmount: numericConstants_1.ZERO,
|
|
521
543
|
quoteAssetAmount: new _1.BN(0),
|
|
522
544
|
lastCumulativeFundingRate: numericConstants_1.ZERO,
|
|
523
545
|
quoteEntryAmount: new _1.BN(0),
|
|
@@ -532,13 +554,13 @@ class ClearingHouseUser {
|
|
|
532
554
|
};
|
|
533
555
|
if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO))
|
|
534
556
|
return new _1.BN(-1);
|
|
535
|
-
const market = this.clearingHouse.
|
|
536
|
-
const
|
|
557
|
+
const market = this.clearingHouse.getPerpMarketAccount(proposedPerpPosition.marketIndex);
|
|
558
|
+
const proposedPerpPositionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, proposedPerpPosition, this.getOracleDataForMarket(market.marketIndex));
|
|
537
559
|
// total position value after trade
|
|
538
|
-
const totalPositionValueAfterTrade = totalPositionValueExcludingTargetMarket.add(
|
|
539
|
-
const marginRequirementExcludingTargetMarket = this.getUserAccount().
|
|
540
|
-
if (!position.marketIndex.eq(
|
|
541
|
-
const market = this.clearingHouse.
|
|
560
|
+
const totalPositionValueAfterTrade = totalPositionValueExcludingTargetMarket.add(proposedPerpPositionValue);
|
|
561
|
+
const marginRequirementExcludingTargetMarket = this.getUserAccount().perpPositions.reduce((totalMarginRequirement, position) => {
|
|
562
|
+
if (!position.marketIndex.eq(perpPosition.marketIndex)) {
|
|
563
|
+
const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
|
|
542
564
|
const positionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, this.getOracleDataForMarket(market.marketIndex));
|
|
543
565
|
const marketMarginRequirement = positionValue
|
|
544
566
|
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, position.baseAssetAmount.abs(), 'Maintenance')))
|
|
@@ -550,14 +572,14 @@ class ClearingHouseUser {
|
|
|
550
572
|
const freeCollateralExcludingTargetMarket = totalCollateral.sub(marginRequirementExcludingTargetMarket);
|
|
551
573
|
// if the position value after the trade is less than free collateral, there is no liq price
|
|
552
574
|
if (totalPositionValueAfterTrade.lte(freeCollateralExcludingTargetMarket) &&
|
|
553
|
-
|
|
575
|
+
proposedPerpPosition.baseAssetAmount.abs().gt(numericConstants_1.ZERO)) {
|
|
554
576
|
return new _1.BN(-1);
|
|
555
577
|
}
|
|
556
|
-
const marginRequirementAfterTrade = marginRequirementExcludingTargetMarket.add(
|
|
557
|
-
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market,
|
|
578
|
+
const marginRequirementAfterTrade = marginRequirementExcludingTargetMarket.add(proposedPerpPositionValue
|
|
579
|
+
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, proposedPerpPosition.baseAssetAmount.abs(), 'Maintenance')))
|
|
558
580
|
.div(numericConstants_1.MARGIN_PRECISION));
|
|
559
581
|
const freeCollateralAfterTrade = totalCollateral.sub(marginRequirementAfterTrade);
|
|
560
|
-
const marketMaxLeverage = this.getMaxLeverage(
|
|
582
|
+
const marketMaxLeverage = this.getMaxLeverage(proposedPerpPosition.marketIndex, 'Maintenance');
|
|
561
583
|
let priceDelta;
|
|
562
584
|
if (proposedBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
563
585
|
priceDelta = freeCollateralAfterTrade
|
|
@@ -577,13 +599,13 @@ class ClearingHouseUser {
|
|
|
577
599
|
}
|
|
578
600
|
let markPriceAfterTrade;
|
|
579
601
|
if (positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
|
|
580
|
-
markPriceAfterTrade = (0, _1.calculateMarkPrice)(this.clearingHouse.
|
|
602
|
+
markPriceAfterTrade = (0, _1.calculateMarkPrice)(this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex), this.getOracleDataForMarket(perpPosition.marketIndex));
|
|
581
603
|
}
|
|
582
604
|
else {
|
|
583
605
|
const direction = positionBaseSizeChange.gt(numericConstants_1.ZERO)
|
|
584
606
|
? _1.PositionDirection.LONG
|
|
585
607
|
: _1.PositionDirection.SHORT;
|
|
586
|
-
markPriceAfterTrade = (0, _1.calculateTradeSlippage)(direction, positionBaseSizeChange.abs(), this.clearingHouse.
|
|
608
|
+
markPriceAfterTrade = (0, _1.calculateTradeSlippage)(direction, positionBaseSizeChange.abs(), this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex), 'base', this.getOracleDataForMarket(perpPosition.marketIndex))[3]; // newPrice after swap
|
|
587
609
|
}
|
|
588
610
|
if (priceDelta.gt(markPriceAfterTrade)) {
|
|
589
611
|
return new _1.BN(-1);
|
|
@@ -660,23 +682,23 @@ class ClearingHouseUser {
|
|
|
660
682
|
else {
|
|
661
683
|
// current leverage is greater than max leverage - can only reduce position size
|
|
662
684
|
if (!targetingSameSide) {
|
|
663
|
-
const market = this.clearingHouse.
|
|
664
|
-
const
|
|
685
|
+
const market = this.clearingHouse.getPerpMarketAccount(targetMarketIndex);
|
|
686
|
+
const perpPositionValue = this.getPositionValue(targetMarketIndex, oracleData);
|
|
665
687
|
const totalCollateral = this.getTotalCollateral();
|
|
666
688
|
const marginRequirement = this.getInitialMarginRequirement();
|
|
667
|
-
const marginFreedByClosing =
|
|
689
|
+
const marginFreedByClosing = perpPositionValue
|
|
668
690
|
.mul(new _1.BN(market.marginRatioInitial))
|
|
669
691
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
670
692
|
const marginRequirementAfterClosing = marginRequirement.sub(marginFreedByClosing);
|
|
671
693
|
if (marginRequirementAfterClosing.gt(totalCollateral)) {
|
|
672
|
-
maxPositionSize =
|
|
694
|
+
maxPositionSize = perpPositionValue;
|
|
673
695
|
}
|
|
674
696
|
else {
|
|
675
697
|
const freeCollateralAfterClose = totalCollateral.sub(marginRequirementAfterClosing);
|
|
676
698
|
const buyingPowerAfterClose = freeCollateralAfterClose
|
|
677
699
|
.mul(this.getMaxLeverage(targetMarketIndex))
|
|
678
700
|
.div(numericConstants_1.TEN_THOUSAND);
|
|
679
|
-
maxPositionSize =
|
|
701
|
+
maxPositionSize = perpPositionValue.add(buyingPowerAfterClose);
|
|
680
702
|
}
|
|
681
703
|
}
|
|
682
704
|
else {
|
|
@@ -708,13 +730,13 @@ class ClearingHouseUser {
|
|
|
708
730
|
currentPositionQuoteAmount = currentPositionQuoteAmount.neg();
|
|
709
731
|
if (tradeSide === _1.PositionDirection.SHORT)
|
|
710
732
|
tradeQuoteAmount = tradeQuoteAmount.neg();
|
|
711
|
-
const
|
|
733
|
+
const currentPerpPositionAfterTrade = currentPositionQuoteAmount
|
|
712
734
|
.add(tradeQuoteAmount)
|
|
713
735
|
.abs();
|
|
714
736
|
const totalPositionAfterTradeExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(targetMarketIndex);
|
|
715
737
|
const totalCollateral = this.getTotalCollateral();
|
|
716
738
|
if (totalCollateral.gt(numericConstants_1.ZERO)) {
|
|
717
|
-
const newLeverage =
|
|
739
|
+
const newLeverage = currentPerpPositionAfterTrade
|
|
718
740
|
.add(totalPositionAfterTradeExcludingTargetMarket)
|
|
719
741
|
.abs()
|
|
720
742
|
.mul(numericConstants_1.TEN_THOUSAND)
|
|
@@ -731,7 +753,7 @@ class ClearingHouseUser {
|
|
|
731
753
|
* @returns feeForQuote : Precision QUOTE_PRECISION
|
|
732
754
|
*/
|
|
733
755
|
calculateFeeForQuoteAmount(quoteAmount) {
|
|
734
|
-
const feeStructure = this.clearingHouse.getStateAccount().
|
|
756
|
+
const feeStructure = this.clearingHouse.getStateAccount().perpFeeStructure;
|
|
735
757
|
return quoteAmount
|
|
736
758
|
.mul(feeStructure.feeNumerator)
|
|
737
759
|
.div(feeStructure.feeDenominator);
|
|
@@ -742,22 +764,22 @@ class ClearingHouseUser {
|
|
|
742
764
|
* @returns positionValue : Precision QUOTE_PRECISION
|
|
743
765
|
*/
|
|
744
766
|
getTotalPositionValueExcludingMarket(marketToIgnore) {
|
|
745
|
-
const
|
|
767
|
+
const currentPerpPosition = this.getUserPosition(marketToIgnore) ||
|
|
746
768
|
this.getEmptyPosition(marketToIgnore);
|
|
747
769
|
const oracleData = this.getOracleDataForMarket(marketToIgnore);
|
|
748
|
-
let
|
|
749
|
-
if (
|
|
750
|
-
|
|
770
|
+
let currentPerpPositionValueUSDC = numericConstants_1.ZERO;
|
|
771
|
+
if (currentPerpPosition) {
|
|
772
|
+
currentPerpPositionValueUSDC = this.getPositionValue(marketToIgnore, oracleData);
|
|
751
773
|
}
|
|
752
|
-
return this.getTotalPositionValue().sub(
|
|
774
|
+
return this.getTotalPositionValue().sub(currentPerpPositionValueUSDC);
|
|
753
775
|
}
|
|
754
776
|
getOracleDataForMarket(marketIndex) {
|
|
755
|
-
const oracleKey = this.clearingHouse.
|
|
777
|
+
const oracleKey = this.clearingHouse.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
756
778
|
const oracleData = this.clearingHouse.getOraclePriceDataAndSlot(oracleKey).data;
|
|
757
779
|
return oracleData;
|
|
758
780
|
}
|
|
759
|
-
|
|
760
|
-
const oracleKey = this.clearingHouse.
|
|
781
|
+
getOracleDataForSpotMarket(marketIndex) {
|
|
782
|
+
const oracleKey = this.clearingHouse.getSpotMarketAccount(marketIndex).oracle;
|
|
761
783
|
const oracleData = this.clearingHouse.getOraclePriceDataAndSlot(oracleKey).data;
|
|
762
784
|
return oracleData;
|
|
763
785
|
}
|
package/lib/config.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { PerpMarketConfig } from './constants/perpMarkets';
|
|
3
|
+
import { SpotMarketConfig } from './constants/spotMarkets';
|
|
4
4
|
import { BN } from '@project-serum/anchor';
|
|
5
5
|
import { OracleInfo } from './oracles/types';
|
|
6
6
|
declare type DriftConfig = {
|
|
@@ -8,8 +8,8 @@ declare type DriftConfig = {
|
|
|
8
8
|
PYTH_ORACLE_MAPPING_ADDRESS: string;
|
|
9
9
|
CLEARING_HOUSE_PROGRAM_ID: string;
|
|
10
10
|
USDC_MINT_ADDRESS: string;
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
PERP_MARKETS: PerpMarketConfig[];
|
|
12
|
+
SPOT_MARKETS: SpotMarketConfig[];
|
|
13
13
|
};
|
|
14
14
|
export declare type DriftEnv = 'devnet' | 'mainnet-beta';
|
|
15
15
|
export declare const configs: {
|
|
@@ -27,9 +27,9 @@ export declare const initialize: (props: {
|
|
|
27
27
|
env: DriftEnv;
|
|
28
28
|
overrideEnv?: Partial<DriftConfig>;
|
|
29
29
|
}) => DriftConfig;
|
|
30
|
-
export declare function
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
export declare function getMarketsAndOraclesForSubscription(env: DriftEnv): {
|
|
31
|
+
perpMarketIndexes: BN[];
|
|
32
|
+
spotMarketIndexes: BN[];
|
|
33
33
|
oracleInfos: OracleInfo[];
|
|
34
34
|
};
|
|
35
35
|
export {};
|