Skip to content

Commit

Permalink
Merge pull request #3 from PoCInnovation/wallet
Browse files Browse the repository at this point in the history
[ADD] wallet commands
  • Loading branch information
alexandreTimal authored Feb 1, 2025
2 parents 4bce8c7 + 228ac1f commit 1278023
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 30 deletions.
14 changes: 11 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use clap::{Parser, Subcommand, ValueHint};

use crate::wallet::WalletSubcommands;
use crate::wallet::WalletsSubcommands;
use crate::infos::InfosSubcommands;
use crate::events::EventsSubcommands;

#[derive(Parser)]
#[command(version)]
Expand All @@ -18,13 +19,20 @@ pub struct Kermit {
pub enum KermitSubcommand {
/// Wallet management utilities.
#[command(visible_alias = "w")]
Wallet {
Wallets {
#[command(subcommand)]
command: WalletSubcommands,
command: WalletsSubcommands,
},
/// Infos about node and hashrate.
#[command(visible_alias = "i")]
Infos {
#[command(subcommand)]
command: InfosSubcommands,
},
/// Event for contract, block and hash.
#[command(visible_alias = "e")]
Events {
#[command(subcommand)]
command: EventsSubcommands,
}
}
76 changes: 76 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::utils::get;
use anyhow::Result;
use clap::Parser;
use serde_json::Value;

/// CLI arguments for `kermit events`.
#[derive(Parser)]
pub enum EventsSubcommands {
/// Get events for a contract within a counter range.
#[command(visible_alias = "ec")]
ContractEvents {
contract_address: String,
start: i32,
limit: Option<i32>,
group: Option<i32>,
},

/// Get the current value of the events counter for a contract.
#[command(visible_alias = "cc")]
ContractCurrentCount { contract_address: String },

/// Get contract events for a transaction.
#[command(visible_alias = "etc")]
TxContractEvents { tx_id: String, group: Option<i32> },

/// Get contract events for a block.
#[command(visible_alias = "ebh")]
BlockContractEvents {
block_hash: String,
group: Option<i32>,
},
}

impl EventsSubcommands {
pub async fn run(self, url: &str) -> Result<()> {
let value: Value = match self {
Self::ContractEvents {
contract_address,
start,
limit,
group,
} => {
let mut endpoint = format!("/events/contract/{}?start={}", contract_address, start);
if let Some(limit) = limit {
endpoint.push_str(&format!("&limit={}", limit));
}
if let Some(group) = group {
endpoint.push_str(&format!("&limit={}", group));
}
get(url, &endpoint).await?
},
Self::ContractCurrentCount { contract_address } => {
let endpoint = format!("/events/contract/{}/current-count", contract_address);
get(url, &endpoint).await?
},
Self::TxContractEvents { tx_id, group } => {
let mut endpoint = format!("/events/tx-id/{}", tx_id);
if let Some(group) = group {
endpoint.push_str(&format!("&limit={}", group));
}
get(url, &endpoint).await?
},
Self::BlockContractEvents { block_hash, group } => {
let mut endpoint = format!("/events/block-hash/{}", block_hash);
if let Some(group) = group {
endpoint.push_str(&format!("&limit={}", group));
}
get(url, &endpoint).await?
},
};

serde_json::to_writer_pretty(std::io::stdout(), &value)?;

Ok(())
}
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod args;
mod utils;
mod wallet;
mod infos;
mod events;

use anyhow::Result;
use args::{Kermit, KermitSubcommand};
Expand All @@ -19,8 +20,9 @@ async fn run() -> Result<()> {
let kermit = Kermit::parse();

match kermit.cmd {
KermitSubcommand::Wallet { command } => command.run().await?,
KermitSubcommand::Wallets { command } => command.run(&kermit.url).await?,
KermitSubcommand::Infos { command } => command.run(&kermit.url).await?,
KermitSubcommand::Events { command } => command.run(&kermit.url).await?,
}

Ok(())
Expand Down
6 changes: 2 additions & 4 deletions src/utils/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn put<T: DeserializeOwned, U: Serialize>(
}

/// Perform a DELETE request to the given URL
pub async fn delete<T: DeserializeOwned>(url: &str, endpoint: &str) -> Result<T> {
pub async fn delete(url: &str, endpoint: &str) -> Result<()> {
let client = Client::new();

let url = format!("{}{}", url, endpoint);
Expand All @@ -100,7 +100,5 @@ pub async fn delete<T: DeserializeOwned>(url: &str, endpoint: &str) -> Result<T>
bail!(err.detail);
}

let data = res.json().await?;

Ok(data)
Ok(())
}
Loading

0 comments on commit 1278023

Please sign in to comment.