Skip to content

Commit 4f343ec

Browse files
test: use waitForReceipt helper for reliable tx confirmation
Replace time.Sleep(2s) with waitForReceipt that polls up to 30 seconds. All tests now explicitly assert receipt status is 0x1 (success). Refactored formatReceipt to accept receipt map instead of fetching. Amp-Thread-ID: https://ampcode.com/threads/T-019c0a6d-ce6b-74cd-83ac-70d77d77ea1e Co-authored-by: Amp <amp@ampcode.com>
1 parent 4bb9176 commit 4f343ec

File tree

1 file changed

+80
-69
lines changed

1 file changed

+80
-69
lines changed

tests/integration_test.go

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,29 @@ func init() {
7575
}
7676
}
7777

78-
// formatReceipt formats a transaction receipt for human-readable output (similar to cast)
79-
func formatReceipt(t *testing.T, txHash string, rpcClient *client.Client) {
78+
// waitForReceipt waits for a transaction receipt with retries and returns it
79+
func waitForReceipt(t *testing.T, rpcClient *client.Client, txHash string) map[string]interface{} {
8080
t.Helper()
8181
ctx := context.Background()
8282

83-
resp, err := rpcClient.SendRequest(ctx, "eth_getTransactionReceipt", txHash)
84-
if err != nil {
85-
t.Logf("Failed to get receipt: %v", err)
86-
return
83+
for i := 0; i < 15; i++ {
84+
time.Sleep(2 * time.Second)
85+
resp, err := rpcClient.SendRequest(ctx, "eth_getTransactionReceipt", txHash)
86+
if err == nil && resp.Result != nil {
87+
if receipt, ok := resp.Result.(map[string]interface{}); ok {
88+
return receipt
89+
}
90+
}
8791
}
92+
t.Logf("Warning: Receipt not available after 30 seconds for tx %s", txHash)
93+
return nil
94+
}
8895

89-
receipt, ok := resp.Result.(map[string]interface{})
90-
if !ok {
91-
t.Logf("Receipt not available yet")
96+
// formatReceipt formats a transaction receipt for human-readable output (similar to cast)
97+
func formatReceipt(t *testing.T, receipt map[string]interface{}) {
98+
t.Helper()
99+
if receipt == nil {
100+
t.Logf("Receipt not available")
92101
return
93102
}
94103

@@ -293,8 +302,11 @@ func TestIntegration_SimpleTransaction(t *testing.T) {
293302
require.NoError(t, err)
294303
t.Logf("Transaction hash: %s", txHash)
295304

296-
time.Sleep(2 * time.Second)
297-
formatReceipt(t, txHash, rpcClient)
305+
receipt := waitForReceipt(t, rpcClient, txHash)
306+
require.NotNil(t, receipt, "Failed to get receipt")
307+
status, _ := receipt["status"].(string)
308+
require.Equal(t, "0x1", status, "Transaction failed")
309+
formatReceipt(t, receipt)
298310
}
299311

300312
// TestIntegration_FeeTokenLiquidity tests adding fee token liquidity
@@ -347,8 +359,11 @@ func TestIntegration_FeeTokenLiquidity(t *testing.T) {
347359
require.NoError(t, err)
348360
t.Logf("%s liquidity tx hash: %s", ft.name, txHash)
349361

350-
time.Sleep(2 * time.Second)
351-
formatReceipt(t, txHash, rpcClient)
362+
receipt := waitForReceipt(t, rpcClient, txHash)
363+
require.NotNil(t, receipt, "Failed to get receipt")
364+
status, _ := receipt["status"].(string)
365+
require.Equal(t, "0x1", status, "Transaction failed")
366+
formatReceipt(t, receipt)
352367
})
353368
}
354369
}
@@ -393,8 +408,11 @@ func TestIntegration_SendWithFeeToken(t *testing.T) {
393408
require.NoError(t, err)
394409
t.Logf("Sent with %s fee token, tx hash: %s", ft.name, txHash)
395410

396-
time.Sleep(2 * time.Second)
397-
formatReceipt(t, txHash, rpcClient)
411+
receipt := waitForReceipt(t, rpcClient, txHash)
412+
require.NotNil(t, receipt, "Failed to get receipt")
413+
status, _ := receipt["status"].(string)
414+
require.Equal(t, "0x1", status, "Transaction failed")
415+
formatReceipt(t, receipt)
398416
})
399417
}
400418
}
@@ -432,8 +450,11 @@ func TestIntegration_2DNonces(t *testing.T) {
432450
require.NoError(t, err)
433451
t.Logf("2D nonce (key=%d) tx hash: %s", key, txHash)
434452

435-
time.Sleep(2 * time.Second)
436-
formatReceipt(t, txHash, rpcClient)
453+
receipt := waitForReceipt(t, rpcClient, txHash)
454+
require.NotNil(t, receipt, "Failed to get receipt")
455+
status, _ := receipt["status"].(string)
456+
require.Equal(t, "0x1", status, "Transaction failed")
457+
formatReceipt(t, receipt)
437458
})
438459
}
439460
}
@@ -470,8 +491,11 @@ func TestIntegration_ExpiringNonces(t *testing.T) {
470491
require.NoError(t, err)
471492
t.Logf("Expiring nonce (validBefore=%d) tx hash: %s", validBefore, txHash)
472493

473-
time.Sleep(2 * time.Second)
474-
formatReceipt(t, txHash, rpcClient)
494+
receipt := waitForReceipt(t, rpcClient, txHash)
495+
require.NotNil(t, receipt, "Failed to get receipt")
496+
status, _ := receipt["status"].(string)
497+
require.Equal(t, "0x1", status, "Transaction failed")
498+
formatReceipt(t, receipt)
475499
})
476500

