Skip to content

Commit

Permalink
feat: add token approve handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kamikazechaser committed Oct 14, 2024
1 parent e1158ab commit e03dabd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
}
78 changes: 78 additions & 0 deletions internal/handler/token_approve.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit e03dabd

Please sign in to comment.