-
Notifications
You must be signed in to change notification settings - Fork 113
feat: Add a stellar contract info wasm-hash command #1878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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: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.
There was a problem hiding this comment.
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.