477501
t.Run("ValidAfterAndBefore", func(t *testing.T) {
@@ -501,8 +525,11 @@ func TestIntegration_ExpiringNonces(t *testing.T) {
501525
require.NoError(t, err)
502526
t.Logf("Expiring nonce (validAfter=%d, validBefore=%d) tx hash: %s", validAfter, validBefore, txHash)
503527

504-
time.Sleep(2 * time.Second)
505-
formatReceipt(t, txHash, rpcClient)
528+
receipt := waitForReceipt(t, rpcClient, txHash)
529+
require.NotNil(t, receipt, "Failed to get receipt")
530+
status, _ := receipt["status"].(string)
531+
require.Equal(t, "0x1", status, "Transaction failed")
532+
formatReceipt(t, receipt)
506533
})
507534
}
508535

@@ -551,27 +578,11 @@ func TestIntegration_SponsoredTransaction(t *testing.T) {
551578
require.NoError(t, err)
552579
t.Logf("Sponsored transaction hash: %s", txHash)
553580

554-
// Wait for receipt with retries (devnet can be slow)
555-
var receipt map[string]interface{}
556-
for i := 0; i < 15; i++ {
557-
time.Sleep(2 * time.Second)
558-
resp, err := rpcClient.SendRequest(ctx, "eth_getTransactionReceipt", txHash)
559-
if err == nil && resp.Result != nil {
560-
if r, ok := resp.Result.(map[string]interface{}); ok {
561-
receipt = r
562-
t.Logf("Got receipt after %d attempts", i+1)
563-
break
564-
}
565-
}
566-
t.Logf("Waiting for receipt... attempt %d", i+1)
567-
}
568-
569-
if receipt == nil {
570-
t.Log("Receipt not available - tx may be pending. Skipping fee payer verification.")
571-
t.Skip("Receipt not available after 30 seconds - devnet may be slow")
572-
}
573-
574-
formatReceipt(t, txHash, rpcClient)
581+
receipt := waitForReceipt(t, rpcClient, txHash)
582+
require.NotNil(t, receipt, "Failed to get receipt")
583+
status, _ := receipt["status"].(string)
584+
require.Equal(t, "0x1", status, "Sponsored transaction failed")
585+
formatReceipt(t, receipt)
575586

576587
// Verify the fee payer in receipt matches sponsor
577588
feePayer, _ := receipt["feePayer"].(string)
@@ -639,27 +650,12 @@ func TestIntegration_AccessKeys(t *testing.T) {
639650
require.NoError(t, err)
640651
t.Logf("Authorize access key tx hash: %s", txHash)
641652

642-
// Wait for receipt to confirm authorization succeeded
643-
var receipt map[string]interface{}
644-
for i := 0; i < 15; i++ {
645-
time.Sleep(2 * time.Second)
646-
resp, err := rpcClient.SendRequest(ctx, "eth_getTransactionReceipt", txHash)
647-
if err == nil && resp.Result != nil {
648-
if r, ok := resp.Result.(map[string]interface{}); ok {
649-
receipt = r
650-
t.Logf("Got receipt after %d attempts", i+1)
651-
break
652-
}
653-
}
654-
t.Logf("Waiting for authorization receipt... attempt %d", i+1)
655-
}
653+
receipt := waitForReceipt(t, rpcClient, txHash)
656654
require.NotNil(t, receipt, "Failed to get authorization receipt")
657-
658-
// Check if authorization succeeded
659655
status, _ := receipt["status"].(string)
660656
require.Equal(t, "0x1", status, "Authorization tx failed")
661657
t.Logf("Authorization tx succeeded")
662-
formatReceipt(t, txHash, rpcClient)
658+
formatReceipt(t, receipt)
663659
})
664660

