-
Notifications
You must be signed in to change notification settings - Fork 126
Add remove command
#837
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
Add remove command
#837
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
b7e587a
first version of remove command integration
lean-apple 810661a
import remove command
lean-apple 4b86a21
fix some type errors for code_hash
lean-apple d6c41d9
update with last changes
lean-apple 08d5198
optimize parse_code_hash function use
lean-apple ebeecd5
remove rpc call and code use
lean-apple 9fa4706
Merge branch 'master' into ln-add-remove-command
lean-apple ea7f284
get last changes
lean-apple f07ee02
use new way to get code_hash
lean-apple a3fea1d
fix event for remove command result
lean-apple f27a4ee
fix fmt
lean-apple 3426398
change the way to get code hash
lean-apple 2f2f68c
add last changes
lean-apple def1ade
add remove command integration test
lean-apple 33a6662
fix fmt
lean-apple 05d9e75
Merge branch 'master' into ln-add-remove-command
lean-apple 27e25b9
ignore integration tests
lean-apple 2b2ce84
complete documentation for remove command
lean-apple d94be80
remove unused parse code hash function
lean-apple 67e5a77
fix: draft for code_hash arg return
lean-apple dcccb12
complete remove command documentation with code-hash
lean-apple 4aee17f
fix previous merge of last changes
lean-apple bb30200
fix previous merge of last changes
lean-apple 6551782
fix previous merge of last changes
lean-apple 9daca4c
fix: fix code_hash check but still error variant issue
lean-apple c9fc21c
Merge branch 'master' into ln-add-remove-command
lean-apple e80644a
fix code-hash handle
lean-apple caf4879
get last changes
lean-apple 6058aa8
optimize get hash and error message
lean-apple bd958be
fix documentation
lean-apple 3622781
improve code_hash
lean-apple 7383163
fix error handling
lean-apple a857219
Add api imports
ascjones a39c712
Use correct event type
ascjones cba704b
Fix extrinsics.md
ascjones c733739
Fix extrinsics.md
ascjones 27763a2
Wrap long error message
ascjones 59117d8
Error message formatting
ascjones 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
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
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,145 @@ | ||
| // Copyright 2018-2022 Parity Technologies (UK) Ltd. | ||
| // This file is part of cargo-contract. | ||
| // | ||
| // cargo-contract is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // cargo-contract is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with cargo-contract. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| use super::{ | ||
| runtime_api::api::{ | ||
| self, | ||
| contracts::events::CodeRemoved, | ||
| }, | ||
| submit_extrinsic, | ||
| Client, | ||
| CodeHash, | ||
| ContractMessageTranscoder, | ||
| DefaultConfig, | ||
| ExtrinsicOpts, | ||
| PairSigner, | ||
| TokenMetadata, | ||
| }; | ||
| use crate::{ | ||
| cmd::extrinsics::{ | ||
| events::DisplayEvents, | ||
| parse_code_hash, | ||
| ErrorVariant, | ||
| }, | ||
| name_value_println, | ||
| }; | ||
| use anyhow::Result; | ||
| use std::fmt::Debug; | ||
| use subxt::{ | ||
| Config, | ||
| OnlineClient, | ||
| }; | ||
|
|
||
| #[derive(Debug, clap::Args)] | ||
| #[clap(name = "remove", about = "Remove a contract's code")] | ||
| pub struct RemoveCommand { | ||
| /// The hash of the smart contract code already uploaded to the chain. | ||
| #[clap(long, value_parser = parse_code_hash)] | ||
| code_hash: Option<<DefaultConfig as Config>::Hash>, | ||
| #[clap(flatten)] | ||
| extrinsic_opts: ExtrinsicOpts, | ||
| /// Export the call output in JSON format. | ||
| #[clap(long, conflicts_with = "verbose")] | ||
| output_json: bool, | ||
| } | ||
|
|
||
| impl RemoveCommand { | ||
| pub fn is_json(&self) -> bool { | ||
| self.output_json | ||
| } | ||
|
|
||
| pub fn run(&self) -> Result<(), ErrorVariant> { | ||
| let artifacts = self.extrinsic_opts.contract_artifacts()?; | ||
| let transcoder = artifacts.contract_transcoder()?; | ||
| let signer = super::pair_signer(self.extrinsic_opts.signer()?); | ||
|
|
||
| let artifacts_path = artifacts.artifact_path().to_path_buf(); | ||
|
|
||
| let final_code_hash = match (self.code_hash.as_ref(), artifacts.code.as_ref()) { | ||
| (Some(code_h), _) => { | ||
| Ok(code_h.0) | ||
| } | ||
| (None, Some(_)) => { | ||
| artifacts.code_hash() | ||
| } | ||
| (None, None) => { | ||
| Err(anyhow::anyhow!( | ||
| "No code_hash was provided or contract code was not found from artifact \ | ||
| file {}. Please provide a code hash with --code-hash argument or specify the \ | ||
| path for artifacts files with --manifest-path", | ||
| artifacts_path.display() | ||
| )) | ||
| } | ||
| }?; | ||
|
|
||
| async_std::task::block_on(async { | ||
| let url = self.extrinsic_opts.url_to_string(); | ||
| let client = OnlineClient::from_url(url.clone()).await?; | ||
| if let Some(code_removed) = self | ||
| .remove_code( | ||
| &client, | ||
| sp_core::H256(final_code_hash), | ||
| &signer, | ||
| &transcoder, | ||
| ) | ||
| .await? | ||
| { | ||
| let remove_result = code_removed.code_hash; | ||
|
|
||
| if self.output_json { | ||
| println!("{}", &remove_result); | ||
| } else { | ||
| name_value_println!("Code hash", format!("{remove_result:?}")); | ||
| } | ||
| Result::<(), ErrorVariant>::Ok(()) | ||
| } else { | ||
| let error_code_hash = hex::encode(final_code_hash); | ||
| Err(anyhow::anyhow!( | ||
| "Error removing the code for the supplied code hash: {}", | ||
| error_code_hash | ||
| ) | ||
| .into()) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| async fn remove_code( | ||
| &self, | ||
| client: &Client, | ||
| code_hash: CodeHash, | ||
| signer: &PairSigner, | ||
| transcoder: &ContractMessageTranscoder, | ||
| ) -> Result<Option<CodeRemoved>, ErrorVariant> { | ||
| let call = api::tx() | ||
| .contracts() | ||
| .remove_code(sp_core::H256(code_hash.0)); | ||
|
|
||
| let result = submit_extrinsic(client, &call, signer).await?; | ||
| let display_events = | ||
| DisplayEvents::from_events(&result, Some(transcoder), &client.metadata())?; | ||
|
|
||
| let output = if self.output_json { | ||
| display_events.to_json()? | ||
| } else { | ||
| let token_metadata = TokenMetadata::query(client).await?; | ||
| display_events | ||
| .display_events(self.extrinsic_opts.verbosity()?, &token_metadata)? | ||
| }; | ||
| println!("{output}"); | ||
| let code_removed = result.find_first::<CodeRemoved>()?; | ||
| Ok(code_removed) | ||
| } | ||
| } |
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
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
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.