refactor: add msat model to encapsulate conversions#889
Open
refactor: add msat model to encapsulate conversions#889
Conversation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
3d2db8b to
286655e
Compare
73b5102 to
b3b2aeb
Compare
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
b3b2aeb to
ebc01a1
Compare
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
jvsena42
reviewed
Apr 6, 2026
Member
There was a problem hiding this comment.
@claude Give me a suggestions of instruction to CLAUDE.md to always use the convention from this class in sats operations
Contributor
There was a problem hiding this comment.
Claude finished @jvsena42's task in 52s —— View job
CLAUDE.md instruction suggestions for MSat convention
Here are the suggested rules to add to the Rules section in CLAUDE.md, following the existing style:
- ALWAYS use `MSat(value).ceil()` or `msatCeilOf(value)` when converting millisats to sats rounding **up** (payment amounts, display amounts, minimum bounds); NEVER use raw `(x + 999u) / 1000u`
- ALWAYS use `MSat(value).floor()` or `msatFloorOf(value)` when converting millisats to sats rounding **down** (fees, upper bounds, capacity); NEVER use raw `x / 1000u` or `x / MSATS_PER_SAT`
- ALWAYS use `MSat.PER_SAT` when multiplying sats to millisats; NEVER use the raw literal `1000u` for sat→msat conversions
- NEVER mix `.ceil()` and `.floor()` for the same semantic value — use `.ceil()` for minimum/payment amounts and `.floor()` for maximum/fee amountsRationale per rule:
| Rule | Why |
|---|---|
msatCeilOf for payment amounts |
Rounding down a minimum produces a sats value that, when multiplied back × 1000, falls below the protocol minimum and can cause LNURL-Withdraw rejections |
msatFloorOf for fees/upper bounds |
Rounding up a fee overestimates cost; rounding up a channel capacity could exceed actual balance |
MSat.PER_SAT for sat→msat |
Prevents magic number 1000u from reappearing after MSATS_PER_SAT was removed in this PR |
| No mixing ceil/floor | A wrong rounding direction is a silent correctness bug — the rule forces the author to consciously pick the right one |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces an
MSatinline value class that replaces all scattered(x + 999u) / 1000uceiling andx / 1000ufloor patterns with self-documenting conversions.Description
The msat-to-sat conversions were spread across 13 files with no domain type to explain what's happening. After PR #879 added more ceiling conversions, it became harder to understand the intent behind
(x + 999u) / 1000uwithout reading the PR context. The newMSatvalue class (zero runtime overhead) makes the rounding intent self-documenting.MSatinline value class inmodels/withceil()andfloor()functionsmsatCeilOf()andmsatFloorOf()top-level sugar functions for concise call sites(x + 999u) / 1000u,msatsToSatsCeil) withmsatCeilOf(x)x / 1000u,x / MSATS_PER_SAT) withmsatFloorOf(x)msatsToSatsCeil()function andMSATS_PER_SATconstant fromLnurl.ktMSat.PER_SATconstant for the sat→msat directionMSatTest.ktwith 10 test casesPreview
N/A — no UI changes, pure refactor.
QA Notes
All tests listed and checked below were verified manually, this is a refactoring PR which shouldn't require extensive retesting by reviewer.
Tests
All tests should use msat values: