Use FeeRate to prevent fees miscalculation
This commit is contained in:
parent
ef7749b4a5
commit
90d87e7d9a
30
src/api.rs
30
src/api.rs
@ -68,7 +68,7 @@ use sdk_common::pcd::{
|
||||
};
|
||||
use sdk_common::prd::{AnkPrdHash, Prd, PrdType};
|
||||
use sdk_common::silentpayments::{create_transaction as internal_create_transaction, sign_transaction as internal_sign_tx, TsUnsignedTransaction};
|
||||
use sdk_common::sp_client::{OutputSpendStatus, OwnedOutput, Recipient, RecipientAddress, SilentPaymentUnsignedTransaction, SpClient, SpendKey};
|
||||
use sdk_common::sp_client::{FeeRate, OutputSpendStatus, OwnedOutput, Recipient, RecipientAddress, SilentPaymentUnsignedTransaction, SpClient, SpendKey};
|
||||
use sdk_common::secrets::SecretsStore;
|
||||
|
||||
use crate::user::{lock_local_device, set_new_device, LOCAL_DEVICE};
|
||||
@ -1047,7 +1047,7 @@ fn create_transaction_for_addresses(
|
||||
device: &Device,
|
||||
freezed_utxos: &HashSet<OutPoint>,
|
||||
sp_addresses: &[SilentPaymentAddress],
|
||||
fee_rate: u32
|
||||
fee_rate: FeeRate
|
||||
) -> anyhow::Result<SilentPaymentUnsignedTransaction> {
|
||||
let mut recipients = Vec::with_capacity(sp_addresses.len());
|
||||
for sp_address in sp_addresses {
|
||||
@ -1058,18 +1058,30 @@ fn create_transaction_for_addresses(
|
||||
recipients.push(recipient);
|
||||
}
|
||||
|
||||
// If we had mandatory inputs, we would add them at the top of the list
|
||||
// Take spendable outputs and filter out the freezed utxos
|
||||
let candidates_inputs: Vec<(OutPoint, OwnedOutput)> = device.get_outputs()
|
||||
.into_iter()
|
||||
.filter_map(|(outpoint, output)| {
|
||||
if output.spend_status == OutputSpendStatus::Unspent && !freezed_utxos.contains(outpoint) {
|
||||
Some((*outpoint, output.clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut tx = internal_create_transaction(
|
||||
vec![],
|
||||
device.get_outputs().clone(),
|
||||
freezed_utxos,
|
||||
candidates_inputs,
|
||||
device.get_sp_client(),
|
||||
recipients,
|
||||
None,
|
||||
Amount::from_sat(fee_rate.into()),
|
||||
fee_rate,
|
||||
)?;
|
||||
|
||||
let unsigned_transaction = SpClient::finalize_transaction(tx)?;
|
||||
|
||||
log::debug!("{:#?}", unsigned_transaction.unsigned_tx);
|
||||
|
||||
Ok(unsigned_transaction)
|
||||
}
|
||||
|
||||
@ -1094,7 +1106,7 @@ pub fn create_transaction(addresses: Vec<String>, fee_rate: u32) -> ApiResult<Ap
|
||||
|
||||
let mut freezed_utxos = lock_freezed_utxos()?;
|
||||
|
||||
let partial_tx = create_transaction_for_addresses(&local_device, &freezed_utxos, &sp_addresses, fee_rate)?;
|
||||
let partial_tx = create_transaction_for_addresses(&local_device, &freezed_utxos, &sp_addresses, FeeRate::from_sat_per_vb(fee_rate as f32))?;
|
||||
let new_secrets = get_shared_secrets_in_transaction(&partial_tx, &sp_addresses)?;
|
||||
|
||||
let unsigned_tx = SpClient::finalize_transaction(partial_tx)?;
|
||||
@ -1150,7 +1162,9 @@ pub fn create_new_process(
|
||||
|
||||
let relay_address: SilentPaymentAddress = relay_address.try_into()?;
|
||||
|
||||
let tx = create_transaction_for_addresses(&local_device, &freezed_utxos, &vec![relay_address], fee_rate)?;
|
||||
let fee_rate_checked = FeeRate::from_sat_per_vb(fee_rate as f32);
|
||||
|
||||
let tx = create_transaction_for_addresses(&local_device, &freezed_utxos, &vec![relay_address], fee_rate_checked)?;
|
||||
|
||||
let unsigned_transaction = SpClient::finalize_transaction(tx)?;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user