github.com/decred/dcrlnd@v0.7.6/lnwire/mat.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/decred/dcrd/dcrutil/v4"
     7  )
     8  
     9  const (
    10  	// mAtomScale is a value that's used to scale atoms to milli-atoms, and
    11  	// the other way around.
    12  	mAtomScale uint64 = 1000
    13  
    14  	// MaxMilliAtom is the maximum number of msats that can be expressed in
    15  	// this data type.
    16  	MaxMilliAtom = ^MilliAtom(0)
    17  )
    18  
    19  // MilliAtom are the native unit of the Lightning Network. A milli-atom
    20  // is simply 1/1000th of an atom. There are 1000 milli-atoms in a single
    21  // atom. Within the network, all HTLC payments are denominated in
    22  // milli-atoms. As milli-atoms aren't deliverable on the native
    23  // blockchain, before settling to broadcasting, the values are rounded down to
    24  // the nearest atom.
    25  type MilliAtom uint64
    26  
    27  // NewMAtomsFromAtoms creates a new MilliAtom instance from a target amount
    28  // of Atoms.
    29  func NewMAtomsFromAtoms(at dcrutil.Amount) MilliAtom {
    30  	return MilliAtom(uint64(at) * mAtomScale)
    31  }
    32  
    33  // ToCoin converts the target MilliAtom amount to its corresponding value
    34  // when expressed in DCR.
    35  func (mat MilliAtom) ToCoin() float64 {
    36  	at := mat.ToAtoms()
    37  	return at.ToCoin()
    38  }
    39  
    40  // ToAtoms converts the target MilliAtom amount to atoms. Simply, this
    41  // sheds a factor of 1000 from the milli-atoms amount in order to convert it to Atoms.
    42  func (mat MilliAtom) ToAtoms() dcrutil.Amount {
    43  	return dcrutil.Amount(uint64(mat) / mAtomScale)
    44  }
    45  
    46  // String returns the string representation of the milli-atoms amount.
    47  func (mat MilliAtom) String() string {
    48  	return fmt.Sprintf("%v milli-atoms", uint64(mat))
    49  }
    50  
    51  // TODO(roasbeef): extend with arithmetic operations?