-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relayer): HTTP api for exposing events table for bridge UI (#271)
* WIP on HTTP api for exposing events table for bridge UI * error checking * escape string * newHTTPServer tests
- Loading branch information
1 parent
0052673
commit 7b5e6b8
Showing
17 changed files
with
325 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package http | ||
|
||
import ( | ||
"errors" | ||
"html" | ||
"math/big" | ||
"net/http" | ||
|
||
"github.com/cyberhorsey/webutils" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func (srv *Server) GetEventsByAddress(c echo.Context) error { | ||
chainID, ok := new(big.Int).SetString(c.QueryParam("chainID"), 10) | ||
if !ok { | ||
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, errors.New("invalid chain id")) | ||
} | ||
|
||
address := html.EscapeString(c.QueryParam("address")) | ||
|
||
events, err := srv.eventRepo.FindAllByAddress(c.Request().Context(), chainID, common.HexToAddress(address)) | ||
if err != nil { | ||
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err) | ||
} | ||
|
||
return c.JSON(http.StatusOK, events) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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/relayer" | ||
) | ||
|
||
func Test_GetEventsByAddress(t *testing.T) { | ||
srv := newTestServer("") | ||
|
||
_, err := srv.eventRepo.Save(context.Background(), relayer.SaveEventOpts{ | ||
Name: "name", | ||
Data: `{"Owner": "0x0000000000000000000000000000000000000123"}`, | ||
ChainID: big.NewInt(167001), | ||
Status: relayer.EventStatusNew, | ||
}) | ||
|
||
assert.Equal(t, nil, err) | ||
|
||
tests := []struct { | ||
name string | ||
address string | ||
chainID string | ||
wantStatus int | ||
wantBodyRegexpMatches []string | ||
}{ | ||
{ | ||
"successEmptyList", | ||
"0x456", | ||
"167001", | ||
http.StatusOK, | ||
[]string{`\[\]`}, | ||
}, | ||
{ | ||
"success", | ||
"0x0000000000000000000000000000000000000123", | ||
"167001", | ||
http.StatusOK, | ||
[]string{`[{"id":780800018316137516,"name":"name", | ||
"data":{"Owner":"0x0000000000000000000000000000000000000123"},"status":0,"chainID":167001}]`}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := testutils.NewUnauthenticatedRequest( | ||
echo.GET, | ||
fmt.Sprintf("/events?address=%v&chainID=%v", | ||
tt.address, | ||
tt.chainID), | ||
|
||
nil, | ||
) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
srv.ServeHTTP(rec, req) | ||
|
||
testutils.AssertStatusAndBody(t, rec, tt.wantStatus, tt.wantBodyRegexpMatches) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.