map.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package util
  2. import (
  3. "encoding/json"
  4. "math"
  5. "math/big"
  6. )
  7. // PutIntoMapIf 根据条件放进map
  8. func PutIntoMapIf(m map[string]interface{}, cond bool, key string, value interface{}) {
  9. if cond {
  10. m[key] = value
  11. }
  12. }
  13. // S2Json struct to json
  14. func S2Json(v interface{}) string {
  15. buf, err := json.Marshal(v)
  16. if err != nil {
  17. return ""
  18. }
  19. return string(buf)
  20. }
  21. // Json2S trans json to object
  22. func Json2S(src string, dest interface{}) error {
  23. return json.Unmarshal([]byte(src), dest)
  24. }
  25. func S2S(data, v interface{}) error {
  26. return Json2S(S2Json(data), v)
  27. }
  28. // BigIntToFloat64 大整数转为float64
  29. func BigIntToFloat64(v *big.Int) float64 {
  30. a := &big.Float{}
  31. f, _ := a.SetInt(v).Float64()
  32. return f
  33. }
  34. // Float64ToBigInt float6¢ 转为大整数
  35. func Float64ToBigInt(v float64) *big.Int {
  36. f := big.NewFloat(v)
  37. r, _ := f.Int(nil)
  38. return r
  39. }
  40. // StringToU64 string to uint64
  41. func StringToU64(s string) uint64 {
  42. bi, ok := (&big.Int{}).SetString(s, 10)
  43. if ok {
  44. return bi.Uint64()
  45. }
  46. return 0
  47. }
  48. // Gas 计算以太坊gas, gasPrice 单位是gwei
  49. func Gas(gasLimit uint64, gasPrice uint64) *big.Int {
  50. limit := (&big.Int{}).SetUint64(gasLimit)
  51. price := (&big.Int{}).Mul((&big.Int{}).SetUint64(gasPrice), big.NewInt(1e9))
  52. return (&big.Int{}).Mul(limit, price)
  53. }
  54. // LeftPadString 字符串前面填充到指定到个数
  55. func LeftPadString(s, padding string, total int) string {
  56. if len(s) >= total {
  57. return s
  58. }
  59. count := total - len(s)
  60. for i := 0; i < count; i++ {
  61. s = padding + s
  62. }
  63. return s
  64. }
  65. // AddStrInt string int add
  66. func AddStrInt(a, b string) *big.Int {
  67. ai, _ := (&big.Int{}).SetString(a, 10)
  68. bi, _ := (&big.Int{}).SetString(b, 10)
  69. sum := (big.NewInt(0)).Add(ai, bi)
  70. return sum
  71. }
  72. // SubStrInt return a-b
  73. func SubStrInt(a, b string) *big.Int {
  74. ai, _ := (&big.Int{}).SetString(a, 10)
  75. bi, _ := (&big.Int{}).SetString(b, 10)
  76. diff := (big.NewInt(0)).Sub(ai, bi)
  77. return diff
  78. }
  79. // F64ToU64 float64 转 uint64 并且乘以给定到小数点位数
  80. func F64ToU64(value float64, decimal int) uint64 {
  81. return uint64(value * math.Pow10(decimal))
  82. }
  83. // FormatUsdt 格式化usdt
  84. func FormatUsdt(v float64) float64 {
  85. f := new(big.Float).Quo(big.NewFloat(v), big.NewFloat(1e6))
  86. res, _ := f.Float64()
  87. return res
  88. }
  89. // FormatEther 格式化以太币
  90. func FormatEther(v string) string {
  91. i, _ := new(big.Int).SetString(v, 10)
  92. i = i.Div(i, big.NewInt(1e9))
  93. f := new(big.Float).SetUint64(i.Uint64())
  94. f = f.Quo(f, big.NewFloat(1e9))
  95. return f.Text('f', 9)
  96. }