Skip to content

Commit 167ce20

Browse files
author
colinlyguo
committed
address comments
1 parent 260a398 commit 167ce20

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

rollup/internal/orm/pending_transaction.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package orm
33
import (
44
"bytes"
55
"context"
6-
"database/sql"
6+
"errors"
77
"fmt"
88
"time"
99

@@ -212,21 +212,23 @@ func (o *PendingTransaction) UpdateOtherTransactionsAsFailedByNonce(ctx context.
212212
// GetMaxNonceBySenderAddress retrieves the maximum nonce for a specific sender address.
213213
// Returns -1 if no transactions are found for the given address.
214214
func (o *PendingTransaction) GetMaxNonceBySenderAddress(ctx context.Context, senderAddress string) (int64, error) {
215-
var maxNonce sql.NullInt64
215+
var result struct {
216+
Nonce int64 `gorm:"column:nonce"`
217+
}
216218

217-
row := o.db.WithContext(ctx).
219+
err := o.db.WithContext(ctx).
218220
Model(&PendingTransaction{}).
219-
Select("MAX(nonce)").
221+
Select("nonce").
220222
Where("sender_address = ?", senderAddress).
221-
Row()
223+
Order("nonce DESC").
224+
First(&result).Error
222225

223-
if err := row.Scan(&maxNonce); err != nil {
226+
if err != nil {
227+
if errors.Is(err, gorm.ErrRecordNotFound) {
228+
return -1, nil
229+
}
224230
return -1, fmt.Errorf("failed to get max nonce by sender address, address: %s, err: %w", senderAddress, err)
225231
}
226232

227-
if !maxNonce.Valid {
228-
return -1, nil
229-
}
230-
231-
return maxNonce.Int64, nil
233+
return result.Nonce, nil
232234
}

0 commit comments

Comments
 (0)