Skip to content

Commit

Permalink
feat(eventindexer): store swap sender correctly, plus check min amt (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey authored Jul 7, 2023
1 parent 03eb96f commit 67ba5e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
25 changes: 22 additions & 3 deletions packages/eventindexer/indexer/save_swap_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ package indexer
import (
"context"
"encoding/json"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
"github.com/taikoxyz/taiko-mono/packages/eventindexer/contracts/swap"
)

var (
minTradeAmount = big.NewInt(10000000000000000)
)

func (svc *Service) saveSwapEvents(
ctx context.Context,
chainID *big.Int,
Expand All @@ -24,8 +30,6 @@ func (svc *Service) saveSwapEvents(
for {
event := events.Event

log.Infof("new Swap event for sender: %v", event.Sender.Hex())

if err := svc.saveSwapEvent(ctx, chainID, event); err != nil {
eventindexer.SwapEventsProcessedError.Inc()

Expand All @@ -43,6 +47,21 @@ func (svc *Service) saveSwapEvent(
chainID *big.Int,
event *swap.SwapSwap,
) error {
log.Infof("swap event for sender 0x%v, amount: %v",
common.Bytes2Hex(event.Raw.Topics[2].Bytes()[12:]),
event.Amount0In.String(),
)

// we only want events with > 0.1 ETH swap
if event.Amount0In.Cmp(minTradeAmount) <= 0 && event.Amount1Out.Cmp(minTradeAmount) <= 0 {
log.Infof("skipping skip event, min trade too low. amountIn: %v, amountOut: %v",
event.Amount0In.String(),
event.Amount1Out.String(),
)

return nil
}

marshaled, err := json.Marshal(event)
if err != nil {
return errors.Wrap(err, "json.Marshal(event)")
Expand All @@ -53,7 +72,7 @@ func (svc *Service) saveSwapEvent(
Data: string(marshaled),
ChainID: chainID,
Event: eventindexer.EventNameSwap,
Address: event.Sender.Hex(),
Address: fmt.Sprintf("0x%v", common.Bytes2Hex(event.Raw.Topics[2].Bytes()[12:])),
})
if err != nil {
return errors.Wrap(err, "svc.eventRepo.Save")
Expand Down
2 changes: 0 additions & 2 deletions packages/eventindexer/indexer/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ func (svc *Service) subscribeSwap(ctx context.Context, chainID *big.Int, errChan
errChan <- errors.Wrap(err, "sub.Err()")
case event := <-sink:
go func() {
log.Infof("swap event for sender %v", event.Sender.Hex())

if err := svc.saveSwapEvent(ctx, chainID, event); err != nil {
eventindexer.SwapEventsProcessedError.Inc()

Expand Down

0 comments on commit 67ba5e4

Please sign in to comment.