Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export transaction #6111

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion applications/minotari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ use minotari_app_grpc::tls::certs::{generate_self_signed_certs, print_warning, w
use minotari_wallet::{
connectivity_service::WalletConnectivityInterface,
output_manager_service::{handle::OutputManagerHandle, UtxoSelectionCriteria},
transaction_service::handle::{TransactionEvent, TransactionServiceHandle},
transaction_service::{
handle::{TransactionEvent, TransactionServiceHandle},
storage::models::WalletTransaction,
},
TransactionStage,
WalletConfig,
WalletSqlite,
Expand Down Expand Up @@ -90,6 +93,7 @@ pub enum WalletCommand {
DiscoverPeer,
Whois,
ExportUtxos,
ExportTx,
ExportSpentUtxos,
CountUtxos,
SetBaseNode,
Expand Down Expand Up @@ -798,6 +802,21 @@ pub async fn command_runner(
},
Err(e) => eprintln!("ExportUtxos error! {}", e),
},
ExportTx(args) => match transaction_service.get_any_transaction(args.tx_id.into()).await {
Ok(Some(tx)) => {
if let Some(file) = args.output_file {
if let Err(e) = write_tx_to_csv_file(tx, file) {
eprintln!("ExportTx error! {}", e);
}
} else {
println!("Tx: {:?}", tx);
}
},
Ok(None) => {
eprintln!("ExportTx error!, No tx found ")
},
Err(e) => eprintln!("ExportTx error! {}", e),
},
ExportSpentUtxos(args) => match output_service.get_spent_outputs().await {
Ok(utxos) => {
let utxos: Vec<(WalletOutput, Commitment)> =
Expand Down Expand Up @@ -1078,6 +1097,16 @@ fn write_utxos_to_csv_file(utxos: Vec<(WalletOutput, Commitment)>, file_path: Pa
}
Ok(())
}

fn write_tx_to_csv_file(tx: WalletTransaction, file_path: PathBuf) -> Result<(), CommandError> {
let file = File::create(file_path).map_err(|e| CommandError::CSVFile(e.to_string()))?;
let mut csv_file = LineWriter::new(file);
let tx_string = serde_json::to_string(&tx).map_err(|e| CommandError::CSVFile(e.to_string()))?;
writeln!(csv_file, "{}", tx_string).map_err(|e| CommandError::CSVFile(e.to_string()))?;

Ok(())
}

#[allow(dead_code)]
fn write_json_file<P: AsRef<Path>, T: Serialize>(path: P, data: &T) -> Result<(), CommandError> {
fs::create_dir_all(path.as_ref().parent().unwrap()).map_err(|e| CommandError::JsonFile(e.to_string()))?;
Expand Down
8 changes: 8 additions & 0 deletions applications/minotari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub enum CliCommands {
DiscoverPeer(DiscoverPeerArgs),
Whois(WhoisArgs),
ExportUtxos(ExportUtxosArgs),
ExportTx(ExportTxArgs),
ExportSpentUtxos(ExportUtxosArgs),
CountUtxos,
SetBaseNode(SetBaseNodeArgs),
Expand Down Expand Up @@ -241,6 +242,13 @@ pub struct ExportUtxosArgs {
pub output_file: Option<PathBuf>,
}

#[derive(Debug, Args, Clone)]
pub struct ExportTxArgs {
pub tx_id: u64,
#[clap(short, long)]
pub output_file: Option<PathBuf>,
}

#[derive(Debug, Args, Clone)]
pub struct SetBaseNodeArgs {
pub public_key: UniPublicKey,
Expand Down
12 changes: 11 additions & 1 deletion applications/minotari_console_wallet/src/wallet_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ mod test {
--start-time now --message Stressing_it_a_bit...!_(from_Feeling-a-bit-Generous) \
5c4f2a4b3f3f84e047333218a84fd24f581a9d7e4f23b78e3714e9d174427d615e

export-tx 123456789 --output-file pie.txt

# End of script file
"
.to_string();
Expand All @@ -511,6 +513,7 @@ mod test {
let mut make_it_rain = false;
let mut coin_split = false;
let mut discover_peer = false;
let mut export_tx = false;
let mut whois = false;
for command in commands {
match command {
Expand All @@ -524,6 +527,11 @@ mod test {
CliCommands::DiscoverPeer(_) => discover_peer = true,
CliCommands::Whois(_) => whois = true,
CliCommands::ExportUtxos(_) => {},
CliCommands::ExportTx(args) => {
if args.tx_id == 123456789 && args.output_file == Some("pie.txt".into()) {
export_tx = true
}
},
CliCommands::ExportSpentUtxos(_) => {},
CliCommands::CountUtxos => {},
CliCommands::SetBaseNode(_) => {},
Expand All @@ -537,6 +545,8 @@ mod test {
CliCommands::CreateTlsCerts => {},
}
}
assert!(get_balance && send_tari && burn_tari && make_it_rain && coin_split && discover_peer && whois);
assert!(
get_balance && send_tari && burn_tari && make_it_rain && coin_split && discover_peer && whois && export_tx
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl From<InboundTransaction> for CompletedTransaction {
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum WalletTransaction {
PendingInbound(InboundTransaction),
Expand Down
Loading