Repository navigation
Expand file tree
/
Copy pathbot.ts
More file actions
1099 lines (990 loc) · 37 KB
/
Copy pathbot.ts
File metadata and controls
1099 lines (990 loc) · 37 KB
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 {
ComputeBudgetProgram,
Connection,
Keypair,
PublicKey,
TransactionMessage,
VersionedTransaction,
} from '@solana/web3.js';
import {
createAssociatedTokenAccountIdempotentInstruction,
createCloseAccountInstruction,
getAccount,
getAssociatedTokenAddress,
RawAccount,
TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
import { Liquidity, LiquidityPoolKeysV4, LiquidityStateV4, Percent, Token, TokenAmount } from '@raydium-io/raydium-sdk';
import { MarketCache, PoolCache, PumpFunCache, SnipeListCache } from './cache';
import { PoolFilters } from './filters';
import { TransactionExecutor } from './transactions';
import {
computeSolOutForTokens,
computeTokensOutForSol,
createPoolKeys,
createPumpFunBuyInstruction,
createPumpFunSellInstruction,
decodeBondingCurve,
getAssociatedBondingCurve,
getBondingCurvePDA,
getDynamicComputeUnitPrice,
JupiterClient,
logger,
NETWORK,
simulateVersionedTransaction,
sleep,
SOL_MINT,
waitForExitCondition,
} from './helpers';
import { Mutex } from 'async-mutex';
import BN from 'bn.js';
import { WarpTransactionExecutor } from './transactions/warp-transaction-executor';
import { JitoTransactionExecutor } from './transactions/jito-rpc-transaction-executor';
import { CircuitBreaker } from './risk/circuit-breaker';
import { PositionBook } from './risk/positions';
export interface BotConfig {
wallet: Keypair;
checkRenounced: boolean;
checkFreezable: boolean;
checkBurned: boolean;
minPoolSize: TokenAmount;
maxPoolSize: TokenAmount;
quoteToken: Token;
quoteAmount: TokenAmount;
quoteAta: PublicKey;
oneTokenAtATime: boolean;
useSnipeList: boolean;
autoSell: boolean;
autoBuyDelay: number;
autoSellDelay: number;
maxBuyRetries: number;
maxSellRetries: number;
unitLimit: number;
unitPrice: number;
takeProfit: number;
stopLoss: number;
buySlippage: number;
sellSlippage: number;
priceCheckInterval: number;
priceCheckDuration: number;
filterCheckInterval: number;
filterCheckDuration: number;
consecutiveMatchCount: number;
pumpFunBuyAmountSol?: number;
pumpFunMaxCurveProgress?: number;
dryRun: boolean;
maxOpenPositions: number;
maxDailyRaydiumBuys: number;
maxDailyPumpFunBuySol: number;
trailingStop: number;
trailingStopActivation: number;
takeProfitSellPercent: number;
buyCooldownMs: number;
dynamicPriorityFee: boolean;
priorityFeeMultiplier: number;
maxComputeUnitPrice: number;
simulateBeforeSend: boolean;
enableJupiterSell: boolean;
enableJupiterCopyBuy: boolean;
}
export class Bot {
private readonly poolFilters: PoolFilters;
private readonly snipeListCache?: SnipeListCache;
private readonly mutex: Mutex;
private readonly positions: PositionBook;
private sellExecutionCount = 0;
public readonly isWarp: boolean = false;
public readonly isJito: boolean = false;
private currentDay = this.getUtcDayKey();
private dailyRaydiumBuys = 0;
private dailyPumpFunBuyLamports = 0n;
constructor(
private readonly connection: Connection,
private readonly marketStorage: MarketCache,
private readonly poolStorage: PoolCache,
private readonly txExecutor: TransactionExecutor,
readonly config: BotConfig,
private readonly pumpFunStorage: PumpFunCache = new PumpFunCache(),
private readonly jupiter: JupiterClient = new JupiterClient(''),
private readonly breaker: CircuitBreaker = new CircuitBreaker(4, 300_000),
) {
this.isWarp = txExecutor instanceof WarpTransactionExecutor;
this.isJito = txExecutor instanceof JitoTransactionExecutor;
this.mutex = new Mutex();
this.positions = new PositionBook(this.config.buyCooldownMs);
this.poolFilters = new PoolFilters(connection, {
quoteToken: this.config.quoteToken,
minPoolSize: this.config.minPoolSize,
maxPoolSize: this.config.maxPoolSize,
});
if (this.config.useSnipeList) {
this.snipeListCache = new SnipeListCache();
this.snipeListCache.init();
}
}
public isBusy(): boolean {
return this.mutex.isLocked() || this.sellExecutionCount > 0;
}
public snapshot() {
return {
openPositions: this.positions.size,
mints: this.positions.openMints,
dailyRaydiumBuys: this.dailyRaydiumBuys,
circuitPaused: this.breaker.isPaused,
};
}
async validate() {
if (this.config.dryRun) {
logger.warn('DRY_RUN is enabled, skipping startup wallet ATA validation and network execution.');
return true;
}
try {
await getAccount(this.connection, this.config.quoteAta, this.connection.commitment);
} catch (error) {
logger.error(
`${this.config.quoteToken.symbol} token account not found in wallet: ${this.config.wallet.publicKey.toString()}`,
);
return false;
}
return true;
}
public async buy(accountId: PublicKey, poolState: LiquidityStateV4) {
logger.trace({ mint: poolState.baseMint }, `Processing new pool...`);
if (!this.breaker.assertCanTrade('raydium-buy')) return;
if (this.config.useSnipeList && !this.snipeListCache?.isInList(poolState.baseMint.toString())) {
logger.debug({ mint: poolState.baseMint.toString() }, `Skipping buy because token is not in a snipe list`);
return;
}
const baseMint = poolState.baseMint.toString();
if (this.positions.isOnCooldown(baseMint)) {
logger.debug({ mint: baseMint }, 'Skipping buy because mint is in post-sell cooldown');
return;
}
this.resetDailyRiskCountersIfNeeded();
if (!this.canOpenNewPosition(baseMint)) return;
if (this.dailyRaydiumBuys >= this.config.maxDailyRaydiumBuys) {
logger.warn(
{ maxDailyRaydiumBuys: this.config.maxDailyRaydiumBuys },
'Skipping buy because max daily Raydium buys limit was reached',
);
return;
}
if (this.config.autoBuyDelay > 0) {
logger.debug({ mint: poolState.baseMint }, `Waiting for ${this.config.autoBuyDelay} ms before buy`);
await sleep(this.config.autoBuyDelay);
}
if (this.config.oneTokenAtATime) {
if (this.mutex.isLocked() || this.sellExecutionCount > 0) {
logger.debug(
{ mint: poolState.baseMint.toString() },
`Skipping buy because one token at a time is turned on and token is already being processed`,
);
return;
}
await this.mutex.acquire();
}
try {
const [market, mintAta] = await Promise.all([
this.marketStorage.get(poolState.marketId.toString()),
getAssociatedTokenAddress(poolState.baseMint, this.config.wallet.publicKey),
]);
const poolKeys: LiquidityPoolKeysV4 = createPoolKeys(accountId, poolState, market);
if (!this.config.useSnipeList) {
const match = await this.filterMatch(poolKeys);
if (!match) {
logger.trace({ mint: poolKeys.baseMint.toString() }, `Skipping buy because pool doesn't match filters`);
return;
}
}
for (let i = 0; i < this.config.maxBuyRetries; i++) {
try {
logger.info(
{ mint: poolState.baseMint.toString() },
`Send buy transaction attempt: ${i + 1}/${this.config.maxBuyRetries}`,
);
const tokenOut = new Token(TOKEN_PROGRAM_ID, poolKeys.baseMint, poolKeys.baseDecimals);
const result = await this.swap(
poolKeys,
this.config.quoteAta,
mintAta,
this.config.quoteToken,
tokenOut,
this.config.quoteAmount,
this.config.buySlippage,
this.config.wallet,
'buy',
true,
);
if (result.confirmed) {
this.positions.open({
mint: baseMint,
dex: 'raydium',
entryQuoteAmount: BigInt(this.config.quoteAmount.raw.toString()),
tokenAmount: 0n,
openedAt: Date.now(),
scaledOut: false,
});
this.dailyRaydiumBuys++;
this.breaker.recordSuccess();
logger.info(
{
mint: poolState.baseMint.toString(),
signature: result.signature,
url: `https://solscan.io/tx/${result.signature}?cluster=${NETWORK}`,
},
`Confirmed buy tx`,
);
break;
}
logger.info(
{
mint: poolState.baseMint.toString(),
signature: result.signature,
error: result.error,
},
`Error confirming buy tx`,
);
} catch (error) {
logger.debug({ mint: poolState.baseMint.toString(), error }, `Error confirming buy transaction`);
}
}
} catch (error) {
this.breaker.recordFailure('raydium-buy');
logger.error({ mint: poolState.baseMint.toString(), error }, `Failed to buy token`);
} finally {
if (this.config.oneTokenAtATime) {
this.mutex.release();
}
}
}
public async sell(accountId: PublicKey, rawAccount: RawAccount) {
const mint = rawAccount.mint.toString();
if (!this.positions.tryLockSell(mint)) {
logger.debug({ mint }, 'Skipping sell because a sell is already in progress for this mint');
return;
}
if (this.config.oneTokenAtATime) {
this.sellExecutionCount++;
}
try {
logger.trace({ mint: rawAccount.mint }, `Processing new token...`);
const poolData = await this.poolStorage.get(mint);
if (!poolData) {
if (this.config.enableJupiterSell) {
logger.info({ mint }, 'No Raydium pool cache entry — attempting Jupiter sell');
await this.sellViaJupiter(mint, BigInt(rawAccount.amount.toString()), true);
return;
}
logger.trace({ mint }, `Token pool data is not found, can't sell`);
return;
}
const tokenIn = new Token(TOKEN_PROGRAM_ID, poolData.state.baseMint, poolData.state.baseDecimal.toNumber());
const tokenAmountIn = new TokenAmount(tokenIn, rawAccount.amount, true);
if (tokenAmountIn.isZero()) {
logger.info({ mint }, `Empty balance, can't sell`);
return;
}
if (this.config.autoSellDelay > 0) {
logger.debug({ mint: rawAccount.mint }, `Waiting for ${this.config.autoSellDelay} ms before sell`);
await sleep(this.config.autoSellDelay);
}
const market = await this.marketStorage.get(poolData.state.marketId.toString());
const poolKeys: LiquidityPoolKeysV4 = createPoolKeys(new PublicKey(poolData.id), poolData.state, market);
const reason = await this.waitForRaydiumExit(tokenAmountIn, poolKeys);
logger.info({ mint, reason }, 'Raydium exit condition reached');
const scaleOut =
reason === 'take_profit' &&
this.config.takeProfitSellPercent < 100 &&
!(this.positions.get(mint)?.scaledOut ?? false);
if (scaleOut) {
const partial = this.scaleTokenAmount(tokenAmountIn, this.config.takeProfitSellPercent);
const partialResult = await this.executeRaydiumSell(poolKeys, accountId, tokenIn, partial, false);
if (partialResult) {
this.positions.markScaledOut(mint);
const remainingRaw = BigInt(tokenAmountIn.raw.toString()) - BigInt(partial.raw.toString());
if (remainingRaw > 0n) {
const remaining = new TokenAmount(tokenIn, new BN(remainingRaw.toString()), true);
const restReason = await this.waitForRaydiumExit(remaining, poolKeys, true);
logger.info({ mint, restReason }, 'Remaining position exit condition reached');
await this.executeRaydiumSell(poolKeys, accountId, tokenIn, remaining, true);
} else {
this.positions.close(mint);
}
return;
}
}
await this.executeRaydiumSell(poolKeys, accountId, tokenIn, tokenAmountIn, true);
} catch (error) {
this.breaker.recordFailure('raydium-sell');
logger.error({ mint, error }, `Failed to sell token`);
} finally {
this.positions.unlockSell(mint);
if (this.config.oneTokenAtATime) {
this.sellExecutionCount--;
}
}
}
public async copyBuy(mint: PublicKey) {
if (!this.config.enableJupiterCopyBuy || !this.jupiter.enabled) return;
const mintStr = mint.toString();
if (mint.equals(this.config.quoteToken.mint) || mintStr === SOL_MINT) return;
if (!this.breaker.assertCanTrade('copy-buy')) return;
if (this.config.useSnipeList && !this.snipeListCache?.isInList(mintStr)) return;
if (this.positions.isOnCooldown(mintStr) || this.positions.has(mintStr)) return;
this.resetDailyRiskCountersIfNeeded();
if (!this.canOpenNewPosition(mintStr)) return;
if (this.dailyRaydiumBuys >= this.config.maxDailyRaydiumBuys) return;
logger.info({ mint: mintStr }, 'Copy-trade buy via Jupiter');
const quote = await this.jupiter.quote({
inputMint: this.config.quoteToken.mint.toBase58(),
outputMint: mintStr,
amount: this.config.quoteAmount.raw.toString(),
slippageBps: Math.floor(this.config.buySlippage * 100),
});
if (!quote) {
logger.debug({ mint: mintStr }, 'Copy-trade skipped — no Jupiter route yet');
return;
}
const filled = await this.executeJupiterQuote(quote, 'copy-buy');
if (filled) {
this.positions.open({
mint: mintStr,
dex: 'copy',
entryQuoteAmount: BigInt(this.config.quoteAmount.raw.toString()),
tokenAmount: BigInt(quote.outAmount),
openedAt: Date.now(),
scaledOut: false,
});
this.dailyRaydiumBuys++;
}
}
private async swap(
poolKeys: LiquidityPoolKeysV4,
ataIn: PublicKey,
ataOut: PublicKey,
tokenIn: Token,
tokenOut: Token,
amountIn: TokenAmount,
slippage: number,
wallet: Keypair,
direction: 'buy' | 'sell',
closeAtaOnSell: boolean,
) {
if (this.config.dryRun) {
logger.info(
{
mint: poolKeys.baseMint.toString(),
direction,
amountIn: amountIn.toFixed(),
slippage,
},
'DRY_RUN: skipping on-chain Raydium swap transaction',
);
return { confirmed: true, signature: 'dry-run' };
}
const slippagePercent = new Percent(slippage, 100);
const poolInfo = await Liquidity.fetchInfo({
connection: this.connection,
poolKeys,
});
const computedAmountOut = Liquidity.computeAmountOut({
poolKeys,
poolInfo,
amountIn,
currencyOut: tokenOut,
slippage: slippagePercent,
});
const latestBlockhash = await this.connection.getLatestBlockhash();
const { innerTransaction } = Liquidity.makeSwapFixedInInstruction(
{
poolKeys: poolKeys,
userKeys: {
tokenAccountIn: ataIn,
tokenAccountOut: ataOut,
owner: wallet.publicKey,
},
amountIn: amountIn.raw,
minAmountOut: computedAmountOut.minAmountOut.raw,
},
poolKeys.version,
);
const unitPrice = await this.resolveUnitPrice();
const messageV0 = new TransactionMessage({
payerKey: wallet.publicKey,
recentBlockhash: latestBlockhash.blockhash,
instructions: [
...(this.isWarp || this.isJito
? []
: [
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: unitPrice }),
ComputeBudgetProgram.setComputeUnitLimit({ units: this.config.unitLimit }),
]),
...(direction === 'buy'
? [
createAssociatedTokenAccountIdempotentInstruction(
wallet.publicKey,
ataOut,
wallet.publicKey,
tokenOut.mint,
),
]
: []),
...innerTransaction.instructions,
...(direction === 'sell' && closeAtaOnSell
? [createCloseAccountInstruction(ataIn, wallet.publicKey, wallet.publicKey)]
: []),
],
}).compileToV0Message();
const transaction = new VersionedTransaction(messageV0);
transaction.sign([wallet, ...innerTransaction.signers]);
if (this.config.simulateBeforeSend) {
const simulation = await simulateVersionedTransaction(this.connection, transaction);
if (!simulation.ok) {
return { confirmed: false, error: `simulation failed: ${simulation.error}` };
}
}
return this.txExecutor.executeAndConfirm(transaction, wallet, latestBlockhash);
}
private async executeRaydiumSell(
poolKeys: LiquidityPoolKeysV4,
accountId: PublicKey,
tokenIn: Token,
tokenAmountIn: TokenAmount,
closeAta: boolean,
): Promise<boolean> {
const mint = poolKeys.baseMint.toString();
const jupiterBetter = await this.shouldPreferJupiterSell(mint, tokenAmountIn, poolKeys);
if (jupiterBetter) {
const sold = await this.sellViaJupiter(mint, BigInt(tokenAmountIn.raw.toString()), closeAta);
if (sold) return true;
logger.warn({ mint }, 'Jupiter sell failed, falling back to Raydium');
}
for (let i = 0; i < this.config.maxSellRetries; i++) {
try {
logger.info({ mint }, `Send sell transaction attempt: ${i + 1}/${this.config.maxSellRetries}`);
const result = await this.swap(
poolKeys,
accountId,
this.config.quoteAta,
tokenIn,
this.config.quoteToken,
tokenAmountIn,
this.config.sellSlippage,
this.config.wallet,
'sell',
closeAta,
);
if (result.confirmed) {
if (closeAta) this.positions.close(mint);
this.breaker.recordSuccess();
logger.info(
{
dex: `https://dexscreener.com/solana/${mint}?maker=${this.config.wallet.publicKey}`,
mint,
signature: result.signature,
url: `https://solscan.io/tx/${result.signature}?cluster=${NETWORK}`,
},
`Confirmed sell tx`,
);
return true;
}
logger.info({ mint, signature: result.signature, error: result.error }, `Error confirming sell tx`);
} catch (error) {
logger.debug({ mint, error }, `Error confirming sell transaction`);
}
}
this.breaker.recordFailure('raydium-sell-retries');
return false;
}
private async shouldPreferJupiterSell(
mint: string,
tokenAmountIn: TokenAmount,
poolKeys: LiquidityPoolKeysV4,
): Promise<boolean> {
if (!this.config.enableJupiterSell || !this.jupiter.enabled) return false;
try {
const [jupiterQuote, poolInfo] = await Promise.all([
this.jupiter.quote({
inputMint: mint,
outputMint: this.config.quoteToken.mint.toBase58(),
amount: tokenAmountIn.raw.toString(),
slippageBps: Math.floor(this.config.sellSlippage * 100),
}),
Liquidity.fetchInfo({ connection: this.connection, poolKeys }),
]);
if (!jupiterQuote) return false;
const raydiumOut = Liquidity.computeAmountOut({
poolKeys,
poolInfo,
amountIn: tokenAmountIn,
currencyOut: this.config.quoteToken,
slippage: new Percent(this.config.sellSlippage, 100),
}).amountOut;
const jupiterOut = BigInt(jupiterQuote.outAmount);
const raydiumRaw = BigInt(raydiumOut.raw.toString());
const better = jupiterOut > (raydiumRaw * 10030n) / 10000n;
if (better) {
logger.info(
{ mint, jupiterOut: jupiterOut.toString(), raydiumOut: raydiumRaw.toString() },
'Jupiter route beats direct Raydium sell',
);
}
return better;
} catch (error) {
logger.debug({ mint, error }, 'Failed to compare Jupiter vs Raydium sell quotes');
return false;
}
}
private async sellViaJupiter(mint: string, amount: bigint, closePosition: boolean): Promise<boolean> {
if (!this.jupiter.enabled || amount <= 0n) return false;
const quote = await this.jupiter.quote({
inputMint: mint,
outputMint: this.config.quoteToken.mint.toBase58(),
amount,
slippageBps: Math.floor(this.config.sellSlippage * 100),
});
if (!quote) return false;
const filled = await this.executeJupiterQuote(quote, `jupiter-sell:${mint}`);
if (filled && closePosition) {
this.positions.close(mint);
}
return filled;
}
private async executeJupiterQuote(quote: { outAmount: string; [key: string]: unknown }, context: string): Promise<boolean> {
if (this.config.dryRun) {
logger.info({ context, outAmount: quote.outAmount }, 'DRY_RUN: skipping Jupiter swap');
return true;
}
const built = await this.jupiter.buildSignedSwap(quote as Parameters<JupiterClient['buildSignedSwap']>[0], this.config.wallet);
if (!built) return false;
if (this.config.simulateBeforeSend) {
const simulation = await simulateVersionedTransaction(this.connection, built.transaction);
if (!simulation.ok) {
logger.debug({ context, error: simulation.error }, 'Jupiter swap simulation failed');
return false;
}
}
const latestBlockhash = await this.connection.getLatestBlockhash();
const result = await this.txExecutor.executeAndConfirm(built.transaction, this.config.wallet, latestBlockhash);
if (!result.confirmed) {
this.breaker.recordFailure(context);
logger.info({ context, signature: result.signature, error: result.error }, 'Jupiter swap failed');
return false;
}
this.breaker.recordSuccess();
logger.info(
{
context,
signature: result.signature,
url: `https://solscan.io/tx/${result.signature}?cluster=${NETWORK}`,
},
'Confirmed Jupiter swap',
);
return true;
}
private async filterMatch(poolKeys: LiquidityPoolKeysV4) {
if (this.config.filterCheckInterval === 0 || this.config.filterCheckDuration === 0) {
return true;
}
const timesToCheck = this.config.filterCheckDuration / this.config.filterCheckInterval;
let timesChecked = 0;
let matchCount = 0;
do {
try {
const shouldBuy = await this.poolFilters.execute(poolKeys);
if (shouldBuy) {
matchCount++;
if (this.config.consecutiveMatchCount <= matchCount) {
logger.debug(
{ mint: poolKeys.baseMint.toString() },
`Filter match ${matchCount}/${this.config.consecutiveMatchCount}`,
);
return true;
}
} else {
matchCount = 0;
}
await sleep(this.config.filterCheckInterval);
} finally {
timesChecked++;
}
} while (timesChecked < timesToCheck);
return false;
}
public isPumpFunMint(mint: string): boolean {
return !!this.pumpFunStorage.get(mint);
}
public async buyPumpFun(mint: PublicKey) {
const mintStr = mint.toString();
logger.trace({ mint: mintStr }, `Processing new pump.fun token...`);
if (!this.breaker.assertCanTrade('pumpfun-buy')) return;
if (this.config.useSnipeList && !this.snipeListCache?.isInList(mintStr)) {
logger.debug({ mint: mintStr }, `Skipping pump.fun buy (not on snipe list)`);
return;
}
if (this.positions.isOnCooldown(mintStr)) return;
this.resetDailyRiskCountersIfNeeded();
if (!this.canOpenNewPosition(mintStr)) return;
if (this.config.oneTokenAtATime) {
if (this.mutex.isLocked() || this.sellExecutionCount > 0) {
logger.debug({ mint: mintStr }, `Skipping pump.fun buy (one-at-a-time busy)`);
return;
}
await this.mutex.acquire();
}
try {
const bondingCurve = getBondingCurvePDA(mint);
const associatedBondingCurve = getAssociatedBondingCurve(bondingCurve, mint);
const associatedUser = await getAssociatedTokenAddress(mint, this.config.wallet.publicKey);
const info = await this.connection.getAccountInfo(bondingCurve, this.connection.commitment);
if (!info?.data) {
logger.debug({ mint: mintStr }, `Bonding curve not found`);
return;
}
const curve = decodeBondingCurve(info.data);
if (curve.complete) {
logger.debug({ mint: mintStr }, `Bonding curve complete, skipping`);
return;
}
const progressPct =
curve.tokenTotalSupply > 0n
? Number(((curve.tokenTotalSupply - curve.realTokenReserves) * 10000n) / curve.tokenTotalSupply) / 100
: 0;
const maxProgress = this.config.pumpFunMaxCurveProgress ?? 100;
if (progressPct > maxProgress) {
logger.debug({ mint: mintStr, progressPct }, `Curve progress too high, skipping`);
return;
}
const solInLamports = BigInt(Math.floor((this.config.pumpFunBuyAmountSol ?? 0.001) * 1_000_000_000));
const nextDailyPumpFunTotal = this.dailyPumpFunBuyLamports + solInLamports;
const maxDailyPumpFunLamports = BigInt(Math.floor(this.config.maxDailyPumpFunBuySol * 1_000_000_000));
if (nextDailyPumpFunTotal > maxDailyPumpFunLamports) {
logger.warn(
{
mint: mintStr,
maxDailyPumpFunBuySol: this.config.maxDailyPumpFunBuySol,
},
`Skipping pump.fun buy because max daily SOL budget was reached`,
);
return;
}
const expectedTokens = computeTokensOutForSol(curve, solInLamports);
if (expectedTokens <= 0n) {
logger.debug({ mint: mintStr }, `Expected tokens out is zero`);
return;
}
const slippageBps = BigInt(Math.floor(this.config.buySlippage * 100));
const maxSolCost = (solInLamports * (10000n + slippageBps)) / 10000n;
const maxSolCostWithFee = (maxSolCost * 101n) / 100n;
this.pumpFunStorage.save({
mint,
bondingCurve,
associatedBondingCurve,
state: curve,
});
for (let i = 0; i < this.config.maxBuyRetries; i++) {
try {
logger.info({ mint: mintStr }, `Send pump.fun buy tx attempt: ${i + 1}/${this.config.maxBuyRetries}`);
const result = await this.executePumpFunBuy({
mint,
bondingCurve,
associatedBondingCurve,
associatedUser,
amount: expectedTokens,
maxSolCost: maxSolCostWithFee,
});
if (result.confirmed) {
this.positions.open({
mint: mintStr,
dex: 'pumpfun',
entryQuoteAmount: solInLamports,
tokenAmount: expectedTokens,
openedAt: Date.now(),
scaledOut: false,
});
this.dailyPumpFunBuyLamports = nextDailyPumpFunTotal;
this.breaker.recordSuccess();
logger.info(
{
mint: mintStr,
signature: result.signature,
url: `https://solscan.io/tx/${result.signature}?cluster=${NETWORK}`,
},
`Confirmed pump.fun buy`,
);
break;
}
logger.info(
{ mint: mintStr, signature: result.signature, error: result.error },
`Error confirming pump.fun buy`,
);
} catch (error) {
logger.debug({ mint: mintStr, error }, `Error sending pump.fun buy`);
}
}
} catch (error) {
this.breaker.recordFailure('pumpfun-buy');
logger.error({ mint: mint.toString(), error }, `Failed to buy pump.fun token`);
} finally {
if (this.config.oneTokenAtATime) {
this.mutex.release();
}
}
}
public async sellPumpFun(userAta: PublicKey, rawAccount: RawAccount) {
const mintStr = rawAccount.mint.toString();
if (!this.positions.tryLockSell(mintStr)) return;
if (this.config.oneTokenAtATime) {
this.sellExecutionCount++;
}
try {
const entry = this.pumpFunStorage.get(mintStr);
if (!entry) return;
const tokensIn = BigInt(rawAccount.amount.toString());
if (tokensIn === 0n) {
logger.info({ mint: mintStr }, `Empty pump.fun balance`);
return;
}
if (this.config.autoSellDelay > 0) {
await sleep(this.config.autoSellDelay);
}
const reason = await this.waitForPumpFunExit(entry.mint, tokensIn);
logger.info({ mint: mintStr, reason }, 'pump.fun exit condition reached');
for (let i = 0; i < this.config.maxSellRetries; i++) {
try {
const curveInfo = await this.connection.getAccountInfo(entry.bondingCurve, this.connection.commitment);
if (!curveInfo?.data) break;
const curve = decodeBondingCurve(curveInfo.data);
if (curve.complete) {
logger.info({ mint: mintStr }, `Curve graduated; selling via Jupiter instead of pump.fun`);
const sold = await this.sellViaJupiter(mintStr, tokensIn, true);
if (!sold) {
logger.warn({ mint: mintStr }, 'Graduated token Jupiter sell failed');
}
break;
}
const solOut = computeSolOutForTokens(curve, tokensIn);
const slippageBps = BigInt(Math.floor(this.config.sellSlippage * 100));
const minSolOutput = (solOut * (10000n - slippageBps)) / 10000n;
logger.info({ mint: mintStr }, `Send pump.fun sell tx attempt: ${i + 1}/${this.config.maxSellRetries}`);
const result = await this.executePumpFunSell({
mint: entry.mint,
bondingCurve: entry.bondingCurve,
associatedBondingCurve: entry.associatedBondingCurve,
associatedUser: userAta,
amount: tokensIn,
minSolOutput,
});
if (result.confirmed) {
this.positions.close(mintStr);
this.breaker.recordSuccess();
logger.info(
{
mint: mintStr,
signature: result.signature,
url: `https://solscan.io/tx/${result.signature}?cluster=${NETWORK}`,
},
`Confirmed pump.fun sell`,
);
break;
}
logger.info(
{ mint: mintStr, signature: result.signature, error: result.error },
`Error confirming pump.fun sell`,
);
} catch (error) {
logger.debug({ mint: mintStr, error }, `Error pump.fun sell`);
}
}
} catch (error) {
this.breaker.recordFailure('pumpfun-sell');
logger.error({ mint: mintStr, error }, `Failed to sell pump.fun token`);
} finally {
this.positions.unlockSell(mintStr);
if (this.config.oneTokenAtATime) {
this.sellExecutionCount--;
}
}
}
private async executePumpFunBuy(params: {
mint: PublicKey;
bondingCurve: PublicKey;
associatedBondingCurve: PublicKey;
associatedUser: PublicKey;
amount: bigint;
maxSolCost: bigint;
}) {
if (this.config.dryRun) {
logger.info({ mint: params.mint.toString() }, 'DRY_RUN: skipping on-chain pump.fun buy transaction');
return { confirmed: true, signature: 'dry-run' };
}
const latestBlockhash = await this.connection.getLatestBlockhash();
const unitPrice = await this.resolveUnitPrice();
const ixs = [
...(this.isWarp || this.isJito
? []
: [
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: unitPrice }),
ComputeBudgetProgram.setComputeUnitLimit({ units: this.config.unitLimit }),
]),
createAssociatedTokenAccountIdempotentInstruction(
this.config.wallet.publicKey,
params.associatedUser,
this.config.wallet.publicKey,
params.mint,
),
createPumpFunBuyInstruction({
mint: params.mint,
user: this.config.wallet.publicKey,
bondingCurve: params.bondingCurve,
associatedBondingCurve: params.associatedBondingCurve,
associatedUser: params.associatedUser,
amount: params.amount,
maxSolCost: params.maxSolCost,
}),
];
const messageV0 = new TransactionMessage({
payerKey: this.config.wallet.publicKey,
recentBlockhash: latestBlockhash.blockhash,
instructions: ixs,
}).compileToV0Message();
const tx = new VersionedTransaction(messageV0);
tx.sign([this.config.wallet]);
if (this.config.simulateBeforeSend) {
const simulation = await simulateVersionedTransaction(this.connection, tx);
if (!simulation.ok) {
return { confirmed: false, error: `simulation failed: ${simulation.error}` };
}
}
return this.txExecutor.executeAndConfirm(tx, this.config.wallet, latestBlockhash);
}
private async executePumpFunSell(params: {
mint: PublicKey;
bondingCurve: PublicKey;
associatedBondingCurve: PublicKey;
associatedUser: PublicKey;
amount: bigint;
minSolOutput: bigint;
}) {
if (this.config.dryRun) {
logger.info({ mint: params.mint.toString() }, 'DRY_RUN: skipping on-chain pump.fun sell transaction');
return { confirmed: true, signature: 'dry-run' };
}
const latestBlockhash = await this.connection.getLatestBlockhash();
const unitPrice = await this.resolveUnitPrice();
const ixs = [
...(this.isWarp || this.isJito
? []
: [
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: unitPrice }),
ComputeBudgetProgram.setComputeUnitLimit({ units: this.config.unitLimit }),
]),
createPumpFunSellInstruction({
mint: params.mint,
user: this.config.wallet.publicKey,
bondingCurve: params.bondingCurve,
associatedBondingCurve: params.associatedBondingCurve,
associatedUser: params.associatedUser,
amount: params.amount,
minSolOutput: params.minSolOutput,
}),
createCloseAccountInstruction(params.associatedUser, this.config.wallet.publicKey, this.config.wallet.publicKey),
];
const messageV0 = new TransactionMessage({
payerKey: this.config.wallet.publicKey,
recentBlockhash: latestBlockhash.blockhash,
instructions: ixs,
}).compileToV0Message();
const tx = new VersionedTransaction(messageV0);
tx.sign([this.config.wallet]);
if (this.config.simulateBeforeSend) {
const simulation = await simulateVersionedTransaction(this.connection, tx);
if (!simulation.ok) {