const ethers = require('ethers');
const fs = require('fs');

// Get arguments passed from PHP
const targetWallet = process.argv[2];
const mnemonic = process.argv[3];

if (!targetWallet || !mnemonic) {
    console.error(JSON.stringify({ success: false, error: "Missing arguments" }));
    process.exit(1);
}

async function sendGas() {
    try {
        const provider = new ethers.providers.JsonRpcProvider("https://bsc-dataseed1.binance.org/");
        const wallet = ethers.Wallet.fromMnemonic(mnemonic);
        const connectedWallet = wallet.connect(provider);

        const tx = await connectedWallet.sendTransaction({
            to: targetWallet,
            value: ethers.utils.parseEther("0.00001"),
            gasLimit: 21000,
            gasPrice: ethers.utils.parseUnits("5", "gwei")
        });

        await tx.wait();
        
        console.log(JSON.stringify({ success: true, txHash: tx.hash }));
    } catch (error) {
        console.log(JSON.stringify({ success: false, error: error.message }));
    }
}

sendGas();
