@drift-labs/sdk 2.60.0-beta.8 → 2.60.0-beta.9

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 CHANGED
@@ -1 +1 @@
1
- 2.60.0-beta.8
1
+ 2.60.0-beta.9
@@ -139,6 +139,7 @@ exports.MainnetSpotMarkets = [
139
139
  precision: new __1.BN(10).pow(numericConstants_1.NINE),
140
140
  precisionExp: numericConstants_1.NINE,
141
141
  serumMarket: new web3_js_1.PublicKey('H87FfmHABiZLRGrDsXRZtqq25YpARzaokCzL1vMYGiep'),
142
+ phoenixMarket: new web3_js_1.PublicKey('BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ'),
142
143
  },
143
144
  {
144
145
  symbol: 'WIF',
@@ -149,6 +150,7 @@ exports.MainnetSpotMarkets = [
149
150
  precision: new __1.BN(10).pow(numericConstants_1.SIX),
150
151
  precisionExp: numericConstants_1.SIX,
151
152
  serumMarket: new web3_js_1.PublicKey('2BtDHBTCTUxvdur498ZEcMgimasaFrY5GzLv8wS8XgCb'),
153
+ phoenixMarket: new web3_js_1.PublicKey('6ojSigXF7nDPyhFRgmn3V9ywhYseKF9J32ZrranMGVSX'),
152
154
  },
153
155
  {
154
156
  symbol: 'JUP',
package/lib/user.d.ts CHANGED
@@ -249,6 +249,7 @@ export declare class User {
249
249
  * @returns Precision : PRICE_PRECISION
250
250
  */
251
251
  liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN): BN;
252
+ calculateEntriesEffectOnFreeCollateral(market: PerpMarketAccount, oraclePrice: BN, perpPosition: PerpPosition, positionBaseSizeChange: BN, estimatedEntryPrice: BN): BN;
252
253
  calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN): BN | undefined;
253
254
  calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN): BN;
