05d05ac5ee
This commit adds a new type to the lnwire package: MilliSatoshi. A milli-satoshi is simply 1/1000th of a satoshi, and will be used for all internal accounting when sending payments, calculating fees, updating commitment state, etc. Two helper methods are added: ToBTC(), and ToSatoshis() to make manipulation of the values easy.
78 lines
1.3 KiB
Go
78 lines
1.3 KiB
Go
package lnwire
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
)
|
|
|
|
func TestMilliSatoshiConversion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
mSatAmount MilliSatoshi
|
|
|
|
satAmount btcutil.Amount
|
|
btcAmount float64
|
|
}{
|
|
{
|
|
mSatAmount: 0,
|
|
satAmount: 0,
|
|
btcAmount: 0,
|
|
},
|
|
{
|
|
mSatAmount: 10,
|
|
satAmount: 0,
|
|
btcAmount: 0,
|
|
},
|
|
{
|
|
mSatAmount: 999,
|
|
satAmount: 0,
|
|
btcAmount: 0,
|
|
},
|
|
{
|
|
mSatAmount: 1000,
|
|
satAmount: 1,
|
|
btcAmount: 1e-8,
|
|
},
|
|
{
|
|
mSatAmount: 10000,
|
|
satAmount: 10,
|
|
btcAmount: 0.00000010,
|
|
},
|
|
{
|
|
mSatAmount: 100000000000,
|
|
satAmount: 100000000,
|
|
btcAmount: 1,
|
|
},
|
|
{
|
|
mSatAmount: 2500000000000,
|
|
satAmount: 2500000000,
|
|
btcAmount: 25,
|
|
},
|
|
{
|
|
mSatAmount: 5000000000000,
|
|
satAmount: 5000000000,
|
|
btcAmount: 50,
|
|
},
|
|
{
|
|
mSatAmount: 21 * 1e6 * 1e8 * 1e3,
|
|
satAmount: 21 * 1e6 * 1e8,
|
|
btcAmount: 21 * 1e6,
|
|
},
|
|
}
|
|
|
|
for i, test := range testCases {
|
|
if test.mSatAmount.ToSatoshis() != test.satAmount {
|
|
t.Fatalf("test #%v: wrong sat amount, expected %v "+
|
|
"got %v", i, int64(test.satAmount),
|
|
int64(test.mSatAmount.ToSatoshis()))
|
|
}
|
|
if test.mSatAmount.ToBTC() != test.btcAmount {
|
|
t.Fatalf("test #%v: wrong btc amount, expected %v "+
|
|
"got %v", i, test.btcAmount,
|
|
test.mSatAmount.ToBTC())
|
|
}
|
|
}
|
|
}
|