Skip to content

Commit

Permalink
feat(devx): Add filter flag to Iota CLI
Browse files Browse the repository at this point in the history
Signed-off-by: salaheldinsoliman <[email protected]>
  • Loading branch information
salaheldinsoliman committed Sep 18, 2024
1 parent f7a61eb commit f1bee6d
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/iota-json-rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ serde_json.workspace = true
serde_with.workspace = true
tabled.workspace = true
tracing.workspace = true
clap = { version = "4.0", features = ["derive"] }


move-binary-format.workspace = true
move-bytecode-utils.workspace = true
Expand Down
40 changes: 37 additions & 3 deletions crates/iota-json-rpc-types/src/iota_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use tabled::{
builder::Builder as TableBuilder,
settings::{style::HorizontalLine, Panel as TablePanel, Style as TableStyle},
};
use clap::Parser;

use crate::{
balance_changes::BalanceChange, iota_transaction::GenericSignature::Signature,
Expand Down Expand Up @@ -96,20 +97,34 @@ pub type TransactionBlocksPage = Page<IotaTransactionBlockResponse, TransactionD
rename = "TransactionBlockResponseOptions",
default
)]
#[derive(Parser)]
pub struct IotaTransactionBlockResponseOptions {
/// Whether to show transaction input data. Default to be False
#[clap(long, required = false)]
pub show_input: bool,
/// Whether to show bcs-encoded transaction input data

/// Whether to show BCS-encoded transaction input data
#[clap(long, required = false)]
pub show_raw_input: bool,

/// Whether to show transaction effects. Default to be False
#[clap(long, required = false)]
pub show_effects: bool,

/// Whether to show transaction events. Default to be False
#[clap(long, required = false)]
pub show_events: bool,
/// Whether to show object_changes. Default to be False

/// Whether to show object changes. Default to be False
#[clap(long, required = false)]
pub show_object_changes: bool,
/// Whether to show balance_changes. Default to be False

/// Whether to show balance changes. Default to be False
#[clap(long, required = false)]
pub show_balance_changes: bool,

/// Whether to show raw transaction effects. Default to be False
#[clap(long, required = false)]
pub show_raw_effects: bool,
}

Expand Down Expand Up @@ -198,6 +213,25 @@ impl IotaTransactionBlockResponseOptions {
pub fn only_digest(&self) -> bool {
self == &Self::default()
}

pub fn from_cli(mut self, opts: Vec<String>) -> Self {
if opts.contains(&"input".to_string()) {
self.show_input = true;
}
if opts.contains(&"effects".to_string()) {
self.show_effects = true;
}
if opts.contains(&"events".to_string()) {
self.show_events = true;
}
if opts.contains(&"object_changes".to_string()) {
self.show_object_changes = true;
}
if opts.contains(&"balance_changes".to_string()) {
self.show_balance_changes = true;
}
self
}
}

