|
| 1 | +package mev_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/KyberNetwork/tradinglib/pkg/mev" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/core/types" |
| 13 | + "github.com/ethereum/go-ethereum/crypto" |
| 14 | + "github.com/ethereum/go-ethereum/ethclient" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestBaseChainSender_SendRawTransaction(t *testing.T) { |
| 19 | + t.Skip("Skip by default - uncomment to run actual test against Base mainnet") |
| 20 | + |
| 21 | + // Create HTTP client |
| 22 | + httpClient := &http.Client{Timeout: time.Second * 30} |
| 23 | + |
| 24 | + // Initialize BaseChainSender with Base mainnet RPC |
| 25 | + sender := mev.NewL2ChainSender( |
| 26 | + httpClient, |
| 27 | + "https://mainnet.base.org", |
| 28 | + mev.BundleSenderTypeL2, |
| 29 | + ) |
| 30 | + |
| 31 | + // Verify sender type |
| 32 | + require.Equal(t, mev.BundleSenderTypeL2, sender.GetSenderType()) |
| 33 | + |
| 34 | + // Create a test transaction (this is a dummy transaction that will likely fail) |
| 35 | + // In a real scenario, you would use proper private key, nonce, gas price, etc. |
| 36 | + privateKey, err := crypto.GenerateKey() |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + // Connect to Base mainnet to get current gas price and nonce |
| 40 | + ethClient, err := ethclient.Dial("https://mainnet.base.org") |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + fromAddress := crypto.PubkeyToAddress(privateKey.PublicKey) |
| 44 | + nonce, err := ethClient.PendingNonceAt(context.Background(), fromAddress) |
| 45 | + require.NoError(t, err) |
| 46 | + |
| 47 | + gasPrice, err := ethClient.SuggestGasPrice(context.Background()) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + // Create a simple ETH transfer transaction |
| 51 | + toAddress := common.HexToAddress("0x0000000000000000000000000000000000000001") // Burn address |
| 52 | + value := big.NewInt(1) // 1 wei |
| 53 | + gasLimit := uint64(21000) // Standard ETH transfer gas |
| 54 | + |
| 55 | + // Get chain ID for Base mainnet (8453) |
| 56 | + chainID, err := ethClient.ChainID(context.Background()) |
| 57 | + require.NoError(t, err) |
| 58 | + require.Equal(t, int64(8453), chainID.Int64()) // Base mainnet chain ID |
| 59 | + |
| 60 | + // Create transaction |
| 61 | + tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, nil) |
| 62 | + |
| 63 | + // Sign transaction |
| 64 | + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + // Send transaction using BaseChainSender |
| 68 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 69 | + defer cancel() |
| 70 | + |
| 71 | + resp, err := sender.SendRawTransaction(ctx, signedTx) |
| 72 | + |
| 73 | + // Log the response for debugging |
| 74 | + t.Logf("Response: %+v", resp) |
| 75 | + t.Logf("Error: %v", err) |
| 76 | + |
| 77 | + // The transaction will likely fail due to insufficient funds, but we should get a proper response |
| 78 | + // We expect either a successful response with transaction hash or a proper error response |
| 79 | + if err != nil { |
| 80 | + // Check if it's a proper RPC error (not a network/parsing error) |
| 81 | + t.Logf("Expected error due to insufficient funds or other RPC error: %v", err) |
| 82 | + } else { |
| 83 | + // If successful, verify response structure |
| 84 | + require.Equal(t, "2.0", resp.Jsonrpc) |
| 85 | + require.Equal(t, 1, resp.ID) |
| 86 | + require.NotEmpty(t, resp.Result) |
| 87 | + t.Logf("Transaction hash: %s", resp.Result) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestBaseChainSender_SendRawTransaction_InvalidTx(t *testing.T) { |
| 92 | + t.Skip("Skip by default - uncomment to run actual test against Base mainnet") |
| 93 | + // Create HTTP client |
| 94 | + httpClient := &http.Client{Timeout: time.Second * 10} |
| 95 | + |
| 96 | + // Initialize BaseChainSender with Base mainnet RPC |
| 97 | + sender := mev.NewL2ChainSender( |
| 98 | + httpClient, |
| 99 | + "https://mainnet.base.org", |
| 100 | + mev.BundleSenderTypeL2, |
| 101 | + ) |
| 102 | + |
| 103 | + // Create an invalid transaction (unsigned) |
| 104 | + toAddress := common.HexToAddress("0x0000000000000000000000000000000000000001") |
| 105 | + value := big.NewInt(1) |
| 106 | + gasLimit := uint64(21000) |
| 107 | + gasPrice := big.NewInt(1000000000) // 1 gwei |
| 108 | + |
| 109 | + // Create unsigned transaction |
| 110 | + tx := types.NewTransaction(0, toAddress, value, gasLimit, gasPrice, nil) |
| 111 | + |
| 112 | + // Try to send unsigned transaction (should fail) |
| 113 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 114 | + defer cancel() |
| 115 | + |
| 116 | + resp, err := sender.SendRawTransaction(ctx, tx) |
| 117 | + |
| 118 | + // Should get an error due to invalid transaction |
| 119 | + t.Logf("Response: %+v", resp) |
| 120 | + t.Logf("Error: %v", err) |
| 121 | + |
| 122 | + // We expect an error here |
| 123 | + require.Error(t, err) |
| 124 | +} |
| 125 | + |
| 126 | +func TestBaseChainSender_Interface_Compliance(t *testing.T) { |
| 127 | + t.Skip("Skip by default - uncomment to run actual test against Base mainnet") |
| 128 | + // Test that BaseChainSender implements ISendRawTransaction interface |
| 129 | + httpClient := &http.Client{Timeout: time.Second * 10} |
| 130 | + sender := mev.NewL2ChainSender( |
| 131 | + httpClient, |
| 132 | + "https://mainnet.base.org", |
| 133 | + mev.BundleSenderTypeL2, |
| 134 | + ) |
| 135 | + |
| 136 | + // Verify it implements the interface |
| 137 | + var _ mev.ISendRawTransaction = sender |
| 138 | +} |
| 139 | + |
| 140 | +func TestNewBaseChainSender(t *testing.T) { |
| 141 | + t.Skip("Skip by default - uncomment to run actual test against Base mainnet") |
| 142 | + httpClient := &http.Client{Timeout: time.Second * 10} |
| 143 | + endpoint := "https://mainnet.base.org" |
| 144 | + senderType := mev.BundleSenderTypeL2 |
| 145 | + |
| 146 | + sender := mev.NewL2ChainSender(httpClient, endpoint, senderType) |
| 147 | + |
| 148 | + require.NotNil(t, sender) |
| 149 | + require.Equal(t, senderType, sender.GetSenderType()) |
| 150 | +} |
0 commit comments