Skip to content

Commit b3dc070

Browse files
fix(transaction): clean up transaction_test.go
1 parent b9b021f commit b3dc070

1 file changed

Lines changed: 36 additions & 135 deletions

File tree

pkg/transaction/transaction_test.go

Lines changed: 36 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,39 @@ func signerMockForTransaction(t *testing.T, signedTx *types.Transaction, sender
7575
)
7676
}
7777

78+
func checkStoredTransaction(t *testing.T, transactionService transaction.Service, txHash common.Hash, request *transaction.TxRequest, recipient common.Address, gasLimit uint64, gasPrice *big.Int, nonce uint64) {
79+
t.Helper()
80+
81+
storedTransaction, err := transactionService.StoredTransaction(txHash)
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
if storedTransaction.To == nil || *storedTransaction.To != recipient {
87+
t.Fatalf("got wrong recipient in stored transaction. wanted %x, got %x", recipient, storedTransaction.To)
88+
}
89+
90+
if !bytes.Equal(storedTransaction.Data, request.Data) {
91+
t.Fatalf("got wrong data in stored transaction. wanted %x, got %x", request.Data, storedTransaction.Data)
92+
}
93+
94+
if storedTransaction.Description != request.Description {
95+
t.Fatalf("got wrong description in stored transaction. wanted %x, got %x", request.Description, storedTransaction.Description)
96+
}
97+
98+
if storedTransaction.GasLimit != gasLimit {
99+
t.Fatalf("got wrong gas limit in stored transaction. wanted %d, got %d", gasLimit, storedTransaction.GasLimit)
100+
}
101+
102+
if gasPrice.Cmp(storedTransaction.GasPrice) != 0 {
103+
t.Fatalf("got wrong gas price in stored transaction. wanted %d, got %d", gasPrice, storedTransaction.GasPrice)
104+
}
105+
106+
if storedTransaction.Nonce != nonce {
107+
t.Fatalf("got wrong nonce in stored transaction. wanted %d, got %d", nonce, storedTransaction.Nonce)
108+
}
109+
}
110+
78111
func TestTransactionSend(t *testing.T) {
79112
t.Parallel()
80113

@@ -130,12 +163,6 @@ func TestTransactionSend(t *testing.T) {
130163
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
131164
return nonce - 1, nil
132165
}),
133-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
134-
return suggestedGasTip, nil
135-
}),
136-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
137-
return &types.Header{BaseFee: baseFee}, nil
138-
}),
139166
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
140167
return gasFeeCap, suggestedGasTip, nil
141168
}),
@@ -163,34 +190,7 @@ func TestTransactionSend(t *testing.T) {
163190
t.Fatal("returning wrong transaction hash")
164191
}
165192

166-
storedTransaction, err := transactionService.StoredTransaction(txHash)
167-
if err != nil {
168-
t.Fatal(err)
169-
}
170-
171-
if storedTransaction.To == nil || *storedTransaction.To != recipient {
172-
t.Fatalf("got wrong recipient in stored transaction. wanted %x, got %x", recipient, storedTransaction.To)
173-
}
174-
175-
if !bytes.Equal(storedTransaction.Data, request.Data) {
176-
t.Fatalf("got wrong data in stored transaction. wanted %x, got %x", request.Data, storedTransaction.Data)
177-
}
178-
179-
if storedTransaction.Description != request.Description {
180-
t.Fatalf("got wrong description in stored transaction. wanted %x, got %x", request.Description, storedTransaction.Description)
181-
}
182-
183-
if storedTransaction.GasLimit != gasLimit {
184-
t.Fatalf("got wrong gas limit in stored transaction. wanted %d, got %d", gasLimit, storedTransaction.GasLimit)
185-
}
186-
187-
if gasFeeCap.Cmp(storedTransaction.GasPrice) != 0 {
188-
t.Fatalf("got wrong gas price in stored transaction. wanted %d, got %d", gasFeeCap, storedTransaction.GasPrice)
189-
}
190-
191-
if storedTransaction.Nonce != nonce {
192-
t.Fatalf("got wrong nonce in stored transaction. wanted %d, got %d", nonce, storedTransaction.Nonce)
193-
}
193+
checkStoredTransaction(t, transactionService, txHash, request, recipient, gasLimit, gasFeeCap, nonce)
194194

195195
pending, err := transactionService.PendingTransactions()
196196
if err != nil {
@@ -240,12 +240,6 @@ func TestTransactionSend(t *testing.T) {
240240
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
241241
return nonce - 1, nil
242242
}),
243-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
244-
return suggestedGasTip, nil
245-
}),
246-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
247-
return &types.Header{BaseFee: baseFee}, nil
248-
}),
249243
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
250244
return gasFeeCap, suggestedGasTip, nil
251245
}),
@@ -273,34 +267,7 @@ func TestTransactionSend(t *testing.T) {
273267
t.Fatal("returning wrong transaction hash")
274268
}
275269

