Skip to content

Commit 3856b26

Browse files
committed
feature: add cli_dmsg
1 parent 7c39957 commit 3856b26

File tree

10 files changed

+387
-3
lines changed

10 files changed

+387
-3
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
"src/cli_airdrop",
44
"src/cli_cryptogram",
5+
"src/cli_dmsg",
56
"src/ic_message",
67
"src/ic_message_channel",
78
"src/ic_message_profile",
@@ -55,3 +56,5 @@ ic-certification = "2.6"
5556
ic-oss-types = "0.9"
5657
getrandom = { version = "0.2", features = ["custom"] }
5758
rand = { version = "0.8", features = ["getrandom"] }
59+
chrono = { version = "0.4", features = ["serde"] }
60+
clap = { version = "=4.5", features = ["derive"] }

proposals/proposal-219.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
# Load the environment variables
4+
source "$(pwd)"/proposals/env.sh
5+
6+
# build and get batch_id, evidence:
7+
# dfx deploy ic_message_frontend --ic --by-proposal
8+
9+
export BLOB="$(didc encode --format blob '(record {batch_id=29:nat; evidence=blob "\dd\f7\32\7f\0a\6a\82\dc\5e\56\8c\f0\57\eb\39\a3\97\f9\66\45\69\1e\64\73\41\3f\45\57\68\7d\b2\c7"})')"
10+
11+
quill sns make-proposal --canister-ids-file ./sns_canister_ids.json --pem-file $PROPOSAL_PEM_FILE $PROPOSAL_NEURON_ID --proposal "(
12+
record {
13+
title = \"Execute commit_proposed_batch() to revert ic_message_frontend from v2.6.7 to v2.6.6\";
14+
url = \"https://dMsg.net/\";
15+
summary = \"This proposal executes commit_proposed_batch() on 2fvu6-tqaaa-aaaap-akksa-cai to release ic_message_frontend v2.6.6.\n\n1. chore: update dependencies for Svelte5.\";
16+
action = opt variant {
17+
ExecuteGenericNervousSystemFunction = record {
18+
function_id = 1100 : nat64;
19+
payload = ${BLOB};
20+
}
21+
};
22+
}
23+
)" > proposal-message.json
24+
25+
# quill send proposal-message.json

src/cli_airdrop/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sha2 = { workspace = true }
1616
tokio = { workspace = true }
1717
ic-agent = { workspace = true }
1818
ic-certification = { workspace = true }
19-
clap = { version = "=4.5", features = ["derive"] }
19+
clap = { workspace = true }
2020
ciborium = { workspace = true }
2121
num-traits = { workspace = true }
2222
icrc-ledger-types = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }

src/cli_cryptogram/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ lib_panda = { path = "../lib_panda", version = "0.2" }
1212
candid = { workspace = true }
1313
base64 = { workspace = true }
1414
serde = { workspace = true }
15-
clap = { version = "=4.5", features = ["derive"] }
15+
clap = { workspace = true }

src/cli_dmsg/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "cli_dmsg"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[[bin]]
7+
name = "cli_dmsg"
8+
path = "src/main.rs"
9+
10+
[dependencies]
11+
candid = { workspace = true, features = ["value", "printer"] }
12+
hex = { workspace = true }
13+
serde = { workspace = true }
14+
serde_bytes = { workspace = true }
15+
sha2 = { workspace = true }
16+
tokio = { workspace = true }
17+
ic-agent = { workspace = true }
18+
ic-certification = { workspace = true }
19+
ciborium = { workspace = true }
20+
num-traits = { workspace = true }
21+
chrono = { workspace = true }
22+
clap = { workspace = true }
23+
icrc-ledger-types = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
24+
ic-icrc1 = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
25+
ic-ledger-core = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
26+
ic-icrc1-tokens-u64 = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
27+
ic_message_types = { path = "../ic_message_types", version = "2" }

src/cli_dmsg/src/agent.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use candid::{
2+
utils::{encode_args, ArgumentEncoder},
3+
CandidType, Decode, Principal,
4+
};
5+
use ic_agent::{Agent, Identity};
6+
7+
use super::format_error;
8+
9+
pub async fn build_agent(host: &str, identity: Box<dyn Identity>) -> Result<Agent, String> {
10+
let agent = Agent::builder()
11+
.with_url(host)
12+
.with_boxed_identity(identity)
13+
.with_verify_query_signatures(true)
14+
.build()
15+
.map_err(format_error)?;
16+
if host.starts_with("http://") {
17+
agent.fetch_root_key().await.map_err(format_error)?;
18+
}
19+
20+
Ok(agent)
21+
}
22+
23+
pub async fn update_call<In, Out>(
24+
agent: &Agent,
25+
canister_id: &Principal,
26+
method_name: &str,
27+
args: In,
28+
) -> Result<Out, String>
29+
where
30+
In: ArgumentEncoder + Send,
31+
Out: CandidType + for<'a> candid::Deserialize<'a>,
32+
{
33+
let input = encode_args(args).map_err(format_error)?;
34+
let res = agent
35+
.update(canister_id, method_name)
36+
.with_arg(input)
37+
.call_and_wait()
38+
.await
39+
.map_err(format_error)?;
40+
let output = Decode!(res.as_slice(), Out).map_err(format_error)?;
41+
Ok(output)
42+
}
43+
44+
pub async fn query_call<In, Out>(
45+
agent: &Agent,
46+
canister_id: &Principal,
47+
method_name: &str,
48+
args: In,
49+
) -> Result<Out, String>
50+
where
51+
In: ArgumentEncoder + Send,
52+
Out: CandidType + for<'a> candid::Deserialize<'a>,
53+
{
54+
let input = encode_args(args).map_err(format_error)?;
55+
let res = agent
56+
.query(canister_id, method_name)
57+
.with_arg(input)
58+
.call()
59+
.await
60+
.map_err(format_error)?;
61+
let output = Decode!(res.as_slice(), Out).map_err(format_error)?;
62+
Ok(output)
63+
}

src/cli_dmsg/src/dmsg.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use candid::{Nat, Principal};
2+
use ic_agent::Agent;
3+
use ic_message_types::profile::UserInfo;
4+
use icrc_ledger_types::icrc1::{
5+
account::Account,
6+
transfer::{TransferArg, TransferError},
7+
};
8+
9+
use super::{
10+
agent::{query_call, update_call},
11+
format_error,
12+
};
13+
14+
#[derive(Clone)]
15+
pub struct DMsgAgent {
16+
pub agent: Agent,
17+
pub canister_id: Principal,
18+
}
19+
20+
impl DMsgAgent {
21+
pub async fn get_user_by_username(&self, username: &str) -> Result<UserInfo, String> {
22+
let output: Result<UserInfo, String> = query_call(
23+
&self.agent,
24+
&self.canister_id,
25+
"get_by_username",
26+
(username,),
27+
)
28+
.await?;
29+
30+
output
31+
}
32+
33+
pub async fn send_token_to(
34+
&self,
35+
token_canister: &Principal,
36+
to: Principal,
37+
amount_e8s: u64,
38+
) -> Result<Nat, String> {
39+
let output: Result<Nat, TransferError> = update_call(
40+
&self.agent,
41+
&token_canister,
42+
"icrc1_transfer",
43+
(TransferArg {
44+
from_subaccount: None,
45+
to: Account {
46+
owner: to,
47+
subaccount: None,
48+
},
49+
fee: None,
50+
created_at_time: None,
51+
memo: None,
52+
amount: amount_e8s.into(),
53+
},),
54+
)
55+
.await?;
56+
output.map_err(format_error)
57+
}
58+
}

0 commit comments

Comments
 (0)