feat: refactor handlers

This commit is contained in:
2024-09-18 17:12:31 +03:00
parent 315e09fad9
commit fe7f096026
28 changed files with 1030 additions and 1147 deletions

View File

@@ -9,20 +9,13 @@ import (
)
type (
Callback func(context.Context, event.Event) error
HandlerFunc func(context.Context, interface{}, Callback) error
Callback func(context.Context, event.Event) error
LogPayload struct {
Log *types.Log
Timestamp uint64
}
LogRouteEntry struct {
Name string
Signature common.Hash
HandlerFunc HandlerFunc
}
InputDataPayload struct {
From string
InputData string
@@ -32,56 +25,65 @@ type (
TxHash string
}
LogHandlerFunc func(context.Context, LogPayload, Callback) error
InputDataHandlerFunc func(context.Context, InputDataPayload, Callback) error
LogRouteEntry struct {
Signature common.Hash
HandlerFunc LogHandlerFunc
}
InputDataEntry struct {
Name string
Signature string
HandlerFunc HandlerFunc
HandlerFunc InputDataHandlerFunc
}
Router struct {
callbackFn Callback
logHandlers map[common.Hash]LogRouteEntry
inputDataHandlers map[string]InputDataEntry
}
)
func New() *Router {
func New(callbackFn Callback) *Router {
return &Router{
callbackFn: callbackFn,
logHandlers: make(map[common.Hash]LogRouteEntry),
inputDataHandlers: make(map[string]InputDataEntry),
}
}
func (r *Router) RegisterLogRoute(signature common.Hash, handlerFunc HandlerFunc) {
func (r *Router) RegisterLogRoute(signature common.Hash, handlerFunc LogHandlerFunc) {
r.logHandlers[signature] = LogRouteEntry{
Signature: signature,
HandlerFunc: handlerFunc,
}
}
func (r *Router) RegisterInputDataRoute(signature string, handlerFunc HandlerFunc) {
func (r *Router) RegisterInputDataRoute(signature string, handlerFunc InputDataHandlerFunc) {
r.inputDataHandlers[signature] = InputDataEntry{
Signature: signature,
HandlerFunc: handlerFunc,
}
}
func (r *Router) ProcessLog(ctx context.Context, payload LogPayload, cb Callback) error {
func (r *Router) ProcessLog(ctx context.Context, payload LogPayload) error {
handler, ok := r.logHandlers[payload.Log.Topics[0]]
if ok {
return handler.HandlerFunc(ctx, payload, cb)
return handler.HandlerFunc(ctx, payload, r.callbackFn)
}
return nil
}
func (r *Router) ProcessInputData(ctx context.Context, payload InputDataPayload, cb Callback) error {
func (r *Router) ProcessInputData(ctx context.Context, payload InputDataPayload) error {
if len(payload.InputData) < 8 {
return nil
}
handler, ok := r.inputDataHandlers[payload.InputData[:8]]
if ok {
return handler.HandlerFunc(ctx, payload, cb)
return handler.HandlerFunc(ctx, payload, r.callbackFn)
}
return nil