Skip to content

Commit

Permalink
feat: add within network check for approve events
Browse files Browse the repository at this point in the history
Merge commit '5962c26' from cel2
  • Loading branch information
kamikazechaser committed Nov 20, 2024
2 parents e503ecf + 5962c26 commit 4470095
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmd/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
}
20 changes: 18 additions & 2 deletions internal/handler/token_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down
15 changes: 3 additions & 12 deletions internal/handler/token_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
12 changes: 12 additions & 0 deletions internal/handler/within_network.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4470095

Please sign in to comment.