Move in events specs from vise events

This commit is contained in:
lash 2025-01-13 16:33:55 +00:00
parent f20238e16a
commit 25149ff265
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 68 additions and 2 deletions

View File

@ -16,6 +16,7 @@ import (
"git.defalsify.org/vise.git/db"
fsdb "git.defalsify.org/vise.git/db/fs"
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
"git.grassecon.net/grassrootseconomics/sarafu-api/event"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
)
@ -74,6 +75,7 @@ type DevAccountService struct {
autoVouchers []string
autoVoucherValue map[string]int
defaultAccount string
emitterFunc event.EmitterFunc
// accountsSession map[string]string
}
@ -107,6 +109,11 @@ func NewDevAccountService(ctx context.Context, d string) *DevAccountService {
return svc
}
func (das *DevAccountService) WithEmitter(fn event.EmitterFunc) *DevAccountService {
das.emitterFunc = fn
return das
}
func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v []byte) error {
var acc Account
@ -306,7 +313,17 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
das.defaultAccount = pubKey
}
logg.InfoCtxf(ctx, "account created", "account", acc)
if das.emitterFunc != nil {
msg := event.Msg{
Typ: event.EventRegistrationTag,
Item: acc,
}
err = das.emitterFunc(ctx, msg)
if err != nil {
logg.ErrorCtxf(ctx, "emitter returned error", "err", err, "msg", msg)
}
}
logg.TraceCtxf(ctx, "account created", "account", acc)
return &models.AccountResult{
PublicKey: pubKey,
@ -458,7 +475,17 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
return nil, err
}
das.txs[hsh] = mytx
logg.InfoCtxf(ctx, "token transfer created", "tx", mytx)
if das.emitterFunc != nil {
msg := event.Msg{
Typ: event.EventTokenTransferTag,
Item: mytx,
}
err = das.emitterFunc(ctx, msg)
if err != nil {
logg.ErrorCtxf(ctx, "emitter returned error", "err", err, "msg", msg)
}
}
logg.TraceCtxf(ctx, "token transfer created", "tx", mytx)
return &models.TokenTransferResponse{
TrackingId: uid.String(),
}, nil

39
event/msg.go Normal file
View File

@ -0,0 +1,39 @@
package event
import (
"context"
)
const (
// TODO: integrate with sarafu-vise-events
EventTokenTransferTag = "TOKEN_TRANSFER"
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
}