Skip to content

Commit

Permalink
[gh-2478] complete tx submit command and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Aug 23, 2024
1 parent 3d9ce96 commit 2105106
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
68 changes: 46 additions & 22 deletions crates/rooch/src/commands/transaction/commands/submit.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::cli_types::{CommandAction, WalletContextOptions};
use std::{fs::File, io::Read};

use crate::cli_types::{CommandAction, TransactionOptions, WalletContextOptions};
use async_trait::async_trait;
use rooch_rpc_api::jsonrpc_types::TransactionWithInfoPageView;
use rooch_types::error::RoochResult;
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's hash
#[clap(long)]
pub cursor: Option<u64>,

/// Transaction data hex to be used for submitting
#[clap(long)]
pub limit: Option<u64>,
signed_tx_hex: Option<String>,

/// descending order
#[clap(short = 'd', long)]
descending_order: Option<bool>,
#[clap(flatten)]
tx_options: TransactionOptions,

#[clap(flatten)]
pub(crate) context_options: WalletContextOptions,
context: WalletContextOptions,

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

#[async_trait]
impl CommandAction<TransactionWithInfoPageView> for SubmitCommand {
async fn execute(self) -> RoochResult<TransactionWithInfoPageView> {
let client = self.context_options.build()?.get_client().await?;

let resp = client
.rooch
.get_transactions_by_order(self.cursor, self.limit, self.descending_order)
.await?;

Ok(resp)
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: 0 additions & 3 deletions crates/rooch/src/commands/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
use crate::cli_types::CommandAction;
use crate::commands::transaction::commands::{
build::BuildCommand, get_transactions_by_hash::GetTransactionsByHashCommand,
get_transactions_by_order::GetTransactionsByOrderCommand, sign::SignCommand,
get_transactions_by_order::GetTransactionsByOrderCommand,
get_transactions_by_hash::GetTransactionsByHashCommand,
get_transactions_by_order::GetTransactionsByOrderCommand, submit::SubmitCommand,
};
use async_trait::async_trait;
Expand Down
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 2105106

Please sign in to comment.