-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversion.go
346 lines (296 loc) · 9.83 KB
/
conversion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2019 - 2023 Weald Technology Trading Limited.
//
// 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.
// string2eth provides methods for converting between number of Wei and a string
// represetation of the same, with the latter allowing all commonly-used
// representations as inputs. String outputs are provided in a sensible format
// given the value.
package string2eth
import (
"errors"
"fmt"
"math/big"
"regexp"
"strings"
)
var (
ErrEmptyValue = errors.New("failed to parse empty value")
ErrInvalidFormat = errors.New("invalid format")
ErrNegative = errors.New("value resulted in negative number of Wei")
ErrFractional = errors.New("value resulted in fractional number of Wei")
ErrUnknownUnit = errors.New("unknown unit")
ErrParseFailure = errors.New("failed to parse")
)
// StringToWei turns a string in to number of Wei.
// The string can be a simple number of Wei, e.g. "1000000000000000" or it can
// be a number followed by a unit, e.g. "10 ether". Unit names are
// case-insensitive, and can be either given names (e.g. "finney") or metric
// names (e.g. "mlliether").
// Note that this function expects use of the period as the decimal separator.
func StringToWei(input string) (*big.Int, error) {
if input == "" {
return nil, ErrEmptyValue
}
// Remove unused runes that may be in an input string.
input = strings.ReplaceAll(input, " ", "")
input = strings.ReplaceAll(input, "_", "")
var result big.Int
// Separate the number from the unit (if any)
re := regexp.MustCompile(`^(-?[0-9]*(?:\.[0-9]*)?)([A-Za-z]+)?$`)
subMatches := re.FindAllStringSubmatch(input, -1)
var units string
if len(subMatches) != 1 {
return nil, ErrInvalidFormat
}
units = subMatches[0][2]
if strings.Contains(subMatches[0][1], ".") {
err := decimalStringToWei(subMatches[0][1], units, &result)
if err != nil {
return nil, err
}
} else {
err := integerStringToWei(subMatches[0][1], units, &result)
if err != nil {
return nil, err
}
}
// Ensure we don't have a negative number.
if result.Cmp(new(big.Int)) < 0 {
return nil, ErrNegative
}
return &result, nil
}
// StringToGWei turns a string in to number of GWei.
// See StringToWei for details.
// Any part of the value below 1GWei in denomination is lost.
func StringToGWei(input string) (uint64, error) {
wei, err := StringToWei(input)
if err != nil {
return 0, err
}
return wei.Div(wei, billion).Uint64(), nil
}
// Used in WeiToString.
var (
zero = big.NewInt(0)
thousand = big.NewInt(1000)
)
// Used in GWeiToString.
var billion = big.NewInt(1000000000)
// GWeiToString turns a number of GWei in to a string.
// See WeiToString for details.
func GWeiToString(input uint64, standard bool) string {
return WeiToString(new(big.Int).Mul(new(big.Int).SetUint64(input), billion), standard)
}
// WeiToGWeiString turns a number of wei in to a Gwei string.
func WeiToGWeiString(input *big.Int) string {
if input == nil {
return "0"
}
intValue := new(big.Int).Div(input, billion)
decValue := new(big.Int).Sub(input, new(big.Int).Mul(intValue, billion))
// Return our value.
if decValue.Cmp(zero) == 0 {
return fmt.Sprintf("%s GWei", intValue)
}
decStr := strings.TrimRight(fmt.Sprintf("%09d", decValue.Int64()), "0")
return fmt.Sprintf("%s.%s GWei", intValue, decStr)
}
// WeiToString turns a number of Wei in to a string.
// If the 'standard' argument is true then this will display the value
// in either (KMG)Wei or Ether only.
func WeiToString(input *big.Int, standard bool) string {
if input == nil {
return "0"
}
// Take a copy of the input so that we can mutate it.
value := new(big.Int).Set(input)
// Short circuit on 0.
if value.Cmp(zero) == 0 {
return "0"
}
// Step 1: work out simple units, keeping value as a whole number.
value, unitPos := weiToStringStep1(value)
// Step 2: move value to a fraction if sensible.
outputValue, unitPos, desiredUnitPos, decimalPlace := weiToStringStep2(value, unitPos, standard)
// Step 3: generate output.
outputValue, unitPos = weiToStringStep3(outputValue, unitPos, desiredUnitPos, decimalPlace)
if unitPos >= len(metricUnits) {
return "overflow"
}
// Return our value.
return fmt.Sprintf("%s %s", outputValue, metricUnits[unitPos])
}
// weiToStringStep1 steps the value down by thousands to obtain a smaller value
// with unit reference.
func weiToStringStep1(value *big.Int) (*big.Int, int) {
unitPos := 0
modInt := new(big.Int).Set(value)
for value.Cmp(thousand) >= 0 && modInt.Mod(value, thousand).Cmp(zero) == 0 {
unitPos++
value = value.Div(value, thousand)
modInt = modInt.Set(value)
}
return value, unitPos
}
// weiToStringStep2 starts to turn a value into a string, handling the case where
// the resultant output may be a decial.
func weiToStringStep2(value *big.Int, unitPos int, standard bool) (string, int, int, int) {
// Because of the inaccuracy of floating point we use string manipulation
// to place the decimal in the correct position.
outputValue := value.Text(10)
desiredUnitPos := unitPos
if len(outputValue) > 3 {
desiredUnitPos += len(outputValue) / 3
if len(outputValue)%3 == 0 {
desiredUnitPos--
}
}
decimalPlace := len(outputValue)
if desiredUnitPos > 3 && standard {
// Because Gwei covers a large range allow anything up to 0.001 ETH
// to display as Gwei.
if desiredUnitPos == 4 {
desiredUnitPos = 3
} else {
desiredUnitPos = 6
}
}
for unitPos < desiredUnitPos {
decimalPlace -= 3
unitPos++
}
return outputValue, unitPos, desiredUnitPos, decimalPlace
}
// weiToStringStep3 finishes generation of the output value, ensuring the appropriate
// number of 0s and tidying up to provide a presentable result.
func weiToStringStep3(outputValue string, unitPos int, desiredUnitPos int, decimalPlace int) (string, int) {
for unitPos > desiredUnitPos {
outputValue += strings.Repeat("0", 3)
decimalPlace += 3
unitPos--
}
if decimalPlace <= 0 {
outputValue = "0." + strings.Repeat("0", 0-decimalPlace) + outputValue
} else if decimalPlace < len(outputValue) {
outputValue = outputValue[:decimalPlace] + "." + outputValue[decimalPlace:]
}
// Trim trailing zeros if this is a decimal.
if strings.Contains(outputValue, ".") {
outputValue = strings.TrimRight(outputValue, "0")
}
return outputValue, unitPos
}
func decimalStringToWei(amount string, unit string, result *big.Int) error {
// Because floating point maths is not accurate we need to break potentially
// large decimal fractions in to two separate pieces: the integer part and the
// decimal part.
parts := strings.Split(amount, ".")
// The value for the integer part of the number is easy.
if parts[0] != "" {
err := integerStringToWei(parts[0], unit, result)
if err != nil {
return fmt.Errorf("%w %s %s", ErrParseFailure, amount, unit)
}
}
// The value for the decimal part of the number is harder. We left-shift it
// so that we end up multiplying two integers rather than two floats, as the
// latter is unreliable.
// Obtain multiplier.
// This will never fail because it is already called above in integerStringToWei().
multiplier, _ := UnitToMultiplier(unit)
// Trim trailing 0s.
trimmedDecimal := strings.TrimRight(parts[1], "0")
if len(trimmedDecimal) == 0 {
// Nothing more to do.
return nil
}
var decVal big.Int
decVal.SetString(trimmedDecimal, 10)
// Divide multiplier by 10^len(trimmed decimal) to obtain sane value.
div := big.NewInt(10)
for i := 0; i < len(trimmedDecimal); i++ {
multiplier.Div(multiplier, div)
}
// Ensure we don't have a fractional number of Wei.
if multiplier.Sign() == 0 {
return ErrFractional
}
var decResult big.Int
decResult.Mul(multiplier, &decVal)
// Add it to the integer result.
result.Add(result, &decResult)
return nil
}
func integerStringToWei(amount string, unit string, result *big.Int) error {
// Obtain number.
number := new(big.Int)
_, success := number.SetString(amount, 10)
if !success {
return fmt.Errorf("%w %s %s", ErrParseFailure, amount, unit)
}
// Obtain multiplier.
multiplier, err := UnitToMultiplier(unit)
if err != nil {
return fmt.Errorf("%w %s %s", ErrParseFailure, amount, unit)
}
result.Mul(number, multiplier)
return nil
}
// Metric units.
var metricUnits = [...]string{
"Wei",
"KWei",
"MWei",
"GWei",
"Microether",
"Milliether",
"Ether",
"Kiloether",
"Megaether",
"Gigaether",
"Teraether",
}
// UnitToMultiplier takes the name of an Ethereum unit and returns a multiplier.
//
//nolint:cyclop
func UnitToMultiplier(unit string) (*big.Int, error) {
result := big.NewInt(0)
switch strings.ToLower(unit) {
case "", "wei":
result.SetString("1", 10)
case "ada", "kwei", "kilowei":
result.SetString("1000", 10)
case "babbage", "mwei", "megawei":
result.SetString("1000000", 10)
case "shannon", "gwei", "gigawei":
result.SetString("1000000000", 10)
case "szazbo", "micro", "microether":
result.SetString("1000000000000", 10)
case "finney", "milli", "milliether":
result.SetString("1000000000000000", 10)
case "eth", "ether":
result.SetString("1000000000000000000", 10)
case "einstein", "kilo", "kiloether":
result.SetString("1000000000000000000000", 10)
case "mega", "megaether":
result.SetString("1000000000000000000000000", 10)
case "giga", "gigaether":
result.SetString("1000000000000000000000000000", 10)
case "tera", "teraether":
result.SetString("1000000000000000000000000000000", 10)
default:
return nil, fmt.Errorf("%w %s", ErrUnknownUnit, unit)
}
return result, nil
}