@drift-labs/sdk 2.97.0-beta.16 → 2.97.0-beta.18
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/driftClient.d.ts +2 -2
- package/lib/driftClient.js +17 -4
- package/lib/math/margin.d.ts +3 -3
- package/lib/math/margin.js +9 -9
- package/lib/user.d.ts +1 -1
- package/lib/user.js +2 -2
- package/package.json +1 -1
- package/src/driftClient.ts +22 -3
- package/src/math/margin.ts +15 -11
- package/src/user.ts +5 -2
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.97.0-beta.
|
|
1
|
+
2.97.0-beta.18
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -791,8 +791,8 @@ export declare class DriftClient {
|
|
|
791
791
|
private getBuildEncodedVaaIxs;
|
|
792
792
|
enableUserHighLeverageMode(subAccountId: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
793
793
|
getEnableHighLeverageModeIx(subAccountId: number): Promise<anchor.web3.TransactionInstruction>;
|
|
794
|
-
disableUserHighLeverageMode(user: PublicKey, txParams?: TxParams): Promise<TransactionSignature>;
|
|
795
|
-
getDisableHighLeverageModeIx(user: PublicKey): Promise<anchor.web3.TransactionInstruction>;
|
|
794
|
+
disableUserHighLeverageMode(user: PublicKey, userAccount?: UserAccount, txParams?: TxParams): Promise<TransactionSignature>;
|
|
795
|
+
getDisableHighLeverageModeIx(user: PublicKey, userAccount?: UserAccount): Promise<anchor.web3.TransactionInstruction>;
|
|
796
796
|
fetchHighLeverageModeConfig(): Promise<HighLeverageModeConfig>;
|
|
797
797
|
private handleSignedTransaction;
|
|
798
798
|
private handlePreSignedTransaction;
|
package/lib/driftClient.js
CHANGED
|
@@ -3451,7 +3451,10 @@ class DriftClient {
|
|
|
3451
3451
|
const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
|
|
3452
3452
|
const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
3453
3453
|
const remainingAccounts = this.getRemainingAccounts({
|
|
3454
|
-
userAccounts: [
|
|
3454
|
+
userAccounts: [
|
|
3455
|
+
userAccount,
|
|
3456
|
+
...makerInfos.map((makerInfo) => makerInfo.makerUserAccount),
|
|
3457
|
+
],
|
|
3455
3458
|
useMarketLastSlotCache: true,
|
|
3456
3459
|
writablePerpMarketIndexes: [marketIndex],
|
|
3457
3460
|
});
|
|
@@ -4259,6 +4262,9 @@ class DriftClient {
|
|
|
4259
4262
|
return txSig;
|
|
4260
4263
|
}
|
|
4261
4264
|
async getEnableHighLeverageModeIx(subAccountId) {
|
|
4265
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
4266
|
+
userAccounts: [this.getUserAccount(subAccountId)],
|
|
4267
|
+
});
|
|
4262
4268
|
const ix = await this.program.instruction.enableUserHighLeverageMode(subAccountId, {
|
|
4263
4269
|
accounts: {
|
|
4264
4270
|
state: await this.getStatePublicKey(),
|
|
@@ -4266,14 +4272,20 @@ class DriftClient {
|
|
|
4266
4272
|
authority: this.wallet.publicKey,
|
|
4267
4273
|
highLeverageModeConfig: (0, pda_1.getHighLeverageModeConfigPublicKey)(this.program.programId),
|
|
4268
4274
|
},
|
|
4275
|
+
remainingAccounts,
|
|
4269
4276
|
});
|
|
4270
4277
|
return ix;
|
|
4271
4278
|
}
|
|
4272
|
-
async disableUserHighLeverageMode(user, txParams) {
|
|
4273
|
-
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getDisableHighLeverageModeIx(user), txParams), [], this.opts);
|
|
4279
|
+
async disableUserHighLeverageMode(user, userAccount, txParams) {
|
|
4280
|
+
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getDisableHighLeverageModeIx(user, userAccount), txParams), [], this.opts);
|
|
4274
4281
|
return txSig;
|
|
4275
4282
|
}
|
|
4276
|
-
async getDisableHighLeverageModeIx(user) {
|
|
4283
|
+
async getDisableHighLeverageModeIx(user, userAccount) {
|
|
4284
|
+
const remainingAccounts = userAccount
|
|
4285
|
+
? this.getRemainingAccounts({
|
|
4286
|
+
userAccounts: [userAccount],
|
|
4287
|
+
})
|
|
4288
|
+
: undefined;
|
|
4277
4289
|
const ix = await this.program.instruction.disableUserHighLeverageMode({
|
|
4278
4290
|
accounts: {
|
|
4279
4291
|
state: await this.getStatePublicKey(),
|
|
@@ -4281,6 +4293,7 @@ class DriftClient {
|
|
|
4281
4293
|
authority: this.wallet.publicKey,
|
|
4282
4294
|
highLeverageModeConfig: (0, pda_1.getHighLeverageModeConfigPublicKey)(this.program.programId),
|
|
4283
4295
|
},
|
|
4296
|
+
remainingAccounts,
|
|
4284
4297
|
});
|
|
4285
4298
|
return ix;
|
|
4286
4299
|
}
|
package/lib/math/margin.d.ts
CHANGED
|
@@ -21,19 +21,19 @@ export declare function calculateWorstCasePerpLiabilityValue(perpPosition: PerpP
|
|
|
21
21
|
worstCaseBaseAssetAmount: BN;
|
|
22
22
|
worstCaseLiabilityValue: BN;
|
|
23
23
|
};
|
|
24
|
-
export declare function calculatePerpLiabilityValue(baseAssetAmount: BN,
|
|
24
|
+
export declare function calculatePerpLiabilityValue(baseAssetAmount: BN, price: BN, isPredictionMarket: boolean): BN;
|
|
25
25
|
/**
|
|
26
26
|
* Calculates the margin required to open a trade, in quote amount. Only accounts for the trade size as a scalar value, does not account for the trade direction or current open positions and whether the trade would _actually_ be risk-increasing and use any extra collateral.
|
|
27
27
|
* @param targetMarketIndex
|
|
28
28
|
* @param baseSize
|
|
29
29
|
* @returns
|
|
30
30
|
*/
|
|
31
|
-
export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
|
|
31
|
+
export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number, userHighLeverageMode?: boolean, entryPrice?: BN): BN;
|
|
32
32
|
/**
|
|
33
33
|
* Similar to calculatetMarginUSDCRequiredForTrade, but calculates how much of a given collateral is required to cover the margin requirements for a given trade. Basically does the same thing as getMarginUSDCRequiredForTrade but also accounts for asset weight of the selected collateral.
|
|
34
34
|
*
|
|
35
35
|
* Returns collateral required in the precision of the target collateral market.
|
|
36
36
|
*/
|
|
37
|
-
export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
|
|
37
|
+
export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number, userHighLeverageMode?: boolean, estEntryPrice?: BN): BN;
|
|
38
38
|
export declare function calculateCollateralValueOfDeposit(driftClient: DriftClient, collateralIndex: number, baseSize: BN): BN;
|
|
39
39
|
export declare function calculateLiquidationPrice(freeCollateral: BN, freeCollateralDelta: BN, oraclePrice: BN): BN;
|
package/lib/math/margin.js
CHANGED
|
@@ -100,20 +100,20 @@ function calculateWorstCasePerpLiabilityValue(perpPosition, perpMarket, oraclePr
|
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
exports.calculateWorstCasePerpLiabilityValue = calculateWorstCasePerpLiabilityValue;
|
|
103
|
-
function calculatePerpLiabilityValue(baseAssetAmount,
|
|
103
|
+
function calculatePerpLiabilityValue(baseAssetAmount, price, isPredictionMarket) {
|
|
104
104
|
if (isPredictionMarket) {
|
|
105
105
|
if (baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
106
|
-
return baseAssetAmount.mul(
|
|
106
|
+
return baseAssetAmount.mul(price).div(numericConstants_1.BASE_PRECISION);
|
|
107
107
|
}
|
|
108
108
|
else {
|
|
109
109
|
return baseAssetAmount
|
|
110
110
|
.abs()
|
|
111
|
-
.mul(numericConstants_1.MAX_PREDICTION_PRICE.sub(
|
|
111
|
+
.mul(numericConstants_1.MAX_PREDICTION_PRICE.sub(price))
|
|
112
112
|
.div(numericConstants_1.BASE_PRECISION);
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
else {
|
|
116
|
-
return baseAssetAmount.abs().mul(
|
|
116
|
+
return baseAssetAmount.abs().mul(price).div(numericConstants_1.BASE_PRECISION);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
|
|
@@ -123,10 +123,10 @@ exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
|
|
|
123
123
|
* @param baseSize
|
|
124
124
|
* @returns
|
|
125
125
|
*/
|
|
126
|
-
function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode) {
|
|
126
|
+
function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode, entryPrice) {
|
|
127
127
|
const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
|
|
128
|
-
const
|
|
129
|
-
const perpLiabilityValue = calculatePerpLiabilityValue(baseSize,
|
|
128
|
+
const price = entryPrice !== null && entryPrice !== void 0 ? entryPrice : driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex).price;
|
|
129
|
+
const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, price, (0, types_1.isVariant)(targetMarket.contractType, 'prediction'));
|
|
130
130
|
const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio, userHighLeverageMode))
|
|
131
131
|
.mul(perpLiabilityValue)
|
|
132
132
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
@@ -138,8 +138,8 @@ exports.calculateMarginUSDCRequiredForTrade = calculateMarginUSDCRequiredForTrad
|
|
|
138
138
|
*
|
|
139
139
|
* Returns collateral required in the precision of the target collateral market.
|
|
140
140
|
*/
|
|
141
|
-
function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio, userHighLeverageMode) {
|
|
142
|
-
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode);
|
|
141
|
+
function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio, userHighLeverageMode, estEntryPrice) {
|
|
142
|
+
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode, estEntryPrice);
|
|
143
143
|
const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
|
|
144
144
|
const collateralOracleData = driftClient.getOracleDataForSpotMarket(collateralIndex);
|
|
145
145
|
const scaledAssetWeight = (0, __1.calculateScaledInitialAssetWeight)(collateralMarket, collateralOracleData.price);
|
package/lib/user.d.ts
CHANGED
|
@@ -280,7 +280,7 @@ export declare class User {
|
|
|
280
280
|
* @returns : Precision PRICE_PRECISION
|
|
281
281
|
*/
|
|
282
282
|
liquidationPriceAfterClose(positionMarketIndex: number, closeQuoteAmount: BN, estimatedEntryPrice?: BN): BN;
|
|
283
|
-
getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN): BN;
|
|
283
|
+
getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN, estEntryPrice?: BN): BN;
|
|
284
284
|
getCollateralDepositRequiredForTrade(targetMarketIndex: number, baseSize: BN, collateralIndex: number): BN;
|
|
285
285
|
/**
|
|
286
286
|
* Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
|
package/lib/user.js
CHANGED
|
@@ -1371,8 +1371,8 @@ class User {
|
|
|
1371
1371
|
.neg();
|
|
1372
1372
|
return this.liquidationPrice(positionMarketIndex, closeBaseAmount, estimatedEntryPrice);
|
|
1373
1373
|
}
|
|
1374
|
-
getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize) {
|
|
1375
|
-
return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio);
|
|
1374
|
+
getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize, estEntryPrice) {
|
|
1375
|
+
return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio, undefined, estEntryPrice);
|
|
1376
1376
|
}
|
|
1377
1377
|
getCollateralDepositRequiredForTrade(targetMarketIndex, baseSize, collateralIndex) {
|
|
1378
1378
|
return (0, margin_1.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, this.getUserAccount().maxMarginRatio, false // assume user cant be high leverage if they havent created user account ?
|
package/package.json
CHANGED
package/src/driftClient.ts
CHANGED
|
@@ -6547,7 +6547,10 @@ export class DriftClient {
|
|
|
6547
6547
|
const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
6548
6548
|
|
|
6549
6549
|
const remainingAccounts = this.getRemainingAccounts({
|
|
6550
|
-
userAccounts: [
|
|
6550
|
+
userAccounts: [
|
|
6551
|
+
userAccount,
|
|
6552
|
+
...makerInfos.map((makerInfo) => makerInfo.makerUserAccount),
|
|
6553
|
+
],
|
|
6551
6554
|
useMarketLastSlotCache: true,
|
|
6552
6555
|
writablePerpMarketIndexes: [marketIndex],
|
|
6553
6556
|
});
|
|
@@ -8120,6 +8123,10 @@ export class DriftClient {
|
|
|
8120
8123
|
}
|
|
8121
8124
|
|
|
8122
8125
|
public async getEnableHighLeverageModeIx(subAccountId: number) {
|
|
8126
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
8127
|
+
userAccounts: [this.getUserAccount(subAccountId)],
|
|
8128
|
+
});
|
|
8129
|
+
|
|
8123
8130
|
const ix = await this.program.instruction.enableUserHighLeverageMode(
|
|
8124
8131
|
subAccountId,
|
|
8125
8132
|
{
|
|
@@ -8135,6 +8142,7 @@ export class DriftClient {
|
|
|
8135
8142
|
this.program.programId
|
|
8136
8143
|
),
|
|
8137
8144
|
},
|
|
8145
|
+
remainingAccounts,
|
|
8138
8146
|
}
|
|
8139
8147
|
);
|
|
8140
8148
|
|
|
@@ -8143,11 +8151,12 @@ export class DriftClient {
|
|
|
8143
8151
|
|
|
8144
8152
|
public async disableUserHighLeverageMode(
|
|
8145
8153
|
user: PublicKey,
|
|
8154
|
+
userAccount?: UserAccount,
|
|
8146
8155
|
txParams?: TxParams
|
|
8147
8156
|
): Promise<TransactionSignature> {
|
|
8148
8157
|
const { txSig } = await this.sendTransaction(
|
|
8149
8158
|
await this.buildTransaction(
|
|
8150
|
-
await this.getDisableHighLeverageModeIx(user),
|
|
8159
|
+
await this.getDisableHighLeverageModeIx(user, userAccount),
|
|
8151
8160
|
txParams
|
|
8152
8161
|
),
|
|
8153
8162
|
[],
|
|
@@ -8156,7 +8165,16 @@ export class DriftClient {
|
|
|
8156
8165
|
return txSig;
|
|
8157
8166
|
}
|
|
8158
8167
|
|
|
8159
|
-
public async getDisableHighLeverageModeIx(
|
|
8168
|
+
public async getDisableHighLeverageModeIx(
|
|
8169
|
+
user: PublicKey,
|
|
8170
|
+
userAccount?: UserAccount
|
|
8171
|
+
) {
|
|
8172
|
+
const remainingAccounts = userAccount
|
|
8173
|
+
? this.getRemainingAccounts({
|
|
8174
|
+
userAccounts: [userAccount],
|
|
8175
|
+
})
|
|
8176
|
+
: undefined;
|
|
8177
|
+
|
|
8160
8178
|
const ix = await this.program.instruction.disableUserHighLeverageMode({
|
|
8161
8179
|
accounts: {
|
|
8162
8180
|
state: await this.getStatePublicKey(),
|
|
@@ -8166,6 +8184,7 @@ export class DriftClient {
|
|
|
8166
8184
|
this.program.programId
|
|
8167
8185
|
),
|
|
8168
8186
|
},
|
|
8187
|
+
remainingAccounts,
|
|
8169
8188
|
});
|
|
8170
8189
|
|
|
8171
8190
|
return ix;
|
package/src/math/margin.ts
CHANGED
|
@@ -187,20 +187,20 @@ export function calculateWorstCasePerpLiabilityValue(
|
|
|
187
187
|
|
|
188
188
|
export function calculatePerpLiabilityValue(
|
|
189
189
|
baseAssetAmount: BN,
|
|
190
|
-
|
|
190
|
+
price: BN,
|
|
191
191
|
isPredictionMarket: boolean
|
|
192
192
|
): BN {
|
|
193
193
|
if (isPredictionMarket) {
|
|
194
194
|
if (baseAssetAmount.gt(ZERO)) {
|
|
195
|
-
return baseAssetAmount.mul(
|
|
195
|
+
return baseAssetAmount.mul(price).div(BASE_PRECISION);
|
|
196
196
|
} else {
|
|
197
197
|
return baseAssetAmount
|
|
198
198
|
.abs()
|
|
199
|
-
.mul(MAX_PREDICTION_PRICE.sub(
|
|
199
|
+
.mul(MAX_PREDICTION_PRICE.sub(price))
|
|
200
200
|
.div(BASE_PRECISION);
|
|
201
201
|
}
|
|
202
202
|
} else {
|
|
203
|
-
return baseAssetAmount.abs().mul(
|
|
203
|
+
return baseAssetAmount.abs().mul(price).div(BASE_PRECISION);
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
206
|
|
|
@@ -215,16 +215,18 @@ export function calculateMarginUSDCRequiredForTrade(
|
|
|
215
215
|
targetMarketIndex: number,
|
|
216
216
|
baseSize: BN,
|
|
217
217
|
userMaxMarginRatio?: number,
|
|
218
|
-
userHighLeverageMode?: boolean
|
|
218
|
+
userHighLeverageMode?: boolean,
|
|
219
|
+
entryPrice?: BN
|
|
219
220
|
): BN {
|
|
220
221
|
const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
222
|
+
|
|
223
|
+
const price =
|
|
224
|
+
entryPrice ??
|
|
225
|
+
driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex).price;
|
|
224
226
|
|
|
225
227
|
const perpLiabilityValue = calculatePerpLiabilityValue(
|
|
226
228
|
baseSize,
|
|
227
|
-
|
|
229
|
+
price,
|
|
228
230
|
isVariant(targetMarket.contractType, 'prediction')
|
|
229
231
|
);
|
|
230
232
|
|
|
@@ -254,14 +256,16 @@ export function calculateCollateralDepositRequiredForTrade(
|
|
|
254
256
|
baseSize: BN,
|
|
255
257
|
collateralIndex: number,
|
|
256
258
|
userMaxMarginRatio?: number,
|
|
257
|
-
userHighLeverageMode?: boolean
|
|
259
|
+
userHighLeverageMode?: boolean,
|
|
260
|
+
estEntryPrice?: BN
|
|
258
261
|
): BN {
|
|
259
262
|
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(
|
|
260
263
|
driftClient,
|
|
261
264
|
targetMarketIndex,
|
|
262
265
|
baseSize,
|
|
263
266
|
userMaxMarginRatio,
|
|
264
|
-
userHighLeverageMode
|
|
267
|
+
userHighLeverageMode,
|
|
268
|
+
estEntryPrice
|
|
265
269
|
);
|
|
266
270
|
|
|
267
271
|
const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
|
package/src/user.ts
CHANGED
|
@@ -2613,13 +2613,16 @@ export class User {
|
|
|
2613
2613
|
|
|
2614
2614
|
public getMarginUSDCRequiredForTrade(
|
|
2615
2615
|
targetMarketIndex: number,
|
|
2616
|
-
baseSize: BN
|
|
2616
|
+
baseSize: BN,
|
|
2617
|
+
estEntryPrice?: BN
|
|
2617
2618
|
): BN {
|
|
2618
2619
|
return calculateMarginUSDCRequiredForTrade(
|
|
2619
2620
|
this.driftClient,
|
|
2620
2621
|
targetMarketIndex,
|
|
2621
2622
|
baseSize,
|
|
2622
|
-
this.getUserAccount().maxMarginRatio
|
|
2623
|
+
this.getUserAccount().maxMarginRatio,
|
|
2624
|
+
undefined,
|
|
2625
|
+
estEntryPrice
|
|
2623
2626
|
);
|
|
2624
2627
|
}
|
|
2625
2628
|
|