Skip to content

Commit

Permalink
Read Native Staking Withdrawal Data
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay111meher committed Dec 16, 2024
1 parent 3dfff0b commit 2bbfab6
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
7 changes: 7 additions & 0 deletions kalypso-cli/src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,13 @@
"chain_id",
"proof_marketplace"
]
},
{
"name": "Read Native Staking Pending Withdrawals",
"description": "Reads Native Staking pending withdrawals",
"required_prompts":[
"indexer_url", "operator_address"
]
}
]
}
3 changes: 3 additions & 0 deletions kalypso-cli/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ pub fn get_operation(name: &str) -> Option<Box<dyn Operation>> {
Some(Box::new(set_operator_commision::SetOperatorCommision))
}
"Read Rewards Info" => Some(Box::new(claim_rewards::ReadRewardsInfo)),
"Read Native Staking Pending Withdrawals" => {
Some(Box::new(stake::ReadWithdrawalRequestIds))
}
_ => unimplemented!(),
}
}
109 changes: 107 additions & 2 deletions kalypso-cli/src/operations/stake.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,69 @@
use std::collections::HashMap;
use std::{collections::HashMap, sync::Once};

use async_trait::async_trait;
use ethers::signers::Signer;
use ethers::{signers::Signer, types::Address};
use serde::{Deserialize, Serialize};

use crate::common_deps::CommonDeps;

use super::Operation;

pub struct RequestNativeStakeWithdrawal;

#[async_trait]
impl Operation for RequestNativeStakeWithdrawal {
async fn execute(&self, config: HashMap<String, String>) -> Result<(), String> {
unimplemented!()
}
}

pub struct ReadWithdrawalRequestIds;

#[async_trait]
impl Operation for ReadWithdrawalRequestIds {
async fn execute(&self, config: HashMap<String, String>) -> Result<(), String> {
let read_stake_data_info = CommonDeps::read_stake_data_info(&config)?;
let withdrawal_requests = read_staking_data(
&read_stake_data_info.operator_address,
read_stake_data_info.indexer_url,
)
.await
.map_err(|e| format!("Unable to read stake data from indexer: {}", e))?;

static INIT: Once = Once::new();
INIT.call_once(|| {
println!(
"\n\nWithdrawal Requests for Operator {:?}\n\n",
read_stake_data_info.operator_address
);
println!(
"{:<42} | {:<42} | {:>42} | {:<5}",
"Account", "Amount", "Token", "Index"
);
println!("{:-<42}-+-{:-<42}-+-{:-<42}-+-{:-<5}", "", "", "", "");
});

for withdrawal in withdrawal_requests {
println!(
"{:<42} | {:<42} | {:>42} | {:<5}",
withdrawal.account, withdrawal.amount, withdrawal.token, withdrawal.index
);
}

println!();
Ok(())
}
}

pub struct ProcessWithdrawalRequests;

#[async_trait]
impl Operation for ProcessWithdrawalRequests {
async fn execute(&self, config: HashMap<String, String>) -> Result<(), String> {
unimplemented!()
}
}

pub struct NativeStaking;

#[async_trait]
Expand Down Expand Up @@ -73,3 +130,51 @@ impl Operation for NativeStaking {
Ok(())
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WithdrawRequest {
account: String,
token: String,
amount: String,
index: String,
}

// Define the structure of the entire API response
#[derive(Deserialize, Debug)]
struct ApiResponse {
withdrawal_requests: Vec<WithdrawRequest>,
// Include other fields if necessary
}

use reqwest::header::{HeaderMap, CONTENT_TYPE};
use std::error::Error;

async fn read_staking_data(
operator_address: &Address,
indexer_url: String,
) -> Result<Vec<WithdrawRequest>, Box<dyn Error>> {
// Initialize the HTTP client with a timeout (optional but recommended)
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()?;

// Construct the full URL
let full_url = format!("{}/ui/generator/{:?}", indexer_url, operator_address);

// Prepare the headers
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, "application/json".parse()?);

// Make the GET request
let response = client.get(&full_url).headers(headers).send().await?;

// Check if the response status is success
if !response.status().is_success() {
return Err(format!("Request failed with status: {}", response.status()).into());
}

// Parse the JSON response
let api_response: ApiResponse = response.json().await?;

Ok(api_response.withdrawal_requests)
}

0 comments on commit 2bbfab6

Please sign in to comment.