Skip to content

Commit

Permalink
feat(eventindexer): add get events by address/name param for community (
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey authored Jun 21, 2023
1 parent 773331b commit 146f8d5
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/eventindexer/contracts/taikol1/TaikoL1.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/eventindexer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"database/sql"
"math/big"
"net/http"

"github.com/morkid/paginate"
"gorm.io/datatypes"
)

Expand Down Expand Up @@ -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)
}
22 changes: 22 additions & 0 deletions packages/eventindexer/http/get_by_address_and_event.go
Original file line number Diff line number Diff line change
@@ -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)
}
68 changes: 68 additions & 0 deletions packages/eventindexer/http/get_by_address_and_event_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
1 change: 1 addition & 0 deletions packages/eventindexer/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions packages/eventindexer/mock/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions packages/eventindexer/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 146f8d5

Please sign in to comment.