feat: complete rewrite

This commit is contained in:
Filip Dunder
2024-04-15 15:27:22 +02:00
parent b13f9864d2
commit 643002ae8a
36 changed files with 3898 additions and 705 deletions
+3
View File
@@ -0,0 +1,3 @@
export * from './market.cache';
export * from './pool.cache';
export * from './snipe-list.cache';
+58
View File
@@ -0,0 +1,58 @@
import { Connection, PublicKey } from '@solana/web3.js';
import { getMinimalMarketV3, logger, MINIMAL_MARKET_STATE_LAYOUT_V3, MinimalMarketLayoutV3 } from '../helpers';
import { MAINNET_PROGRAM_ID, MARKET_STATE_LAYOUT_V3, Token } from '@raydium-io/raydium-sdk';
export class MarketCache {
private readonly keys: Map<string, MinimalMarketLayoutV3> = new Map<string, MinimalMarketLayoutV3>();
constructor(private readonly connection: Connection) {}
async init(config: { quoteToken: Token }) {
logger.debug({}, `Fetching all existing ${config.quoteToken.symbol} markets...`);
const accounts = await this.connection.getProgramAccounts(MAINNET_PROGRAM_ID.OPENBOOK_MARKET, {
commitment: this.connection.commitment,
dataSlice: {
offset: MARKET_STATE_LAYOUT_V3.offsetOf('eventQueue'),
length: MINIMAL_MARKET_STATE_LAYOUT_V3.span,
},
filters: [
{ dataSize: MARKET_STATE_LAYOUT_V3.span },
{
memcmp: {
offset: MARKET_STATE_LAYOUT_V3.offsetOf('quoteMint'),
bytes: config.quoteToken.mint.toBase58(),
},
},
],
});
for (const account of accounts) {
const market = MINIMAL_MARKET_STATE_LAYOUT_V3.decode(account.account.data);
this.keys.set(account.pubkey.toString(), market);
}
logger.debug({}, `Cached ${this.keys.size} markets`);
}
public save(marketId: string, keys: MinimalMarketLayoutV3) {
if (!this.keys.has(marketId)) {
logger.trace({}, `Caching new market: ${marketId}`);
this.keys.set(marketId, keys);
}
}
public async get(marketId: string): Promise<MinimalMarketLayoutV3> {
if (this.keys.has(marketId)) {
return this.keys.get(marketId)!;
}
logger.trace({}, `Fetching new market keys for ${marketId}`);
const market = await this.fetch(marketId);
this.keys.set(marketId, market);
return market;
}
private fetch(marketId: string): Promise<MinimalMarketLayoutV3> {
return getMinimalMarketV3(this.connection, new PublicKey(marketId), this.connection.commitment);
}
}
+20
View File
@@ -0,0 +1,20 @@
import { LiquidityStateV4 } from '@raydium-io/raydium-sdk';
import { logger } from '../helpers';
export class PoolCache {
private readonly keys: Map<string, { id: string; state: LiquidityStateV4 }> = new Map<
string,
{ id: string; state: LiquidityStateV4 }
>();
public save(id: string, state: LiquidityStateV4) {
if (!this.keys.has(state.baseMint.toString())) {
logger.trace(`Caching new pool for mint: ${state.baseMint.toString()}`);
this.keys.set(state.baseMint.toString(), { id, state });
}
}
public async get(mint: string): Promise<{ id: string; state: LiquidityStateV4 }> {
return this.keys.get(mint)!;
}
}
+34
View File
@@ -0,0 +1,34 @@
import fs from 'fs';
import path from 'path';
import { logger, SNIPE_LIST_REFRESH_INTERVAL } from '../helpers';
export class SnipeListCache {
private snipeList: string[] = [];
constructor() {
setInterval(this.loadSnipeList, SNIPE_LIST_REFRESH_INTERVAL);
}
public init() {
this.loadSnipeList();
}
public isInList(mint: string) {
return this.snipeList.includes(mint);
}
private loadSnipeList() {
logger.trace('Refreshing snipe list...');
const count = this.snipeList.length;
const data = fs.readFileSync(path.join(__dirname, 'snipe-list.txt'), 'utf-8');
this.snipeList = data
.split('\n')
.map((a) => a.trim())
.filter((a) => a);
if (this.snipeList.length != count) {
logger.info(`Loaded snipe list: ${this.snipeList.length}`);
}
}
}