-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi_network.go
More file actions
103 lines (87 loc) · 2.35 KB
/
Copy pathapi_network.go
File metadata and controls
103 lines (87 loc) · 2.35 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package blockfrost
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
const (
resourceNetwork = "network"
resourceNetworkEras = "network/eras"
)
// NetworkSupply contains information on network supply
type NetworkSupply struct {
Max string `json:"max"`
Total string `json:"total"`
Circulating string `json:"circulating"`
Locked string `json:"locked"`
}
// NetworkStake contains information on the cardano network stake
type NetworkStake struct {
Live string `json:"live"`
Active string `json:"active"`
}
// NetworkInfo contains network stake and supply information on the network
type NetworkInfo struct {
Supply NetworkSupply `json:"supply"`
Stake NetworkStake `json:"stake"`
}
// Network returns detailed network information.
func (c *apiClient) Network(ctx context.Context) (ni NetworkInfo, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceNetwork))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&ni); err != nil {
return
}
return ni, nil
}
// NetworkEraTime contains era start/end time information
type NetworkEraTime struct {
Time int `json:"time"`
Slot int `json:"slot"`
Epoch int `json:"epoch"`
}
// NetworkEraParameters contains era parameters
type NetworkEraParameters struct {
EpochLength int `json:"epoch_length"`
SlotLength int `json:"slot_length"`
SafeZone int `json:"safe_zone"`
}
// NetworkEra contains information on a network era
type NetworkEra struct {
Start NetworkEraTime `json:"start"`
End NetworkEraTime `json:"end"`
Parameters NetworkEraParameters `json:"parameters"`
}
// NetworkEras returns network era information.
func (c *apiClient) NetworkEras(ctx context.Context) (ne []NetworkEra, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceNetworkEras))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&ne); err != nil {
return
}
return ne, nil
}