665661
// Access key doesn't need funding - it signs on behalf of root account
@@ -693,8 +689,11 @@ func TestIntegration_AccessKeys(t *testing.T) {
693689
require.NoError(t, err)
694690
t.Logf("Access key signed tx hash: %s", txHash)
695691

696-
time.Sleep(2 * time.Second)
697-
formatReceipt(t, txHash, rpcClient)
692+
receipt := waitForReceipt(t, rpcClient, txHash)
693+
require.NotNil(t, receipt, "Failed to get receipt")
694+
status, _ := receipt["status"].(string)
695+
require.Equal(t, "0x1", status, "Access key transaction failed")
696+
formatReceipt(t, receipt)
698697
})
699698
}
700699

@@ -729,8 +728,11 @@ func TestIntegration_BatchTransactions(t *testing.T) {
729728
require.NoError(t, err)
730729
t.Logf("Batch (2 calls) tx hash: %s", txHash)
731730

732-
time.Sleep(2 * time.Second)
733-
formatReceipt(t, txHash, rpcClient)
731+
receipt := waitForReceipt(t, rpcClient, txHash)
732+
require.NotNil(t, receipt, "Failed to get receipt")
733+
status, _ := receipt["status"].(string)
734+
require.Equal(t, "0x1", status, "Batch transaction failed")
735+
formatReceipt(t, receipt)
734736
})
735737

736738
t.Run("ThreeCalls", func(t *testing.T) {
@@ -757,8 +759,11 @@ func TestIntegration_BatchTransactions(t *testing.T) {
757759
require.NoError(t, err)
758760
t.Logf("Batch (3 calls) tx hash: %s", txHash)
759761

760-
time.Sleep(2 * time.Second)
761-
formatReceipt(t, txHash, rpcClient)
762+
receipt := waitForReceipt(t, rpcClient, txHash)
763+
require.NotNil(t, receipt, "Failed to get receipt")
764+
status, _ := receipt["status"].(string)
765+
require.Equal(t, "0x1", status, "Batch transaction failed")
766+
formatReceipt(t, receipt)
762767
})
763768
}
764769

@@ -794,8 +799,11 @@ func TestIntegration_SetUserFeeToken(t *testing.T) {
794799
require.NoError(t, err)
795800
t.Logf("Set user fee token to BetaUSD tx hash: %s", txHash)
796801

797-
time.Sleep(2 * time.Second)
798-
formatReceipt(t, txHash, rpcClient)
802+
receipt := waitForReceipt(t, rpcClient, txHash)
803+
require.NotNil(t, receipt, "Failed to get receipt")
804+
status, _ := receipt["status"].(string)
805+
require.Equal(t, "0x1", status, "SetUserFeeToken failed")
806+
formatReceipt(t, receipt)
799807
})
800808

801809
t.Run("ResetToNative", func(t *testing.T) {
@@ -822,8 +830,11 @@ func TestIntegration_SetUserFeeToken(t *testing.T) {
822830
require.NoError(t, err)
823831
t.Logf("Reset user fee token to native tx hash: %s", txHash)
824832

825-
time.Sleep(2 * time.Second)
826-
formatReceipt(t, txHash, rpcClient)
833+
receipt := waitForReceipt(t, rpcClient, txHash)
834+
require.NotNil(t, receipt, "Failed to get receipt")
835+
status, _ := receipt["status"].(string)
836+
require.Equal(t, "0x1", status, "ResetUserFeeToken failed")
837+
formatReceipt(t, receipt)
827838
})
828839
}
829840

0 commit comments

Comments
 (0)