2021-09-14 03:51:47 +00:00
|
|
|
package heliumapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-09-15 03:34:08 +00:00
|
|
|
"time"
|
2021-09-14 03:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
Data struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
Balance int `json:"balance"`
|
|
|
|
Block int `json:"block"`
|
|
|
|
DCBalance int `json:"dc_balance"`
|
|
|
|
DCNonce int `json:"dc_nonce"`
|
|
|
|
SECBalance int `json:"sec_balance"`
|
|
|
|
SECNonce int `json:"sec_nonce"`
|
|
|
|
SpeculativeNonce int `json:"speculative_nonce"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-09-15 03:34:08 +00:00
|
|
|
type ActivityCounts struct {
|
|
|
|
Data map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
type RewardTotal struct {
|
|
|
|
Meta struct {
|
|
|
|
MinTime string `json:"min_time"`
|
|
|
|
MaxTime string `json:"max_time"`
|
|
|
|
} `json:"meta"`
|
|
|
|
|
|
|
|
Data struct {
|
|
|
|
Total float64 `json:"total"`
|
|
|
|
Sum float64 `json:"sum"`
|
|
|
|
Stddev float64 `json:"stddev"`
|
|
|
|
Min float64 `json:"min"`
|
|
|
|
Median float64 `json:"median"`
|
|
|
|
Max float64 `json:"max"`
|
|
|
|
Avg float64 `json:"avg"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-09-14 03:51:47 +00:00
|
|
|
func GetAccountForAddress(account string) (*Account, error) {
|
|
|
|
path := "/v1/accounts/" + account
|
|
|
|
|
|
|
|
// query the api
|
|
|
|
respBody, err := getHeliumApi(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the response
|
|
|
|
respobject := Account{}
|
|
|
|
err = json.Unmarshal(respBody, &respobject)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &respobject, nil
|
|
|
|
}
|
2021-09-15 03:34:08 +00:00
|
|
|
|
|
|
|
func GetActivityCountsForAccount(account string) (*ActivityCounts, error) {
|
|
|
|
path := "/v1/accounts/" + account + "/activity/count"
|
|
|
|
|
|
|
|
// query the api
|
|
|
|
respBody, err := getHeliumApi(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the response
|
|
|
|
respobject := ActivityCounts{}
|
|
|
|
err = json.Unmarshal(respBody, &respobject)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &respobject, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRewardTotalsForAccount(account string, minTime *time.Time, maxTime *time.Time) (*RewardTotal, error) {
|
|
|
|
path := "/v1/accounts/" + account + "/rewards/sum"
|
|
|
|
params := map[string]string{}
|
|
|
|
if minTime != nil {
|
|
|
|
params["min_time"] = minTime.UTC().Format("2006-01-02T15:04:05Z")
|
|
|
|
}
|
|
|
|
if maxTime != nil {
|
|
|
|
params["max_time"] = minTime.UTC().Format("2006-01-02T15:04:05Z")
|
|
|
|
}
|
|
|
|
|
|
|
|
// query the api
|
|
|
|
respBody, err := getHeliumApi(path, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the response
|
|
|
|
respobject := RewardTotal{}
|
|
|
|
err = json.Unmarshal(respBody, &respobject)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &respobject, nil
|
|
|
|
|
|
|
|
}
|