Skip to content

Instantly share code, notes, and snippets.

@codewithgun
Last active April 22, 2024 04:53
Show Gist options
  • Save codewithgun/0f98e669117567f5986b03e0eda47c94 to your computer and use it in GitHub Desktop.
Save codewithgun/0f98e669117567f5986b03e0eda47c94 to your computer and use it in GitHub Desktop.

Revisions

  1. codewithgun renamed this gist Apr 22, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. codewithgun created this gist Apr 22, 2024.
    71 changes: 71 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    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(())
    }