-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.go
More file actions
76 lines (69 loc) · 1.36 KB
/
unit.go
File metadata and controls
76 lines (69 loc) · 1.36 KB
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
package ether
import (
"math/big"
"strings"
)
// Unit is a denomination of Ethereum currency by exponent value from Wei.
type Unit int
const (
Unknown Unit = -1
Wei Unit = 0
Kwei Unit = 3
Mwei Unit = 6
Gwei Unit = 9
Microether Unit = 12
Milliether Unit = 15
Ether Unit = 18
// Aliases
Babbage = Kwei
Lovelace = Mwei
Shannon = Gwei
Szabo = Microether
Finney = Milliether
Eth = Ether
)
// Num returns the denomination as a numerator in wei.
func (u Unit) Num() *big.Int {
r := big.NewInt(10)
return r.Exp(r, big.NewInt(int64(u)), nil)
}
// String returns the lowercase string representation of the denomination.
func (u Unit) String() string {
switch u {
case Wei:
return "wei"
case Kwei:
return "kwei"
case Mwei:
return "mwei"
case Gwei:
return "gwei"
case Microether:
return "microether"
case Milliether:
return "milliether"
case Ether:
return "ether"
}
return "unknown"
}
// ParseUnit takes a string and returns a unit. Unit is Unknown if parsing failed. It is case-insensitive.
func ParseUnit(s string) Unit {
switch strings.ToLower(strings.TrimSpace(s)) {
case "wei":
return Wei
case "kwei":
return Kwei
case "mwei":
return Mwei
case "gwei":
return Gwei
case "microether":
return Microether
case "milliether":
return Milliether
case "ether":
return Ether
}
return Unknown
}