diff --git a/dev/api.go b/dev/api.go index 23bb5de..f953c77 100644 --- a/dev/api.go +++ b/dev/api.go @@ -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 diff --git a/event/msg.go b/event/msg.go new file mode 100644 index 0000000..c3f8337 --- /dev/null +++ b/event/msg.go @@ -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 +}