-
Notifications
You must be signed in to change notification settings - Fork 739
/
listeners.ts
112 lines (103 loc) · 3.22 KB
/
listeners.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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';
export class Listeners extends EventEmitter {
private subscriptions: number[] = [];
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);
}
const raydiumSubscription = await this.subscribeToRaydiumPools(config);
this.subscriptions.push(raydiumSubscription);
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 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(),
},
},
],
);
}
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);
}
}
}