Jelajahi Sumber

feat: add support for buying only specified tokens

Filip Dunder 1 tahun lalu
induk
melakukan
9e1b2da0f1
4 mengubah file dengan 84 tambahan dan 17 penghapusan
  1. 3 1
      .env.copy
  2. 20 1
      README.md
  3. 61 15
      buy.ts
  4. 0 0
      snipe-list.txt

+ 3 - 1
.env.copy

@@ -3,4 +3,6 @@ RPC_ENDPOINT=https://api.mainnet-beta.solana.com
 RPC_WEBSOCKET_ENDPOINT=wss://api.mainnet-beta.solana.com
 QUOTE_MINT=WSOL
 QUOTE_AMOUNT=0.1
-COMMITMENT_LEVEL=finalized
+COMMITMENT_LEVEL=finalized
+USE_SNIPE_LIST=false
+SNIPE_LIST_REFRESH_INTERVAL=30000

+ 20 - 1
README.md

@@ -1,6 +1,6 @@
 
 # Solana Sniper Bot
-This code is written as proof of concept for demonstrating how we can buy new tokens immediately after liquidity pool is created.
+This code is written as proof of concept for demonstrating how we can buy new tokens immediately after liquidity pool is open for trading.
 
 Script listens to new raydium USDC/SOL pools and buys token for a fixed amount in USDC/SOL.  
 Depending on speed of RPC node, the purchase usually happens before token is available on Raydium UI for swapping.
@@ -18,15 +18,34 @@ In order to run the script you need to:
   - QUOTE_MINT (which pools to snipe, USDC or WSOL)
   - QUOTE_AMOUNT (amount used to buy each new token)
   - COMMITMENT_LEVEL
+  - USE_SNIPE_LIST (buy only tokens listed in snipe-list.txt)
+  - SNIPE_LIST_REFRESH_INTERVAL (how often snipe list should be refreshed in milliseconds)
 - Install dependencies by typing: `npm install`
 - Run the script by typing: `npm run buy` in terminal
 
 You should see the following output:  
 ![output](output.png)
 
+## Snipe list
+By default, script buys each token which has new liquidity pool created and open for trading. 
+There are scenarios when you want to buy one specific token as soon as possible during the launch event.
+To achieve this, you'll have to use snipe list.
+- Change variable `USE_SNIPE_LIST` to `true`
+- Add token mint addresses you wish to buy in `snipe-list.txt` file
+  - Add each address as a new line
+
+This will prevent script from buying everything, and instead it will buy just listed tokens.
+You can update the list while script is running. Script will check for new values in specified interval (`SNIPE_LIST_REFRESH_INTERVAL`).
+
+Pool must not exist before the script starts.
+It will buy only when new pool is open for trading. If you want to buy token that will be launched in the future, make sure that script is running before the launch.
+
 ## Common issues
 If you have an error which is not listed here, please create a new issue in this repository.
 
+### Empty transaction
+- If you see empty transactions on SolScan most likely fix is to change commitment level to `finalized`.
+
 ### Unsupported RPC node
 - If you see following error in your log file:  
   `Error: 410 Gone:  {"jsonrpc":"2.0","error":{"code": 410, "message":"The RPC call or parameters have been disabled."}, "id": "986f3599-b2b7-47c4-b951-074c19842bad" }`  

+ 61 - 15
buy.ts

@@ -35,6 +35,8 @@ import { retrieveEnvVariable } from './utils';
 import { getAllMarketsV3, MinimalMarketLayoutV3 } from './market';
 import pino from 'pino';
 import bs58 from 'bs58';
