diff --git a/src/api.rs b/src/api.rs index eae5ac144a..1153e865b8 100644 --- a/src/api.rs +++ b/src/api.rs @@ -228,8 +228,15 @@ pub struct SatInscriptions { #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct AddressInfo { - pub outputs: Vec, + pub outputs: Vec, pub inscriptions: Vec, pub sat_balance: u64, pub runes_balances: Vec<(SpacedRune, Decimal, Option)>, } + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct OutpointInfo { + #[serde(flatten)] + pub output: OutPoint, + pub value: u64, +} diff --git a/src/index.rs b/src/index.rs index 302cbad4d7..255d4068f8 100644 --- a/src/index.rs +++ b/src/index.rs @@ -2317,20 +2317,22 @@ impl Index { ) } - pub(crate) fn get_sat_balances_for_outputs(&self, outputs: &Vec) -> Result { + pub(crate) fn get_sat_balances_for_outputs(&self, outputs: &Vec) -> Result> { 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> { diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 38e501f4ed..4ace766cb7 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -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)?; @@ -969,7 +979,7 @@ impl Server { Ok(if accept_json { Json(api::AddressInfo { sat_balance, - outputs, + outputs: outpoint_infos, inscriptions, runes_balances, })