-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathsolana.ts
1112 lines (953 loc) · 38.9 KB
/
solana.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import crypto from 'crypto';
import bs58 from 'bs58';
import fse from 'fs-extra';
import { TokenListType } from '../../services/base';
import { HttpException, SIMULATION_ERROR_MESSAGE, SIMULATION_ERROR_CODE } from '../../services/error-handler';
import { TokenInfo } from '@solana/spl-token-registry';
import {
Connection,
Keypair,
PublicKey,
ComputeBudgetProgram,
MessageV0,
Signer,
Transaction,
TransactionResponse,
VersionedTransaction,
VersionedTransactionResponse,
} from '@solana/web3.js';
import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, unpackAccount, getMint, programSupportsExtensions } from "@solana/spl-token";
import { walletPath } from '../../services/base';
import { ConfigManagerCertPassphrase } from '../../services/config-manager-cert-passphrase';
import { logger } from '../../services/logger';
import { TokenListResolutionStrategy } from '../../services/token-list-resolution';
import { Config, getSolanaConfig } from './solana.config';
import { SolanaController } from './solana.controllers';
// Constants used for fee calculations
export const BASE_FEE = 5000;
const LAMPORT_TO_SOL = 1 / Math.pow(10, 9);
enum TransactionResponseStatusCode {
FAILED = -1,
UNCONFIRMED = 0,
CONFIRMED = 1,
}
// Add accounts from https://triton.one/solana-prioritization-fees/ to track general fees
const PRIORITY_FEE_ACCOUNTS = [
'4qGj88CX3McdTXEviEaqeP2pnZJxRTsZFWyU3Mrnbku4',
'2oLNTQKRb4a2117kFi6BYTUDu3RPrMVAHFhCfPKMosxX',
'xKUz6fZ79SXnjGYaYhhYTYQBoRUBoCyuDMkBa1tL3zU',
'GASeo1wEK3Rwep6fsAt212Jw9zAYguDY5qUwTnyZ4RH',
'B8emFMG91JJsBELV4XVkTNe3YTs85x4nCqub7dRZUY1p',
'DteH7aNKykAG2b2KQo7DD9XvLBfNgAuf2ixj5HC7ppTk',
'5HngGmYzvSuh3XyU11brHDpMTHXQQRQQT4udGFtQSjgR',
'GD37bnQdGkDsjNqnVGr9qWTnQJSKMHbsiXX9tXLMUcaL',
'4po3YMfioHkNP4mL4N46UWJvBoQDS2HFjzGm1ifrUWuZ',
'5veMSa4ks66zydSaKSPMhV7H2eF88HvuKDArScNH9jaG',
];
interface PriorityFeeResponse {
jsonrpc: string;
result: Array<{
prioritizationFee: number;
slot: number;
}>;
id: number;
}
export class Solana {
public connection: Connection;
public network: string;
public nativeTokenSymbol: string;
public tokenList: TokenInfo[] = [];
public config: Config;
private _tokenMap: Record<string, TokenInfo> = {};
private static _instances: { [name: string]: Solana };
public controller: typeof SolanaController;
private static lastPriorityFeeEstimate: {
timestamp: number;
fee: number;
} | null = null;
private static PRIORITY_FEE_CACHE_MS = 10000; // 10 second cache
private constructor(network: string) {
this.network = network;
this.config = getSolanaConfig('solana', network);
this.nativeTokenSymbol = this.config.network.nativeCurrencySymbol;
this.connection = new Connection(this.config.network.nodeURL, { commitment: 'confirmed' });
this.controller = SolanaController;
}
public static async getInstance(network: string): Promise<Solana> {
if (!Solana._instances) {
Solana._instances = {};
}
if (!Solana._instances[network]) {
const instance = new Solana(network);
await instance.init();
Solana._instances[network] = instance;
}
return Solana._instances[network];
}
private async init(): Promise<void> {
try {
await this.loadTokens(
this.config.network.tokenListSource,
this.config.network.tokenListType
);
} catch (e) {
logger.error(`Failed to initialize ${this.network}: ${e}`);
throw e;
}
}
async getTokenList(
tokenListSource?: string,
tokenListType?: TokenListType
): Promise<TokenInfo[]> {
// If no source/type provided, return stored list
if (!tokenListSource || !tokenListType) {
return this.tokenList;
}
// Otherwise fetch new list
const tokens = await new TokenListResolutionStrategy(
tokenListSource,
tokenListType
).resolve();
return tokens;
}
async loadTokens(
tokenListSource: string,
tokenListType: TokenListType
): Promise<void> {
try {
// Get tokens from source
const tokens = await new TokenListResolutionStrategy(
tokenListSource,
tokenListType
).resolve();
this.tokenList = tokens;
// Create symbol -> token mapping
tokens.forEach((token: TokenInfo) => {
this._tokenMap[token.symbol] = token;
});
logger.info(`Loaded ${tokens.length} tokens for ${this.network}`);
} catch (error) {
logger.error(`Failed to load token list for ${this.network}: ${error.message}`);
throw error;
}
}
async getToken(addressOrSymbol: string): Promise<TokenInfo | null> {
// First try to find by symbol (case-insensitive)
const normalizedSearch = addressOrSymbol.toUpperCase().trim();
let token = this.tokenList.find(
(token: TokenInfo) => token.symbol.toUpperCase().trim() === normalizedSearch
);
// If not found by symbol, try to find by address
if (!token) {
token = this.tokenList.find(
(token: TokenInfo) => token.address.toLowerCase() === addressOrSymbol.toLowerCase()
);
}
// If still not found, try to create a new token assuming addressOrSymbol is an address
if (!token) {
try {
// Validate if it's a valid public key
const mintPubkey = new PublicKey(addressOrSymbol);
// Fetch mint info to get decimals
const mintInfo = await getMint(this.connection, mintPubkey);
// Create a basic token object with fetched decimals
token = {
address: addressOrSymbol,
symbol: `DUMMY_${addressOrSymbol.slice(0, 4)}`,
name: `Dummy Token (${addressOrSymbol.slice(0, 8)}...)`,
decimals: mintInfo.decimals,
chainId: 101,
};
} catch (e) {
// Not a valid public key or couldn't fetch mint info, return null
return null;
}
}
return token;
}
// returns Keypair for a private key, which should be encoded in Base58
getKeypairFromPrivateKey(privateKey: string): Keypair {
const decoded = bs58.decode(privateKey);
return Keypair.fromSecretKey(new Uint8Array(decoded));
}
async getWallet(address: string): Promise<Keypair> {
const path = `${walletPath}/solana`;
const encryptedPrivateKey: string = await fse.readFile(
`${path}/${address}.json`,
'utf8'
);
const passphrase = ConfigManagerCertPassphrase.readPassphrase();
if (!passphrase) {
throw new Error('missing passphrase');
}
const decrypted = await this.decrypt(encryptedPrivateKey, passphrase);
return Keypair.fromSecretKey(new Uint8Array(bs58.decode(decrypted)));
}
async encrypt(secret: string, password: string): Promise<string> {
const algorithm = 'aes-256-ctr';
const iv = crypto.randomBytes(16);
const salt = crypto.randomBytes(32);
const key = crypto.pbkdf2Sync(password, new Uint8Array(salt), 5000, 32, 'sha512');
const cipher = crypto.createCipheriv(algorithm, new Uint8Array(key), new Uint8Array(iv));
const encryptedBuffers = [
new Uint8Array(cipher.update(new Uint8Array(Buffer.from(secret)))),
new Uint8Array(cipher.final())
];
const encrypted = Buffer.concat(encryptedBuffers);
const ivJSON = iv.toJSON();
const saltJSON = salt.toJSON();
const encryptedJSON = encrypted.toJSON();
return JSON.stringify({
algorithm,
iv: ivJSON,
salt: saltJSON,
encrypted: encryptedJSON,
});
}
async decrypt(encryptedSecret: string, password: string): Promise<string> {
const hash = JSON.parse(encryptedSecret);
const salt = new Uint8Array(Buffer.from(hash.salt, 'utf8'));
const iv = new Uint8Array(Buffer.from(hash.iv, 'utf8'));
const key = crypto.pbkdf2Sync(password, salt, 5000, 32, 'sha512');
const decipher = crypto.createDecipheriv(
hash.algorithm,
new Uint8Array(key),
iv
);
const decryptedBuffers = [
new Uint8Array(decipher.update(new Uint8Array(Buffer.from(hash.encrypted, 'hex')))),
new Uint8Array(decipher.final())
];
const decrypted = Buffer.concat(decryptedBuffers);
return decrypted.toString();
}
async getBalance(wallet: Keypair, symbols?: string[]): Promise<Record<string, number>> {
const publicKey = wallet.publicKey;
let balances: Record<string, number> = {};
// Fetch SOL balance only if symbols is undefined or includes "SOL" (case-insensitive)
if (!symbols || symbols.some(s => s.toUpperCase() === "SOL")) {
const solBalance = await this.connection.getBalance(publicKey);
const solBalanceInSol = solBalance * LAMPORT_TO_SOL;
balances["SOL"] = solBalanceInSol;
}
// Get all token accounts for the provided address
const [legacyAccounts, token2022Accounts] = await Promise.all([
this.connection.getTokenAccountsByOwner(publicKey, { programId: TOKEN_PROGRAM_ID }),
this.connection.getTokenAccountsByOwner(publicKey, { programId: TOKEN_2022_PROGRAM_ID })
]);
const allAccounts = [...legacyAccounts.value, ...token2022Accounts.value];
// Process token accounts
for (const value of allAccounts) {
const programId = value.account.owner;
const parsedAccount = unpackAccount(
value.pubkey,
value.account,
programId,
);
const mintAddress = parsedAccount.mint.toBase58();
// Only check tokens from our token list when no symbols are specified
const token = this.tokenList.find(t => t.address === mintAddress);
if (token && (!symbols || symbols.some(s =>
s.toUpperCase() === token.symbol.toUpperCase() ||
s.toLowerCase() === mintAddress.toLowerCase()
))) {
const amount = parsedAccount.amount;
const uiAmount = Number(amount) / Math.pow(10, token.decimals);
balances[token.symbol] = uiAmount;
}
if (!token) {
console.log('token not found for address:', mintAddress);
} else {
console.log('token', token.symbol, 'balance:', parsedAccount.amount.toString());
}
}
return balances;
}
// returns a Solana TransactionResponse for a txHash.
async getTransaction(
payerSignature: string
): Promise<VersionedTransactionResponse | null> {
return this.connection.getTransaction(
payerSignature,
{
commitment: 'confirmed',
maxSupportedTransactionVersion: 0,
}
);
}
// returns a Solana TransactionResponseStatusCode for a txData.
public async getTransactionStatusCode(
txData: TransactionResponse | null
): Promise<TransactionResponseStatusCode> {
let txStatus;
if (!txData) {
// tx not yet confirmed by validator
txStatus = TransactionResponseStatusCode.UNCONFIRMED;
} else {
// If txData exists, check if there's an error in the metadata
txStatus =
txData.meta?.err == null
? TransactionResponseStatusCode.CONFIRMED
: TransactionResponseStatusCode.FAILED;
}
return txStatus;
}
// returns the current block number
async getCurrentBlockNumber(): Promise<number> {
return await this.connection.getSlot('processed');
}
async close() {
if (this.network in Solana._instances) {
delete Solana._instances[this.network];
}
}
public async getGasPrice(): Promise<number> {
const priorityFeePerCU = await this.estimatePriorityFees();
// Calculate total priority fee in lamports (priorityFeePerCU is already in lamports/CU)
const priorityFee = this.config.defaultComputeUnits * priorityFeePerCU;
// Add base fee (in lamports) and convert total to SOL
const totalLamports = BASE_FEE + priorityFee;
const gasCost = totalLamports * LAMPORT_TO_SOL;
return gasCost;
}
async estimatePriorityFees(): Promise<number> {
// Check cache first
if (
Solana.lastPriorityFeeEstimate &&
Date.now() - Solana.lastPriorityFeeEstimate.timestamp < Solana.PRIORITY_FEE_CACHE_MS
) {
return Solana.lastPriorityFeeEstimate.fee;
}
try {
const params: string[][] = [];
params.push(PRIORITY_FEE_ACCOUNTS);
const payload = {
method: 'getRecentPrioritizationFees',
params: params,
id: 1,
jsonrpc: '2.0',
};
const response = await fetch(this.connection.rpcEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
if (!response.ok) {
logger.error(`Failed to fetch priority fees, using minimum fee: ${response.status}`);
return this.config.minPriorityFee * 1_000_000 / this.config.defaultComputeUnits;
}
const data: PriorityFeeResponse = await response.json();
// Extract fees and filter out zeros
const fees = data.result
.map((item) => item.prioritizationFee)
.filter((fee) => fee > 0);
const minimumFeeLamports = this.config.minPriorityFee * 1e9 / this.config.defaultComputeUnits;
console.log('minimumFeeLamports', minimumFeeLamports);
if (fees.length === 0) {
return minimumFeeLamports;
}
// Sort fees in ascending order for percentile calculation
fees.sort((a, b) => a - b);
// Calculate statistics
const minFee = Math.min(...fees) / 1_000_000; // Convert to lamports
const maxFee = Math.max(...fees) / 1_000_000; // Convert to lamports
const averageFee = fees.reduce((sum, fee) => sum + fee, 0) / fees.length / 1_000_000 // Convert to lamports
logger.info(`Recent priority fees paid: ${minFee.toFixed(4)} - ${maxFee.toFixed(4)} lamports/CU (avg: ${averageFee.toFixed(4)})`);
// Calculate index for percentile
const percentileIndex = Math.ceil(fees.length * this.config.basePriorityFeePct / 100);
let basePriorityFee = fees[percentileIndex - 1] / 1_000_000; // Convert to lamports
// Ensure fee is not below minimum (convert SOL to lamports)
basePriorityFee = Math.max(basePriorityFee, minimumFeeLamports);
logger.info(`Base priority fee: ${basePriorityFee.toFixed(4)} lamports/CU (${basePriorityFee === minimumFeeLamports ? 'minimum' : `${this.config.basePriorityFeePct}th percentile`})`);
// Cache the result
Solana.lastPriorityFeeEstimate = {
timestamp: Date.now(),
fee: basePriorityFee,
};
return basePriorityFee;
} catch (error: any) {
throw new Error(`Failed to fetch priority fees: ${error.message}`);
}
}
public async confirmTransaction(
signature: string,
timeout: number = 3000,
): Promise<{ confirmed: boolean; txData?: any }> {
try {
const confirmationPromise = new Promise<{ confirmed: boolean; txData?: any }>(async (resolve, reject) => {
// Use getTransaction instead of getSignatureStatuses for more reliability
const txData = await this.connection.getTransaction(signature, {
commitment: 'confirmed',
maxSupportedTransactionVersion: 0,
});
if (!txData) {
return resolve({ confirmed: false });
}
// Check if transaction is already confirmed but had an error
if (txData.meta?.err) {
return reject(new Error(
`Transaction failed with error: ${JSON.stringify(txData.meta.err)}`
));
}
// More definitive check using slot confirmation
const status = await this.connection.getSignatureStatus(signature);
const isConfirmed = status.value?.confirmationStatus === 'confirmed' ||
status.value?.confirmationStatus === 'finalized';
resolve({ confirmed: !!isConfirmed, txData });
});
const timeoutPromise = new Promise<{ confirmed: boolean }>((_, reject) =>
setTimeout(() => reject(new Error('Confirmation timed out')), timeout),
);
return await Promise.race([confirmationPromise, timeoutPromise]);
} catch (error: any) {
throw new Error(`Failed to confirm transaction: ${error.message}`);
}
}
private getFee(txData: any): number {
if (!txData?.meta) {
return 0;
}
// Convert fee from lamports to SOL
return (txData.meta.fee || 0) * LAMPORT_TO_SOL;
}
public async sendAndConfirmTransaction(
tx: Transaction | VersionedTransaction,
signers: Signer[] = [],
computeUnits?: number
): Promise<{ signature: string; fee: number }> {
let currentPriorityFee = await this.estimatePriorityFees();
const computeUnitsToUse = computeUnits || this.config.defaultComputeUnits;
while (true) {
const basePriorityFeeLamports = currentPriorityFee * computeUnitsToUse;
logger.info(`Sending transaction with ${currentPriorityFee} lamports/CU priority fee and max priority fee of ${(basePriorityFeeLamports * LAMPORT_TO_SOL).toFixed(6)} SOL`);
// Check if we've exceeded max fee (convert maxPriorityFee from SOL to lamports)
if (basePriorityFeeLamports > this.config.maxPriorityFee * 1e9) {
throw new Error(`Exceeded maximum priority fee of ${this.config.maxPriorityFee} SOL`);
}
if (tx instanceof Transaction) {
tx = await this.prepareTx(
tx,
currentPriorityFee,
computeUnitsToUse,
signers
);
} else {
tx = await this.prepareVersionedTx(
tx,
currentPriorityFee,
computeUnitsToUse,
signers
);
await this.connection.simulateTransaction(tx);
}
let retryCount = 0;
while (retryCount < this.config.retryCount) {
try {
const signature = await this.sendRawTransaction(
'message' in tx ? tx.serialize() : tx.serialize(),
'lastValidBlockHeight' in tx ? tx.lastValidBlockHeight : undefined,
);
const confirmationResult = await this.confirmTransaction(signature);
logger.info(`[${retryCount + 1}/${this.config.retryCount}] Transaction ${signature} confirmation status: ${confirmationResult.confirmed ? 'confirmed' : 'unconfirmed'}`);
if (confirmationResult.confirmed && confirmationResult.txData) {
const actualFee = this.getFee(confirmationResult.txData);
logger.info(`Transaction ${signature} confirmed with total fee: ${actualFee.toFixed(6)} SOL`);
return { signature, fee: actualFee };
}
retryCount++;
await new Promise(resolve => setTimeout(resolve, this.config.retryIntervalMs));
} catch (error) {
// Only retry if error is not a definitive failure
if (error.message.includes('Transaction failed')) {
throw error;
}
retryCount++;
await new Promise(resolve => setTimeout(resolve, this.config.retryIntervalMs));
}
}
// If we get here, transaction wasn't confirmed after RETRY_COUNT attempts
// Increase the priority fee and try again
currentPriorityFee = Math.floor(currentPriorityFee * this.config.priorityFeeMultiplier);
logger.info(`Increasing priority fee to ${currentPriorityFee} lamports/CU (max fee of ${(currentPriorityFee * computeUnitsToUse * LAMPORT_TO_SOL).toFixed(6)} SOL`);
}
}
private async prepareTx(
tx: Transaction,
currentPriorityFee: number,
computeUnitsToUse: number,
signers: Signer[]
): Promise<Transaction> {
const priorityFeeMicroLamports = Math.floor(currentPriorityFee * 1_000_000);
const priorityFeeInstruction = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: priorityFeeMicroLamports,
});
// Remove any existing priority fee instructions and add the new one
tx.instructions = [
...tx.instructions.filter(inst => !inst.programId.equals(ComputeBudgetProgram.programId)),
priorityFeeInstruction
];
// Set compute unit limit
const computeUnitInstruction = ComputeBudgetProgram.setComputeUnitLimit({
units: computeUnitsToUse
});
tx.add(computeUnitInstruction);
// Get latest blockhash
const { value: { lastValidBlockHeight, blockhash } } =
await this.connection.getLatestBlockhashAndContext('confirmed');
tx.lastValidBlockHeight = lastValidBlockHeight;
tx.recentBlockhash = blockhash;
tx.sign(...signers);
return tx;
}
private async prepareVersionedTx(
tx: VersionedTransaction,
currentPriorityFee: number,
computeUnits: number,
_signers: Signer[]
): Promise<VersionedTransaction> {
const originalMessage = tx.message;
const originalStaticCount = originalMessage.staticAccountKeys.length;
const originalSignatures = tx.signatures; // Clone original signatures array
let modifiedTx: VersionedTransaction;
// Create new compute budget instructions
const priorityFeeMicroLamports = Math.floor(currentPriorityFee * 1_000_000);
const computeBudgetInstructions = [
ComputeBudgetProgram.setComputeUnitLimit({ units: computeUnits }),
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: priorityFeeMicroLamports })
];
// Check if ComputeBudgetProgram is in static keys
const computeBudgetProgramIndex = originalMessage.staticAccountKeys.findIndex(
key => key.equals(ComputeBudgetProgram.programId)
);
// Add ComputeBudget program to static keys, adjust indexes, and create modified instructions
if (computeBudgetProgramIndex === -1) {
// Add ComputeBudget program to static keys
const newStaticKeys = [
...originalMessage.staticAccountKeys,
ComputeBudgetProgram.programId,
];
// Process original instructions with index adjustment
const originalInstructions = originalMessage.compiledInstructions.map(ix => ({
...ix,
accountKeyIndexes: ix.accountKeyIndexes.map(index =>
index >= originalStaticCount ? index + 1 : index
)
}));
// Create modified instructions
const modifiedInstructions = [
...computeBudgetInstructions.map(ix => ({
programIdIndex: newStaticKeys.indexOf(ComputeBudgetProgram.programId), // Use new index
accountKeyIndexes: [],
data: ix.data instanceof Buffer ? new Uint8Array(ix.data) : ix.data
})),
...originalInstructions
];
// Build new transaction
modifiedTx = new VersionedTransaction(
new MessageV0({
header: originalMessage.header,
staticAccountKeys: newStaticKeys,
recentBlockhash: originalMessage.recentBlockhash!,
compiledInstructions: modifiedInstructions.map(ix => ({
programIdIndex: ix.programIdIndex,
accountKeyIndexes: ix.accountKeyIndexes,
data: ix.data instanceof Buffer ? new Uint8Array(ix.data) : ix.data
})),
addressTableLookups: originalMessage.addressTableLookups
})
);
} else {
// Remove compute budget instructions from original instructions
const nonComputeBudgetInstructions = originalMessage.compiledInstructions.filter(ix =>
!originalMessage.staticAccountKeys[ix.programIdIndex].equals(ComputeBudgetProgram.programId)
);
// Create modified instructions
const modifiedInstructions = [
...computeBudgetInstructions.map(ix => ({
programIdIndex: computeBudgetProgramIndex, // Use existing index if already present
accountKeyIndexes: [],
data: ix.data instanceof Buffer ? new Uint8Array(ix.data) : ix.data
})),
...nonComputeBudgetInstructions
];
// Build new transaction with same keys but modified instructions
modifiedTx = new VersionedTransaction(
new MessageV0({
header: originalMessage.header,
staticAccountKeys: originalMessage.staticAccountKeys,
recentBlockhash: originalMessage.recentBlockhash,
compiledInstructions: modifiedInstructions.map(ix => ({
programIdIndex: ix.programIdIndex,
accountKeyIndexes: ix.accountKeyIndexes,
data: ix.data instanceof Buffer ? new Uint8Array(ix.data) : ix.data
})),
addressTableLookups: originalMessage.addressTableLookups
})
);
}
// DON'T DELETE COMMENTS BELOW
// console.log('Original message:', JSON.stringify({
// message: {
// header: originalMessage.header,
// staticAccountKeys: originalMessage.staticAccountKeys.map(k => k.toBase58()),
// recentBlockhash: originalMessage.recentBlockhash,
// compiledInstructions: originalMessage.compiledInstructions.map(ix => ({
// programIdIndex: ix.programIdIndex,
// accountKeyIndexes: ix.accountKeyIndexes,
// data: bs58.encode(ix.data)
// })),
// addressTableLookups: originalMessage.addressTableLookups
// }
// }, null, 2));
// console.log('Modified transaction:', JSON.stringify({
// message: {
// header: modifiedTx.message.header,
// staticAccountKeys: modifiedTx.message.staticAccountKeys.map(k => k.toBase58()),
// recentBlockhash: modifiedTx.message.recentBlockhash,
// compiledInstructions: modifiedTx.message.compiledInstructions.map(ix => ({
// programIdIndex: ix.programIdIndex,
// accountKeyIndexes: ix.accountKeyIndexes,
// data: bs58.encode(ix.data)
// })),
// addressTableLookups: modifiedTx.message.addressTableLookups
// }
// }, null, 2));
modifiedTx.signatures = originalSignatures;
modifiedTx.sign([..._signers]);
console.log('modifiedTx:', modifiedTx);
return modifiedTx;
}
async sendAndConfirmRawTransaction(
transaction: VersionedTransaction | Transaction
): Promise<{ confirmed: boolean; signature: string; txData: any }> {
// Convert Transaction to VersionedTransaction if necessary
if (!(transaction instanceof VersionedTransaction)) {
// Ensure transaction is properly prepared
const { blockhash } = await this.connection.getLatestBlockhash();
transaction.recentBlockhash = transaction.recentBlockhash || blockhash;
transaction.feePayer = transaction.feePayer ||
(transaction.signatures[0]?.publicKey || null);
// Get serialized transaction bytes
const serializedTx = transaction.serialize();
return this._sendAndConfirmRawTransaction(serializedTx);
}
// For VersionedTransaction, use existing logic
const serializedTx = transaction.serialize();
return this._sendAndConfirmRawTransaction(serializedTx);
}
// Create a private method to handle the actual sending
private async _sendAndConfirmRawTransaction(
serializedTx: Buffer | Uint8Array
): Promise<{ confirmed: boolean; signature: string; txData: any }> {
let retryCount = 0;
while (retryCount < this.config.retryCount) {
const signature = await this.connection.sendRawTransaction(
serializedTx,
{ skipPreflight: true }
);
const { confirmed, txData } = await this.confirmTransaction(signature);
logger.info(`[${retryCount + 1}/${this.config.retryCount}] Transaction ${signature} status: ${confirmed ? 'confirmed' : 'unconfirmed'}`);
if (confirmed && txData) {
return { confirmed, signature, txData };
}
retryCount++;
await new Promise((resolve) => setTimeout(resolve, this.config.retryIntervalMs));
}
return { confirmed: false, signature: '', txData: null };
}
async sendRawTransaction(
rawTx: Buffer | Uint8Array | Array<number>,
lastValidBlockHeight: number,
): Promise<string> {
let blockheight = await this.connection
.getBlockHeight({ commitment: 'confirmed' });
let signatures: string[];
let retryCount = 0;
while (blockheight <= lastValidBlockHeight + 50) {
try {
const sendRawTransactionResults = await Promise.allSettled([
this.connection.sendRawTransaction(rawTx, {
skipPreflight: true,
preflightCommitment: 'confirmed',
maxRetries: 0,
})
]);
const successfulResults = sendRawTransactionResults.filter(
(result) => result.status === 'fulfilled',
);
if (successfulResults.length > 0) {
// Map all successful results to get their values (signatures)
signatures = successfulResults
.map((result) => (result.status === 'fulfilled' ? result.value : ''))
.filter(sig => sig !== ''); // Filter out empty strings
// Verify all signatures match
if (!signatures.every((sig) => sig === signatures[0])) {
retryCount++;
await new Promise((resolve) => setTimeout(resolve, this.config.retryIntervalMs));
continue;
}
return signatures[0];
}
retryCount++;
await new Promise((resolve) => setTimeout(resolve, this.config.retryIntervalMs));
} catch (error) {
retryCount++;
await new Promise((resolve) => setTimeout(resolve, this.config.retryIntervalMs));
}
}
// If we exit the while loop without returning, we've exceeded block height
throw new Error('Maximum blockheight exceeded');
}
async extractTokenBalanceChangeAndFee(
signature: string,
mint: string,
owner: string,
): Promise<{ balanceChange: number; fee: number }> {
let txDetails;
for (let attempt = 0; attempt < 20; attempt++) {
try {
txDetails = await this.connection.getParsedTransaction(signature, {
maxSupportedTransactionVersion: 0,
});
if (txDetails) {
break; // Exit loop if txDetails is not null
} else {
throw new Error('Transaction details are null');
}
} catch (error: any) {
if (attempt < 10) {
await new Promise((resolve) => setTimeout(resolve, 1000));
} else {
// Return default values after 10 attempts
return { balanceChange: 0, fee: 0 };
}
}
}
const preTokenBalances = txDetails.meta?.preTokenBalances || [];
const postTokenBalances = txDetails.meta?.postTokenBalances || [];
const preBalance =
preTokenBalances.find((balance) => balance.mint === mint && balance.owner === owner)
?.uiTokenAmount.uiAmount || 0;
const postBalance =
postTokenBalances.find((balance) => balance.mint === mint && balance.owner === owner)
?.uiTokenAmount.uiAmount || 0;
const balanceChange = postBalance - preBalance;
const fee = (txDetails.meta?.fee || 0) / 1_000_000_000; // Convert lamports to SOL
return { balanceChange, fee };
}
async extractAccountBalanceChangeAndFee(
signature: string,
accountIndex: number,
): Promise<{ balanceChange: number; fee: number }> {
let txDetails;
for (let attempt = 0; attempt < 20; attempt++) {
try {
txDetails = await this.connection.getParsedTransaction(signature, {
maxSupportedTransactionVersion: 0,
});
if (txDetails) {
break; // Exit loop if txDetails is not null
} else {
throw new Error('Transaction details are null');
}
} catch (error: any) {
if (attempt < 19) {
await new Promise((resolve) => setTimeout(resolve, 1000));
} else {
// Return default values after 10 attempts
return { balanceChange: 0, fee: 0 };
}
}
}
const preBalances = txDetails.meta?.preBalances || [];
const postBalances = txDetails.meta?.postBalances || [];
const balanceChange =
Math.abs(postBalances[accountIndex] - preBalances[accountIndex]) * LAMPORT_TO_SOL;
const fee = (txDetails.meta?.fee || 0) * LAMPORT_TO_SOL;
return { balanceChange, fee };
}
// Validate if a string is a valid Solana private key
public static validateSolPrivateKey(secretKey: string): boolean {
try {
const secretKeyBytes = bs58.decode(secretKey);
Keypair.fromSecretKey(new Uint8Array(secretKeyBytes));
return true;
} catch (error) {
return false;
}
}
// Add new method to get first wallet address
public async getFirstWalletAddress(): Promise<string | null> {
const path = `${walletPath}/solana`;
try {
// Create directory if it doesn't exist
await fse.ensureDir(path);
// Get all .json files in the directory
const files = await fse.readdir(path);
const walletFiles = files.filter(f => f.endsWith('.json'));
if (walletFiles.length === 0) {
return null;
}
// Return first wallet address (without .json extension)
return walletFiles[0].slice(0, -5);
} catch (error) {
return null;
}
}
// Update getTokenBySymbol to use new getToken method
public async getTokenBySymbol(tokenSymbol: string): Promise<TokenInfo | undefined> {
return (await this.getToken(tokenSymbol)) || undefined;
}
/**
* Helper function to get balance changes for both tokens in a pair
* @param signature Transaction signature
* @param tokenX First token (can be SOL)
* @param tokenY Second token (can be SOL)
* @param walletAddress Wallet address to check balances for
* @returns Balance changes and transaction fee
*/
async extractPairBalanceChangesAndFee(
signature: string,
tokenX: TokenInfo,
tokenY: TokenInfo,
walletAddress: string,
): Promise<{
baseTokenBalanceChange: number;
quoteTokenBalanceChange: number;
fee: number;
}> {
let baseTokenBalanceChange: number, quoteTokenBalanceChange: number;
if (tokenX.symbol === 'SOL') {
({ balanceChange: baseTokenBalanceChange } = await this.extractAccountBalanceChangeAndFee(signature, 0));
} else {
({ balanceChange: baseTokenBalanceChange } = await this.extractTokenBalanceChangeAndFee(
signature,
tokenX.address,
walletAddress
));
}
if (tokenY.symbol === 'SOL') {
({ balanceChange: quoteTokenBalanceChange } = await this.extractAccountBalanceChangeAndFee(signature, 0));
} else {
({ balanceChange: quoteTokenBalanceChange } = await this.extractTokenBalanceChangeAndFee(
signature,
tokenY.address,
walletAddress
));
}
const { fee } = await this.extractAccountBalanceChangeAndFee(signature, 0);
return {
baseTokenBalanceChange: Math.abs(baseTokenBalanceChange),
quoteTokenBalanceChange: Math.abs(quoteTokenBalanceChange),
fee,
};
}
public async simulateTransaction(transaction: VersionedTransaction | Transaction) {
try {
if (!(transaction instanceof VersionedTransaction)) {
// Convert regular Transaction to VersionedTransaction for simulation
const { blockhash } = await this.connection.getLatestBlockhash();
transaction.recentBlockhash = transaction.recentBlockhash || blockhash;
transaction.feePayer = transaction.feePayer ||
(transaction.signatures[0]?.publicKey || null);
// Convert to VersionedTransaction
const messageV0 = new MessageV0({
header: transaction.compileMessage().header,