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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::constants;

use log::info;
use serde::Serialize;
use solana_account_decoder::UiAccountEncoding;
use solana_client::{
    pubsub_client::{LogsSubscription, PubsubClient},
    rpc_config::{
        RpcAccountInfoConfig, RpcBlockSubscribeConfig,
        RpcBlockSubscribeFilter, RpcProgramAccountsConfig,
        RpcTransactionLogsConfig, RpcTransactionLogsFilter,
    },
};
use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey};
use std::{str::FromStr, time::Duration};

pub struct Listener {
    ws_url: String,
}

#[derive(Debug, Serialize, Default)]
pub struct Swap {
    pub signature: String,

    pub quote_amount: f64,
    pub quote_mint: String,

    pub base_amount: f64,
    pub base_mint: String,

    pub sol_amount_ui: f64,
}

pub trait BlockAndProgramSubscribable {
    fn block_subscribe(&self) -> Result<(), Box<dyn std::error::Error>>;
    fn program_subscribe(&self) -> Result<(), Box<dyn std::error::Error>>;
    fn slot_subscribe(&self) -> Result<(), Box<dyn std::error::Error>>;
}

impl Listener {
    pub fn new(ws_url: String) -> Listener {
        Listener { ws_url }
    }

    pub fn account_subscribe(
        &self,
        pubkey: &Pubkey,
    ) -> Result<LogsSubscription, Box<dyn std::error::Error>> {
        let (subs, receiver) = PubsubClient::logs_subscribe(
            self.ws_url.as_str(),
            RpcTransactionLogsFilter::Mentions(vec![pubkey.to_string()]),
            RpcTransactionLogsConfig {
                commitment: Some(CommitmentConfig::processed()),
            },
        )?;
        Ok((subs, receiver))
    }

    pub fn pool_subscribe(
        &self,
        amm_pool: &Pubkey,
    ) -> Result<LogsSubscription, Box<dyn std::error::Error>> {
        let (subs, receiver) = PubsubClient::logs_subscribe(
            self.ws_url.as_str(),
            RpcTransactionLogsFilter::Mentions(vec![amm_pool.to_string()]),
            RpcTransactionLogsConfig {
                commitment: Some(CommitmentConfig::processed()),
            },
        )?;
        Ok((subs, receiver))
    }

    pub fn logs_subscribe(
        &self,
    ) -> Result<LogsSubscription, Box<dyn std::error::Error>> {
        let raydium_pubkey =
            Pubkey::from_str(constants::RAYDIUM_LIQUIDITY_POOL_V4_PUBKEY)?;
        let config = RpcTransactionLogsConfig {
            commitment: Some(CommitmentConfig::confirmed()),
        };
        let filter = RpcTransactionLogsFilter::Mentions(vec![
            raydium_pubkey.to_string()
        ]);
        let (subs, receiver) = PubsubClient::logs_subscribe(
            self.ws_url.as_str(),
            filter,
            config,
        )?;

        info!("listening to logs for {:?}", raydium_pubkey);
        Ok((subs, receiver))
    }
}

impl BlockAndProgramSubscribable for Listener {
    fn slot_subscribe(&self) -> Result<(), Box<dyn std::error::Error>> {
        let (mut subs, receiver) =
            PubsubClient::slot_subscribe(self.ws_url.as_str())?;
        info!("listening to slots over {}", self.ws_url);

        if let Ok(slot) = receiver.recv() {
            let mut ts = tokio::time::Instant::now();
            info!("starting slot: {:?}", slot);
            while let Ok(slot) = receiver.recv() {
                info!(
                    "slot: {:?} in {}ms",
                    slot.slot,
                    ts.elapsed().as_millis()
                );
                ts = tokio::time::Instant::now();
            }
        }

        subs.shutdown().unwrap();

        Ok(())
    }
    fn block_subscribe(&self) -> Result<(), Box<dyn std::error::Error>> {
        let raydium_pubkey =
            Pubkey::from_str(constants::RAYDIUM_LIQUIDITY_POOL_V4_PUBKEY)?;

        let filter = RpcBlockSubscribeFilter::MentionsAccountOrProgram(
            raydium_pubkey.to_string(),
        );
        let config = RpcBlockSubscribeConfig::default();

        let (mut subs, receiver) = PubsubClient::block_subscribe(
            self.ws_url.as_str(),
            filter,
            Some(config),
        )?;

        info!("Filtering for mentions of {:?}", raydium_pubkey);

        while let Ok(block) = receiver.recv_timeout(Duration::from_secs(1)) {
            info!("Received block: {:?}", block);
        }

        subs.shutdown().unwrap();

        Ok(())
    }

    fn program_subscribe(&self) -> Result<(), Box<dyn std::error::Error>> {
        let raydium_pubkey =
            Pubkey::from_str(constants::RAYDIUM_LIQUIDITY_POOL_V4_PUBKEY)?;
        let config = RpcProgramAccountsConfig {
            account_config: RpcAccountInfoConfig {
                encoding: Some(UiAccountEncoding::JsonParsed),
                data_slice: None,
                commitment: Some(CommitmentConfig::processed()),
                min_context_slot: None,
            },
            ..RpcProgramAccountsConfig::default()
        };
        let (mut subs, receiver) = PubsubClient::program_subscribe(
            self.ws_url.as_str(),
            &raydium_pubkey,
            Some(config),
        )?;

        info!("listening on program {:?}", raydium_pubkey);

        let mut i = 0;
        while let Ok(account) = receiver.recv_timeout(Duration::from_secs(1)) {
            i += 1;
            info!("Received account: {:?}", account);
            if i == 1 {
                break;
            }
        }
        subs.shutdown().unwrap();

        Ok(())
    }
}