Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
Signed-off-by: salaheldinsoliman <[email protected]>
  • Loading branch information
salaheldinsoliman committed Oct 9, 2024
1 parent 065513f commit 79b63ea
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 30 deletions.
171 changes: 146 additions & 25 deletions crates/iota/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ use fastcrypto::{
use iota_execution::verifier::VerifierOverrides;
use iota_json::IotaJsonValue;
use iota_json_rpc_types::{
Coin, DynamicFieldPage, IotaCoinMetadata, IotaData, IotaExecutionStatus, IotaObjectData,
IotaObjectDataOptions, IotaObjectResponse, IotaObjectResponseQuery, IotaParsedData,
IotaRawData, IotaTransactionBlockEffectsAPI, IotaTransactionBlockResponse,
IotaTransactionBlockResponseOptions,
Coin, DynamicFieldPage, IotaCoinMetadata, IotaData, IotaObjectData, IotaObjectDataOptions,
IotaObjectResponse, IotaObjectResponseQuery, IotaParsedData, IotaRawData,
IotaTransactionBlockResponse, IotaTransactionBlockResponseOptions,
};
use iota_keys::keystore::AccountKeystore;
use iota_move::build::resolve_lock_file_path;
Expand Down Expand Up @@ -85,7 +84,7 @@ mod profiler_tests;

#[macro_export]
macro_rules! serialize_or_execute {
($tx_data:expr, $serialize_unsigned:expr, $serialize_signed:expr, $context:expr, $result_variant:ident) => {{
($tx_data:expr, $serialize_unsigned:expr, $serialize_signed:expr, $context:expr, $result_variant:ident, $opts:expr) => {{
assert!(
!$serialize_unsigned || !$serialize_signed,
"Cannot specify both --serialize-unsigned-transaction and --serialize-signed-transaction"
Expand All @@ -107,15 +106,30 @@ macro_rules! serialize_or_execute {
IotaClientCommandResult::SerializedSignedTransaction(sender_signed_data)
} else {
let transaction = Transaction::new(sender_signed_data);
let response = $context.execute_transaction_may_fail(transaction).await?;
let effects = response.effects.as_ref().ok_or_else(|| {
anyhow!("Effects from IotaTransactionBlockResult should not be empty")
})?;
if matches!(effects.status(), IotaExecutionStatus::Failure { .. }) {
return Err(anyhow!(
"Error executing transaction: {:#?}",
effects.status()
));


let client = $context.get_client().await?;
let response: iota_json_rpc_types::IotaTransactionBlockResponse =
client
.quorum_driver_api()
.execute_transaction_block(
transaction,
$opts.unwrap_or_else(||
IotaTransactionBlockResponseOptions::new()
.with_effects()
.with_input()
.with_events()
.with_object_changes()
.with_balance_changes(),
),
Some(iota_types::quorum_driver_types::ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await?;


let errors: &Vec<String> = response.errors.as_ref();
if !errors.is_empty() {
return Err(anyhow!("Error executing transaction: {:#?}", errors));
}
IotaClientCommandResult::$result_variant(response)
}
Expand Down Expand Up @@ -178,7 +192,6 @@ pub enum IotaClientCommands {
#[clap(long, num_args(1..))]
args: Vec<IotaJsonValue>,
/// ID of the gas object for gas payment, in 20 bytes Hex string
#[clap(long)]
/// If not provided, a gas object with at least gas_budget value will be
/// selected
#[clap(long)]
Expand Down Expand Up @@ -208,6 +221,13 @@ pub enum IotaClientCommands {
/// <SIGNED_TX_BYTES>`.
#[clap(long, required = false)]
serialize_signed_transaction: bool,

/// Select which fields of the response to display.
/// If not provided, all fields are displayed.
/// The fields are: effects, input, events, object_changes,
/// balance_changes.
#[clap(long, required = false, value_delimiter = ',', num_args = 0.., value_parser = parse_emit_opts)]
emit: Vec<EmitOption>,
},

/// Query the chain identifier from the rpc endpoint.
Expand Down Expand Up @@ -863,6 +883,48 @@ pub enum IotaClientCommands {
},
}

/// Custom parser for emit options.
/*fn parse_emit_opts(s: &str) -> Result<String, String> {
// Validate the input if necessary
// For instance, if you want to restrict to specific fields, add checks here.
match s {
"effects" | "input" | "events" | "object_changes" | "balance_changes" => Ok(s.to_string()),
_ => Err(format!("Invalid option: {}", s)), // Return an error if invalid
}
}*/

#[derive(Clone)]
pub enum EmitOption {
Effects,
Input,
Events,
ObjectChanges,
BalanceChanges,
}


/// Converts a string into the corresponding `EmitOption` enum.
///
/// # Arguments
/// * `s` - A string representing an option.
///
/// # Returns
/// * `Ok(EmitOption)` if the string is valid.
/// * `Err(String)` if the string is invalid.
///
/// Valid options: "effects", "input", "events", "object_changes", "balance_changes".
fn parse_emit_opts(s: &str) -> Result<EmitOption, String> {
match s {
"effects" => Ok(EmitOption::Effects),
"input" => Ok(EmitOption::Input),
"events" => Ok(EmitOption::Events),
"object_changes" => Ok(EmitOption::ObjectChanges),
"balance_changes" => Ok(EmitOption::BalanceChanges),
_ => Err(format!("Invalid option: {}", s)),
}
}


#[derive(serde::Deserialize)]
struct FaucetResponse {
error: Option<String>,
Expand Down Expand Up @@ -1099,7 +1161,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Upgrade
Upgrade,
None
)
}
IotaClientCommands::Publish {
Expand Down Expand Up @@ -1155,7 +1218,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Publish
Publish,
None
)
}

Expand Down Expand Up @@ -1266,6 +1330,7 @@ impl IotaClientCommands {
args,
serialize_unsigned_transaction,
serialize_signed_transaction,
emit,
} => {
let tx_data = construct_move_call_transaction(
package, &module, &function, type_args, gas, gas_budget, gas_price, args,
Expand All @@ -1277,7 +1342,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Call
Call,
Some(opts_from_cli(emit))
)
}

Expand All @@ -1301,7 +1367,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Transfer
Transfer,
None
)
}

Expand All @@ -1325,7 +1392,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
TransferIota
TransferIota,
None
)
}

Expand Down Expand Up @@ -1370,7 +1438,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Pay
Pay,
None
)
}

Expand Down Expand Up @@ -1414,7 +1483,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
PayIota
PayIota,
None
)
}

