@drift-labs/sdk 2.55.0-beta.3 → 2.56.0-beta.0
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 +8 -3
- package/lib/idl/drift.json +1 -1
- package/lib/user.d.ts +2 -1
- package/lib/user.js +6 -8
- package/package.json +1 -1
- package/src/driftClient.ts +14 -3
- package/src/idl/drift.json +1 -1
- package/src/user.ts +8 -8
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.
|
|
1
|
+
2.56.0-beta.0
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -121,7 +121,7 @@ export declare class DriftClient {
|
|
|
121
121
|
updateUserCustomMarginRatio(updates: {
|
|
122
122
|
marginRatio: number;
|
|
123
123
|
subAccountId: number;
|
|
124
|
-
}[]): Promise<TransactionSignature>;
|
|
124
|
+
}[], txParams?: TxParams): Promise<TransactionSignature>;
|
|
125
125
|
getUpdateUserCustomMarginRatioIx(marginRatio: number, subAccountId?: number): Promise<TransactionInstruction>;
|
|
126
126
|
getUpdateUserMarginTradingEnabledIx(marginTradingEnabled: boolean, subAccountId?: number, userAccountPublicKey?: PublicKey): Promise<TransactionInstruction>;
|
|
127
127
|
updateUserMarginTradingEnabled(updates: {
|
|
@@ -238,7 +238,7 @@ export declare class DriftClient {
|
|
|
238
238
|
* @param txParams
|
|
239
239
|
* @returns
|
|
240
240
|
*/
|
|
241
|
-
initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, donateAmount?: BN, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
|
|
241
|
+
initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, donateAmount?: BN, txParams?: TxParams, customMaxMarginRatio?: number): Promise<[TransactionSignature, PublicKey]>;
|
|
242
242
|
initializeUserAccountForDevnet(subAccountId: number, name: string, marketIndex: number, tokenFaucet: TokenFaucet, amount: BN, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
|
|
243
243
|
/**
|
|
244
244
|
* Withdraws from a user account. If deposit doesn't already exist, creates a borrow
|
package/lib/driftClient.js
CHANGED
|
@@ -540,12 +540,12 @@ class DriftClient {
|
|
|
540
540
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
541
541
|
return txSig;
|
|
542
542
|
}
|
|
543
|
-
async updateUserCustomMarginRatio(updates) {
|
|
543
|
+
async updateUserCustomMarginRatio(updates, txParams) {
|
|
544
544
|
const ixs = await Promise.all(updates.map(async ({ marginRatio, subAccountId }) => {
|
|
545
545
|
const ix = await this.getUpdateUserCustomMarginRatioIx(marginRatio, subAccountId);
|
|
546
546
|
return ix;
|
|
547
547
|
}));
|
|
548
|
-
const tx = await this.buildTransaction(ixs, this.txParams);
|
|
548
|
+
const tx = await this.buildTransaction(ixs, txParams !== null && txParams !== void 0 ? txParams : this.txParams);
|
|
549
549
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
550
550
|
return txSig;
|
|
551
551
|
}
|
|
@@ -1092,7 +1092,7 @@ class DriftClient {
|
|
|
1092
1092
|
* @param txParams
|
|
1093
1093
|
* @returns
|
|
1094
1094
|
*/
|
|
1095
|
-
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, txParams) {
|
|
1095
|
+
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, txParams, customMaxMarginRatio) {
|
|
1096
1096
|
var _a;
|
|
1097
1097
|
const ixs = [];
|
|
1098
1098
|
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
@@ -1140,6 +1140,11 @@ class DriftClient {
|
|
|
1140
1140
|
const donateIx = await this.getDepositIntoSpotMarketRevenuePoolIx(1, donateAmount, wsolTokenAccount);
|
|
1141
1141
|
ixs.push(donateIx);
|
|
1142
1142
|
}
|
|
1143
|
+
// Set the max margin ratio to initialize account with if passed
|
|
1144
|
+
if (customMaxMarginRatio) {
|
|
1145
|
+
const customMarginRatioIx = await this.getUpdateUserCustomMarginRatioIx(customMaxMarginRatio, subAccountId);
|
|
1146
|
+
ixs.push(customMarginRatioIx);
|
|
1147
|
+
}
|
|
1143
1148
|
// Close the wrapped sol account at the end of the transaction
|
|
1144
1149
|
if (createWSOLTokenAccount) {
|
|
1145
1150
|
ixs.push((0, spl_token_1.createCloseAccountInstruction)(wsolTokenAccount, authority, authority, []));
|
package/lib/idl/drift.json
CHANGED
package/lib/user.d.ts
CHANGED
|
@@ -85,10 +85,11 @@ export declare class User {
|
|
|
85
85
|
getPerpBidAsks(marketIndex: number): [BN, BN];
|
|
86
86
|
/**
|
|
87
87
|
* calculates the open bids and asks for an lp
|
|
88
|
+
* optionally pass in lpShares to see what bid/asks a user *would* take on
|
|
88
89
|
* @returns : lp open bids
|
|
89
90
|
* @returns : lp open asks
|
|
90
91
|
*/
|
|
91
|
-
getLPBidAsks(marketIndex: number): [BN, BN];
|
|
92
|
+
getLPBidAsks(marketIndex: number, lpShares?: BN): [BN, BN];
|
|
92
93
|
/**
|
|
93
94
|
* calculates the market position if the lp position was settled
|
|
94
95
|
* @returns : the settled userPosition
|
package/lib/user.js
CHANGED
|
@@ -227,22 +227,20 @@ class User {
|
|
|
227
227
|
}
|
|
228
228
|
/**
|
|
229
229
|
* calculates the open bids and asks for an lp
|
|
230
|
+
* optionally pass in lpShares to see what bid/asks a user *would* take on
|
|
230
231
|
* @returns : lp open bids
|
|
231
232
|
* @returns : lp open asks
|
|
232
233
|
*/
|
|
233
|
-
getLPBidAsks(marketIndex) {
|
|
234
|
+
getLPBidAsks(marketIndex, lpShares) {
|
|
234
235
|
const position = this.getPerpPosition(marketIndex);
|
|
235
|
-
|
|
236
|
+
const lpSharesToCalc = lpShares !== null && lpShares !== void 0 ? lpShares : position === null || position === void 0 ? void 0 : position.lpShares;
|
|
237
|
+
if (!lpSharesToCalc || lpSharesToCalc.eq(numericConstants_1.ZERO)) {
|
|
236
238
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
237
239
|
}
|
|
238
240
|
const market = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
239
241
|
const [marketOpenBids, marketOpenAsks] = (0, amm_1.calculateMarketOpenBidAsk)(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
|
|
240
|
-
const lpOpenBids = marketOpenBids
|
|
241
|
-
|
|
242
|
-
.div(market.amm.sqrtK);
|
|
243
|
-
const lpOpenAsks = marketOpenAsks
|
|
244
|
-
.mul(position.lpShares)
|
|
245
|
-
.div(market.amm.sqrtK);
|
|
242
|
+
const lpOpenBids = marketOpenBids.mul(lpSharesToCalc).div(market.amm.sqrtK);
|
|
243
|
+
const lpOpenAsks = marketOpenAsks.mul(lpSharesToCalc).div(market.amm.sqrtK);
|
|
246
244
|
return [lpOpenBids, lpOpenAsks];
|
|
247
245
|
}
|
|
248
246
|
/**
|
package/package.json
CHANGED
package/src/driftClient.ts
CHANGED
|
@@ -904,7 +904,8 @@ export class DriftClient {
|
|
|
904
904
|
}
|
|
905
905
|
|
|
906
906
|
public async updateUserCustomMarginRatio(
|
|
907
|
-
updates: { marginRatio: number; subAccountId: number }[]
|
|
907
|
+
updates: { marginRatio: number; subAccountId: number }[],
|
|
908
|
+
txParams?: TxParams
|
|
908
909
|
): Promise<TransactionSignature> {
|
|
909
910
|
const ixs = await Promise.all(
|
|
910
911
|
updates.map(async ({ marginRatio, subAccountId }) => {
|
|
@@ -916,7 +917,7 @@ export class DriftClient {
|
|
|
916
917
|
})
|
|
917
918
|
);
|
|
918
919
|
|
|
919
|
-
const tx = await this.buildTransaction(ixs, this.txParams);
|
|
920
|
+
const tx = await this.buildTransaction(ixs, txParams ?? this.txParams);
|
|
920
921
|
|
|
921
922
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
922
923
|
return txSig;
|
|
@@ -1877,7 +1878,8 @@ export class DriftClient {
|
|
|
1877
1878
|
fromSubAccountId?: number,
|
|
1878
1879
|
referrerInfo?: ReferrerInfo,
|
|
1879
1880
|
donateAmount?: BN,
|
|
1880
|
-
txParams?: TxParams
|
|
1881
|
+
txParams?: TxParams,
|
|
1882
|
+
customMaxMarginRatio?: number
|
|
1881
1883
|
): Promise<[TransactionSignature, PublicKey]> {
|
|
1882
1884
|
const ixs = [];
|
|
1883
1885
|
|
|
@@ -1971,6 +1973,15 @@ export class DriftClient {
|
|
|
1971
1973
|
ixs.push(donateIx);
|
|
1972
1974
|
}
|
|
1973
1975
|
|
|
1976
|
+
// Set the max margin ratio to initialize account with if passed
|
|
1977
|
+
if (customMaxMarginRatio) {
|
|
1978
|
+
const customMarginRatioIx = await this.getUpdateUserCustomMarginRatioIx(
|
|
1979
|
+
customMaxMarginRatio,
|
|
1980
|
+
subAccountId
|
|
1981
|
+
);
|
|
1982
|
+
ixs.push(customMarginRatioIx);
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1974
1985
|
// Close the wrapped sol account at the end of the transaction
|
|
1975
1986
|
if (createWSOLTokenAccount) {
|
|
1976
1987
|
ixs.push(
|
package/src/idl/drift.json
CHANGED
package/src/user.ts
CHANGED
|
@@ -388,12 +388,16 @@ export class User {
|
|
|
388
388
|
|
|
389
389
|
/**
|
|
390
390
|
* calculates the open bids and asks for an lp
|
|
391
|
+
* optionally pass in lpShares to see what bid/asks a user *would* take on
|
|
391
392
|
* @returns : lp open bids
|
|
392
393
|
* @returns : lp open asks
|
|
393
394
|
*/
|
|
394
|
-
public getLPBidAsks(marketIndex: number): [BN, BN] {
|
|
395
|
+
public getLPBidAsks(marketIndex: number, lpShares?: BN): [BN, BN] {
|
|
395
396
|
const position = this.getPerpPosition(marketIndex);
|
|
396
|
-
|
|
397
|
+
|
|
398
|
+
const lpSharesToCalc = lpShares ?? position?.lpShares;
|
|
399
|
+
|
|
400
|
+
if (!lpSharesToCalc || lpSharesToCalc.eq(ZERO)) {
|
|
397
401
|
return [ZERO, ZERO];
|
|
398
402
|
}
|
|
399
403
|
|
|
@@ -405,12 +409,8 @@ export class User {
|
|
|
405
409
|
market.amm.orderStepSize
|
|
406
410
|
);
|
|
407
411
|
|
|
408
|
-
const lpOpenBids = marketOpenBids
|
|
409
|
-
|
|
410
|
-
.div(market.amm.sqrtK);
|
|
411
|
-
const lpOpenAsks = marketOpenAsks
|
|
412
|
-
.mul(position.lpShares)
|
|
413
|
-
.div(market.amm.sqrtK);
|
|
412
|
+
const lpOpenBids = marketOpenBids.mul(lpSharesToCalc).div(market.amm.sqrtK);
|
|
413
|
+
const lpOpenAsks = marketOpenAsks.mul(lpSharesToCalc).div(market.amm.sqrtK);
|
|
414
414
|
|
|
415
415
|
return [lpOpenBids, lpOpenAsks];
|
|
416
416
|
}
|