@drift-labs/sdk 2.85.0-beta.0 → 2.85.0-beta.2
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 +1 -1
- package/lib/driftClient.js +7 -12
- package/lib/tx/txParamProcessor.d.ts +2 -1
- package/lib/tx/txParamProcessor.js +7 -3
- package/lib/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/driftClient.ts +11 -13
- package/src/tx/txParamProcessor.ts +11 -2
- package/src/types.ts +1 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.85.0-beta.
|
|
1
|
+
2.85.0-beta.2
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -649,7 +649,7 @@ export declare class DriftClient {
|
|
|
649
649
|
getOracleDataForSpotMarket(marketIndex: number): OraclePriceData;
|
|
650
650
|
initializeInsuranceFundStake(marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
651
651
|
getInitializeInsuranceFundStakeIx(marketIndex: number): Promise<TransactionInstruction>;
|
|
652
|
-
getAddInsuranceFundStakeIx(marketIndex: number, amount: BN, collateralAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
|
|
652
|
+
getAddInsuranceFundStakeIx(marketIndex: number, amount: BN, collateralAccountPublicKey: PublicKey, fromSubAccount?: boolean): Promise<TransactionInstruction>;
|
|
653
653
|
/**
|
|
654
654
|
* Add to an insurance fund stake and optionally initialize the account
|
|
655
655
|
*/
|
package/lib/driftClient.js
CHANGED
|
@@ -2544,7 +2544,7 @@ class DriftClient {
|
|
|
2544
2544
|
};
|
|
2545
2545
|
if (shouldUseSimulationComputeUnits || shouldExitIfSimulationFails) {
|
|
2546
2546
|
const placeAndTakeTxToSim = (await this.buildTransaction(placeAndTakeIxs, txParams, undefined, undefined, true, recentBlockHash));
|
|
2547
|
-
const simulationResult = await txParamProcessor_1.TransactionParamProcessor.getTxSimComputeUnits(placeAndTakeTxToSim, this.connection, (_a = txParams.computeUnitsBufferMultiplier) !== null && _a !== void 0 ? _a : 1.2);
|
|
2547
|
+
const simulationResult = await txParamProcessor_1.TransactionParamProcessor.getTxSimComputeUnits(placeAndTakeTxToSim, this.connection, (_a = txParams.computeUnitsBufferMultiplier) !== null && _a !== void 0 ? _a : 1.2, txParams.lowerBoundCu);
|
|
2548
2548
|
if (shouldExitIfSimulationFails && !simulationResult.success) {
|
|
2549
2549
|
earlyExitFailedPlaceAndTakeSim = true;
|
|
2550
2550
|
return;
|
|
@@ -3318,12 +3318,12 @@ class DriftClient {
|
|
|
3318
3318
|
},
|
|
3319
3319
|
});
|
|
3320
3320
|
}
|
|
3321
|
-
async getAddInsuranceFundStakeIx(marketIndex, amount, collateralAccountPublicKey) {
|
|
3321
|
+
async getAddInsuranceFundStakeIx(marketIndex, amount, collateralAccountPublicKey, fromSubAccount) {
|
|
3322
3322
|
const spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
3323
3323
|
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
3324
3324
|
const remainingAccounts = this.getRemainingAccounts({
|
|
3325
|
-
userAccounts: [this.getUserAccount()],
|
|
3326
|
-
useMarketLastSlotCache: true,
|
|
3325
|
+
userAccounts: fromSubAccount ? [this.getUserAccount()] : [],
|
|
3326
|
+
useMarketLastSlotCache: fromSubAccount ? true : false,
|
|
3327
3327
|
writableSpotMarketIndexes: [marketIndex],
|
|
3328
3328
|
});
|
|
3329
3329
|
const ix = this.program.instruction.addInsuranceFundStake(marketIndex, amount, {
|
|
@@ -3371,7 +3371,7 @@ class DriftClient {
|
|
|
3371
3371
|
const initializeIx = await this.getInitializeInsuranceFundStakeIx(marketIndex);
|
|
3372
3372
|
addIfStakeIxs.push(initializeIx);
|
|
3373
3373
|
}
|
|
3374
|
-
const addFundsIx = await this.getAddInsuranceFundStakeIx(marketIndex, amount, tokenAccount);
|
|
3374
|
+
const addFundsIx = await this.getAddInsuranceFundStakeIx(marketIndex, amount, tokenAccount, fromSubaccount);
|
|
3375
3375
|
addIfStakeIxs.push(addFundsIx);
|
|
3376
3376
|
if (createWSOLTokenAccount) {
|
|
3377
3377
|
addIfStakeIxs.push((0, spl_token_1.createCloseAccountInstruction)(tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
|
|
@@ -3384,8 +3384,7 @@ class DriftClient {
|
|
|
3384
3384
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3385
3385
|
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
3386
3386
|
const remainingAccounts = this.getRemainingAccounts({
|
|
3387
|
-
userAccounts: [
|
|
3388
|
-
useMarketLastSlotCache: true,
|
|
3387
|
+
userAccounts: [],
|
|
3389
3388
|
writableSpotMarketIndexes: [marketIndex],
|
|
3390
3389
|
});
|
|
3391
3390
|
const ix = await this.program.instruction.requestRemoveInsuranceFundStake(marketIndex, amount, {
|
|
@@ -3427,7 +3426,6 @@ class DriftClient {
|
|
|
3427
3426
|
return txSig;
|
|
3428
3427
|
}
|
|
3429
3428
|
async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey, txParams) {
|
|
3430
|
-
var _a, _b;
|
|
3431
3429
|
const removeIfStakeIxs = [];
|
|
3432
3430
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3433
3431
|
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
@@ -3450,11 +3448,8 @@ class DriftClient {
|
|
|
3450
3448
|
removeIfStakeIxs.push(createTokenAccountIx);
|
|
3451
3449
|
}
|
|
3452
3450
|
}
|
|
3453
|
-
const userAccountExists = !!((_b = (_a = this.getUser()) === null || _a === void 0 ? void 0 : _a.accountSubscriber) === null || _b === void 0 ? void 0 : _b.isSubscribed) &&
|
|
3454
|
-
(await this.checkIfAccountExists(this.getUser().userAccountPublicKey));
|
|
3455
3451
|
const remainingAccounts = this.getRemainingAccounts({
|
|
3456
|
-
userAccounts:
|
|
3457
|
-
useMarketLastSlotCache: false,
|
|
3452
|
+
userAccounts: [],
|
|
3458
3453
|
writableSpotMarketIndexes: [marketIndex],
|
|
3459
3454
|
});
|
|
3460
3455
|
const removeStakeIx = await this.program.instruction.removeInsuranceFundStake(marketIndex, {
|
|
@@ -8,7 +8,8 @@ type TransactionBuildingProps = {
|
|
|
8
8
|
*/
|
|
9
9
|
export declare class TransactionParamProcessor {
|
|
10
10
|
private static getComputeUnitsFromSim;
|
|
11
|
-
static getTxSimComputeUnits(tx: VersionedTransaction, connection: Connection, bufferMultiplier: number
|
|
11
|
+
static getTxSimComputeUnits(tx: VersionedTransaction, connection: Connection, bufferMultiplier: number, // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
|
|
12
|
+
lowerBoundCu?: number): Promise<{
|
|
12
13
|
success: boolean;
|
|
13
14
|
computeUnits: number;
|
|
14
15
|
}>;
|
|
@@ -15,8 +15,8 @@ class TransactionParamProcessor {
|
|
|
15
15
|
}
|
|
16
16
|
return undefined;
|
|
17
17
|
}
|
|
18
|
-
static async getTxSimComputeUnits(tx, connection, bufferMultiplier // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
|
|
19
|
-
) {
|
|
18
|
+
static async getTxSimComputeUnits(tx, connection, bufferMultiplier, // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
|
|
19
|
+
lowerBoundCu) {
|
|
20
20
|
var _a, _b, _c;
|
|
21
21
|
try {
|
|
22
22
|
if (TEST_SIMS_ALWAYS_FAIL)
|
|
@@ -29,7 +29,11 @@ class TransactionParamProcessor {
|
|
|
29
29
|
}
|
|
30
30
|
const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
|
|
31
31
|
// Apply the buffer, but round down to the MAX_COMPUTE_UNITS, and round up to the nearest whole number
|
|
32
|
-
|
|
32
|
+
let bufferedComputeUnits = Math.ceil(Math.min(computeUnits * bufferMultiplier, MAX_COMPUTE_UNITS));
|
|
33
|
+
// If a lower bound CU is passed then enforce it
|
|
34
|
+
if (lowerBoundCu) {
|
|
35
|
+
bufferedComputeUnits = Math.max(bufferedComputeUnits, Math.min(lowerBoundCu, MAX_COMPUTE_UNITS));
|
|
36
|
+
}
|
|
33
37
|
return {
|
|
34
38
|
success: true,
|
|
35
39
|
computeUnits: bufferedComputeUnits,
|
package/lib/types.d.ts
CHANGED
|
@@ -1085,6 +1085,7 @@ export type ProcessingTxParams = {
|
|
|
1085
1085
|
computeUnitsBufferMultiplier?: number;
|
|
1086
1086
|
useSimulatedComputeUnitsForCUPriceCalculation?: boolean;
|
|
1087
1087
|
getCUPriceFromComputeUnits?: (computeUnits: number) => number;
|
|
1088
|
+
lowerBoundCu?: number;
|
|
1088
1089
|
};
|
|
1089
1090
|
export type TxParams = BaseTxParams & ProcessingTxParams;
|
|
1090
1091
|
export declare class SwapReduceOnly {
|
package/package.json
CHANGED
package/src/driftClient.ts
CHANGED
|
@@ -1527,6 +1527,7 @@ export class DriftClient {
|
|
|
1527
1527
|
|
|
1528
1528
|
if (params.useMarketLastSlotCache) {
|
|
1529
1529
|
const lastUserSlot = this.getUserAccountAndSlot()?.slot;
|
|
1530
|
+
|
|
1530
1531
|
for (const [
|
|
1531
1532
|
marketIndex,
|
|
1532
1533
|
slot,
|
|
@@ -4663,7 +4664,8 @@ export class DriftClient {
|
|
|
4663
4664
|
await TransactionParamProcessor.getTxSimComputeUnits(
|
|
4664
4665
|
placeAndTakeTxToSim,
|
|
4665
4666
|
this.connection,
|
|
4666
|
-
txParams.computeUnitsBufferMultiplier ?? 1.2
|
|
4667
|
+
txParams.computeUnitsBufferMultiplier ?? 1.2,
|
|
4668
|
+
txParams.lowerBoundCu
|
|
4667
4669
|
);
|
|
4668
4670
|
|
|
4669
4671
|
if (shouldExitIfSimulationFails && !simulationResult.success) {
|
|
@@ -6306,7 +6308,8 @@ export class DriftClient {
|
|
|
6306
6308
|
public async getAddInsuranceFundStakeIx(
|
|
6307
6309
|
marketIndex: number,
|
|
6308
6310
|
amount: BN,
|
|
6309
|
-
collateralAccountPublicKey: PublicKey
|
|
6311
|
+
collateralAccountPublicKey: PublicKey,
|
|
6312
|
+
fromSubAccount?: boolean
|
|
6310
6313
|
): Promise<TransactionInstruction> {
|
|
6311
6314
|
const spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
6312
6315
|
const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
|
|
@@ -6316,8 +6319,8 @@ export class DriftClient {
|
|
|
6316
6319
|
);
|
|
6317
6320
|
|
|
6318
6321
|
const remainingAccounts = this.getRemainingAccounts({
|
|
6319
|
-
userAccounts: [this.getUserAccount()],
|
|
6320
|
-
useMarketLastSlotCache: true,
|
|
6322
|
+
userAccounts: fromSubAccount ? [this.getUserAccount()] : [],
|
|
6323
|
+
useMarketLastSlotCache: fromSubAccount ? true : false,
|
|
6321
6324
|
writableSpotMarketIndexes: [marketIndex],
|
|
6322
6325
|
});
|
|
6323
6326
|
|
|
@@ -6416,7 +6419,8 @@ export class DriftClient {
|
|
|
6416
6419
|
const addFundsIx = await this.getAddInsuranceFundStakeIx(
|
|
6417
6420
|
marketIndex,
|
|
6418
6421
|
amount,
|
|
6419
|
-
tokenAccount
|
|
6422
|
+
tokenAccount,
|
|
6423
|
+
fromSubaccount
|
|
6420
6424
|
);
|
|
6421
6425
|
|
|
6422
6426
|
addIfStakeIxs.push(addFundsIx);
|
|
@@ -6456,8 +6460,7 @@ export class DriftClient {
|
|
|
6456
6460
|
);
|
|
6457
6461
|
|
|
6458
6462
|
const remainingAccounts = this.getRemainingAccounts({
|
|
6459
|
-
userAccounts: [
|
|
6460
|
-
useMarketLastSlotCache: true,
|
|
6463
|
+
userAccounts: [],
|
|
6461
6464
|
writableSpotMarketIndexes: [marketIndex],
|
|
6462
6465
|
});
|
|
6463
6466
|
|
|
@@ -6566,13 +6569,8 @@ export class DriftClient {
|
|
|
6566
6569
|
}
|
|
6567
6570
|
}
|
|
6568
6571
|
|
|
6569
|
-
const userAccountExists =
|
|
6570
|
-
!!this.getUser()?.accountSubscriber?.isSubscribed &&
|
|
6571
|
-
(await this.checkIfAccountExists(this.getUser().userAccountPublicKey));
|
|
6572
|
-
|
|
6573
6572
|
const remainingAccounts = this.getRemainingAccounts({
|
|
6574
|
-
userAccounts:
|
|
6575
|
-
useMarketLastSlotCache: false,
|
|
6573
|
+
userAccounts: [],
|
|
6576
6574
|
writableSpotMarketIndexes: [marketIndex],
|
|
6577
6575
|
});
|
|
6578
6576
|
|
|
@@ -32,7 +32,8 @@ export class TransactionParamProcessor {
|
|
|
32
32
|
public static async getTxSimComputeUnits(
|
|
33
33
|
tx: VersionedTransaction,
|
|
34
34
|
connection: Connection,
|
|
35
|
-
bufferMultiplier: number // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
|
|
35
|
+
bufferMultiplier: number, // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
|
|
36
|
+
lowerBoundCu?: number
|
|
36
37
|
): Promise<{ success: boolean; computeUnits: number }> {
|
|
37
38
|
try {
|
|
38
39
|
if (TEST_SIMS_ALWAYS_FAIL)
|
|
@@ -49,10 +50,18 @@ export class TransactionParamProcessor {
|
|
|
49
50
|
const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
|
|
50
51
|
|
|
51
52
|
// Apply the buffer, but round down to the MAX_COMPUTE_UNITS, and round up to the nearest whole number
|
|
52
|
-
|
|
53
|
+
let bufferedComputeUnits = Math.ceil(
|
|
53
54
|
Math.min(computeUnits * bufferMultiplier, MAX_COMPUTE_UNITS)
|
|
54
55
|
);
|
|
55
56
|
|
|
57
|
+
// If a lower bound CU is passed then enforce it
|
|
58
|
+
if (lowerBoundCu) {
|
|
59
|
+
bufferedComputeUnits = Math.max(
|
|
60
|
+
bufferedComputeUnits,
|
|
61
|
+
Math.min(lowerBoundCu, MAX_COMPUTE_UNITS)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
56
65
|
return {
|
|
57
66
|
success: true,
|
|
58
67
|
computeUnits: bufferedComputeUnits,
|
package/src/types.ts
CHANGED
|
@@ -1042,6 +1042,7 @@ export type ProcessingTxParams = {
|
|
|
1042
1042
|
computeUnitsBufferMultiplier?: number;
|
|
1043
1043
|
useSimulatedComputeUnitsForCUPriceCalculation?: boolean;
|
|
1044
1044
|
getCUPriceFromComputeUnits?: (computeUnits: number) => number;
|
|
1045
|
+
lowerBoundCu?: number;
|
|
1045
1046
|
};
|
|
1046
1047
|
|
|
1047
1048
|
export type TxParams = BaseTxParams & ProcessingTxParams;
|