sarafu-api/event/msg.go

67 lines
1.2 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-13 22:36:45 +01:00
EventTokenMintTag = "TOKEN_MINT"
2025-01-13 17:33:55 +01:00
EventRegistrationTag = "CUSTODIAL_REGISTRATION"
)
type Msg struct {
Typ string
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 {
To string
Value int
VoucherAddress string
TxHash string
From string
}
type EventTokenMint struct {
To string
Value int
TxHash string
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)
}