partner_chains_smart_contracts_commands/
governed_map.rs1use crate::{GenesisUtxo, PaymentFilePath};
2use partner_chains_cardano_offchain::governed_map::{
3 run_get, run_insert, run_list, run_remove, run_update,
4};
5use serde_json::json;
6use sidechain_domain::byte_string::ByteString;
7use std::collections::HashMap;
8
9#[derive(Clone, Debug, clap::Subcommand)]
10#[allow(clippy::large_enum_variant)]
11pub enum GovernedMapCmd {
13 Insert(InsertCmd),
18 Update(UpdateCmd),
20 Remove(RemoveCmd),
22 List(ListCmd),
24 Get(GetCmd),
26}
27
28impl GovernedMapCmd {
29 pub async fn execute(self) -> crate::SubCmdResult {
31 match self {
32 Self::Insert(cmd) => cmd.execute().await,
33 Self::Update(cmd) => cmd.execute().await,
34 Self::Remove(cmd) => cmd.execute().await,
35 Self::List(cmd) => cmd.execute().await,
36 Self::Get(cmd) => cmd.execute().await,
37 }
38 }
39}
40
41#[derive(Clone, Debug, clap::Parser)]
42pub struct InsertCmd {
44 #[clap(flatten)]
45 common_arguments: crate::CommonArguments,
46 #[arg(long)]
47 key: String,
49 #[arg(long)]
50 value: ByteString,
52 #[clap(flatten)]
53 payment_key_file: PaymentFilePath,
55 #[clap(flatten)]
56 genesis_utxo: GenesisUtxo,
58}
59
60impl InsertCmd {
61 pub async fn execute(self) -> crate::SubCmdResult {
63 let payment_key = self.payment_key_file.read_key()?;
64
65 let client = self.common_arguments.get_ogmios_client().await?;
66
67 let result = run_insert(
68 self.genesis_utxo.into(),
69 self.key,
70 self.value,
71 &payment_key,
72 &client,
73 &self.common_arguments.retries(),
74 )
75 .await;
76 print_result_json(result)
77 }
78}
79
80#[derive(Clone, Debug, clap::Parser)]
81pub struct UpdateCmd {
83 #[clap(flatten)]
84 common_arguments: crate::CommonArguments,
85 #[arg(long)]
86 key: String,
88 #[arg(long)]
89 value: ByteString,
91 #[arg(long)]
92 current_value: Option<ByteString>,
94 #[clap(flatten)]
95 payment_key_file: PaymentFilePath,
97 #[clap(flatten)]
98 genesis_utxo: GenesisUtxo,
100}
101
102impl UpdateCmd {
103 pub async fn execute(self) -> crate::SubCmdResult {
105 let payment_key = self.payment_key_file.read_key()?;
106
107 let client = self.common_arguments.get_ogmios_client().await?;
108
109 let result = run_update(
110 self.genesis_utxo.into(),
111 self.key,
112 self.value,
113 self.current_value,
114 &payment_key,
115 &client,
116 &self.common_arguments.retries(),
117 )
118 .await;
119 print_result_json(result)
120 }
121}
122
123#[derive(Clone, Debug, clap::Parser)]
124pub struct RemoveCmd {
126 #[clap(flatten)]
127 common_arguments: crate::CommonArguments,
128 #[arg(long)]
129 key: String,
131 #[clap(flatten)]
132 payment_key_file: PaymentFilePath,
134 #[clap(flatten)]
135 genesis_utxo: GenesisUtxo,
137}
138
139impl RemoveCmd {
140 pub async fn execute(self) -> crate::SubCmdResult {
142 let payment_key = self.payment_key_file.read_key()?;
143
144 let client = self.common_arguments.get_ogmios_client().await?;
145
146 let result = run_remove(
147 self.genesis_utxo.into(),
148 self.key,
149 &payment_key,
150 &client,
151 &self.common_arguments.retries(),
152 )
153 .await;
154 print_result_json(result)
155 }
156}
157
158#[derive(Clone, Debug, clap::Parser)]
159pub struct ListCmd {
161 #[clap(flatten)]
162 common_arguments: crate::CommonArguments,
163 #[clap(flatten)]
164 genesis_utxo: GenesisUtxo,
166}
167
168impl ListCmd {
169 pub async fn execute(self) -> crate::SubCmdResult {
171 let client = self.common_arguments.get_ogmios_client().await?;
172 let kv_pairs: HashMap<_, _> = run_list(self.genesis_utxo.into(), &client)
173 .await?
174 .map(|datum| (datum.key, datum.value.to_hex_string()))
175 .collect();
176
177 Ok(json!(kv_pairs))
178 }
179}
180
181#[derive(Clone, Debug, clap::Parser)]
182pub struct GetCmd {
184 #[clap(flatten)]
185 common_arguments: crate::CommonArguments,
186 #[arg(long)]
187 key: String,
189 #[clap(flatten)]
190 genesis_utxo: GenesisUtxo,
192}
193
194impl GetCmd {
195 pub async fn execute(self) -> crate::SubCmdResult {
197 let client = self.common_arguments.get_ogmios_client().await?;
198 let Some(value) = run_get(self.genesis_utxo.into(), self.key.clone(), &client).await?
199 else {
200 return Ok(json!({}).into());
201 };
202
203 Ok(json!(value.to_hex_string()).into())
204 }
205}
206
207fn print_result_json(
209 result: anyhow::Result<Option<crate::MultiSigSmartContractResult>>,
210) -> crate::SubCmdResult {
211 match result {
212 Err(err) => Err(err)?,
213 Ok(Some(res)) => Ok(json!(res)),
214 Ok(None) => Ok(json!({})),
215 }
216}