Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Handle HTTP request error
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Dec 30, 2023
1 parent 91e9347 commit 393cab6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cargo install atomicalsir
#### Download the pre-built binary
You can download the pre-build binary from our [GitHub release](https://github.com/hack-ink/subalfred/releases)

#### Build from source code
#### Build from source code (requires the nightly Rust)
To build from the source code, use the following commands:

```sh
Expand Down
86 changes: 53 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![feature(async_closure)]

// std
use std::{
fs::{self, File, OpenOptions},
future::Future,
io::{BufRead, BufReader, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
Expand Down Expand Up @@ -50,13 +53,23 @@ async fn main() -> Result<()> {

match strategy {
Strategy::AverageFirst =>
for _ in w.unconfirmed_count().await?..=12 {
for _ in loop_query(
async || query_unconfirmed_tx_count(&w.address).await,
"unconfirmed transaction count",
)
.await..=12
{
w.mine(max_fee, stash.as_deref(), electrumx.as_deref()).await?;

sleep = false;
},
Strategy::WalletFirst => 'inner: loop {
if w.unconfirmed_count().await? <= 12 {
if loop_query(
async || { query_unconfirmed_tx_count(&w.address) }.await,
"unconfirmed transaction count",
)
.await <= 12
{
w.mine(max_fee, stash.as_deref(), electrumx.as_deref()).await?;

sleep = false;
Expand Down Expand Up @@ -167,38 +180,8 @@ impl Wallet {
.unwrap_or_default()
}

async fn unconfirmed_count(&self) -> Result<u32> {
#[derive(Debug, Deserialize)]
struct Unspent {
unconfirmed_n_tx: u32,
}

tracing::info!("fee: {}", self.address);

let unconfirmed_count = reqwest::get(format!(
"https://api.blockcypher.com/v1/btc/main/addrs/{}?unspentOnly=true",
self.address
))
.await?
.json::<Unspent>()
.await?
.unconfirmed_n_tx;

tracing::info!("unconfirmed transaction count: {unconfirmed_count}");

Ok(unconfirmed_count)
}

async fn mine(&self, max_fee: u32, stash: Option<&str>, electrumx: Option<&str>) -> Result<()> {
let fee = loop {
if let Ok(f) = query_fee().await {
break f;
}

tracing::warn!("failed to query fee; retrying in 3 seconds");

thread::sleep(Duration::from_secs(3));
};
let fee = loop_query(query_fee, "fee").await;

tracing::info!("current priority fee: {fee} sat/vB");

Expand Down Expand Up @@ -345,6 +328,22 @@ fn execute(
Ok(())
}

async fn loop_query<F, Fut, T>(function: F, target: &str) -> T
where
F: Fn() -> Fut,
Fut: Future<Output = Result<T>>,
{
loop {
if let Ok(f) = function().await {
return f;
}

tracing::warn!("failed to query {target}; retrying in 3 seconds");

thread::sleep(Duration::from_secs(3));
}
}

async fn query_fee() -> Result<u32> {
// #[derive(Debug, Deserialize)]
// struct Satsbyte {
Expand All @@ -368,6 +367,27 @@ async fn query_fee() -> Result<u32> {
.fastest_fee)
}

async fn query_unconfirmed_tx_count(address: &str) -> Result<u32> {
#[derive(Debug, Deserialize)]
struct Unspent {
unconfirmed_n_tx: u32,
}

tracing::info!("fee: {address}");

let unconfirmed_count = reqwest::get(format!(
"https://api.blockcypher.com/v1/btc/main/addrs/{address}?unspentOnly=true",
))
.await?
.json::<Unspent>()
.await?
.unconfirmed_n_tx;

tracing::info!("unconfirmed transaction count: {unconfirmed_count}");

Ok(unconfirmed_count)
}

fn kill(pid: u32) -> Result<()> {
let pid = pid.to_string();

Expand Down

0 comments on commit 393cab6

Please sign in to comment.