-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Feliciss
committed
Aug 22, 2024
1 parent
7872e3e
commit cd790bc
Showing
3 changed files
with
49 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters