2022-11-01 04:03:57 -07:00
|
|
|
use crate::note_selection::types::Source;
|
|
|
|
use crate::note_selection::UTXO;
|
2022-11-06 04:50:51 -08:00
|
|
|
use crate::CoinConfig;
|
2022-11-01 04:03:57 -07:00
|
|
|
|
2022-11-06 04:50:51 -08:00
|
|
|
pub async fn fetch_utxos(
|
|
|
|
coin: u8,
|
|
|
|
account: u32,
|
|
|
|
last_height: u32,
|
|
|
|
use_transparent_inputs: bool,
|
|
|
|
anchor_offset: u32,
|
|
|
|
) -> anyhow::Result<Vec<UTXO>> {
|
2022-11-01 04:03:57 -07:00
|
|
|
let mut utxos = vec![];
|
|
|
|
if use_transparent_inputs {
|
|
|
|
utxos.extend(get_transparent_utxos(coin, account).await?);
|
|
|
|
}
|
|
|
|
let coin = CoinConfig::get(coin);
|
|
|
|
let db = coin.db.as_ref().unwrap();
|
|
|
|
let db = db.lock().unwrap();
|
|
|
|
let anchor_height = last_height.saturating_sub(anchor_offset);
|
|
|
|
utxos.extend(db.get_unspent_received_notes(account, anchor_height)?);
|
|
|
|
Ok(utxos)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_transparent_utxos(coin: u8, account: u32) -> anyhow::Result<Vec<UTXO>> {
|
|
|
|
let coin = CoinConfig::get(coin);
|
2022-11-06 04:50:51 -08:00
|
|
|
let taddr = {
|
|
|
|
let db = coin.db.as_ref().unwrap();
|
|
|
|
let db = db.lock().unwrap();
|
|
|
|
db.get_taddr(account)?
|
|
|
|
};
|
2022-11-01 04:03:57 -07:00
|
|
|
if let Some(taddr) = taddr {
|
|
|
|
let mut client = coin.connect_lwd().await?;
|
|
|
|
let utxos = crate::taddr::get_utxos(&mut client, &taddr, account).await?;
|
2022-11-06 04:50:51 -08:00
|
|
|
let utxos: Vec<_> = utxos
|
|
|
|
.iter()
|
|
|
|
.map(|utxo| {
|
|
|
|
let source = Source::Transparent {
|
|
|
|
txid: utxo.txid.clone().try_into().unwrap(),
|
|
|
|
index: utxo.index as u32,
|
|
|
|
};
|
|
|
|
UTXO {
|
|
|
|
source,
|
|
|
|
amount: utxo.value_zat as u64,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2022-11-01 04:03:57 -07:00
|
|
|
Ok(utxos)
|
2022-11-06 04:50:51 -08:00
|
|
|
} else {
|
2022-11-01 04:03:57 -07:00
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|