Last active
April 22, 2024 04:53
-
-
Save codewithgun/0f98e669117567f5986b03e0eda47c94 to your computer and use it in GitHub Desktop.
DLMM account subscribe and decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use anchor_client::anchor_lang::AccountDeserialize; | |
use anyhow::Result; | |
use base64::Engine; | |
use futures::{SinkExt, StreamExt}; | |
use lb_clmm::state::position::PositionV2; | |
use serde_json::{json, Value}; | |
use tokio_tungstenite::{connect_async, tungstenite::Message}; | |
#[tokio::main] | |
async fn main() -> Result<()> { | |
let rpc = env::var("rpc")?; | |
let (ws_stream, _) = connect_async(rpc).await?; | |
let (mut write, mut read) = ws_stream.split(); | |
let subscribe_message = json!({ | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "accountSubscribe", | |
"params": [ | |
"HV45N5eWmURSiGJ1p5iFUDui5gWHeF9yZn3Cmv567368", // Position pubkey | |
{ | |
"encoding": "jsonParsed" | |
} | |
] | |
}); | |
write | |
.send(Message::Text(subscribe_message.to_string())) | |
.await?; | |
if let Some(Ok(Message::Text(first_resp))) = read.next().await { | |
let first_resp: Value = serde_json::from_str(&first_resp)?; | |
if let Some(subscription_id) = first_resp["result"].as_u64() { | |
println!( | |
"Subscription successful with id {}, listening for events \n", | |
subscription_id | |
); | |
} | |
loop { | |
if let Some(Ok(Message::Text(message))) = read.next().await { | |
let updated_account_info: Value = serde_json::from_str(&message)?; | |
let Some(account_data) = updated_account_info | |
.get("params") | |
.and_then(|params| params.get("result")) | |
.and_then(|result| result.get("value")) | |
.and_then(|value| value.get("data")) | |
else { | |
continue; | |
}; | |
let Some(encoded_data) = account_data.get(0).and_then(|v| v.as_str()) else { | |
continue; | |
}; | |
let raw_data = base64::prelude::BASE64_STANDARD.decode(encoded_data)?; | |
let position_account = PositionV2::try_deserialize(&mut raw_data.as_ref())?; | |
println!( | |
"Total claimed fee x,y {},{}", | |
position_account.total_claimed_fee_x_amount, | |
position_account.total_claimed_fee_y_amount | |
); | |
} | |
} | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment