Function validate_and_subtract_fee_min_dust

Source
pub fn validate_and_subtract_fee_min_dust(
    amount: Amount,
    fee: Amount,
) -> Result<Amount, FeeValidationError>
Expand description

Validates fee amounts and calculates the resulting amount after fee.

This function ensures two critical conditions are met:

  1. Fee doesn’t exceed the original amount (prevents overflow)
  2. Amount after fee is >= P2TR_DUST (ensures economically viable output)

§Returns

  • Ok(Amount) - The amount after subtracting the fee
  • Err(FeeValidationError) - If any validation condition fails

§Example

use ark::fees::{validate_and_subtract_fee_min_dust, FeeValidationError};
use bitcoin::Amount;
use bitcoin_ext::P2TR_DUST;

let amount = Amount::from_sat(10_000);
let fee = Amount::from_sat(100);
let result = validate_and_subtract_fee_min_dust(amount, fee);
assert_eq!(result.unwrap(), Amount::from_sat(9_900));

let amount = Amount::from_sat(10_000);
let fee = Amount::from_sat(9_670);
let result = validate_and_subtract_fee_min_dust(amount, fee);
assert_eq!(result.unwrap(), P2TR_DUST);

let amount = Amount::from_sat(10_000);
let fee = Amount::from_sat(11_000);
let result = validate_and_subtract_fee_min_dust(amount, fee);
assert_eq!(result.unwrap_err(), FeeValidationError::FeeExceedsAmount { amount, fee });

let amount = Amount::from_sat(10_000);
let fee = Amount::from_sat(10_000);
let result = validate_and_subtract_fee_min_dust(amount, fee);
assert_eq!(result.unwrap_err(), FeeValidationError::AmountAfterFeeBelowDust {
	amount,
	fee,
	amount_after_fee: amount - fee,
});