TX pending处理😧
代码
通过发送相同的 nonce,gas 比之前的更高
import { createPublicClient, createWalletClient, http, parseEther } from 'viem';
import { sepolia } from 'viem/chains';
import { privateKeyToAccount, mnemonicToAccount } from 'viem/accounts';
// 创建客户端
const publicClient = createPublicClient({
chain: sepolia,
transport: http(),
});
const walletClient = createWalletClient({
account: mnemonicToAccount('你的助记词'),
chain: sepolia,
transport: http(),
});
async function cancelTransaction() {
const account = '你的账号';
// 获取待 处理交易的Nonce值
const nonce = await publicClient.getTransactionCount({ address: account, blockTag: 'pending' });
// 创建一笔新的交易
const tx = {
from: account,
to: account, // 发送给自己
value: parseEther('0.01'), // 发送0 ETH
gas: 21000,
gasPrice: parseEther('0.0000001'), // 设置较高的Gas Price,单位是ETH
nonce: nonce,
};
// 发送交易
const hash = await walletClient.sendTransaction(tx as any);
console.log('Transaction hash:', hash);
}
cancelTransaction().catch(console.error);
取消交易之后
