⚙️ How It Works (Solana Sandwich Bot ‐ Node.js) - cryptoking-max/solana-sandwich-bot GitHub Wiki
⚙️ How It Works (Solana Sandwich Bot - Node.js)
A sandwich attack involves inserting two trades around a target (victim) transaction to exploit price movements caused by that transaction. The attack is performed in this sequence:
[Front-run (you)] ➝ [Victim Trade] ➝ [Back-run (you)]
Let’s break this down step-by-step for Solana:
1. Mempool Listening
- The bot connects to a private RPC node or gRPC stream (like Helius or Triton).
- It listens to pending swap instructions, especially targeting DEX aggregator transactions (e.g., Jupiter).
- Node.js tools: Use WebSocket (
websocket
,ws
) or gRPC (@grpc/grpc-js
) to subscribe to transaction data.
You listen for
Swap
,Route
, or Jupiter-specific instructions in transactions before they're confirmed.
2. Transaction Filtering
-
The bot analyzes each transaction to:
- Detect large swap amounts (e.g., >1000 USDC).
- Ensure low slippage protection (i.e., sandwichable).
- Confirm DEX involvement (usually Jupiter Aggregator).
- Validate that the token pair has sufficient liquidity.
You decode instructions using
@solana/web3.js
or a parser likeborsh
orbuffer-layout
.
3. Simulate Front-Run
-
Before acting, the bot queries the Jupiter Quote API to simulate:
- A small front-run buy of the same token pair.
- The expected price impact of the victim’s trade.
- The back-run sell profitability.
const res = await axios.get(`https://quote-api.jup.ag/v6/quote`, {
params: {
inputMint: SOL,
outputMint: targetToken,
amount: frontRunAmount,
slippage: 0.5
}
});
The goal is to ensure the round-trip trade will be profitable after the victim moves the price.
4. Front-Run Execution
- The bot submits a buy transaction just before the victim’s transaction.
- This can be done by manually setting transaction priority or using Solana’s compute unit price to speed up confirmation.
- Use
sendTransaction()
with manual signing and pre-flight disabled for speed.
const transaction = new Transaction().add(...);
transaction.feePayer = yourPublicKey;
transaction.recentBlockhash = await getLatestBlockhash();
transaction.sign(...);
await connection.sendRawTransaction(serializedTx, { skipPreflight: true });
5. Victim Transaction Monitoring
- The bot monitors the targeted transaction hash until it’s confirmed.
- Once confirmed, it ensures the price has moved in the predicted direction (in your favor).
This can be done using
connection.getSignatureStatus()
or listening for block confirmations.
6. Back-Run Execution
- Once the price is favorable, the bot sends a sell transaction to complete the sandwich.
- It uses a Jupiter route or DEX swap back to the base token (e.g., USDC or SOL).
- The spread between buy and sell becomes the profit.
7. Profit Tracking and Logs
-
The bot calculates:
- Entry price, exit price
- Gas cost (compute unit cost × price)
- Net profit in SOL or USDC
-
Logs are stored locally or in a database (e.g., MongoDB or JSON logs).
🔄 Summary Flow:
[Listen] ---> [Filter] ---> [Simulate] ---> [Front-Run]
|
[Wait for Victim Confirm]
|
[Back-Run Sell]
|
[Log Profit / Loss]