diff --git a/internal/services/events.go b/internal/services/events.go index 97240bf..e1733ed 100644 --- a/internal/services/events.go +++ b/internal/services/events.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strings" "time" "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" @@ -59,9 +60,7 @@ func (s *Service) processEvent( err = s.processFinalityProviderEditedEvent(ctx, bbnEvent) case EventFinalityProviderStatusChange: log.Debug().Msg("Processing finality provider status change event") - // TODO: fix error from this event - // https://github.com/babylonlabs-io/babylon-staking-indexer/issues/24 - s.processFinalityProviderStateChangeEvent(ctx, bbnEvent) + err = s.processFinalityProviderStateChangeEvent(ctx, bbnEvent) case EventBTCDelegationCreated: log.Debug().Msg("Processing new BTC delegation event") err = s.processNewBTCDelegationEvent(ctx, bbnEvent, blockHeight) @@ -124,8 +123,11 @@ func parseEvent[T proto.Message]( ) } + // Sanitize the event attributes before parsing + sanitizedEvent := sanitizeEvent(event) + // Use the SDK's ParseTypedEvent function - protoMsg, err := sdk.ParseTypedEvent(event) + protoMsg, err := sdk.ParseTypedEvent(sanitizedEvent) if err != nil { log.Debug().Interface("raw_event", event).Msg("Raw event data") return result, types.NewError( @@ -390,3 +392,27 @@ func (s *Service) validateSlashedFinalityProviderEvent(ctx context.Context, even return true, nil } + +func sanitizeEvent(event abcitypes.Event) abcitypes.Event { + sanitizedAttrs := make([]abcitypes.EventAttribute, len(event.Attributes)) + for i, attr := range event.Attributes { + // Remove any extra quotes and ensure proper JSON formatting + value := strings.Trim(attr.Value, "\"") + // If the value isn't already a JSON value (object, array, or quoted string), + // wrap it in quotes + if !strings.HasPrefix(value, "{") && !strings.HasPrefix(value, "[") { + value = fmt.Sprintf("\"%s\"", value) + } + + sanitizedAttrs[i] = abcitypes.EventAttribute{ + Key: attr.Key, + Value: value, + Index: attr.Index, + } + } + + return abcitypes.Event{ + Type: event.Type, + Attributes: sanitizedAttrs, + } +}