github.com/cosmos/cosmos-sdk@v0.50.10/runtime/events.go (about)

     1  package runtime
     2  
     3  import (
     4  	"context"
     5  
     6  	"google.golang.org/protobuf/runtime/protoiface"
     7  
     8  	"cosmossdk.io/core/event"
     9  
    10  	sdk "github.com/cosmos/cosmos-sdk/types"
    11  )
    12  
    13  var _ event.Service = (*EventService)(nil)
    14  
    15  type EventService struct {
    16  	Events
    17  }
    18  
    19  func (es EventService) EventManager(ctx context.Context) event.Manager {
    20  	sdkCtx := sdk.UnwrapSDKContext(ctx)
    21  	return &Events{sdkCtx.EventManager()}
    22  }
    23  
    24  var _ event.Manager = (*Events)(nil)
    25  
    26  type Events struct {
    27  	sdk.EventManagerI
    28  }
    29  
    30  func NewEventManager(ctx context.Context) event.Manager {
    31  	sdkCtx := sdk.UnwrapSDKContext(ctx)
    32  	return &Events{sdkCtx.EventManager()}
    33  }
    34  
    35  // Emit emits an typed event that is defined in the protobuf file.
    36  // In the future these events will be added to consensus.
    37  func (e Events) Emit(ctx context.Context, event protoiface.MessageV1) error {
    38  	return e.EventManagerI.EmitTypedEvent(event)
    39  }
    40  
    41  // EmitKV emits a key value pair event.
    42  func (e Events) EmitKV(ctx context.Context, eventType string, attrs ...event.Attribute) error {
    43  	attributes := make([]sdk.Attribute, 0, len(attrs))
    44  
    45  	for _, attr := range attrs {
    46  		attributes = append(attributes, sdk.NewAttribute(attr.Key, attr.Value))
    47  	}
    48  
    49  	e.EventManagerI.EmitEvents(sdk.Events{sdk.NewEvent(eventType, attributes...)})
    50  	return nil
    51  }
    52  
    53  // Emit emits an typed event that is defined in the protobuf file.
    54  // In the future these events will be added to consensus.
    55  func (e Events) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error {
    56  	return e.EventManagerI.EmitTypedEvent(event)
    57  }