common.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package ethutil
  2. import (
  3. "fmt"
  4. "math/big"
  5. "runtime"
  6. )
  7. func IsWindows() bool {
  8. return runtime.GOOS == "windows"
  9. }
  10. func WindonizePath(path string) string {
  11. if string(path[0]) == "/" && IsWindows() {
  12. path = path[1:]
  13. }
  14. return path
  15. }
  16. // The different number of units
  17. var (
  18. Douglas = BigPow(10, 42)
  19. Einstein = BigPow(10, 21)
  20. Ether = BigPow(10, 18)
  21. Finney = BigPow(10, 15)
  22. Szabo = BigPow(10, 12)
  23. Shannon = BigPow(10, 9)
  24. Babbage = BigPow(10, 6)
  25. Ada = BigPow(10, 3)
  26. Wei = big.NewInt(1)
  27. )
  28. //
  29. // Currency to string
  30. // Returns a string representing a human readable format
  31. func CurrencyToString(num *big.Int) string {
  32. var (
  33. fin *big.Int = num
  34. denom string = "Wei"
  35. )
  36. switch {
  37. case num.Cmp(Douglas) >= 0:
  38. fin = new(big.Int).Div(num, Douglas)
  39. denom = "Douglas"
  40. case num.Cmp(Einstein) >= 0:
  41. fin = new(big.Int).Div(num, Einstein)
  42. denom = "Einstein"
  43. case num.Cmp(Ether) >= 0:
  44. fin = new(big.Int).Div(num, Ether)
  45. denom = "Ether"
  46. case num.Cmp(Finney) >= 0:
  47. fin = new(big.Int).Div(num, Finney)
  48. denom = "Finney"
  49. case num.Cmp(Szabo) >= 0:
  50. fin = new(big.Int).Div(num, Szabo)
  51. denom = "Szabo"
  52. case num.Cmp(Shannon) >= 0:
  53. fin = new(big.Int).Div(num, Shannon)
  54. denom = "Shannon"
  55. case num.Cmp(Babbage) >= 0:
  56. fin = new(big.Int).Div(num, Babbage)
  57. denom = "Babbage"
  58. case num.Cmp(Ada) >= 0:
  59. fin = new(big.Int).Div(num, Ada)
  60. denom = "Ada"
  61. }
  62. // TODO add comment clarifying expected behavior
  63. if len(fin.String()) > 5 {
  64. return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
  65. }
  66. return fmt.Sprintf("%v %s", fin, denom)
  67. }
  68. // Common big integers often used
  69. var (
  70. Big1 = big.NewInt(1)
  71. Big2 = big.NewInt(2)
  72. Big3 = big.NewInt(3)
  73. Big0 = big.NewInt(0)
  74. BigTrue = Big1
  75. BigFalse = Big0
  76. Big32 = big.NewInt(32)
  77. Big256 = big.NewInt(0xff)
  78. )