-
Notifications
You must be signed in to change notification settings - Fork 129
Add stellar contract info hash command
#2576
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
cmd/crates/soroban-test/tests/it/integration/contract/info_hash.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| use crate::integration::util::{deploy_contract, test_address, DeployOptions, HELLO_WORLD}; | ||
|
|
||
| use soroban_test::{AssertExt, TestEnv}; | ||
|
|
||
| #[tokio::test] | ||
| async fn info_hash_with_wasm_file() { | ||
| let sandbox = &TestEnv::new(); | ||
| let expected = HELLO_WORLD.hash().unwrap().to_string(); | ||
|
|
||
| let actual = sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("info") | ||
| .arg("hash") | ||
| .arg("--wasm") | ||
| .arg(HELLO_WORLD.path()) | ||
| .assert() | ||
| .success() | ||
| .stdout_as_str(); | ||
|
|
||
| assert_eq!(actual, expected); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn info_hash_with_contract_id() { | ||
| let sandbox = &TestEnv::new(); | ||
| let expected = HELLO_WORLD.hash().unwrap().to_string(); | ||
| let contract_id = deploy_contract(sandbox, HELLO_WORLD, DeployOptions::default()).await; | ||
|
|
||
| let actual = sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("info") | ||
| .arg("hash") | ||
| .arg("--id") | ||
| .arg(&contract_id) | ||
| .assert() | ||
| .success() | ||
| .stdout_as_str(); | ||
|
|
||
| assert_eq!(actual, expected); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn info_hash_with_contract_alias() { | ||
| let sandbox = &TestEnv::new(); | ||
| let expected = HELLO_WORLD.hash().unwrap().to_string(); | ||
| let contract_id = deploy_contract(sandbox, HELLO_WORLD, DeployOptions::default()).await; | ||
|
|
||
| sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("alias") | ||
| .arg("add") | ||
| .arg("hello") | ||
| .arg("--id") | ||
| .arg(&contract_id) | ||
| .assert() | ||
| .success(); | ||
|
|
||
| let actual = sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("info") | ||
| .arg("hash") | ||
| .arg("--id") | ||
| .arg("hello") | ||
| .assert() | ||
| .success() | ||
| .stdout_as_str(); | ||
|
|
||
| assert_eq!(actual, expected); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn info_hash_errors_on_stellar_asset_contract() { | ||
| let sandbox = &TestEnv::new(); | ||
| let issuer = test_address(sandbox); | ||
| let sac_id = sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("asset") | ||
| .arg("deploy") | ||
| .arg(format!("--asset=USDC:{issuer}")) | ||
| .assert() | ||
| .success() | ||
| .stdout_as_str(); | ||
|
|
||
| sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("info") | ||
| .arg("hash") | ||
| .arg("--id") | ||
| .arg(&sac_id) | ||
| .assert() | ||
| .failure(); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn info_hash_requires_one_source() { | ||
| let sandbox = &TestEnv::new(); | ||
|
|
||
| sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("info") | ||
| .arg("hash") | ||
| .assert() | ||
| .failure(); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn info_hash_wasm_and_id_are_mutually_exclusive() { | ||
| let sandbox = &TestEnv::new(); | ||
| let contract_id = deploy_contract(sandbox, HELLO_WORLD, DeployOptions::default()).await; | ||
|
|
||
|
fnando marked this conversation as resolved.
|
||
| sandbox | ||
| .new_assert_cmd("contract") | ||
| .arg("info") | ||
| .arg("hash") | ||
| .arg("--wasm") | ||
| .arg(HELLO_WORLD.path()) | ||
| .arg("--id") | ||
| .arg(&contract_id) | ||
| .assert() | ||
| .failure(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| mod fetch; | ||
| mod info_hash; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| use clap::Parser; | ||
|
|
||
| use crate::{ | ||
| commands::global, | ||
| config::{ | ||
| self, locator, | ||
| network::{self}, | ||
| }, | ||
| wasm, | ||
| }; | ||
|
|
||
| #[derive(Parser, Debug, Clone)] | ||
| #[command(group( | ||
| clap::ArgGroup::new("source") | ||
| .required(true) | ||
| .args(&["wasm", "contract_id"]), | ||
| ))] | ||
| #[group(skip)] | ||
| pub struct Cmd { | ||
| /// Path to a local .wasm file. | ||
| #[arg(long, conflicts_with = "contract_id")] | ||
| pub wasm: Option<PathBuf>, | ||
| /// Contract ID or alias of a deployed contract. | ||
| #[arg( | ||
| long, | ||
| visible_alias = "id", | ||
| env = "STELLAR_CONTRACT_ID", | ||
| conflicts_with = "wasm" | ||
| )] | ||
| pub contract_id: Option<config::UnresolvedContract>, | ||
| #[command(flatten)] | ||
| pub network: network::Args, | ||
| #[command(flatten)] | ||
| pub locator: locator::Args, | ||
| } | ||
|
|
||
| #[derive(thiserror::Error, Debug)] | ||
| pub enum Error { | ||
| #[error(transparent)] | ||
| Wasm(#[from] wasm::Error), | ||
| #[error(transparent)] | ||
| Network(#[from] network::Error), | ||
| #[error(transparent)] | ||
| Locator(#[from] locator::Error), | ||
| } | ||
|
|
||
| impl Cmd { | ||
| pub async fn run(&self, _global_args: &global::Args) -> Result<(), Error> { | ||
| let hash = if let Some(path) = &self.wasm { | ||
| wasm::Args { wasm: path.clone() }.hash()? | ||
| } else if let Some(contract_id) = &self.contract_id { | ||
| let network = self.network.get(&self.locator)?; | ||
| let resolved = | ||
| contract_id.resolve_contract_id(&self.locator, &network.network_passphrase)?; | ||
| wasm::fetch_wasm_hash_from_contract(&resolved, &network).await? | ||
| } else { | ||
| unreachable!("clap ArgGroup guarantees one of --wasm or --contract-id is set"); | ||
| }; | ||
|
|
||
| println!("{hash}"); | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.