-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
58 lines (46 loc) · 1.17 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package mempool
import (
"encoding/json"
"fmt"
"github.com/BoltzExchange/boltz-lnd/logger"
"net/http"
"strings"
)
const feeRecommendationEndpoint = "/v1/fees/recommended"
type feeEstimation struct {
FastestFee int64 `json:"fastestFee"`
HalfHourFee int64 `json:"halfHourFee"`
HourFee int64 `json:"hourFee"`
EconomyFee int64 `json:"economyFee"`
MinimumFee int64 `json:"minimumFee"`
}
type client struct {
api string
}
func initClient(endpoint string) *client {
endpointStripped := strings.TrimSuffix(endpoint, "/")
logger.Info("mempool.space API for fee estimations: " + endpointStripped)
return &client{
api: endpointStripped,
}
}
func (c *client) getFeeRecommendation() (*feeEstimation, error) {
req, err := http.NewRequest(http.MethodGet, c.api+feeRecommendationEndpoint, nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed with status: %d", res.StatusCode)
}
var fees feeEstimation
err = json.NewDecoder(res.Body).Decode(&fees)
if err != nil {
return nil, err
}
return &fees, nil
}