@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
|
@@ -9,6 +9,7 @@ const anchor_1 = require("@coral-xyz/anchor");
|
|
|
9
9
|
const baseTxSender_1 = require("./baseTxSender");
|
|
10
10
|
const bs58_1 = __importDefault(require("bs58"));
|
|
11
11
|
const DEFAULT_RETRY = 2000;
|
|
12
|
+
const PLACEHOLDER_BLOCKHASH = 'Fdum64WVeej6DeL85REV9NvfSxEJNPZ74DBk7A8kTrKP';
|
|
12
13
|
class WhileValidTxSender extends baseTxSender_1.BaseTxSender {
|
|
13
14
|
constructor({ connection, wallet, opts = { ...anchor_1.AnchorProvider.defaultOptions(), maxRetries: 0 }, retrySleep = DEFAULT_RETRY, additionalConnections = new Array(), additionalTxSenderCallbacks = [], }) {
|
|
14
15
|
super({
|
|
@@ -48,10 +49,10 @@ class WhileValidTxSender extends baseTxSender_1.BaseTxSender {
|
|
|
48
49
|
this.untilValid.set(txSig, latestBlockhash);
|
|
49
50
|
return signedTx;
|
|
50
51
|
}
|
|
51
|
-
async getVersionedTransaction(ixs, lookupTableAccounts, _additionalSigners, _opts) {
|
|
52
|
+
async getVersionedTransaction(ixs, lookupTableAccounts, _additionalSigners, _opts, blockhash) {
|
|
52
53
|
const message = new web3_js_1.TransactionMessage({
|
|
53
54
|
payerKey: this.wallet.publicKey,
|
|
54
|
-
recentBlockhash:
|
|
55
|
+
recentBlockhash: blockhash !== null && blockhash !== void 0 ? blockhash : PLACEHOLDER_BLOCKHASH,
|
|
55
56
|
instructions: ixs,
|
|
56
57
|
}).compileToV0Message(lookupTableAccounts);
|
|
57
58
|
const tx = new web3_js_1.VersionedTransaction(message);
|
|
@@ -59,18 +60,19 @@ class WhileValidTxSender extends baseTxSender_1.BaseTxSender {
|
|
|
59
60
|
}
|
|
60
61
|
async sendVersionedTransaction(tx, additionalSigners, opts, preSigned, extraConfirmationOptions) {
|
|
61
62
|
const latestBlockhash = await this.connection.getLatestBlockhash();
|
|
62
|
-
tx.message.recentBlockhash = latestBlockhash.blockhash;
|
|
63
63
|
let signedTx;
|
|
64
64
|
if (preSigned) {
|
|
65
65
|
signedTx = tx;
|
|
66
66
|
// @ts-ignore
|
|
67
67
|
}
|
|
68
68
|
else if (this.wallet.payer) {
|
|
69
|
+
tx.message.recentBlockhash = latestBlockhash.blockhash;
|
|
69
70
|
// @ts-ignore
|
|
70
71
|
tx.sign((additionalSigners !== null && additionalSigners !== void 0 ? additionalSigners : []).concat(this.wallet.payer));
|
|
71
72
|
signedTx = tx;
|
|
72
73
|
}
|
|
73
74
|
else {
|
|
75
|
+
tx.message.recentBlockhash = latestBlockhash.blockhash;
|
|
74
76
|
additionalSigners === null || additionalSigners === void 0 ? void 0 : additionalSigners.filter((s) => s !== undefined).forEach((kp) => {
|
|
75
77
|
tx.sign([kp]);
|
|
76
78
|
});
|
package/lib/types.d.ts
CHANGED
|
@@ -1038,10 +1038,15 @@ export type ReferrerInfo = {
|
|
|
1038
1038
|
referrer: PublicKey;
|
|
1039
1039
|
referrerStats: PublicKey;
|
|
1040
1040
|
};
|
|
1041
|
-
export type
|
|
1041
|
+
export type BaseTxParams = {
|
|
1042
1042
|
computeUnits?: number;
|
|
1043
1043
|
computeUnitsPrice?: number;
|
|
1044
1044
|
};
|
|
1045
|
+
export type ProcessingTxParams = {
|
|
1046
|
+
useSimulatedComputeUnits?: boolean;
|
|
1047
|
+
computeUnitsBufferMultiplier?: number;
|
|
1048
|
+
};
|
|
1049
|
+
export type TxParams = BaseTxParams & ProcessingTxParams;
|
|
1045
1050
|
export declare class SwapReduceOnly {
|
|
1046
1051
|
static readonly In: {
|
|
1047
1052
|
in: {};
|
package/lib/user.js
CHANGED
|
@@ -1690,8 +1690,14 @@ class User {
|
|
|
1690
1690
|
const feeTier = this.getUserFeeTier(marketType);
|
|
1691
1691
|
let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
|
|
1692
1692
|
let makerFee = feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
|
|
1693
|
-
if (marketIndex !== undefined
|
|
1694
|
-
|
|
1693
|
+
if (marketIndex !== undefined) {
|
|
1694
|
+
let marketAccount = null;
|
|
1695
|
+
if ((0, types_1.isVariant)(marketType, 'perp')) {
|
|
1696
|
+
marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
1697
|
+
}
|
|
1698
|
+
else {
|
|
1699
|
+
marketAccount = this.driftClient.getSpotMarketAccount(marketIndex);
|
|
1700
|
+
}
|
|
1695
1701
|
takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
|
|
1696
1702
|
makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
|
|
1697
1703
|
}
|
package/package.json
CHANGED
package/src/driftClient.ts
CHANGED
|
@@ -42,6 +42,8 @@ import {
|
|
|
42
42
|
PhoenixV1FulfillmentConfigAccount,
|
|
43
43
|
ModifyOrderPolicy,
|
|
44
44
|
SwapReduceOnly,
|
|
45
|
+
BaseTxParams,
|
|
46
|
+
ProcessingTxParams,
|
|
45
47
|
} from './types';
|
|
46
48
|
import * as anchor from '@coral-xyz/anchor';
|
|
47
49
|
import driftIDL from './idl/drift.json';
|
|
@@ -125,6 +127,7 @@ import { UserStatsSubscriptionConfig } from './userStatsConfig';
|
|
|
125
127
|
import { getMarinadeDepositIx, getMarinadeFinanceProgram } from './marinade';
|
|
126
128
|
import { getOrderParams } from './orderParams';
|
|
127
129
|
import { numberToSafeBN } from './math/utils';
|
|
130
|
+
import { TransactionProcessor as TransactionParamProcessor } from './tx/txParamProcessor';
|
|
128
131
|
|
|
129
132
|
type RemainingAccountParams = {
|
|
130
133
|
userAccounts: UserAccount[];
|
|
@@ -734,10 +737,44 @@ export class DriftClient {
|
|
|
734
737
|
return result;
|
|
735
738
|
}
|
|
736
739
|
|
|
740
|
+
private async getProcessedTransactionParams(
|
|
741
|
+
txParams: {
|
|
742
|
+
instructions: TransactionInstruction | TransactionInstruction[];
|
|
743
|
+
txParams?: BaseTxParams;
|
|
744
|
+
txVersion?: TransactionVersion;
|
|
745
|
+
lookupTables?: AddressLookupTableAccount[];
|
|
746
|
+
},
|
|
747
|
+
txParamProcessingParams: ProcessingTxParams
|
|
748
|
+
): Promise<BaseTxParams> {
|
|
749
|
+
const tx = await TransactionParamProcessor.process({
|
|
750
|
+
txProps: {
|
|
751
|
+
instructions: txParams.instructions,
|
|
752
|
+
txParams: txParams.txParams,
|
|
753
|
+
txVersion: txParams.txVersion,
|
|
754
|
+
lookupTables: txParams.lookupTables,
|
|
755
|
+
},
|
|
756
|
+
txBuilder: (updatedTxParams) =>
|
|
757
|
+
this.buildTransaction(
|
|
758
|
+
updatedTxParams.instructions,
|
|
759
|
+
updatedTxParams?.txParams,
|
|
760
|
+
updatedTxParams.txVersion,
|
|
761
|
+
updatedTxParams.lookupTables,
|
|
762
|
+
true
|
|
763
|
+
) as Promise<VersionedTransaction>,
|
|
764
|
+
processConfig: txParamProcessingParams,
|
|
765
|
+
processParams: {
|
|
766
|
+
connection: this.connection,
|
|
767
|
+
},
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
return tx;
|
|
771
|
+
}
|
|
772
|
+
|
|
737
773
|
public async initializeUserAccount(
|
|
738
774
|
subAccountId = 0,
|
|
739
775
|
name?: string,
|
|
740
|
-
referrerInfo?: ReferrerInfo
|
|
776
|
+
referrerInfo?: ReferrerInfo,
|
|
777
|
+
txParams?: TxParams
|
|
741
778
|
): Promise<[TransactionSignature, PublicKey]> {
|
|
742
779
|
const initializeIxs = [];
|
|
743
780
|
|
|
@@ -757,7 +794,7 @@ export class DriftClient {
|
|
|
757
794
|
}
|
|
758
795
|
|
|
759
796
|
initializeIxs.push(initializeUserAccountIx);
|
|
760
|
-
const tx = await this.buildTransaction(initializeIxs);
|
|
797
|
+
const tx = await this.buildTransaction(initializeIxs, txParams);
|
|
761
798
|
|
|
762
799
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
763
800
|
|
|
@@ -1992,17 +2029,6 @@ export class DriftClient {
|
|
|
1992
2029
|
|
|
1993
2030
|
const isSolMarket = spotMarket.mint.equals(WRAPPED_SOL_MINT);
|
|
1994
2031
|
|
|
1995
|
-
let params: TxParams = {
|
|
1996
|
-
computeUnits: txParams?.computeUnits ?? 600_000,
|
|
1997
|
-
};
|
|
1998
|
-
|
|
1999
|
-
if (txParams?.computeUnitsPrice) {
|
|
2000
|
-
params = {
|
|
2001
|
-
...params,
|
|
2002
|
-
computeUnitsPrice: txParams.computeUnitsPrice,
|
|
2003
|
-
};
|
|
2004
|
-
}
|
|
2005
|
-
|
|
2006
2032
|
const authority = this.wallet.publicKey;
|
|
2007
2033
|
|
|
2008
2034
|
const isFromSubaccount =
|
|
@@ -2090,7 +2116,7 @@ export class DriftClient {
|
|
|
2090
2116
|
);
|
|
2091
2117
|
}
|
|
2092
2118
|
|
|
2093
|
-
const tx = await this.buildTransaction(ixs,
|
|
2119
|
+
const tx = await this.buildTransaction(ixs, txParams);
|
|
2094
2120
|
|
|
2095
2121
|
const { txSig, slot } = await this.sendTransaction(
|
|
2096
2122
|
tx,
|
|
@@ -2110,7 +2136,8 @@ export class DriftClient {
|
|
|
2110
2136
|
marketIndex: number,
|
|
2111
2137
|
tokenFaucet: TokenFaucet,
|
|
2112
2138
|
amount: BN,
|
|
2113
|
-
referrerInfo?: ReferrerInfo
|
|
2139
|
+
referrerInfo?: ReferrerInfo,
|
|
2140
|
+
txParams?: TxParams
|
|
2114
2141
|
): Promise<[TransactionSignature, PublicKey]> {
|
|
2115
2142
|
const ixs = [];
|
|
2116
2143
|
|
|
@@ -2147,7 +2174,7 @@ export class DriftClient {
|
|
|
2147
2174
|
}
|
|
2148
2175
|
ixs.push(initializeUserAccountIx, depositCollateralIx);
|
|
2149
2176
|
|
|
2150
|
-
const tx = await this.buildTransaction(ixs);
|
|
2177
|
+
const tx = await this.buildTransaction(ixs, txParams);
|
|
2151
2178
|
|
|
2152
2179
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
2153
2180
|
|
|
@@ -2231,10 +2258,11 @@ export class DriftClient {
|
|
|
2231
2258
|
);
|
|
2232
2259
|
}
|
|
2233
2260
|
|
|
2234
|
-
const tx = await this.buildTransaction(
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2261
|
+
const tx = await this.buildTransaction(
|
|
2262
|
+
withdrawIxs,
|
|
2263
|
+
txParams ?? this.txParams
|
|
2264
|
+
);
|
|
2265
|
+
|
|
2238
2266
|
const { txSig, slot } = await this.sendTransaction(
|
|
2239
2267
|
tx,
|
|
2240
2268
|
additionalSigners,
|
|
@@ -4535,15 +4563,37 @@ export class DriftClient {
|
|
|
4535
4563
|
ixs.push(bracketOrdersIx);
|
|
4536
4564
|
}
|
|
4537
4565
|
|
|
4538
|
-
const
|
|
4566
|
+
const shouldUseSimulationComputeUnits = txParams?.useSimulatedComputeUnits;
|
|
4567
|
+
const shouldExitIfSimulationFails = simulateFirst;
|
|
4568
|
+
|
|
4569
|
+
const txParamsWithoutImplicitSimulation = {
|
|
4570
|
+
...txParams,
|
|
4571
|
+
useSimulationComputeUnits: false,
|
|
4572
|
+
};
|
|
4573
|
+
|
|
4574
|
+
let placeAndTakeTx = await this.buildTransaction(
|
|
4539
4575
|
ixs,
|
|
4540
|
-
|
|
4541
|
-
)
|
|
4576
|
+
txParamsWithoutImplicitSimulation
|
|
4577
|
+
);
|
|
4578
|
+
|
|
4579
|
+
if (shouldUseSimulationComputeUnits || shouldExitIfSimulationFails) {
|
|
4580
|
+
const simulationResult =
|
|
4581
|
+
await TransactionParamProcessor.getTxSimComputeUnits(
|
|
4582
|
+
// @ts-ignore :: TODO - TEST WITH LEGACY TRANSACTION
|
|
4583
|
+
placeAndTakeTx,
|
|
4584
|
+
this.connection
|
|
4585
|
+
);
|
|
4542
4586
|
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4587
|
+
if (shouldExitIfSimulationFails && !simulationResult.success) {
|
|
4588
|
+
return;
|
|
4589
|
+
}
|
|
4590
|
+
|
|
4591
|
+
if (shouldUseSimulationComputeUnits) {
|
|
4592
|
+
placeAndTakeTx = await this.buildTransaction(ixs, {
|
|
4593
|
+
...txParamsWithoutImplicitSimulation,
|
|
4594
|
+
computeUnits: simulationResult.computeUnits,
|
|
4595
|
+
});
|
|
4596
|
+
}
|
|
4547
4597
|
}
|
|
4548
4598
|
|
|
4549
4599
|
let cancelExistingOrdersTx: Transaction;
|
|
@@ -6504,7 +6554,11 @@ export class DriftClient {
|
|
|
6504
6554
|
}
|
|
6505
6555
|
: undefined;
|
|
6506
6556
|
|
|
6507
|
-
|
|
6557
|
+
const version = (tx as VersionedTransaction)?.version;
|
|
6558
|
+
const isVersionedTx =
|
|
6559
|
+
tx instanceof VersionedTransaction || version !== undefined;
|
|
6560
|
+
|
|
6561
|
+
if (isVersionedTx) {
|
|
6508
6562
|
return this.txSender.sendVersionedTransaction(
|
|
6509
6563
|
tx as VersionedTransaction,
|
|
6510
6564
|
additionalSigners,
|
|
@@ -6523,14 +6577,65 @@ export class DriftClient {
|
|
|
6523
6577
|
}
|
|
6524
6578
|
}
|
|
6525
6579
|
|
|
6580
|
+
/**
|
|
6581
|
+
*
|
|
6582
|
+
* @param instructions
|
|
6583
|
+
* @param txParams
|
|
6584
|
+
* @param txVersion
|
|
6585
|
+
* @param lookupTables
|
|
6586
|
+
* @param forceVersionedTransaction Return a VersionedTransaction instance even if the version of the transaction is Legacy
|
|
6587
|
+
* @returns
|
|
6588
|
+
*/
|
|
6526
6589
|
async buildTransaction(
|
|
6527
6590
|
instructions: TransactionInstruction | TransactionInstruction[],
|
|
6528
6591
|
txParams?: TxParams,
|
|
6529
6592
|
txVersion?: TransactionVersion,
|
|
6530
|
-
lookupTables?: AddressLookupTableAccount[]
|
|
6593
|
+
lookupTables?: AddressLookupTableAccount[],
|
|
6594
|
+
forceVersionedTransaction?: boolean
|
|
6531
6595
|
): Promise<Transaction | VersionedTransaction> {
|
|
6596
|
+
txVersion = txVersion ?? this.txVersion;
|
|
6597
|
+
|
|
6598
|
+
// # Collect and process Tx Params
|
|
6599
|
+
let baseTxParams: BaseTxParams = {
|
|
6600
|
+
computeUnits: txParams?.computeUnits ?? this.txParams.computeUnits,
|
|
6601
|
+
computeUnitsPrice:
|
|
6602
|
+
txParams?.computeUnitsPrice ?? this.txParams.computeUnitsPrice,
|
|
6603
|
+
};
|
|
6604
|
+
|
|
6605
|
+
if (txParams?.useSimulatedComputeUnits) {
|
|
6606
|
+
const splitTxParams: {
|
|
6607
|
+
baseTxParams: BaseTxParams;
|
|
6608
|
+
txParamProcessingParams: ProcessingTxParams;
|
|
6609
|
+
} = {
|
|
6610
|
+
baseTxParams: {
|
|
6611
|
+
computeUnits: txParams?.computeUnits,
|
|
6612
|
+
computeUnitsPrice: txParams?.computeUnitsPrice,
|
|
6613
|
+
},
|
|
6614
|
+
txParamProcessingParams: {
|
|
6615
|
+
useSimulatedComputeUnits: txParams?.useSimulatedComputeUnits,
|
|
6616
|
+
computeUnitsBufferMultiplier: txParams?.computeUnitsBufferMultiplier,
|
|
6617
|
+
},
|
|
6618
|
+
};
|
|
6619
|
+
|
|
6620
|
+
const processedTxParams = await this.getProcessedTransactionParams(
|
|
6621
|
+
{
|
|
6622
|
+
instructions,
|
|
6623
|
+
txParams: splitTxParams.baseTxParams,
|
|
6624
|
+
txVersion,
|
|
6625
|
+
lookupTables,
|
|
6626
|
+
},
|
|
6627
|
+
splitTxParams.txParamProcessingParams
|
|
6628
|
+
);
|
|
6629
|
+
|
|
6630
|
+
baseTxParams = {
|
|
6631
|
+
...baseTxParams,
|
|
6632
|
+
...processedTxParams,
|
|
6633
|
+
};
|
|
6634
|
+
}
|
|
6635
|
+
|
|
6636
|
+
// # Create Tx Instructions
|
|
6532
6637
|
const allIx = [];
|
|
6533
|
-
const computeUnits =
|
|
6638
|
+
const computeUnits = baseTxParams?.computeUnits;
|
|
6534
6639
|
if (computeUnits !== 200_000) {
|
|
6535
6640
|
allIx.push(
|
|
6536
6641
|
ComputeBudgetProgram.setComputeUnitLimit({
|
|
@@ -6538,8 +6643,7 @@ export class DriftClient {
|
|
|
6538
6643
|
})
|
|
6539
6644
|
);
|
|
6540
6645
|
}
|
|
6541
|
-
const computeUnitsPrice =
|
|
6542
|
-
txParams?.computeUnitsPrice ?? this.txParams.computeUnitsPrice;
|
|
6646
|
+
const computeUnitsPrice = baseTxParams?.computeUnitsPrice;
|
|
6543
6647
|
if (computeUnitsPrice !== 0) {
|
|
6544
6648
|
allIx.push(
|
|
6545
6649
|
ComputeBudgetProgram.setComputeUnitPrice({
|
|
@@ -6554,9 +6658,24 @@ export class DriftClient {
|
|
|
6554
6658
|
allIx.push(instructions);
|
|
6555
6659
|
}
|
|
6556
6660
|
|
|
6557
|
-
|
|
6661
|
+
const latestBlockHashAndContext =
|
|
6662
|
+
await this.connection.getLatestBlockhashAndContext({
|
|
6663
|
+
commitment: this.opts.preflightCommitment,
|
|
6664
|
+
});
|
|
6665
|
+
|
|
6666
|
+
// # Create and return Transaction
|
|
6558
6667
|
if (txVersion === 'legacy') {
|
|
6559
|
-
|
|
6668
|
+
if (forceVersionedTransaction) {
|
|
6669
|
+
const message = new TransactionMessage({
|
|
6670
|
+
payerKey: this.provider.wallet.publicKey,
|
|
6671
|
+
recentBlockhash: latestBlockHashAndContext.value.blockhash,
|
|
6672
|
+
instructions: allIx,
|
|
6673
|
+
}).compileToLegacyMessage();
|
|
6674
|
+
|
|
6675
|
+
return new VersionedTransaction(message);
|
|
6676
|
+
} else {
|
|
6677
|
+
return new Transaction().add(...allIx);
|
|
6678
|
+
}
|
|
6560
6679
|
} else {
|
|
6561
6680
|
const marketLookupTable = await this.fetchMarketLookupTableAccount();
|
|
6562
6681
|
lookupTables = lookupTables
|
|
@@ -6564,11 +6683,7 @@ export class DriftClient {
|
|
|
6564
6683
|
: [marketLookupTable];
|
|
6565
6684
|
const message = new TransactionMessage({
|
|
6566
6685
|
payerKey: this.provider.wallet.publicKey,
|
|
6567
|
-
recentBlockhash:
|
|
6568
|
-
await this.provider.connection.getRecentBlockhash(
|
|
6569
|
-
this.opts.preflightCommitment
|
|
6570
|
-
)
|
|
6571
|
-
).blockhash,
|
|
6686
|
+
recentBlockhash: latestBlockHashAndContext.value.blockhash,
|
|
6572
6687
|
instructions: allIx,
|
|
6573
6688
|
}).compileToV0Message(lookupTables);
|
|
6574
6689
|
|
package/src/idl/drift.json
CHANGED
|
@@ -5,11 +5,19 @@ export type DriftPriorityFeeResponse = HeliusPriorityFeeLevels[];
|
|
|
5
5
|
|
|
6
6
|
export async function fetchDriftPriorityFee(
|
|
7
7
|
url: string,
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
marketTypes: string[],
|
|
9
|
+
marketIndexs: number[]
|
|
10
10
|
): Promise<DriftPriorityFeeResponse> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
try {
|
|
12
|
+
const response = await fetch(
|
|
13
|
+
`${url}/batchPriorityFees?marketType=${marketTypes.join(
|
|
14
|
+
','
|
|
15
|
+
)}&marketIndex=${marketIndexs.join(',')}`
|
|
16
|
+
);
|
|
17
|
+
return await response.json();
|
|
18
|
+
} catch (err) {
|
|
19
|
+
console.error(err);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return [];
|
|
15
23
|
}
|
|
@@ -29,23 +29,29 @@ export async function fetchHeliusPriorityFee(
|
|
|
29
29
|
lookbackDistance: number,
|
|
30
30
|
addresses: string[]
|
|
31
31
|
): Promise<HeliusPriorityFeeResponse> {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
try {
|
|
33
|
+
const response = await fetch(heliusRpcUrl, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: { 'Content-Type': 'application/json' },
|
|
36
|
+
body: JSON.stringify({
|
|
37
|
+
jsonrpc: '2.0',
|
|
38
|
+
id: '1',
|
|
39
|
+
method: 'getPriorityFeeEstimate',
|
|
40
|
+
params: [
|
|
41
|
+
{
|
|
42
|
+
accountKeys: addresses,
|
|
43
|
+
options: {
|
|
44
|
+
includeAllPriorityFeeLevels: true,
|
|
45
|
+
lookbackSlots: lookbackDistance,
|
|
46
|
+
},
|
|
45
47
|
},
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
],
|
|
49
|
+
}),
|
|
50
|
+
});
|
|
51
|
+
return await response.json();
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error(err);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return undefined;
|
|
51
57
|
}
|
|
@@ -13,7 +13,6 @@ import {
|
|
|
13
13
|
fetchHeliusPriorityFee,
|
|
14
14
|
} from './heliusPriorityFeeMethod';
|
|
15
15
|
import { fetchDriftPriorityFee } from './driftPriorityFeeMethod';
|
|
16
|
-
import { getVariant, MarketType } from '..';
|
|
17
16
|
|
|
18
17
|
export type DriftMarketInfo = {
|
|
19
18
|
marketType: string;
|
|
@@ -24,7 +23,7 @@ export class PriorityFeeSubscriber {
|
|
|
24
23
|
connection: Connection;
|
|
25
24
|
frequencyMs: number;
|
|
26
25
|
addresses: string[];
|
|
27
|
-
|
|
26
|
+
driftMarkets?: DriftMarketInfo[];
|
|
28
27
|
customStrategy?: PriorityFeeStrategy;
|
|
29
28
|
averageStrategy = new AverageOverSlotsStrategy();
|
|
30
29
|
maxStrategy = new MaxOverSlotsStrategy();
|
|
@@ -51,7 +50,7 @@ export class PriorityFeeSubscriber {
|
|
|
51
50
|
this.addresses = config.addresses
|
|
52
51
|
? config.addresses.map((address) => address.toBase58())
|
|
53
52
|
: [];
|
|
54
|
-
this.
|
|
53
|
+
this.driftMarkets = config.driftMarkets;
|
|
55
54
|
|
|
56
55
|
if (config.customStrategy) {
|
|
57
56
|
this.customStrategy = config.customStrategy;
|
|
@@ -107,13 +106,15 @@ export class PriorityFeeSubscriber {
|
|
|
107
106
|
this.lookbackDistance,
|
|
108
107
|
this.addresses
|
|
109
108
|
);
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
if (samples.length > 0) {
|
|
110
|
+
this.latestPriorityFee = samples[0].prioritizationFee;
|
|
111
|
+
this.lastSlotSeen = samples[0].slot;
|
|
112
112
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
this.lastAvgStrategyResult = this.averageStrategy.calculate(samples);
|
|
114
|
+
this.lastMaxStrategyResult = this.maxStrategy.calculate(samples);
|
|
115
|
+
if (this.customStrategy) {
|
|
116
|
+
this.lastCustomStrategyResult = this.customStrategy.calculate(samples);
|
|
117
|
+
}
|
|
117
118
|
}
|
|
118
119
|
}
|
|
119
120
|
|
|
@@ -137,26 +138,19 @@ export class PriorityFeeSubscriber {
|
|
|
137
138
|
}
|
|
138
139
|
|
|
139
140
|
private async loadForDrift(): Promise<void> {
|
|
140
|
-
if (!this.
|
|
141
|
+
if (!this.driftMarkets) {
|
|
141
142
|
return;
|
|
142
143
|
}
|
|
143
144
|
const sample = await fetchDriftPriorityFee(
|
|
144
145
|
this.driftPriorityFeeEndpoint!,
|
|
145
|
-
this.
|
|
146
|
-
this.
|
|
146
|
+
this.driftMarkets.map((m) => m.marketType),
|
|
147
|
+
this.driftMarkets.map((m) => m.marketIndex)
|
|
147
148
|
);
|
|
148
149
|
if (sample.length > 0) {
|
|
149
150
|
this.lastAvgStrategyResult = sample[HeliusPriorityLevel.MEDIUM];
|
|
150
151
|
this.lastMaxStrategyResult = sample[HeliusPriorityLevel.UNSAFE_MAX];
|
|
151
152
|
if (this.customStrategy) {
|
|
152
|
-
|
|
153
|
-
this.lastCustomStrategyResult = this.customStrategy.calculate({
|
|
154
|
-
jsonrpc: '',
|
|
155
|
-
result: {
|
|
156
|
-
priorityFeeLevels: sample[0],
|
|
157
|
-
},
|
|
158
|
-
id: '',
|
|
159
|
-
});
|
|
153
|
+
this.lastCustomStrategyResult = this.customStrategy.calculate(sample);
|
|
160
154
|
}
|
|
161
155
|
}
|
|
162
156
|
}
|
|
@@ -251,7 +245,7 @@ export class PriorityFeeSubscriber {
|
|
|
251
245
|
this.addresses = addresses.map((k) => k.toBase58());
|
|
252
246
|
}
|
|
253
247
|
|
|
254
|
-
public updateMarketTypeAndIndex(
|
|
255
|
-
this.
|
|
248
|
+
public updateMarketTypeAndIndex(driftMarkets: DriftMarketInfo[]) {
|
|
249
|
+
this.driftMarkets = driftMarkets;
|
|
256
250
|
}
|
|
257
251
|
}
|
|
@@ -10,19 +10,25 @@ export async function fetchSolanaPriorityFee(
|
|
|
10
10
|
lookbackDistance: number,
|
|
11
11
|
addresses: string[]
|
|
12
12
|
): Promise<SolanaPriorityFeeResponse[]> {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
try {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
const rpcJSONResponse: any = await connection._rpcRequest(
|
|
16
|
+
'getRecentPrioritizationFees',
|
|
17
|
+
[addresses]
|
|
18
|
+
);
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
const results: SolanaPriorityFeeResponse[] = rpcJSONResponse?.result;
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
if (!results.length) return;
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
// Sort and filter results based on the slot lookback setting
|
|
25
|
+
const descResults = results.sort((a, b) => b.slot - a.slot);
|
|
26
|
+
const cutoffSlot = descResults[0].slot - lookbackDistance;
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
return descResults.filter((result) => result.slot >= cutoffSlot);
|
|
29
|
+
} catch (err) {
|
|
30
|
+
console.error(err);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return [];
|
|
28
34
|
}
|
package/src/priorityFee/types.ts
CHANGED
|
@@ -2,12 +2,16 @@ 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
|
|
|
6
7
|
export interface PriorityFeeStrategy {
|
|
7
8
|
// calculate the priority fee for a given set of samples.
|
|
8
9
|
// expect samples to be sorted in descending order (by slot)
|
|
9
10
|
calculate(
|
|
10
|
-
samples:
|
|
11
|
+
samples:
|
|
12
|
+
| SolanaPriorityFeeResponse[]
|
|
13
|
+
| HeliusPriorityFeeResponse
|
|
14
|
+
| DriftPriorityFeeResponse
|
|
11
15
|
): number;
|
|
12
16
|
}
|
|
13
17
|
|
|
@@ -25,7 +29,7 @@ export type PriorityFeeSubscriberConfig = {
|
|
|
25
29
|
/// addresses you plan to write lock, used to determine priority fees
|
|
26
30
|
addresses?: PublicKey[];
|
|
27
31
|
/// drift market type and index, optionally provide at initialization time if using priorityFeeMethod.DRIFT
|
|
28
|
-
|
|
32
|
+
driftMarkets?: DriftMarketInfo[];
|
|
29
33
|
/// custom strategy to calculate priority fees, defaults to AVERAGE
|
|
30
34
|
customStrategy?: PriorityFeeStrategy;
|
|
31
35
|
/// method for fetching priority fee samples
|