import { ethers } from 'ethers';
import fs from 'fs';

const PRIVATE_KEY = '0x886ea4cffe76c386fecf3ff321ac9ae913737c46c17bc6ce2413752144668a2a';
const RPC_URL = 'https://evm.kava.io';

// dUSDT Contract
const CONTRACT_ADDRESS = '0xA9F3A35dBa8699c8C681D8db03F0c1A8CEB9D7c3';
const ABI = JSON.parse(fs.readFileSync('build/DurianUSDT.abi', 'utf8'));

// Transfer destination
const TO_ADDRESS = '0xdd919d51ec09c0830fd000a0ff6f0e1859b48db2';

async function transferAll() {
    // Connect to Kava mainnet
    const provider = new ethers.JsonRpcProvider(RPC_URL);
    const wallet = new ethers.Wallet(PRIVATE_KEY, provider);

    console.log('From address:', wallet.address);
    console.log('To address:', TO_ADDRESS);

    // Connect to contract
    const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, wallet);

    // Get current balance
    const balance = await contract.balanceOf(wallet.address);
    console.log('Current balance:', ethers.formatUnits(balance, 6), 'dUSDT');

    if (balance === 0n) {
        console.log('No balance to transfer');
        return;
    }

    // Transfer all tokens
    console.log('\nTransferring all tokens...');
    const tx = await contract.transfer(TO_ADDRESS, balance);
    console.log('Transaction hash:', tx.hash);

    // Wait for confirmation
    console.log('Waiting for confirmation...');
    const receipt = await tx.wait();
    console.log('Confirmed in block:', receipt.blockNumber);
    console.log('Gas used:', receipt.gasUsed.toString());

    // Verify balances
    console.log('\n=== Transfer Complete ===');
    const newFromBalance = await contract.balanceOf(wallet.address);
    const newToBalance = await contract.balanceOf(TO_ADDRESS);
    console.log('From balance:', ethers.formatUnits(newFromBalance, 6), 'dUSDT');
    console.log('To balance:', ethers.formatUnits(newToBalance, 6), 'dUSDT');

    console.log('\nExplorer link:');
    console.log(`https://kavascan.com/tx/${tx.hash}`);
}

transferAll().catch(console.error);