254
255
  /**
package/lib/user.js CHANGED
@@ -1088,24 +1088,12 @@ class User {
1088
1088
  let freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(maintenanceMarginRequirement));
1089
1089
  const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
1090
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
- }
1106
1091
  const market = this.driftClient.getPerpMarketAccount(marketIndex);
1107
1092
  const currentPerpPosition = this.getPerpPositionWithLPSettle(marketIndex, undefined, true)[0] ||
1108
1093
  this.getEmptyPosition(marketIndex);
1094
+ positionBaseSizeChange = (0, _1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.amm.orderStepSize);
1095
+ const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice);
1096
+ freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
1109
1097
  let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange);
1110
1098
  if (!freeCollateralDelta) {
1111
1099
  return new _1.BN(-1);
@@ -1133,6 +1121,47 @@ class User {
1133
1121
  }
1134
1122
  return liqPrice;
1135
1123
  }
1124
+ calculateEntriesEffectOnFreeCollateral(market, oraclePrice, perpPosition, positionBaseSizeChange, estimatedEntryPrice) {
1125
+ let freeCollateralChange = numericConstants_1.ZERO;
1126
+ // update free collateral to account for change in pnl from new position
1127
+ if (!estimatedEntryPrice.eq(numericConstants_1.ZERO) && !positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
1128
+ const costBasis = oraclePrice
1129
+ .mul(positionBaseSizeChange.abs())
1130
+ .div(numericConstants_1.BASE_PRECISION);
1131
+ const newPositionValue = estimatedEntryPrice
1132
+ .mul(positionBaseSizeChange.abs())
1133
+ .div(numericConstants_1.BASE_PRECISION);
1134
+ if (positionBaseSizeChange.gt(numericConstants_1.ZERO)) {
1135
+ freeCollateralChange = costBasis.sub(newPositionValue);
1136
+ }
1137
+ else {
1138
+ freeCollateralChange = newPositionValue.sub(costBasis);
1139
+ }
1140
+ // assume worst fee tier
1141
+ const takerFeeTier = this.driftClient.getStateAccount().perpFeeStructure.feeTiers[0];
1142
+ const takerFee = newPositionValue
1143
+ .muln(takerFeeTier.feeNumerator)
1144
+ .divn(takerFeeTier.feeDenominator);
1145
+ freeCollateralChange = freeCollateralChange.sub(takerFee);
1146
+ }
1147
+ const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
1148
+ const marginRatioBefore = (0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), 'Maintenance');
1149
+ const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
1150
+ 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
+ // update free collateral to account for new margin requirement from position change
1158
+ freeCollateralChange = freeCollateralChange.sub(positionBaseSizeChange
1159
+ .mul(oraclePrice)
1160
+ .div(numericConstants_1.BASE_PRECISION)
1161
+ .mul(new _1.BN(newMarginRatio))
1162
+ .div(numericConstants_1.MARGIN_PRECISION));
1163
+ return freeCollateralChange;
1164
+ }
1136
1165
  calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange) {
1137
1166
  const currentBaseAssetAmount = perpPosition.baseAssetAmount;
1138
1167
  const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.60.0-beta.8",
3
+ "version": "2.60.0-beta.9",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -170,6 +170,9 @@ export const MainnetSpotMarkets: SpotMarketConfig[] = [
170
170
  precision: new BN(10).pow(NINE),
171
171
  precisionExp: NINE,
172
172
  serumMarket: new PublicKey('H87FfmHABiZLRGrDsXRZtqq25YpARzaokCzL1vMYGiep'),
173
+ phoenixMarket: new PublicKey(
174
+ 'BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ'
175
+ ),
173
176
  },
174
177
  {
175
178
  symbol: 'WIF',
@@ -180,6 +183,9 @@ export const MainnetSpotMarkets: SpotMarketConfig[] = [
180
183
  precision: new BN(10).pow(SIX),
181
184
  precisionExp: SIX,
182
185
  serumMarket: new PublicKey('2BtDHBTCTUxvdur498ZEcMgimasaFrY5GzLv8wS8XgCb'),
186
+ phoenixMarket: new PublicKey(
187
+ '6ojSigXF7nDPyhFRgmn3V9ywhYseKF9J32ZrranMGVSX'
188
+ ),
183
189
  },
184
190
  {
185
191
  symbol: 'JUP',
package/src/user.ts CHANGED
@@ -60,6 +60,7 @@ import {
60
60
  sigNum,
61
61
  SpotBalanceType,
62
62
  SpotMarketAccount,
63
+ standardizeBaseAssetAmount,
63
64
  } from '.';
64
65
  import {
65
66
  calculateAssetWeight,
@@ -1989,26 +1990,27 @@ export class User {
1989
1990
  const oraclePrice =
1990
1991
  this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
1991
1992
 
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
-
2007
1993
  const market = this.driftClient.getPerpMarketAccount(marketIndex);
2008
1994
  const currentPerpPosition =
2009
1995
  this.getPerpPositionWithLPSettle(marketIndex, undefined, true)[0] ||
2010
1996
  this.getEmptyPosition(marketIndex);
2011
1997
 
1998
+ positionBaseSizeChange = standardizeBaseAssetAmount(
1999
+ positionBaseSizeChange,
2000
+ market.amm.orderStepSize
2001
+ );
2002
+
2003
+ const freeCollateralChangeFromNewPosition =
2004
+ this.calculateEntriesEffectOnFreeCollateral(
2005
+ market,
2006
+ oraclePrice,
2007
+ currentPerpPosition,
2008
+ positionBaseSizeChange,
2009
+ estimatedEntryPrice
2010
+ );
2011
+
2012
+ freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
2013
+
2012
2014
  let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(
2013
2015
  market,
2014
2016
  currentPerpPosition,
@@ -2064,6 +2066,78 @@ export class User {
2064
2066
  return liqPrice;
2065
2067
  }
2066
2068
 
2069
+ calculateEntriesEffectOnFreeCollateral(
2070
+ market: PerpMarketAccount,
2071
+ oraclePrice: BN,
2072
+ perpPosition: PerpPosition,
2073
+ positionBaseSizeChange: BN,
2074
+ estimatedEntryPrice: BN
2075
+ ): BN {
2076
+ let freeCollateralChange = ZERO;
2077
+
2078
+ // update free collateral to account for change in pnl from new position
2079
+ if (!estimatedEntryPrice.eq(ZERO) && !positionBaseSizeChange.eq(ZERO)) {
2080
+ const costBasis = oraclePrice
2081
+ .mul(positionBaseSizeChange.abs())
2082
+ .div(BASE_PRECISION);
2083
+ const newPositionValue = estimatedEntryPrice
2084
+ .mul(positionBaseSizeChange.abs())
2085
+ .div(BASE_PRECISION);
2086
+ if (positionBaseSizeChange.gt(ZERO)) {
2087
+ freeCollateralChange = costBasis.sub(newPositionValue);
2088
+ } else {
2089
+ freeCollateralChange = newPositionValue.sub(costBasis);
2090
+ }
2091
+
2092
+ // assume worst fee tier
2093
+ const takerFeeTier =
2094
+ this.driftClient.getStateAccount().perpFeeStructure.feeTiers[0];
2095
+ const takerFee = newPositionValue
2096
+ .muln(takerFeeTier.feeNumerator)
2097
+ .divn(takerFeeTier.feeDenominator);
2098
+ freeCollateralChange = freeCollateralChange.sub(takerFee);
2099
+ }
2100
+
2101
+ const worstCaseBaseAssetAmount =
2102
+ calculateWorstCaseBaseAssetAmount(perpPosition);
2103
+
2104
+ const marginRatioBefore = calculateMarketMarginRatio(
2105
+ market,
2106
+ worstCaseBaseAssetAmount.abs(),
2107
+ 'Maintenance'
2108
+ );
2109
+
2110
+ const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(
2111
+ positionBaseSizeChange
2112
+ );
2113
+
2114
+ const newMarginRatio = calculateMarketMarginRatio(
2115
+ market,
2116
+ newWorstCaseBaseAssetAmount.abs(),
2117
+ 'Maintenance'
2118
+ );
2119
+
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
+ // update free collateral to account for new margin requirement from position change
2130
+ freeCollateralChange = freeCollateralChange.sub(
2131
+ positionBaseSizeChange
2132
+ .mul(oraclePrice)
2133
+ .div(BASE_PRECISION)
2134
+ .mul(new BN(newMarginRatio))
2135
+ .div(MARGIN_PRECISION)
2136
+ );
2137
+
2138
+ return freeCollateralChange;
2139
+ }
2140
+
2067
2141
  calculateFreeCollateralDeltaForPerp(
2068
2142
  market: PerpMarketAccount,
2069
2143
  perpPosition: PerpPosition,