OSDN Git Service

fix bug and add tool for claim tx
[bytom/vapor.git] / tools / monitor_tx / http_client.go
1 package main
2
3 import (
4         "encoding/json"
5         "errors"
6         "io/ioutil"
7         "net/http"
8 )
9
10 type RespMsg struct {
11         MainchainAddress string `json:"mainchain_address,omitempty"`
12         ControlProgram   string `json:"control_program,omitempty"`
13         ClaimScript      string `json:"claim_script,omitempty"`
14 }
15
16 type Response struct {
17         Code int    `json:"code,omitempty"`
18         Msg  string `json:"msg,omitempty"`
19         Data string `json:"data,omitempty"`
20 }
21
22 func getPeginInfo() (map[string]string, error) {
23         resp, err := http.Get("http://127.0.0.1:8000/api/get_pegin_address")
24         if err != nil {
25                 return nil, err
26         }
27         if resp.StatusCode < 200 || resp.StatusCode >= 300 {
28                 return nil, errors.New("connect fail")
29         }
30         body, err := ioutil.ReadAll(resp.Body)
31         if err != nil {
32                 return nil, err
33         }
34
35         rep := Response{}
36         err = json.Unmarshal(body, &rep)
37         if err != nil {
38                 return nil, err
39         }
40         var msg []RespMsg
41         err = json.Unmarshal([]byte(rep.Data), &msg)
42         if err != nil {
43                 return nil, err
44         }
45         mapMsg := make(map[string]string)
46         for _, m := range msg {
47                 mapMsg[m.ClaimScript] = m.ControlProgram
48         }
49         return mapMsg, nil
50 }