As a newbie dev, I tried to write an airdrop script using AI but I'm failing to get it to work.
My goal is to be able to send an equal amount of Solana to multiple wallet addresses listed in a .txt file. Can anyone help troubleshoot on this? I keep getting TypeErrors.
const solanaWeb3 = require('@solana/web3.js')
// Replace with your own mnemonic and wallet address
const MNEMONIC = 'password password password password password password password password password password password password'
const WALLET_ADDRESS = 'walletaddresswouldgohere69696969'
// Replace with the path to the wallets file
const WALLETS_FILE_PATH = '/Users/Documents/Airdrops/wallets.txt'
// Replace with the amount of Solana to airdrop
const AIRDROP_AMOUNT = 0.0001
async function airdrop() {
// Initialize the wallet
const wallet = await solanaWeb3.wallet.fromMnemonic(MNEMONIC)
// Set the wallet address
wallet.setAddress(WALLET_ADDRESS)
// Initialize the result logs
const logs = {
cancelled: [],
failed: [],
successful: [],
unconfirmed: [],
}
// Read the wallets file
const wallets = fs.readFileSync(WALLETS_FILE_PATH, 'utf-8')
// Split the file content into an array of addresses
const recipientAddresses = wallets.split('\n')
// Iterate over the recipient addresses
for (const recipientAddress of recipientAddresses) {
// Airdrop the Solana
const result = await wallet.send(recipientAddress, AIRDROP_AMOUNT)
// Check the transaction status and add it to the appropriate log
if (result.isCancelled()) {
logs.cancelled.push(result)
} else if (result.isFailed()) {
logs.failed.push(result)
} else if (result.isSuccessful()) {
logs.successful.push(result)
} else {
logs.unconfirmed.push(result)
}
}
// Write the logs to separate .txt files
fs.writeFileSync('./cancelled.txt', logs.cancelled.join('\n'))
fs.writeFileSync('./failed.txt', logs.failed.join('\n'))
fs.writeFileSync('./successful.txt', logs.successful.join('\n'))
fs.writeFileSync('./unconfirmed.txt', logs.unconfirmed.join('\n'))
}
// Run the airdrop function
airdrop()