@SatoshiMoto, сам не научишься — никто не научит.
Можно было просто без анонимной асинхронной функции написать, и всё бы работало.
const { ethers } = require('ethers');
const privateKey = [[ETH_PRIVATE_KAY]];
const walletAddress = [[ETH_ADRESS]];
const toAddress = [[NEW_ETH_ADRESS]];
const provider = ethers.getDefaultProvider('https://arbitrum.llamarpc.com');
const wallet = new ethers.Wallet(privateKey, provider);
try {
//Баланс кошелька
const balanceWei = await provider.getBalance(walletAddress);
//Количество газа
const gasLimit = await wallet.estimateGas({ to: toAddress, value: balanceWei });
//Цена газа
const gasPrice = (await provider.getFeeData()).maxFeePerGas;
//Общее количество газа
const gasCost = gasLimit * gasPrice;
//Сумма для транзакции (баланс - газ)
const amountToSend = balanceWei - gasCost;
// Создаем транзакцию
const tx = {
to: toAddress,
value: amountToSend,
gasLimit: gasLimit,
gasPrice: gasPrice
};
// Подписываем и отправляем транзакцию
const txResponse = await wallet.sendTransaction(tx);
console.log(`https://arbiscan.io/tx/${txResponse.hash}`);
} catch (error) {
console.log('Error sending transaction');
}