Expand Down Expand Up @@ -1442,7 +1512,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
PayAllIota
PayAllIota,
None
)
}

Expand Down Expand Up @@ -1582,7 +1653,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
SplitCoin
SplitCoin,
None
)
}
IotaClientCommands::MergeCoin {
Expand All @@ -1604,7 +1676,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
MergeCoin
MergeCoin,
None
)
}
IotaClientCommands::Switch { address, env } => {
Expand Down Expand Up @@ -2676,3 +2749,51 @@ fn format_balance(

format!("{whole}.{fractional}{suffix}")
}

/*fn opts_from_cli(opts: Vec<String>) -> IotaTransactionBlockResponseOptions {
if opts.is_empty() {
return IotaTransactionBlockResponseOptions::new()
.with_effects()
.with_input()
.with_events()
.with_object_changes()
.with_balance_changes();
}
let mut options = IotaTransactionBlockResponseOptions::new();
for opt in opts {
match opt.as_str() {
"input" => options.show_input = true,
"events" => options.show_events = true,
"object_changes" => options.show_object_changes = true,
"balance_changes" => options.show_balance_changes = true,
"effects" => options.show_effects = true,
_ => {}
}
}
options
}*/

fn opts_from_cli(opts: Vec<EmitOption>) -> IotaTransactionBlockResponseOptions {
if opts.is_empty() {
return IotaTransactionBlockResponseOptions::new()
.with_effects()
.with_input()
.with_events()
.with_object_changes()
.with_balance_changes();
}

let mut options = IotaTransactionBlockResponseOptions::new();
for opt in opts {
match opt {
EmitOption::Input => options.show_input = true,
EmitOption::Events => options.show_events = true,
EmitOption::ObjectChanges => options.show_object_changes = true,
EmitOption::BalanceChanges => options.show_balance_changes = true,
EmitOption::Effects => options.show_effects = true,
}
}
options
}

4 changes: 2 additions & 2 deletions crates/iota/src/client_ptb/ptb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ impl PTB {
);

if program_metadata.serialize_unsigned_set {
serialize_or_execute!(tx_data, true, false, context, PTB).print(true);
serialize_or_execute!(tx_data, true, false, context, PTB, None).print(true);
return Ok(());
}

if program_metadata.serialize_signed_set {
serialize_or_execute!(tx_data, false, true, context, PTB).print(true);
serialize_or_execute!(tx_data, false, true, context, PTB, None).print(true);
return Ok(());
}

Expand Down
Loading

0 comments on commit 79b63ea

Please sign in to comment.