Created
February 13, 2025 02:03
-
-
Save passandscore/aaef3cfafaa7ae1a6a61b725c5e0a84d to your computer and use it in GitHub Desktop.
Cancel Stuck Pending EVM Transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from "ethers"; | |
import * as fs from "fs"; | |
import * as path from "path"; | |
import * as dotenv from "dotenv"; | |
// Terminal Command: npx ts-node scripts/sendZeroValueToSelf.ts | |
// Used when a transaction fails to send | |
// Load environment variables from .env file | |
dotenv.config(); | |
async function main() { | |
// Verify environment variables | |
if (!process.env.ALCHEMY_KEY) { | |
throw new Error("ALCHEMY_KEY not found in environment variables"); | |
} | |
// Read private key from mainnet.secret | |
const privateKey = fs | |
.readFileSync(path.join(__dirname, "../mainnet.secret"), "utf8") | |
.trim(); | |
// Connect to Linea mainnet with explicit network configuration | |
const provider = new ethers.providers.JsonRpcProvider( | |
`https://linea-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_KEY}`, | |
{ | |
name: 'linea', | |
chainId: 59144 | |
} | |
); | |
// Verify connection | |
try { | |
await provider.getNetwork(); | |
console.log("Successfully connected to Linea network"); | |
} catch (error) { | |
console.error("Failed to connect to network:", error); | |
process.exit(1); | |
} | |
// Create wallet instance | |
const wallet = new ethers.Wallet(privateKey, provider); | |
console.log("Wallet address:", wallet.address); | |
// Create transaction | |
const tx = { | |
to: wallet.address, // Sending to self | |
value: 0, // Zero ETH | |
gasLimit: 21000, // Standard gas limit for ETH transfer | |
nonce: 161, | |
}; | |
try { | |
// Send transaction | |
const transaction = await wallet.sendTransaction(tx); | |
console.log("Transaction sent! Hash:", transaction.hash); | |
// Wait for transaction to be mined | |
const receipt = await transaction.wait(); | |
console.log("Transaction confirmed in block:", receipt.blockNumber); | |
} catch (error) { | |
console.error("Error sending transaction:", error); | |
} | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment