@drift-labs/sdk 2.60.0-beta.11 → 2.60.0-beta.13
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/VERSION +1 -1
- package/lib/user.d.ts +5 -4
- package/lib/user.js +19 -21
- package/package.json +1 -1
- package/src/user.ts +30 -32
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.60.0-beta.
|
|
1
|
+
2.60.0-beta.13
|
package/lib/user.d.ts
CHANGED
|
@@ -111,7 +111,7 @@ export declare class User {
|
|
|
111
111
|
/**
|
|
112
112
|
* @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
|
|
113
113
|
*/
|
|
114
|
-
getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean): BN;
|
|
114
|
+
getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean, includeOpenOrders?: boolean): BN;
|
|
115
115
|
/**
|
|
116
116
|
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
117
117
|
*/
|
|
@@ -246,12 +246,13 @@ export declare class User {
|
|
|
246
246
|
* Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
|
|
247
247
|
* @param marketIndex
|
|
248
248
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
249
|
+
* @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
|
|
249
250
|
* @returns Precision : PRICE_PRECISION
|
|
250
251
|
*/
|
|
251
|
-
liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN): BN;
|
|
252
|
+
liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN, marginCategory?: MarginCategory): BN;
|
|
252
253
|
calculateEntriesEffectOnFreeCollateral(market: PerpMarketAccount, oraclePrice: BN, perpPosition: PerpPosition, positionBaseSizeChange: BN, estimatedEntryPrice: BN): BN;
|
|
253
|
-
calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN): BN | undefined;
|
|
254
|
-
calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN): BN;
|
|
254
|
+
calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN, marginCategory?: MarginCategory): BN | undefined;
|
|
255
|
+
calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN, marginCategory?: MarginCategory): BN;
|
|
255
256
|
/**
|
|
256
257
|
* Calculates the estimated liquidation price for a position after closing a quote amount of the position.
|
|
257
258
|
* @param positionMarketIndex
|
package/lib/user.js
CHANGED
|
@@ -432,8 +432,8 @@ class User {
|
|
|
432
432
|
/**
|
|
433
433
|
* @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
|
|
434
434
|
*/
|
|
435
|
-
getMarginRequirement(marginCategory, liquidationBuffer, strict = false) {
|
|
436
|
-
return this.getTotalPerpPositionValue(marginCategory, liquidationBuffer,
|
|
435
|
+
getMarginRequirement(marginCategory, liquidationBuffer, strict = false, includeOpenOrders = true) {
|
|
436
|
+
return this.getTotalPerpPositionValue(marginCategory, liquidationBuffer, includeOpenOrders, strict).add(this.getSpotMarketLiabilityValue(undefined, marginCategory, liquidationBuffer, includeOpenOrders, strict));
|
|
437
437
|
}
|
|
438
438
|
/**
|
|
439
439
|
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
@@ -1080,12 +1080,13 @@ class User {
|
|
|
1080
1080
|
* Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
|
|
1081
1081
|
* @param marketIndex
|
|
1082
1082
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
1083
|
+
* @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
|
|
1083
1084
|
* @returns Precision : PRICE_PRECISION
|
|
1084
1085
|
*/
|
|
1085
|
-
liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO) {
|
|
1086
|
-
const totalCollateral = this.getTotalCollateral(
|
|
1087
|
-
const
|
|
1088
|
-
let freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(
|
|
1086
|
+
liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance') {
|
|
1087
|
+
const totalCollateral = this.getTotalCollateral(marginCategory);
|
|
1088
|
+
const marginRequirement = this.getMarginRequirement(marginCategory, undefined, false);
|
|
1089
|
+
let freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(marginRequirement));
|
|
1089
1090
|
const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1090
1091
|
const oraclePrice = this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
1091
1092
|
const market = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
@@ -1094,7 +1095,7 @@ class User {
|
|
|
1094
1095
|
positionBaseSizeChange = (0, _1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.amm.orderStepSize);
|
|
1095
1096
|
const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice);
|
|
1096
1097
|
freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
|
|
1097
|
-
let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange);
|
|
1098
|
+
let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, marginCategory);
|
|
1098
1099
|
if (!freeCollateralDelta) {
|
|
1099
1100
|
return new _1.BN(-1);
|
|
1100
1101
|
}
|
|
@@ -1105,7 +1106,7 @@ class User {
|
|
|
1105
1106
|
const spotPosition = this.getSpotPosition(spotMarketWithSameOracle.marketIndex);
|
|
1106
1107
|
if (spotPosition) {
|
|
1107
1108
|
const signedTokenAmount = (0, _1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketWithSameOracle, spotPosition.balanceType), spotPosition.balanceType);
|
|
1108
|
-
const spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount);
|
|
1109
|
+
const spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount, marginCategory);
|
|
1109
1110
|
freeCollateralDelta = freeCollateralDelta.add(spotFreeCollateralDelta || numericConstants_1.ZERO);
|
|
1110
1111
|
}
|
|
1111
1112
|
}
|
|
@@ -1135,6 +1136,8 @@ class User {
|
|
|
1135
1136
|
freeCollateralChange = costBasis.sub(newPositionValue);
|
|
1136
1137
|
}
|
|
1137
1138
|
else {
|
|
1139
|
+
console.log('newPositionValue', newPositionValue.toString());
|
|
1140
|
+
console.log('costBasis', costBasis.toString());
|
|
1138
1141
|
freeCollateralChange = newPositionValue.sub(costBasis);
|
|
1139
1142
|
}
|
|
1140
1143
|
// assume worst fee tier
|
|
@@ -1145,30 +1148,25 @@ class User {
|
|
|
1145
1148
|
freeCollateralChange = freeCollateralChange.sub(takerFee);
|
|
1146
1149
|
}
|
|
1147
1150
|
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
|
|
1148
|
-
const marginRatioBefore = (0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), 'Maintenance');
|
|
1149
1151
|
const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
|
|
1150
1152
|
const newMarginRatio = (0, _1.calculateMarketMarginRatio)(market, newWorstCaseBaseAssetAmount.abs(), 'Maintenance');
|
|
1151
|
-
// update free collateral to account for change in margin ratio from position change
|
|
1152
|
-
freeCollateralChange = freeCollateralChange.sub(worstCaseBaseAssetAmount
|
|
1153
|
-
.mul(oraclePrice)
|
|
1154
|
-
.div(numericConstants_1.BASE_PRECISION)
|
|
1155
|
-
.mul(new _1.BN(newMarginRatio - marginRatioBefore))
|
|
1156
|
-
.div(numericConstants_1.MARGIN_PRECISION));
|
|
1157
1153
|
// update free collateral to account for new margin requirement from position change
|
|
1158
|
-
freeCollateralChange = freeCollateralChange.sub(
|
|
1154
|
+
freeCollateralChange = freeCollateralChange.sub(newWorstCaseBaseAssetAmount
|
|
1155
|
+
.abs()
|
|
1156
|
+
.sub(worstCaseBaseAssetAmount.abs())
|
|
1159
1157
|
.mul(oraclePrice)
|
|
1160
1158
|
.div(numericConstants_1.BASE_PRECISION)
|
|
1161
1159
|
.mul(new _1.BN(newMarginRatio))
|
|
1162
1160
|
.div(numericConstants_1.MARGIN_PRECISION));
|
|
1163
1161
|
return freeCollateralChange;
|
|
1164
1162
|
}
|
|
1165
|
-
calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange) {
|
|
1163
|
+
calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange, marginCategory = 'Maintenance') {
|
|
1166
1164
|
const currentBaseAssetAmount = perpPosition.baseAssetAmount;
|
|
1167
1165
|
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
|
|
1168
1166
|
const orderBaseAssetAmount = worstCaseBaseAssetAmount.sub(currentBaseAssetAmount);
|
|
1169
1167
|
const proposedBaseAssetAmount = currentBaseAssetAmount.add(positionBaseSizeChange);
|
|
1170
1168
|
const proposedWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
|
|
1171
|
-
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedWorstCaseBaseAssetAmount.abs(),
|
|
1169
|
+
const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedWorstCaseBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio);
|
|
1172
1170
|
const marginRatioQuotePrecision = new _1.BN(marginRatio)
|
|
1173
1171
|
.mul(numericConstants_1.QUOTE_PRECISION)
|
|
1174
1172
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
@@ -1194,17 +1192,17 @@ class User {
|
|
|
1194
1192
|
}
|
|
1195
1193
|
return freeCollateralDelta;
|
|
1196
1194
|
}
|
|
1197
|
-
calculateFreeCollateralDeltaForSpot(market, signedTokenAmount) {
|
|
1195
|
+
calculateFreeCollateralDeltaForSpot(market, signedTokenAmount, marginCategory = 'Maintenance') {
|
|
1198
1196
|
const tokenPrecision = new _1.BN(Math.pow(10, market.decimals));
|
|
1199
1197
|
if (signedTokenAmount.gt(numericConstants_1.ZERO)) {
|
|
1200
|
-
const assetWeight = (0, spotBalance_1.calculateAssetWeight)(signedTokenAmount, this.driftClient.getOraclePriceDataAndSlot(market.oracle).data.price, market,
|
|
1198
|
+
const assetWeight = (0, spotBalance_1.calculateAssetWeight)(signedTokenAmount, this.driftClient.getOraclePriceDataAndSlot(market.oracle).data.price, market, marginCategory);
|
|
1201
1199
|
return numericConstants_1.QUOTE_PRECISION.mul(assetWeight)
|
|
1202
1200
|
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
|
|
1203
1201
|
.mul(signedTokenAmount)
|
|
1204
1202
|
.div(tokenPrecision);
|
|
1205
1203
|
}
|
|
1206
1204
|
else {
|
|
1207
|
-
const liabilityWeight = (0, spotBalance_1.calculateLiabilityWeight)(signedTokenAmount.abs(), market,
|
|
1205
|
+
const liabilityWeight = (0, spotBalance_1.calculateLiabilityWeight)(signedTokenAmount.abs(), market, marginCategory);
|
|
1208
1206
|
return numericConstants_1.QUOTE_PRECISION.neg()
|
|
1209
1207
|
.mul(liabilityWeight)
|
|
1210
1208
|
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
|
package/package.json
CHANGED
package/src/user.ts
CHANGED
|
@@ -674,19 +674,20 @@ export class User {
|
|
|
674
674
|
public getMarginRequirement(
|
|
675
675
|
marginCategory: MarginCategory,
|
|
676
676
|
liquidationBuffer?: BN,
|
|
677
|
-
strict = false
|
|
677
|
+
strict = false,
|
|
678
|
+
includeOpenOrders = true
|
|
678
679
|
): BN {
|
|
679
680
|
return this.getTotalPerpPositionValue(
|
|
680
681
|
marginCategory,
|
|
681
682
|
liquidationBuffer,
|
|
682
|
-
|
|
683
|
+
includeOpenOrders,
|
|
683
684
|
strict
|
|
684
685
|
).add(
|
|
685
686
|
this.getSpotMarketLiabilityValue(
|
|
686
687
|
undefined,
|
|
687
688
|
marginCategory,
|
|
688
689
|
liquidationBuffer,
|
|
689
|
-
|
|
690
|
+
includeOpenOrders,
|
|
690
691
|
strict
|
|
691
692
|
)
|
|
692
693
|
);
|
|
@@ -1970,19 +1971,22 @@ export class User {
|
|
|
1970
1971
|
* Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
|
|
1971
1972
|
* @param marketIndex
|
|
1972
1973
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
1974
|
+
* @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
|
|
1973
1975
|
* @returns Precision : PRICE_PRECISION
|
|
1974
1976
|
*/
|
|
1975
1977
|
public liquidationPrice(
|
|
1976
1978
|
marketIndex: number,
|
|
1977
1979
|
positionBaseSizeChange: BN = ZERO,
|
|
1978
|
-
estimatedEntryPrice: BN = ZERO
|
|
1980
|
+
estimatedEntryPrice: BN = ZERO,
|
|
1981
|
+
marginCategory: MarginCategory = 'Maintenance'
|
|
1979
1982
|
): BN {
|
|
1980
|
-
const totalCollateral = this.getTotalCollateral(
|
|
1981
|
-
const
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1983
|
+
const totalCollateral = this.getTotalCollateral(marginCategory);
|
|
1984
|
+
const marginRequirement = this.getMarginRequirement(
|
|
1985
|
+
marginCategory,
|
|
1986
|
+
undefined,
|
|
1987
|
+
false
|
|
1985
1988
|
);
|
|
1989
|
+
let freeCollateral = BN.max(ZERO, totalCollateral.sub(marginRequirement));
|
|
1986
1990
|
|
|
1987
1991
|
const oracle =
|
|
1988
1992
|
this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
@@ -2014,7 +2018,8 @@ export class User {
|
|
|
2014
2018
|
let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(
|
|
2015
2019
|
market,
|
|
2016
2020
|
currentPerpPosition,
|
|
2017
|
-
positionBaseSizeChange
|
|
2021
|
+
positionBaseSizeChange,
|
|
2022
|
+
marginCategory
|
|
2018
2023
|
);
|
|
2019
2024
|
|
|
2020
2025
|
if (!freeCollateralDelta) {
|
|
@@ -2041,7 +2046,8 @@ export class User {
|
|
|
2041
2046
|
const spotFreeCollateralDelta =
|
|
2042
2047
|
this.calculateFreeCollateralDeltaForSpot(
|
|
2043
2048
|
spotMarketWithSameOracle,
|
|
2044
|
-
signedTokenAmount
|
|
2049
|
+
signedTokenAmount,
|
|
2050
|
+
marginCategory
|
|
2045
2051
|
);
|
|
2046
2052
|
freeCollateralDelta = freeCollateralDelta.add(
|
|
2047
2053
|
spotFreeCollateralDelta || ZERO
|
|
@@ -2086,6 +2092,8 @@ export class User {
|
|
|
2086
2092
|
if (positionBaseSizeChange.gt(ZERO)) {
|
|
2087
2093
|
freeCollateralChange = costBasis.sub(newPositionValue);
|
|
2088
2094
|
} else {
|
|
2095
|
+
console.log('newPositionValue', newPositionValue.toString());
|
|
2096
|
+
console.log('costBasis', costBasis.toString());
|
|
2089
2097
|
freeCollateralChange = newPositionValue.sub(costBasis);
|
|
2090
2098
|
}
|
|
2091
2099
|
|
|
@@ -2101,12 +2109,6 @@ export class User {
|
|
|
2101
2109
|
const worstCaseBaseAssetAmount =
|
|
2102
2110
|
calculateWorstCaseBaseAssetAmount(perpPosition);
|
|
2103
2111
|
|
|
2104
|
-
const marginRatioBefore = calculateMarketMarginRatio(
|
|
2105
|
-
market,
|
|
2106
|
-
worstCaseBaseAssetAmount.abs(),
|
|
2107
|
-
'Maintenance'
|
|
2108
|
-
);
|
|
2109
|
-
|
|
2110
2112
|
const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(
|
|
2111
2113
|
positionBaseSizeChange
|
|
2112
2114
|
);
|
|
@@ -2117,18 +2119,11 @@ export class User {
|
|
|
2117
2119
|
'Maintenance'
|
|
2118
2120
|
);
|
|
2119
2121
|
|
|
2120
|
-
// update free collateral to account for change in margin ratio from position change
|
|
2121
|
-
freeCollateralChange = freeCollateralChange.sub(
|
|
2122
|
-
worstCaseBaseAssetAmount
|
|
2123
|
-
.mul(oraclePrice)
|
|
2124
|
-
.div(BASE_PRECISION)
|
|
2125
|
-
.mul(new BN(newMarginRatio - marginRatioBefore))
|
|
2126
|
-
.div(MARGIN_PRECISION)
|
|
2127
|
-
);
|
|
2128
|
-
|
|
2129
2122
|
// update free collateral to account for new margin requirement from position change
|
|
2130
2123
|
freeCollateralChange = freeCollateralChange.sub(
|
|
2131
|
-
|
|
2124
|
+
newWorstCaseBaseAssetAmount
|
|
2125
|
+
.abs()
|
|
2126
|
+
.sub(worstCaseBaseAssetAmount.abs())
|
|
2132
2127
|
.mul(oraclePrice)
|
|
2133
2128
|
.div(BASE_PRECISION)
|
|
2134
2129
|
.mul(new BN(newMarginRatio))
|
|
@@ -2141,7 +2136,8 @@ export class User {
|
|
|
2141
2136
|
calculateFreeCollateralDeltaForPerp(
|
|
2142
2137
|
market: PerpMarketAccount,
|
|
2143
2138
|
perpPosition: PerpPosition,
|
|
2144
|
-
positionBaseSizeChange: BN
|
|
2139
|
+
positionBaseSizeChange: BN,
|
|
2140
|
+
marginCategory: MarginCategory = 'Maintenance'
|
|
2145
2141
|
): BN | undefined {
|
|
2146
2142
|
const currentBaseAssetAmount = perpPosition.baseAssetAmount;
|
|
2147
2143
|
|
|
@@ -2160,7 +2156,8 @@ export class User {
|
|
|
2160
2156
|
const marginRatio = calculateMarketMarginRatio(
|
|
2161
2157
|
market,
|
|
2162
2158
|
proposedWorstCaseBaseAssetAmount.abs(),
|
|
2163
|
-
|
|
2159
|
+
marginCategory,
|
|
2160
|
+
this.getUserAccount().maxMarginRatio
|
|
2164
2161
|
);
|
|
2165
2162
|
const marginRatioQuotePrecision = new BN(marginRatio)
|
|
2166
2163
|
.mul(QUOTE_PRECISION)
|
|
@@ -2195,7 +2192,8 @@ export class User {
|
|
|
2195
2192
|
|
|
2196
2193
|
calculateFreeCollateralDeltaForSpot(
|
|
2197
2194
|
market: SpotMarketAccount,
|
|
2198
|
-
signedTokenAmount: BN
|
|
2195
|
+
signedTokenAmount: BN,
|
|
2196
|
+
marginCategory: MarginCategory = 'Maintenance'
|
|
2199
2197
|
): BN {
|
|
2200
2198
|
const tokenPrecision = new BN(Math.pow(10, market.decimals));
|
|
2201
2199
|
|
|
@@ -2204,7 +2202,7 @@ export class User {
|
|
|
2204
2202
|
signedTokenAmount,
|
|
2205
2203
|
this.driftClient.getOraclePriceDataAndSlot(market.oracle).data.price,
|
|
2206
2204
|
market,
|
|
2207
|
-
|
|
2205
|
+
marginCategory
|
|
2208
2206
|
);
|
|
2209
2207
|
|
|
2210
2208
|
return QUOTE_PRECISION.mul(assetWeight)
|
|
@@ -2215,7 +2213,7 @@ export class User {
|
|
|
2215
2213
|
const liabilityWeight = calculateLiabilityWeight(
|
|
2216
2214
|
signedTokenAmount.abs(),
|
|
2217
2215
|
market,
|
|
2218
|
-
|
|
2216
|
+
marginCategory
|
|
2219
2217
|
);
|
|
2220
2218
|
|
|
2221
2219
|
return QUOTE_PRECISION.neg()
|