util.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package util
  2. import (
  3. "github.com/ethereum/go-ethereum/common/math"
  4. "math/big"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. var Ether = math.BigPow(10, 18)
  10. var DGC = math.BigPow(10, 16)
  11. var Shannon = math.BigPow(10, 9)
  12. type JsonRpcResponseGetter struct {
  13. m map[string]interface{}
  14. }
  15. func MakeTimestamp() int64 {
  16. return time.Now().UnixNano() / int64(time.Millisecond)
  17. }
  18. func MakeTimestampZero(t uint64) uint64{
  19. gap := t % 86400
  20. t = t - gap
  21. return t
  22. }
  23. func MakeTimestampToDate(t int64) string{
  24. date := time.UnixMicro(t*1000000)
  25. const layout = "2006-01-02"
  26. return date.Format(layout)
  27. }
  28. func NewJsonRpcResponseGetter(result interface{}) JsonRpcResponseGetter {
  29. return JsonRpcResponseGetter{m: result.(map[string]interface{})}
  30. }
  31. func (this JsonRpcResponseGetter) GetString(key string) string {
  32. return this.m[key].(string)
  33. }
  34. func (this JsonRpcResponseGetter) GetUint64(key string) uint64 {
  35. str := this.m[key].(string)
  36. if strings.HasPrefix(str, "0x") {
  37. str = str[2:]
  38. }
  39. ret, err := strconv.ParseUint(str, 16, 64)
  40. if err != nil {
  41. panic(err)
  42. }
  43. return ret
  44. }
  45. func MustParseDuration(s string) time.Duration {
  46. value, err := time.ParseDuration(s)
  47. if err != nil {
  48. panic("util: Can't parse duration `" + s + "`: " + err.Error())
  49. }
  50. return value
  51. }
  52. func RewardInShannon(dgc *big.Int) int64 {
  53. reward := new(big.Int).Div(dgc, Shannon)
  54. return reward.Int64()
  55. }
  56. func RewardInShannonByBig(dgc *big.Int) *big.Int {
  57. reward := new(big.Int).Div(dgc, Shannon)
  58. return reward
  59. }
  60. func StringInSlice(a string, list []string) bool {
  61. for _, b := range list {
  62. if b == a {
  63. return true
  64. }
  65. }
  66. return false
  67. }