-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMSat.kt
More file actions
26 lines (20 loc) · 871 Bytes
/
MSat.kt
File metadata and controls
26 lines (20 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package to.bitkit.models
/**
* A non-boxing wrapper for millisatoshi [ULong] values that provides explicit sat conversions.
*
* Encapsulates the rounding intent directly in the API: [ceil] rounds up, [floor] truncates.
*/
@JvmInline
value class MSat(val value: ULong) {
companion object {
const val PER_SAT: ULong = 1000u
}
/** Round up to the nearest whole sat. Use for payment/display amounts. */
fun ceil(): ULong = (value + PER_SAT - 1u) / PER_SAT
/** Truncate sub-sat remainder. Use for fees and upper bounds. */
fun floor(): ULong = value / PER_SAT
}
/** Round [msat] up to the nearest whole sat. Use for payment/display amounts. */
fun msatCeilOf(msat: ULong): ULong = MSat(msat).ceil()
/** Truncate sub-sat remainder from [msat]. Use for fees and upper bounds. */
fun msatFloorOf(msat: ULong): ULong = MSat(msat).floor()