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
|
|
|
|
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-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
|
|
|
|
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"
|
|
|
|
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
|
|
|
|
resBodies, err := getHeliumApiWithCursor(path, ¶ms)
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
|
|
|
|
}
|
|
|
|
combinedResp.Data = append(combinedResp.Data, activityResp.Data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return activity.NewActivities(combinedResp)
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-09-17 20:45:01 +00:00
|
|
|
params["max_time"] = maxTime.UTC().Format("2006-01-02T15:04:05Z")
|
2021-09-15 03:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-09-15 05:04:32 +00:00
|
|
|
}
|
2021-09-15 03:34:08 +00:00
|
|
|
|
2021-09-15 05:04:32 +00:00
|
|
|
func GetHotspotsForAccount(account string) (*AccountHotspots, error) {
|
|
|
|
path := "/v1/accounts/" + account + "/hotspots"
|
|
|
|
|
|
|
|
// query the api
|
|
|
|
respBody, err := getHeliumApi(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the response
|
|
|
|
respobject := AccountHotspots{}
|
|
|
|
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
|
|
|
}
|