276-
storedTransaction, err := transactionService.StoredTransaction(txHash)
277-
if err != nil {
278-
t.Fatal(err)
279-
}
280-
281-
if storedTransaction.To == nil || *storedTransaction.To != recipient {
282-
t.Fatalf("got wrong recipient in stored transaction. wanted %x, got %x", recipient, storedTransaction.To)
283-
}
284-
285-
if !bytes.Equal(storedTransaction.Data, request.Data) {
286-
t.Fatalf("got wrong data in stored transaction. wanted %x, got %x", request.Data, storedTransaction.Data)
287-
}
288-
289-
if storedTransaction.Description != request.Description {
290-
t.Fatalf("got wrong description in stored transaction. wanted %x, got %x", request.Description, storedTransaction.Description)
291-
}
292-
293-
if storedTransaction.GasLimit != gasLimit {
294-
t.Fatalf("got wrong gas limit in stored transaction. wanted %d, got %d", gasLimit, storedTransaction.GasLimit)
295-
}
296-
297-
if gasFeeCap.Cmp(storedTransaction.GasPrice) != 0 {
298-
t.Fatalf("got wrong gas price in stored transaction. wanted %d, got %d", gasFeeCap, storedTransaction.GasPrice)
299-
}
300-
301-
if storedTransaction.Nonce != nonce {
302-
t.Fatalf("got wrong nonce in stored transaction. wanted %d, got %d", nonce, storedTransaction.Nonce)
303-
}
270+
checkStoredTransaction(t, transactionService, txHash, request, recipient, gasLimit, gasFeeCap, nonce)
304271

305272
pending, err := transactionService.PendingTransactions()
306273
if err != nil {
@@ -359,12 +326,6 @@ func TestTransactionSend(t *testing.T) {
359326
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
360327
return nonce - 1, nil
361328
}),
362-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
363-
return suggestedGasTip, nil
364-
}),
365-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
366-
return &types.Header{BaseFee: baseFee}, nil
367-
}),
368329
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
369330
return gasFeeCapWithBoost, suggestedGasTip, nil
370331
}),
@@ -392,34 +353,7 @@ func TestTransactionSend(t *testing.T) {
392353
t.Fatal("returning wrong transaction hash")
393354
}
394355

395-
storedTransaction, err := transactionService.StoredTransaction(txHash)
396-
if err != nil {
397-
t.Fatal(err)
398-
}
399-
400-
if storedTransaction.To == nil || *storedTransaction.To != recipient {
401-
t.Fatalf("got wrong recipient in stored transaction. wanted %x, got %x", recipient, storedTransaction.To)
402-
}
403-
404-
if !bytes.Equal(storedTransaction.Data, request.Data) {
405-
t.Fatalf("got wrong data in stored transaction. wanted %x, got %x", request.Data, storedTransaction.Data)
406-
}
407-
408-
if storedTransaction.Description != request.Description {
409-
t.Fatalf("got wrong description in stored transaction. wanted %x, got %x", request.Description, storedTransaction.Description)
410-
}
411-
412-
if storedTransaction.GasLimit != gasLimit {
413-
t.Fatalf("got wrong gas limit in stored transaction. wanted %d, got %d", gasLimit, storedTransaction.GasLimit)
414-
}
415-
416-
if gasFeeCapWithBoost.Cmp(storedTransaction.GasPrice) != 0 {
417-
t.Fatalf("got wrong gas price in stored transaction. wanted %d, got %d", gasFeeCapWithBoost, storedTransaction.GasPrice)
418-
}
419-
420-
if storedTransaction.Nonce != nonce {
421-
t.Fatalf("got wrong nonce in stored transaction. wanted %d, got %d", nonce, storedTransaction.Nonce)
422-
}
356+
checkStoredTransaction(t, transactionService, txHash, request, recipient, gasLimit, gasFeeCapWithBoost, nonce)
423357

424358
pending, err := transactionService.PendingTransactions()
425359
if err != nil {
@@ -474,12 +408,6 @@ func TestTransactionSend(t *testing.T) {
474408
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
475409
return nonce, nil
476410
}),
477-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
478-
return suggestedGasTip, nil
479-
}),
480-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
481-
return &types.Header{BaseFee: baseFee}, nil
482-
}),
483411
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
484412
return gasFeeCap, suggestedGasTip, nil
485413
}),
@@ -545,12 +473,6 @@ func TestTransactionSend(t *testing.T) {
545473
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
546474
return nextNonce, nil
547475
}),
548-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
549-
return suggestedGasTip, nil
550-
}),
551-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
552-
return &types.Header{BaseFee: baseFee}, nil
553-
}),
554476
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
555477
return gasFeeCap, suggestedGasTip, nil
556478
}),
@@ -617,12 +539,6 @@ func TestTransactionSend(t *testing.T) {
617539
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
618540
return nextNonce, nil
619541
}),
620-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
621-
return suggestedGasTip, nil
622-
}),
623-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
624-
return &types.Header{BaseFee: baseFee}, nil
625-
}),
626542
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
627543
return customGasFeeCap, customGasFeeCap, nil
628544
}),
@@ -766,12 +682,6 @@ func TestTransactionResend(t *testing.T) {
766682
}
767683
return nil
768684
}),
769-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
770-
return gasTip, nil
771-
}),
772-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
773-
return &types.Header{BaseFee: baseFee}, nil
774-
}),
775685
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
776686
return gasFeeCap, gasTip, nil
777687
}),
@@ -859,12 +769,6 @@ func TestTransactionCancel(t *testing.T) {
859769
}
860770
return nil
861771
}),
862-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
863-
return gasTip, nil
864-
}),
865-
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
866-
return &types.Header{BaseFee: baseFee}, nil
867-
}),
868772
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
869773
return fee, minimumTip, nil
870774
}),
@@ -915,9 +819,6 @@ func TestTransactionCancel(t *testing.T) {
915819
}
916820
return nil
917821
}),
918-
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
919-
return gasTip, nil
920-
}),
921822
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
922823
return gasFee, gasTip, nil
923824
}),

0 commit comments

Comments
 (0)