Fix `GET /api/v1/transactions?address={addr}` (#550)
### Description This pull request fixes a broken database query in `GET /api/v1/transactions?address={addr}`, which was causing the endpoint to fail.
This commit is contained in:
parent
520997f6d7
commit
31d2d9fc72
|
@ -15,7 +15,6 @@ import (
|
|||
errs "github.com/wormhole-foundation/wormhole-explorer/api/internal/errors"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/api/internal/pagination"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/api/internal/tvl"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/api/types"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
|
||||
sdk "github.com/wormhole-foundation/wormhole/sdk/vaa"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
@ -879,17 +878,21 @@ func (r *Repository) FindTransactions(
|
|||
// Pagination is implemented using a keyset cursor pattern, based on the (timestamp, ID) pair.
|
||||
func (r *Repository) ListTransactionsByAddress(
|
||||
ctx context.Context,
|
||||
address *types.Address,
|
||||
address string,
|
||||
pagination *pagination.Pagination,
|
||||
) ([]TransactionDto, error) {
|
||||
|
||||
// Build the aggregation pipeline
|
||||
var pipeline mongo.Pipeline
|
||||
{
|
||||
// filter by address
|
||||
// filter transactions by destination address
|
||||
pipeline = append(pipeline, bson.D{
|
||||
{"$match", bson.D{{"parsedPayload.toAddress", bson.M{"$eq": "0x" + address.Hex()}}}},
|
||||
})
|
||||
{"$match", bson.D{
|
||||
{"$or", bson.A{
|
||||
bson.D{{"standardizedProperties.toAddress", bson.M{"$eq": address}}},
|
||||
bson.D{{"standardizedProperties.toAddress", bson.M{"$eq": "0x" + address}}},
|
||||
}},
|
||||
}}})
|
||||
|
||||
// specify sorting criteria
|
||||
pipeline = append(pipeline, bson.D{
|
||||
|
|
|
@ -130,7 +130,7 @@ func (s *Service) ListTransactions(
|
|||
|
||||
func (s *Service) ListTransactionsByAddress(
|
||||
ctx context.Context,
|
||||
address *types.Address,
|
||||
address string,
|
||||
pagination *pagination.Pagination,
|
||||
) ([]TransactionDto, error) {
|
||||
|
||||
|
|
|
@ -177,27 +177,9 @@ func ExtractObservationHash(c *fiber.Ctx, l *zap.Logger) (string, error) {
|
|||
|
||||
// ExtractAddressFromQueryParams parses the `address` parameter from the query string.
|
||||
//
|
||||
// If the parameter doesn't exist, the function returns a nil address without errors.
|
||||
func ExtractAddressFromQueryParams(c *fiber.Ctx, l *zap.Logger) (*types.Address, error) {
|
||||
|
||||
val := c.Query("address")
|
||||
|
||||
if val == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Attempt to parse the address
|
||||
addr, err := types.StringToAddress(val, true /*acceptSolanaFormat*/)
|
||||
if err != nil {
|
||||
requestID := fmt.Sprintf("%v", c.Locals("requestid"))
|
||||
l.Error("failed to decode address",
|
||||
zap.Error(err),
|
||||
zap.String("requestID", requestID),
|
||||
)
|
||||
return nil, response.NewInvalidParamError(c, "MALFORMED ADDR", errors.WithStack(err))
|
||||
}
|
||||
|
||||
return addr, nil
|
||||
// If the parameter is not present, the function returns an empty string
|
||||
func ExtractAddressFromQueryParams(c *fiber.Ctx, l *zap.Logger) string {
|
||||
return c.Query("address")
|
||||
}
|
||||
|
||||
// ExtractAddressFromPath parses the `id` parameter from the route path.
|
||||
|
|
|
@ -365,14 +365,11 @@ func (c *Controller) ListTransactions(ctx *fiber.Ctx) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
address, err := middleware.ExtractAddressFromQueryParams(ctx, c.logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
address := middleware.ExtractAddressFromQueryParams(ctx, c.logger)
|
||||
|
||||
// Query transactions from the database
|
||||
var dtos []transactions.TransactionDto
|
||||
if address != nil {
|
||||
if address != "" {
|
||||
dtos, err = c.srv.ListTransactionsByAddress(ctx.Context(), address, pagination)
|
||||
} else {
|
||||
dtos, err = c.srv.ListTransactions(ctx.Context(), pagination)
|
||||
|
|
Loading…
Reference in New Issue