Skip to content

Commit f56e26f

Browse files
committed
feat: add cli commands
1 parent 7f4d835 commit f56e26f

File tree

12 files changed

+2231
-160
lines changed

12 files changed

+2231
-160
lines changed

cli/src/action/agent.rs

Lines changed: 159 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::fmt::Display;
22

33
use anyhow::anyhow;
44
use tabled::Table;
5-
use torus_client::{client::TorusClient, subxt::utils::AccountId32};
5+
use torus_client::{client::TorusClient, interfaces, subxt::utils::AccountId32};
66

77
use crate::{
8-
action::{Action, ActionContext},
8+
action::{Action, ActionContext, Changes},
99
keypair::Keypair,
1010
store::{get_account, get_key},
11-
util::format_torus,
11+
util::to_percent_u8,
1212
};
1313

1414
pub struct AgentInfoAction {
@@ -95,7 +95,7 @@ impl Action for RegisterAgentAction {
9595
}
9696

9797
async fn estimate_fee(&self, ctx: &impl ActionContext) -> anyhow::Result<u128> {
98-
let fee = if !ctx.is_testnet() {
98+
let fee = if ctx.is_mainnet() {
9999
let client = TorusClient::for_mainnet().await?;
100100
client
101101
.torus0()
@@ -124,24 +124,19 @@ impl Action for RegisterAgentAction {
124124
Ok(fee)
125125
}
126126

127-
async fn confirmation_phrase(
128-
&self,
129-
ctx: &impl ActionContext,
130-
) -> anyhow::Result<Option<String>> {
127+
async fn get_changes(&self, ctx: &impl ActionContext) -> anyhow::Result<Option<Changes>> {
131128
let fee = self.estimate_fee(ctx).await?;
132-
Ok(Some(format!(
133-
"Are you sure you want to register {} as an agent? {}\n[y/N]",
134-
self.keypair.ss58_address(),
135-
if fee != 0 {
136-
format!("(there will be a {} torus fee)", format_torus(fee))
137-
} else {
138-
"".to_string()
139-
}
140-
)))
129+
Ok(Some(Changes {
130+
changes: vec![format!(
131+
"register an agent with name {} which is not changeable later",
132+
self.name
133+
)],
134+
fee: Some(fee),
135+
}))
141136
}
142137

143138
async fn execute(&self, ctx: &impl ActionContext) -> anyhow::Result<Self::ResponseData> {
144-
if !ctx.is_testnet() {
139+
if ctx.is_mainnet() {
145140
let client = TorusClient::for_mainnet().await?;
146141
client
147142
.torus0()
@@ -190,7 +185,7 @@ impl Action for UnregisterAgentAction {
190185
}
191186

192187
async fn estimate_fee(&self, ctx: &impl ActionContext) -> anyhow::Result<u128> {
193-
let fee = if !ctx.is_testnet() {
188+
let fee = if ctx.is_mainnet() {
194189
let client = TorusClient::for_mainnet().await?;
195190
client
196191
.torus0()
@@ -209,24 +204,20 @@ impl Action for UnregisterAgentAction {
209204
Ok(fee)
210205
}
211206

212-
async fn confirmation_phrase(
213-
&self,
214-
ctx: &impl ActionContext,
215-
) -> anyhow::Result<Option<String>> {
207+
async fn get_changes(&self, ctx: &impl ActionContext) -> anyhow::Result<Option<Changes>> {
216208
let fee = self.estimate_fee(ctx).await?;
217-
Ok(Some(format!(
218-
"Are you sure you want to unregister {} from agent? {}\n[y/N]",
219-
self.keypair.ss58_address(),
220-
if fee != 0 {
221-
format!("(there will be a {} torus fee)", format_torus(fee))
222-
} else {
223-
"".to_string()
224-
}
225-
)))
209+
210+
Ok(Some(Changes {
211+
changes: vec![format!(
212+
"Unregister the agent {}",
213+
self.keypair.ss58_address()
214+
)],
215+
fee: Some(fee),
216+
}))
226217
}
227218

228219
async fn execute(&self, ctx: &impl ActionContext) -> anyhow::Result<Self::ResponseData> {
229-
if !ctx.is_testnet() {
220+
if ctx.is_mainnet() {
230221
let client = TorusClient::for_mainnet().await?;
231222
client
232223
.torus0()
@@ -255,6 +246,141 @@ impl Display for UnregisterAgentActionResponse {
255246
}
256247
}
257248

249+
pub struct UpdateAgentAction {
250+
key: Keypair,
251+
url: String,
252+
metadata: Option<String>,
253+
staking_fee: Option<u32>,
254+
weight_control_fee: Option<u32>,
255+
}
256+
257+
impl Action for UpdateAgentAction {
258+
type Params = (String, String, Option<String>, Option<u32>, Option<u32>);
259+
type ResponseData = UpdateAgentActionResponse;
260+
261+
async fn create(
262+
ctx: &impl ActionContext,
263+
(key, url, metadata, staking_fee, weight_control_fee): Self::Params,
264+
) -> anyhow::Result<Self> {
265+
let key = get_key(&key)?;
266+
let (_, keypair) = ctx.decrypt(&key)?;
267+
268+
Ok(Self {
269+
key: keypair,
270+
url,
271+
metadata,
272+
staking_fee,
273+
weight_control_fee,
274+
})
275+
}
276+
277+
async fn estimate_fee(&self, ctx: &impl ActionContext) -> anyhow::Result<u128> {
278+
let staking_fee = self.staking_fee.map(to_percent_u8).transpose()?;
279+
let weight_control_fee = self.weight_control_fee.map(to_percent_u8).transpose()?;
280+
281+
let fee = if ctx.is_mainnet() {
282+
let client = TorusClient::for_mainnet().await?;
283+
client
284+
.torus0()
285+
.calls()
286+
.update_agent_fee(
287+
self.url.as_bytes().to_vec(),
288+
self.metadata.clone().map(|str| str.as_bytes().to_vec()),
289+
staking_fee.map(
290+
interfaces::mainnet::api::runtime_types::sp_arithmetic::per_things::Percent,
291+
),
292+
weight_control_fee.clone().map(
293+
interfaces::mainnet::api::runtime_types::sp_arithmetic::per_things::Percent,
294+
),
295+
self.key.clone(),
296+
)
297+
.await?
298+
} else {
299+
let client = TorusClient::for_testnet().await?;
300+
client
301+
.torus0()
302+
.calls()
303+
.update_agent_fee(
304+
self.url.as_bytes().to_vec(),
305+
self.metadata.clone().map(|str| str.as_bytes().to_vec()),
306+
staking_fee.map(
307+
interfaces::testnet::api::runtime_types::sp_arithmetic::per_things::Percent,
308+
),
309+
weight_control_fee.clone().map(
310+
interfaces::testnet::api::runtime_types::sp_arithmetic::per_things::Percent,
311+
),
312+
self.key.clone(),
313+
)
314+
.await?
315+
};
316+
317+
Ok(fee)
318+
}
319+
320+
async fn get_changes(&self, ctx: &impl ActionContext) -> anyhow::Result<Option<Changes>> {
321+
let fee = self.estimate_fee(ctx).await?;
322+
Ok(Some(Changes {
323+
changes: vec![format!(
324+
"Update agent `{}` information",
325+
self.key.ss58_address()
326+
)],
327+
fee: Some(fee),
328+
}))
329+
}
330+
331+
async fn execute(&self, ctx: &impl ActionContext) -> anyhow::Result<Self::ResponseData> {
332+
let staking_fee = self.staking_fee.map(to_percent_u8).transpose()?;
333+
let weight_control_fee = self.weight_control_fee.map(to_percent_u8).transpose()?;
334+
335+
if ctx.is_mainnet() {
336+
let client = TorusClient::for_mainnet().await?;
337+
client
338+
.torus0()
339+
.calls()
340+
.update_agent_wait(
341+
self.url.as_bytes().to_vec(),
342+
self.metadata.clone().map(|str| str.as_bytes().to_vec()),
343+
staking_fee.map(
344+
interfaces::mainnet::api::runtime_types::sp_arithmetic::per_things::Percent,
345+
),
346+
weight_control_fee.clone().map(
347+
interfaces::mainnet::api::runtime_types::sp_arithmetic::per_things::Percent,
348+
),
349+
self.key.clone(),
350+
)
351+
.await?;
352+
} else {
353+
let client = TorusClient::for_testnet().await?;
354+
client
355+
.torus0()
356+
.calls()
357+
.update_agent_wait(
358+
self.url.as_bytes().to_vec(),
359+
self.metadata.clone().map(|str| str.as_bytes().to_vec()),
360+
staking_fee.map(
361+
interfaces::testnet::api::runtime_types::sp_arithmetic::per_things::Percent,
362+
),
363+
weight_control_fee.clone().map(
364+
interfaces::testnet::api::runtime_types::sp_arithmetic::per_things::Percent,
365+
),
366+
self.key.clone(),
367+
)
368+
.await?;
369+
}
370+
371+
Ok(UpdateAgentActionResponse)
372+
}
373+
}
374+
375+
#[derive(serde::Serialize)]
376+
pub struct UpdateAgentActionResponse;
377+
378+
impl Display for UpdateAgentActionResponse {
379+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
380+
write!(f, "Updated successfully")
381+
}
382+
}
383+
258384
async fn get_agent_info(
259385
ctx: &impl ActionContext,
260386
account: &AccountId32,

0 commit comments

Comments
 (0)