Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Feb 20, 2024
1 parent 9d39147 commit 843eaf4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ keywords = ["crypto", "bitcoin"]
readme = "README.md"

[dependencies]
bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" }
bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", rev="426408" }
rand = {version = "0.8.5", default-features = false, optional = true}

[dev-dependencies]
Expand All @@ -24,10 +24,10 @@ rust-bitcoin-coin-selection = {path = ".", features = ["rand"]}
rand = "0.8.5"

[patch.crates-io]
bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" }
bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" }
bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" }
bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" }
bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="426408" }
bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="426408" }
bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="426408" }
bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="426408" }

[[bench]]
name = "coin_selection"
Expand Down
21 changes: 14 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,22 @@ const OUTPOINT_SIZE: u64 = 32 + 4;
const TX_IN_BASE_WEIGHT: Weight =
Weight::from_vb_unwrap(OUTPOINT_SIZE + SEQUENCE_SIZE);

impl WeightedUtxo {
fn calculate_fee(&self, fee_rate: FeeRate) -> Option<Amount> {
let weight = self.satisfaction_weight.checked_add(TX_IN_BASE_WEIGHT)?;
fee_rate.checked_mul_by_weight(weight)
}
// Predict the fee Amount to spend a UTXO.
//
// To predict the fee, the predicted weight is:
// weight = satisfaction_weight + TX_IN base weight.
//
// The fee is then calculated as:
// fee = weight * fee_rate
fn calculate_fee_prediction(satisfaction_weight: Weight, fee_rate: FeeRate) -> Option<Amount> {
let weight = satisfaction_weight.checked_add(TX_IN_BASE_WEIGHT)?;
fee_rate.checked_mul_by_weight(weight)
}

impl WeightedUtxo {
fn waste(&self, fee_rate: FeeRate, long_term_fee_rate: FeeRate) -> Option<SignedAmount> {
let fee: SignedAmount = self.calculate_fee(fee_rate)?.to_signed().ok()?;
let lt_fee: SignedAmount = self.calculate_fee(long_term_fee_rate)?.to_signed().ok()?;
let fee: SignedAmount = calculate_fee_prediction(self.satisfaction_weight, fee_rate)?.to_signed().ok()?;
let lt_fee: SignedAmount = calculate_fee_prediction(self.satisfaction_weight, long_term_fee_rate)?.to_signed().ok()?;
fee.checked_sub(lt_fee)
}
}
Expand Down

0 comments on commit 843eaf4

Please sign in to comment.