97 lines
3.8 KiB
Go
97 lines
3.8 KiB
Go
package activity
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type ActivityResp struct {
|
|
Data []json.RawMessage `json:"data"`
|
|
}
|
|
|
|
// 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.
|
|
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":
|
|
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)
|
|
case "assert_location_v1":
|
|
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)
|
|
case "assert_location_v2":
|
|
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)
|
|
case "payment_v1":
|
|
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)
|
|
case "payment_v2":
|
|
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)
|
|
case "rewards_v1":
|
|
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)
|
|
case "stake_validator_v1":
|
|
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)
|
|
case "token_burn_v1":
|
|
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)
|
|
case "unstake_validator_v1":
|
|
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)
|
|
default:
|
|
fmt.Printf("ignoring unimplemented activy type: %v", activityType.Type)
|
|
}
|
|
}
|
|
|
|
return &activities, nil
|
|
}
|