Skip to content

Dynamic Fee Calculation for Different Address Types #258

@bennyhodl

Description

@bennyhodl

The current fee calculation logic assumes all payout script pubkeys are P2WPKH, leading to inaccurate fee estimation when contracts use different address types such as multi-sig, legacy addresses, Pay-to-Script-Hash (P2SH), or Pay-to-Taproot (P2TR).

Problem

The codebase uses hardcoded assumptions about script pubkey sizes and witness costs:

Current behavior:

  • Assumes P2WPKH for all payout calculations
  • Uses fixed weight/size values regardless of actual script type
  • Results in under/over-estimation of required fees

Desired behavior:

  • Dynamic fee calculation based on actual script pubkey type
  • Accurate weight estimation for different address formats
  • Support for all common Bitcoin address types

Technical Impact

Fee Calculation Areas

  • CET Output Fees: Payout script pubkey size affects transaction weight
  • Refund Transaction Fees: Return address script type impacts costs
  • Change Output Fees: Wallet change address type considerations
  • Input Spending Fees: Different input types have different witness costs

Implementation Approach

Script Type Detection

  • Analyze script pubkey to determine address type
  • Use bitcoin::Script methods for type identification
  • Handle both input and output script types

Dynamic Weight Calculation

  • Replace hardcoded weight values with script-type-specific calculations
  • Account for witness vs non-witness script types
  • Include multi-sig threshold and key count in calculations

Fee Estimation Updates

  • Update weight_to_fee calculations to be script-aware
  • Modify party parameter calculations to use actual script costs
  • Ensure accuracy across different contract configurations

Example Use Cases

  • Multi-sig DLCs: Corporate contracts requiring multiple signatures
  • Legacy Integration: Supporting older wallet infrastructure
  • Taproot Contracts: Privacy-enhanced DLCs with script-path conditions
  • Mixed Environments: Parties using different wallet types

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions