mirror of
https://github.com/fdundjer/solana-sniper-bot.git
synced 2025-11-09 20:12:06 +10:00
Add logic to use env variables
This commit is contained in:
22
buy.ts
22
buy.ts
@ -21,13 +21,11 @@ import {
|
||||
RAYDIUM_LIQUIDITY_PROGRAM_ID_V4,
|
||||
OPENBOOK_PROGRAM_ID,
|
||||
} from './liquidity';
|
||||
import { retry } from './utils';
|
||||
import { retry, retrieveEnvVariable } from './utils';
|
||||
import { USDC_AMOUNT, USDC_TOKEN_ID } from './common';
|
||||
import { getAllMarketsV3 } from './market';
|
||||
import pino from 'pino';
|
||||
import dotenv from 'dotenv';
|
||||
import bs58 from "bs58";
|
||||
dotenv.config();
|
||||
|
||||
const transport = pino.transport({
|
||||
targets: [
|
||||
@ -60,12 +58,16 @@ export const logger = pino(
|
||||
);
|
||||
|
||||
const network = 'mainnet-beta';
|
||||
const RPC_ENDPOINT = retrieveEnvVariable('RPC_ENDPOINT', logger);
|
||||
const RPC_WEBSOCKET_ENDPOINT = retrieveEnvVariable('RPC_WEBSOCKET_ENDPOINT', logger);
|
||||
|
||||
if (!RPC_ENDPOINT || !RPC_WEBSOCKET_ENDPOINT) {
|
||||
logger.error('RPC_ENDPOINT / RPC_WEBSOCKET_ENDPOINT is not set');
|
||||
process.exit(1);
|
||||
}
|
||||
const solanaConnection = new Connection(
|
||||
'ENTER RPC ENDPOINT HERE',
|
||||
{
|
||||
wsEndpoint:
|
||||
'ENTER RPC WEBSOCKET ENDPOINT HERE',
|
||||
},
|
||||
RPC_ENDPOINT,
|
||||
{wsEndpoint: RPC_WEBSOCKET_ENDPOINT},
|
||||
);
|
||||
|
||||
export type MinimalTokenAccountData = {
|
||||
@ -82,11 +84,11 @@ let existingTokenAccounts: Map<string, MinimalTokenAccountData> = new Map<
|
||||
|
||||
let wallet: Keypair;
|
||||
let usdcTokenKey: PublicKey;
|
||||
const PRIVATE_KEY: string = process.env.PRIVATE_KEY || '';
|
||||
const PRIVATE_KEY = retrieveEnvVariable('PRIVATE_KEY', logger);
|
||||
|
||||
async function init(): Promise<void> {
|
||||
if (!PRIVATE_KEY) {
|
||||
console.error('PRIVATE_KEY is not set');
|
||||
logger.error('PRIVATE_KEY is not set');
|
||||
process.exit(1);
|
||||
}
|
||||
wallet = Keypair.fromSecretKey(bs58.decode(PRIVATE_KEY));
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
import { Logger } from "pino";
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
/**
|
||||
* Runs the function `fn`
|
||||
* and retries automatically if it fails.
|
||||
@ -23,3 +27,12 @@ export const retry = async <T>(
|
||||
};
|
||||
|
||||
export const sleep = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
export const retrieveEnvVariable = (variableName: string, logger: Logger) => {
|
||||
const variable = process.env[variableName] || '';
|
||||
if (!variable) {
|
||||
logger.error(`${variableName} is not set`);
|
||||
process.exit(1);
|
||||
}
|
||||
return variable;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user