sarafu-vise/handlers/event/event.go

62 lines
1.6 KiB
Go
Raw Permalink Normal View History

2025-01-13 18:26:27 +01:00
package event
import (
2025-01-13 22:39:15 +01:00
"context"
2025-01-13 18:47:55 +01:00
"fmt"
"git.defalsify.org/vise.git/persist"
2025-01-13 22:39:15 +01:00
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
2025-01-13 18:26:27 +01:00
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
2025-01-13 18:26:27 +01:00
)
2025-01-13 22:39:15 +01:00
var (
logg = logging.NewVanilla().WithDomain("sarafu-vise.handlers.event")
)
type EventsUpdater struct {
2025-01-13 18:26:27 +01:00
api remote.AccountService
formatFunc func(string, int, any) string
2025-01-13 22:39:15 +01:00
store storage.StorageService
2025-01-13 18:26:27 +01:00
}
2025-01-13 22:39:15 +01:00
func NewEventsUpdater(api remote.AccountService, store storage.StorageService) *EventsUpdater {
return &EventsUpdater{
2025-01-13 18:26:27 +01:00
api: api,
2025-01-13 18:47:55 +01:00
formatFunc: func(tag string, i int, o any) string {
return fmt.Sprintf("%d %v", i, o)
},
store: store,
2025-01-13 18:26:27 +01:00
}
}
func (eu *EventsUpdater) ToEventsHandler() *apievent.EventsHandler {
eh := apievent.NewEventsHandler()
2025-01-14 08:02:42 +01:00
eh = eh.WithHandler(apievent.EventTokenMintTag, eu.handleTokenMint)
eh = eh.WithHandler(apievent.EventTokenTransferTag, eu.handleTokenTransfer)
eh = eh.WithHandler(apievent.EventRegistrationTag, eu.handleCustodialRegistration)
return eh
}
2025-01-13 22:39:15 +01:00
func (eu *EventsUpdater) handleNoop(ctx context.Context, ev any) error {
logg.WarnCtxf(ctx, "noop event handler")
return nil
}
func (eu *EventsUpdater) getStore(ctx context.Context) (*persist.Persister, *store.UserDataStore, error) {
userDb, err := eu.store.GetUserdataDb(ctx)
if err != nil {
return nil, nil, err
}
userStore := &store.UserDataStore{
Db: userDb,
}
pr, err := eu.store.GetPersister(ctx)
if err != nil {
return nil, nil, err
}
return pr, userStore, nil
}