Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sat values to address response (#3809) #4101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,15 @@ pub struct SatInscriptions {

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct AddressInfo {
pub outputs: Vec<OutPoint>,
pub outputs: Vec<OutpointInfo>,
pub inscriptions: Vec<InscriptionId>,
pub sat_balance: u64,
pub runes_balances: Vec<(SpacedRune, Decimal, Option<char>)>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct OutpointInfo {
#[serde(flatten)]
pub output: OutPoint,
pub value: u64,
}
12 changes: 7 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2317,20 +2317,22 @@ impl Index {
)
}

pub(crate) fn get_sat_balances_for_outputs(&self, outputs: &Vec<OutPoint>) -> Result<u64> {
pub(crate) fn get_sat_balances_for_outputs(&self, outputs: &Vec<OutPoint>) -> Result<Vec<u64>> {
let outpoint_to_utxo_entry = self
.database
.begin_read()?
.open_table(OUTPOINT_TO_UTXO_ENTRY)?;

let mut acc = 0;
let mut balances = Vec::with_capacity(outputs.len());
for output in outputs {
if let Some(utxo_entry) = outpoint_to_utxo_entry.get(&output.store())? {
acc += utxo_entry.value().parse(self).total_value();
};
balances.push(utxo_entry.value().parse(self).total_value());
} else {
balances.push(0);
}
}

Ok(acc)
Ok(balances)
}

pub(crate) fn get_output_info(&self, outpoint: OutPoint) -> Result<Option<(api::Output, TxOut)>> {
Expand Down
18 changes: 14 additions & 4 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,11 +956,21 @@ impl Server {
.require_network(server_config.chain.network())
.map_err(|err| ServerError::BadRequest(err.to_string()))?;

let mut outputs = index.get_address_info(&address)?;
let outputs = index.get_address_info(&address)?;

outputs.sort();
let sat_balances = index.get_sat_balances_for_outputs(&outputs)?;
let sat_balance = sat_balances.iter().sum();

let sat_balance = index.get_sat_balances_for_outputs(&outputs)?;
let mut outpoint_infos: Vec<_> = outputs
.iter()
.enumerate()
.map(|(i, output)| api::OutpointInfo {
output: *output,
value: sat_balances[i],
})
.collect();

outpoint_infos.sort_by_key(|info| info.output);

let inscriptions = index.get_inscriptions_for_outputs(&outputs)?;

Expand All @@ -969,7 +979,7 @@ impl Server {
Ok(if accept_json {
Json(api::AddressInfo {
sat_balance,
outputs,
outputs: outpoint_infos,
inscriptions,
runes_balances,
})
Expand Down
Loading