Skip to content

Commit

Permalink
[gh-2478] add tx submit command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Aug 22, 2024
1 parent 7872e3e commit 7a939a8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 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)
}
}
1 change: 1 addition & 0 deletions crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Feature: Rooch CLI integration tests
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"
Then cmd: "transaction submit --signed-tx-hex {{$.transaction[-1]}}"

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

0 comments on commit 7a939a8

Please sign in to comment.