-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifil.go
84 lines (67 loc) · 2 KB
/
ifil.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
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
package invariants
import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"
"net/http"
"github.com/glifio/invariants/singleton"
)
type IFILTotalSupplyJSON struct {
Height uint64 `json:"height"`
IFILTotalSupply string `json:"iFILTotalSupply"`
}
type IFILTotalSupply struct {
Height uint64
IFILTotalSupply *big.Int
}
// GetIFILTotalSupplyFromAPI calls the REST API to get the iFIL total supply
func GetIFILTotalSupplyFromAPI(ctx context.Context, eventsURL string, height uint64) (*IFILTotalSupply, error) {
url := fmt.Sprintf("%s/ifil/%d/total-supply", eventsURL, height)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
log.Println("error creating request:", err)
return nil, err
}
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("error getting response:", err)
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad http status: %v", res.StatusCode)
}
var response IFILTotalSupplyJSON
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON response: %v", err)
}
totalSupply := big.NewInt(0)
totalSupply.SetString(response.IFILTotalSupply, 10)
iFILTotalSupply := IFILTotalSupply{
Height: response.Height,
IFILTotalSupply: totalSupply,
}
return &iFILTotalSupply, nil
}
// GetIFILTotalSupplyFromNode calls the node to get the iFIL total supply
func GetIFILTotalSupplyFromNode(ctx context.Context, height uint64) (*IFILTotalSupply, uint64, error) {
height, err := getNextEpoch(ctx, height)
if err != nil {
return nil, height, err
}
blockNumber := big.NewInt(int64(height))
q := singleton.PoolsSDK.Query()
totalSupply, err := q.IFILSupply(ctx, blockNumber)
if err != nil {
return nil, height, err
}
iFILTotalSupply := IFILTotalSupply{
Height: height,
IFILTotalSupply: totalSupply,
}
return &iFILTotalSupply, height, nil
}