From 146f8d52100c3aa7412549e0703c4fc363a6ec29 Mon Sep 17 00:00:00 2001 From: jeff <113397187+cyberhorsey@users.noreply.github.com> Date: Tue, 20 Jun 2023 19:33:35 -0700 Subject: [PATCH] feat(eventindexer): add get events by address/name param for community (#14025) --- .../eventindexer/contracts/taikol1/TaikoL1.go | 12 ++-- packages/eventindexer/event.go | 8 +++ .../http/get_by_address_and_event.go | 22 ++++++ .../http/get_by_address_and_event_test.go | 68 +++++++++++++++++++ packages/eventindexer/http/routes.go | 1 + .../eventindexer/mock/event_repository.go | 21 ++++++ packages/eventindexer/repo/event.go | 22 ++++++ 7 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 packages/eventindexer/http/get_by_address_and_event.go create mode 100644 packages/eventindexer/http/get_by_address_and_event_test.go diff --git a/packages/eventindexer/contracts/taikol1/TaikoL1.go b/packages/eventindexer/contracts/taikol1/TaikoL1.go index 8fed2caf9e5..93ab0210445 100644 --- a/packages/eventindexer/contracts/taikol1/TaikoL1.go +++ b/packages/eventindexer/contracts/taikol1/TaikoL1.go @@ -8,12 +8,12 @@ import ( "math/big" "strings" - ethereum "github.com/taikochain/go-taiko" - "github.com/taikochain/go-taiko/accounts/abi" - "github.com/taikochain/go-taiko/accounts/abi/bind" - "github.com/taikochain/go-taiko/common" - "github.com/taikochain/go-taiko/core/types" - "github.com/taikochain/go-taiko/event" + ethereum "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/packages/eventindexer/event.go b/packages/eventindexer/event.go index 66fd1a68ad4..b695fb9359d 100644 --- a/packages/eventindexer/event.go +++ b/packages/eventindexer/event.go @@ -4,7 +4,9 @@ import ( "context" "database/sql" "math/big" + "net/http" + "github.com/morkid/paginate" "gorm.io/datatypes" ) @@ -65,4 +67,10 @@ type EventRepository interface { id int, ) error GetCountByAddressAndEventName(ctx context.Context, address string, event string) (int, error) + GetByAddressAndEventName( + ctx context.Context, + req *http.Request, + address string, + event string, + ) (paginate.Page, error) } diff --git a/packages/eventindexer/http/get_by_address_and_event.go b/packages/eventindexer/http/get_by_address_and_event.go new file mode 100644 index 00000000000..38d1410f8b9 --- /dev/null +++ b/packages/eventindexer/http/get_by_address_and_event.go @@ -0,0 +1,22 @@ +package http + +import ( + "net/http" + + "github.com/cyberhorsey/webutils" + "github.com/labstack/echo/v4" +) + +func (srv *Server) GetByAddressAndEventName(c echo.Context) error { + page, err := srv.eventRepo.GetByAddressAndEventName( + c.Request().Context(), + c.Request(), + c.QueryParam("address"), + c.QueryParam("event"), + ) + if err != nil { + return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err) + } + + return c.JSON(http.StatusOK, page) +} diff --git a/packages/eventindexer/http/get_by_address_and_event_test.go b/packages/eventindexer/http/get_by_address_and_event_test.go new file mode 100644 index 00000000000..526a3679d9a --- /dev/null +++ b/packages/eventindexer/http/get_by_address_and_event_test.go @@ -0,0 +1,68 @@ +package http + +import ( + "context" + "fmt" + "math/big" + "net/http" + "net/http/httptest" + "testing" + + "github.com/cyberhorsey/webutils/testutils" + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + "github.com/taikoxyz/taiko-mono/packages/eventindexer" +) + +func Test_GetByAddressAndEvent(t *testing.T) { + srv := newTestServer("") + + _, err := srv.eventRepo.Save(context.Background(), eventindexer.SaveEventOpts{ + Name: "name", + Data: `{"Owner": "0x0000000000000000000000000000000000000123"}`, + ChainID: big.NewInt(167001), + Address: "0x123", + Event: eventindexer.EventNameBlockProposed, + }) + + assert.Equal(t, nil, err) + + tests := []struct { + name string + address string + event string + wantStatus int + wantBodyRegexpMatches []string + }{ + { + "successZeroEvents", + "0xhasntProposedAnything", + eventindexer.EventNameBlockProposed, + http.StatusOK, + []string{`{"items":`}, + }, + { + "success", + "0x123", + eventindexer.EventNameBlockProposed, + http.StatusOK, + []string{`{"items":`}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := testutils.NewUnauthenticatedRequest( + echo.GET, + fmt.Sprintf("/events?address=%v&event=%v", tt.address, tt.event), + nil, + ) + + rec := httptest.NewRecorder() + + srv.ServeHTTP(rec, req) + + testutils.AssertStatusAndBody(t, rec, tt.wantStatus, tt.wantBodyRegexpMatches) + }) + } +} diff --git a/packages/eventindexer/http/routes.go b/packages/eventindexer/http/routes.go index 30a3aee777f..8c0f1620b54 100644 --- a/packages/eventindexer/http/routes.go +++ b/packages/eventindexer/http/routes.go @@ -7,5 +7,6 @@ func (srv *Server) configureRoutes() { srv.echo.GET("/uniqueProvers", srv.GetUniqueProvers) srv.echo.GET("/uniqueProposers", srv.GetUniqueProposers) srv.echo.GET("/eventByAddress", srv.GetCountByAddressAndEventName) + srv.echo.GET("/events", srv.GetByAddressAndEventName) srv.echo.GET("/stats", srv.GetStats) } diff --git a/packages/eventindexer/mock/event_repository.go b/packages/eventindexer/mock/event_repository.go index 6e274228637..9b5e3882130 100644 --- a/packages/eventindexer/mock/event_repository.go +++ b/packages/eventindexer/mock/event_repository.go @@ -3,7 +3,9 @@ package mock import ( "context" "math/rand" + "net/http" + "github.com/morkid/paginate" "github.com/taikoxyz/taiko-mono/packages/eventindexer" "gorm.io/datatypes" "gorm.io/gorm" @@ -59,6 +61,25 @@ func (r *EventRepository) GetCountByAddressAndEventName( return count, nil } +func (r *EventRepository) GetByAddressAndEventName( + ctx context.Context, + req *http.Request, + address string, + event string, +) (paginate.Page, error) { + var events []*eventindexer.Event + + for _, e := range r.events { + if e.Address == address && e.Event == event { + events = append(events, e) + } + } + + return paginate.Page{ + Items: events, + }, nil +} + func (r *EventRepository) FindByEventTypeAndBlockID( ctx context.Context, eventType string, diff --git a/packages/eventindexer/repo/event.go b/packages/eventindexer/repo/event.go index 2ce163a4d54..da647870fbe 100644 --- a/packages/eventindexer/repo/event.go +++ b/packages/eventindexer/repo/event.go @@ -2,7 +2,9 @@ package repo import ( "context" + "net/http" + "github.com/morkid/paginate" "github.com/pkg/errors" "github.com/taikoxyz/taiko-mono/packages/eventindexer" "gorm.io/datatypes" @@ -112,3 +114,23 @@ func (r *EventRepository) GetCountByAddressAndEventName( return count, nil } + +func (r *EventRepository) GetByAddressAndEventName( + ctx context.Context, + req *http.Request, + address string, + event string, +) (paginate.Page, error) { + pg := paginate.New(&paginate.Config{ + DefaultSize: 100, + }) + + q := r.db.GormDB(). + Raw("SELECT * FROM events WHERE event = ? AND address = ?", event, address) + + reqCtx := pg.With(q) + + page := reqCtx.Request(req).Response(&[]eventindexer.Event{}) + + return page, nil +}