@drift-labs/sdk 2.31.1-beta.24 → 2.31.1-beta.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/VERSION +1 -1
- package/lib/idl/drift.json +1 -1
- package/lib/user.d.ts +5 -2
- package/lib/user.js +15 -12
- package/package.json +1 -1
- package/src/idl/drift.json +1 -1
- package/src/user.ts +26 -17
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.31.1-beta.
|
|
1
|
+
2.31.1-beta.26
|
package/lib/idl/drift.json
CHANGED
package/lib/user.d.ts
CHANGED
|
@@ -127,7 +127,6 @@ export declare class User {
|
|
|
127
127
|
getSpotLiabilityValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory, liquidationBuffer?: BN, strict?: boolean, now?: BN): BN;
|
|
128
128
|
getSpotMarketAssetValue(marketIndex?: number, marginCategory?: MarginCategory, includeOpenOrders?: boolean, strict?: boolean, now?: BN): BN;
|
|
129
129
|
getSpotAssetValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory, strict?: boolean, now?: BN): BN;
|
|
130
|
-
getSpotTokenAmount(marketIndex: number): BN;
|
|
131
130
|
getSpotPositionValue(marketIndex: number, marginCategory?: MarginCategory, includeOpenOrders?: boolean, strict?: boolean, now?: BN): BN;
|
|
132
131
|
getNetSpotMarketValue(withWeightMarginCategory?: MarginCategory): BN;
|
|
133
132
|
/**
|
|
@@ -267,12 +266,16 @@ export declare class User {
|
|
|
267
266
|
* @param outMarketIndex
|
|
268
267
|
* @param calculateSwap function to similate in to out swa
|
|
269
268
|
* @param iterationLimit how long to run appromixation before erroring out
|
|
269
|
+
* @param inDelta how much to initially add to inAmount
|
|
270
|
+
* @param outDelta how much to initially add to outAmount
|
|
270
271
|
*/
|
|
271
|
-
getMaxSwapAmount({ inMarketIndex, outMarketIndex, calculateSwap, iterationLimit, }: {
|
|
272
|
+
getMaxSwapAmount({ inMarketIndex, outMarketIndex, calculateSwap, iterationLimit, inDelta, outDelta, }: {
|
|
272
273
|
inMarketIndex: number;
|
|
273
274
|
outMarketIndex: number;
|
|
274
275
|
calculateSwap?: (inAmount: BN) => BN;
|
|
275
276
|
iterationLimit?: number;
|
|
277
|
+
inDelta?: BN;
|
|
278
|
+
outDelta?: BN;
|
|
276
279
|
}): {
|
|
277
280
|
inAmount: BN;
|
|
278
281
|
outAmount: BN;
|
package/lib/user.js
CHANGED
|
@@ -502,11 +502,6 @@ class User {
|
|
|
502
502
|
}
|
|
503
503
|
return assetValue;
|
|
504
504
|
}
|
|
505
|
-
getSpotTokenAmount(marketIndex) {
|
|
506
|
-
var _a;
|
|
507
|
-
const spotPosition = (_a = this.getSpotPosition(marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptySpotPosition(marketIndex);
|
|
508
|
-
return (0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, this.driftClient.getSpotMarketAccount(marketIndex), spotPosition.balanceType);
|
|
509
|
-
}
|
|
510
505
|
getSpotPositionValue(marketIndex, marginCategory, includeOpenOrders, strict = false, now) {
|
|
511
506
|
const { totalAssetValue, totalLiabilityValue } = this.getSpotMarketAssetAndLiabilityValue(marketIndex, marginCategory, undefined, includeOpenOrders, strict, now);
|
|
512
507
|
return totalAssetValue.sub(totalLiabilityValue);
|
|
@@ -1083,13 +1078,13 @@ class User {
|
|
|
1083
1078
|
// if the user is buying/selling and already short/long, need to account for closing out short/long
|
|
1084
1079
|
if ((0, types_1.isVariant)(direction, 'long') && currentSpotMarketNetValue.lt(numericConstants_1.ZERO)) {
|
|
1085
1080
|
tradeAmount = currentSpotMarketNetValue.abs();
|
|
1086
|
-
const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.
|
|
1081
|
+
const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getTokenAmount(targetMarketIndex).abs(), _1.SpotBalanceType.BORROW);
|
|
1087
1082
|
freeCollateral = freeCollateral.add(tradeAmount.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
|
|
1088
1083
|
}
|
|
1089
1084
|
else if ((0, types_1.isVariant)(direction, 'short') &&
|
|
1090
1085
|
currentSpotMarketNetValue.gt(numericConstants_1.ZERO)) {
|
|
1091
1086
|
tradeAmount = currentSpotMarketNetValue;
|
|
1092
|
-
const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.
|
|
1087
|
+
const marginRatio = (0, _1.calculateSpotMarketMarginRatio)(market, 'Initial', this.getTokenAmount(targetMarketIndex), _1.SpotBalanceType.DEPOSIT);
|
|
1093
1088
|
freeCollateral = freeCollateral.add(tradeAmount.mul(new _1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
|
|
1094
1089
|
}
|
|
1095
1090
|
tradeAmount = tradeAmount.add(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio)));
|
|
@@ -1110,8 +1105,10 @@ class User {
|
|
|
1110
1105
|
* @param outMarketIndex
|
|
1111
1106
|
* @param calculateSwap function to similate in to out swa
|
|
1112
1107
|
* @param iterationLimit how long to run appromixation before erroring out
|
|
1108
|
+
* @param inDelta how much to initially add to inAmount
|
|
1109
|
+
* @param outDelta how much to initially add to outAmount
|
|
1113
1110
|
*/
|
|
1114
|
-
getMaxSwapAmount({ inMarketIndex, outMarketIndex, calculateSwap, iterationLimit = 1000, }) {
|
|
1111
|
+
getMaxSwapAmount({ inMarketIndex, outMarketIndex, calculateSwap, iterationLimit = 1000, inDelta, outDelta, }) {
|
|
1115
1112
|
const inMarket = this.driftClient.getSpotMarketAccount(inMarketIndex);
|
|
1116
1113
|
const outMarket = this.driftClient.getSpotMarketAccount(outMarketIndex);
|
|
1117
1114
|
const inOraclePrice = this.getOracleDataForSpotMarket(inMarketIndex).price;
|
|
@@ -1119,10 +1116,16 @@ class User {
|
|
|
1119
1116
|
const inPrecision = new _1.BN(10 ** inMarket.decimals);
|
|
1120
1117
|
const outPrecision = new _1.BN(10 ** outMarket.decimals);
|
|
1121
1118
|
const outSaferThanIn = inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
1122
|
-
|
|
1119
|
+
let inSpotPosition = this.getSpotPosition(inMarketIndex) ||
|
|
1123
1120
|
this.getEmptySpotPosition(inMarketIndex);
|
|
1124
|
-
|
|
1121
|
+
if (inDelta) {
|
|
1122
|
+
inSpotPosition = this.cloneAndUpdateSpotPosition(inSpotPosition, inDelta, inMarket);
|
|
1123
|
+
}
|
|
1124
|
+
let outSpotPosition = this.getSpotPosition(outMarketIndex) ||
|
|
1125
1125
|
this.getEmptySpotPosition(outMarketIndex);
|
|
1126
|
+
if (outDelta) {
|
|
1127
|
+
outSpotPosition = this.cloneAndUpdateSpotPosition(outSpotPosition, outDelta, outMarket);
|
|
1128
|
+
}
|
|
1126
1129
|
const freeCollateral = this.getFreeCollateral();
|
|
1127
1130
|
const inContributionInitial = this.calculateSpotPositionFreeCollateralContribution(inSpotPosition);
|
|
1128
1131
|
const { totalAssetValue: inTotalAssetValueInitial, totalLiabilityValue: inTotalLiabilityValueInitial, } = this.calculateSpotPositionLeverageContribution(inSpotPosition);
|
|
@@ -1141,9 +1144,9 @@ class User {
|
|
|
1141
1144
|
}
|
|
1142
1145
|
let inSwap = numericConstants_1.ZERO;
|
|
1143
1146
|
let outSwap = numericConstants_1.ZERO;
|
|
1144
|
-
const inTokenAmount = this.
|
|
1147
|
+
const inTokenAmount = this.getTokenAmount(inMarketIndex);
|
|
1145
1148
|
if (freeCollateral.lt(numericConstants_1.ONE)) {
|
|
1146
|
-
if (outSaferThanIn) {
|
|
1149
|
+
if (outSaferThanIn && inTokenAmount.gt(numericConstants_1.ZERO)) {
|
|
1147
1150
|
inSwap = inTokenAmount;
|
|
1148
1151
|
outSwap = calculateSwap(inSwap);
|
|
1149
1152
|
}
|
package/package.json
CHANGED
package/src/idl/drift.json
CHANGED
package/src/user.ts
CHANGED
|
@@ -934,17 +934,6 @@ export class User {
|
|
|
934
934
|
return assetValue;
|
|
935
935
|
}
|
|
936
936
|
|
|
937
|
-
public getSpotTokenAmount(marketIndex: number): BN {
|
|
938
|
-
const spotPosition =
|
|
939
|
-
this.getSpotPosition(marketIndex) ??
|
|
940
|
-
this.getEmptySpotPosition(marketIndex);
|
|
941
|
-
return getTokenAmount(
|
|
942
|
-
spotPosition.scaledBalance,
|
|
943
|
-
this.driftClient.getSpotMarketAccount(marketIndex),
|
|
944
|
-
spotPosition.balanceType
|
|
945
|
-
);
|
|
946
|
-
}
|
|
947
|
-
|
|
948
937
|
public getSpotPositionValue(
|
|
949
938
|
marketIndex: number,
|
|
950
939
|
marginCategory?: MarginCategory,
|
|
@@ -1953,7 +1942,7 @@ export class User {
|
|
|
1953
1942
|
const marginRatio = calculateSpotMarketMarginRatio(
|
|
1954
1943
|
market,
|
|
1955
1944
|
'Initial',
|
|
1956
|
-
this.
|
|
1945
|
+
this.getTokenAmount(targetMarketIndex).abs(),
|
|
1957
1946
|
SpotBalanceType.BORROW
|
|
1958
1947
|
);
|
|
1959
1948
|
freeCollateral = freeCollateral.add(
|
|
@@ -1967,7 +1956,7 @@ export class User {
|
|
|
1967
1956
|
const marginRatio = calculateSpotMarketMarginRatio(
|
|
1968
1957
|
market,
|
|
1969
1958
|
'Initial',
|
|
1970
|
-
this.
|
|
1959
|
+
this.getTokenAmount(targetMarketIndex),
|
|
1971
1960
|
SpotBalanceType.DEPOSIT
|
|
1972
1961
|
);
|
|
1973
1962
|
freeCollateral = freeCollateral.add(
|
|
@@ -1998,17 +1987,23 @@ export class User {
|
|
|
1998
1987
|
* @param outMarketIndex
|
|
1999
1988
|
* @param calculateSwap function to similate in to out swa
|
|
2000
1989
|
* @param iterationLimit how long to run appromixation before erroring out
|
|
1990
|
+
* @param inDelta how much to initially add to inAmount
|
|
1991
|
+
* @param outDelta how much to initially add to outAmount
|
|
2001
1992
|
*/
|
|
2002
1993
|
public getMaxSwapAmount({
|
|
2003
1994
|
inMarketIndex,
|
|
2004
1995
|
outMarketIndex,
|
|
2005
1996
|
calculateSwap,
|
|
2006
1997
|
iterationLimit = 1000,
|
|
1998
|
+
inDelta,
|
|
1999
|
+
outDelta,
|
|
2007
2000
|
}: {
|
|
2008
2001
|
inMarketIndex: number;
|
|
2009
2002
|
outMarketIndex: number;
|
|
2010
2003
|
calculateSwap?: (inAmount: BN) => BN;
|
|
2011
2004
|
iterationLimit?: number;
|
|
2005
|
+
inDelta?: BN;
|
|
2006
|
+
outDelta?: BN;
|
|
2012
2007
|
}): { inAmount: BN; outAmount: BN; leverage: BN } {
|
|
2013
2008
|
const inMarket = this.driftClient.getSpotMarketAccount(inMarketIndex);
|
|
2014
2009
|
const outMarket = this.driftClient.getSpotMarketAccount(outMarketIndex);
|
|
@@ -2023,12 +2018,26 @@ export class User {
|
|
|
2023
2018
|
const outSaferThanIn =
|
|
2024
2019
|
inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
2025
2020
|
|
|
2026
|
-
|
|
2021
|
+
let inSpotPosition =
|
|
2027
2022
|
this.getSpotPosition(inMarketIndex) ||
|
|
2028
2023
|
this.getEmptySpotPosition(inMarketIndex);
|
|
2029
|
-
|
|
2024
|
+
if (inDelta) {
|
|
2025
|
+
inSpotPosition = this.cloneAndUpdateSpotPosition(
|
|
2026
|
+
inSpotPosition,
|
|
2027
|
+
inDelta,
|
|
2028
|
+
inMarket
|
|
2029
|
+
);
|
|
2030
|
+
}
|
|
2031
|
+
let outSpotPosition =
|
|
2030
2032
|
this.getSpotPosition(outMarketIndex) ||
|
|
2031
2033
|
this.getEmptySpotPosition(outMarketIndex);
|
|
2034
|
+
if (outDelta) {
|
|
2035
|
+
outSpotPosition = this.cloneAndUpdateSpotPosition(
|
|
2036
|
+
outSpotPosition,
|
|
2037
|
+
outDelta,
|
|
2038
|
+
outMarket
|
|
2039
|
+
);
|
|
2040
|
+
}
|
|
2032
2041
|
|
|
2033
2042
|
const freeCollateral = this.getFreeCollateral();
|
|
2034
2043
|
|
|
@@ -2063,9 +2072,9 @@ export class User {
|
|
|
2063
2072
|
|
|
2064
2073
|
let inSwap = ZERO;
|
|
2065
2074
|
let outSwap = ZERO;
|
|
2066
|
-
const inTokenAmount = this.
|
|
2075
|
+
const inTokenAmount = this.getTokenAmount(inMarketIndex);
|
|
2067
2076
|
if (freeCollateral.lt(ONE)) {
|
|
2068
|
-
if (outSaferThanIn) {
|
|
2077
|
+
if (outSaferThanIn && inTokenAmount.gt(ZERO)) {
|
|
2069
2078
|
inSwap = inTokenAmount;
|
|
2070
2079
|
outSwap = calculateSwap(inSwap);
|
|
2071
2080
|
}
|