• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13035292482

29 Jan 2025 03:59PM UTC coverage: 49.3% (-9.5%) from 58.777%
13035292482

Pull #9456

github

mohamedawnallah
docs: update release-notes-0.19.0.md

In this commit, we warn users about the removal
of RPCs `SendToRoute`, `SendToRouteSync`, `SendPayment`,
and `SendPaymentSync` in the next release 0.20.
Pull Request #9456: lnrpc+docs: deprecate warning `SendToRoute`, `SendToRouteSync`, `SendPayment`, and `SendPaymentSync` in Release 0.19

100634 of 204126 relevant lines covered (49.3%)

1.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.88
/zpay32/amountunits.go
1
package zpay32
2

3
import (
4
        "fmt"
5
        "strconv"
6

7
        "github.com/lightningnetwork/lnd/lnwire"
8
)
9

10
var (
11
        // toMSat is a map from a unit to a function that converts an amount
12
        // of that unit to millisatoshis.
13
        toMSat = map[byte]func(uint64) (lnwire.MilliSatoshi, error){
14
                'm': mBtcToMSat,
15
                'u': uBtcToMSat,
16
                'n': nBtcToMSat,
17
                'p': pBtcToMSat,
18
        }
19

20
        // fromMSat is a map from a unit to a function that converts an amount
21
        // in millisatoshis to an amount of that unit.
22
        fromMSat = map[byte]func(lnwire.MilliSatoshi) (uint64, error){
23
                'm': mSatToMBtc,
24
                'u': mSatToUBtc,
25
                'n': mSatToNBtc,
26
                'p': mSatToPBtc,
27
        }
28
)
29

30
// mBtcToMSat converts the given amount in milliBTC to millisatoshis.
31
func mBtcToMSat(m uint64) (lnwire.MilliSatoshi, error) {
3✔
32
        return lnwire.MilliSatoshi(m) * 100000000, nil
3✔
33
}
3✔
34

35
// uBtcToMSat converts the given amount in microBTC to millisatoshis.
36
func uBtcToMSat(u uint64) (lnwire.MilliSatoshi, error) {
3✔
37
        return lnwire.MilliSatoshi(u * 100000), nil
3✔
38
}
3✔
39

40
// nBtcToMSat converts the given amount in nanoBTC to millisatoshis.
41
func nBtcToMSat(n uint64) (lnwire.MilliSatoshi, error) {
3✔
42
        return lnwire.MilliSatoshi(n * 100), nil
3✔
43
}
3✔
44

45
// pBtcToMSat converts the given amount in picoBTC to millisatoshis.
46
func pBtcToMSat(p uint64) (lnwire.MilliSatoshi, error) {
×
47
        if p < 10 {
×
48
                return 0, fmt.Errorf("minimum amount is 10p")
×
49
        }
×
50
        if p%10 != 0 {
×
51
                return 0, fmt.Errorf("amount %d pBTC not expressible in msat",
×
52
                        p)
×
53
        }
×
54
        return lnwire.MilliSatoshi(p / 10), nil
×
55
}
56

57
// mSatToMBtc converts the given amount in millisatoshis to milliBTC.
58
func mSatToMBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
59
        if msat%100000000 != 0 {
6✔
60
                return 0, fmt.Errorf("%d msat not expressible "+
3✔
61
                        "in mBTC", msat)
3✔
62
        }
3✔
63
        return uint64(msat / 100000000), nil
3✔
64
}
65

66
// mSatToUBtc converts the given amount in millisatoshis to microBTC.
67
func mSatToUBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
68
        if msat%100000 != 0 {
6✔
69
                return 0, fmt.Errorf("%d msat not expressible "+
3✔
70
                        "in uBTC", msat)
3✔
71
        }
3✔
72
        return uint64(msat / 100000), nil
3✔
73
}
74

75
// mSatToNBtc converts the given amount in millisatoshis to nanoBTC.
76
func mSatToNBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
77
        if msat%100 != 0 {
3✔
78
                return 0, fmt.Errorf("%d msat not expressible in nBTC", msat)
×
79
        }
×
80
        return uint64(msat / 100), nil
3✔
81
}
82

83
// mSatToPBtc converts the given amount in millisatoshis to picoBTC.
84
func mSatToPBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
85
        return uint64(msat * 10), nil
3✔
86
}
3✔
87

88
// decodeAmount returns the amount encoded by the provided string in
89
// millisatoshi.
90
func decodeAmount(amount string) (lnwire.MilliSatoshi, error) {
3✔
91
        if len(amount) < 1 {
3✔
92
                return 0, fmt.Errorf("amount must be non-empty")
×
93
        }
×
94

95
        // If last character is a digit, then the amount can just be
96
        // interpreted as BTC.
97
        char := amount[len(amount)-1]
3✔
98
        digit := char - '0'
3✔
99
        if digit >= 0 && digit <= 9 {
3✔
100
                btc, err := strconv.ParseUint(amount, 10, 64)
×
101
                if err != nil {
×
102
                        return 0, err
×
103
                }
×
104
                return lnwire.MilliSatoshi(btc) * mSatPerBtc, nil
×
105
        }
106

107
        // If not a digit, it must be part of the known units.
108
        conv, ok := toMSat[char]
3✔
109
        if !ok {
3✔
110
                return 0, fmt.Errorf("unknown multiplier %c", char)
×
111
        }
×
112

113
        // Known unit.
114
        num := amount[:len(amount)-1]
3✔
115
        if len(num) < 1 {
3✔
116
                return 0, fmt.Errorf("number must be non-empty")
×
117
        }
×
118

119
        am, err := strconv.ParseUint(num, 10, 64)
3✔
120
        if err != nil {
3✔
121
                return 0, err
×
122
        }
×
123

124
        return conv(am)
3✔
125
}
126

127
// encodeAmount encodes the provided millisatoshi amount using as few characters
128
// as possible.
129
func encodeAmount(msat lnwire.MilliSatoshi) (string, error) {
3✔
130
        // If possible to express in BTC, that will always be the shortest
3✔
131
        // representation.
3✔
132
        if msat%mSatPerBtc == 0 {
3✔
133
                return strconv.FormatInt(int64(msat/mSatPerBtc), 10), nil
×
134
        }
×
135

136
        // Should always be expressible in pico BTC.
137
        pico, err := fromMSat['p'](msat)
3✔
138
        if err != nil {
3✔
139
                return "", fmt.Errorf("unable to express %d msat as pBTC: %w",
×
140
                        msat, err)
×
141
        }
×
142
        shortened := strconv.FormatUint(pico, 10) + "p"
3✔
143
        for unit, conv := range fromMSat {
6✔
144
                am, err := conv(msat)
3✔
145
                if err != nil {
6✔
146
                        // Not expressible using this unit.
3✔
147
                        continue
3✔
148
                }
149

150
                // Save the shortest found representation.
151
                str := strconv.FormatUint(am, 10) + string(unit)
3✔
152
                if len(str) < len(shortened) {
6✔
153
                        shortened = str
3✔
154
                }
3✔
155
        }
156

157
        return shortened, nil
3✔
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc