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"
|
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/pagination"
|
||||||
"github.com/wormhole-foundation/wormhole-explorer/api/internal/tvl"
|
"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"
|
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
|
||||||
sdk "github.com/wormhole-foundation/wormhole/sdk/vaa"
|
sdk "github.com/wormhole-foundation/wormhole/sdk/vaa"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"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.
|
// Pagination is implemented using a keyset cursor pattern, based on the (timestamp, ID) pair.
|
||||||
func (r *Repository) ListTransactionsByAddress(
|
func (r *Repository) ListTransactionsByAddress(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
address *types.Address,
|
address string,
|
||||||
pagination *pagination.Pagination,
|
pagination *pagination.Pagination,
|
||||||
) ([]TransactionDto, error) {
|
) ([]TransactionDto, error) {
|
||||||
|
|
||||||
// Build the aggregation pipeline
|
// Build the aggregation pipeline
|
||||||
var pipeline mongo.Pipeline
|
var pipeline mongo.Pipeline
|
||||||
{
|
{
|
||||||
// filter by address
|
// filter transactions by destination address
|
||||||
pipeline = append(pipeline, bson.D{
|
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
|
// specify sorting criteria
|
||||||
pipeline = append(pipeline, bson.D{
|
pipeline = append(pipeline, bson.D{
|
||||||
|
|
|
@ -130,7 +130,7 @@ func (s *Service) ListTransactions(
|
||||||
|
|
||||||
func (s *Service) ListTransactionsByAddress(
|
func (s *Service) ListTransactionsByAddress(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
address *types.Address,
|
address string,
|
||||||
pagination *pagination.Pagination,
|
pagination *pagination.Pagination,
|
||||||
) ([]TransactionDto, error) {
|
) ([]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.
|
// ExtractAddressFromQueryParams parses the `address` parameter from the query string.
|
||||||
//
|
//
|
||||||
// If the parameter doesn't exist, the function returns a nil address without errors.
|
// If the parameter is not present, the function returns an empty string
|
||||||
func ExtractAddressFromQueryParams(c *fiber.Ctx, l *zap.Logger) (*types.Address, error) {
|
func ExtractAddressFromQueryParams(c *fiber.Ctx, l *zap.Logger) string {
|
||||||
|
return c.Query("address")
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtractAddressFromPath parses the `id` parameter from the route path.
|
// ExtractAddressFromPath parses the `id` parameter from the route path.
|
||||||
|
|
|
@ -365,14 +365,11 @@ func (c *Controller) ListTransactions(ctx *fiber.Ctx) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
address, err := middleware.ExtractAddressFromQueryParams(ctx, c.logger)
|
address := middleware.ExtractAddressFromQueryParams(ctx, c.logger)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query transactions from the database
|
// Query transactions from the database
|
||||||
var dtos []transactions.TransactionDto
|
var dtos []transactions.TransactionDto
|
||||||
if address != nil {
|
if address != "" {
|
||||||
dtos, err = c.srv.ListTransactionsByAddress(ctx.Context(), address, pagination)
|
dtos, err = c.srv.ListTransactionsByAddress(ctx.Context(), address, pagination)
|
||||||
} else {
|
} else {
|
||||||
dtos, err = c.srv.ListTransactions(ctx.Context(), pagination)
|
dtos, err = c.srv.ListTransactions(ctx.Context(), pagination)
|
||||||
|
|
Loading…
Reference in New Issue