Skip to content

Commit b129715

Browse files
committed
fix issue not being able to send tx
1 parent 0cf51ec commit b129715

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func main() {
108108
var baseTimings []stats
109109

110110
chainId, err := baseClient.NetworkID(context.Background())
111+
log.Printf("Chain ID: %v", chainId)
111112
if err != nil {
112113
log.Fatalf("Failed to get network ID: %v", err)
113114
}
@@ -201,7 +202,8 @@ func writeToFile(filename string, data []stats) error {
201202
}
202203

203204
func timeTransaction(chainId *big.Int, privateKey *ecdsa.PrivateKey, fromAddress common.Address, toAddress common.Address, client *ethclient.Client, useSyncRPC bool, pollingIntervalMs int) (stats, error) {
204-
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
205+
// Use confirmed nonce to avoid conflicts with pending transactions
206+
nonce, err := client.NonceAt(context.Background(), fromAddress, nil)
205207
if err != nil {
206208
return stats{}, fmt.Errorf("unable to get nonce: %v", err)
207209
}
@@ -215,14 +217,21 @@ func timeTransaction(chainId *big.Int, privateKey *ecdsa.PrivateKey, fromAddress
215217

216218
tip, err := client.SuggestGasTipCap(context.Background())
217219
if err != nil {
218-
return stats{}, fmt.Errorf("unable to get gas price: %v", err)
220+
return stats{}, fmt.Errorf("unable to get gas tip cap: %v", err)
219221
}
220222

223+
// Add 20% buffer to tip to ensure replacement transactions are accepted
224+
tipWithBuffer := new(big.Int).Mul(tip, big.NewInt(120))
225+
tipWithBuffer.Div(tipWithBuffer, big.NewInt(100))
226+
227+
// Set GasFeeCap to baseFee + tip, with proper calculation and buffer
228+
gasFeeCapWithBuffer := new(big.Int).Mul(gasPrice, big.NewInt(2))
229+
221230
tx := types.NewTx(&types.DynamicFeeTx{
222231
ChainID: chainId,
223232
Nonce: nonce,
224-
GasTipCap: tip,
225-
GasFeeCap: gasPrice,
233+
GasTipCap: tipWithBuffer,
234+
GasFeeCap: gasFeeCapWithBuffer,
226235
Gas: gasLimit,
227236
To: &toAddress,
228237
Value: value,

0 commit comments

Comments
 (0)