Skip to content

Commit

Permalink
test-utils: use bitcoin::Amount
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyhodl committed Dec 13, 2024
1 parent e052f8d commit 04cac5f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
6 changes: 4 additions & 2 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bitcoin::hashes::serde;
use bitcoin::psbt::Psbt;
use bitcoin::secp256k1::rand::thread_rng;
use bitcoin::secp256k1::SecretKey;
use bitcoin::Amount;
use bitcoin::{consensus::Decodable, Network, PrivateKey, Transaction, Txid};
use bitcoin::{secp256k1::PublicKey, Address, OutPoint, ScriptBuf, TxOut};
use bitcoincore_rpc::jsonrpc::serde_json;
Expand Down Expand Up @@ -276,7 +277,7 @@ impl Wallet for BitcoinCoreProvider {

fn get_utxos_for_amount(
&self,
amount: u64,
amount: Amount,
_fee_rate: u64,
lock_utxos: bool,
) -> Result<Vec<Utxo>, ManagerError> {
Expand Down Expand Up @@ -312,7 +313,8 @@ impl Wallet for BitcoinCoreProvider {
})
.collect::<Result<Vec<UtxoWrap>, Error>>()?;
// TODO(tibo): properly compute the cost of change
let selection = select_coins(amount, 20, &mut utxo_pool).ok_or(Error::NotEnoughCoins)?;
let selection =
select_coins(amount.to_sat(), 20, &mut utxo_pool).ok_or(Error::NotEnoughCoins)?;

if lock_utxos {
let outputs: Vec<_> = selection.iter().map(|x| x.0.outpoint).collect();
Expand Down
4 changes: 2 additions & 2 deletions dlc-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ where

let total_collateral = channel.counter_params.collateral + channel.own_params.collateral;
//Todo(tibo): compute fee for settle transaction.
let fee_remainder = 0 as u64; //channel.fund_tx.output[channel.fund_output_index].value - total_collateral;
let fee_remainder = 0_u64; //channel.fund_tx.output[channel.fund_output_index].value - total_collateral;
let final_offer_payout = total_collateral - own_payout + Amount::from_sat(fee_remainder / 2);
let final_accept_payout = own_payout + Amount::from_sat(fee_remainder / 2);

Expand Down Expand Up @@ -776,7 +776,7 @@ where

let total_collateral = channel.counter_params.collateral + channel.own_params.collateral;
//Todo(tibo): compute fee for settle transaction.
let fee_remainder = 0 as u64; //channel.fund_tx.output[channel.fund_output_index].value - total_collateral;
let fee_remainder = 0_u64; //channel.fund_tx.output[channel.fund_output_index].value - total_collateral;
let final_offer_payout =
total_collateral - counter_payout + Amount::from_sat(fee_remainder / 2);
let final_accept_payout = counter_payout + Amount::from_sat(fee_remainder / 2);
Expand Down
10 changes: 5 additions & 5 deletions mocks/src/mock_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub struct MockWallet {
}

impl MockWallet {
pub fn new(blockchain: &Rc<MockBlockchain>, utxo_values: &[u64]) -> Self {
pub fn new(blockchain: &Rc<MockBlockchain>, utxo_values: &[Amount]) -> Self {
let mut utxos = Vec::with_capacity(utxo_values.len());

for utxo_value in utxo_values {
let tx_out = TxOut {
value: Amount::from_sat(*utxo_value),
value: *utxo_value,
script_pubkey: ScriptBuf::default(),
};
let tx = Transaction {
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Wallet for MockWallet {

fn get_utxos_for_amount(
&self,
amount: u64,
amount: Amount,
_fee_rate: u64,
_lock_utxos: bool,
) -> Result<Vec<dlc_manager::Utxo>, Error> {
Expand All @@ -88,15 +88,15 @@ impl Wallet for MockWallet {
seed, seed,
));

let mut sum = 0;
let mut sum = Amount::ZERO;

let res = utxo_pool
.iter()
.take_while(|x| {
if sum >= amount {
return false;
}
sum += x.tx_out.value.to_sat();
sum += x.tx_out.value;
true
})
.cloned()
Expand Down
3 changes: 2 additions & 1 deletion sample/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::DlcManager;
use crate::DlcMessageHandler;
use crate::PeerManager;
use bitcoin::secp256k1::PublicKey;
use bitcoin::Amount;
use dlc_manager::channel::signed_channel::SignedChannelState;
use dlc_manager::channel::signed_channel::SignedChannelStateType;
use dlc_manager::contract::contract_input::ContractInput;
Expand Down Expand Up @@ -313,7 +314,7 @@ pub(crate) async fn poll_for_user_input(
}
s @ "offersettlechannel" => {
let channel_id = read_id_or_continue!(words, s, "channel id");
let counter_payout: u64 = match words.next().map(|w| w.parse().ok()) {
let counter_payout: Amount = match words.next().map(|w| w.parse().ok()) {
Some(Some(p)) => p,
_ => {
println!("Missing or invalid counter payout parameter");
Expand Down
10 changes: 5 additions & 5 deletions simple-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ where
}

/// Returns the sum of all UTXOs value.
pub fn get_balance(&self) -> u64 {
pub fn get_balance(&self) -> Amount {
self.storage
.get_utxos()
.unwrap()
.iter()
.map(|x| x.tx_out.value.to_sat())
.map(|x| x.tx_out.value)
.sum()
}

Expand Down Expand Up @@ -237,7 +237,7 @@ where

fn get_utxos_for_amount(
&self,
amount: u64,
amount: Amount,
fee_rate: u64,
lock_utxos: bool,
) -> Result<Vec<Utxo>> {
Expand Down Expand Up @@ -270,7 +270,7 @@ where
Vec::new(),
utxos,
fee_rate,
amount,
amount.to_sat(),
&dummy_drain,
&mut bitcoin::key::rand::thread_rng(),
)
Expand Down Expand Up @@ -347,7 +347,7 @@ where
&mut tx,
input_index,
bitcoin::sighash::EcdsaSighashType::All,
tx_out.value.to_sat(),
tx_out.value,
)?;

let tx_input = tx.input[input_index].clone();
Expand Down

0 comments on commit 04cac5f

Please sign in to comment.