Skip to content

Commit

Permalink
[gh-2478] add tx submit command. (#2479)
Browse files Browse the repository at this point in the history
* [gh-2478] add tx submit command.

* [gh-2478] complete tx submit command and add tests.

* [gh-2478] fix module not found issue.

---------

Co-authored-by: Feliciss <10203-feliciss@users.noreply.0xacab.org>
  • Loading branch information
feliciss and Feliciss authored Aug 23, 2024
1 parent 88d81d1 commit 49f5a7b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/rooch/src/commands/transaction/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod build;
pub mod get_transactions_by_hash;
pub mod get_transactions_by_order;
pub mod sign;
pub mod submit;
63 changes: 63 additions & 0 deletions crates/rooch/src/commands/transaction/commands/submit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

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

use crate::cli_types::{CommandAction, TransactionOptions, WalletContextOptions};
use async_trait::async_trait;
use rooch_rpc_api::jsonrpc_types::ExecuteTransactionResponseView;
use rooch_types::{
error::{RoochError, RoochResult},
transaction::RoochTransaction,
};

/// Get transactions by order
#[derive(Debug, clap::Parser)]
pub struct SubmitCommand {
/// Transaction data hex to be used for submitting
#[clap(long)]
signed_tx_hex: Option<String>,

#[clap(flatten)]
tx_options: TransactionOptions,

#[clap(flatten)]
context: WalletContextOptions,

/// File location for the file being read
#[clap(long)]
file_location: Option<String>,
}

#[async_trait]
impl CommandAction<ExecuteTransactionResponseView> for SubmitCommand {
async fn execute(self) -> RoochResult<ExecuteTransactionResponseView> {
let context = self.context.build()?;
let submitted_tx;

if let Some(file_location) = self.file_location {
let mut file = File::open(file_location)?;
let mut signed_tx = Vec::new();
file.read_to_end(&mut signed_tx)?;
let tx: RoochTransaction = bcs::from_bytes(&signed_tx)
.map_err(|_| RoochError::BcsError(format!("Invalid signed tx: {:?}", signed_tx)))?;
submitted_tx = context.execute(tx).await?;
} else if let Some(signed_tx_hex) = self.signed_tx_hex {
let signed_tx = hex::decode(signed_tx_hex.clone()).map_err(|_| {
RoochError::CommandArgumentError(format!(
"Invalid signed transaction hex: {}",
signed_tx_hex
))
})?;
let tx: RoochTransaction = bcs::from_bytes(&signed_tx)
.map_err(|_| RoochError::BcsError(format!("Invalid signed tx: {:?}", signed_tx)))?;
submitted_tx = context.execute(tx).await?;
} else {
return Err(RoochError::CommandArgumentError(
"Argument --file-location or --signed-tx-hex are not provided".to_owned(),
));
}

Ok(submitted_tx)
}
}
3 changes: 3 additions & 0 deletions crates/rooch/src/commands/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::cli_types::CommandAction;
use crate::commands::transaction::commands::{
build::BuildCommand, get_transactions_by_hash::GetTransactionsByHashCommand,
get_transactions_by_order::GetTransactionsByOrderCommand, sign::SignCommand,
submit::SubmitCommand,
};
use async_trait::async_trait;
use clap::{Parser, Subcommand};
Expand All @@ -27,6 +28,7 @@ impl CommandAction<String> for Transaction {
TransactionCommand::GetTransactionsByHash(cmd) => cmd.execute_serialized().await,
TransactionCommand::Build(cmd) => cmd.execute_serialized().await,
TransactionCommand::Sign(cmd) => cmd.execute_serialized().await,
TransactionCommand::Submit(cmd) => cmd.execute_serialized().await,
}
}
}
Expand All @@ -37,4 +39,5 @@ pub enum TransactionCommand {
GetTransactionsByOrder(GetTransactionsByOrderCommand),
GetTransactionsByHash(GetTransactionsByHashCommand),
Sign(SignCommand),
Submit(SubmitCommand),
}
2 changes: 2 additions & 0 deletions crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Feature: Rooch CLI integration tests
Then assert: "'{{$.transaction[-1]}}' not_contains error"
Then cmd: "transaction sign --tx-hex {{$.transaction[-1]}} --json"
Then assert: "'{{$.transaction[-1]}}' not_contains error"
Then cmd: "transaction submit --signed-tx-hex {{$.transaction[-1]}}"
Then assert: "{{$.transaction[-1].execution_info.status.type}} == executed"

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

0 comments on commit 49f5a7b

Please sign in to comment.