@drift-labs/sdk 0.2.0-master.25 → 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 +7 -6
- package/lib/addresses/pda.js +31 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +83 -42
- package/lib/clearingHouse.d.ts +69 -42
- package/lib/clearingHouse.js +753 -277
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +139 -119
- package/lib/config.d.ts +7 -7
- package/lib/config.js +20 -20
- 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/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +7 -7
- package/lib/idl/clearing_house.json +1008 -279
- package/lib/index.d.ts +5 -3
- package/lib/index.js +5 -3
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +1 -1
- 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 +123 -33
- package/lib/types.js +31 -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 +49 -44
- package/src/admin.js +1284 -0
- package/src/admin.ts +140 -47
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1083 -378
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +232 -168
- 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 +29 -29
- 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/fetchLogs.js +117 -0
- package/src/events/pollingLogProvider.js +113 -0
- 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 +3 -0
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +10 -8
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1008 -279
- package/src/index.js +75 -0
- package/src/index.ts +5 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +6 -3
- 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 +110 -33
- 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,
|
|
@@ -112,7 +113,7 @@ class ClearingHouseUser {
|
|
|
112
113
|
getSettledLPPosition(marketIndex) {
|
|
113
114
|
const _position = this.getUserPosition(marketIndex);
|
|
114
115
|
const position = this.getClonedPosition(_position);
|
|
115
|
-
const market = this.clearingHouse.
|
|
116
|
+
const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
|
|
116
117
|
const nShares = position.lpShares;
|
|
117
118
|
const deltaBaa = market.amm.marketPositionPerLp.baseAssetAmount
|
|
118
119
|
.sub(position.lastNetBaseAssetAmountPerLp)
|
|
@@ -126,22 +127,22 @@ class ClearingHouseUser {
|
|
|
126
127
|
const sign = { true: new _1.BN(1), false: new _1.BN(-1) }[v.gte(numericConstants_1.ZERO).toString()];
|
|
127
128
|
return sign;
|
|
128
129
|
}
|
|
129
|
-
|
|
130
|
-
.abs()
|
|
131
|
-
.
|
|
132
|
-
|
|
133
|
-
const _standardizedBaa = deltaBaa.sub(remainder);
|
|
134
|
-
let remainderBaa;
|
|
135
|
-
if (_standardizedBaa.abs().gte(market.amm.baseAssetAmountStepSize)) {
|
|
136
|
-
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];
|
|
137
134
|
}
|
|
138
|
-
|
|
139
|
-
|
|
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;
|
|
140
145
|
}
|
|
141
|
-
const standardizedBaa = deltaBaa.sub(remainderBaa);
|
|
142
|
-
const reaminderPerLP = remainderBaa.mul(numericConstants_1.AMM_RESERVE_PRECISION).div(nShares);
|
|
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';
|
|
@@ -211,25 +212,25 @@ class ClearingHouseUser {
|
|
|
211
212
|
/**
|
|
212
213
|
* @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
|
|
213
214
|
*/
|
|
214
|
-
getMarginRequirement(type) {
|
|
215
|
+
getMarginRequirement(type, liquidationBuffer) {
|
|
215
216
|
return this.getUserAccount()
|
|
216
|
-
.
|
|
217
|
-
const market = this.clearingHouse.
|
|
218
|
-
if (
|
|
217
|
+
.perpPositions.reduce((marginRequirement, perpPosition) => {
|
|
218
|
+
const market = this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex);
|
|
219
|
+
if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
|
|
219
220
|
// is an lp
|
|
220
221
|
// clone so we dont mutate the position
|
|
221
|
-
|
|
222
|
+
perpPosition = this.getClonedPosition(perpPosition);
|
|
222
223
|
// settle position
|
|
223
224
|
const [settledPosition, dustBaa, _] = this.getSettledLPPosition(market.marketIndex);
|
|
224
|
-
|
|
225
|
+
perpPosition.baseAssetAmount =
|
|
225
226
|
settledPosition.baseAssetAmount.add(dustBaa);
|
|
226
|
-
|
|
227
|
+
perpPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
|
|
227
228
|
// open orders
|
|
228
229
|
let openAsks;
|
|
229
230
|
if (market.amm.maxBaseAssetReserve > market.amm.baseAssetReserve) {
|
|
230
231
|
openAsks = market.amm.maxBaseAssetReserve
|
|
231
232
|
.sub(market.amm.baseAssetReserve)
|
|
232
|
-
.mul(
|
|
233
|
+
.mul(perpPosition.lpShares)
|
|
233
234
|
.div(market.amm.sqrtK)
|
|
234
235
|
.mul(new _1.BN(-1));
|
|
235
236
|
}
|
|
@@ -240,25 +241,33 @@ class ClearingHouseUser {
|
|
|
240
241
|
if (market.amm.minBaseAssetReserve < market.amm.baseAssetReserve) {
|
|
241
242
|
openBids = market.amm.baseAssetReserve
|
|
242
243
|
.sub(market.amm.minBaseAssetReserve)
|
|
243
|
-
.mul(
|
|
244
|
+
.mul(perpPosition.lpShares)
|
|
244
245
|
.div(market.amm.sqrtK);
|
|
245
246
|
}
|
|
246
247
|
else {
|
|
247
248
|
openBids = numericConstants_1.ZERO;
|
|
248
249
|
}
|
|
249
|
-
|
|
250
|
-
|
|
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;
|
|
251
256
|
}
|
|
252
|
-
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(
|
|
257
|
+
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
|
|
253
258
|
const worstCaseAssetValue = worstCaseBaseAssetAmount
|
|
254
259
|
.abs()
|
|
255
|
-
.mul(
|
|
260
|
+
.mul(valuationPrice)
|
|
256
261
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
257
|
-
|
|
262
|
+
const positionMarginRequirement = worstCaseAssetValue
|
|
258
263
|
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), type)))
|
|
259
|
-
.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);
|
|
260
269
|
}, numericConstants_1.ZERO)
|
|
261
|
-
.add(this.
|
|
270
|
+
.add(this.getSpotMarketLiabilityValue(undefined, type, liquidationBuffer));
|
|
262
271
|
}
|
|
263
272
|
/**
|
|
264
273
|
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
@@ -269,24 +278,26 @@ class ClearingHouseUser {
|
|
|
269
278
|
/**
|
|
270
279
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
271
280
|
*/
|
|
272
|
-
getMaintenanceMarginRequirement() {
|
|
273
|
-
return this.getMarginRequirement('Maintenance');
|
|
281
|
+
getMaintenanceMarginRequirement(liquidationBuffer) {
|
|
282
|
+
return this.getMarginRequirement('Maintenance', liquidationBuffer);
|
|
274
283
|
}
|
|
275
284
|
/**
|
|
276
285
|
* calculates unrealized position price pnl
|
|
277
286
|
* @returns : Precision QUOTE_PRECISION
|
|
278
287
|
*/
|
|
279
288
|
getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory) {
|
|
289
|
+
const quoteSpotMarket = this.clearingHouse.getQuoteSpotMarketAccount();
|
|
280
290
|
return this.getUserAccount()
|
|
281
|
-
.
|
|
282
|
-
.reduce((unrealizedPnl,
|
|
283
|
-
const market = this.clearingHouse.
|
|
284
|
-
|
|
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);
|
|
285
296
|
if (withWeightMarginCategory !== undefined) {
|
|
286
297
|
if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
287
298
|
positionUnrealizedPnl = positionUnrealizedPnl
|
|
288
|
-
.mul((0, _1.calculateUnrealizedAssetWeight)(market, positionUnrealizedPnl, withWeightMarginCategory))
|
|
289
|
-
.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));
|
|
290
301
|
}
|
|
291
302
|
}
|
|
292
303
|
return unrealizedPnl.add(positionUnrealizedPnl);
|
|
@@ -298,74 +309,79 @@ class ClearingHouseUser {
|
|
|
298
309
|
*/
|
|
299
310
|
getUnrealizedFundingPNL(marketIndex) {
|
|
300
311
|
return this.getUserAccount()
|
|
301
|
-
.
|
|
302
|
-
.reduce((pnl,
|
|
303
|
-
const market = this.clearingHouse.
|
|
304
|
-
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));
|
|
305
316
|
}, numericConstants_1.ZERO);
|
|
306
317
|
}
|
|
307
|
-
|
|
308
|
-
return this.getUserAccount().
|
|
309
|
-
if (
|
|
310
|
-
(0, types_1.isVariant)(
|
|
311
|
-
(
|
|
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))) {
|
|
312
324
|
return totalLiabilityValue;
|
|
313
325
|
}
|
|
314
326
|
// Todo this needs to account for whether it's based on initial or maintenance requirements
|
|
315
|
-
const
|
|
316
|
-
const tokenAmount = (0,
|
|
327
|
+
const spotMarketAccount = this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
|
|
328
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarketAccount, spotPosition.balanceType);
|
|
317
329
|
let liabilityValue = tokenAmount
|
|
318
|
-
.mul(this.
|
|
330
|
+
.mul(this.getOracleDataForSpotMarket(spotMarketAccount.marketIndex).price)
|
|
319
331
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
320
|
-
.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)));
|
|
321
333
|
if (withWeightMarginCategory !== undefined) {
|
|
322
|
-
|
|
334
|
+
let weight = (0, spotBalance_1.calculateLiabilityWeight)(tokenAmount, spotMarketAccount, withWeightMarginCategory);
|
|
335
|
+
if (liquidationBuffer !== undefined) {
|
|
336
|
+
weight = weight.add(liquidationBuffer);
|
|
337
|
+
}
|
|
323
338
|
liabilityValue = liabilityValue
|
|
324
339
|
.mul(weight)
|
|
325
|
-
.div(numericConstants_1.
|
|
340
|
+
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
326
341
|
}
|
|
327
342
|
return totalLiabilityValue.add(liabilityValue);
|
|
328
343
|
}, numericConstants_1.ZERO);
|
|
329
344
|
}
|
|
330
|
-
|
|
331
|
-
return this.getUserAccount().
|
|
332
|
-
if (
|
|
333
|
-
(0, types_1.isVariant)(
|
|
334
|
-
(
|
|
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))) {
|
|
335
351
|
return totalAssetValue;
|
|
336
352
|
}
|
|
337
353
|
// Todo this needs to account for whether it's based on initial or maintenance requirements
|
|
338
|
-
const
|
|
339
|
-
const tokenAmount = (0,
|
|
354
|
+
const spotMarketAccount = this.clearingHouse.getSpotMarketAccount(spotPosition.marketIndex);
|
|
355
|
+
const tokenAmount = (0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarketAccount, spotPosition.balanceType);
|
|
340
356
|
let assetValue = tokenAmount
|
|
341
|
-
.mul(this.
|
|
357
|
+
.mul(this.getOracleDataForSpotMarket(spotMarketAccount.marketIndex).price)
|
|
342
358
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
343
|
-
.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)));
|
|
344
360
|
if (withWeightMarginCategory !== undefined) {
|
|
345
|
-
const weight = (0,
|
|
346
|
-
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);
|
|
347
363
|
}
|
|
348
364
|
return totalAssetValue.add(assetValue);
|
|
349
365
|
}, numericConstants_1.ZERO);
|
|
350
366
|
}
|
|
351
|
-
|
|
352
|
-
return this.
|
|
367
|
+
getNetSpotMarketValue(withWeightMarginCategory) {
|
|
368
|
+
return this.getSpotMarketAssetValue(undefined, withWeightMarginCategory).sub(this.getSpotMarketLiabilityValue(undefined, withWeightMarginCategory));
|
|
353
369
|
}
|
|
354
370
|
/**
|
|
355
371
|
* calculates TotalCollateral: collateral + unrealized pnl
|
|
356
372
|
* @returns : Precision QUOTE_PRECISION
|
|
357
373
|
*/
|
|
358
374
|
getTotalCollateral(marginCategory = 'Initial') {
|
|
359
|
-
return this.
|
|
375
|
+
return this.getSpotMarketAssetValue(undefined, marginCategory).add(this.getUnrealizedPNL(true, undefined, marginCategory));
|
|
360
376
|
}
|
|
361
377
|
/**
|
|
362
378
|
* calculates sum of position value across all positions in margin system
|
|
363
379
|
* @returns : Precision QUOTE_PRECISION
|
|
364
380
|
*/
|
|
365
381
|
getTotalPositionValue() {
|
|
366
|
-
return this.getUserAccount().
|
|
367
|
-
const market = this.clearingHouse.
|
|
368
|
-
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));
|
|
369
385
|
return positionValue.add(posVal);
|
|
370
386
|
}, numericConstants_1.ZERO);
|
|
371
387
|
}
|
|
@@ -375,7 +391,7 @@ class ClearingHouseUser {
|
|
|
375
391
|
*/
|
|
376
392
|
getPositionValue(marketIndex, oraclePriceData) {
|
|
377
393
|
const userPosition = this.getUserPosition(marketIndex) || this.getEmptyPosition(marketIndex);
|
|
378
|
-
const market = this.clearingHouse.
|
|
394
|
+
const market = this.clearingHouse.getPerpMarketAccount(userPosition.marketIndex);
|
|
379
395
|
return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData);
|
|
380
396
|
}
|
|
381
397
|
getPositionSide(currentPosition) {
|
|
@@ -394,7 +410,7 @@ class ClearingHouseUser {
|
|
|
394
410
|
* @returns : Precision MARK_PRICE_PRECISION
|
|
395
411
|
*/
|
|
396
412
|
getPositionEstimatedExitPriceAndPnl(position, amountToClose, useAMMClose = false) {
|
|
397
|
-
const market = this.clearingHouse.
|
|
413
|
+
const market = this.clearingHouse.getPerpMarketAccount(position.marketIndex);
|
|
398
414
|
const entryPrice = (0, position_1.calculateEntryPrice)(position);
|
|
399
415
|
const oraclePriceData = this.getOracleDataForMarket(position.marketIndex);
|
|
400
416
|
if (amountToClose) {
|
|
@@ -447,7 +463,7 @@ class ClearingHouseUser {
|
|
|
447
463
|
* @returns : Precision TEN_THOUSAND
|
|
448
464
|
*/
|
|
449
465
|
getMaxLeverage(marketIndex, category = 'Initial') {
|
|
450
|
-
const market = this.clearingHouse.
|
|
466
|
+
const market = this.clearingHouse.getPerpMarketAccount(marketIndex);
|
|
451
467
|
const marginRatioCategory = (0, _1.calculateMarketMarginRatio)(market,
|
|
452
468
|
// worstCaseBaseAssetAmount.abs(),
|
|
453
469
|
numericConstants_1.ZERO, // todo
|
|
@@ -468,21 +484,24 @@ class ClearingHouseUser {
|
|
|
468
484
|
}
|
|
469
485
|
canBeLiquidated() {
|
|
470
486
|
const totalCollateral = this.getTotalCollateral();
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
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);
|
|
475
494
|
}
|
|
476
495
|
/**
|
|
477
496
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
478
497
|
* @returns
|
|
479
498
|
*/
|
|
480
499
|
needsToSettleFundingPayment() {
|
|
481
|
-
for (const userPosition of this.getUserAccount().
|
|
500
|
+
for (const userPosition of this.getUserAccount().perpPositions) {
|
|
482
501
|
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
483
502
|
continue;
|
|
484
503
|
}
|
|
485
|
-
const market = this.clearingHouse.
|
|
504
|
+
const market = this.clearingHouse.getPerpMarketAccount(userPosition.marketIndex);
|
|
486
505
|
if (market.amm.cumulativeFundingRateLong.eq(userPosition.lastCumulativeFundingRate) ||
|
|
487
506
|
market.amm.cumulativeFundingRateShort.eq(userPosition.lastCumulativeFundingRate)) {
|
|
488
507
|
continue;
|
|
@@ -493,12 +512,12 @@ class ClearingHouseUser {
|
|
|
493
512
|
}
|
|
494
513
|
/**
|
|
495
514
|
* Calculate the liquidation price of a position, with optional parameter to calculate the liquidation price after a trade
|
|
496
|
-
* @param
|
|
515
|
+
* @param PerpPosition
|
|
497
516
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
498
517
|
* @param partial
|
|
499
518
|
* @returns Precision : MARK_PRICE_PRECISION
|
|
500
519
|
*/
|
|
501
|
-
liquidationPrice(
|
|
520
|
+
liquidationPrice(perpPosition, positionBaseSizeChange = numericConstants_1.ZERO) {
|
|
502
521
|
// solves formula for example canBeLiquidated below
|
|
503
522
|
/* example: assume BTC price is $40k (examine 10% up/down)
|
|
504
523
|
|
|
@@ -511,15 +530,16 @@ class ClearingHouseUser {
|
|
|
511
530
|
3. (10k - 4k) / (100k - 4k) = 6k/96k => .0625 */
|
|
512
531
|
const totalCollateral = this.getTotalCollateral();
|
|
513
532
|
// calculate the total position value ignoring any value from the target market of the trade
|
|
514
|
-
const totalPositionValueExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(
|
|
515
|
-
const
|
|
516
|
-
this.getEmptyPosition(
|
|
517
|
-
const
|
|
518
|
-
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);
|
|
519
538
|
// calculate position for current market after trade
|
|
520
|
-
const
|
|
521
|
-
marketIndex:
|
|
539
|
+
const proposedPerpPosition = {
|
|
540
|
+
marketIndex: perpPosition.marketIndex,
|
|
522
541
|
baseAssetAmount: proposedBaseAssetAmount,
|
|
542
|
+
remainderBaseAssetAmount: numericConstants_1.ZERO,
|
|
523
543
|
quoteAssetAmount: new _1.BN(0),
|
|
524
544
|
lastCumulativeFundingRate: numericConstants_1.ZERO,
|
|
525
545
|
quoteEntryAmount: new _1.BN(0),
|
|
@@ -534,13 +554,13 @@ class ClearingHouseUser {
|
|
|
534
554
|
};
|
|
535
555
|
if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO))
|
|
536
556
|
return new _1.BN(-1);
|
|
537
|
-
const market = this.clearingHouse.
|
|
538
|
-
const
|
|
557
|
+
const market = this.clearingHouse.getPerpMarketAccount(proposedPerpPosition.marketIndex);
|
|
558
|
+
const proposedPerpPositionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, proposedPerpPosition, this.getOracleDataForMarket(market.marketIndex));
|
|
539
559
|
// total position value after trade
|
|
540
|
-
const totalPositionValueAfterTrade = totalPositionValueExcludingTargetMarket.add(
|
|
541
|
-
const marginRequirementExcludingTargetMarket = this.getUserAccount().
|
|
542
|
-
if (!position.marketIndex.eq(
|
|
543
|
-
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);
|
|
544
564
|
const positionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, this.getOracleDataForMarket(market.marketIndex));
|
|
545
565
|
const marketMarginRequirement = positionValue
|
|
546
566
|
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, position.baseAssetAmount.abs(), 'Maintenance')))
|
|
@@ -552,14 +572,14 @@ class ClearingHouseUser {
|
|
|
552
572
|
const freeCollateralExcludingTargetMarket = totalCollateral.sub(marginRequirementExcludingTargetMarket);
|
|
553
573
|
// if the position value after the trade is less than free collateral, there is no liq price
|
|
554
574
|
if (totalPositionValueAfterTrade.lte(freeCollateralExcludingTargetMarket) &&
|
|
555
|
-
|
|
575
|
+
proposedPerpPosition.baseAssetAmount.abs().gt(numericConstants_1.ZERO)) {
|
|
556
576
|
return new _1.BN(-1);
|
|
557
577
|
}
|
|
558
|
-
const marginRequirementAfterTrade = marginRequirementExcludingTargetMarket.add(
|
|
559
|
-
.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')))
|
|
560
580
|
.div(numericConstants_1.MARGIN_PRECISION));
|
|
561
581
|
const freeCollateralAfterTrade = totalCollateral.sub(marginRequirementAfterTrade);
|
|
562
|
-
const marketMaxLeverage = this.getMaxLeverage(
|
|
582
|
+
const marketMaxLeverage = this.getMaxLeverage(proposedPerpPosition.marketIndex, 'Maintenance');
|
|
563
583
|
let priceDelta;
|
|
564
584
|
if (proposedBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
565
585
|
priceDelta = freeCollateralAfterTrade
|
|
@@ -579,13 +599,13 @@ class ClearingHouseUser {
|
|
|
579
599
|
}
|
|
580
600
|
let markPriceAfterTrade;
|
|
581
601
|
if (positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
|
|
582
|
-
markPriceAfterTrade = (0, _1.calculateMarkPrice)(this.clearingHouse.
|
|
602
|
+
markPriceAfterTrade = (0, _1.calculateMarkPrice)(this.clearingHouse.getPerpMarketAccount(perpPosition.marketIndex), this.getOracleDataForMarket(perpPosition.marketIndex));
|
|
583
603
|
}
|
|
584
604
|
else {
|
|
585
605
|
const direction = positionBaseSizeChange.gt(numericConstants_1.ZERO)
|
|
586
606
|
? _1.PositionDirection.LONG
|
|
587
607
|
: _1.PositionDirection.SHORT;
|
|
588
|
-
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
|
|
589
609
|
}
|
|
590
610
|
if (priceDelta.gt(markPriceAfterTrade)) {
|
|
591
611
|
return new _1.BN(-1);
|
|
@@ -662,23 +682,23 @@ class ClearingHouseUser {
|
|
|
662
682
|
else {
|
|
663
683
|
// current leverage is greater than max leverage - can only reduce position size
|
|
664
684
|
if (!targetingSameSide) {
|
|
665
|
-
const market = this.clearingHouse.
|
|
666
|
-
const
|
|
685
|
+
const market = this.clearingHouse.getPerpMarketAccount(targetMarketIndex);
|
|
686
|
+
const perpPositionValue = this.getPositionValue(targetMarketIndex, oracleData);
|
|
667
687
|
const totalCollateral = this.getTotalCollateral();
|
|
668
688
|
const marginRequirement = this.getInitialMarginRequirement();
|
|
669
|
-
const marginFreedByClosing =
|
|
689
|
+
const marginFreedByClosing = perpPositionValue
|
|
670
690
|
.mul(new _1.BN(market.marginRatioInitial))
|
|
671
691
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
672
692
|
const marginRequirementAfterClosing = marginRequirement.sub(marginFreedByClosing);
|
|
673
693
|
if (marginRequirementAfterClosing.gt(totalCollateral)) {
|
|
674
|
-
maxPositionSize =
|
|
694
|
+
maxPositionSize = perpPositionValue;
|
|
675
695
|
}
|
|
676
696
|
else {
|
|
677
697
|
const freeCollateralAfterClose = totalCollateral.sub(marginRequirementAfterClosing);
|
|
678
698
|
const buyingPowerAfterClose = freeCollateralAfterClose
|
|
679
699
|
.mul(this.getMaxLeverage(targetMarketIndex))
|
|
680
700
|
.div(numericConstants_1.TEN_THOUSAND);
|
|
681
|
-
maxPositionSize =
|
|
701
|
+
maxPositionSize = perpPositionValue.add(buyingPowerAfterClose);
|
|
682
702
|
}
|
|
683
703
|
}
|
|
684
704
|
else {
|
|
@@ -710,13 +730,13 @@ class ClearingHouseUser {
|
|
|
710
730
|
currentPositionQuoteAmount = currentPositionQuoteAmount.neg();
|
|
711
731
|
if (tradeSide === _1.PositionDirection.SHORT)
|
|
712
732
|
tradeQuoteAmount = tradeQuoteAmount.neg();
|
|
713
|
-
const
|
|
733
|
+
const currentPerpPositionAfterTrade = currentPositionQuoteAmount
|
|
714
734
|
.add(tradeQuoteAmount)
|
|
715
735
|
.abs();
|
|
716
736
|
const totalPositionAfterTradeExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(targetMarketIndex);
|
|
717
737
|
const totalCollateral = this.getTotalCollateral();
|
|
718
738
|
if (totalCollateral.gt(numericConstants_1.ZERO)) {
|
|
719
|
-
const newLeverage =
|
|
739
|
+
const newLeverage = currentPerpPositionAfterTrade
|
|
720
740
|
.add(totalPositionAfterTradeExcludingTargetMarket)
|
|
721
741
|
.abs()
|
|
722
742
|
.mul(numericConstants_1.TEN_THOUSAND)
|
|
@@ -733,7 +753,7 @@ class ClearingHouseUser {
|
|
|
733
753
|
* @returns feeForQuote : Precision QUOTE_PRECISION
|
|
734
754
|
*/
|
|
735
755
|
calculateFeeForQuoteAmount(quoteAmount) {
|
|
736
|
-
const feeStructure = this.clearingHouse.getStateAccount().
|
|
756
|
+
const feeStructure = this.clearingHouse.getStateAccount().perpFeeStructure;
|
|
737
757
|
return quoteAmount
|
|
738
758
|
.mul(feeStructure.feeNumerator)
|
|
739
759
|
.div(feeStructure.feeDenominator);
|
|
@@ -744,22 +764,22 @@ class ClearingHouseUser {
|
|
|
744
764
|
* @returns positionValue : Precision QUOTE_PRECISION
|
|
745
765
|
*/
|
|
746
766
|
getTotalPositionValueExcludingMarket(marketToIgnore) {
|
|
747
|
-
const
|
|
767
|
+
const currentPerpPosition = this.getUserPosition(marketToIgnore) ||
|
|
748
768
|
this.getEmptyPosition(marketToIgnore);
|
|
749
769
|
const oracleData = this.getOracleDataForMarket(marketToIgnore);
|
|
750
|
-
let
|
|
751
|
-
if (
|
|
752
|
-
|
|
770
|
+
let currentPerpPositionValueUSDC = numericConstants_1.ZERO;
|
|
771
|
+
if (currentPerpPosition) {
|
|
772
|
+
currentPerpPositionValueUSDC = this.getPositionValue(marketToIgnore, oracleData);
|
|
753
773
|
}
|
|
754
|
-
return this.getTotalPositionValue().sub(
|
|
774
|
+
return this.getTotalPositionValue().sub(currentPerpPositionValueUSDC);
|
|
755
775
|
}
|
|
756
776
|
getOracleDataForMarket(marketIndex) {
|
|
757
|
-
const oracleKey = this.clearingHouse.
|
|
777
|
+
const oracleKey = this.clearingHouse.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
758
778
|
const oracleData = this.clearingHouse.getOraclePriceDataAndSlot(oracleKey).data;
|
|
759
779
|
return oracleData;
|
|
760
780
|
}
|
|
761
|
-
|
|
762
|
-
const oracleKey = this.clearingHouse.
|
|
781
|
+
getOracleDataForSpotMarket(marketIndex) {
|
|
782
|
+
const oracleKey = this.clearingHouse.getSpotMarketAccount(marketIndex).oracle;
|
|
763
783
|
const oracleData = this.clearingHouse.getOraclePriceDataAndSlot(oracleKey).data;
|
|
764
784
|
return oracleData;
|
|
765
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 {};
|
package/lib/config.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
3
|
+
exports.getMarketsAndOraclesForSubscription = exports.initialize = exports.getConfig = exports.configs = void 0;
|
|
4
|
+
const perpMarkets_1 = require("./constants/perpMarkets");
|
|
5
|
+
const spotMarkets_1 = require("./constants/spotMarkets");
|
|
6
6
|
exports.configs = {
|
|
7
7
|
devnet: {
|
|
8
8
|
ENV: 'devnet',
|
|
9
9
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
10
10
|
CLEARING_HOUSE_PROGRAM_ID: '3v1iEjbSSLSSYyt1pmx4UB5rqJGurmz71RibXF7X6UF3',
|
|
11
11
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
PERP_MARKETS: perpMarkets_1.DevnetPerpMarkets,
|
|
13
|
+
SPOT_MARKETS: spotMarkets_1.DevnetSpotMarkets,
|
|
14
14
|
},
|
|
15
15
|
'mainnet-beta': {
|
|
16
16
|
ENV: 'mainnet-beta',
|
|
17
17
|
PYTH_ORACLE_MAPPING_ADDRESS: 'AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J',
|
|
18
18
|
CLEARING_HOUSE_PROGRAM_ID: 'dammHkt7jmytvbS3nHTxQNEcP59aE57nxwV21YdqEDN',
|
|
19
19
|
USDC_MINT_ADDRESS: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
PERP_MARKETS: perpMarkets_1.MainnetMarkets,
|
|
21
|
+
SPOT_MARKETS: spotMarkets_1.MainnetSpotMarkets,
|
|
22
22
|
},
|
|
23
23
|
};
|
|
24
24
|
let currentConfig = exports.configs.devnet;
|
|
@@ -40,28 +40,28 @@ const initialize = (props) => {
|
|
|
40
40
|
return currentConfig;
|
|
41
41
|
};
|
|
42
42
|
exports.initialize = initialize;
|
|
43
|
-
function
|
|
44
|
-
const
|
|
45
|
-
const
|
|
43
|
+
function getMarketsAndOraclesForSubscription(env) {
|
|
44
|
+
const perpMarketIndexes = [];
|
|
45
|
+
const spotMarketIndexes = [];
|
|
46
46
|
const oracleInfos = new Map();
|
|
47
|
-
for (const market of
|
|
48
|
-
|
|
47
|
+
for (const market of perpMarkets_1.PerpMarkets[env]) {
|
|
48
|
+
perpMarketIndexes.push(market.marketIndex);
|
|
49
49
|
oracleInfos.set(market.oracle.toString(), {
|
|
50
50
|
publicKey: market.oracle,
|
|
51
51
|
source: market.oracleSource,
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
|
-
for (const
|
|
55
|
-
|
|
56
|
-
oracleInfos.set(
|
|
57
|
-
publicKey:
|
|
58
|
-
source:
|
|
54
|
+
for (const spotMarket of spotMarkets_1.SpotMarkets[env]) {
|
|
55
|
+
spotMarketIndexes.push(spotMarket.marketIndex);
|
|
56
|
+
oracleInfos.set(spotMarket.oracle.toString(), {
|
|
57
|
+
publicKey: spotMarket.oracle,
|
|
58
|
+
source: spotMarket.oracleSource,
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
return {
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
perpMarketIndexes: perpMarketIndexes,
|
|
63
|
+
spotMarketIndexes: spotMarketIndexes,
|
|
64
64
|
oracleInfos: Array.from(oracleInfos.values()),
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
|
-
exports.
|
|
67
|
+
exports.getMarketsAndOraclesForSubscription = getMarketsAndOraclesForSubscription;
|