@@ -597,3 +597,61 @@ func TestPendingTransactionOrm(t *testing.T) {
597597 err = pendingTransactionOrm .DeleteTransactionByTxHash (context .Background (), common .HexToHash ("0x123" ))
598598 assert .Error (t , err ) // Should return error for non-existent transaction
599599}
600+
601+ func TestPendingTransaction_GetMaxNonceBySenderAddress (t * testing.T ) {
602+ sqlDB , err := db .DB ()
603+ assert .NoError (t , err )
604+ assert .NoError (t , migrate .ResetDB (sqlDB ))
605+
606+ // When there are no transactions for this sender address, should return 0
607+ maxNonce , err := pendingTransactionOrm .GetMaxNonceBySenderAddress (context .Background (), "0xdeadbeef" )
608+ assert .NoError (t , err )
609+ assert .Equal (t , uint64 (0 ), maxNonce )
610+
611+ // Insert two transactions with different nonces for the same sender address
612+ senderMeta := & SenderMeta {
613+ Name : "testName" ,
614+ Service : "testService" ,
615+ Address : common .HexToAddress ("0xdeadbeef" ),
616+ Type : types .SenderTypeCommitBatch ,
617+ }
618+
619+ tx0 := gethTypes .NewTx (& gethTypes.DynamicFeeTx {
620+ Nonce : 1 ,
621+ To : & common.Address {},
622+ Data : []byte {},
623+ Gas : 21000 ,
624+ AccessList : gethTypes.AccessList {},
625+ Value : big .NewInt (0 ),
626+ ChainID : big .NewInt (1 ),
627+ GasTipCap : big .NewInt (0 ),
628+ GasFeeCap : big .NewInt (1 ),
629+ V : big .NewInt (0 ),
630+ R : big .NewInt (0 ),
631+ S : big .NewInt (0 ),
632+ })
633+ tx1 := gethTypes .NewTx (& gethTypes.DynamicFeeTx {
634+ Nonce : 3 ,
635+ To : & common.Address {},
636+ Data : []byte {},
637+ Gas : 22000 ,
638+ AccessList : gethTypes.AccessList {},
639+ Value : big .NewInt (0 ),
640+ ChainID : big .NewInt (1 ),
641+ GasTipCap : big .NewInt (1 ),
642+ GasFeeCap : big .NewInt (2 ),
643+ V : big .NewInt (0 ),
644+ R : big .NewInt (0 ),
645+ S : big .NewInt (0 ),
646+ })
647+
648+ err = pendingTransactionOrm .InsertPendingTransaction (context .Background (), "test" , senderMeta , tx0 , 0 )
649+ assert .NoError (t , err )
650+ err = pendingTransactionOrm .InsertPendingTransaction (context .Background (), "test" , senderMeta , tx1 , 0 )
651+ assert .NoError (t , err )
652+
653+ // Now the max nonce for this sender should be 3
654+ maxNonce , err = pendingTransactionOrm .GetMaxNonceBySenderAddress (context .Background (), senderMeta .Address .String ())
655+ assert .NoError (t , err )
656+ assert .Equal (t , uint64 (3 ), maxNonce )
657+ }
0 commit comments