Skip to content

Commit f7fa8fd

Browse files
committed
feat: minor adjustments to formatting and information display
1 parent cb2312e commit f7fa8fd

File tree

7 files changed

+106
-55
lines changed

7 files changed

+106
-55
lines changed

cli/src/cli/agent.rs

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use tabled::Table;
2-
use torus_client::client::TorusClient;
2+
use torus_client::{client::TorusClient, subxt::utils::AccountId32};
33

44
use crate::{
55
cli::CliCtx,
@@ -37,6 +37,7 @@ pub enum AgentCliSubCommand {
3737

3838
#[derive(tabled::Tabled)]
3939
struct AgentInfo {
40+
address: String,
4041
name: String,
4142
metadata: String,
4243
url: String,
@@ -46,13 +47,15 @@ struct AgentInfo {
4647

4748
impl AgentInfo {
4849
fn new(
50+
address: String,
4951
name: Vec<u8>,
5052
metadata: Vec<u8>,
5153
url: Vec<u8>,
5254
last_update_block: u64,
5355
registration_block: u64,
5456
) -> Self {
5557
Self {
58+
address,
5659
name: String::from_utf8_lossy(&name[..]).to_string(),
5760
metadata: String::from_utf8_lossy(&metadata[..]).to_string(),
5861
url: String::from_utf8_lossy(&url[..]).to_string(),
@@ -67,39 +70,7 @@ pub async fn info(ctx: &CliCtx, account: String) -> anyhow::Result<()> {
6770

6871
println!("Fetching agent data...");
6972

70-
let agent_info = if ctx.is_testnet() {
71-
let client = TorusClient::for_testnet().await?;
72-
client
73-
.torus0()
74-
.storage()
75-
.agents_get(&account)
76-
.await?
77-
.map(|agent| {
78-
AgentInfo::new(
79-
agent.name.0,
80-
agent.metadata.0,
81-
agent.url.0,
82-
agent.last_update_block,
83-
agent.registration_block,
84-
)
85-
})
86-
} else {
87-
let client = TorusClient::for_mainnet().await?;
88-
client
89-
.torus0()
90-
.storage()
91-
.agents_get(&account)
92-
.await?
93-
.map(|agent| {
94-
AgentInfo::new(
95-
agent.name.0,
96-
agent.metadata.0,
97-
agent.url.0,
98-
agent.last_update_block,
99-
agent.registration_block,
100-
)
101-
})
102-
};
73+
let agent_info = get_agent_info(ctx, &account).await?;
10374

10475
if let Some(agent_info) = agent_info {
10576
let table = Table::kv(std::iter::once(agent_info));
@@ -136,7 +107,7 @@ pub async fn register(
136107
name.as_bytes().to_vec(),
137108
url.as_bytes().to_vec(),
138109
metadata.as_bytes().to_vec(),
139-
keypair,
110+
keypair.clone(),
140111
)
141112
.await?;
142113
} else {
@@ -148,12 +119,61 @@ pub async fn register(
148119
name.as_bytes().to_vec(),
149120
url.as_bytes().to_vec(),
150121
metadata.as_bytes().to_vec(),
151-
keypair,
122+
keypair.clone(),
152123
)
153124
.await?;
154125
};
155126

127+
let agent_info = get_agent_info(ctx, &keypair.account()).await?;
128+
let Some(agent_info) = agent_info else {
129+
println!("Something went wrong...");
130+
return Ok(());
131+
};
132+
156133
println!("Agent registered successfully!");
157134

135+
let table = Table::kv(std::iter::once(agent_info));
136+
println!("{table}");
137+
158138
Ok(())
159139
}
140+
141+
async fn get_agent_info(ctx: &CliCtx, account: &AccountId32) -> anyhow::Result<Option<AgentInfo>> {
142+
let agent_info = if ctx.is_testnet() {
143+
let client = TorusClient::for_testnet().await?;
144+
client
145+
.torus0()
146+
.storage()
147+
.agents_get(account)
148+
.await?
149+
.map(|agent| {
150+
AgentInfo::new(
151+
agent.key.to_string(),
152+
agent.name.0,
153+
agent.metadata.0,
154+
agent.url.0,
155+
agent.last_update_block,
156+
agent.registration_block,
157+
)
158+
})
159+
} else {
160+
let client = TorusClient::for_mainnet().await?;
161+
client
162+
.torus0()
163+
.storage()
164+
.agents_get(account)
165+
.await?
166+
.map(|agent| {
167+
AgentInfo::new(
168+
agent.key.to_string(),
169+
agent.name.0,
170+
agent.metadata.0,
171+
agent.url.0,
172+
agent.last_update_block,
173+
agent.registration_block,
174+
)
175+
})
176+
};
177+
178+
Ok(agent_info)
179+
}

cli/src/cli/balance.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use torus_client::{
1212
use crate::{
1313
cli::CliCtx,
1414
store::{get_account, get_key},
15+
util::torus,
1516
};
1617

1718
#[derive(clap::Args)]
@@ -65,10 +66,10 @@ pub async fn check(ctx: &CliCtx, key: String) -> anyhow::Result<()> {
6566
.unwrap_or((0, 0, 0));
6667

6768
let mut table = Table::new(vec![
68-
(("free".to_string()), data.0.to_string()),
69+
(("free".to_string()), torus(data.0)),
6970
(
7071
("reserved (stake + others)".to_string()),
71-
(data.1 + data.2).to_string(),
72+
torus(data.1 + data.2),
7273
),
7374
]);
7475
table.with(Remove::row(FirstRow));
@@ -89,7 +90,7 @@ pub async fn transfer(
8990

9091
let target = AccountId32::from_str(&target)?;
9192

92-
ctx.confirm(&format!("transfer {amount} to {target}"))?;
93+
ctx.confirm(&format!("transfer {} to {target}", torus(amount)))?;
9394

9495
println!("Transfering...");
9596

cli/src/cli/namespace.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use torus_client::{
77
use crate::{
88
cli::CliCtx,
99
store::{get_account, get_key},
10+
util::torus,
1011
};
1112

1213
#[derive(clap::Args)]
@@ -94,7 +95,9 @@ pub async fn register(ctx: &CliCtx, key: String, path: String) -> anyhow::Result
9495
};
9596

9697
ctx.confirm(&format!(
97-
"register namespace {path} for a fee of {fee} and deposit of {deposit}"
98+
"register namespace {path} for a fee of {} and deposit of {}",
99+
torus(fee),
100+
torus(deposit),
98101
))?;
99102

100103
println!("Registering namespace...");

cli/src/cli/stake.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use torus_client::{
99
use crate::{
1010
cli::CliCtx,
1111
store::{get_account, get_key},
12+
util::torus,
1213
};
1314

1415
#[derive(clap::Parser)]
@@ -65,12 +66,14 @@ pub enum StakeCliSubCommand {
6566
#[derive(tabled::Tabled)]
6667
struct StakingEntry {
6768
target: String,
68-
amount: u128,
69+
amount: String,
6970
}
7071

7172
pub async fn given(ctx: &CliCtx, key: String) -> anyhow::Result<()> {
7273
let account = get_account(&key)?;
7374

75+
println!("Fetching given stake...");
76+
7477
let staking = if ctx.is_testnet() {
7578
let client = TorusClient::for_testnet().await?;
7679
client
@@ -91,15 +94,22 @@ pub async fn given(ctx: &CliCtx, key: String) -> anyhow::Result<()> {
9194
.await?
9295
};
9396

94-
let staking = staking
95-
.into_iter()
97+
let mut entries = staking
98+
.iter()
9699
.map(|((_, target), amount)| StakingEntry {
97100
target: target.to_string(),
98-
amount,
99-
});
101+
amount: torus(*amount),
102+
})
103+
.collect::<Vec<_>>();
100104

101-
let table = Table::new(staking);
105+
let sum = staking.iter().map(|(_, amount)| *amount).sum::<u128>();
102106

107+
entries.push(StakingEntry {
108+
target: "".to_string(),
109+
amount: torus(sum),
110+
});
111+
112+
let table = Table::new(entries);
103113
println!("{table}");
104114

105115
Ok(())
@@ -108,12 +118,14 @@ pub async fn given(ctx: &CliCtx, key: String) -> anyhow::Result<()> {
108118
#[derive(tabled::Tabled)]
109119
struct StakedEntry {
110120
source: String,
111-
amount: u128,
121+
amount: String,
112122
}
113123

114124
pub async fn received(ctx: &CliCtx, key: String) -> anyhow::Result<()> {
115125
let account = get_account(&key)?;
116126

127+
println!("Fetching received stake...");
128+
117129
let staked = if ctx.is_testnet() {
118130
let client = TorusClient::for_testnet().await?;
119131
client
@@ -134,13 +146,22 @@ pub async fn received(ctx: &CliCtx, key: String) -> anyhow::Result<()> {
134146
.await?
135147
};
136148

137-
let staked = staked.into_iter().map(|((_, source), amount)| StakedEntry {
138-
source: source.to_string(),
139-
amount,
140-
});
149+
let mut entries = staked
150+
.iter()
151+
.map(|((_, target), amount)| StakedEntry {
152+
source: target.to_string(),
153+
amount: torus(*amount),
154+
})
155+
.collect::<Vec<_>>();
141156

142-
let table = Table::new(staked);
157+
let sum = staked.iter().map(|(_, amount)| *amount).sum::<u128>();
158+
159+
entries.push(StakedEntry {
160+
source: "".to_string(),
161+
amount: torus(sum),
162+
});
143163

164+
let table = Table::new(entries);
144165
println!("{table}");
145166

146167
Ok(())
@@ -152,7 +173,7 @@ pub async fn add(ctx: &CliCtx, key: String, target: String, amount: u128) -> any
152173

153174
let target = AccountId32::from_str(&target)?;
154175

155-
ctx.confirm(&format!("add {amount} stake to {target}"))?;
176+
ctx.confirm(&format!("add {} stake to {target}", torus(amount)))?;
156177

157178
println!("Staking...");
158179

@@ -183,7 +204,7 @@ pub async fn remove(ctx: &CliCtx, key: String, target: String, amount: u128) ->
183204

184205
let target = AccountId32::from_str(&target)?;
185206

186-
ctx.confirm(&format!("remove {amount} stake from {target}"))?;
207+
ctx.confirm(&format!("remove {} stake from {target}", torus(amount)))?;
187208

188209
println!("Unstaking...");
189210

@@ -222,7 +243,8 @@ pub async fn transfer(
222243
let target = AccountId32::from_str(&target)?;
223244

224245
ctx.confirm(&format!(
225-
"transfer {amount} stake from {source} to {target}"
246+
"transfer {} stake from {source} to {target}",
247+
torus(amount)
226248
))?;
227249

228250
println!("Transfering stake...");

cli/src/keypair.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use torus_client::subxt::{
1515
use crate::store::Key;
1616

1717
#[allow(clippy::large_enum_variant)]
18+
#[derive(Clone)]
1819
pub enum Keypair {
1920
ED25519(ed25519::Pair),
2021
SR25519(sr25519::Pair),

cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod cli;
22
mod keypair;
33
mod store;
4+
mod util;
45

56
#[tokio::main]
67
async fn main() -> anyhow::Result<()> {

cli/src/util.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn torus(amount: u128) -> String {
2+
format!("{:.5}", amount as u128 / 10u128.pow(18))
3+
}

0 commit comments

Comments
 (0)