Skip to content

Commit

Permalink
[gh-2471] add tx build command. (#2472)
Browse files Browse the repository at this point in the history
* [gh-2471] add tx build command.

* [gh-2471] add args to the build command.

* [gh-2471] add hex output to build command.

* [gh-2471] add a test case of transaction build command.

* [gh-2471] resolve issues arisen in the comments.

* [gh-2471] update test.

* [gh-2471] fix syntax errors.

* [gh-2471] resolve issues arisen in the comments.

* [gh-2471] fix test.

* [gh-2471] fix lint error.

---------

Co-authored-by: Feliciss <10203-feliciss@users.noreply.0xacab.org>
  • Loading branch information
feliciss and Feliciss committed Aug 22, 2024
1 parent 1158cc0 commit e725ba7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
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.

1 change: 1 addition & 0 deletions crates/rooch-rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { workspace = true }
jsonrpsee = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
hex = { workspace = true }

move-core-types = { workspace = true }

Expand Down
107 changes: 107 additions & 0 deletions crates/rooch/src/commands/transaction/commands/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use std::{fs::File, io::Write};

use crate::cli_types::{CommandAction, TransactionOptions, WalletContextOptions};
use anyhow::Result;
use async_trait::async_trait;
use move_command_line_common::types::ParsedStructType;
use move_core_types::language_storage::TypeTag;
use moveos_types::transaction::MoveAction;
use rooch_types::{
error::RoochResult,
function_arg::{parse_function_arg, FunctionArg, ParsedFunctionId},
};

/// Get transactions by order
#[derive(Debug, clap::Parser)]
pub struct BuildCommand {
/// Function name as `<ADDRESS>::<MODULE_ID>::<FUNCTION_NAME>`
/// Example: `0x42::message::set_message`, `rooch_framework::empty::empty`
#[clap(long, required = true)]
pub function: ParsedFunctionId,

/// TypeTag arguments separated by spaces.
///
/// Example: `0x1::M::T1 0x1::M::T2 rooch_framework::empty::Empty`
#[clap(
long = "type-args",
value_parser=ParsedStructType::parse,
)]
pub type_args: Vec<ParsedStructType>,

/// Arguments combined with their type separated by spaces.
///
/// Supported types [u8, u16, u32, u64, u128, u256, bool, object_id, string, address, vector<inner_type>]
///
/// Example: `address:0x1 bool:true u8:0 u256:1234 'vector<u32>:a,b,c,d'`
/// address and uint can be written in short form like `@0x1 1u8 4123u256`.
#[clap(long = "args", value_parser=parse_function_arg)]
pub args: Vec<FunctionArg>,

#[clap(flatten)]
tx_options: TransactionOptions,

#[clap(flatten)]
context: WalletContextOptions,

/// File destination for the file being written
#[clap(long)]
file_destination: Option<String>,

/// Return command outputs in json format
#[clap(long, default_value = "false")]
json: bool,
}

#[async_trait]
impl CommandAction<Option<String>> for BuildCommand {
async fn execute(self) -> RoochResult<Option<String>> {
let context = self.context.build()?;
let address_mapping = context.address_mapping();
let sender = context.resolve_address(self.tx_options.sender)?.into();
let max_gas_amount = self.tx_options.max_gas_amount;

let function_id = self.function.into_function_id(&address_mapping)?;
let args = self
.args
.into_iter()
.map(|arg| arg.into_bytes(&address_mapping))
.collect::<Result<Vec<_>>>()?;
let type_args = self
.type_args
.into_iter()
.map(|tag| {
Ok(TypeTag::Struct(Box::new(
tag.into_struct_tag(&address_mapping)?,
)))
})
.collect::<Result<Vec<_>>>()?;
let action = MoveAction::new_function_call(function_id, type_args, args);

let tx_data = context
.build_tx_data(sender, action, max_gas_amount)
.await?;

if let Some(file_destination) = self.file_destination {
let mut file = File::create(file_destination)?;
file.write_all(&tx_data.encode())?;
println!("Write transaction hex succeeded in the destination");

Ok(None)
} else {
let tx_data_hex = hex::encode(tx_data.encode());
if self.json {
Ok(Some(tx_data_hex))
} else {
println!(
"Build transaction succeeded with the transaction hex [{}]",
tx_data_hex
);

Ok(None)
}
}
}
}
1 change: 1 addition & 0 deletions crates/rooch/src/commands/transaction/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

pub mod build;
pub mod get_transactions_by_hash;
pub mod get_transactions_by_order;
4 changes: 3 additions & 1 deletion crates/rooch/src/commands/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::cli_types::CommandAction;
use crate::commands::transaction::commands::{
get_transactions_by_hash::GetTransactionsByHashCommand,
build::BuildCommand, get_transactions_by_hash::GetTransactionsByHashCommand,
get_transactions_by_order::GetTransactionsByOrderCommand,
};
use async_trait::async_trait;
Expand All @@ -25,12 +25,14 @@ impl CommandAction<String> for Transaction {
match self.cmd {
TransactionCommand::GetTransactionsByOrder(cmd) => cmd.execute_serialized().await,
TransactionCommand::GetTransactionsByHash(cmd) => cmd.execute_serialized().await,
TransactionCommand::Build(cmd) => cmd.execute_serialized().await,
}
}
}

#[derive(Subcommand)]
pub enum TransactionCommand {
Build(BuildCommand),
GetTransactionsByOrder(GetTransactionsByOrderCommand),
GetTransactionsByHash(GetTransactionsByHashCommand),
}
1 change: 1 addition & 0 deletions crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Feature: Rooch CLI integration tests
# transaction
Then cmd: "transaction get-transactions-by-order --cursor 0 --limit 1 --descending-order false"
Then cmd: "transaction get-transactions-by-hash --hashes {{$.transaction[-1].data[0].execution_info.tx_hash}}"
Then cmd: "transaction build --function rooch_framework::empty::empty --json"

# alias tx for transaction
Then cmd: "tx get-transactions-by-order --cursor 1 --limit 2 --descending-order true"
Expand Down

0 comments on commit e725ba7

Please sign in to comment.