Skip to content
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

[CLI] DRY out repetitive pattern for profile/submit txn #7693

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
common::types::{account_address_from_public_key, CliError, CliTypedResult, PromptOptions},
common::types::{
account_address_from_public_key, CliError, CliTypedResult, PromptOptions,
TransactionOptions, TransactionSummary,
},
config::GlobalConfig,
CliResult,
};
Expand All @@ -12,7 +15,10 @@ use aptos_keygen::KeyGen;
use aptos_logger::{debug, Level};
use aptos_rest_client::{aptos_api_types::HashValue, Account, Client, State};
use aptos_telemetry::service::telemetry_is_disabled;
use aptos_types::{chain_id::ChainId, transaction::authenticator::AuthenticationKey};
use aptos_types::{
chain_id::ChainId,
transaction::{authenticator::AuthenticationKey, TransactionPayload},
};
use itertools::Itertools;
use move_core_types::account_address::AccountAddress;
use reqwest::Url;
Expand Down Expand Up @@ -443,3 +449,20 @@ pub fn start_logger() {
logger.channel_size(1000).is_async(false).level(Level::Warn);
logger.build();
}

/// For transaction payload and options, either get gas profile or submit for execution.
pub async fn profile_or_submit(
Copy link
Contributor

Choose a reason for hiding this comment

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

Potentially makes sense for this to be a method on TransactionOptons

payload: TransactionPayload,
txn_options_ref: &TransactionOptions,
) -> CliTypedResult<TransactionSummary> {
// Profile gas if needed.
if txn_options_ref.profile_gas {
txn_options_ref.profile_gas(payload).await
} else {
// Otherwise submit the transaction.
txn_options_ref
.submit_transaction(payload)
.await
.map(TransactionSummary::from)
}
}
29 changes: 4 additions & 25 deletions crates/aptos/src/move_tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
},
utils::{
check_if_file_exists, create_dir_if_not_exist, dir_default_to_current,
prompt_yes_with_override, write_to_file,
profile_or_submit, prompt_yes_with_override, write_to_file,
},
},
governance::CompileScriptFunction,
Expand Down Expand Up @@ -744,14 +744,7 @@ impl CliCommand<TransactionSummary> for PublishPackage {
MAX_PUBLISH_PACKAGE_SIZE, size
)));
}
if txn_options.profile_gas {
txn_options.profile_gas(payload).await
} else {
txn_options
.submit_transaction(payload)
.await
.map(TransactionSummary::from)
}
profile_or_submit(payload, &txn_options).await
}
}

Expand Down Expand Up @@ -1144,14 +1137,7 @@ impl CliCommand<TransactionSummary> for RunFunction {
args,
));

if self.txn_options.profile_gas {
self.txn_options.profile_gas(payload).await
} else {
self.txn_options
.submit_transaction(payload)
.await
.map(TransactionSummary::from)
}
profile_or_submit(payload, &self.txn_options).await
}
}

Expand Down Expand Up @@ -1255,14 +1241,7 @@ impl CliCommand<TransactionSummary> for RunScript {

let payload = TransactionPayload::Script(Script::new(bytecode, type_args, args));

if self.txn_options.profile_gas {
self.txn_options.profile_gas(payload).await
} else {
self.txn_options
.submit_transaction(payload)
.await
.map(TransactionSummary::from)
}
profile_or_submit(payload, &self.txn_options).await
}
}

Expand Down