Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/soroban-cli/src/commands/contract/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod env_meta;
pub mod interface;
pub mod meta;
pub mod shared;
pub mod wasm_hash;

#[derive(Debug, clap::Subcommand)]
pub enum Cmd {
Expand Down Expand Up @@ -49,6 +50,9 @@ pub enum Cmd {
///
/// Outputs no data when no data is present in the contract.
EnvMeta(env_meta::Cmd),

/// Get the wasm hash of a deployed contract
WasmHash(wasm_hash::Cmd),
Comment on lines +54 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fnando wdyt naming this command executable? Technically contracts can have an executable that is either:

  • stellar-asset
  • wasm-hash

In the past when we've made commands that accepted a Contract ID and errored on stellar-assets, it's been annoying that the edge case wasn't supported.

The command could output not just the hash but the type of executable, maybe in a simple json format. Or just use xdr-json of the ContractExecutable xdr type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also might be fair that getting a wasm-hash explicitly as the only output could be more helpful when scripting in some cases. There might be a case for both commands co-existing.

}

#[derive(thiserror::Error, Debug)]
Expand All @@ -59,6 +63,8 @@ pub enum Error {
Meta(#[from] meta::Error),
#[error(transparent)]
EnvMeta(#[from] env_meta::Error),
#[error(transparent)]
WasmHash(#[from] wasm_hash::Error),
}

impl Cmd {
Expand All @@ -67,6 +73,7 @@ impl Cmd {
Cmd::Interface(interface) => interface.run(global_args).await?,
Cmd::Meta(meta) => meta.run(global_args).await?,
Cmd::EnvMeta(env_meta) => env_meta.run(global_args).await?,
Cmd::WasmHash(cmd) => cmd.run(global_args).await?,
};
println!("{result}");
Ok(())
Expand Down
67 changes: 67 additions & 0 deletions cmd/soroban-cli/src/commands/contract/info/wasm_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use clap::{arg, command, Command, ArgMatches};
use soroban_rpc::client::Client;
use soroban_xdr::{ScAddress, ScVal, DecodeError};
use stellar_xdr::XdrCodec;
use crate::config::Config;
use crate::error::Result;
use clap::Parser;
use stellar_xdr::curr as xdr;

use crate::commands::{config::network, global};
use crate::config::locator;
use crate::rpc;

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub config_locator: locator::Args,

#[command(flatten)]
pub network: network::Args,

/// Contract ID to get the wasm hash for
#[arg(long)]
pub contract_id: stellar_strkey::Contract,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a string, that is then used with alias resolution. Worth looking at other commands that accept a contract ID to see how this works.

}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),

#[error(transparent)]
Network(#[from] network::Error),

#[error(transparent)]
Rpc(#[from] rpc::Error),
}

impl Cmd {
pub async fn run(&self, _global_args: &global::Args) -> Result<(), Error> {
let network = self.network.get(&self.config_locator)?;
let client = network.get_client()?;

// Get the contract instance ledger entry
let key = xdr::LedgerKey::ContractData(xdr::LedgerKeyContractData {
contract: self.contract_id.clone().into(),
key: xdr::ScVal::LedgerKeyContractInstance,
durability: xdr::ContractDataDurability::Persistent,
});

let entry = client.get_ledger_entry(&key).await?;

// Extract the wasm hash from the contract instance
if let xdr::LedgerEntryData::ContractData(data) = entry.data {
if let xdr::ScVal::ContractInstance(instance) = data.val {
if let xdr::ContractExecutable::Wasm(hash) = instance.executable {
println!("{}", hex::encode(hash.0));
return Ok(());
}
}
}

Err(rpc::Error::InvalidResponse("Contract instance not found".into()).into())
Comment on lines +57 to +64
Copy link
Member

@leighmcculloch leighmcculloch Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should handle the other contract executable types, and emit the name of the executable if not a wasm-hash. See my other comment here: #1878 (comment).

}
}

3 changes: 3 additions & 0 deletions cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod read;
pub mod restore;
pub mod upload;


use crate::{commands::global, print::Print};

#[derive(Debug, clap::Subcommand)]
Expand Down Expand Up @@ -91,6 +92,7 @@ pub enum Cmd {
///
/// If no keys are specificed the contract itself is restored.
Restore(restore::Cmd),

}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -142,6 +144,7 @@ pub enum Error {

#[error(transparent)]
Restore(#[from] restore::Error),

}

impl Cmd {
Expand Down
Loading