@drift-labs/sdk 2.60.0-beta.5 → 2.60.0-beta.6
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 +1 -1
- package/lib/user.js +19 -4
- package/package.json +1 -1
- package/src/user.ts +24 -6
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.60.0-beta.
|
|
1
|
+
2.60.0-beta.6
|
package/lib/user.d.ts
CHANGED
|
@@ -248,7 +248,7 @@ export declare class User {
|
|
|
248
248
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
249
249
|
* @returns Precision : PRICE_PRECISION
|
|
250
250
|
*/
|
|
251
|
-
liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN): BN;
|
|
251
|
+
liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN): BN;
|
|
252
252
|
calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN): BN | undefined;
|
|
253
253
|
calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN): BN;
|
|
254
254
|
/**
|
package/lib/user.js
CHANGED
|
@@ -1082,10 +1082,27 @@ class User {
|
|
|
1082
1082
|
* @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
|
|
1083
1083
|
* @returns Precision : PRICE_PRECISION
|
|
1084
1084
|
*/
|
|
1085
|
-
liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO) {
|
|
1085
|
+
liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO) {
|
|
1086
1086
|
const totalCollateral = this.getTotalCollateral('Maintenance');
|
|
1087
1087
|
const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
|
|
1088
|
-
|
|
1088
|
+
let freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(maintenanceMarginRequirement));
|
|
1089
|
+
const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1090
|
+
const oraclePrice = this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
1091
|
+
// update free collateral to accoutn from pnl based on entry price
|
|
1092
|
+
if (!estimatedEntryPrice.eq(numericConstants_1.ZERO) && !positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
|
|
1093
|
+
const costBasis = oraclePrice
|
|
1094
|
+
.mul(positionBaseSizeChange.abs())
|
|
1095
|
+
.div(numericConstants_1.BASE_PRECISION);
|
|
1096
|
+
const newPositionValue = estimatedEntryPrice
|
|
1097
|
+
.mul(positionBaseSizeChange.abs())
|
|
1098
|
+
.div(numericConstants_1.BASE_PRECISION);
|
|
1099
|
+
if (positionBaseSizeChange.gt(numericConstants_1.ZERO)) {
|
|
1100
|
+
freeCollateral = freeCollateral.add(costBasis.sub(newPositionValue));
|
|
1101
|
+
}
|
|
1102
|
+
else {
|
|
1103
|
+
freeCollateral = freeCollateral.add(newPositionValue.sub(costBasis));
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1089
1106
|
const market = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
1090
1107
|
const currentPerpPosition = this.getPerpPositionWithLPSettle(marketIndex, undefined, true)[0] ||
|
|
1091
1108
|
this.getEmptyPosition(marketIndex);
|
|
@@ -1093,7 +1110,6 @@ class User {
|
|
|
1093
1110
|
if (!freeCollateralDelta) {
|
|
1094
1111
|
return new _1.BN(-1);
|
|
1095
1112
|
}
|
|
1096
|
-
const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1097
1113
|
const spotMarketWithSameOracle = this.driftClient
|
|
1098
1114
|
.getSpotMarketAccounts()
|
|
1099
1115
|
.find((market) => market.oracle.equals(oracle));
|
|
@@ -1108,7 +1124,6 @@ class User {
|
|
|
1108
1124
|
if (freeCollateralDelta.eq(numericConstants_1.ZERO)) {
|
|
1109
1125
|
return new _1.BN(-1);
|
|
1110
1126
|
}
|
|
1111
|
-
const oraclePrice = this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
1112
1127
|
const liqPriceDelta = freeCollateral
|
|
1113
1128
|
.mul(numericConstants_1.QUOTE_PRECISION)
|
|
1114
1129
|
.div(freeCollateralDelta);
|
package/package.json
CHANGED
package/src/user.ts
CHANGED
|
@@ -1973,15 +1973,37 @@ export class User {
|
|
|
1973
1973
|
*/
|
|
1974
1974
|
public liquidationPrice(
|
|
1975
1975
|
marketIndex: number,
|
|
1976
|
-
positionBaseSizeChange: BN = ZERO
|
|
1976
|
+
positionBaseSizeChange: BN = ZERO,
|
|
1977
|
+
estimatedEntryPrice: BN = ZERO
|
|
1977
1978
|
): BN {
|
|
1978
1979
|
const totalCollateral = this.getTotalCollateral('Maintenance');
|
|
1979
1980
|
const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
|
|
1980
|
-
|
|
1981
|
+
let freeCollateral = BN.max(
|
|
1981
1982
|
ZERO,
|
|
1982
1983
|
totalCollateral.sub(maintenanceMarginRequirement)
|
|
1983
1984
|
);
|
|
1984
1985
|
|
|
1986
|
+
const oracle =
|
|
1987
|
+
this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1988
|
+
|
|
1989
|
+
const oraclePrice =
|
|
1990
|
+
this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
1991
|
+
|
|
1992
|
+
// update free collateral to accoutn from pnl based on entry price
|
|
1993
|
+
if (!estimatedEntryPrice.eq(ZERO) && !positionBaseSizeChange.eq(ZERO)) {
|
|
1994
|
+
const costBasis = oraclePrice
|
|
1995
|
+
.mul(positionBaseSizeChange.abs())
|
|
1996
|
+
.div(BASE_PRECISION);
|
|
1997
|
+
const newPositionValue = estimatedEntryPrice
|
|
1998
|
+
.mul(positionBaseSizeChange.abs())
|
|
1999
|
+
.div(BASE_PRECISION);
|
|
2000
|
+
if (positionBaseSizeChange.gt(ZERO)) {
|
|
2001
|
+
freeCollateral = freeCollateral.add(costBasis.sub(newPositionValue));
|
|
2002
|
+
} else {
|
|
2003
|
+
freeCollateral = freeCollateral.add(newPositionValue.sub(costBasis));
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
|
|
1985
2007
|
const market = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
1986
2008
|
const currentPerpPosition =
|
|
1987
2009
|
this.getPerpPositionWithLPSettle(marketIndex, undefined, true)[0] ||
|
|
@@ -1997,8 +2019,6 @@ export class User {
|
|
|
1997
2019
|
return new BN(-1);
|
|
1998
2020
|
}
|
|
1999
2021
|
|
|
2000
|
-
const oracle =
|
|
2001
|
-
this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
2002
2022
|
const spotMarketWithSameOracle = this.driftClient
|
|
2003
2023
|
.getSpotMarketAccounts()
|
|
2004
2024
|
.find((market) => market.oracle.equals(oracle));
|
|
@@ -2031,8 +2051,6 @@ export class User {
|
|
|
2031
2051
|
return new BN(-1);
|
|
2032
2052
|
}
|
|
2033
2053
|
|
|
2034
|
-
const oraclePrice =
|
|
2035
|
-
this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
2036
2054
|
const liqPriceDelta = freeCollateral
|
|
2037
2055
|
.mul(QUOTE_PRECISION)
|
|
2038
2056
|
.div(freeCollateralDelta);
|