Skip to content

Commit

Permalink
liquid: fetch asset metadata from esplora
Browse files Browse the repository at this point in the history
Signed-off-by: lvaccaro <[email protected]>
  • Loading branch information
lvaccaro committed Dec 8, 2023
1 parent 8dbefd4 commit abe366e
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 14 deletions.
578 changes: 575 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl BTCWallet {
&mut self,
addr: &str,
from_height: Option<usize>,
) -> Result<HashMap<String, u64>, bdk::Error> {
) -> Result<HashMap<String, String>, bdk::Error> {
let list = self.check_address(addr, from_height)?;
let mut balances = HashMap::new();

Expand All @@ -127,7 +127,7 @@ impl BTCWallet {
}
}
};
balances.insert("btc".to_string(), amount);
balances.insert("btc".to_string(), amount.to_string());
Ok(balances)
}

Expand Down
4 changes: 2 additions & 2 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl ClightningWallet {
&mut self,
addr: &str,
_from_height: Option<usize>,
) -> Result<HashMap<String, u64>, Error> {
) -> Result<HashMap<String, String>, Error> {
let mut balances = HashMap::new();
let decoded = self.decode(addr).unwrap();
let payment_hash = decoded["result"]["payment_hash"].as_str().unwrap();
Expand All @@ -246,7 +246,7 @@ impl ClightningWallet {
}
_ => 0,
};
balances.insert(addr.to_string(), msat);
balances.insert(addr.to_string(), msat.to_string());
Ok(balances)
}

Expand Down
7 changes: 6 additions & 1 deletion liquid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
[package]
name = "btctipserver-liquid"
version = "0.1.1-dev"
edition = "2021"

[dependencies]
edk = { git = "https://github.com/lvaccaro/edk.git" }
dirs-next = "2.0.0"
structopt = "0.3"
hex = "0.4"
reqwest = { version = "0.11.22", features = ["blocking", "json"] }
serde = "1.0.114"
serde_json = "1.0"
serde_derive = "1.0.114"

[features]
electrum = ["edk/electrum"]
electrum = ["edk/electrum"]
37 changes: 37 additions & 0 deletions liquid/src/esplora.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use edk::bdk::Error;
use serde_derive::Deserialize;
use std::collections::HashMap;

pub fn gen_err() -> Error {
Error::Generic(format!("oh no!"))
}

#[derive(Deserialize, Clone, Debug)]
pub struct Asset {
pub asset_id: String,
pub precision: u8,
pub name: String,
pub ticker: String,
}

pub struct EsploraRepository {
pub assets: HashMap<String, Asset>,
}

impl EsploraRepository {
pub fn get(&mut self, asset_id: String) -> Result<Asset, Error> {
match self.assets.get(&asset_id).as_ref() {
Some(&asset) => Ok(asset.clone()),
None => self.fetch(asset_id),
}
}

pub fn fetch(&mut self, asset_id: String) -> Result<Asset, Error> {
let url = format!("https://blockstream.info/liquid/api/asset/{}", asset_id);
let res = reqwest::blocking::get(url).map_err(|_| gen_err())?;
let asset: Asset = res.json().map_err(|_| gen_err())?;
println!("Asset: {:#?}", asset);
self.assets.insert(asset_id, asset.clone());
Ok(asset)
}
}
32 changes: 28 additions & 4 deletions liquid/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
pub mod config;
pub mod esplora;

pub extern crate edk;
extern crate reqwest;
extern crate structopt;

use crate::config::LiquidOpts;
use edk::bdk::electrum_client::Client;
use edk::bdk::Error;
use std::collections::HashMap;
use std::ops::Div;
use std::str::FromStr;

use edk::bdk::sled::{self, Tree};
use edk::miniscript::elements::secp256k1_zkp;
use edk::miniscript::elements::slip77::MasterBlindingKey;
use edk::miniscript::elements::Address;
use edk::miniscript::{Descriptor, DescriptorPublicKey};
use esplora::EsploraRepository;
use std::fs;
use std::path::PathBuf;

pub struct LiquidWallet {
wallet: edk::Wallet<Tree>,
esplora: EsploraRepository,
}

pub fn gen_err() -> Error {
Expand Down Expand Up @@ -63,7 +68,12 @@ impl LiquidWallet {
opts.network(),
)
.unwrap();
Ok(LiquidWallet { wallet })
Ok(LiquidWallet {
wallet,
esplora: EsploraRepository {
assets: HashMap::new(),
},
})
}
}

Expand All @@ -82,7 +92,7 @@ impl LiquidWallet {
&mut self,
addr: &str,
_from_height: Option<usize>,
) -> Result<HashMap<String, u64>, Error> {
) -> Result<HashMap<String, String>, Error> {
let addr = Address::from_str(addr).map_err(|_| gen_err())?;
let mut balances = HashMap::new();
for unblind in self
Expand All @@ -92,9 +102,23 @@ impl LiquidWallet {
.unblinds
{
let tx_out = unblind.1;
*balances.entry(tx_out.asset.to_string()).or_insert(0) += tx_out.value;
*balances.entry(tx_out.asset).or_insert(0) += tx_out.value;
}
Ok(balances)

let res = balances
.into_iter()
.filter_map(|(key, value)| {
let asset_id = key.to_string();
match self.esplora.get(asset_id.clone()) {
Ok(asset) => Some((
format!("{} ({})", asset.name, asset_id),
(value / 10_u64.pow(asset.precision.into())).to_string(),
)),
Err(_) => Some((key.to_string(), value.to_string())),
}
})
.collect();
Ok(res)
}

pub fn network(&mut self) -> Result<String, Error> {
Expand Down
1 change: 0 additions & 1 deletion server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ pub fn page(wallet: &mut Wallet, uri: &str) -> Result<String, Error> {
.balance_address(&page.address, Option::from(0))
.map_err(|_| gen_err())?
.into_iter()
.filter(|(_, v)| *v > 0)
.map(|(k, v)| (k.clone(), v.clone()))
.map(|(k, v)| format!("{}: {}", k, v))
.collect::<Vec<String>>()
Expand Down
2 changes: 1 addition & 1 deletion server/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Wallet {
&mut self,
addr: &str,
_from_height: Option<usize>,
) -> Result<HashMap<String, u64>, Error> {
) -> Result<HashMap<String, String>, Error> {
match self {
Wallet::BTCWallet(w) => w.balance_address(addr, _from_height).map_err(|_| gen_err()),
Wallet::LiquidWallet(w) => w.balance_address(addr, _from_height).map_err(|_| gen_err()),
Expand Down

0 comments on commit abe366e

Please sign in to comment.