common.go 1.8 KB
Newer Older
obscuren's avatar
obscuren committed
1 2 3 4 5
package ethutil

import (
	"fmt"
	"math/big"
Maran's avatar
Maran committed
6
	"runtime"
obscuren's avatar
obscuren committed
7 8
)

Maran's avatar
Maran committed
9 10 11 12 13 14 15 16 17 18 19
func IsWindows() bool {
	return runtime.GOOS == "windows"
}

func WindonizePath(path string) string {
	if string(path[0]) == "/" && IsWindows() {
		path = path[1:]
	}
	return path
}

20
// The different number of units
obscuren's avatar
obscuren committed
21
var (
obscuren's avatar
obscuren committed
22 23 24 25 26 27 28 29 30
	Douglas  = BigPow(10, 42)
	Einstein = BigPow(10, 21)
	Ether    = BigPow(10, 18)
	Finney   = BigPow(10, 15)
	Szabo    = BigPow(10, 12)
	Shannon  = BigPow(10, 9)
	Babbage  = BigPow(10, 6)
	Ada      = BigPow(10, 3)
	Wei      = big.NewInt(1)
obscuren's avatar
obscuren committed
31 32
)

33
//
Maran's avatar
Maran committed
34
// Currency to string
35
// Returns a string representing a human readable format
obscuren's avatar
obscuren committed
36
func CurrencyToString(num *big.Int) string {
obscuren's avatar
obscuren committed
37 38 39 40 41
	var (
		fin   *big.Int = num
		denom string   = "Wei"
	)

obscuren's avatar
obscuren committed
42
	switch {
obscuren's avatar
obscuren committed
43
	case num.Cmp(Douglas) >= 0:
obscuren's avatar
obscuren committed
44 45
		fin = new(big.Int).Div(num, Douglas)
		denom = "Douglas"
obscuren's avatar
obscuren committed
46
	case num.Cmp(Einstein) >= 0:
obscuren's avatar
obscuren committed
47 48
		fin = new(big.Int).Div(num, Einstein)
		denom = "Einstein"
obscuren's avatar
obscuren committed
49
	case num.Cmp(Ether) >= 0:
obscuren's avatar
obscuren committed
50 51
		fin = new(big.Int).Div(num, Ether)
		denom = "Ether"
obscuren's avatar
obscuren committed
52
	case num.Cmp(Finney) >= 0:
obscuren's avatar
obscuren committed
53 54
		fin = new(big.Int).Div(num, Finney)
		denom = "Finney"
obscuren's avatar
obscuren committed
55
	case num.Cmp(Szabo) >= 0:
obscuren's avatar
obscuren committed
56 57
		fin = new(big.Int).Div(num, Szabo)
		denom = "Szabo"
Maran's avatar
Maran committed
58
	case num.Cmp(Shannon) >= 0:
obscuren's avatar
obscuren committed
59 60
		fin = new(big.Int).Div(num, Shannon)
		denom = "Shannon"
Maran's avatar
Maran committed
61
	case num.Cmp(Babbage) >= 0:
obscuren's avatar
obscuren committed
62 63
		fin = new(big.Int).Div(num, Babbage)
		denom = "Babbage"
Maran's avatar
Maran committed
64
	case num.Cmp(Ada) >= 0:
obscuren's avatar
obscuren committed
65 66 67 68
		fin = new(big.Int).Div(num, Ada)
		denom = "Ada"
	}

69
	// TODO add comment clarifying expected behavior
obscuren's avatar
obscuren committed
70 71
	if len(fin.String()) > 5 {
		return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
obscuren's avatar
obscuren committed
72 73
	}

obscuren's avatar
obscuren committed
74
	return fmt.Sprintf("%v %s", fin, denom)
obscuren's avatar
obscuren committed
75
}
76

77
// Common big integers often used
78
var (
79 80
	Big1     = big.NewInt(1)
	Big2     = big.NewInt(2)
obscuren's avatar
obscuren committed
81
	Big3     = big.NewInt(3)
82 83 84 85 86
	Big0     = big.NewInt(0)
	BigTrue  = Big1
	BigFalse = Big0
	Big32    = big.NewInt(32)
	Big256   = big.NewInt(0xff)
87
)