Skip to content

Commit

Permalink
feat: print strata explorer links if configured (#453)
Browse files Browse the repository at this point in the history
* feat: print strata explorer links if configured

* fix: don't rename the file name for bitcoin explorer
  • Loading branch information
Zk2u authored Nov 5, 2024
1 parent 59a60f3 commit 603442d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
4 changes: 2 additions & 2 deletions bin/strata-cli/src/cmd/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
recovery::DescriptorRecovery,
seed::Seed,
settings::Settings,
signet::{get_fee_rate, log_fee_rate, print_explorer_url, SignetWallet},
signet::{get_fee_rate, log_fee_rate, print_bitcoin_explorer_url, SignetWallet},
strata::StrataWallet,
taproot::{ExtractP2trPubkey, NotTaprootAddress},
};
Expand Down Expand Up @@ -148,7 +148,7 @@ pub async fn deposit(
.expect("successful broadcast");
let txid = tx.compute_txid();
pb.finish_with_message(format!("Transaction {} broadcasted", txid));
let _ = print_explorer_url(&txid, &term, &settings);
let _ = print_bitcoin_explorer_url(&txid, &term, &settings);
let _ = term.write_line(&format!(
"Expect transaction confirmation in ~{:?}. Funds will take longer than this to be available on Strata.",
SIGNET_BLOCK_TIME
Expand Down
10 changes: 6 additions & 4 deletions bin/strata-cli/src/cmd/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{
constants::SATS_TO_WEI,
seed::Seed,
settings::Settings,
signet::{get_fee_rate, log_fee_rate, print_explorer_url, SignetWallet},
strata::StrataWallet,
signet::{get_fee_rate, log_fee_rate, print_bitcoin_explorer_url, SignetWallet},
strata::{print_strata_explorer_url, StrataWallet},
};

/// Drains the internal wallet to the provided
Expand Down Expand Up @@ -83,7 +83,7 @@ pub async fn drain(
l1w.sign(&mut psbt, Default::default()).unwrap();
let tx = psbt.extract_tx().expect("fully signed tx");
settings.signet_backend.broadcast_tx(&tx).await.unwrap();
let _ = print_explorer_url(&tx.compute_txid(), &term, &settings);
let _ = print_bitcoin_explorer_url(&tx.compute_txid(), &term, &settings);
let _ = term.write_line(&format!("Drained signet wallet to {}", address,));
}

Expand All @@ -108,7 +108,9 @@ pub async fn drain(

let tx = l2w.transaction_request().to(address).value(max_send_amount);

let _ = l2w.send_transaction(tx).await.unwrap();
let res = l2w.send_transaction(tx).await.unwrap();

let _ = print_strata_explorer_url(res.tx_hash(), &term, &settings);

let _ = term.write_line(&format!(
"Drained {} from Strata wallet to {}",
Expand Down
6 changes: 3 additions & 3 deletions bin/strata-cli/src/cmd/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
use crate::{
seed::Seed,
settings::Settings,
signet::{print_explorer_url, SignetWallet},
signet::{print_bitcoin_explorer_url, SignetWallet},
};

/// Request some bitcoin from the faucet
Expand Down Expand Up @@ -170,14 +170,14 @@ pub async fn faucet(args: FaucetArgs, seed: Seed, settings: Settings) {
if status == StatusCode::OK {
#[cfg(feature = "strata_faucet")]
if network_type == NetworkType::Signet {
let _ = print_explorer_url(
let _ = print_bitcoin_explorer_url(
&Txid::from_str(&body).expect("valid txid"),
&term,
&settings,
);
}
#[cfg(not(feature = "strata_faucet"))]
let _ = print_explorer_url(
let _ = print_bitcoin_explorer_url(
&Txid::from_str(&body).expect("valid txid"),
&term,
&settings,
Expand Down
8 changes: 4 additions & 4 deletions bin/strata-cli/src/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
net_type::{net_type_or_exit, NetworkType},
seed::Seed,
settings::Settings,
signet::{get_fee_rate, log_fee_rate, print_explorer_url, SignetWallet},
strata::StrataWallet,
signet::{get_fee_rate, log_fee_rate, print_bitcoin_explorer_url, SignetWallet},
strata::{print_strata_explorer_url, StrataWallet},
};

/// Send some bitcoin from the internal wallet.
Expand Down Expand Up @@ -72,7 +72,7 @@ pub async fn send(args: SendArgs, seed: Seed, settings: Settings) {
.broadcast_tx(&tx)
.await
.expect("successful broadcast");
let _ = print_explorer_url(&tx.compute_txid(), &term, &settings);
let _ = print_bitcoin_explorer_url(&tx.compute_txid(), &term, &settings);
}
NetworkType::Strata => {
let l2w = StrataWallet::new(&seed, &settings.strata_endpoint).expect("valid wallet");
Expand All @@ -84,7 +84,7 @@ pub async fn send(args: SendArgs, seed: Seed, settings: Settings) {
.send_transaction(tx)
.await
.expect("successful broadcast");
let _ = term.write_line(&format!("Transaction {} sent", res.tx_hash()));
let _ = print_strata_explorer_url(res.tx_hash(), &term, &settings);
}
};

Expand Down
6 changes: 3 additions & 3 deletions bin/strata-cli/src/cmd/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
seed::Seed,
settings::Settings,
signet::SignetWallet,
strata::StrataWallet,
strata::{print_strata_explorer_url, StrataWallet},
taproot::ExtractP2trPubkey,
};

Expand Down Expand Up @@ -69,6 +69,6 @@ pub async fn withdraw(args: WithdrawArgs, seed: Seed, settings: Settings) {
let pb = ProgressBar::new_spinner().with_message("Broadcasting transaction");
pb.enable_steady_tick(Duration::from_millis(100));
let res = l2w.send_transaction(tx).await.unwrap();
let txid = res.tx_hash();
pb.finish_with_message(format!("Broadcast successful. Txid: {}", txid));
pb.finish_with_message("Broadcast successful");
let _ = print_strata_explorer_url(res.tx_hash(), &term, &settings);
}
13 changes: 8 additions & 5 deletions bin/strata-cli/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub struct SettingsFromFile {
pub bitcoind_rpc_endpoint: Option<String>,
pub strata_endpoint: String,
pub faucet_endpoint: String,
pub mempool_endpoint: String,
pub mempool_endpoint: Option<String>,
pub blockscout_endpoint: Option<String>,
pub bridge_pubkey: Option<Hex<[u8; 32]>>,
pub network: Option<Network>,
}
Expand All @@ -44,7 +45,8 @@ pub struct Settings {
pub faucet_endpoint: String,
pub bridge_musig2_pubkey: XOnlyPublicKey,
pub descriptor_db: PathBuf,
pub mempool_endpoint: String,
pub mempool_space_endpoint: Option<String>,
pub blockscout_endpoint: Option<String>,
pub bridge_strata_address: StrataAddress,
pub linux_seed_file: PathBuf,
pub network: Network,
Expand Down Expand Up @@ -106,7 +108,6 @@ impl Settings {
strata_endpoint: from_file.strata_endpoint,
data_dir: proj_dirs.data_dir().to_owned(),
faucet_endpoint: from_file.faucet_endpoint,
mempool_endpoint: from_file.mempool_endpoint,
bridge_musig2_pubkey: XOnlyPublicKey::from_slice(&match from_file.bridge_pubkey {
Some(key) => key.0,
None => {
Expand All @@ -116,12 +117,14 @@ impl Settings {
}
})
.expect("valid length"),
config_file: CONFIG_FILE.clone(),
network: from_file.network.unwrap_or(DEFAULT_NETWORK),
descriptor_db: descriptor_file,
mempool_space_endpoint: from_file.mempool_endpoint,
blockscout_endpoint: from_file.blockscout_endpoint,
bridge_strata_address: StrataAddress::from_str(BRIDGE_STRATA_ADDRESS)
.expect("valid strata address"),
linux_seed_file,
network: from_file.network.unwrap_or(DEFAULT_NETWORK),
config_file: CONFIG_FILE.clone(),
signet_backend: sync_backend,
})
}
Expand Down
17 changes: 12 additions & 5 deletions bin/strata-cli/src/signet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ pub async fn get_fee_rate(
}
}

pub fn print_explorer_url(txid: &Txid, term: &Term, settings: &Settings) -> Result<(), io::Error> {
term.write_line(&format!(
"View transaction at {}",
style(format!("{}/tx/{txid}", settings.mempool_endpoint)).blue()
))
pub fn print_bitcoin_explorer_url(
txid: &Txid,
term: &Term,
settings: &Settings,
) -> Result<(), io::Error> {
term.write_line(&match settings.mempool_space_endpoint {
Some(ref url) => format!(
"View transaction at {}",
style(format!("{url}/tx/{txid}")).blue()
),
None => format!("Transaction ID: {txid}"),
})
}

#[derive(Clone, Debug)]
Expand Down
23 changes: 21 additions & 2 deletions bin/strata-cli/src/strata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::ops::{Deref, DerefMut};
use std::{
io,
ops::{Deref, DerefMut},
};

use alloy::{
network::{Ethereum, EthereumWallet},
primitives::TxHash,
providers::{
fillers::{
BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller,
Expand All @@ -11,8 +15,23 @@ use alloy::{
},
transports::http::{Client, Http},
};
use console::{style, Term};

use crate::{seed::Seed, settings::Settings};

use crate::seed::Seed;
pub fn print_strata_explorer_url(
txid: &TxHash,
term: &Term,
settings: &Settings,
) -> Result<(), io::Error> {
term.write_line(&match settings.blockscout_endpoint {
Some(ref url) => format!(
"View transaction at {}",
style(format!("{url}/tx/{txid}")).blue()
),
None => format!("Transaction ID: {txid}"),
})
}

// alloy moment 💀
type Provider = FillProvider<
Expand Down

0 comments on commit 603442d

Please sign in to comment.