|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type FactomProFetcher struct { |
| 13 | + // Cache is a cheeky way to speed things up |
| 14 | + Cache map[string]*proBalance |
| 15 | +} |
| 16 | + |
| 17 | +func NewFactomPro(ctx context.Context) (*FactomProFetcher, error) { |
| 18 | + f := &FactomProFetcher{} |
| 19 | + err := f.preloadCache(ctx) |
| 20 | + if err != nil { |
| 21 | + return nil, fmt.Errorf("preload cache: %w", err) |
| 22 | + } |
| 23 | + return f, nil |
| 24 | +} |
| 25 | + |
| 26 | +type proBalance struct { |
| 27 | + PubAddr string `json:"pubAddress"` |
| 28 | + PubKey string `json:"pubKey"` |
| 29 | + Balance int64 `json:"balance"` |
| 30 | + Type string `json:"type"` |
| 31 | +} |
| 32 | + |
| 33 | +func (fa *FactomProFetcher) FactoidBalance(ctx context.Context, addr string) (int64, error) { |
| 34 | + return fa.balance(ctx, addr) |
| 35 | +} |
| 36 | + |
| 37 | +func (fa *FactomProFetcher) EntryCreditBalance(ctx context.Context, addr string) (int64, error) { |
| 38 | + return fa.balance(ctx, addr) |
| 39 | +} |
| 40 | + |
| 41 | +func (fa *FactomProFetcher) preloadCache(ctx context.Context) error { |
| 42 | + if fa.Cache == nil { |
| 43 | + fa.Cache = make(map[string]*proBalance) |
| 44 | + var ret struct { |
| 45 | + Result []*proBalance `json:"result"` |
| 46 | + } |
| 47 | + |
| 48 | + _, err := jsonRequest(ctx, |
| 49 | + "https://explorer.factom.pro/explorer/richlist", |
| 50 | + "GET", |
| 51 | + nil, |
| 52 | + &ret) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + for _, addr := range ret.Result { |
| 58 | + fa.Cache[addr.PubAddr] = addr |
| 59 | + } |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (fa *FactomProFetcher) Height(ctx context.Context) (int64, error) { |
| 65 | + var ret struct { |
| 66 | + Result struct { |
| 67 | + Version string `json:"version"` |
| 68 | + BlockchainHeight int64 `json:"blockchainHeight"` |
| 69 | + APIHeight int64 `json:"apiHeight"` |
| 70 | + Name string `json:"name"` |
| 71 | + AnchorsHeight struct { |
| 72 | + Btc int `json:"btc"` |
| 73 | + Eth int `json:"eth"` |
| 74 | + } `json:"anchorsHeight"` |
| 75 | + } `json:"result"` |
| 76 | + } |
| 77 | + |
| 78 | + _, err := jsonRequest(ctx, "https://explorer.factom.pro/explorer", "GET", nil, &ret) |
| 79 | + if err != nil { |
| 80 | + return -1, err |
| 81 | + } |
| 82 | + return ret.Result.BlockchainHeight, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (fa *FactomProFetcher) balance(ctx context.Context, addr string) (int64, error) { |
| 86 | + if v, ok := fa.Cache[addr]; ok { |
| 87 | + return v.Balance, nil |
| 88 | + } |
| 89 | + |
| 90 | + var ret struct { |
| 91 | + Result proBalance `json:"result"` |
| 92 | + } |
| 93 | + _, err := jsonRequest(ctx, |
| 94 | + fmt.Sprintf("https://explorer.factom.pro/explorer/addresses/%s", addr), |
| 95 | + "GET", |
| 96 | + nil, |
| 97 | + &ret) |
| 98 | + if err != nil { |
| 99 | + return -1, err |
| 100 | + } |
| 101 | + return ret.Result.Balance, nil |
| 102 | +} |
| 103 | + |
| 104 | +func jsonRequest(ctx context.Context, u string, method string, body interface{}, ret interface{}) (*http.Response, error) { |
| 105 | + bodyBuf := bytes.NewBuffer([]byte{}) |
| 106 | + if body != nil { |
| 107 | + err := json.NewEncoder(bodyBuf).Encode(body) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("encode body: %w", err) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + req, err := http.NewRequestWithContext(ctx, method, u, bodyBuf) |
| 114 | + if err != nil { |
| 115 | + return nil, fmt.Errorf("new request: %w", err) |
| 116 | + } |
| 117 | + |
| 118 | + cli := http.Client{ |
| 119 | + Timeout: 5 * time.Second, |
| 120 | + } |
| 121 | + resp, err := cli.Do(req) |
| 122 | + if err != nil { |
| 123 | + return resp, err |
| 124 | + } |
| 125 | + defer resp.Body.Close() |
| 126 | + |
| 127 | + if ret != nil { |
| 128 | + err := json.NewDecoder(resp.Body).Decode(&ret) |
| 129 | + if err != nil { |
| 130 | + return resp, fmt.Errorf("decode resp: %w", err) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return resp, nil |
| 135 | +} |
0 commit comments