-
Notifications
You must be signed in to change notification settings - Fork 282
Add more numeric utilities to MilliSatoshi #1103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f2866f2
package: small visual/comments clean-up
t-bast a835b06
Add more numeric utilities to MilliSatoshi
t-bast 4306fdc
Add msat/sat/mbtc/btc postfix to long
t-bast 5cd4b26
Add more overloads to maxOf / minOf
t-bast b126167
Move MilliSatoshi out of package object.
t-bast 4f1e119
fixup! Move MilliSatoshi out of package object. Provide utilities in …
t-bast a64ddc8
fixup! fixup! Move MilliSatoshi out of package object. Provide utilit…
t-bast 88a9a9c
Replace maxOf/minOf by max/min directly on MilliSatoshi
t-bast e3c096b
Update bitcoin-lib to 0.15-SNAPSHOT
t-bast 05f2c6a
Fix eclair-node-gui build
t-bast d880e1b
fixup! Fix eclair-node-gui build
t-bast 608a9de
Remove unnecessary calls to .toMilliSatoshi.
t-bast eef4658
Switch to bitcoin-lib 0.15.
t-bast 1ee4878
Merge master
t-bast 3cc8303
Use msat and sat postfix operators in tests.
t-bast 8e6c393
Merge master
t-bast 45c8b66
Add more msat postfix usage
t-bast a1f9d38
Small improvements and build fix
t-bast d20a95c
Fix Rusty's test spec
t-bast 276d95a
Use msat postfix in PaymentRequest
t-bast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
71 changes: 71 additions & 0 deletions
71
eclair-core/src/main/scala/fr/acinq/eclair/MilliSatoshi.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2019 ACINQ SAS | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package fr.acinq.eclair | ||
|
|
||
| import fr.acinq.bitcoin.{Btc, BtcAmount, MilliBtc, Satoshi, btc2satoshi, millibtc2satoshi} | ||
|
|
||
| /** | ||
| * Created by t-bast on 22/08/2019. | ||
| */ | ||
|
|
||
| /** | ||
| * One MilliSatoshi is a thousand of a Satoshi, the smallest unit usable in bitcoin | ||
| */ | ||
| case class MilliSatoshi(private val underlying: Long) extends Ordered[MilliSatoshi] { | ||
|
|
||
| // @formatter:off | ||
| def +(other: MilliSatoshi) = MilliSatoshi(underlying + other.underlying) | ||
| def +(other: BtcAmount) = MilliSatoshi(underlying + other.toMilliSatoshi.underlying) | ||
| def -(other: MilliSatoshi) = MilliSatoshi(underlying - other.underlying) | ||
| def -(other: BtcAmount) = MilliSatoshi(underlying - other.toMilliSatoshi.underlying) | ||
| def *(m: Long) = MilliSatoshi(underlying * m) | ||
| def *(m: Double) = MilliSatoshi((underlying * m).toLong) | ||
| def /(d: Long) = MilliSatoshi(underlying / d) | ||
| def unary_-() = MilliSatoshi(-underlying) | ||
|
|
||
| override def compare(other: MilliSatoshi): Int = underlying.compareTo(other.underlying) | ||
| // Since BtcAmount is a sealed trait that MilliSatoshi cannot extend, we need to redefine comparison operators. | ||
| def compare(other: BtcAmount): Int = compare(other.toMilliSatoshi) | ||
| def <=(other: BtcAmount): Boolean = compare(other) <= 0 | ||
| def >=(other: BtcAmount): Boolean = compare(other) >= 0 | ||
| def <(other: BtcAmount): Boolean = compare(other) < 0 | ||
| def >(other: BtcAmount): Boolean = compare(other) > 0 | ||
|
|
||
| // We provide asymmetric min/max functions to provide more control on the return type. | ||
| def max(other: MilliSatoshi): MilliSatoshi = if (this > other) this else other | ||
|
t-bast marked this conversation as resolved.
|
||
| def max(other: BtcAmount): MilliSatoshi = if (this > other) this else other.toMilliSatoshi | ||
| def min(other: MilliSatoshi): MilliSatoshi = if (this < other) this else other | ||
| def min(other: BtcAmount): MilliSatoshi = if (this < other) this else other.toMilliSatoshi | ||
|
|
||
| def truncateToSatoshi: Satoshi = Satoshi(underlying / 1000) | ||
| def toLong: Long = underlying | ||
| override def toString = s"$underlying msat" | ||
| // @formatter:on | ||
|
|
||
| } | ||
|
|
||
| object MilliSatoshi { | ||
|
|
||
| private def satoshi2millisatoshi(input: Satoshi): MilliSatoshi = MilliSatoshi(input.toLong * 1000L) | ||
|
|
||
| def toMilliSatoshi(amount: BtcAmount): MilliSatoshi = amount match { | ||
| case sat: Satoshi => satoshi2millisatoshi(sat) | ||
| case millis: MilliBtc => satoshi2millisatoshi(millibtc2satoshi(millis)) | ||
| case bitcoin: Btc => satoshi2millisatoshi(btc2satoshi(bitcoin)) | ||
| } | ||
|
|
||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.