main.go-testnet 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "net/http"
  10. "os"
  11. "os/signal"
  12. "sync-block/collector"
  13. "sync-block/er"
  14. "sync-block/eth"
  15. "syscall"
  16. "time"
  17. _ "github.com/go-sql-driver/mysql"
  18. )
  19. type Config struct {
  20. Db struct {
  21. Addr string
  22. DbName string
  23. ID string
  24. Password string
  25. }
  26. CollectionAmount int64
  27. }
  28. type ConfigRipple struct {
  29. Db struct {
  30. Addr string
  31. DbName string
  32. ID string
  33. Password string
  34. }
  35. }
  36. const URL = "http://3.38.62.211:8545"
  37. //const URL = "https://mainnet.infura.io/v3/ce1c08d3371f40a7997b2bd690c52d71"
  38. func main() {
  39. log.SetFlags(log.LstdFlags | log.Lshortfile)
  40. configPath := ""
  41. flag.StringVar(&configPath, "c", "config.json", "")
  42. flag.Parse()
  43. conf := Config{}
  44. data, err := ioutil.ReadFile(configPath)
  45. if err != nil {
  46. panic(err)
  47. }
  48. json.Unmarshal(data, &conf)
  49. log.Printf("%#v\n", conf)
  50. eth, err := eth.New(URL)
  51. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true",
  52. conf.Db.ID, conf.Db.Password, conf.Db.Addr, conf.Db.DbName))
  53. err = db.Ping()
  54. er.Check(err)
  55. c := collector.New(db, eth, conf.CollectionAmount)
  56. ch := make(chan os.Signal)
  57. signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
  58. go func() {
  59. for {
  60. sig := <-ch
  61. switch sig {
  62. case syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM:
  63. c.Stop()
  64. break
  65. default:
  66. fmt.Printf("Unknown signal(%d)\n", sig)
  67. }
  68. }
  69. }()
  70. configPathRipple := ""
  71. flag.StringVar(&configPathRipple, "d", "config-ripple.json", "")
  72. flag.Parse()
  73. confRipple := ConfigRipple{}
  74. dataRipple, err := ioutil.ReadFile(configPathRipple)
  75. if err != nil {
  76. panic(err)
  77. }
  78. json.Unmarshal(dataRipple, &confRipple)
  79. log.Printf("%#v\n", conf)
  80. dbRipple, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true",
  81. confRipple.Db.ID, confRipple.Db.Password, confRipple.Db.Addr, confRipple.Db.DbName))
  82. err = dbRipple.Ping()
  83. er.Check(err)
  84. go xrpUpdater(dbRipple)
  85. log.Println("sync block started")
  86. c.Run()
  87. log.Println("sync block terminated")
  88. }
  89. func xrpUpdater(db *sql.DB) {
  90. for {
  91. resp, err := http.Get("https://api.upbit.com/v1/ticker?markets=KRW-XRP")
  92. var m []map[string]interface{}
  93. var data []byte
  94. var scr float64
  95. var ok bool
  96. if err != nil {
  97. log.Println(err)
  98. goto end
  99. }
  100. data, err = ioutil.ReadAll(resp.Body)
  101. if err != nil {
  102. log.Println(err)
  103. goto end
  104. }
  105. err = json.Unmarshal(data, &m)
  106. if err != nil {
  107. log.Println(err)
  108. goto end
  109. }
  110. if len(m) == 0 {
  111. goto end
  112. }
  113. scr, ok = m[0]["signed_change_rate"].(float64)
  114. if ok {
  115. _, err = db.Exec("update crp_token_group set updated_on = ?, curr_krw_price = ?, change_rate = ? where id = 3",
  116. time.Now().Unix(), m[0]["trade_price"], scr*100)
  117. }
  118. end:
  119. log.Println("updated xrp price")
  120. time.Sleep(time.Minute * 10)
  121. }
  122. }