riftenlabs-indexer/src/electrum.rs

30 lines
957 B
Rust
Raw Normal View History

2024-02-14 11:11:16 +01:00
// Copyright (C) 2024 Riften Labs AS
//
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
use anyhow::{Context, Result};
use bitcoincash::{consensus::deserialize, BlockHeader};
use electrum_client::{Client, ElectrumApi};
use serde_json::Value;
/// Fetch blockchain tip from electrum server
pub fn electrum_get_tip(client: &Client) -> Result<(BlockHeader, u64)> {
let tip: Value =
serde_json::from_str(&client.raw_call("blockchain.headers.tip", [])?.to_string())?;
let height = tip
.get("height")
.context("no height")?
.as_i64()
.context("no int")?;
let header = tip
.get("hex")
.context("no hex in header")?
.as_str()
.context("hex not str")?;
Ok((deserialize(&hex::decode(header)?)?, height as u64))
}