buy.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import {
  2. Liquidity,
  3. LIQUIDITY_STATE_LAYOUT_V4,
  4. LiquidityPoolKeys,
  5. LiquidityStateV4,
  6. MARKET_STATE_LAYOUT_V2,
  7. MARKET_STATE_LAYOUT_V3,
  8. MarketStateV3,
  9. Token,
  10. TokenAmount,
  11. } from '@raydium-io/raydium-sdk';
  12. import {
  13. createAssociatedTokenAccountIdempotentInstruction,
  14. getAssociatedTokenAddressSync,
  15. TOKEN_PROGRAM_ID,
  16. } from '@solana/spl-token';
  17. import {
  18. Keypair,
  19. Connection,
  20. PublicKey,
  21. ComputeBudgetProgram,
  22. KeyedAccountInfo,
  23. TransactionMessage,
  24. VersionedTransaction,
  25. Commitment,
  26. } from '@solana/web3.js';
  27. import {
  28. getAllAccountsV4,
  29. getTokenAccounts,
  30. RAYDIUM_LIQUIDITY_PROGRAM_ID_V4,
  31. OPENBOOK_PROGRAM_ID,
  32. createPoolKeys,
  33. } from './liquidity';
  34. import { retrieveEnvVariable } from './utils';
  35. import { getAllMarketsV3, MinimalMarketLayoutV3 } from './market';
  36. import pino from 'pino';
  37. import bs58 from 'bs58';
  38. import * as fs from 'fs';
  39. import * as path from 'path';
  40. import BN from 'bn.js';
  41. const transport = pino.transport({
  42. targets: [
  43. {
  44. level: 'trace',
  45. target: 'pino/file',
  46. options: {
  47. destination: 'buy.log',
  48. },
  49. },
  50. {
  51. level: 'trace',
  52. target: 'pino-pretty',
  53. options: {},
  54. },
  55. ],
  56. });
  57. export const logger = pino(
  58. {
  59. redact: ['poolKeys'],
  60. serializers: {
  61. error: pino.stdSerializers.err,
  62. },
  63. base: undefined,
  64. },
  65. transport,
  66. );
  67. const network = 'mainnet-beta';
  68. const RPC_ENDPOINT = retrieveEnvVariable('RPC_ENDPOINT', logger);
  69. const RPC_WEBSOCKET_ENDPOINT = retrieveEnvVariable(
  70. 'RPC_WEBSOCKET_ENDPOINT',
  71. logger,
  72. );
  73. const solanaConnection = new Connection(RPC_ENDPOINT, {
  74. wsEndpoint: RPC_WEBSOCKET_ENDPOINT,
  75. });
  76. export type MinimalTokenAccountData = {
  77. mint: PublicKey;
  78. address: PublicKey;
  79. poolKeys?: LiquidityPoolKeys;
  80. market?: MinimalMarketLayoutV3;
  81. };
  82. let existingLiquidityPools: Set<string> = new Set<string>();
  83. let existingOpenBookMarkets: Set<string> = new Set<string>();
  84. let existingTokenAccounts: Map<string, MinimalTokenAccountData> = new Map<
  85. string,
  86. MinimalTokenAccountData
  87. >();
  88. let wallet: Keypair;
  89. let quoteToken: Token;
  90. let quoteTokenAssociatedAddress: PublicKey;
  91. let quoteAmount: TokenAmount;
  92. let commitment: Commitment = retrieveEnvVariable(
  93. 'COMMITMENT_LEVEL',
  94. logger,
  95. ) as Commitment;
  96. const USE_SNIPE_LIST = retrieveEnvVariable('USE_SNIPE_LIST', logger) === 'true';
  97. const SNIPE_LIST_REFRESH_INTERVAL = Number(
  98. retrieveEnvVariable('SNIPE_LIST_REFRESH_INTERVAL', logger),
  99. );
  100. let snipeList: string[] = [];
  101. async function init(): Promise<void> {
  102. // get wallet
  103. const PRIVATE_KEY = retrieveEnvVariable('PRIVATE_KEY', logger);
  104. wallet = Keypair.fromSecretKey(bs58.decode(PRIVATE_KEY));
  105. logger.info(`Wallet Address: ${wallet.publicKey}`);
  106. // get quote mint and amount
  107. const QUOTE_MINT = retrieveEnvVariable('QUOTE_MINT', logger);
  108. const QUOTE_AMOUNT = retrieveEnvVariable('QUOTE_AMOUNT', logger);
  109. switch (QUOTE_MINT) {
  110. case 'WSOL': {
  111. quoteToken = Token.WSOL;
  112. quoteAmount = new TokenAmount(Token.WSOL, QUOTE_AMOUNT, false);
  113. break;
  114. }
  115. case 'USDC': {
  116. quoteToken = new Token(
  117. TOKEN_PROGRAM_ID,
  118. new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
  119. 6,
  120. 'USDC',
  121. 'USDC',
  122. );
  123. quoteAmount = new TokenAmount(quoteToken, QUOTE_AMOUNT, false);
  124. break;
  125. }
  126. default: {
  127. throw new Error(
  128. `Unsupported quote mint "${QUOTE_MINT}". Supported values are USDC and WSOL`,
  129. );
  130. }
  131. }
  132. logger.info(
  133. `Script will buy all new tokens using ${QUOTE_MINT}. Amount that will be used to buy each token is: ${quoteAmount.toFixed().toString()}`,
  134. );
  135. let message = {
  136. embeds: [
  137. {
  138. title: `Script will buy all new tokens using ${QUOTE_MINT}. Amount that will be used to buy each token is: ${quoteAmount.toFixed().toString()}`,
  139. color: 1127128,
  140. },
  141. ],
  142. };
  143. // post it to discord
  144. const DISCORD_WEBHOOK = retrieveEnvVariable('DISCORD_WEBHOOK', logger);
  145. // use native fetch to post to discord
  146. fetch(DISCORD_WEBHOOK, {
  147. method: 'POST',
  148. headers: {
  149. 'Content-Type': 'application/json',
  150. },
  151. body: JSON.stringify(message),
  152. });
  153. // get all existing liquidity pools
  154. const allLiquidityPools = await getAllAccountsV4(
  155. solanaConnection,
  156. quoteToken.mint,
  157. commitment,
  158. );
  159. existingLiquidityPools = new Set(
  160. allLiquidityPools.map((p) => p.id.toString()),
  161. );
  162. // get all open-book markets
  163. const allMarkets = await getAllMarketsV3(
  164. solanaConnection,
  165. quoteToken.mint,
  166. commitment,
  167. );
  168. existingOpenBookMarkets = new Set(allMarkets.map((p) => p.id.toString()));
  169. logger.info(
  170. `Total ${quoteToken.symbol} markets ${existingOpenBookMarkets.size}`,
  171. );
  172. logger.info(
  173. `Total ${quoteToken.symbol} pools ${existingLiquidityPools.size}`,
  174. );
  175. // post to discord webhook
  176. message = {
  177. embeds: [
  178. {
  179. title: `Total ${quoteToken.symbol} markets ${existingOpenBookMarkets.size}`,
  180. color: 1127128,
  181. },
  182. {
  183. title: `Total ${quoteToken.symbol} pools ${existingLiquidityPools.size}`,
  184. color: 14177041,
  185. },
  186. ],
  187. };
  188. // post it to discord
  189. fetch(DISCORD_WEBHOOK, {
  190. method: 'POST',
  191. headers: {
  192. 'Content-Type': 'application/json',
  193. },
  194. body: JSON.stringify(message),
  195. });
  196. // check existing wallet for associated token account of quote mint
  197. const tokenAccounts = await getTokenAccounts(
  198. solanaConnection,
  199. wallet.publicKey,
  200. commitment,
  201. );
  202. for (const ta of tokenAccounts) {
  203. existingTokenAccounts.set(ta.accountInfo.mint.toString(), <
  204. MinimalTokenAccountData
  205. >{
  206. mint: ta.accountInfo.mint,
  207. address: ta.pubkey,
  208. });
  209. }
  210. const tokenAccount = tokenAccounts.find(
  211. (acc) => acc.accountInfo.mint.toString() === quoteToken.mint.toString(),
  212. )!;
  213. if (!tokenAccount) {
  214. throw new Error(
  215. `No ${quoteToken.symbol} token account found in wallet: ${wallet.publicKey}`,
  216. );
  217. }
  218. quoteTokenAssociatedAddress = tokenAccount.pubkey;
  219. // load tokens to snipe
  220. loadSnipeList();
  221. }
  222. export async function processRaydiumPool(updatedAccountInfo: KeyedAccountInfo) {
  223. let accountData: LiquidityStateV4 | undefined;
  224. try {
  225. accountData = LIQUIDITY_STATE_LAYOUT_V4.decode(
  226. updatedAccountInfo.accountInfo.data,
  227. );
  228. if (!shouldBuy(accountData.baseMint.toString())) {
  229. return;
  230. }
  231. await buy(updatedAccountInfo.accountId, accountData);
  232. // wait for 2 seconds before selling
  233. await new Promise((resolve) => setTimeout(resolve, 2000));
  234. await sell(updatedAccountInfo.accountId, accountData);
  235. } catch (e) {
  236. logger.error({ ...accountData, error: e }, `Failed to process pool`);
  237. }
  238. }
  239. export async function processOpenBookMarket(
  240. updatedAccountInfo: KeyedAccountInfo,
  241. ) {
  242. let accountData: MarketStateV3 | undefined;
  243. try {
  244. accountData = MARKET_STATE_LAYOUT_V3.decode(
  245. updatedAccountInfo.accountInfo.data,
  246. );
  247. // to be competitive, we collect market data before buying the token...
  248. if (existingTokenAccounts.has(accountData.baseMint.toString())) {
  249. return;
  250. }
  251. const ata = getAssociatedTokenAddressSync(
  252. accountData.baseMint,
  253. wallet.publicKey,
  254. );
  255. existingTokenAccounts.set(accountData.baseMint.toString(), <
  256. MinimalTokenAccountData
  257. >{
  258. address: ata,
  259. mint: accountData.baseMint,
  260. market: <MinimalMarketLayoutV3>{
  261. bids: accountData.bids,
  262. asks: accountData.asks,
  263. eventQueue: accountData.eventQueue,
  264. },
  265. });
  266. } catch (e) {
  267. logger.error({ ...accountData, error: e }, `Failed to process market`);
  268. }
  269. }
  270. async function buy(
  271. accountId: PublicKey,
  272. accountData: LiquidityStateV4,
  273. ): Promise<void> {
  274. const tokenAccount = existingTokenAccounts.get(
  275. accountData.baseMint.toString(),
  276. );
  277. if (!tokenAccount) {
  278. return;
  279. }
  280. tokenAccount.poolKeys = createPoolKeys(
  281. accountId,
  282. accountData,
  283. tokenAccount.market!,
  284. );
  285. const { innerTransaction, address } = Liquidity.makeSwapFixedInInstruction(
  286. {
  287. poolKeys: tokenAccount.poolKeys,
  288. userKeys: {
  289. tokenAccountIn: quoteTokenAssociatedAddress,
  290. tokenAccountOut: tokenAccount.address,
  291. owner: wallet.publicKey,
  292. },
  293. amountIn: quoteAmount.raw,
  294. minAmountOut: 0,
  295. },
  296. tokenAccount.poolKeys.version,
  297. );
  298. const latestBlockhash = await solanaConnection.getLatestBlockhash({
  299. commitment: commitment,
  300. });
  301. const messageV0 = new TransactionMessage({
  302. payerKey: wallet.publicKey,
  303. recentBlockhash: latestBlockhash.blockhash,
  304. instructions: [
  305. ComputeBudgetProgram.setComputeUnitLimit({ units: 400000 }),
  306. ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 30000 }),
  307. createAssociatedTokenAccountIdempotentInstruction(
  308. wallet.publicKey,
  309. tokenAccount.address,
  310. wallet.publicKey,
  311. accountData.baseMint,
  312. ),
  313. ...innerTransaction.instructions,
  314. ],
  315. }).compileToV0Message();
  316. const transaction = new VersionedTransaction(messageV0);
  317. transaction.sign([wallet, ...innerTransaction.signers]);
  318. const signature = await solanaConnection.sendRawTransaction(
  319. transaction.serialize(),
  320. {
  321. maxRetries: 20,
  322. preflightCommitment: commitment,
  323. },
  324. );
  325. logger.info(
  326. {
  327. mint: accountData.baseMint,
  328. url: `https://solscan.io/tx/${signature}?cluster=${network}`,
  329. },
  330. 'Buy',
  331. );
  332. // post to discord webhook
  333. const message = {
  334. embeds: [
  335. {
  336. title: `Bought token: ${accountData.baseMint.toBase58()}`,
  337. color: 1127128,
  338. url: `https://solscan.io/tx/${signature}?cluster=${network}`,
  339. },
  340. ],
  341. };
  342. const DISCORD_WEBHOOK = retrieveEnvVariable('DISCORD_WEBHOOK', logger);
  343. // use native fetch to post to discord
  344. fetch(DISCORD_WEBHOOK, {
  345. method: 'POST',
  346. headers: {
  347. 'Content-Type': 'application/json',
  348. },
  349. body: JSON.stringify(message),
  350. });
  351. }
  352. const maxRetries = 60;
  353. async function sell(
  354. accountId: PublicKey,
  355. accountData: LiquidityStateV4,
  356. ): Promise<void> {
  357. const tokenAccount = existingTokenAccounts.get(
  358. accountData.baseMint.toString(),
  359. );
  360. if (!tokenAccount) {
  361. return;
  362. }
  363. let retries = 0;
  364. let balanceFound = false;
  365. while (retries < maxRetries) {
  366. try {
  367. const balanceResponse = (await solanaConnection.getTokenAccountBalance(tokenAccount.address)).value.amount;
  368. if (balanceResponse !== null && Number(balanceResponse) > 0 && !balanceFound) {
  369. balanceFound = true;
  370. console.log("Token balance: ", balanceResponse);
  371. // send to discord
  372. const tokenBalanceMessage = {
  373. embeds: [
  374. {
  375. title: `Token balance: ${balanceResponse}`,
  376. color: 1127128,
  377. },
  378. ],
  379. };
  380. const DISCORD_WEBHOOK = retrieveEnvVariable('DISCORD_WEBHOOK', logger);
  381. // use native fetch to post to discord
  382. fetch(DISCORD_WEBHOOK, {
  383. method: 'POST',
  384. headers: {
  385. 'Content-Type': 'application/json',
  386. },
  387. body: JSON.stringify(tokenBalanceMessage),
  388. });
  389. tokenAccount.poolKeys = createPoolKeys(
  390. accountId,
  391. accountData,
  392. tokenAccount.market!,
  393. );
  394. const { innerTransaction, address } = Liquidity.makeSwapFixedInInstruction(
  395. {
  396. poolKeys: tokenAccount.poolKeys,
  397. userKeys: {
  398. tokenAccountIn: tokenAccount.address,
  399. tokenAccountOut: quoteTokenAssociatedAddress,
  400. owner: wallet.publicKey,
  401. },
  402. amountIn: new BN(balanceResponse),
  403. minAmountOut: 0,
  404. },
  405. tokenAccount.poolKeys.version,
  406. );
  407. const latestBlockhash = await solanaConnection.getLatestBlockhash({
  408. commitment: commitment,
  409. });
  410. const messageV0 = new TransactionMessage({
  411. payerKey: wallet.publicKey,
  412. recentBlockhash: latestBlockhash.blockhash,
  413. instructions: [
  414. ComputeBudgetProgram.setComputeUnitLimit({ units: 400000 }),
  415. ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 200000 }),
  416. createAssociatedTokenAccountIdempotentInstruction(
  417. wallet.publicKey,
  418. tokenAccount.address,
  419. wallet.publicKey,
  420. accountData.baseMint,
  421. ),
  422. ...innerTransaction.instructions,
  423. ],
  424. }).compileToV0Message();
  425. const transaction = new VersionedTransaction(messageV0);
  426. transaction.sign([wallet, ...innerTransaction.signers]);
  427. const signature = await solanaConnection.sendRawTransaction(
  428. transaction.serialize(),
  429. {
  430. maxRetries: 5,
  431. preflightCommitment: commitment,
  432. },
  433. );
  434. logger.info(
  435. {
  436. mint: accountData.baseMint,
  437. url: `https://solscan.io/tx/${signature}?cluster=${network}`,
  438. },
  439. 'sell',
  440. );
  441. // post to discord webhook
  442. const sellMessage = {
  443. embeds: [
  444. {
  445. title: `Sold token: ${accountData.baseMint.toBase58()}`,
  446. color: 1127128,
  447. url: `https://solscan.io/tx/${signature}?cluster=${network}`,
  448. },
  449. ],
  450. };
  451. // const DISCORD_WEBHOOK = retrieveEnvVariable('DISCORD_WEBHOOK', logger);
  452. // use native fetch to post to discord
  453. fetch(DISCORD_WEBHOOK, {
  454. method: 'POST',
  455. headers: {
  456. 'Content-Type': 'application/json',
  457. },
  458. body: JSON.stringify(sellMessage),
  459. });
  460. break;
  461. }
  462. } catch (error) {
  463. }
  464. retries++;
  465. await new Promise((resolve) => setTimeout(resolve, 1000));
  466. }
  467. }
  468. function loadSnipeList() {
  469. if (!USE_SNIPE_LIST) {
  470. return;
  471. }
  472. const count = snipeList.length;
  473. const data = fs.readFileSync(path.join(__dirname, 'snipe-list.txt'), 'utf-8');
  474. snipeList = data
  475. .split('\n')
  476. .map((a) => a.trim())
  477. .filter((a) => a);
  478. if (snipeList.length != count) {
  479. logger.info(`Loaded snipe list: ${snipeList.length}`);
  480. }
  481. }
  482. function shouldBuy(key: string): boolean {
  483. return USE_SNIPE_LIST ? snipeList.includes(key) : true;
  484. }
  485. const runListener = async () => {
  486. await init();
  487. const raydiumSubscriptionId = solanaConnection.onProgramAccountChange(
  488. RAYDIUM_LIQUIDITY_PROGRAM_ID_V4,
  489. async (updatedAccountInfo) => {
  490. const key = updatedAccountInfo.accountId.toString();
  491. const existing = existingLiquidityPools.has(key);
  492. if (!existing) {
  493. existingLiquidityPools.add(key);
  494. const _ = processRaydiumPool(updatedAccountInfo);
  495. }
  496. },
  497. commitment,
  498. [
  499. { dataSize: LIQUIDITY_STATE_LAYOUT_V4.span },
  500. {
  501. memcmp: {
  502. offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf('quoteMint'),
  503. bytes: quoteToken.mint.toBase58(),
  504. },
  505. },
  506. {
  507. memcmp: {
  508. offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf('marketProgramId'),
  509. bytes: OPENBOOK_PROGRAM_ID.toBase58(),
  510. },
  511. },
  512. {
  513. memcmp: {
  514. offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf('status'),
  515. bytes: bs58.encode([6, 0, 0, 0, 0, 0, 0, 0]),
  516. },
  517. },
  518. ],
  519. );
  520. const openBookSubscriptionId = solanaConnection.onProgramAccountChange(
  521. OPENBOOK_PROGRAM_ID,
  522. async (updatedAccountInfo) => {
  523. const key = updatedAccountInfo.accountId.toString();
  524. const existing = existingOpenBookMarkets.has(key);
  525. if (!existing) {
  526. existingOpenBookMarkets.add(key);
  527. const _ = processOpenBookMarket(updatedAccountInfo);
  528. }
  529. },
  530. commitment,
  531. [
  532. { dataSize: MARKET_STATE_LAYOUT_V2.span },
  533. {
  534. memcmp: {
  535. offset: MARKET_STATE_LAYOUT_V2.offsetOf('quoteMint'),
  536. bytes: quoteToken.mint.toBase58(),
  537. },
  538. },
  539. ],
  540. );
  541. logger.info(`Listening for raydium changes: ${raydiumSubscriptionId}`);
  542. logger.info(`Listening for open book changes: ${openBookSubscriptionId}`);
  543. // post to discord webhook
  544. const message = {
  545. embeds: [
  546. {
  547. title: `Listening for raydium changes: ${raydiumSubscriptionId}`,
  548. color: 1127128,
  549. },
  550. {
  551. title: `Listening for open book changes: ${openBookSubscriptionId}`,
  552. color: 14177041,
  553. },
  554. ],
  555. };
  556. const DISCORD_WEBHOOK = retrieveEnvVariable('DISCORD_WEBHOOK', logger);
  557. // use native fetch to post to discord
  558. fetch(DISCORD_WEBHOOK, {
  559. method: 'POST',
  560. headers: {
  561. 'Content-Type': 'application/json',
  562. },
  563. body: JSON.stringify(message),
  564. });
  565. if (USE_SNIPE_LIST) {
  566. setInterval(loadSnipeList, SNIPE_LIST_REFRESH_INTERVAL);
  567. }
  568. };
  569. runListener();