From 5962c26659bed2d2fc6880b0688bbdcee32737e1 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Wed, 20 Nov 2024 10:57:51 +0300 Subject: [PATCH] feat: add within network check for approve events --- cmd/service/router.go | 4 ++-- internal/handler/token_approve.go | 20 ++++++++++++++++++-- internal/handler/token_transfer.go | 15 +++------------ internal/handler/within_network.go | 12 ++++++++++++ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 internal/handler/within_network.go diff --git a/cmd/service/router.go b/cmd/service/router.go index 83eb2ec..a864a49 100644 --- a/cmd/service/router.go +++ b/cmd/service/router.go @@ -25,7 +25,7 @@ func bootstrapEventRouter(cacheProvider cache.Cache, pubCB router.Callback) *rou router.RegisterLogRoute(w3.H("0xab8530f87dc9b59234c4623bf917212bb2536d647574c8e7e5da92c2ede0c9f8"), handler.HandleTokenMintLog()) router.RegisterLogRoute(w3.H("0x894e56e1dac400b4475c83d8af0f0aa44de17c62764bd82f6e768a504e242461"), handler.HandleCustodialRegistrationLog()) router.RegisterLogRoute(w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), handler.HandleTokenTransferLog(handlerContainer)) - router.RegisterLogRoute(w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"), handler.HandleTokenApproveLog()) + router.RegisterLogRoute(w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"), handler.HandleTokenApproveLog(handlerContainer)) router.RegisterInputDataRoute("63e4bff4", handler.HandleFaucetGiveInputData()) router.RegisterInputDataRoute("de82efb4", handler.HandleFaucetGiveInputData()) @@ -42,7 +42,7 @@ func bootstrapEventRouter(cacheProvider cache.Cache, pubCB router.Callback) *rou router.RegisterInputDataRoute("4420e486", handler.HandleCustodialRegistrationInputData()) router.RegisterInputDataRoute("a9059cbb", handler.HandleTokenTransferInputData(handlerContainer)) router.RegisterInputDataRoute("23b872dd", handler.HandleTokenTransferInputData(handlerContainer)) - router.RegisterInputDataRoute("095ea7b3", handler.HandleTokenApproveInputData()) + router.RegisterInputDataRoute("095ea7b3", handler.HandleTokenApproveInputData(handlerContainer)) return router } diff --git a/internal/handler/token_approve.go b/internal/handler/token_approve.go index 7f32925..1087ab0 100644 --- a/internal/handler/token_approve.go +++ b/internal/handler/token_approve.go @@ -17,7 +17,7 @@ var ( tokenApproveToSig = w3.MustNewFunc("approve(address, uint256)", "bool") ) -func HandleTokenApproveLog() router.LogHandlerFunc { +func HandleTokenApproveLog(hc *HandlerContainer) router.LogHandlerFunc { return func(ctx context.Context, lp router.LogPayload, c router.Callback) error { var ( owner common.Address @@ -29,6 +29,14 @@ func HandleTokenApproveLog() router.LogHandlerFunc { return err } + proceed, err := hc.checkWithinNetwork(ctx, lp.Log.Address.Hex(), owner.Hex(), spender.Hex()) + if err != nil { + return err + } + if !proceed { + return nil + } + tokenApproveEvent := event.Event{ Index: lp.Log.Index, Block: lp.Log.BlockNumber, @@ -48,7 +56,7 @@ func HandleTokenApproveLog() router.LogHandlerFunc { } } -func HandleTokenApproveInputData() router.InputDataHandlerFunc { +func HandleTokenApproveInputData(hc *HandlerContainer) router.InputDataHandlerFunc { return func(ctx context.Context, idp router.InputDataPayload, c router.Callback) error { var ( spender common.Address @@ -59,6 +67,14 @@ func HandleTokenApproveInputData() router.InputDataHandlerFunc { return err } + proceed, err := hc.checkWithinNetwork(ctx, idp.ContractAddress, idp.From, spender.Hex()) + if err != nil { + return err + } + if !proceed { + return nil + } + tokenApproveEvent := event.Event{ Block: idp.Block, ContractAddress: idp.ContractAddress, diff --git a/internal/handler/token_transfer.go b/internal/handler/token_transfer.go index aeac378..5cbd917 100644 --- a/internal/handler/token_transfer.go +++ b/internal/handler/token_transfer.go @@ -30,7 +30,7 @@ func HandleTokenTransferLog(hc *HandlerContainer) router.LogHandlerFunc { return err } - proceed, err := hc.checkTransferWithinNetwork(ctx, lp.Log.Address.Hex(), from.Hex(), to.Hex()) + proceed, err := hc.checkWithinNetwork(ctx, lp.Log.Address.Hex(), from.Hex(), to.Hex()) if err != nil { return err } @@ -79,7 +79,7 @@ func HandleTokenTransferInputData(hc *HandlerContainer) router.InputDataHandlerF return err } - proceed, err := hc.checkTransferWithinNetwork(ctx, idp.ContractAddress, idp.From, to.Hex()) + proceed, err := hc.checkWithinNetwork(ctx, idp.ContractAddress, idp.From, to.Hex()) if err != nil { return err } @@ -105,7 +105,7 @@ func HandleTokenTransferInputData(hc *HandlerContainer) router.InputDataHandlerF return err } - proceed, err := hc.checkTransferWithinNetwork(ctx, idp.ContractAddress, from.Hex(), to.Hex()) + proceed, err := hc.checkWithinNetwork(ctx, idp.ContractAddress, from.Hex(), to.Hex()) if err != nil { return err } @@ -125,12 +125,3 @@ func HandleTokenTransferInputData(hc *HandlerContainer) router.InputDataHandlerF return nil } } - -func (hc *HandlerContainer) checkTransferWithinNetwork(ctx context.Context, contractAddress string, from string, to string) (bool, error) { - exists, err := hc.cache.ExistsNetwork(ctx, contractAddress, from, to) - if err != nil { - return false, err - } - - return exists, nil -} diff --git a/internal/handler/within_network.go b/internal/handler/within_network.go new file mode 100644 index 0000000..c0c3c0e --- /dev/null +++ b/internal/handler/within_network.go @@ -0,0 +1,12 @@ +package handler + +import "context" + +func (hc *HandlerContainer) checkWithinNetwork(ctx context.Context, contractAddress string, from string, to string) (bool, error) { + exists, err := hc.cache.ExistsNetwork(ctx, contractAddress, from, to) + if err != nil { + return false, err + } + + return exists, nil +}