Skip to content

fix: Handle number inputs correctly in computeRoundingModulus#225

Merged
matthewjablack merged 1 commit into
masterfrom
fix/builder-number-input-handling
Jul 14, 2025
Merged

fix: Handle number inputs correctly in computeRoundingModulus#225
matthewjablack merged 1 commit into
masterfrom
fix/builder-number-input-handling

Conversation

@matthewjablack
Copy link
Copy Markdown
Contributor

Summary

This PR fixes a bug in the computeRoundingModulus function where number inputs were incorrectly treated as raw satoshi values instead of bitcoin amounts.

Problem

The computeRoundingModulus function accepts three types of inputs:

  • Value objects (which have a .sats property)
  • BigInt values (raw satoshi amounts)
  • number values (intended to represent bitcoin amounts)

However, the function was treating number inputs as raw satoshi values, which caused incorrect calculations when users passed bitcoin amounts like 0.001 or 1.5.

Solution

  • Number inputs: Now properly converted to satoshis by multiplying by 1e8 and rounding
  • Value objects: Unchanged behavior (uses .sats property)
  • BigInt inputs: Unchanged behavior (used directly as satoshi values)

Changes Made

  • Updated computeRoundingModulus to use proper type checking and conversion
  • Added comprehensive test coverage for all input types:
    • Bitcoin amount inputs (new functionality)
    • Value object inputs
    • BigInt inputs (raw satoshi values)
    • Mixed input scenarios
    • Edge cases (zero values, very small amounts)
  • Updated existing tests to use BigInt for raw satoshi values

Backward Compatibility

  • Breaking: Number inputs now represent bitcoin amounts instead of satoshi values
  • Non-breaking: Value objects and BigInt inputs work exactly as before
  • Existing code using Value objects or BigInt values is unaffected

Example Usage

// Before (incorrect): number treated as satoshis
computeRoundingModulus(100000, 100000000); // 100k sats, 100M sats

// After (correct): number treated as bitcoin amounts
computeRoundingModulus(0.001, 1.0);       // 0.001 BTC, 1.0 BTC
computeRoundingModulus(BigInt(100000), BigInt(100000000)); // For raw satoshi values

This fix ensures that the function behaves intuitively when working with bitcoin amounts while maintaining full compatibility with existing code that uses proper types.

@matthewjablack matthewjablack force-pushed the fix/builder-number-input-handling branch from ac7b571 to 6e7ea27 Compare July 14, 2025 22:50
- convert number inputs to satoshis (multiply by 1e8)
- preserve existing behavior for Value objects and BigInt inputs
- add comprehensive test coverage for all input types
- update existing tests to use BigInt for raw satoshi values

Previously, number inputs were incorrectly treated as raw satoshi
values instead of bitcoin amounts, leading to incorrect calculations.
@matthewjablack matthewjablack force-pushed the fix/builder-number-input-handling branch from 6e7ea27 to e5c362b Compare July 14, 2025 22:53
@matthewjablack matthewjablack merged commit 999e392 into master Jul 14, 2025
2 checks passed
@matthewjablack matthewjablack deleted the fix/builder-number-input-handling branch July 14, 2025 22:58
matthewjablack added a commit that referenced this pull request Jul 16, 2025
- feat: add single funded DLC support to messaging (#224)
- fix: handle number inputs in computeRoundingModulus (#225)
- chore: upgrade ESLint tooling & improve code quality (#226)
- update: bump all package versions to 1.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant