Skip to content

Commit

Permalink
update withdrawals tx format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Nov 11, 2024
1 parent 26e3b1e commit 97f1ccb
Show file tree
Hide file tree
Showing 24 changed files with 510 additions and 330 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl App {
const NUM_TRANSACTIONS: usize = 1000;
let (txs, tx_fees) = self.node.get_transactions(NUM_TRANSACTIONS)?;
let coinbase = match tx_fees {
0 => vec![],
bitcoin::Amount::ZERO => vec![],
_ => vec![types::Output {
address: self.wallet.get_new_address()?,
content: types::OutputContent::Value(tx_fees),
Expand Down Expand Up @@ -326,8 +326,8 @@ impl App {
prev_main_hash,
};
let bribe = fee.unwrap_or_else(|| {
if tx_fees > 0 {
bitcoin::Amount::from_sat(tx_fees)
if tx_fees > bitcoin::Amount::ZERO {
tx_fees
} else {
Self::EMPTY_BLOCK_BMM_BRIBE
}
Expand Down
4 changes: 2 additions & 2 deletions app/gui/block_explorer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bitcoin::Amount;
use eframe::egui;
use human_size::{Byte, Kibibyte, Mebibyte, SpecificSize};
use thunder::{
Expand Down Expand Up @@ -52,9 +53,8 @@ impl BlockExplorer {
let prev_main_hash = &format!("{}", header.prev_main_hash);
let body_size =
bincode::serialize(&body).unwrap_or(vec![]).len();
let coinbase_value: u64 =
let coinbase_value: Amount =
body.coinbase.iter().map(GetValue::get_value).sum();
let coinbase_value = bitcoin::Amount::from_sat(coinbase_value);
let num_transactions = body.transactions.len();
let body_size = if let Ok(body_size) =
SpecificSize::new(body_size as f64, Byte)
Expand Down
9 changes: 3 additions & 6 deletions app/gui/coins/transfer_receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ fn create_transfer(
fee: bitcoin::Amount,
) -> anyhow::Result<()> {
let accumulator = app.node.get_tip_accumulator()?;
let tx = app.wallet.create_transaction(
&accumulator,
dest,
amount.to_sat(),
fee.to_sat(),
)?;
let tx = app
.wallet
.create_transaction(&accumulator, dest, amount, fee)?;
app.sign_and_send(tx)?;
Ok(())
}
Expand Down
13 changes: 5 additions & 8 deletions app/gui/coins/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ impl TxBuilder {
.iter()
.filter(|(outpoint, _)| selected.contains(outpoint))
.collect();
let value_in: u64 = spent_utxos
let value_in: bitcoin::Amount = spent_utxos
.iter()
.map(|(_, output)| output.get_value())
.sum();
self.tx_creator.value_in = value_in;
spent_utxos.sort_by_key(|(outpoint, _)| format!("{outpoint}"));
ui.separator();
ui.monospace(format!("Total: {}", bitcoin::Amount::from_sat(value_in)));
ui.monospace(format!("Total: {}", value_in));
ui.separator();
egui::Grid::new("utxos").striped(true).show(ui, |ui| {
ui.monospace("kind");
Expand All @@ -67,13 +67,10 @@ impl TxBuilder {
pub fn show_value_out(&mut self, ui: &mut egui::Ui) {
ui.heading("Value Out");
ui.separator();
let value_out: u64 =
let value_out: bitcoin::Amount =
self.base_tx.outputs.iter().map(GetValue::get_value).sum();
self.tx_creator.value_out = value_out;
ui.monospace(format!(
"Total: {}",
bitcoin::Amount::from_sat(value_out)
));
ui.monospace(format!("Total: {}", value_out));
ui.separator();
egui::Grid::new("outputs").striped(true).show(ui, |ui| {
let mut remove = None;
Expand All @@ -83,7 +80,7 @@ impl TxBuilder {
ui.end_row();
for (vout, output) in self.base_tx.outputs.iter().enumerate() {
let address = &format!("{}", output.address)[0..8];
let value = bitcoin::Amount::from_sat(output.get_value());
let value = output.get_value();
ui.monospace(format!("{vout}"));
ui.monospace(address.to_string());
ui.with_layout(
Expand Down
5 changes: 2 additions & 3 deletions app/gui/coins/tx_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::app::App;

#[derive(Debug, Default)]
pub struct TxCreator {
pub value_in: u64,
pub value_out: u64,
pub value_in: bitcoin::Amount,
pub value_out: bitcoin::Amount,
// if the base tx has changed, need to recompute final tx
base_txid: Txid,
final_tx: Option<Transaction>,
Expand Down Expand Up @@ -47,7 +47,6 @@ impl TxCreator {
ui.monospace(format!("txid: {txid}"));
if self.value_in >= self.value_out {
let fee = self.value_in - self.value_out;
let fee = bitcoin::Amount::from_sat(fee);
ui.monospace(format!("fee: {fee}"));
if ui.button("sign and send").clicked() {
if let Err(err) = send_tx(app, final_tx) {
Expand Down
8 changes: 3 additions & 5 deletions app/gui/coins/utxo_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl UtxoCreator {
let utxo = Output {
address: address.expect("should not happen"),
content: OutputContent::Value(
value.expect("should not happen").to_sat(),
value.expect("should not happen"),
),
};
tx.outputs.push(utxo);
Expand Down Expand Up @@ -161,12 +161,10 @@ impl UtxoCreator {
let utxo = Output {
address: address.expect("invalid address"),
content: OutputContent::Withdrawal {
value: value.expect("invalid value").to_sat(),
value: value.expect("invalid value"),
main_address: main_address
.expect("invalid main_address"),
main_fee: main_fee
.expect("invalid main_fee")
.to_sat(),
main_fee: main_fee.expect("invalid main_fee"),
},
};
tx.outputs.push(utxo);
Expand Down
6 changes: 3 additions & 3 deletions app/gui/coins/utxo_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl UtxoSelector {
let selected: HashSet<_> =
tx.inputs.iter().map(|(outpoint, _)| *outpoint).collect();
let utxos_read = app.utxos.read();
let total: u64 = utxos_read
let total: bitcoin::Amount = utxos_read
.iter()
.filter(|(outpoint, _)| !selected.contains(outpoint))
.map(|(_, output)| output.get_value())
Expand All @@ -30,7 +30,7 @@ impl UtxoSelector {
drop(utxos_read);
utxos.sort_by_key(|(outpoint, _)| format!("{outpoint}"));
ui.separator();
ui.monospace(format!("Total: {}", bitcoin::Amount::from_sat(total)));
ui.monospace(format!("Total: {}", total));
ui.separator();
egui::Grid::new("utxos").striped(true).show(ui, |ui| {
ui.monospace("kind");
Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn show_utxo(ui: &mut egui::Ui, outpoint: &OutPoint, output: &Output) {
}
};
let hash = &hash[0..8];
let value = bitcoin::Amount::from_sat(output.get_value());
let value = output.get_value();
ui.monospace(kind.to_string());
ui.monospace(format!("{hash}:{vout}",));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Max), |ui| {
Expand Down
20 changes: 6 additions & 14 deletions app/gui/mempool_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ impl MemPoolExplorer {
for (index, transaction) in
transactions.iter().enumerate()
{
let value_out: u64 = transaction
let value_out: bitcoin::Amount = transaction
.transaction
.outputs
.iter()
.map(GetValue::get_value)
.sum();
let value_in: u64 = transaction
let value_in: bitcoin::Amount = transaction
.transaction
.inputs
.iter()
.map(|(outpoint, _)| {
utxos.get(outpoint).map(GetValue::get_value)
})
.sum::<Option<u64>>()
.unwrap_or(0);
.sum::<Option<bitcoin::Amount>>()
.unwrap_or(bitcoin::Amount::ZERO);
let txid =
&format!("{}", transaction.transaction.txid())
[0..8];
Expand All @@ -58,10 +58,6 @@ impl MemPoolExplorer {
egui::Align::Max,
),
|ui| {
let value_out =
bitcoin::Amount::from_sat(
value_out,
);
ui.monospace(format!("{value_out}"));
},
);
Expand All @@ -70,8 +66,6 @@ impl MemPoolExplorer {
egui::Align::Max,
),
|ui| {
let fee =
bitcoin::Amount::from_sat(fee);
ui.monospace(format!("{fee}"));
},
);
Expand Down Expand Up @@ -117,8 +111,7 @@ impl MemPoolExplorer {
};
let output = &utxos[outpoint];
let hash = &hash[0..8];
let value =
bitcoin::Amount::from_sat(output.get_value());
let value = output.get_value();
ui.monospace(kind.to_string());
ui.monospace(format!("{hash}:{vout}",));
ui.monospace(format!("{value}",));
Expand All @@ -140,8 +133,7 @@ impl MemPoolExplorer {
transaction.transaction.outputs.iter().enumerate()
{
let address = &format!("{}", output.address)[0..8];
let value =
bitcoin::Amount::from_sat(output.get_value());
let value = output.get_value();
ui.monospace(format!("{vout}"));
ui.monospace(address.to_string());
ui.monospace(format!("{value}"));
Expand Down
17 changes: 7 additions & 10 deletions app/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{net::SocketAddr, task::Poll};
use eframe::egui::{self, RichText};
use strum::{EnumIter, IntoEnumIterator};
use thunder::{util::Watchable, wallet::Wallet};
use util::{show_btc_amount_from_sats, BITCOIN_LOGO_FA, BITCOIN_ORANGE};
use util::{show_btc_amount, BITCOIN_LOGO_FA, BITCOIN_ORANGE};

use crate::{app::App, line_buffer::LineBuffer, util::PromiseStream};

Expand Down Expand Up @@ -65,7 +65,7 @@ struct BottomPanel {
wallet_updated: PromiseStream<<Wallet as Watchable<()>>::WatchStream>,
/// None if uninitialized
/// Some(None) if failed to initialize
balance_sats: Option<Option<u64>>,
balance: Option<Option<bitcoin::Amount>>,
}

impl BottomPanel {
Expand All @@ -74,13 +74,13 @@ impl BottomPanel {
let wallet_updated = PromiseStream::from(wallet.watch());
Self {
wallet_updated,
balance_sats: None,
balance: None,
}
}

/// Updates values
fn update(&mut self, app: &App) {
self.balance_sats = match app.wallet.get_balance() {
self.balance = match app.wallet.get_balance() {
Ok(balance) => Some(Some(balance)),
Err(err) => {
let err = anyhow::Error::from(err);
Expand All @@ -91,18 +91,15 @@ impl BottomPanel {
}

fn show_balance(&self, ui: &mut egui::Ui) {
match self.balance_sats {
Some(Some(balance_sats)) => {
match self.balance {
Some(Some(balance)) => {
ui.monospace(
RichText::new(BITCOIN_LOGO_FA.to_string())
.color(BITCOIN_ORANGE),
);
ui.monospace_selectable_singleline(
false,
format!(
"Balance: {}",
show_btc_amount_from_sats(balance_sats)
),
format!("Balance: {}", show_btc_amount(balance)),
);
}
Some(None) => {
Expand Down
6 changes: 3 additions & 3 deletions app/gui/parent_chain/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ fn create_withdrawal(
let tx = app.wallet.create_withdrawal(
&accumulator,
mainchain_address,
amount.to_sat(),
mainchain_fee.to_sat(),
fee.to_sat(),
amount,
mainchain_fee,
fee,
)?;
app.sign_and_send(tx)?;
Ok(())
Expand Down
5 changes: 0 additions & 5 deletions app/gui/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ pub fn show_btc_amount(amount: bitcoin::Amount) -> String {
)
}

/// Show a Bitcoin amount from sats
pub fn show_btc_amount_from_sats(sats: u64) -> String {
show_btc_amount(bitcoin::Amount::from_sat(sats))
}

// extension for InnerResponse<Response> and InnerResponse<Option<Response>>
pub trait InnerResponseExt {
#[allow(dead_code)]
Expand Down
7 changes: 2 additions & 5 deletions app/gui/withdrawals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ impl Withdrawals {
ui.heading("Pending withdrawals");
let bundle = app.node.get_pending_withdrawal_bundle().ok().flatten();
if let Some(bundle) = bundle {
let mut spent_utxos: Vec<_> = bundle.spend_utxos.iter().collect();
let mut spent_utxos: Vec<_> = bundle.spend_utxos().iter().collect();
spent_utxos.sort_by_key(|(outpoint, _)| format!("{outpoint}"));
egui::Grid::new("bundle_utxos")
.striped(true)
.show(ui, |ui| {
for (outpoint, output) in &spent_utxos {
ui.monospace(format!("{outpoint}"));
ui.monospace(format!(
"{}",
bitcoin::Amount::from_sat(output.get_value())
));
ui.monospace(format!("{}", output.get_value()));
ui.end_row();
}
});
Expand Down
19 changes: 14 additions & 5 deletions app/rpc_server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::net::SocketAddr;

use bitcoin::Amount;
use jsonrpsee::{
core::{async_trait, RpcResult},
server::Server,
Expand Down Expand Up @@ -47,7 +48,10 @@ fn convert_wallet_err(err: wallet::Error) -> ErrorObject<'static> {
#[async_trait]
impl RpcServer for RpcServerImpl {
async fn balance(&self) -> RpcResult<u64> {
self.app.wallet.get_balance().map_err(convert_wallet_err)
match self.app.wallet.get_balance() {
Ok(balance) => Ok(balance.to_sat()),
Err(err) => Err(convert_wallet_err(err)),
}
}

async fn create_deposit(
Expand Down Expand Up @@ -188,7 +192,12 @@ impl RpcServer for RpcServerImpl {
let tx = self
.app
.wallet
.create_transaction(&accumulator, dest, value_sats, fee_sats)
.create_transaction(
&accumulator,
dest,
Amount::from_sat(value_sats),
Amount::from_sat(fee_sats),
)
.map_err(convert_wallet_err)?;
let txid = tx.txid();
self.app.sign_and_send(tx).map_err(convert_app_err)?;
Expand All @@ -213,9 +222,9 @@ impl RpcServer for RpcServerImpl {
.create_withdrawal(
&accumulator,
mainchain_address,
amount_sats,
mainchain_fee_sats,
fee_sats,
Amount::from_sat(amount_sats),
Amount::from_sat(mainchain_fee_sats),
Amount::from_sat(fee_sats),
)
.map_err(convert_wallet_err)?;
let txid = tx.txid();
Expand Down
Loading

0 comments on commit 97f1ccb

Please sign in to comment.