soroban_cli/commands/ledger/entry/fetch/
contract_code.rs

1use super::args::Args;
2use crate::xdr::{Hash, LedgerKey, LedgerKeyContractCode};
3use clap::{command, Parser};
4
5#[derive(Parser, Debug, Clone)]
6#[group(skip)]
7pub struct Cmd {
8    /// Get WASM bytecode by hash
9    #[arg(long)]
10    pub wasm_hash: Vec<String>,
11
12    #[command(flatten)]
13    pub args: Args,
14}
15
16#[derive(thiserror::Error, Debug)]
17pub enum Error {
18    #[error("provided hash value is invalid: {0}")]
19    InvalidHash(String),
20    #[error(transparent)]
21    Run(#[from] super::args::Error),
22}
23
24impl Cmd {
25    pub async fn run(&self) -> Result<(), Error> {
26        let mut ledger_keys = vec![];
27        self.insert_keys(&mut ledger_keys)?;
28        Ok(self.args.run(ledger_keys).await?)
29    }
30
31    fn insert_keys(&self, ledger_keys: &mut Vec<LedgerKey>) -> Result<(), Error> {
32        for hash in &self.wasm_hash {
33            let hash = Hash(
34                soroban_spec_tools::utils::contract_id_from_str(hash)
35                    .map_err(|_| Error::InvalidHash(hash.clone()))?,
36            );
37            let key = LedgerKey::ContractCode(LedgerKeyContractCode { hash });
38            ledger_keys.push(key);
39        }
40
41        Ok(())
42    }
43}