tsc & prettier

This commit is contained in:
Filip Dunder
2024-04-30 10:52:42 -03:00
parent 04e5ca7d27
commit 77c9853562
55 changed files with 2842 additions and 790 deletions

94
cache/market.cache.ts vendored
View File

@ -1,58 +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';
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) {}
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...`);
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(),
},
},
],
});
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);
}
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`);
}
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 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)!;
}
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;
}
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);
}
private fetch(marketId: string): Promise<MinimalMarketLayoutV3> {
return getMinimalMarketV3(this.connection, new PublicKey(marketId), this.connection.commitment);
}
}