2017-08-22 07:28:24 +02:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2017-08-22 07:28:24 +02:00
|
|
|
)
|
|
|
|
|
2019-09-30 13:14:49 +02:00
|
|
|
const (
|
|
|
|
// mSatScale is a value that's used to scale satoshis to milli-satoshis, and
|
|
|
|
// the other way around.
|
|
|
|
mSatScale uint64 = 1000
|
|
|
|
|
|
|
|
// MaxMilliSatoshi is the maximum number of msats that can be expressed
|
|
|
|
// in this data type.
|
|
|
|
MaxMilliSatoshi = ^MilliSatoshi(0)
|
|
|
|
)
|
2017-08-22 07:28:24 +02:00
|
|
|
|
|
|
|
// MilliSatoshi are the native unit of the Lightning Network. A milli-satoshi
|
2017-08-25 15:22:25 +02:00
|
|
|
// is simply 1/1000th of a satoshi. There are 1000 milli-satoshis in a single
|
2017-08-22 07:28:24 +02:00
|
|
|
// satoshi. Within the network, all HTLC payments are denominated in
|
|
|
|
// milli-satoshis. As milli-satoshis aren't deliverable on the native
|
|
|
|
// blockchain, before settling to broadcasting, the values are rounded down to
|
|
|
|
// the nearest satoshi.
|
2018-02-25 04:08:43 +01:00
|
|
|
type MilliSatoshi uint64
|
2017-08-22 07:28:24 +02:00
|
|
|
|
|
|
|
// NewMSatFromSatoshis creates a new MilliSatoshi instance from a target amount
|
|
|
|
// of satoshis.
|
|
|
|
func NewMSatFromSatoshis(sat btcutil.Amount) MilliSatoshi {
|
2018-02-25 04:08:43 +01:00
|
|
|
return MilliSatoshi(uint64(sat) * mSatScale)
|
2017-08-22 07:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToBTC converts the target MilliSatoshi amount to its corresponding value
|
|
|
|
// when expressed in BTC.
|
|
|
|
func (m MilliSatoshi) ToBTC() float64 {
|
|
|
|
sat := m.ToSatoshis()
|
|
|
|
return sat.ToBTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSatoshis converts the target MilliSatoshi amount to satoshis. Simply, this
|
|
|
|
// sheds a factor of 1000 from the mSAT amount in order to convert it to SAT.
|
|
|
|
func (m MilliSatoshi) ToSatoshis() btcutil.Amount {
|
2018-02-25 04:08:43 +01:00
|
|
|
return btcutil.Amount(uint64(m) / mSatScale)
|
2017-08-22 07:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of the mSAT amount.
|
|
|
|
func (m MilliSatoshi) String() string {
|
2018-02-25 04:08:43 +01:00
|
|
|
return fmt.Sprintf("%v mSAT", uint64(m))
|
2017-08-22 07:28:24 +02:00
|
|
|
}
|
2018-02-25 04:08:43 +01:00
|
|
|
|
|
|
|
// TODO(roasbeef): extend with arithmetic operations?
|