Skip to content

Commit

Permalink
add event core api to runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Mar 25, 2023
1 parent 443eb3d commit ce684e2
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions runtime/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package runtime

import (
"context"

"cosmossdk.io/core/event"
"google.golang.org/protobuf/runtime/protoiface"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ event.Service = (*EventService)(nil)

type EventService struct {
Events
}

func (es EventService) EventManager(ctx context.Context) event.Manager {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &Events{sdkCtx.EventManager()}
}

var _ event.Manager = (*Events)(nil)

type Events struct {
sdk.EventManagerI
}

func NewEventManager(ctx context.Context) event.Manager {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &Events{sdkCtx.EventManager()}
}

// Emit emits an typed event that is defined in the protobuf file.
// In the future these events will be added to consensus
func (e Events) Emit(ctx context.Context, event protoiface.MessageV1) error {
e.EventManagerI.EmitTypedEvent(event)
return nil
}

// EmitKV emits a key value pair event
func (e Events) EmitKV(ctx context.Context, eventType string, attrs ...event.Attribute) error {
attributes := make([]sdk.Attribute, 0, len(attrs))

for _, attr := range attrs {
attributes = append(attributes, sdk.NewAttribute(attr.Key, attr.Value))
}

events := sdk.Events{
sdk.NewEvent(eventType, attributes...),
}
e.EventManagerI.EmitEvents(events)
return nil
}

// Emit emits an typed event that is defined in the protobuf file.
// In the future these events will be added to consensus
func (e Events) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error {
e.EventManagerI.EmitTypedEvent(event)

return nil
}

0 comments on commit ce684e2

Please sign in to comment.