Skip to content

Commit

Permalink
DRY out repetitive pattern for profile/submit txn (aptos-labs#7693)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnoki authored and WGB5445 committed Apr 14, 2023
1 parent c2cf2cd commit 79f718f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
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(
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

0 comments on commit 79f718f

Please sign in to comment.