+import * as fs from 'fs';
+import * as path from 'path';
 
 const transport = pino.transport({
   targets: [
@@ -80,7 +82,6 @@ const solanaConnection = new Connection(RPC_ENDPOINT, {
 export type MinimalTokenAccountData = {
   mint: PublicKey;
   address: PublicKey;
-  ata: PublicKey;
   poolKeys?: LiquidityPoolKeys;
   market?: MinimalMarketLayoutV3;
 };
@@ -96,7 +97,16 @@ let wallet: Keypair;
 let quoteToken: Token;
 let quoteTokenAssociatedAddress: PublicKey;
 let quoteAmount: TokenAmount;
-let commitment: Commitment = retrieveEnvVariable('COMMITMENT_LEVEL', logger) as Commitment;
+let commitment: Commitment = retrieveEnvVariable(
+  'COMMITMENT_LEVEL',
+  logger,
+) as Commitment;
+
+const USE_SNIPE_LIST = Boolean(retrieveEnvVariable('USE_SNIPE_LIST', logger));
+const SNIPE_LIST_REFRESH_INTERVAL = Number(
+  retrieveEnvVariable('SNIPE_LIST_REFRESH_INTERVAL', logger),
+);
+let snipeList: string[] = [];
 
 async function init(): Promise<void> {
   // get wallet
@@ -132,7 +142,7 @@ async function init(): Promise<void> {
   }
 
   logger.info(
-    `Script will buy all new tokens using ${QUOTE_MINT}. Amount that will be used to buy each token is: ${quoteAmount.toFixed().toString()}`
+    `Script will buy all new tokens using ${QUOTE_MINT}. Amount that will be used to buy each token is: ${quoteAmount.toFixed().toString()}`,
   );
 
   // get all existing liquidity pools
@@ -146,13 +156,12 @@ async function init(): Promise<void> {
   );
 
   // get all open-book markets
-  const allMarkets = await getAllMarketsV3(solanaConnection, quoteToken.mint, commitment);
-  existingOpenBookMarkets = new Set(allMarkets.map((p) => p.id.toString()));
-  const tokenAccounts = await getTokenAccounts(
+  const allMarkets = await getAllMarketsV3(
     solanaConnection,
-    wallet.publicKey,
+    quoteToken.mint,
     commitment,
   );
+  existingOpenBookMarkets = new Set(allMarkets.map((p) => p.id.toString()));
 
   logger.info(
     `Total ${quoteToken.symbol} markets ${existingOpenBookMarkets.size}`,
@@ -162,6 +171,12 @@ async function init(): Promise<void> {
   );
 
   // check existing wallet for associated token account of quote mint
+  const tokenAccounts = await getTokenAccounts(
+    solanaConnection,
+    wallet.publicKey,
+    commitment,
+  );
+
   for (const ta of tokenAccounts) {
     existingTokenAccounts.set(ta.accountInfo.mint.toString(), <
       MinimalTokenAccountData
@@ -182,6 +197,9 @@ async function init(): Promise<void> {
   }
 
   quoteTokenAssociatedAddress = tokenAccount.pubkey;
+
+  // load tokens to snipe
+  loadSnipeList();
 }
 
 export async function processRaydiumPool(updatedAccountInfo: KeyedAccountInfo) {
@@ -190,6 +208,11 @@ export async function processRaydiumPool(updatedAccountInfo: KeyedAccountInfo) {
     accountData = LIQUIDITY_STATE_LAYOUT_V4.decode(
       updatedAccountInfo.accountInfo.data,
     );
+
+    if (!shouldBuy(accountData.baseMint.toString())) {
+      return;
+    }
+
     await buy(updatedAccountInfo.accountId, accountData);
   } catch (e) {
     logger.error({ ...accountData, error: e }, `Failed to process pool`);
@@ -297,16 +320,36 @@ async function buy(
   );
 }
 
+function loadSnipeList() {
+  if (!USE_SNIPE_LIST) {
+    return;
+  }
+
+  const count = snipeList.length;
+  const data = fs.readFileSync(path.join(__dirname, 'snipe-list.txt'), 'utf-8');
+  snipeList = data
+    .split('\n')
+    .map((a) => a.trim())
+    .filter((a) => a);
+
+  if (snipeList.length != count) {
+    logger.info(`Loaded snipe list: ${snipeList.length}`);
+  }
+}
+
+function shouldBuy(key: string): boolean {
+  return USE_SNIPE_LIST ? snipeList.includes(key) : true;
+}
+
 const runListener = async () => {
   await init();
   const raydiumSubscriptionId = solanaConnection.onProgramAccountChange(
     RAYDIUM_LIQUIDITY_PROGRAM_ID_V4,
     async (updatedAccountInfo) => {
-      const existing = existingLiquidityPools.has(
-        updatedAccountInfo.accountId.toString(),
-      );
+      const key = updatedAccountInfo.accountId.toString();
+      const existing = existingLiquidityPools.has(key);
       if (!existing) {
-        existingLiquidityPools.add(updatedAccountInfo.accountId.toString());
+        existingLiquidityPools.add(key);
         const _ = processRaydiumPool(updatedAccountInfo);
       }
     },
@@ -337,11 +380,10 @@ const runListener = async () => {
   const openBookSubscriptionId = solanaConnection.onProgramAccountChange(
     OPENBOOK_PROGRAM_ID,
     async (updatedAccountInfo) => {
-      const existing = existingOpenBookMarkets.has(
-        updatedAccountInfo.accountId.toString(),
-      );
+      const key = updatedAccountInfo.accountId.toString();
+      const existing = existingOpenBookMarkets.has(key);
       if (!existing) {
-        existingOpenBookMarkets.add(updatedAccountInfo.accountId.toString());
+        existingOpenBookMarkets.add(key);
         const _ = processOpenBookMarket(updatedAccountInfo);
       }
     },
@@ -359,6 +401,10 @@ const runListener = async () => {
 
   logger.info(`Listening for raydium changes: ${raydiumSubscriptionId}`);
   logger.info(`Listening for open book changes: ${openBookSubscriptionId}`);
+
+  if (USE_SNIPE_LIST) {
+    setInterval(loadSnipeList, SNIPE_LIST_REFRESH_INTERVAL);
+  }
 };
 
 runListener();

+ 0 - 0
snipe-list.txt