1
0
Fork 0

Compare commits

..

No commits in common. "42e2a23e2651eb12c2a4035486cf6ea59774eded" and "b4598ce49536e46865550377b1822619c002ec3d" have entirely different histories.

8 changed files with 286 additions and 350 deletions

View File

@ -5,10 +5,8 @@ import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/helium-blockchain-exporter/heliumapi"
@ -48,13 +46,6 @@ type Account struct {
Tx AccountTx
}
type AccountTx struct {
DepositTotal int
WithdrawalTotal int
LastUpdate time.Time
UpdateLock sync.Mutex
}
func NewAccount(address string) Account {
return Account{
Address: address,
@ -66,6 +57,12 @@ func NewAccount(address string) Account {
}
}
type AccountTx struct {
DepositTotal int
WithdrawalTotal int
LastUpdate time.Time
}
const (
namespace = "helium"
)
@ -320,27 +317,16 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// Collect fetches the data from the helium blockchain api and delivers them as Prometheus metrics.
// implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
wg := new(sync.WaitGroup)
wg.Add(2)
go e.collectOracleMetrics(wg, ch)
e.collectStatsMetrics(wg, ch)
e.collectOracleMetrics(ch)
e.collectStatsMetrics(ch)
for i := range e.Accounts {
wg.Add(5)
go e.collectAccountMetrics(wg, ch, &e.Accounts[i])
go e.collectAccountActivityMetrics(wg, ch, &e.Accounts[i])
go e.collectAccountRewardsTotalMetrics(wg, ch, &e.Accounts[i])
go e.collectAccountTransactionsMetrics(wg, ch, &e.Accounts[i])
go e.collectHotspotMetrics(wg, ch, &e.Accounts[i])
e.collectAccountMetrics(ch, &e.Accounts[i])
e.collectHotspotMetrics(ch, &e.Accounts[i])
}
wg.Wait()
}
// collectOracleMetrics collect metrics in the oracle group from the helium api
func (e *Exporter) collectOracleMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric) {
defer wg.Done()
func (e *Exporter) collectOracleMetrics(ch chan<- prometheus.Metric) {
currentOraclePrice, err := heliumapi.GetCurrentOraclePrice()
if err != nil {
fmt.Println(err)
@ -348,14 +334,12 @@ func (e *Exporter) collectOracleMetrics(wg *sync.WaitGroup, ch chan<- prometheus
}
ch <- prometheus.MustNewConstMetric(
oraclePrice.Desc, oraclePrice.Type, float64(currentOraclePrice.Price)/100000000,
oraclePrice.Desc, oraclePrice.Type, float64(currentOraclePrice.Data.Price)/100000000,
)
}
// collectStatsMetrics collect metrics in the stats group from the helium api
func (e *Exporter) collectStatsMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric) {
defer wg.Done()
func (e *Exporter) collectStatsMetrics(ch chan<- prometheus.Metric) {
blockchainStats, err := heliumapi.GetBlockchainStats()
if err != nil {
fmt.Println(err)
@ -363,188 +347,98 @@ func (e *Exporter) collectStatsMetrics(wg *sync.WaitGroup, ch chan<- prometheus.
}
ch <- prometheus.MustNewConstMetric(
statsValidators.Desc, statsValidators.Type, float64(blockchainStats.Counts.Validators),
statsValidators.Desc, statsValidators.Type, float64(blockchainStats.Data.Counts.Validators),
)
ch <- prometheus.MustNewConstMetric(
statsOuis.Desc, statsOuis.Type, float64(blockchainStats.Counts.Ouis),
statsOuis.Desc, statsOuis.Type, float64(blockchainStats.Data.Counts.Ouis),
)
ch <- prometheus.MustNewConstMetric(
statsHotspotsDataOnly.Desc, statsHotspotsDataOnly.Type, float64(blockchainStats.Counts.HotspotsDataonly),
statsHotspotsDataOnly.Desc, statsHotspotsDataOnly.Type, float64(blockchainStats.Data.Counts.HotspotsDataonly),
)
ch <- prometheus.MustNewConstMetric(
statsBlocks.Desc, statsBlocks.Type, float64(blockchainStats.Counts.Blocks),
statsBlocks.Desc, statsBlocks.Type, float64(blockchainStats.Data.Counts.Blocks),
)
ch <- prometheus.MustNewConstMetric(
statsChallenges.Desc, statsChallenges.Type, float64(blockchainStats.Counts.Challenges),
statsChallenges.Desc, statsChallenges.Type, float64(blockchainStats.Data.Counts.Challenges),
)
ch <- prometheus.MustNewConstMetric(
statsCities.Desc, statsCities.Type, float64(blockchainStats.Counts.Cities),
statsCities.Desc, statsCities.Type, float64(blockchainStats.Data.Counts.Cities),
)
ch <- prometheus.MustNewConstMetric(
statsConsensusGroups.Desc, statsConsensusGroups.Type, float64(blockchainStats.Counts.ConsensusGroups),
statsConsensusGroups.Desc, statsConsensusGroups.Type, float64(blockchainStats.Data.Counts.ConsensusGroups),
)
ch <- prometheus.MustNewConstMetric(
statsCountries.Desc, statsCountries.Type, float64(blockchainStats.Counts.Countries),
statsCountries.Desc, statsCountries.Type, float64(blockchainStats.Data.Counts.Countries),
)
ch <- prometheus.MustNewConstMetric(
statsHotspots.Desc, statsHotspots.Type, float64(blockchainStats.Counts.Hotspots),
statsHotspots.Desc, statsHotspots.Type, float64(blockchainStats.Data.Counts.Hotspots),
)
ch <- prometheus.MustNewConstMetric(
statsTokenSupply.Desc, statsTokenSupply.Type, blockchainStats.TokenSupply,
statsTokenSupply.Desc, statsTokenSupply.Type, blockchainStats.Data.TokenSupply,
)
}
// collectStatsMetrics collect metrics in the account group from the helium api
func (e *Exporter) collectAccountMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric, account *Account) {
defer wg.Done()
func (e *Exporter) collectAccountMetrics(ch chan<- prometheus.Metric, account *Account) {
accountForAddress, err := heliumapi.GetAccountForAddress(account.Address)
if err != nil {
fmt.Println(err)
return
}
ch <- prometheus.MustNewConstMetric(
accountBalanceHnt.Desc, accountBalanceHnt.Type, float64(accountForAddress.Balance),
account.Address,
)
}
// collectAccountActivityMetrics collect the total number of activities executed by an account from the helium api
func (e *Exporter) collectAccountActivityMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric, account *Account) {
defer wg.Done()
accountActivityForAddress, err := heliumapi.GetActivityCountsForAccount(account.Address)
if err != nil {
fmt.Println(err)
return
}
for accType, count := range *accountActivityForAddress {
ch <- prometheus.MustNewConstMetric(
accountActivity.Desc, accountActivity.Type, float64(count),
account.Address, accType,
)
}
}
// collectAccountRewardsTotalMetrics collect the total rewards accumulated by an account from the helium api
func (e *Exporter) collectAccountRewardsTotalMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric, account *Account) {
defer wg.Done()
accountRewardTotalsForAddress, err := heliumapi.GetRewardTotalsForAccount(account.Address, &e.StartTime, nil)
if err != nil {
fmt.Println(err)
return
}
ch <- prometheus.MustNewConstMetric(
accountRewardsHnt.Desc, accountRewardsHnt.Type, accountRewardTotalsForAddress.Sum,
account.Address,
)
}
// collectAccountTransactionsMetrics collect the total deposited/withdrawn by an account from the helium api
func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric, account *Account) {
defer wg.Done()
// we can only ever allow a single instance of the routine doing
// calculations on the deposited and widthdrawn total
account.Tx.UpdateLock.Lock()
defer account.Tx.UpdateLock.Unlock()
now := time.Now()
activities, err := heliumapi.GetActivityForAccount(account.Address, []string{}, &account.Tx.LastUpdate, &now)
err = account.collectTransactionMetrics(ch)
if err != nil {
fmt.Println(err)
return
}
// impl based on https://github.com/helium/hotspot-app/blob/918563fba84d1abf4554a43a4d42bb838d017bd3/src/features/wallet/root/useActivityItem.tsx#L336
for _, activity := range activities.AddGatewayV1 {
account.Tx.WithdrawalTotal += activity.StakingFee
}
for _, activity := range activities.AssertLocationV1 {
account.Tx.WithdrawalTotal += activity.StakingFee
}
for _, activity := range activities.AssertLocationV2 {
account.Tx.WithdrawalTotal += activity.StakingFee
}
for _, activity := range activities.PaymentV1 {
if activity.Payer == account.Address {
account.Tx.WithdrawalTotal += activity.Amount
} else {
account.Tx.DepositTotal += activity.Amount
}
}
for _, activity := range activities.PaymentV2 {
if activity.Payer == account.Address {
paymentTotal := 0
for _, payment := range activity.Payments {
paymentTotal += payment.Amount
}
account.Tx.WithdrawalTotal += paymentTotal
} else {
for _, payment := range activity.Payments {
if payment.Payee == account.Address {
account.Tx.DepositTotal += payment.Amount
}
}
}
}
for _, activity := range activities.RewardsV1 {
for _, reward := range activity.Rewards {
account.Tx.DepositTotal += reward.Amount
}
}
for _, activity := range activities.RewardsV2 {
for _, reward := range activity.Rewards {
account.Tx.DepositTotal += reward.Amount
}
}
for _, activity := range activities.StakeValidatorV1 {
account.Tx.WithdrawalTotal += activity.Stake
}
for _, activity := range activities.TokenBurnV1 {
account.Tx.WithdrawalTotal += activity.Amount
}
for _, activity := range activities.TransferHotspotV1 {
if activity.Buyer == account.Address {
account.Tx.WithdrawalTotal += activity.AmountToSeller
} else {
account.Tx.DepositTotal += activity.AmountToSeller
}
}
for _, activity := range activities.UnstakeValidatorV1 {
account.Tx.WithdrawalTotal += activity.StakeAmount
}
account.Tx.LastUpdate = now
ch <- prometheus.MustNewConstMetric(
accountDepositsHnt.Desc, accountDepositsHnt.Type, float64(account.Tx.DepositTotal),
accountBalanceHnt.Desc, accountBalanceHnt.Type, float64(accountForAddress.Data.Balance),
account.Address,
)
for accType, count := range accountActivityForAddress.Data {
ch <- prometheus.MustNewConstMetric(
accountWithdrawalsHnt.Desc, accountWithdrawalsHnt.Type, float64(account.Tx.WithdrawalTotal),
accountActivity.Desc, accountActivity.Type, float64(count),
account.Address, accType,
)
}
ch <- prometheus.MustNewConstMetric(
accountRewardsHnt.Desc, accountRewardsHnt.Type, accountRewardTotalsForAddress.Data.Sum,
account.Address,
)
}
// collectStatsMetrics collect metrics of the hotspot of an account from the helium api
func (e *Exporter) collectHotspotMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric, account *Account) {
defer wg.Done()
// collectStatsMetrics collect metrics in the hotspot group from the helium api
func (e *Exporter) collectHotspotMetrics(ch chan<- prometheus.Metric, account *Account) {
hotspotsForAddress, err := heliumapi.GetHotspotsForAccount(account.Address)
if err != nil {
fmt.Println(err)
return
}
for _, hotspotData := range *hotspotsForAddress {
// collect hotspot metric requiring extra queries in a new routine
wg.Add(2)
go e.collectHotspotActivityMetrics(wg, ch, account, hotspotData)
go e.collectHotspotRewardsMetrics(wg, ch, account, hotspotData)
for _, hotspotData := range hotspotsForAddress.Data {
hotspotActivityForAddress, err := heliumapi.GetHotspotActivityCount(hotspotData.Address)
if err != nil {
fmt.Println(err)
return
}
hotspotRewardTotalsForAddress, err := heliumapi.GetRewardsTotalForHotspot(hotspotData.Address, &e.StartTime, nil)
if err != nil {
fmt.Println(err)
return
}
ch <- prometheus.MustNewConstMetric(
hotspotUp.Desc, hotspotUp.Type, bool2Float64(hotspotData.Status.Online == "online"),
@ -570,52 +464,105 @@ func (e *Exporter) collectHotspotMetrics(wg *sync.WaitGroup, ch chan<- prometheu
hotspotAntennaInfo.Desc, hotspotAntennaInfo.Type, 1.0,
account.Address, hotspotData.Address, hotspotData.Name, strconv.Itoa(hotspotData.Gain), strconv.Itoa(hotspotData.Elevation),
)
}
}
// collectHotspotActivityMetrics collect the total number of activities executed by a hotspot from the helium api
func (e *Exporter) collectHotspotActivityMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric, account *Account, hotspotData heliumapi.AccountHotspot) {
defer wg.Done()
hotspotActivityForAddress, err := heliumapi.GetHotspotActivityCount(hotspotData.Address)
if err != nil {
fmt.Println(err)
return
}
for accType, count := range *hotspotActivityForAddress {
for accType, count := range hotspotActivityForAddress.Data {
ch <- prometheus.MustNewConstMetric(
hotspotActivity.Desc, hotspotActivity.Type, float64(count),
account.Address, hotspotData.Address, hotspotData.Name, accType,
)
}
}
// collectHotspotRewardsMetrics collect the total rewards accumulated by a hotspot from the helium api
func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- prometheus.Metric, account *Account, hotspotData heliumapi.AccountHotspot) {
defer wg.Done()
hotspotRewardTotalsForAddress, err := heliumapi.GetRewardsTotalForHotspot(hotspotData.Address, &e.StartTime, nil)
if err != nil {
fmt.Println(err)
return
}
ch <- prometheus.MustNewConstMetric(
hotspotRewardsHnt.Desc, hotspotRewardsHnt.Type, hotspotRewardTotalsForAddress.Sum,
hotspotRewardsHnt.Desc, hotspotRewardsHnt.Type, hotspotRewardTotalsForAddress.Data.Sum,
account.Address, hotspotData.Address, hotspotData.Name,
)
}
}
func (a *Account) collectTransactionMetrics(ch chan<- prometheus.Metric) error {
now := time.Now()
activities, err := heliumapi.GetActivityForAccount(a.Address, []string{}, &a.Tx.LastUpdate, &now)
if err != nil {
return err
}
// logic based on https://github.com/helium/hotspot-app/blob/918563fba84d1abf4554a43a4d42bb838d017bd3/src/features/wallet/root/useActivityItem.tsx#L336
for _, activity := range activities.AddGatewayV1 {
a.Tx.WithdrawalTotal += activity.StakingFee
}
for _, activity := range activities.AssertLocationV1 {
a.Tx.WithdrawalTotal += activity.StakingFee
}
for _, activity := range activities.AssertLocationV2 {
a.Tx.WithdrawalTotal += activity.StakingFee
}
for _, activity := range activities.PaymentV1 {
if activity.Payer == a.Address {
a.Tx.WithdrawalTotal += activity.Amount
} else {
a.Tx.DepositTotal += activity.Amount
}
}
for _, activity := range activities.PaymentV2 {
if activity.Payer == a.Address {
paymentTotal := 0
for _, payment := range activity.Payments {
paymentTotal += payment.Amount
}
a.Tx.WithdrawalTotal += paymentTotal
} else {
for _, payment := range activity.Payments {
if payment.Payee == a.Address {
a.Tx.DepositTotal += payment.Amount
}
}
}
}
for _, activity := range activities.RewardsV1 {
for _, reward := range activity.Rewards {
a.Tx.DepositTotal += reward.Amount
}
}
for _, activity := range activities.RewardsV2 {
for _, reward := range activity.Rewards {
a.Tx.DepositTotal += reward.Amount
}
}
for _, activity := range activities.StakeValidatorV1 {
a.Tx.WithdrawalTotal += activity.Stake
}
for _, activity := range activities.TokenBurnV1 {
a.Tx.WithdrawalTotal += activity.Amount
}
for _, activity := range activities.TransferHotspotV1 {
if activity.Buyer == a.Address {
a.Tx.WithdrawalTotal += activity.AmountToSeller
} else {
a.Tx.DepositTotal += activity.AmountToSeller
}
}
for _, activity := range activities.UnstakeValidatorV1 {
a.Tx.WithdrawalTotal += activity.StakeAmount
}
a.Tx.LastUpdate = now
ch <- prometheus.MustNewConstMetric(
accountDepositsHnt.Desc, accountDepositsHnt.Type, float64(a.Tx.DepositTotal),
a.Address,
)
ch <- prometheus.MustNewConstMetric(
accountWithdrawalsHnt.Desc, accountWithdrawalsHnt.Type, float64(a.Tx.WithdrawalTotal),
a.Address,
)
return nil
}
func main() {
fApiUrl := flag.String("apiUrl", "https://api.helium.io", "The helium api url")
fHeliumAccounts := flag.String("accounts", "", "A comma-delimited list of helium accounts to scrape (optional)")
fMetricsPath := flag.String("metricpath", "/metrics", "The metrics path")
fListenAddress := flag.String("listenAddress", "0.0.0.0", "The http server listen address")
fListenPort := flag.String("listenPort", "9865", "The http server listen port")
flag.Parse()
heliumapi.ApiUrl = *fApiUrl
heliumAccounts := strings.Split(*fHeliumAccounts, ",")
serverAddr := *fListenAddress + ":" + *fListenPort
@ -640,8 +587,5 @@ func main() {
http.Handle(*fMetricsPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
fmt.Printf("listening on %v\n", serverAddr)
if err = http.ListenAndServe(serverAddr, nil); err != nil {
fmt.Println(err)
os.Exit(1)
}
http.ListenAndServe(serverAddr, nil)
}

View File

@ -18,13 +18,13 @@ func GetAccountForAddress(account string) (*Account, error) {
}
// unmarshal the response
respobject := AccountResp{}
respobject := Account{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}
func GetActivityForAccount(account string, filterTypes []string, minTime *time.Time, maxTime *time.Time) (*activity.Activities, error) {
@ -49,7 +49,7 @@ func GetActivityForAccount(account string, filterTypes []string, minTime *time.T
activityResp := activity.ActivityResp{}
err = json.Unmarshal(respBody, &activityResp)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
combinedResp.Data = append(combinedResp.Data, activityResp.Data...)
}
@ -67,13 +67,13 @@ func GetActivityCountsForAccount(account string) (*ActivityCounts, error) {
}
// unmarshal the response
respobject := ActivityCountsResp{}
respobject := ActivityCounts{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}
func GetRewardTotalsForAccount(account string, minTime *time.Time, maxTime *time.Time) (*RewardTotal, error) {
@ -93,16 +93,16 @@ func GetRewardTotalsForAccount(account string, minTime *time.Time, maxTime *time
}
// unmarshal the response
respobject := RewardTotalResp{}
respobject := RewardTotal{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}
func GetHotspotsForAccount(account string) (*[]AccountHotspot, error) {
func GetHotspotsForAccount(account string) (*AccountHotspots, error) {
path := "/v1/accounts/" + account + "/hotspots"
// query the api
@ -112,11 +112,11 @@ func GetHotspotsForAccount(account string) (*[]AccountHotspot, error) {
}
// unmarshal the response
respobject := AccountHotspotsResp{}
respobject := AccountHotspots{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}

View File

@ -21,6 +21,7 @@ func NewActivities(resp ActivityResp) (*Activities, error) {
if err := json.Unmarshal(activityRaw, &activityType); err != nil {
return nil, fmt.Errorf("failed to unmarshal activity: %v", err)
}
fmt.Println(activityType.Type)
switch activityType.Type {
case "add_gateway_v1":
addGatewayV1 := AddGatewayV1{}

View File

@ -16,13 +16,13 @@ func GetHotspotActivityCount(hotspot string) (*ActivityCounts, error) {
}
// unmarshal the response
respobject := ActivityCountsResp{}
respobject := ActivityCounts{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}
func GetRewardsTotalForHotspot(hotspot string, minTime *time.Time, maxTime *time.Time) (*RewardTotal, error) {
@ -42,11 +42,11 @@ func GetRewardsTotalForHotspot(hotspot string, minTime *time.Time, maxTime *time
}
// unmarshal the response
respobject := RewardTotalResp{}
respobject := RewardTotal{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}

View File

@ -6,7 +6,7 @@ import (
)
func GetCurrentOraclePrice() (*CurrentOraclePrice, error) {
path := "/v1/oracle/prices/current"
const path = "/v1/oracle/prices/current"
// query the api
respBody, err := getHeliumApi(path, nil)
@ -15,11 +15,11 @@ func GetCurrentOraclePrice() (*CurrentOraclePrice, error) {
}
// unmarshal the response
respobject := CurrentOraclePriceResp{}
respobject := CurrentOraclePrice{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}

View File

@ -7,13 +7,16 @@ import (
"net/http"
)
const (
apiUrl = "https://api.helium.io"
)
var (
ApiUrl = "https://api.helium.io"
client = &http.Client{}
)
func createGetRequest(path string, params map[string]string) (*http.Request, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", ApiUrl, path), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", apiUrl, path), nil)
// setup headers
req.Header.Add("Accept", "application/json")
@ -47,14 +50,14 @@ func getHeliumApi(path string, params *map[string]string) ([]byte, error) {
// query the api
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to query %v: %v", path, err)
return nil, fmt.Errorf("failed to query path %v: %v", path, err)
}
defer resp.Body.Close()
// read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body of %v: %v", path, err)
return nil, fmt.Errorf("failed to read response body of path %v: %v", path, err)
}
return body, nil
@ -81,14 +84,14 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
// query the api
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to query %v: %v", path, err)
return nil, fmt.Errorf("failed to query path %v: %v", path, err)
}
defer resp.Body.Close()
// read the response body and add it to the result array
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body of %v: %v", path, err)
return nil, fmt.Errorf("failed to read response body of path %v: %v", path, err)
}
res = append(res, body)
@ -96,7 +99,7 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
respCursor.Cursor = ""
err = json.Unmarshal(body, &respCursor)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
// continue querying until there is no longer a cursor

View File

@ -6,7 +6,7 @@ import (
)
func GetBlockchainStats() (*BlockchainStats, error) {
path := "/v1/stats"
const path = "/v1/stats"
// query the api
respBody, err := getHeliumApi(path, nil)
@ -15,11 +15,11 @@ func GetBlockchainStats() (*BlockchainStats, error) {
}
// unmarshal the response
respobject := BlockchainStatsResp{}
respobject := BlockchainStats{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from %v: %v", path, err)
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject.Data, nil
return &respobject, nil
}

View File

@ -1,10 +1,7 @@
package heliumapi
type AccountResp struct {
Data Account `json:"data"`
}
type Account struct {
Data struct {
Address string `json:"address"`
Balance int `json:"balance"`
Block int `json:"block"`
@ -13,13 +10,11 @@ type Account struct {
SECBalance int `json:"sec_balance"`
SECNonce int `json:"sec_nonce"`
SpeculativeNonce int `json:"speculative_nonce"`
} `json:"data"`
}
type AccountHotspotsResp struct {
Data []AccountHotspot `json:"data"`
}
type AccountHotspot struct {
type AccountHotspots struct {
Data []struct {
Lng float64 `json:"lng"`
Lat float64 `json:"lat"`
TimestampAdded string `json:"timestamp_added"`
@ -55,24 +50,20 @@ type AccountHotspot struct {
BlockAdded int `json:"block_added"`
Block int `json:"block"`
Address string `json:"address"`
} `json:"data"`
}
type ActivityCountsResp struct {
Data ActivityCounts
type ActivityCounts struct {
Data map[string]int
}
type ActivityCounts map[string]int
type RewardTotalResp struct {
type RewardTotal struct {
Meta struct {
MinTime string `json:"min_time"`
MaxTime string `json:"max_time"`
} `json:"meta"`
Data RewardTotal `json:"data"`
}
type RewardTotal struct {
Data struct {
Total float64 `json:"total"`
Sum float64 `json:"sum"`
Stddev float64 `json:"stddev"`
@ -80,22 +71,18 @@ type RewardTotal struct {
Median float64 `json:"median"`
Max float64 `json:"max"`
Avg float64 `json:"avg"`
}
type CurrentOraclePriceResp struct {
Data CurrentOraclePrice `json:"data"`
} `json:"data"`
}
type CurrentOraclePrice struct {
Data struct {
Price int `json:"price"`
Block int `json:"block"`
}
type BlockchainStatsResp struct {
Data BlockchainStats `json:"data"`
} `json:"data"`
}
type BlockchainStats struct {
Data struct {
BlockTime struct {
LastDay struct {
Avg float64 `json:"avg"`
@ -149,4 +136,5 @@ type BlockchainStats struct {
} `json:"last_week"`
} `json:"election_times"`
TokenSupply float64 `json:"token_supply"`
} `json:"data"`
}