#[serde_as]
Expand Down
11 changes: 4 additions & 7 deletions crates/iota-sdk/src/wallet_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ impl WalletContext {
pub async fn execute_transaction_must_succeed(
&self,
tx: Transaction,
opts: Vec<String>,
) -> IotaTransactionBlockResponse {
tracing::debug!("Executing transaction: {:?}", tx);
let response = self.execute_transaction_may_fail(tx).await.unwrap();
let response = self.execute_transaction_may_fail(tx, Vec::new()).await.unwrap();
assert!(
response.status_ok().unwrap(),
"Transaction failed: {:?}",
Expand All @@ -317,18 +318,14 @@ impl WalletContext {
pub async fn execute_transaction_may_fail(
&self,
tx: Transaction,
opts: Vec<String>,
) -> anyhow::Result<IotaTransactionBlockResponse> {
let client = self.get_client().await?;
Ok(client
.quorum_driver_api()
.execute_transaction_block(
tx,
IotaTransactionBlockResponseOptions::new()
.with_effects()
.with_input()
.with_events()
.with_object_changes()
.with_balance_changes(),
IotaTransactionBlockResponseOptions::new().from_cli(opts),
Some(iota_types::quorum_driver_types::ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await?)
Expand Down
14 changes: 7 additions & 7 deletions crates/iota-test-transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ pub async fn publish_package(context: &WalletContext, path: PathBuf) -> ObjectRe
.publish(path)
.build(),
);
let resp = context.execute_transaction_must_succeed(txn).await;
let resp = context.execute_transaction_must_succeed(txn, Vec::new()).await;
get_new_package_obj_from_response(&resp).unwrap()
}

Expand All @@ -542,7 +542,7 @@ pub async fn publish_basics_package(context: &WalletContext) -> ObjectRef {
.publish_examples("basics")
.build(),
);
let resp = context.execute_transaction_must_succeed(txn).await;
let resp = context.execute_transaction_must_succeed(txn, Vec::new()).await;
get_new_package_obj_from_response(&resp).unwrap()
}

Expand All @@ -560,7 +560,7 @@ pub async fn publish_basics_package_and_make_counter(
.build(),
);
let resp = context
.execute_transaction_must_succeed(counter_creation_txn)
.execute_transaction_must_succeed(counter_creation_txn, Vec::new())
.await;
let counter_ref = resp
.effects
Expand Down Expand Up @@ -599,7 +599,7 @@ pub async fn increment_counter(
.call_counter_increment(package_id, counter_id, initial_shared_version)
.build(),
);
context.execute_transaction_must_succeed(txn).await
context.execute_transaction_must_succeed(txn, Vec::new()).await
}

/// Executes a transaction to publish the `nfts` package and returns the package
Expand All @@ -615,7 +615,7 @@ pub async fn publish_nfts_package(
.publish_examples("nfts")
.build(),
);
let resp = context.execute_transaction_must_succeed(txn).await;
let resp = context.execute_transaction_must_succeed(txn, Vec::new()).await;
let package_id = get_new_package_obj_from_response(&resp).unwrap().0;
(package_id, gas_id, resp.digest)
}
Expand All @@ -635,7 +635,7 @@ pub async fn create_devnet_nft(
.call_nft_create(package_id)
.build(),
);
let resp = context.execute_transaction_must_succeed(txn).await;
let resp = context.execute_transaction_must_succeed(txn, Vec::new()).await;

let object_id = resp
.effects
Expand Down Expand Up @@ -668,5 +668,5 @@ pub async fn delete_devnet_nft(
.call_nft_delete(package_id, nft_to_delete)
.build(),
);
context.execute_transaction_must_succeed(txn).await
context.execute_transaction_must_succeed(txn, Vec::new()).await
}
46 changes: 31 additions & 15 deletions crates/iota/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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,7 +107,7 @@ 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 response = $context.execute_transaction_may_fail(transaction, $opts).await?;
let effects = response.effects.as_ref().ok_or_else(|| {
anyhow!("Effects from IotaTransactionBlockResult should not be empty")
})?;
Expand Down Expand Up @@ -178,7 +178,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 +207,12 @@ 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: input, effects, events, object_changes, balance_changes.
#[clap(long, required = false)]
emit: Vec<String>,
},

/// Query the chain identifier from the rpc endpoint.
Expand Down Expand Up @@ -1099,7 +1104,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Upgrade
Upgrade,
Vec::new()
)
}
IotaClientCommands::Publish {
Expand Down Expand Up @@ -1155,7 +1161,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Publish
Publish,
Vec::new()
)
}

Expand Down Expand Up @@ -1266,6 +1273,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 +1285,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Call
Call,
emit
)
}

Expand All @@ -1301,7 +1310,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Transfer
Transfer,
Vec::new()
)
}

Expand All @@ -1325,7 +1335,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
TransferIota
TransferIota,
Vec::new()
)
}

Expand Down Expand Up @@ -1370,7 +1381,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
Pay
Pay,
Vec::new()
)
}

Expand Down Expand Up @@ -1414,7 +1426,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
PayIota
PayIota,
Vec::new()
)
}

Expand Down Expand Up @@ -1442,7 +1455,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
PayAllIota
PayAllIota,
Vec::new()
)
}

Expand Down Expand Up @@ -1582,7 +1596,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
SplitCoin
SplitCoin,
Vec::new()
)
}
IotaClientCommands::MergeCoin {
Expand All @@ -1604,7 +1619,8 @@ impl IotaClientCommands {
serialize_unsigned_transaction,
serialize_signed_transaction,
context,
MergeCoin
MergeCoin,
Vec::new()
)
}
IotaClientCommands::Switch { address, env } => {
Expand Down Expand Up @@ -1660,7 +1676,7 @@ impl IotaClientCommands {
}
let transaction = Transaction::from_generic_sig_data(data, sigs);

let response = context.execute_transaction_may_fail(transaction).await?;
let response = context.execute_transaction_may_fail(transaction, Vec::new()).await?;
IotaClientCommandResult::ExecuteSignedTx(response)
}
IotaClientCommands::ExecuteCombinedSignedTx { signed_tx_bytes } => {
Expand All @@ -1671,7 +1687,7 @@ impl IotaClientCommands {
.map_err(|_| anyhow!("Invalid Base64 encoding"))?
).map_err(|_| anyhow!("Failed to parse SenderSignedData bytes, check if it matches the output of iota client commands with --serialize-signed-transaction"))?;
let transaction = Envelope::<SenderSignedData, EmptySignInfo>::new(data);
let response = context.execute_transaction_may_fail(transaction).await?;
let response = context.execute_transaction_may_fail(transaction, Vec::new()).await?;
IotaClientCommandResult::ExecuteSignedTx(response)
}
IotaClientCommands::NewEnv { alias, rpc, ws } => {
Expand Down
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, Vec::new()).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, Vec::new()).print(true);
return Ok(());
}

Expand Down
Loading

0 comments on commit f1bee6d

Please sign in to comment.