feat: use token amount instead of BN

This commit is contained in:
Filip Dunder
2024-03-24 19:01:54 +01:00
parent 1a02604cc5
commit c53a5062dd
3 changed files with 29 additions and 22 deletions

View File

@ -23,6 +23,8 @@ To run the script you need to:
- USE_SNIPE_LIST (buy only tokens listed in snipe-list.txt)
- SNIPE_LIST_REFRESH_INTERVAL (how often snipe list should be refreshed in milliseconds)
- CHECK_IF_MINT_IS_RENOUNCED (script will buy only if mint is renounced)
- MIN_POOL_SIZE (script will buy only if pool size is greater than specified amount)
- set to 0 to disable pool size check
- Install dependencies by typing: `npm install`
- Run the script by typing: `npm run buy` in terminal

45
buy.ts
View File

@ -30,7 +30,6 @@ import { logger } from './utils';
import { getMinimalMarketV3, MinimalMarketLayoutV3 } from './market';
import { MintLayout } from './types';
import bs58 from 'bs58';
import BN from 'bn.js';
import * as fs from 'fs';
import * as path from 'path';
import {
@ -60,7 +59,7 @@ export interface MinimalTokenAccountData {
address: PublicKey;
poolKeys?: LiquidityPoolKeys;
market?: MinimalMarketLayoutV3;
};
}
const existingLiquidityPools: Set<string> = new Set<string>();
const existingOpenBookMarkets: Set<string> = new Set<string>();
@ -70,6 +69,7 @@ let wallet: Keypair;
let quoteToken: Token;
let quoteTokenAssociatedAddress: PublicKey;
let quoteAmount: TokenAmount;
let quoteMinPoolSizeAmount: TokenAmount;
let snipeList: string[] = [];
@ -85,6 +85,7 @@ async function init(): Promise<void> {
case 'WSOL': {
quoteToken = Token.WSOL;
quoteAmount = new TokenAmount(Token.WSOL, QUOTE_AMOUNT, false);
quoteMinPoolSizeAmount = new TokenAmount(quoteToken, MIN_POOL_SIZE, false);
break;
}
case 'USDC': {
@ -96,6 +97,7 @@ async function init(): Promise<void> {
'USDC',
);
quoteAmount = new TokenAmount(quoteToken, QUOTE_AMOUNT, false);
quoteMinPoolSizeAmount = new TokenAmount(quoteToken, MIN_POOL_SIZE, false);
break;
}
default: {
@ -103,9 +105,14 @@ async function init(): Promise<void> {
}
}
logger.info(`Snipe list: ${USE_SNIPE_LIST}`);
logger.info(`Check mint renounced: ${CHECK_IF_MINT_IS_RENOUNCED}`);
logger.info(
`Script will buy all new tokens using ${QUOTE_MINT}. Amount that will be used to buy each token is: ${quoteAmount.toFixed().toString()}`,
`Min pool size: ${quoteMinPoolSizeAmount.isZero() ? 'false' : quoteMinPoolSizeAmount.toFixed()} ${quoteToken.symbol}`,
);
logger.info(`Buy amount: ${quoteAmount.toFixed()} ${quoteToken.symbol}`);
logger.info(`Auto sell: ${AUTO_SELL}`);
logger.info(`Sell delay: ${AUTO_SELL_DELAY === 0 ? 'false' : AUTO_SELL_DELAY}`);
// check existing wallet for associated token account of quote mint
const tokenAccounts = await getTokenAccounts(solanaConnection, wallet.publicKey, COMMITMENT_LEVEL);
@ -149,25 +156,21 @@ export async function processRaydiumPool(id: PublicKey, poolState: LiquidityStat
return;
}
let poolSize = new BN(poolState.swapQuoteInAmount);
if (!quoteMinPoolSizeAmount.isZero()) {
const poolSize = new TokenAmount(quoteToken, poolState.swapQuoteInAmount, true);
logger.info(`Processing pool: ${id.toString()} with ${poolSize.toFixed()} ${quoteToken.symbol} in liquidity`);
poolSize = poolSize.div(new BN(10 ** quoteToken.decimals));
logger.info(
`Processing pool: ${id.toString()} with ${poolSize.toString()} ${quoteToken.symbol} in liquidity`,
);
if (poolSize.lt(new BN(MIN_POOL_SIZE))) {
logger.warn(
{
mint: poolState.baseMint,
pooled: poolSize.toString() + ' ' + quoteToken.symbol,
},
`Skipping pool, smaller than ${MIN_POOL_SIZE} ${quoteToken.symbol}`,
`Swap quote in amount: ${poolSize.toString()}`
);
return;
if (poolSize.lt(quoteMinPoolSizeAmount)) {
logger.warn(
{
mint: poolState.baseMint,
pooled: `${poolSize.toFixed()} ${quoteToken.symbol}`,
},
`Skipping pool, smaller than ${quoteMinPoolSizeAmount.toFixed()} ${quoteToken.symbol}`,
`Swap quote in amount: ${poolSize.toFixed()}`,
);
return;
}
}
if (CHECK_IF_MINT_IS_RENOUNCED) {

View File

@ -14,4 +14,6 @@ export const MAX_SELL_RETRIES = Number(retrieveEnvVariable('MAX_SELL_RETRIES', l
export const AUTO_SELL_DELAY = Number(retrieveEnvVariable('AUTO_SELL_DELAY', logger));
export const PRIVATE_KEY = retrieveEnvVariable('PRIVATE_KEY', logger);
export const QUOTE_MINT = retrieveEnvVariable('QUOTE_MINT', logger);
export const QUOTE_AMOUNT = retrieveEnvVariable('QUOTE_AMOUNT', logger);
export const QUOTE_AMOUNT = retrieveEnvVariable('QUOTE_AMOUNT', logger);
export const MIN_POOL_SIZE = retrieveEnvVariable('MIN_POOL_SIZE', logger);