mirror of
https://github.com/fdundjer/solana-sniper-bot.git
synced 2025-11-25 14:11:08 +10:00
tsc & prettier
This commit is contained in:
17
listeners/index.js
Normal file
17
listeners/index.js
Normal file
@ -0,0 +1,17 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__exportStar(require("./listeners"), exports);
|
||||
@ -1 +1 @@
|
||||
export * from './listeners';
|
||||
export * from "./listeners";
|
||||
|
||||
109
listeners/listeners.js
Normal file
109
listeners/listeners.js
Normal file
@ -0,0 +1,109 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Listeners = void 0;
|
||||
const raydium_sdk_1 = require("@raydium-io/raydium-sdk");
|
||||
const bs58_1 = __importDefault(require("bs58"));
|
||||
const spl_token_1 = require("@solana/spl-token");
|
||||
const events_1 = require("events");
|
||||
class Listeners extends events_1.EventEmitter {
|
||||
constructor(connection) {
|
||||
super();
|
||||
this.connection = connection;
|
||||
this.subscriptions = [];
|
||||
}
|
||||
start(config) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
if (config.cacheNewMarkets) {
|
||||
const openBookSubscription = yield this.subscribeToOpenBookMarkets(config);
|
||||
this.subscriptions.push(openBookSubscription);
|
||||
}
|
||||
const raydiumSubscription = yield this.subscribeToRaydiumPools(config);
|
||||
this.subscriptions.push(raydiumSubscription);
|
||||
if (config.autoSell) {
|
||||
const walletSubscription = yield this.subscribeToWalletChanges(config);
|
||||
this.subscriptions.push(walletSubscription);
|
||||
}
|
||||
});
|
||||
}
|
||||
subscribeToOpenBookMarkets(config) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return this.connection.onProgramAccountChange(raydium_sdk_1.MAINNET_PROGRAM_ID.OPENBOOK_MARKET, (updatedAccountInfo) => __awaiter(this, void 0, void 0, function* () {
|
||||
this.emit("market", updatedAccountInfo);
|
||||
}), this.connection.commitment, [
|
||||
{ dataSize: raydium_sdk_1.MARKET_STATE_LAYOUT_V3.span },
|
||||
{
|
||||
memcmp: {
|
||||
offset: raydium_sdk_1.MARKET_STATE_LAYOUT_V3.offsetOf("quoteMint"),
|
||||
bytes: config.quoteToken.mint.toBase58(),
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
}
|
||||
subscribeToRaydiumPools(config) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return this.connection.onProgramAccountChange(raydium_sdk_1.MAINNET_PROGRAM_ID.AmmV4, (updatedAccountInfo) => __awaiter(this, void 0, void 0, function* () {
|
||||
this.emit("pool", updatedAccountInfo);
|
||||
}), this.connection.commitment, [
|
||||
{ dataSize: raydium_sdk_1.LIQUIDITY_STATE_LAYOUT_V4.span },
|
||||
{
|
||||
memcmp: {
|
||||
offset: raydium_sdk_1.LIQUIDITY_STATE_LAYOUT_V4.offsetOf("quoteMint"),
|
||||
bytes: config.quoteToken.mint.toBase58(),
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: raydium_sdk_1.LIQUIDITY_STATE_LAYOUT_V4.offsetOf("marketProgramId"),
|
||||
bytes: raydium_sdk_1.MAINNET_PROGRAM_ID.OPENBOOK_MARKET.toBase58(),
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: raydium_sdk_1.LIQUIDITY_STATE_LAYOUT_V4.offsetOf("status"),
|
||||
bytes: bs58_1.default.encode([6, 0, 0, 0, 0, 0, 0, 0]),
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
}
|
||||
subscribeToWalletChanges(config) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return this.connection.onProgramAccountChange(spl_token_1.TOKEN_PROGRAM_ID, (updatedAccountInfo) => __awaiter(this, void 0, void 0, function* () {
|
||||
this.emit("wallet", updatedAccountInfo);
|
||||
}), this.connection.commitment, [
|
||||
{
|
||||
dataSize: 165,
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: 32,
|
||||
bytes: config.walletPublicKey.toBase58(),
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
}
|
||||
stop() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
for (let i = this.subscriptions.length; i >= 0; --i) {
|
||||
const subscription = this.subscriptions[i];
|
||||
yield this.connection.removeAccountChangeListener(subscription);
|
||||
this.subscriptions.splice(i, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.Listeners = Listeners;
|
||||
@ -1,112 +1,112 @@
|
||||
import { LIQUIDITY_STATE_LAYOUT_V4, MAINNET_PROGRAM_ID, MARKET_STATE_LAYOUT_V3, Token } from '@raydium-io/raydium-sdk';
|
||||
import bs58 from 'bs58';
|
||||
import { Connection, PublicKey } from '@solana/web3.js';
|
||||
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
||||
import { EventEmitter } from 'events';
|
||||
import { LIQUIDITY_STATE_LAYOUT_V4, MAINNET_PROGRAM_ID, MARKET_STATE_LAYOUT_V3, Token } from "@raydium-io/raydium-sdk";
|
||||
import bs58 from "bs58";
|
||||
import { Connection, PublicKey } from "@solana/web3.js";
|
||||
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
export class Listeners extends EventEmitter {
|
||||
private subscriptions: number[] = [];
|
||||
private subscriptions: number[] = [];
|
||||
|
||||
constructor(private readonly connection: Connection) {
|
||||
super();
|
||||
}
|
||||
constructor(private readonly connection: Connection) {
|
||||
super();
|
||||
}
|
||||
|
||||
public async start(config: {
|
||||
walletPublicKey: PublicKey;
|
||||
quoteToken: Token;
|
||||
autoSell: boolean;
|
||||
cacheNewMarkets: boolean;
|
||||
}) {
|
||||
if (config.cacheNewMarkets) {
|
||||
const openBookSubscription = await this.subscribeToOpenBookMarkets(config);
|
||||
this.subscriptions.push(openBookSubscription);
|
||||
}
|
||||
public async start(config: {
|
||||
walletPublicKey: PublicKey;
|
||||
quoteToken: Token;
|
||||
autoSell: boolean;
|
||||
cacheNewMarkets: boolean;
|
||||
}) {
|
||||
if (config.cacheNewMarkets) {
|
||||
const openBookSubscription = await this.subscribeToOpenBookMarkets(config);
|
||||
this.subscriptions.push(openBookSubscription);
|
||||
}
|
||||
|
||||
const raydiumSubscription = await this.subscribeToRaydiumPools(config);
|
||||
this.subscriptions.push(raydiumSubscription);
|
||||
const raydiumSubscription = await this.subscribeToRaydiumPools(config);
|
||||
this.subscriptions.push(raydiumSubscription);
|
||||
|
||||
if (config.autoSell) {
|
||||
const walletSubscription = await this.subscribeToWalletChanges(config);
|
||||
this.subscriptions.push(walletSubscription);
|
||||
}
|
||||
}
|
||||
if (config.autoSell) {
|
||||
const walletSubscription = await this.subscribeToWalletChanges(config);
|
||||
this.subscriptions.push(walletSubscription);
|
||||
}
|
||||
}
|
||||
|
||||
private async subscribeToOpenBookMarkets(config: { quoteToken: Token }) {
|
||||
return this.connection.onProgramAccountChange(
|
||||
MAINNET_PROGRAM_ID.OPENBOOK_MARKET,
|
||||
async (updatedAccountInfo) => {
|
||||
this.emit('market', updatedAccountInfo);
|
||||
},
|
||||
this.connection.commitment,
|
||||
[
|
||||
{ dataSize: MARKET_STATE_LAYOUT_V3.span },
|
||||
{
|
||||
memcmp: {
|
||||
offset: MARKET_STATE_LAYOUT_V3.offsetOf('quoteMint'),
|
||||
bytes: config.quoteToken.mint.toBase58(),
|
||||
},
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
private async subscribeToOpenBookMarkets(config: { quoteToken: Token }) {
|
||||
return this.connection.onProgramAccountChange(
|
||||
MAINNET_PROGRAM_ID.OPENBOOK_MARKET,
|
||||
async (updatedAccountInfo) => {
|
||||
this.emit("market", updatedAccountInfo);
|
||||
},
|
||||
this.connection.commitment,
|
||||
[
|
||||
{ dataSize: MARKET_STATE_LAYOUT_V3.span },
|
||||
{
|
||||
memcmp: {
|
||||
offset: MARKET_STATE_LAYOUT_V3.offsetOf("quoteMint"),
|
||||
bytes: config.quoteToken.mint.toBase58(),
|
||||
},
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private async subscribeToRaydiumPools(config: { quoteToken: Token }) {
|
||||
return this.connection.onProgramAccountChange(
|
||||
MAINNET_PROGRAM_ID.AmmV4,
|
||||
async (updatedAccountInfo) => {
|
||||
this.emit('pool', updatedAccountInfo);
|
||||
},
|
||||
this.connection.commitment,
|
||||
[
|
||||
{ dataSize: LIQUIDITY_STATE_LAYOUT_V4.span },
|
||||
{
|
||||
memcmp: {
|
||||
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf('quoteMint'),
|
||||
bytes: config.quoteToken.mint.toBase58(),
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf('marketProgramId'),
|
||||
bytes: MAINNET_PROGRAM_ID.OPENBOOK_MARKET.toBase58(),
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf('status'),
|
||||
bytes: bs58.encode([6, 0, 0, 0, 0, 0, 0, 0]),
|
||||
},
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
private async subscribeToRaydiumPools(config: { quoteToken: Token }) {
|
||||
return this.connection.onProgramAccountChange(
|
||||
MAINNET_PROGRAM_ID.AmmV4,
|
||||
async (updatedAccountInfo) => {
|
||||
this.emit("pool", updatedAccountInfo);
|
||||
},
|
||||
this.connection.commitment,
|
||||
[
|
||||
{ dataSize: LIQUIDITY_STATE_LAYOUT_V4.span },
|
||||
{
|
||||
memcmp: {
|
||||
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf("quoteMint"),
|
||||
bytes: config.quoteToken.mint.toBase58(),
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf("marketProgramId"),
|
||||
bytes: MAINNET_PROGRAM_ID.OPENBOOK_MARKET.toBase58(),
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf("status"),
|
||||
bytes: bs58.encode([6, 0, 0, 0, 0, 0, 0, 0]),
|
||||
},
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private async subscribeToWalletChanges(config: { walletPublicKey: PublicKey }) {
|
||||
return this.connection.onProgramAccountChange(
|
||||
TOKEN_PROGRAM_ID,
|
||||
async (updatedAccountInfo) => {
|
||||
this.emit('wallet', updatedAccountInfo);
|
||||
},
|
||||
this.connection.commitment,
|
||||
[
|
||||
{
|
||||
dataSize: 165,
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: 32,
|
||||
bytes: config.walletPublicKey.toBase58(),
|
||||
},
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
private async subscribeToWalletChanges(config: { walletPublicKey: PublicKey }) {
|
||||
return this.connection.onProgramAccountChange(
|
||||
TOKEN_PROGRAM_ID,
|
||||
async (updatedAccountInfo) => {
|
||||
this.emit("wallet", updatedAccountInfo);
|
||||
},
|
||||
this.connection.commitment,
|
||||
[
|
||||
{
|
||||
dataSize: 165,
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
offset: 32,
|
||||
bytes: config.walletPublicKey.toBase58(),
|
||||
},
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
for (let i = this.subscriptions.length; i >= 0; --i) {
|
||||
const subscription = this.subscriptions[i];
|
||||
await this.connection.removeAccountChangeListener(subscription);
|
||||
this.subscriptions.splice(i, 1);
|
||||
}
|
||||
}
|
||||
public async stop() {
|
||||
for (let i = this.subscriptions.length; i >= 0; --i) {
|
||||
const subscription = this.subscriptions[i];
|
||||
await this.connection.removeAccountChangeListener(subscription);
|
||||
this.subscriptions.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user