1
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
helium-blockchain-exporter/heliumapi/accounts.go

131 lines
3.6 KiB
Go
Raw Normal View History

2021-09-14 03:51:47 +00:00
package heliumapi
import (
"encoding/json"
"fmt"
2021-10-03 22:02:25 +00:00
"strings"
"time"
2021-09-14 03:51:47 +00:00
2021-09-25 01:30:22 +00:00
"github.com/helium-blockchain-exporter/heliumapi/activity"
)
2021-09-15 05:04:32 +00:00
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#account-for-address
func GetAccountForAddress(account string) (*Account, error) {
2021-09-14 03:51:47 +00:00
path := "/v1/accounts/" + account
// query the api
respBody, err := getHeliumApi(path, nil)
if err != nil {
return nil, err
}
// unmarshal the response
respobject := AccountResp{}
2021-09-14 03:51:47 +00:00
err = json.Unmarshal(respBody, &respobject)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
2021-09-14 03:51:47 +00:00
}
return &respobject.Data, nil
2021-09-14 03:51:47 +00:00
}
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#activity-for-account
2021-09-25 01:30:22 +00:00
func GetActivityForAccount(account string, filterTypes []string, minTime *time.Time, maxTime *time.Time) (*activity.Activities, error) {
path := "/v1/accounts/" + account + "/activity"
2021-10-03 22:02:25 +00:00
params := map[string]string{
"filter_types": strings.Join(filterTypes, ","),
}
2021-09-25 01:30:22 +00:00
if minTime != nil {
params["min_time"] = minTime.UTC().Format("2006-01-02T15:04:05Z")
}
if maxTime != nil {
params["max_time"] = maxTime.UTC().Format("2006-01-02T15:04:05Z")
}
// query the api
resBodies, err := getHeliumApiWithCursor(path, &params)
if err != nil {
return nil, err
}
// unmarshal the responses and merge them all together
combinedResp := activity.ActivityResp{}
for _, respBody := range resBodies {
activityResp := activity.ActivityResp{}
err = json.Unmarshal(respBody, &activityResp)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
2021-09-25 01:30:22 +00:00
}
combinedResp.Data = append(combinedResp.Data, activityResp.Data...)
}
return activity.NewActivities(combinedResp)
}
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#activity-counts-for-account
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 := ActivityCountsResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil
}
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#reward-totals-for-an-account
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"] = maxTime.UTC().Format("2006-01-02T15:04:05Z")
}
// query the api
respBody, err := getHeliumApi(path, &params)
if err != nil {
return nil, err
}
// unmarshal the response
respobject := RewardTotalResp{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
}
return &respobject.Data, nil
2021-09-15 05:04:32 +00:00
}
2021-09-26 04:42:34 +00:00
// query https://docs.helium.com/api/blockchain/accounts#hotspots-for-account
func GetHotspotsForAccount(account string) (*[]AccountHotspot, error) {
2021-09-15 05:04:32 +00:00
path := "/v1/accounts/" + account + "/hotspots"
// query the api
respBody, err := getHeliumApi(path, nil)
if err != nil {
return nil, err
}
// unmarshal the response
respobject := AccountHotspotsResp{}
2021-09-15 05:04:32 +00:00
err = json.Unmarshal(respBody, &respobject)
if err != nil {
2021-09-26 04:08:10 +00:00
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
2021-09-15 05:04:32 +00:00
}
return &respobject.Data, nil
}