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 balance to address page #3810

Merged
merged 1 commit into from
Jun 7, 2024
Merged
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
13 changes: 13 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,19 @@ impl Index {
.collect()
}

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

let mut acc = 0;
for output in outputs {
if let Some(value) = outpoint_to_txout.get(&output.store())? {
acc += TxOut::load(value.value()).value;
};
}

Ok(acc)
}

pub(crate) fn get_output_info(&self, outpoint: OutPoint) -> Result<Option<(api::Output, TxOut)>> {
let sat_ranges = self.list(outpoint)?;

Expand Down
12 changes: 9 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,12 +862,18 @@ impl Server {

outputs.sort();

let sat_balance = index.get_sat_balances_for_outputs(&outputs)?;

Ok(if accept_json {
Json(outputs).into_response()
} else {
AddressHtml { address, outputs }
.page(server_config)
.into_response()
AddressHtml {
address,
outputs,
sat_balance,
}
.page(server_config)
.into_response()
})
})
}
Expand Down
18 changes: 13 additions & 5 deletions src/templates/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::*;
pub(crate) struct AddressHtml {
pub(crate) address: Address,
pub(crate) outputs: Vec<OutPoint>,
pub(crate) sat_balance: u64,
}

impl PageContent for AddressHtml {
Expand All @@ -27,13 +28,20 @@ mod tests {
.require_network(Network::Bitcoin)
.unwrap(),
outputs: vec![outpoint(1), outpoint(2)],
sat_balance: 99,
},
"<h1>Address bc1phuq0vkls6w926zdaem6x9n02z2gg7j2xfudgwddyey7uyquarlgsh40ev8</h1>
<h2>Outputs</h2>
<ul>
<li><a class=monospace href=/output/1{64}:1>1{64}:1</a></li>
<li><a class=monospace href=/output/2{64}:2>2{64}:2</a></li>
</ul>.*"
<dl>
<dt>sat balance</dt>
<dd>99</dd>
<dt>outputs</dt>
<dd>
<ul>
<li><a class=monospace href=/output/1{64}:1>1{64}:1</a></li>
<li><a class=monospace href=/output/2{64}:2>2{64}:2</a></li>
</ul>
</dd>
</dl>.*"
.unindent()
);
}
Expand Down
14 changes: 10 additions & 4 deletions templates/address.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<h1>Address {{ self.address }}</h1>
<h2>Outputs</h2>
<ul>
<dl>
<dt>sat balance</dt>
<dd>{{ self.sat_balance }}</dd>
<dt>outputs</dt>
<dd>
<ul>
%% for output in self.outputs.iter() {
<li><a class=monospace href=/output/{{ output }}>{{ output }}</a></li>
<li><a class=monospace href=/output/{{ output }}>{{ output }}</a></li>
%% }
</ul>
</ul>
</dd>
</dl>
4 changes: 2 additions & 2 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn run() {
}

#[test]
fn address_page_shows_outputs() {
fn address_page_shows_outputs_and_sat_balance() {
let core = mockcore::spawn();
let ord = TestServer::spawn_with_args(&core, &["--index-addresses"]);

Expand All @@ -54,7 +54,7 @@ fn address_page_shows_outputs() {
ord.assert_response_regex(
format!("/address/{address}"),
format!(
".*<h1>Address {address}</h1>.*<a class=monospace href=/output/{}.*",
".*<h1>Address {address}</h1>.*<dd>200000000</dd>.*<a class=monospace href=/output/{}.*",
OutPoint {
txid: send.txid,
vout: 0
Expand Down
Loading