Skip to content

Commit

Permalink
chore: add Subcommand to cli_dmsg
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 11, 2024
1 parent 3856b26 commit e54e8ae
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions src/cli_dmsg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ path = "src/main.rs"

[dependencies]
candid = { workspace = true, features = ["value", "printer"] }
hex = { workspace = true }
serde = { workspace = true }
serde_bytes = { workspace = true }
sha2 = { workspace = true }
tokio = { workspace = true }
ic-agent = { workspace = true }
ic-certification = { workspace = true }
ciborium = { workspace = true }
num-traits = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true }
icrc-ledger-types = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
ic-icrc1 = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
ic-ledger-core = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
ic-icrc1-tokens-u64 = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
ic_message_types = { path = "../ic_message_types", version = "2" }
34 changes: 32 additions & 2 deletions src/cli_dmsg/src/dmsg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use candid::{Nat, Principal};
use ciborium::from_reader;
use ic_agent::Agent;
use ic_message_types::profile::UserInfo;
use ic_message_types::{profile::UserInfo, NameBlock};
use icrc_ledger_types::icrc1::{
account::Account,
transfer::{TransferArg, TransferError},
};
use icrc_ledger_types::icrc3::blocks::{GetBlocksRequest, GetBlocksResult, ICRC3GenericBlock};

use super::{
agent::{query_call, update_call},
Expand Down Expand Up @@ -38,7 +40,7 @@ impl DMsgAgent {
) -> Result<Nat, String> {
let output: Result<Nat, TransferError> = update_call(
&self.agent,
&token_canister,
token_canister,
"icrc1_transfer",
(TransferArg {
from_subaccount: None,
Expand All @@ -55,4 +57,32 @@ impl DMsgAgent {
.await?;
output.map_err(format_error)
}

pub async fn icrc3_get_blocks(
&self,
start: Nat,
length: Nat,
) -> Result<Vec<NameBlock>, String> {
let output: GetBlocksResult = query_call(
&self.agent,
&self.canister_id,
"icrc3_get_blocks",
(vec![GetBlocksRequest { start, length }],),
)
.await?;
let blks: Vec<NameBlock> = output
.blocks
.into_iter()
.flat_map(|b| match b.block {
ICRC3GenericBlock::Blob(blob) => {
let res: Result<NameBlock, String> =
from_reader(blob.as_slice()).map_err(format_error);
res
}
_ => panic!("unexpected block type"),
})
.collect();

Ok(blks)
}
}
54 changes: 52 additions & 2 deletions src/cli_dmsg/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use candid::{Nat, Principal};
use candid::{pretty::candid::value::pp_value, CandidType, IDLValue, Nat, Principal};
use chrono::prelude::*;
use ciborium::{from_reader, into_writer};
use clap::{Parser, Subcommand};
Expand All @@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
use sha2::Digest;
use std::{
collections::BTreeMap,
fmt::Write,
time::{SystemTime, UNIX_EPOCH},
};

Expand Down Expand Up @@ -51,6 +52,16 @@ pub enum Commands {
#[arg(long, short)]
usernames: Vec<String>,
},
Blocks {
#[arg(long, short)]
start: u64,

#[arg(long, short)]
length: u64,

#[arg(long, short)]
format: Option<String>,
},
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
Expand Down Expand Up @@ -111,7 +122,7 @@ async fn main() -> Result<(), String> {
let now = start_at.duration_since(UNIX_EPOCH).map_err(format_error)?;
let now = now.as_millis() as u64;
let _ = agent
.send_token_to(&TOKEN_CANISTER, user.id, amount_e8s.into())
.send_token_to(&TOKEN_CANISTER, user.id, amount_e8s)
.await?;
sr.0.insert(username.clone(), (user.id, amount_e8s, now));
let utc: DateTime<Utc> = Utc.timestamp_millis_opt(now as i64).unwrap();
Expand All @@ -132,6 +143,35 @@ async fn main() -> Result<(), String> {
println!("Total sent: {}", pretty_amount(send_total));
}

Some(Commands::Blocks {
start,
length,
format,
}) => {
let agent = DMsgAgent {
agent,
canister_id: MSG_CANISTER,
};
let blocks = agent
.icrc3_get_blocks((*start).into(), (*length).into())
.await?;

match format {
Some(_) => {
let mut ss = String::new();
for block in blocks {
write!(&mut ss, " -u {}", block.name).map_err(format_error)?;
}
println!("{}", ss);
}
None => {
for block in blocks {
println!("{:?}", block);
}
}
}
}

None => {}
}
Ok(())
Expand All @@ -155,6 +195,16 @@ where
format!("{:?}", err)
}

fn pretty_println<T>(data: &T) -> Result<(), String>
where
T: CandidType,
{
let val = IDLValue::try_from_candid_type(data).map_err(format_error)?;
let doc = pp_value(7, &val);
println!("{}", doc.pretty(120));
Ok(())
}

fn pretty_amount(amount: u64) -> String {
let amount = amount as f64 / 100_000_000.0;
if amount > 1000000.0 {
Expand Down
2 changes: 1 addition & 1 deletion src/ic_message_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde_bytes::ByteArray;
pub mod channel;
pub mod profile;

#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NameBlock {
#[serde(rename = "h")]
pub height: u64,
Expand Down

0 comments on commit e54e8ae

Please sign in to comment.