Skip to content

Commit

Permalink
fix query
Browse files Browse the repository at this point in the history
  • Loading branch information
Sledro committed Nov 14, 2024
1 parent 91c63b9 commit 01f7e9e
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions database/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"fmt"
"log"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -217,16 +218,39 @@ func (db *Database) GetTransactionsByStatus(ctx context.Context, status string,
}

func (db *Database) GetTransactionsProvenByStatus(ctx context.Context, status string) ([]models.TransactionProven, error) {
transactions, err := db.GetTransactionsByStatus(ctx, status, "withdrawal")
collection := db.client.Database(db.databaseName).Collection("transactions")

pipeline := mongo.Pipeline{
{{Key: "$match", Value: bson.D{
{Key: "type", Value: "withdrawal"},
{Key: "status", Value: status},
}}},
{{Key: "$lookup", Value: bson.D{
{Key: "from", Value: "transactions_proven"},
{Key: "localField", Value: "withdrawal_hash"},
{Key: "foreignField", Value: "withdrawal_hash"},
{Key: "as", Value: "proven"},
}}},
{{Key: "$unwind", Value: "$proven"}},
{{Key: "$replaceRoot", Value: bson.D{
{Key: "newRoot", Value: "$proven"},
}}},
}

cursor, err := collection.Aggregate(ctx, pipeline)
if err != nil {
return nil, fmt.Errorf("failed to get transactions by status: %w", err)
return nil, fmt.Errorf("failed to aggregate transactions: %w", err)
}
defer cursor.Close(ctx)

provenTransactions := make([]models.TransactionProven, len(transactions))
for i, transaction := range transactions {
provenTransactions[i] = models.TransactionProven{
WithdrawalHash: transaction.WithdrawalHash,
}
var provenTransactions []models.TransactionProven
if err := cursor.All(ctx, &provenTransactions); err != nil {
return nil, fmt.Errorf("failed to decode transactions: %w", err)
}

log.Printf("Found %d proven transactions for status %s", len(provenTransactions), status)
for _, tx := range provenTransactions {
log.Printf("Proven tx: hash=%s, l2OutputIndex=%d", tx.WithdrawalHash, tx.L2OutputIndex)
}

return provenTransactions, nil
Expand Down

0 comments on commit 01f7e9e

Please sign in to comment.