@drift-labs/sdk 2.77.0-beta.0 → 2.77.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 +13 -3
- package/lib/driftClient.js +102 -31
- package/lib/idl/drift.json +1 -1
- package/lib/priorityFee/driftPriorityFeeMethod.d.ts +1 -1
- package/lib/priorityFee/driftPriorityFeeMethod.js +9 -3
- package/lib/priorityFee/heliusPriorityFeeMethod.js +24 -18
- package/lib/priorityFee/priorityFeeSubscriber.d.ts +2 -3
- package/lib/priorityFee/priorityFeeSubscriber.js +14 -20
- package/lib/priorityFee/solanaPriorityFeeMethod.js +15 -9
- package/lib/priorityFee/types.d.ts +3 -2
- package/lib/tx/txParamProcessor.d.ts +28 -0
- package/lib/tx/txParamProcessor.js +71 -0
- package/lib/tx/whileValidTxSender.d.ts +1 -1
- package/lib/tx/whileValidTxSender.js +5 -3
- package/lib/types.d.ts +6 -1
- package/lib/user.js +8 -2
- package/package.json +1 -1
- package/src/driftClient.ts +154 -39
- package/src/idl/drift.json +1 -1
- package/src/priorityFee/driftPriorityFeeMethod.ts +14 -6
- package/src/priorityFee/heliusPriorityFeeMethod.ts +24 -18
- package/src/priorityFee/priorityFeeSubscriber.ts +16 -22
- package/src/priorityFee/solanaPriorityFeeMethod.ts +17 -11
- package/src/priorityFee/types.ts +6 -2
- package/src/tx/txParamProcessor.ts +126 -0
- package/src/tx/whileValidTxSender.ts +6 -3
- package/src/types.ts +8 -1
- package/src/user.ts +7 -2
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.77.0-beta.
|
|
1
|
+
2.77.0-beta.2
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -112,7 +112,8 @@ export declare class DriftClient {
|
|
|
112
112
|
* Adds and subscribes to users based on params set by the constructor or by updateWallet.
|
|
113
113
|
*/
|
|
114
114
|
addAndSubscribeToUsers(): Promise<boolean>;
|
|
115
|
-
|
|
115
|
+
private getProcessedTransactionParams;
|
|
116
|
+
initializeUserAccount(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
|
|
116
117
|
getInitializeUserInstructions(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[PublicKey, TransactionInstruction]>;
|
|
117
118
|
getInitializeUserStatsIx(): Promise<TransactionInstruction>;
|
|
118
119
|
getNextSubAccountId(): Promise<number>;
|
|
@@ -249,7 +250,7 @@ export declare class DriftClient {
|
|
|
249
250
|
* @returns
|
|
250
251
|
*/
|
|
251
252
|
initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, donateAmount?: BN, txParams?: TxParams, customMaxMarginRatio?: number): Promise<[TransactionSignature, PublicKey]>;
|
|
252
|
-
initializeUserAccountForDevnet(subAccountId: number, name: string, marketIndex: number, tokenFaucet: TokenFaucet, amount: BN, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
|
|
253
|
+
initializeUserAccountForDevnet(subAccountId: number, name: string, marketIndex: number, tokenFaucet: TokenFaucet, amount: BN, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
|
|
253
254
|
/**
|
|
254
255
|
* Withdraws from a user account. If deposit doesn't already exist, creates a borrow
|
|
255
256
|
* @param amount
|
|
@@ -661,6 +662,15 @@ export declare class DriftClient {
|
|
|
661
662
|
} | undefined;
|
|
662
663
|
private handleSignedTransaction;
|
|
663
664
|
sendTransaction(tx: Transaction | VersionedTransaction, additionalSigners?: Array<Signer>, opts?: ConfirmOptions, preSigned?: boolean): Promise<TxSigAndSlot>;
|
|
664
|
-
|
|
665
|
+
/**
|
|
666
|
+
*
|
|
667
|
+
* @param instructions
|
|
668
|
+
* @param txParams
|
|
669
|
+
* @param txVersion
|
|
670
|
+
* @param lookupTables
|
|
671
|
+
* @param forceVersionedTransaction Return a VersionedTransaction instance even if the version of the transaction is Legacy
|
|
672
|
+
* @returns
|
|
673
|
+
*/
|
|
674
|
+
buildTransaction(instructions: TransactionInstruction | TransactionInstruction[], txParams?: TxParams, txVersion?: TransactionVersion, lookupTables?: AddressLookupTableAccount[], forceVersionedTransaction?: boolean): Promise<Transaction | VersionedTransaction>;
|
|
665
675
|
}
|
|
666
676
|
export {};
|
package/lib/driftClient.js
CHANGED
|
@@ -56,6 +56,7 @@ const memcmp_1 = require("./memcmp");
|
|
|
56
56
|
const marinade_1 = require("./marinade");
|
|
57
57
|
const orderParams_1 = require("./orderParams");
|
|
58
58
|
const utils_2 = require("./math/utils");
|
|
59
|
+
const txParamProcessor_1 = require("./tx/txParamProcessor");
|
|
59
60
|
/**
|
|
60
61
|
* # DriftClient
|
|
61
62
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
@@ -437,7 +438,23 @@ class DriftClient {
|
|
|
437
438
|
}
|
|
438
439
|
return result;
|
|
439
440
|
}
|
|
440
|
-
async
|
|
441
|
+
async getProcessedTransactionParams(txParams, txParamProcessingParams) {
|
|
442
|
+
const tx = await txParamProcessor_1.TransactionProcessor.process({
|
|
443
|
+
txProps: {
|
|
444
|
+
instructions: txParams.instructions,
|
|
445
|
+
txParams: txParams.txParams,
|
|
446
|
+
txVersion: txParams.txVersion,
|
|
447
|
+
lookupTables: txParams.lookupTables,
|
|
448
|
+
},
|
|
449
|
+
txBuilder: (updatedTxParams) => this.buildTransaction(updatedTxParams.instructions, updatedTxParams === null || updatedTxParams === void 0 ? void 0 : updatedTxParams.txParams, updatedTxParams.txVersion, updatedTxParams.lookupTables, true),
|
|
450
|
+
processConfig: txParamProcessingParams,
|
|
451
|
+
processParams: {
|
|
452
|
+
connection: this.connection,
|
|
453
|
+
},
|
|
454
|
+
});
|
|
455
|
+
return tx;
|
|
456
|
+
}
|
|
457
|
+
async initializeUserAccount(subAccountId = 0, name, referrerInfo, txParams) {
|
|
441
458
|
const initializeIxs = [];
|
|
442
459
|
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
443
460
|
if (subAccountId === 0) {
|
|
@@ -446,7 +463,7 @@ class DriftClient {
|
|
|
446
463
|
}
|
|
447
464
|
}
|
|
448
465
|
initializeIxs.push(initializeUserAccountIx);
|
|
449
|
-
const tx = await this.buildTransaction(initializeIxs);
|
|
466
|
+
const tx = await this.buildTransaction(initializeIxs, txParams);
|
|
450
467
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
451
468
|
await this.addUser(subAccountId);
|
|
452
469
|
return [txSig, userAccountPublicKey];
|
|
@@ -1147,21 +1164,11 @@ class DriftClient {
|
|
|
1147
1164
|
* @returns
|
|
1148
1165
|
*/
|
|
1149
1166
|
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, txParams, customMaxMarginRatio) {
|
|
1150
|
-
var _a;
|
|
1151
1167
|
const ixs = [];
|
|
1152
1168
|
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
1153
1169
|
const additionalSigners = [];
|
|
1154
1170
|
const spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
1155
1171
|
const isSolMarket = spotMarket.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
|
|
1156
|
-
let params = {
|
|
1157
|
-
computeUnits: (_a = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits) !== null && _a !== void 0 ? _a : 600000,
|
|
1158
|
-
};
|
|
1159
|
-
if (txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice) {
|
|
1160
|
-
params = {
|
|
1161
|
-
...params,
|
|
1162
|
-
computeUnitsPrice: txParams.computeUnitsPrice,
|
|
1163
|
-
};
|
|
1164
|
-
}
|
|
1165
1172
|
const authority = this.wallet.publicKey;
|
|
1166
1173
|
const isFromSubaccount = fromSubAccountId !== null &&
|
|
1167
1174
|
fromSubAccountId !== undefined &&
|
|
@@ -1203,13 +1210,13 @@ class DriftClient {
|
|
|
1203
1210
|
if (createWSOLTokenAccount) {
|
|
1204
1211
|
ixs.push((0, spl_token_1.createCloseAccountInstruction)(wsolTokenAccount, authority, authority, []));
|
|
1205
1212
|
}
|
|
1206
|
-
const tx = await this.buildTransaction(ixs,
|
|
1213
|
+
const tx = await this.buildTransaction(ixs, txParams);
|
|
1207
1214
|
const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
1208
1215
|
this.spotMarketLastSlotCache.set(marketIndex, slot);
|
|
1209
1216
|
await this.addUser(subAccountId);
|
|
1210
1217
|
return [txSig, userAccountPublicKey];
|
|
1211
1218
|
}
|
|
1212
|
-
async initializeUserAccountForDevnet(subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, marketIndex, tokenFaucet, amount, referrerInfo) {
|
|
1219
|
+
async initializeUserAccountForDevnet(subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, marketIndex, tokenFaucet, amount, referrerInfo, txParams) {
|
|
1213
1220
|
const ixs = [];
|
|
1214
1221
|
const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] = await tokenFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount);
|
|
1215
1222
|
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
|
|
@@ -1221,7 +1228,7 @@ class DriftClient {
|
|
|
1221
1228
|
}
|
|
1222
1229
|
}
|
|
1223
1230
|
ixs.push(initializeUserAccountIx, depositCollateralIx);
|
|
1224
|
-
const tx = await this.buildTransaction(ixs);
|
|
1231
|
+
const tx = await this.buildTransaction(ixs, txParams);
|
|
1225
1232
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
1226
1233
|
await this.addUser(subAccountId);
|
|
1227
1234
|
return [txSig, userAccountPublicKey];
|
|
@@ -1258,10 +1265,7 @@ class DriftClient {
|
|
|
1258
1265
|
if (createWSOLTokenAccount) {
|
|
1259
1266
|
withdrawIxs.push((0, spl_token_1.createCloseAccountInstruction)(associatedTokenAddress, authority, authority, []));
|
|
1260
1267
|
}
|
|
1261
|
-
const tx = await this.buildTransaction(withdrawIxs,
|
|
1262
|
-
...(txParams !== null && txParams !== void 0 ? txParams : this.txParams),
|
|
1263
|
-
computeUnits: 1400000,
|
|
1264
|
-
});
|
|
1268
|
+
const tx = await this.buildTransaction(withdrawIxs, txParams !== null && txParams !== void 0 ? txParams : this.txParams);
|
|
1265
1269
|
const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
1266
1270
|
this.spotMarketLastSlotCache.set(marketIndex, slot);
|
|
1267
1271
|
return txSig;
|
|
@@ -2512,12 +2516,26 @@ class DriftClient {
|
|
|
2512
2516
|
const bracketOrdersIx = await this.getPlaceOrdersIx(bracketOrdersParams, subAccountId);
|
|
2513
2517
|
ixs.push(bracketOrdersIx);
|
|
2514
2518
|
}
|
|
2515
|
-
const
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2519
|
+
const shouldUseSimulationComputeUnits = txParams === null || txParams === void 0 ? void 0 : txParams.useSimulatedComputeUnits;
|
|
2520
|
+
const shouldExitIfSimulationFails = simulateFirst;
|
|
2521
|
+
const txParamsWithoutImplicitSimulation = {
|
|
2522
|
+
...txParams,
|
|
2523
|
+
useSimulationComputeUnits: false,
|
|
2524
|
+
};
|
|
2525
|
+
let placeAndTakeTx = await this.buildTransaction(ixs, txParamsWithoutImplicitSimulation);
|
|
2526
|
+
if (shouldUseSimulationComputeUnits || shouldExitIfSimulationFails) {
|
|
2527
|
+
const simulationResult = await txParamProcessor_1.TransactionProcessor.getTxSimComputeUnits(
|
|
2528
|
+
// @ts-ignore :: TODO - TEST WITH LEGACY TRANSACTION
|
|
2529
|
+
placeAndTakeTx, this.connection);
|
|
2530
|
+
if (shouldExitIfSimulationFails && !simulationResult.success) {
|
|
2520
2531
|
return;
|
|
2532
|
+
}
|
|
2533
|
+
if (shouldUseSimulationComputeUnits) {
|
|
2534
|
+
placeAndTakeTx = await this.buildTransaction(ixs, {
|
|
2535
|
+
...txParamsWithoutImplicitSimulation,
|
|
2536
|
+
computeUnits: simulationResult.computeUnits,
|
|
2537
|
+
});
|
|
2538
|
+
}
|
|
2521
2539
|
}
|
|
2522
2540
|
let cancelExistingOrdersTx;
|
|
2523
2541
|
if (cancelExistingOrders && (0, types_1.isVariant)(orderParams.marketType, 'perp')) {
|
|
@@ -3506,23 +3524,63 @@ class DriftClient {
|
|
|
3506
3524
|
onSignedCb: this.handleSignedTransaction.bind(this),
|
|
3507
3525
|
}
|
|
3508
3526
|
: undefined;
|
|
3509
|
-
|
|
3527
|
+
const version = tx === null || tx === void 0 ? void 0 : tx.version;
|
|
3528
|
+
const isVersionedTx = tx instanceof web3_js_1.VersionedTransaction || version !== undefined;
|
|
3529
|
+
if (isVersionedTx) {
|
|
3510
3530
|
return this.txSender.sendVersionedTransaction(tx, additionalSigners, opts, preSigned, extraConfirmationOptions);
|
|
3511
3531
|
}
|
|
3512
3532
|
else {
|
|
3513
3533
|
return this.txSender.send(tx, additionalSigners, opts, preSigned, extraConfirmationOptions);
|
|
3514
3534
|
}
|
|
3515
3535
|
}
|
|
3516
|
-
|
|
3536
|
+
/**
|
|
3537
|
+
*
|
|
3538
|
+
* @param instructions
|
|
3539
|
+
* @param txParams
|
|
3540
|
+
* @param txVersion
|
|
3541
|
+
* @param lookupTables
|
|
3542
|
+
* @param forceVersionedTransaction Return a VersionedTransaction instance even if the version of the transaction is Legacy
|
|
3543
|
+
* @returns
|
|
3544
|
+
*/
|
|
3545
|
+
async buildTransaction(instructions, txParams, txVersion, lookupTables, forceVersionedTransaction) {
|
|
3517
3546
|
var _a, _b;
|
|
3547
|
+
txVersion = txVersion !== null && txVersion !== void 0 ? txVersion : this.txVersion;
|
|
3548
|
+
// # Collect and process Tx Params
|
|
3549
|
+
let baseTxParams = {
|
|
3550
|
+
computeUnits: (_a = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits) !== null && _a !== void 0 ? _a : this.txParams.computeUnits,
|
|
3551
|
+
computeUnitsPrice: (_b = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice) !== null && _b !== void 0 ? _b : this.txParams.computeUnitsPrice,
|
|
3552
|
+
};
|
|
3553
|
+
if (txParams === null || txParams === void 0 ? void 0 : txParams.useSimulatedComputeUnits) {
|
|
3554
|
+
const splitTxParams = {
|
|
3555
|
+
baseTxParams: {
|
|
3556
|
+
computeUnits: txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits,
|
|
3557
|
+
computeUnitsPrice: txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice,
|
|
3558
|
+
},
|
|
3559
|
+
txParamProcessingParams: {
|
|
3560
|
+
useSimulatedComputeUnits: txParams === null || txParams === void 0 ? void 0 : txParams.useSimulatedComputeUnits,
|
|
3561
|
+
computeUnitsBufferMultiplier: txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsBufferMultiplier,
|
|
3562
|
+
},
|
|
3563
|
+
};
|
|
3564
|
+
const processedTxParams = await this.getProcessedTransactionParams({
|
|
3565
|
+
instructions,
|
|
3566
|
+
txParams: splitTxParams.baseTxParams,
|
|
3567
|
+
txVersion,
|
|
3568
|
+
lookupTables,
|
|
3569
|
+
}, splitTxParams.txParamProcessingParams);
|
|
3570
|
+
baseTxParams = {
|
|
3571
|
+
...baseTxParams,
|
|
3572
|
+
...processedTxParams,
|
|
3573
|
+
};
|
|
3574
|
+
}
|
|
3575
|
+
// # Create Tx Instructions
|
|
3518
3576
|
const allIx = [];
|
|
3519
|
-
const computeUnits =
|
|
3577
|
+
const computeUnits = baseTxParams === null || baseTxParams === void 0 ? void 0 : baseTxParams.computeUnits;
|
|
3520
3578
|
if (computeUnits !== 200000) {
|
|
3521
3579
|
allIx.push(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
3522
3580
|
units: computeUnits,
|
|
3523
3581
|
}));
|
|
3524
3582
|
}
|
|
3525
|
-
const computeUnitsPrice =
|
|
3583
|
+
const computeUnitsPrice = baseTxParams === null || baseTxParams === void 0 ? void 0 : baseTxParams.computeUnitsPrice;
|
|
3526
3584
|
if (computeUnitsPrice !== 0) {
|
|
3527
3585
|
allIx.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
3528
3586
|
microLamports: computeUnitsPrice,
|
|
@@ -3534,9 +3592,22 @@ class DriftClient {
|
|
|
3534
3592
|
else {
|
|
3535
3593
|
allIx.push(instructions);
|
|
3536
3594
|
}
|
|
3537
|
-
|
|
3595
|
+
const latestBlockHashAndContext = await this.connection.getLatestBlockhashAndContext({
|
|
3596
|
+
commitment: this.opts.preflightCommitment,
|
|
3597
|
+
});
|
|
3598
|
+
// # Create and return Transaction
|
|
3538
3599
|
if (txVersion === 'legacy') {
|
|
3539
|
-
|
|
3600
|
+
if (forceVersionedTransaction) {
|
|
3601
|
+
const message = new web3_js_1.TransactionMessage({
|
|
3602
|
+
payerKey: this.provider.wallet.publicKey,
|
|
3603
|
+
recentBlockhash: latestBlockHashAndContext.value.blockhash,
|
|
3604
|
+
instructions: allIx,
|
|
3605
|
+
}).compileToLegacyMessage();
|
|
3606
|
+
return new web3_js_1.VersionedTransaction(message);
|
|
3607
|
+
}
|
|
3608
|
+
else {
|
|
3609
|
+
return new web3_js_1.Transaction().add(...allIx);
|
|
3610
|
+
}
|
|
3540
3611
|
}
|
|
3541
3612
|
else {
|
|
3542
3613
|
const marketLookupTable = await this.fetchMarketLookupTableAccount();
|
|
@@ -3545,7 +3616,7 @@ class DriftClient {
|
|
|
3545
3616
|
: [marketLookupTable];
|
|
3546
3617
|
const message = new web3_js_1.TransactionMessage({
|
|
3547
3618
|
payerKey: this.provider.wallet.publicKey,
|
|
3548
|
-
recentBlockhash:
|
|
3619
|
+
recentBlockhash: latestBlockHashAndContext.value.blockhash,
|
|
3549
3620
|
instructions: allIx,
|
|
3550
3621
|
}).compileToV0Message(lookupTables);
|
|
3551
3622
|
return new web3_js_1.VersionedTransaction(message);
|
package/lib/idl/drift.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { HeliusPriorityFeeLevels } from './heliusPriorityFeeMethod';
|
|
2
2
|
export type DriftPriorityFeeResponse = HeliusPriorityFeeLevels[];
|
|
3
|
-
export declare function fetchDriftPriorityFee(url: string,
|
|
3
|
+
export declare function fetchDriftPriorityFee(url: string, marketTypes: string[], marketIndexs: number[]): Promise<DriftPriorityFeeResponse>;
|
|
@@ -5,8 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.fetchDriftPriorityFee = void 0;
|
|
7
7
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
-
async function fetchDriftPriorityFee(url,
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
async function fetchDriftPriorityFee(url, marketTypes, marketIndexs) {
|
|
9
|
+
try {
|
|
10
|
+
const response = await (0, node_fetch_1.default)(`${url}/batchPriorityFees?marketType=${marketTypes.join(',')}&marketIndex=${marketIndexs.join(',')}`);
|
|
11
|
+
return await response.json();
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
console.error(err);
|
|
15
|
+
}
|
|
16
|
+
return [];
|
|
11
17
|
}
|
|
12
18
|
exports.fetchDriftPriorityFee = fetchDriftPriorityFee;
|
|
@@ -17,24 +17,30 @@ var HeliusPriorityLevel;
|
|
|
17
17
|
/// Fetches the priority fee from the Helius API
|
|
18
18
|
/// https://docs.helius.dev/solana-rpc-nodes/alpha-priority-fee-api
|
|
19
19
|
async function fetchHeliusPriorityFee(heliusRpcUrl, lookbackDistance, addresses) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
20
|
+
try {
|
|
21
|
+
const response = await (0, node_fetch_1.default)(heliusRpcUrl, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'Content-Type': 'application/json' },
|
|
24
|
+
body: JSON.stringify({
|
|
25
|
+
jsonrpc: '2.0',
|
|
26
|
+
id: '1',
|
|
27
|
+
method: 'getPriorityFeeEstimate',
|
|
28
|
+
params: [
|
|
29
|
+
{
|
|
30
|
+
accountKeys: addresses,
|
|
31
|
+
options: {
|
|
32
|
+
includeAllPriorityFeeLevels: true,
|
|
33
|
+
lookbackSlots: lookbackDistance,
|
|
34
|
+
},
|
|
33
35
|
},
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
],
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
return await response.json();
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
console.error(err);
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
39
45
|
}
|
|
40
46
|
exports.fetchHeliusPriorityFee = fetchHeliusPriorityFee;
|
|
@@ -3,7 +3,6 @@ import { PriorityFeeMethod, PriorityFeeStrategy, PriorityFeeSubscriberConfig } f
|
|
|
3
3
|
import { AverageOverSlotsStrategy } from './averageOverSlotsStrategy';
|
|
4
4
|
import { MaxOverSlotsStrategy } from './maxOverSlotsStrategy';
|
|
5
5
|
import { HeliusPriorityFeeLevels, HeliusPriorityLevel } from './heliusPriorityFeeMethod';
|
|
6
|
-
import { MarketType } from '..';
|
|
7
6
|
export type DriftMarketInfo = {
|
|
8
7
|
marketType: string;
|
|
9
8
|
marketIndex: number;
|
|
@@ -12,7 +11,7 @@ export declare class PriorityFeeSubscriber {
|
|
|
12
11
|
connection: Connection;
|
|
13
12
|
frequencyMs: number;
|
|
14
13
|
addresses: string[];
|
|
15
|
-
|
|
14
|
+
driftMarkets?: DriftMarketInfo[];
|
|
16
15
|
customStrategy?: PriorityFeeStrategy;
|
|
17
16
|
averageStrategy: AverageOverSlotsStrategy;
|
|
18
17
|
maxStrategy: MaxOverSlotsStrategy;
|
|
@@ -46,5 +45,5 @@ export declare class PriorityFeeSubscriber {
|
|
|
46
45
|
load(): Promise<void>;
|
|
47
46
|
unsubscribe(): Promise<void>;
|
|
48
47
|
updateAddresses(addresses: PublicKey[]): void;
|
|
49
|
-
updateMarketTypeAndIndex(
|
|
48
|
+
updateMarketTypeAndIndex(driftMarkets: DriftMarketInfo[]): void;
|
|
50
49
|
}
|
|
@@ -7,7 +7,6 @@ const maxOverSlotsStrategy_1 = require("./maxOverSlotsStrategy");
|
|
|
7
7
|
const solanaPriorityFeeMethod_1 = require("./solanaPriorityFeeMethod");
|
|
8
8
|
const heliusPriorityFeeMethod_1 = require("./heliusPriorityFeeMethod");
|
|
9
9
|
const driftPriorityFeeMethod_1 = require("./driftPriorityFeeMethod");
|
|
10
|
-
const __1 = require("..");
|
|
11
10
|
class PriorityFeeSubscriber {
|
|
12
11
|
constructor(config) {
|
|
13
12
|
var _a, _b;
|
|
@@ -24,7 +23,7 @@ class PriorityFeeSubscriber {
|
|
|
24
23
|
this.addresses = config.addresses
|
|
25
24
|
? config.addresses.map((address) => address.toBase58())
|
|
26
25
|
: [];
|
|
27
|
-
this.
|
|
26
|
+
this.driftMarkets = config.driftMarkets;
|
|
28
27
|
if (config.customStrategy) {
|
|
29
28
|
this.customStrategy = config.customStrategy;
|
|
30
29
|
}
|
|
@@ -68,12 +67,14 @@ class PriorityFeeSubscriber {
|
|
|
68
67
|
}
|
|
69
68
|
async loadForSolana() {
|
|
70
69
|
const samples = await (0, solanaPriorityFeeMethod_1.fetchSolanaPriorityFee)(this.connection, this.lookbackDistance, this.addresses);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
if (samples.length > 0) {
|
|
71
|
+
this.latestPriorityFee = samples[0].prioritizationFee;
|
|
72
|
+
this.lastSlotSeen = samples[0].slot;
|
|
73
|
+
this.lastAvgStrategyResult = this.averageStrategy.calculate(samples);
|
|
74
|
+
this.lastMaxStrategyResult = this.maxStrategy.calculate(samples);
|
|
75
|
+
if (this.customStrategy) {
|
|
76
|
+
this.lastCustomStrategyResult = this.customStrategy.calculate(samples);
|
|
77
|
+
}
|
|
77
78
|
}
|
|
78
79
|
}
|
|
79
80
|
async loadForHelius() {
|
|
@@ -91,22 +92,15 @@ class PriorityFeeSubscriber {
|
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
async loadForDrift() {
|
|
94
|
-
if (!this.
|
|
95
|
+
if (!this.driftMarkets) {
|
|
95
96
|
return;
|
|
96
97
|
}
|
|
97
|
-
const sample = await (0, driftPriorityFeeMethod_1.fetchDriftPriorityFee)(this.driftPriorityFeeEndpoint, this.
|
|
98
|
+
const sample = await (0, driftPriorityFeeMethod_1.fetchDriftPriorityFee)(this.driftPriorityFeeEndpoint, this.driftMarkets.map((m) => m.marketType), this.driftMarkets.map((m) => m.marketIndex));
|
|
98
99
|
if (sample.length > 0) {
|
|
99
100
|
this.lastAvgStrategyResult = sample[heliusPriorityFeeMethod_1.HeliusPriorityLevel.MEDIUM];
|
|
100
101
|
this.lastMaxStrategyResult = sample[heliusPriorityFeeMethod_1.HeliusPriorityLevel.UNSAFE_MAX];
|
|
101
102
|
if (this.customStrategy) {
|
|
102
|
-
|
|
103
|
-
this.lastCustomStrategyResult = this.customStrategy.calculate({
|
|
104
|
-
jsonrpc: '',
|
|
105
|
-
result: {
|
|
106
|
-
priorityFeeLevels: sample[0],
|
|
107
|
-
},
|
|
108
|
-
id: '',
|
|
109
|
-
});
|
|
103
|
+
this.lastCustomStrategyResult = this.customStrategy.calculate(sample);
|
|
110
104
|
}
|
|
111
105
|
}
|
|
112
106
|
}
|
|
@@ -186,8 +180,8 @@ class PriorityFeeSubscriber {
|
|
|
186
180
|
updateAddresses(addresses) {
|
|
187
181
|
this.addresses = addresses.map((k) => k.toBase58());
|
|
188
182
|
}
|
|
189
|
-
updateMarketTypeAndIndex(
|
|
190
|
-
this.
|
|
183
|
+
updateMarketTypeAndIndex(driftMarkets) {
|
|
184
|
+
this.driftMarkets = driftMarkets;
|
|
191
185
|
}
|
|
192
186
|
}
|
|
193
187
|
exports.PriorityFeeSubscriber = PriorityFeeSubscriber;
|
|
@@ -2,14 +2,20 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchSolanaPriorityFee = void 0;
|
|
4
4
|
async function fetchSolanaPriorityFee(connection, lookbackDistance, addresses) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
try {
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const rpcJSONResponse = await connection._rpcRequest('getRecentPrioritizationFees', [addresses]);
|
|
8
|
+
const results = rpcJSONResponse === null || rpcJSONResponse === void 0 ? void 0 : rpcJSONResponse.result;
|
|
9
|
+
if (!results.length)
|
|
10
|
+
return;
|
|
11
|
+
// Sort and filter results based on the slot lookback setting
|
|
12
|
+
const descResults = results.sort((a, b) => b.slot - a.slot);
|
|
13
|
+
const cutoffSlot = descResults[0].slot - lookbackDistance;
|
|
14
|
+
return descResults.filter((result) => result.slot >= cutoffSlot);
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
console.error(err);
|
|
18
|
+
}
|
|
19
|
+
return [];
|
|
14
20
|
}
|
|
15
21
|
exports.fetchSolanaPriorityFee = fetchSolanaPriorityFee;
|
|
@@ -2,8 +2,9 @@ import { Connection, PublicKey } from '@solana/web3.js';
|
|
|
2
2
|
import { SolanaPriorityFeeResponse } from './solanaPriorityFeeMethod';
|
|
3
3
|
import { HeliusPriorityFeeResponse } from './heliusPriorityFeeMethod';
|
|
4
4
|
import { DriftMarketInfo } from './priorityFeeSubscriber';
|
|
5
|
+
import { DriftPriorityFeeResponse } from './driftPriorityFeeMethod';
|
|
5
6
|
export interface PriorityFeeStrategy {
|
|
6
|
-
calculate(samples: SolanaPriorityFeeResponse[] | HeliusPriorityFeeResponse): number;
|
|
7
|
+
calculate(samples: SolanaPriorityFeeResponse[] | HeliusPriorityFeeResponse | DriftPriorityFeeResponse): number;
|
|
7
8
|
}
|
|
8
9
|
export declare enum PriorityFeeMethod {
|
|
9
10
|
SOLANA = "solana",
|
|
@@ -14,7 +15,7 @@ export type PriorityFeeSubscriberConfig = {
|
|
|
14
15
|
connection?: Connection;
|
|
15
16
|
frequencyMs: number;
|
|
16
17
|
addresses?: PublicKey[];
|
|
17
|
-
|
|
18
|
+
driftMarkets?: DriftMarketInfo[];
|
|
18
19
|
customStrategy?: PriorityFeeStrategy;
|
|
19
20
|
priorityFeeMethod?: PriorityFeeMethod;
|
|
20
21
|
slotsToCheck?: number;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AddressLookupTableAccount, Connection, TransactionInstruction, TransactionVersion, VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
import { BaseTxParams, ProcessingTxParams } from '..';
|
|
3
|
+
type TransactionProps = {
|
|
4
|
+
instructions: TransactionInstruction | TransactionInstruction[];
|
|
5
|
+
txParams?: BaseTxParams;
|
|
6
|
+
txVersion?: TransactionVersion;
|
|
7
|
+
lookupTables?: AddressLookupTableAccount[];
|
|
8
|
+
forceVersionedTransaction?: boolean;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 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.
|
|
12
|
+
*/
|
|
13
|
+
export declare class TransactionProcessor {
|
|
14
|
+
private static getComputeUnitsFromSim;
|
|
15
|
+
static getTxSimComputeUnits(tx: VersionedTransaction, connection: Connection): Promise<{
|
|
16
|
+
success: boolean;
|
|
17
|
+
computeUnits: number;
|
|
18
|
+
}>;
|
|
19
|
+
static process(props: {
|
|
20
|
+
txProps: TransactionProps;
|
|
21
|
+
txBuilder: (baseTransactionProps: TransactionProps) => Promise<VersionedTransaction>;
|
|
22
|
+
processConfig: ProcessingTxParams;
|
|
23
|
+
processParams: {
|
|
24
|
+
connection: Connection;
|
|
25
|
+
};
|
|
26
|
+
}): Promise<BaseTxParams>;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TransactionProcessor = void 0;
|
|
4
|
+
const COMPUTE_UNIT_BUFFER_FACTOR = 1.2;
|
|
5
|
+
const TEST_SIMS_ALWAYS_FAIL = false;
|
|
6
|
+
/**
|
|
7
|
+
* 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.
|
|
8
|
+
*/
|
|
9
|
+
class TransactionProcessor {
|
|
10
|
+
static async getComputeUnitsFromSim(txSim) {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
if ((_a = txSim === null || txSim === void 0 ? void 0 : txSim.value) === null || _a === void 0 ? void 0 : _a.unitsConsumed) {
|
|
13
|
+
return (_b = txSim === null || txSim === void 0 ? void 0 : txSim.value) === null || _b === void 0 ? void 0 : _b.unitsConsumed;
|
|
14
|
+
}
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
static async getTxSimComputeUnits(tx, connection) {
|
|
18
|
+
var _a, _b, _c;
|
|
19
|
+
try {
|
|
20
|
+
if (TEST_SIMS_ALWAYS_FAIL)
|
|
21
|
+
throw new Error('Test Error::SIMS_ALWAYS_FAIL');
|
|
22
|
+
const simTxResult = await connection.simulateTransaction(tx, {
|
|
23
|
+
replaceRecentBlockhash: true, // This is important to ensure that the blockhash is not too new.. Otherwise we will very often receive a "blockHashNotFound" error
|
|
24
|
+
});
|
|
25
|
+
if ((_a = simTxResult === null || simTxResult === void 0 ? void 0 : simTxResult.value) === null || _a === void 0 ? void 0 : _a.err) {
|
|
26
|
+
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
|
+
}
|
|
28
|
+
const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
|
|
29
|
+
return {
|
|
30
|
+
success: true,
|
|
31
|
+
computeUnits: computeUnits,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
console.warn(`Failed to get Simulated Compute Units for txParamProcessor`, e);
|
|
36
|
+
return {
|
|
37
|
+
success: false,
|
|
38
|
+
computeUnits: undefined,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
static async process(props) {
|
|
43
|
+
var _a;
|
|
44
|
+
// # Exit early if no process config is provided
|
|
45
|
+
if (!props.processConfig || Object.keys(props.processConfig).length === 0) {
|
|
46
|
+
return props.txProps.txParams;
|
|
47
|
+
}
|
|
48
|
+
// # Setup
|
|
49
|
+
const { txProps: txProps, txBuilder: txBuilder, processConfig, processParams: processProps, } = props;
|
|
50
|
+
const baseTransaction = await txBuilder(txProps);
|
|
51
|
+
const finalTxProps = {
|
|
52
|
+
...txProps,
|
|
53
|
+
};
|
|
54
|
+
// # Run Processes
|
|
55
|
+
if (processConfig.useSimulatedComputeUnits) {
|
|
56
|
+
const txSimComputeUnitsResult = await this.getTxSimComputeUnits(baseTransaction, processProps.connection);
|
|
57
|
+
if (txSimComputeUnitsResult.success) {
|
|
58
|
+
const bufferedComputeUnits = txSimComputeUnitsResult.computeUnits *
|
|
59
|
+
((_a = processConfig === null || processConfig === void 0 ? void 0 : processConfig.computeUnitsBufferMultiplier) !== null && _a !== void 0 ? _a : COMPUTE_UNIT_BUFFER_FACTOR);
|
|
60
|
+
// Adjust the transaction based on the simulated compute units
|
|
61
|
+
finalTxProps.txParams = {
|
|
62
|
+
...txProps.txParams,
|
|
63
|
+
computeUnits: bufferedComputeUnits,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// # Return Final Tx Params
|
|
68
|
+
return finalTxProps.txParams;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.TransactionProcessor = TransactionProcessor;
|
|
@@ -28,7 +28,7 @@ export declare class WhileValidTxSender extends BaseTxSender {
|
|
|
28
28
|
});
|
|
29
29
|
sleep(reference: ResolveReference): Promise<void>;
|
|
30
30
|
prepareTx(tx: Transaction, additionalSigners: Array<Signer>, opts: ConfirmOptions, preSigned?: boolean): Promise<Transaction>;
|
|
31
|
-
getVersionedTransaction(ixs: TransactionInstruction[], lookupTableAccounts: AddressLookupTableAccount[], _additionalSigners?: Array<Signer>, _opts?: ConfirmOptions): Promise<VersionedTransaction>;
|
|
31
|
+
getVersionedTransaction(ixs: TransactionInstruction[], lookupTableAccounts: AddressLookupTableAccount[], _additionalSigners?: Array<Signer>, _opts?: ConfirmOptions, blockhash?: string): Promise<VersionedTransaction>;
|
|
32
32
|
sendVersionedTransaction(tx: VersionedTransaction, additionalSigners?: Array<Signer>, opts?: ConfirmOptions, preSigned?: boolean, extraConfirmationOptions?: ExtraConfirmationOptions): Promise<TxSigAndSlot>;
|
|
33
33
|
sendRawTransaction(rawTransaction: Buffer | Uint8Array, opts: ConfirmOptions): Promise<TxSigAndSlot>;
|
|
34
34
|
}
|