From e03dabdf0f6a57937301aa2473de4633120a8d61 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Mon, 14 Oct 2024 09:25:18 +0300 Subject: [PATCH] feat: add token approve handler --- cmd/service/router.go | 2 + internal/handler/token_approve.go | 78 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 internal/handler/token_approve.go diff --git a/cmd/service/router.go b/cmd/service/router.go index b0d7599..83eb2ec 100644 --- a/cmd/service/router.go +++ b/cmd/service/router.go @@ -25,6 +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.RegisterInputDataRoute("63e4bff4", handler.HandleFaucetGiveInputData()) router.RegisterInputDataRoute("de82efb4", handler.HandleFaucetGiveInputData()) @@ -41,6 +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()) return router } diff --git a/internal/handler/token_approve.go b/internal/handler/token_approve.go new file mode 100644 index 0000000..7f32925 --- /dev/null +++ b/internal/handler/token_approve.go @@ -0,0 +1,78 @@ +package handler + +import ( + "context" + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/grassrootseconomics/eth-tracker/pkg/event" + "github.com/grassrootseconomics/eth-tracker/pkg/router" + "github.com/lmittmann/w3" +) + +const approveEventName = "TOKEN_APPROVE" + +var ( + tokenApproveEvent = w3.MustNewEvent("Approval(address indexed _owner, address indexed _spender, uint256 _value)") + tokenApproveToSig = w3.MustNewFunc("approve(address, uint256)", "bool") +) + +func HandleTokenApproveLog() router.LogHandlerFunc { + return func(ctx context.Context, lp router.LogPayload, c router.Callback) error { + var ( + owner common.Address + spender common.Address + value big.Int + ) + + if err := tokenApproveEvent.DecodeArgs(lp.Log, &owner, &spender, &value); err != nil { + return err + } + + tokenApproveEvent := event.Event{ + Index: lp.Log.Index, + Block: lp.Log.BlockNumber, + ContractAddress: lp.Log.Address.Hex(), + Success: true, + Timestamp: lp.Timestamp, + TxHash: lp.Log.TxHash.Hex(), + TxType: approveEventName, + Payload: map[string]any{ + "owner": owner.Hex(), + "spender": spender.Hex(), + "value": value.String(), + }, + } + + return c(ctx, tokenApproveEvent) + } +} + +func HandleTokenApproveInputData() router.InputDataHandlerFunc { + return func(ctx context.Context, idp router.InputDataPayload, c router.Callback) error { + var ( + spender common.Address + value big.Int + ) + + if err := tokenApproveToSig.DecodeArgs(w3.B(idp.InputData), &spender, &value); err != nil { + return err + } + + tokenApproveEvent := event.Event{ + Block: idp.Block, + ContractAddress: idp.ContractAddress, + Success: false, + Timestamp: idp.Timestamp, + TxHash: idp.TxHash, + TxType: approveEventName, + Payload: map[string]any{ + "owner": idp.From, + "spender": spender.Hex(), + "value": value.String(), + }, + } + + return c(ctx, tokenApproveEvent) + } +}