@drift-labs/sdk 2.83.0-beta.0 → 2.83.0-beta.1
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/constants/perpMarkets.js +10 -0
- package/lib/constants/spotMarkets.js +20 -0
- package/lib/driftClient.js +7 -6
- package/lib/tx/txParamProcessor.d.ts +1 -1
- package/lib/tx/txParamProcessor.js +9 -7
- package/lib/user.d.ts +1 -1
- package/lib/user.js +7 -7
- package/package.json +1 -1
- package/src/constants/perpMarkets.ts +10 -0
- package/src/constants/spotMarkets.ts +20 -0
- package/src/driftClient.ts +8 -7
- package/src/tx/txParamProcessor.ts +14 -10
- package/src/user.ts +10 -10
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.83.0-beta.
|
|
1
|
+
2.83.0-beta.1
|
|
@@ -556,6 +556,16 @@ exports.MainnetPerpMarkets = [
|
|
|
556
556
|
launchTs: 1712593532000,
|
|
557
557
|
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
558
558
|
},
|
|
559
|
+
{
|
|
560
|
+
fullName: 'Drift',
|
|
561
|
+
category: ['DEX', 'Solana'],
|
|
562
|
+
symbol: 'DRIFT-PERP',
|
|
563
|
+
baseAssetSymbol: 'DRIFT',
|
|
564
|
+
marketIndex: 30,
|
|
565
|
+
oracle: new web3_js_1.PublicKey('PeNpQeGEm9UEFJ6MBCMauY4WW4h3YxoESPWbsqVKucE'),
|
|
566
|
+
launchTs: 1716595200000,
|
|
567
|
+
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
568
|
+
},
|
|
559
569
|
];
|
|
560
570
|
exports.PerpMarkets = {
|
|
561
571
|
devnet: exports.DevnetPerpMarkets,
|
|
@@ -208,6 +208,26 @@ exports.MainnetSpotMarkets = [
|
|
|
208
208
|
phoenixMarket: new web3_js_1.PublicKey('8BV6rrWsUabnTDA3dE6A69oUDJAj3hMhtBHTJyXB7czp'),
|
|
209
209
|
launchTs: 1715860800000,
|
|
210
210
|
},
|
|
211
|
+
{
|
|
212
|
+
symbol: 'INF',
|
|
213
|
+
marketIndex: 16,
|
|
214
|
+
oracle: new web3_js_1.PublicKey('6AQHz9mpGNjyVafcWdqzzgsJq14Cs8gG6MiQKmdAgCuP'),
|
|
215
|
+
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
216
|
+
mint: new web3_js_1.PublicKey('5oVNBeEEQvYi1cX3ir8Dx5n1P7pdxydbGF2X4TxVusJm'),
|
|
217
|
+
precision: new __1.BN(10).pow(numericConstants_1.NINE),
|
|
218
|
+
precisionExp: numericConstants_1.NINE,
|
|
219
|
+
launchTs: 1716595200000,
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
symbol: 'dSOL',
|
|
223
|
+
marketIndex: 17,
|
|
224
|
+
oracle: new web3_js_1.PublicKey('HJ9K9AamqVMp86j3uQgpA1tdJNRAwfVuL75FD9P3QBrn'),
|
|
225
|
+
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
226
|
+
mint: new web3_js_1.PublicKey('Dso1bDeDjCQxTrWHqUUi63oBvV7Mdm6WaobLbQ7gnPQ'),
|
|
227
|
+
precision: new __1.BN(10).pow(numericConstants_1.NINE),
|
|
228
|
+
precisionExp: numericConstants_1.NINE,
|
|
229
|
+
launchTs: 1716595200000,
|
|
230
|
+
},
|
|
211
231
|
];
|
|
212
232
|
exports.SpotMarkets = {
|
|
213
233
|
devnet: exports.DevnetSpotMarkets,
|
package/lib/driftClient.js
CHANGED
|
@@ -2500,6 +2500,7 @@ class DriftClient {
|
|
|
2500
2500
|
return txSig;
|
|
2501
2501
|
}
|
|
2502
2502
|
async placeAndTakePerpWithAdditionalOrders(orderParams, makerInfo, referrerInfo, bracketOrdersParams = new Array(), txParams, subAccountId, cancelExistingOrders, settlePnl, simulateFirst) {
|
|
2503
|
+
var _a;
|
|
2503
2504
|
const placeAndTakeIxs = [];
|
|
2504
2505
|
const placeAndTakeIx = await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId);
|
|
2505
2506
|
const txsToSign = [];
|
|
@@ -2517,13 +2518,13 @@ class DriftClient {
|
|
|
2517
2518
|
const shouldExitIfSimulationFails = simulateFirst;
|
|
2518
2519
|
const txParamsWithoutImplicitSimulation = {
|
|
2519
2520
|
...txParams,
|
|
2520
|
-
|
|
2521
|
+
useSimulatedComputeUnits: false,
|
|
2521
2522
|
};
|
|
2522
2523
|
// Get recent block hash so that we can re-use it for all transactions. Makes this logic run faster with fewer RPC requests
|
|
2523
2524
|
const recentBlockHash = await this.txHandler.getLatestBlockhashForTransaction();
|
|
2524
2525
|
if (shouldUseSimulationComputeUnits || shouldExitIfSimulationFails) {
|
|
2525
2526
|
const placeAndTakeTxToSim = (await this.buildTransaction(placeAndTakeIxs, txParamsWithoutImplicitSimulation, undefined, undefined, true, recentBlockHash));
|
|
2526
|
-
const simulationResult = await txParamProcessor_1.TransactionParamProcessor.getTxSimComputeUnits(placeAndTakeTxToSim, this.connection);
|
|
2527
|
+
const simulationResult = await txParamProcessor_1.TransactionParamProcessor.getTxSimComputeUnits(placeAndTakeTxToSim, this.connection, (_a = txParams.computeUnitsBufferMultiplier) !== null && _a !== void 0 ? _a : 1.2);
|
|
2527
2528
|
if (shouldExitIfSimulationFails && !simulationResult.success) {
|
|
2528
2529
|
return;
|
|
2529
2530
|
}
|
|
@@ -2538,7 +2539,7 @@ class DriftClient {
|
|
|
2538
2539
|
else {
|
|
2539
2540
|
txsToSign.push({
|
|
2540
2541
|
key: keys.placeAndTakeIx,
|
|
2541
|
-
tx: await this.buildTransaction(placeAndTakeIxs,
|
|
2542
|
+
tx: await this.buildTransaction(placeAndTakeIxs, txParams, undefined, undefined, undefined, recentBlockHash),
|
|
2542
2543
|
});
|
|
2543
2544
|
}
|
|
2544
2545
|
if (cancelExistingOrders && (0, types_1.isVariant)(orderParams.marketType, 'perp')) {
|
|
@@ -3591,7 +3592,7 @@ class DriftClient {
|
|
|
3591
3592
|
return this.txHandler.buildTransaction({
|
|
3592
3593
|
instructions,
|
|
3593
3594
|
txVersion: txVersion !== null && txVersion !== void 0 ? txVersion : this.txVersion,
|
|
3594
|
-
txParams,
|
|
3595
|
+
txParams: txParams !== null && txParams !== void 0 ? txParams : this.txParams,
|
|
3595
3596
|
connection: this.connection,
|
|
3596
3597
|
preFlightCommitment: this.opts.preflightCommitment,
|
|
3597
3598
|
fetchMarketLookupTableAccount: this.fetchMarketLookupTableAccount.bind(this),
|
|
@@ -3604,7 +3605,7 @@ class DriftClient {
|
|
|
3604
3605
|
return this.txHandler.buildBulkTransactions({
|
|
3605
3606
|
instructions,
|
|
3606
3607
|
txVersion: txVersion !== null && txVersion !== void 0 ? txVersion : this.txVersion,
|
|
3607
|
-
txParams,
|
|
3608
|
+
txParams: txParams !== null && txParams !== void 0 ? txParams : this.txParams,
|
|
3608
3609
|
connection: this.connection,
|
|
3609
3610
|
preFlightCommitment: this.opts.preflightCommitment,
|
|
3610
3611
|
fetchMarketLookupTableAccount: this.fetchMarketLookupTableAccount.bind(this),
|
|
@@ -3616,7 +3617,7 @@ class DriftClient {
|
|
|
3616
3617
|
return this.txHandler.buildAndSignTransactionMap({
|
|
3617
3618
|
instructions,
|
|
3618
3619
|
txVersion: txVersion !== null && txVersion !== void 0 ? txVersion : this.txVersion,
|
|
3619
|
-
txParams,
|
|
3620
|
+
txParams: txParams !== null && txParams !== void 0 ? txParams : this.txParams,
|
|
3620
3621
|
connection: this.connection,
|
|
3621
3622
|
preFlightCommitment: this.opts.preflightCommitment,
|
|
3622
3623
|
fetchMarketLookupTableAccount: this.fetchMarketLookupTableAccount.bind(this),
|
|
@@ -8,7 +8,7 @@ type TransactionBuildingProps = {
|
|
|
8
8
|
*/
|
|
9
9
|
export declare class TransactionParamProcessor {
|
|
10
10
|
private static getComputeUnitsFromSim;
|
|
11
|
-
static getTxSimComputeUnits(tx: VersionedTransaction, connection: Connection): Promise<{
|
|
11
|
+
static getTxSimComputeUnits(tx: VersionedTransaction, connection: Connection, bufferMultiplier: number): Promise<{
|
|
12
12
|
success: boolean;
|
|
13
13
|
computeUnits: number;
|
|
14
14
|
}>;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TransactionParamProcessor = void 0;
|
|
4
4
|
const COMPUTE_UNIT_BUFFER_FACTOR = 1.2;
|
|
5
|
+
const MAX_COMPUTE_UNITS = 1400000;
|
|
5
6
|
const TEST_SIMS_ALWAYS_FAIL = false;
|
|
6
7
|
/**
|
|
7
8
|
* This class is responsible for running through a "processing" pipeline for a base transaction, to adjust the standard transaction parameters based on a given configuration.
|
|
@@ -14,7 +15,8 @@ class TransactionParamProcessor {
|
|
|
14
15
|
}
|
|
15
16
|
return undefined;
|
|
16
17
|
}
|
|
17
|
-
static async getTxSimComputeUnits(tx, connection
|
|
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
20
|
var _a, _b, _c;
|
|
19
21
|
try {
|
|
20
22
|
if (TEST_SIMS_ALWAYS_FAIL)
|
|
@@ -26,9 +28,11 @@ class TransactionParamProcessor {
|
|
|
26
28
|
throw new Error((_c = (_b = simTxResult === null || simTxResult === void 0 ? void 0 : simTxResult.value) === null || _b === void 0 ? void 0 : _b.err) === null || _c === void 0 ? void 0 : _c.toString());
|
|
27
29
|
}
|
|
28
30
|
const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
|
|
31
|
+
// Apply the buffer, but round down to the MAX_COMPUTE_UNITS, and round up to the nearest whole number
|
|
32
|
+
const bufferedComputeUnits = Math.ceil(Math.min(computeUnits * bufferMultiplier, MAX_COMPUTE_UNITS));
|
|
29
33
|
return {
|
|
30
34
|
success: true,
|
|
31
|
-
computeUnits:
|
|
35
|
+
computeUnits: bufferedComputeUnits,
|
|
32
36
|
};
|
|
33
37
|
}
|
|
34
38
|
catch (e) {
|
|
@@ -53,14 +57,12 @@ class TransactionParamProcessor {
|
|
|
53
57
|
// # Run Processes
|
|
54
58
|
if (processConfig.useSimulatedComputeUnits) {
|
|
55
59
|
const txToSim = await txBuilder({
|
|
56
|
-
txParams: { ...finalTxParams, computeUnits:
|
|
60
|
+
txParams: { ...finalTxParams, computeUnits: MAX_COMPUTE_UNITS },
|
|
57
61
|
});
|
|
58
|
-
const txSimComputeUnitsResult = await this.getTxSimComputeUnits(txToSim, processProps.connection);
|
|
62
|
+
const txSimComputeUnitsResult = await this.getTxSimComputeUnits(txToSim, processProps.connection, (_a = processConfig === null || processConfig === void 0 ? void 0 : processConfig.computeUnitsBufferMultiplier) !== null && _a !== void 0 ? _a : COMPUTE_UNIT_BUFFER_FACTOR);
|
|
59
63
|
if (txSimComputeUnitsResult.success) {
|
|
60
|
-
const bufferedComputeUnits = txSimComputeUnitsResult.computeUnits *
|
|
61
|
-
((_a = processConfig === null || processConfig === void 0 ? void 0 : processConfig.computeUnitsBufferMultiplier) !== null && _a !== void 0 ? _a : COMPUTE_UNIT_BUFFER_FACTOR);
|
|
62
64
|
// Adjust the transaction based on the simulated compute units
|
|
63
|
-
finalTxParams.computeUnits =
|
|
65
|
+
finalTxParams.computeUnits = txSimComputeUnitsResult.computeUnits;
|
|
64
66
|
}
|
|
65
67
|
}
|
|
66
68
|
if (processConfig === null || processConfig === void 0 ? void 0 : processConfig.useSimulatedComputeUnitsForCUPriceCalculation) {
|
package/lib/user.d.ts
CHANGED
|
@@ -119,7 +119,7 @@ export declare class User {
|
|
|
119
119
|
/**
|
|
120
120
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
121
121
|
*/
|
|
122
|
-
getMaintenanceMarginRequirement(
|
|
122
|
+
getMaintenanceMarginRequirement(): BN;
|
|
123
123
|
getActivePerpPositionsForUserAccount(userAccount: UserAccount): PerpPosition[];
|
|
124
124
|
getActivePerpPositions(): PerpPosition[];
|
|
125
125
|
getActivePerpPositionsAndSlot(): DataAndSlot<PerpPosition[]>;
|
package/lib/user.js
CHANGED
|
@@ -447,7 +447,12 @@ class User {
|
|
|
447
447
|
/**
|
|
448
448
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
449
449
|
*/
|
|
450
|
-
getMaintenanceMarginRequirement(
|
|
450
|
+
getMaintenanceMarginRequirement() {
|
|
451
|
+
// if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
|
|
452
|
+
let liquidationBuffer = undefined;
|
|
453
|
+
if (this.isBeingLiquidated()) {
|
|
454
|
+
liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
|
|
455
|
+
}
|
|
451
456
|
return this.getMarginRequirement('Maintenance', liquidationBuffer);
|
|
452
457
|
}
|
|
453
458
|
getActivePerpPositionsForUserAccount(userAccount) {
|
|
@@ -1025,12 +1030,7 @@ class User {
|
|
|
1025
1030
|
}
|
|
1026
1031
|
canBeLiquidated() {
|
|
1027
1032
|
const totalCollateral = this.getTotalCollateral('Maintenance');
|
|
1028
|
-
|
|
1029
|
-
let liquidationBuffer = undefined;
|
|
1030
|
-
if (this.isBeingLiquidated()) {
|
|
1031
|
-
liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
|
|
1032
|
-
}
|
|
1033
|
-
const marginRequirement = this.getMaintenanceMarginRequirement(liquidationBuffer);
|
|
1033
|
+
const marginRequirement = this.getMaintenanceMarginRequirement();
|
|
1034
1034
|
const canBeLiquidated = totalCollateral.lt(marginRequirement);
|
|
1035
1035
|
return {
|
|
1036
1036
|
canBeLiquidated,
|
package/package.json
CHANGED
|
@@ -567,6 +567,16 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
567
567
|
launchTs: 1712593532000,
|
|
568
568
|
oracleSource: OracleSource.SWITCHBOARD,
|
|
569
569
|
},
|
|
570
|
+
{
|
|
571
|
+
fullName: 'Drift',
|
|
572
|
+
category: ['DEX', 'Solana'],
|
|
573
|
+
symbol: 'DRIFT-PERP',
|
|
574
|
+
baseAssetSymbol: 'DRIFT',
|
|
575
|
+
marketIndex: 30,
|
|
576
|
+
oracle: new PublicKey('PeNpQeGEm9UEFJ6MBCMauY4WW4h3YxoESPWbsqVKucE'),
|
|
577
|
+
launchTs: 1716595200000,
|
|
578
|
+
oracleSource: OracleSource.SWITCHBOARD,
|
|
579
|
+
},
|
|
570
580
|
];
|
|
571
581
|
|
|
572
582
|
export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
|
|
@@ -253,6 +253,26 @@ export const MainnetSpotMarkets: SpotMarketConfig[] = [
|
|
|
253
253
|
),
|
|
254
254
|
launchTs: 1715860800000,
|
|
255
255
|
},
|
|
256
|
+
{
|
|
257
|
+
symbol: 'INF',
|
|
258
|
+
marketIndex: 16,
|
|
259
|
+
oracle: new PublicKey('6AQHz9mpGNjyVafcWdqzzgsJq14Cs8gG6MiQKmdAgCuP'),
|
|
260
|
+
oracleSource: OracleSource.SWITCHBOARD,
|
|
261
|
+
mint: new PublicKey('5oVNBeEEQvYi1cX3ir8Dx5n1P7pdxydbGF2X4TxVusJm'),
|
|
262
|
+
precision: new BN(10).pow(NINE),
|
|
263
|
+
precisionExp: NINE,
|
|
264
|
+
launchTs: 1716595200000,
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
symbol: 'dSOL',
|
|
268
|
+
marketIndex: 17,
|
|
269
|
+
oracle: new PublicKey('HJ9K9AamqVMp86j3uQgpA1tdJNRAwfVuL75FD9P3QBrn'),
|
|
270
|
+
oracleSource: OracleSource.SWITCHBOARD,
|
|
271
|
+
mint: new PublicKey('Dso1bDeDjCQxTrWHqUUi63oBvV7Mdm6WaobLbQ7gnPQ'),
|
|
272
|
+
precision: new BN(10).pow(NINE),
|
|
273
|
+
precisionExp: NINE,
|
|
274
|
+
launchTs: 1716595200000,
|
|
275
|
+
},
|
|
256
276
|
];
|
|
257
277
|
|
|
258
278
|
export const SpotMarkets: { [key in DriftEnv]: SpotMarketConfig[] } = {
|
package/src/driftClient.ts
CHANGED
|
@@ -4561,9 +4561,9 @@ export class DriftClient {
|
|
|
4561
4561
|
const shouldUseSimulationComputeUnits = txParams?.useSimulatedComputeUnits;
|
|
4562
4562
|
const shouldExitIfSimulationFails = simulateFirst;
|
|
4563
4563
|
|
|
4564
|
-
const txParamsWithoutImplicitSimulation = {
|
|
4564
|
+
const txParamsWithoutImplicitSimulation: TxParams = {
|
|
4565
4565
|
...txParams,
|
|
4566
|
-
|
|
4566
|
+
useSimulatedComputeUnits: false,
|
|
4567
4567
|
};
|
|
4568
4568
|
|
|
4569
4569
|
// Get recent block hash so that we can re-use it for all transactions. Makes this logic run faster with fewer RPC requests
|
|
@@ -4583,7 +4583,8 @@ export class DriftClient {
|
|
|
4583
4583
|
const simulationResult =
|
|
4584
4584
|
await TransactionParamProcessor.getTxSimComputeUnits(
|
|
4585
4585
|
placeAndTakeTxToSim,
|
|
4586
|
-
this.connection
|
|
4586
|
+
this.connection,
|
|
4587
|
+
txParams.computeUnitsBufferMultiplier ?? 1.2
|
|
4587
4588
|
);
|
|
4588
4589
|
|
|
4589
4590
|
if (shouldExitIfSimulationFails && !simulationResult.success) {
|
|
@@ -4609,7 +4610,7 @@ export class DriftClient {
|
|
|
4609
4610
|
key: keys.placeAndTakeIx,
|
|
4610
4611
|
tx: await this.buildTransaction(
|
|
4611
4612
|
placeAndTakeIxs,
|
|
4612
|
-
|
|
4613
|
+
txParams,
|
|
4613
4614
|
undefined,
|
|
4614
4615
|
undefined,
|
|
4615
4616
|
undefined,
|
|
@@ -6694,7 +6695,7 @@ export class DriftClient {
|
|
|
6694
6695
|
return this.txHandler.buildTransaction({
|
|
6695
6696
|
instructions,
|
|
6696
6697
|
txVersion: txVersion ?? this.txVersion,
|
|
6697
|
-
txParams,
|
|
6698
|
+
txParams: txParams ?? this.txParams,
|
|
6698
6699
|
connection: this.connection,
|
|
6699
6700
|
preFlightCommitment: this.opts.preflightCommitment,
|
|
6700
6701
|
fetchMarketLookupTableAccount:
|
|
@@ -6715,7 +6716,7 @@ export class DriftClient {
|
|
|
6715
6716
|
return this.txHandler.buildBulkTransactions({
|
|
6716
6717
|
instructions,
|
|
6717
6718
|
txVersion: txVersion ?? this.txVersion,
|
|
6718
|
-
txParams,
|
|
6719
|
+
txParams: txParams ?? this.txParams,
|
|
6719
6720
|
connection: this.connection,
|
|
6720
6721
|
preFlightCommitment: this.opts.preflightCommitment,
|
|
6721
6722
|
fetchMarketLookupTableAccount:
|
|
@@ -6736,7 +6737,7 @@ export class DriftClient {
|
|
|
6736
6737
|
return this.txHandler.buildAndSignTransactionMap({
|
|
6737
6738
|
instructions,
|
|
6738
6739
|
txVersion: txVersion ?? this.txVersion,
|
|
6739
|
-
txParams,
|
|
6740
|
+
txParams: txParams ?? this.txParams,
|
|
6740
6741
|
connection: this.connection,
|
|
6741
6742
|
preFlightCommitment: this.opts.preflightCommitment,
|
|
6742
6743
|
fetchMarketLookupTableAccount:
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
import { BaseTxParams, ProcessingTxParams } from '..';
|
|
8
8
|
|
|
9
9
|
const COMPUTE_UNIT_BUFFER_FACTOR = 1.2;
|
|
10
|
+
const MAX_COMPUTE_UNITS = 1_400_000;
|
|
10
11
|
|
|
11
12
|
const TEST_SIMS_ALWAYS_FAIL = false;
|
|
12
13
|
|
|
@@ -30,7 +31,8 @@ export class TransactionParamProcessor {
|
|
|
30
31
|
|
|
31
32
|
public static async getTxSimComputeUnits(
|
|
32
33
|
tx: VersionedTransaction,
|
|
33
|
-
connection: Connection
|
|
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
|
|
34
36
|
): Promise<{ success: boolean; computeUnits: number }> {
|
|
35
37
|
try {
|
|
36
38
|
if (TEST_SIMS_ALWAYS_FAIL)
|
|
@@ -46,9 +48,14 @@ export class TransactionParamProcessor {
|
|
|
46
48
|
|
|
47
49
|
const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
|
|
48
50
|
|
|
51
|
+
// Apply the buffer, but round down to the MAX_COMPUTE_UNITS, and round up to the nearest whole number
|
|
52
|
+
const bufferedComputeUnits = Math.ceil(
|
|
53
|
+
Math.min(computeUnits * bufferMultiplier, MAX_COMPUTE_UNITS)
|
|
54
|
+
);
|
|
55
|
+
|
|
49
56
|
return {
|
|
50
57
|
success: true,
|
|
51
|
-
computeUnits:
|
|
58
|
+
computeUnits: bufferedComputeUnits,
|
|
52
59
|
};
|
|
53
60
|
} catch (e) {
|
|
54
61
|
console.warn(
|
|
@@ -92,22 +99,19 @@ export class TransactionParamProcessor {
|
|
|
92
99
|
// # Run Processes
|
|
93
100
|
if (processConfig.useSimulatedComputeUnits) {
|
|
94
101
|
const txToSim = await txBuilder({
|
|
95
|
-
txParams: { ...finalTxParams, computeUnits:
|
|
102
|
+
txParams: { ...finalTxParams, computeUnits: MAX_COMPUTE_UNITS },
|
|
96
103
|
});
|
|
97
104
|
|
|
98
105
|
const txSimComputeUnitsResult = await this.getTxSimComputeUnits(
|
|
99
106
|
txToSim,
|
|
100
|
-
processProps.connection
|
|
107
|
+
processProps.connection,
|
|
108
|
+
processConfig?.computeUnitsBufferMultiplier ??
|
|
109
|
+
COMPUTE_UNIT_BUFFER_FACTOR
|
|
101
110
|
);
|
|
102
111
|
|
|
103
112
|
if (txSimComputeUnitsResult.success) {
|
|
104
|
-
const bufferedComputeUnits =
|
|
105
|
-
txSimComputeUnitsResult.computeUnits *
|
|
106
|
-
(processConfig?.computeUnitsBufferMultiplier ??
|
|
107
|
-
COMPUTE_UNIT_BUFFER_FACTOR);
|
|
108
|
-
|
|
109
113
|
// Adjust the transaction based on the simulated compute units
|
|
110
|
-
finalTxParams.computeUnits =
|
|
114
|
+
finalTxParams.computeUnits = txSimComputeUnitsResult.computeUnits;
|
|
111
115
|
}
|
|
112
116
|
}
|
|
113
117
|
|
package/src/user.ts
CHANGED
|
@@ -708,7 +708,15 @@ export class User {
|
|
|
708
708
|
/**
|
|
709
709
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
710
710
|
*/
|
|
711
|
-
public getMaintenanceMarginRequirement(
|
|
711
|
+
public getMaintenanceMarginRequirement(): BN {
|
|
712
|
+
// if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
|
|
713
|
+
let liquidationBuffer = undefined;
|
|
714
|
+
if (this.isBeingLiquidated()) {
|
|
715
|
+
liquidationBuffer = new BN(
|
|
716
|
+
this.driftClient.getStateAccount().liquidationMarginBufferRatio
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
|
|
712
720
|
return this.getMarginRequirement('Maintenance', liquidationBuffer);
|
|
713
721
|
}
|
|
714
722
|
|
|
@@ -1878,15 +1886,7 @@ export class User {
|
|
|
1878
1886
|
} {
|
|
1879
1887
|
const totalCollateral = this.getTotalCollateral('Maintenance');
|
|
1880
1888
|
|
|
1881
|
-
|
|
1882
|
-
let liquidationBuffer = undefined;
|
|
1883
|
-
if (this.isBeingLiquidated()) {
|
|
1884
|
-
liquidationBuffer = new BN(
|
|
1885
|
-
this.driftClient.getStateAccount().liquidationMarginBufferRatio
|
|
1886
|
-
);
|
|
1887
|
-
}
|
|
1888
|
-
const marginRequirement =
|
|
1889
|
-
this.getMaintenanceMarginRequirement(liquidationBuffer);
|
|
1889
|
+
const marginRequirement = this.getMaintenanceMarginRequirement();
|
|
1890
1890
|
const canBeLiquidated = totalCollateral.lt(marginRequirement);
|
|
1891
1891
|
|
|
1892
1892
|
return {
|