Add custodial registration handler

This commit is contained in:
lash 2024-10-24 16:00:46 +01:00
parent 7c10a881fe
commit cab491f0d9
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 69 additions and 25 deletions

View File

@ -0,0 +1,31 @@
package event
import (
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
)
const (
evReg = "CUSTODIAL_REGISTRATION"
)
type eventCustodialRegistration struct {
account string
}
func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistration, bool) {
var ok bool
var ev eventCustodialRegistration
if gev.TxType != evReg {
return nil, false
}
pl := gev.Payload
ev.account, ok = pl["account"].(string)
if !ok {
return nil, false
}
return &ev, true
}
func handleCustodialRegistration(ev *eventCustodialRegistration) error {
return nil
}

View File

@ -3,24 +3,19 @@ package nats
import (
"context"
"encoding/json"
"log/slog"
"os"
nats "github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"git.defalsify.org/vise.git/logging"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.grassecon.net/term/event"
)
const (
evGive = "TRACKER.FAUCET_GIVE"
evReg = "TRACKER.CUSTODIAL_REGISTRATION"
)
var (
logg = logging.NewVanilla().WithDomain("nats")
logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
)
type NatsSubscription struct {
@ -75,31 +70,31 @@ func(n *NatsSubscription) Close() error {
func fail(m jetstream.Msg) {
err := m.Nak()
if err != nil {
logg.Errorf("nats nak fail", "err", err)
logg.Error("nats nak fail", "err", err)
}
}
func(n *NatsSubscription) handleEvent(m jetstream.Msg) {
var ev geEvent.Event
logg.Tracef("have msg", "err", m)
logg.Debug("have msg", "err", m)
b := m.Data()
err := json.Unmarshal(b, &ev)
if err != nil {
logg.Errorf("nats msg deserialize fail", "err", err)
fail(m)
return
logg.Error("nats msg deserialize fail", "err", err)
//fail(m)
} else {
err = n.Route(&ev)
if err != nil {
logg.Error("handler route fail", "err", err)
fail(m)
return
}
}
err = n.Route(&ev)
err = m.Term()
if err != nil {
logg.Errorf("handler route fail", "err", err)
fail(m)
return
}
err = m.Ack()
// err = m.DoubleAck(n.ctx)
if err != nil {
logg.Errorf("ack fail", "err", err)
panic("ack fail")
logg.Error("term fail", "err", err)
panic("term fail")
}
logg.Debug("handle msg complete")
}

View File

@ -1,12 +1,30 @@
package event
import (
"fmt"
"log/slog"
"os"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
)
// TODO: this vocabulary should be public in and provided by the eth-tracker repo
const (
evGive = "FAUCET_GIVE"
)
var (
logg = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
)
type Router struct {
}
func(r *Router) Route(ev *geEvent.Event) error {
return nil
func(r *Router) Route(gev *geEvent.Event) error {
logg.Debug("have event", "ev", gev)
evCC, ok := asCustodialRegistrationEvent(gev)
if !ok {
return handleCustodialRegistration(evCC)
}
return fmt.Errorf("unexpected message")
}