Skip to content

Commit

Permalink
feat: Esplora chain backend
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jun 28, 2024
1 parent 6a43e91 commit 98d8c7f
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 197 deletions.
14 changes: 13 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ SWEEP_TIME=120
SWEEP_INTERVAL=30

# Possible values: mainnet, testnet, regtest
NETWORK=regtest
NETWORK=mainnet

# Rest API configuration
API_HOST=127.0.0.1
API_PORT=1234

# Chain backend to use
# Options:
# - elements
# - esplora
CHAIN_BACKEND=esplora

# Configuration of the Elements daemon to connect to
ELEMENTS_HOST=127.0.0.1
ELEMENTS_PORT=18884
ELEMENTS_COOKIE=/home/michael/Git/TypeScript/boltz-backend/docker/regtest/data/core/cookies/.elements-cookie

# Configuration of the Esplora backend
ESPLORA_ENDPOINT=https://blockstream.info/liquid/api

# Poll interval for new blocks in seconds
ESPLORA_POLL_INTERVAL=10
156 changes: 12 additions & 144 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ crossbeam-channel = "0.5.13"
r2d2 = "0.8.10"
rayon = "1.10.0"
num_cpus = "1.16.0"
esplora-client = { version = "0.9.0", default-features = false, features = ["async-https"] }
async-trait = "0.1.80"

[patch.crates-io]
Expand Down
29 changes: 8 additions & 21 deletions src/chain/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_json::json;
use std::error::Error;
use std::fs;

use crate::chain::types::{ChainDataProvider, NetworkInfo, ZmqNotification};
use crate::chain::types::{ChainBackend, NetworkInfo, ZmqNotification};
use crate::chain::zmq::ZmqClient;

enum StringOrU64 {
Expand Down Expand Up @@ -78,10 +78,6 @@ impl ChainClient {
Ok(self)
}

pub async fn get_network_info(self) -> Result<NetworkInfo, Box<dyn Error>> {
self.request::<NetworkInfo>("getnetworkinfo").await
}

pub async fn get_zmq_notifications(self) -> Result<Vec<ZmqNotification>, Box<dyn Error>> {
self.request::<Vec<ZmqNotification>>("getzmqnotifications")
.await
Expand Down Expand Up @@ -128,23 +124,14 @@ impl ChainClient {

Ok(res.result.unwrap())
}

fn parse_hex<T: elements::encode::Decodable>(hex_str: String) -> Result<T, Box<dyn Error>> {
match elements::encode::deserialize(
match hex::decode(hex_str) {
Ok(res) => res,
Err(err) => return Err(Box::new(err)),
}
.as_ref(),
) {
Ok(block) => Ok(block),
Err(e) => Err(Box::new(e)),
}
}
}

#[async_trait]
impl ChainDataProvider for ChainClient {
impl ChainBackend for ChainClient {
async fn get_network_info(&self) -> Result<NetworkInfo, Box<dyn Error>> {
self.clone().request::<NetworkInfo>("getnetworkinfo").await
}

async fn get_block_count(&self) -> Result<u64, Box<dyn Error>> {
self.clone().request::<u64>("getblockcount").await
}
Expand All @@ -165,7 +152,7 @@ impl ChainDataProvider for ChainClient {
.request_params::<String>("getblock", params)
.await?;

Self::parse_hex(block_hex)
crate::chain::utils::parse_hex(block_hex)
}

async fn send_raw_transaction(&self, hex: String) -> Result<String, Box<dyn Error>> {
Expand All @@ -180,7 +167,7 @@ impl ChainDataProvider for ChainClient {
.request_params::<String>("getrawtransaction", vec![hash])
.await?;

Self::parse_hex(tx_hex)
crate::chain::utils::parse_hex(tx_hex)
}

fn get_tx_receiver(&self) -> Receiver<Transaction> {
Expand Down
Loading

0 comments on commit 98d8c7f

Please sign in to comment.