sarafu-api/event/msg.go

67 lines
1.3 KiB
Go
Raw Normal View History

2025-01-13 17:33:55 +01:00
package event
import (
"context"
2025-01-13 21:17:55 +01:00
"fmt"
2025-01-13 17:33:55 +01:00
)
const (
// TODO: integrate with sarafu-vise-events
EventTokenTransferTag = "TOKEN_TRANSFER"
2025-01-21 16:30:22 +01:00
EventTokenMintTag = "TOKEN_MINT"
EventRegistrationTag = "CUSTODIAL_REGISTRATION"
2025-01-13 17:33:55 +01:00
)
type Msg struct {
2025-01-21 16:30:22 +01:00
Typ string
2025-01-13 17:33:55 +01:00
Item any
}
type EmitterFunc func(context.Context, Msg) error
// fields used for handling custodial registration event.
type EventCustodialRegistration struct {
Account string
}
// fields used for handling token transfer event.
type EventTokenTransfer struct {
2025-01-21 16:30:22 +01:00
To string
Value int
2025-01-13 17:33:55 +01:00
VoucherAddress string
2025-01-21 16:30:22 +01:00
TxHash string
From string
2025-01-13 17:33:55 +01:00
}
type EventTokenMint struct {
2025-01-21 16:30:22 +01:00
To string
Value int
TxHash string
2025-01-13 17:33:55 +01:00
VoucherAddress string
}
2025-01-13 21:17:55 +01:00
type EventsHandlerFunc func(context.Context, any) error
type EventsHandler struct {
handlers map[string]EventsHandlerFunc
}
func NewEventsHandler() *EventsHandler {
return &EventsHandler{
handlers: make(map[string]EventsHandlerFunc),
}
}
func (eh *EventsHandler) WithHandler(tag string, fn EventsHandlerFunc) *EventsHandler {
eh.handlers[tag] = fn
return eh
}
func (eh *EventsHandler) Handle(ctx context.Context, tag string, o any) error {
fn, ok := eh.handlers[tag]
if !ok {
2025-01-13 22:36:45 +01:00
return fmt.Errorf("Handler not registered for tag: %s", tag)
2025-01-13 21:17:55 +01:00
}
return fn(ctx, o)
}