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/activity/activities.go

98 lines
3.8 KiB
Go
Raw Normal View History

2021-09-25 01:30:22 +00:00
package activity
import (
"encoding/json"
"fmt"
2021-10-03 21:06:16 +00:00
"log"
2021-09-25 01:30:22 +00:00
)
type ActivityResp struct {
Data []json.RawMessage `json:"data"`
}
2021-09-26 04:42:34 +00:00
// Parse the response from the activity api.
// The helium api return a list of of activities in an array with mixed type.
// In order to operate on the array, we determine the type using the "type"
// property, unmarshal the activity with the proper model, and add it to the proper
// array in an Activities structure.
2021-09-25 01:30:22 +00:00
func NewActivities(resp ActivityResp) (*Activities, error) {
type ActivityType struct {
Type string `json:"type"`
}
activityType := ActivityType{}
activities := Activities{}
for _, activityRaw := range resp.Data {
if err := json.Unmarshal(activityRaw, &activityType); err != nil {
return nil, fmt.Errorf("failed to unmarshal activity: %v", err)
}
switch activityType.Type {
case "add_gateway_v1":
2021-09-25 19:09:40 +00:00
addGatewayV1 := AddGatewayV1{}
if err := json.Unmarshal(activityRaw, &addGatewayV1); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.AddGatewayV1 = append(activities.AddGatewayV1, addGatewayV1)
2021-09-25 01:30:22 +00:00
case "assert_location_v1":
2021-09-25 19:09:40 +00:00
assertLocationV1 := AssertLocationV1{}
if err := json.Unmarshal(activityRaw, &assertLocationV1); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.AssertLocationV1 = append(activities.AssertLocationV1, assertLocationV1)
2021-09-25 01:30:22 +00:00
case "assert_location_v2":
2021-09-25 19:09:40 +00:00
assertLocationV2 := AssertLocationV2{}
if err := json.Unmarshal(activityRaw, &assertLocationV2); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.AssertLocationV2 = append(activities.AssertLocationV2, assertLocationV2)
2021-09-25 01:30:22 +00:00
case "payment_v1":
2021-09-25 19:09:40 +00:00
paymentV1 := PaymentV1{}
if err := json.Unmarshal(activityRaw, &paymentV1); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.PaymentV1 = append(activities.PaymentV1, paymentV1)
2021-09-25 01:30:22 +00:00
case "payment_v2":
2021-09-25 19:09:40 +00:00
paymentV2 := PaymentV2{}
if err := json.Unmarshal(activityRaw, &paymentV2); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.PaymentV2 = append(activities.PaymentV2, paymentV2)
2021-09-25 01:30:22 +00:00
case "rewards_v1":
2021-09-25 19:09:40 +00:00
rewardV1 := RewardsV1{}
if err := json.Unmarshal(activityRaw, &rewardV1); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.RewardsV1 = append(activities.RewardsV1, rewardV1)
case "rewards_v2":
rewardV2 := RewardsV2{}
if err := json.Unmarshal(activityRaw, &rewardV2); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.RewardsV2 = append(activities.RewardsV2, rewardV2)
2021-09-25 01:30:22 +00:00
case "stake_validator_v1":
2021-09-25 19:09:40 +00:00
stakeValidatorV1 := StakeValidatorV1{}
if err := json.Unmarshal(activityRaw, &stakeValidatorV1); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.StakeValidatorV1 = append(activities.StakeValidatorV1, stakeValidatorV1)
2021-09-25 01:30:22 +00:00
case "token_burn_v1":
2021-09-25 19:09:40 +00:00
tokenBurnV1 := TokenBurnV1{}
if err := json.Unmarshal(activityRaw, &activities.TokenBurnV1); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.TokenBurnV1 = append(activities.TokenBurnV1, tokenBurnV1)
2021-09-25 01:30:22 +00:00
case "unstake_validator_v1":
2021-09-25 19:09:40 +00:00
unstakeValidatorV1 := UnstakeValidatorV1{}
if err := json.Unmarshal(activityRaw, &activities.UnstakeValidatorV1); err != nil {
return nil, fmt.Errorf("failed to unmarshal %v: %v", activityType.Type, err)
}
activities.UnstakeValidatorV1 = append(activities.UnstakeValidatorV1, unstakeValidatorV1)
2021-09-25 01:30:22 +00:00
default:
2021-10-03 21:06:16 +00:00
log.Printf("ignoring unimplemented activy type: %v", activityType.Type)
2021-09-25 01:30:22 +00:00
}
}
return &activities, nil
}