import { ethers } from 'ethers'; import { getContractAddress } from 'gotake-contracts'; import { getNetworkByName } from '../src/utils/networks'; import * as dotenv from 'dotenv'; // Load environment variables dotenv.config(); // Get network from environment const NETWORK = process.env.NETWORK || 'Base Sepolia'; const PRIVATE_KEY = process.env.PRIVATE_KEY; if (!PRIVATE_KEY) { console.error('❌ PRIVATE_KEY environment variable not set'); process.exit(1); } async function addSelfAsAdmin() { console.log('🔧 Adding Self as Admin'); console.log('======================='); console.log(`Network: ${NETWORK}`); try { // Get network configuration const networkConfig = getNetworkByName(NETWORK); if (!networkConfig) { console.error(`❌ Unsupported network: ${NETWORK}`); process.exit(1); } // Setup provider and signer const provider = new ethers.providers.JsonRpcProvider(networkConfig.rpcUrl); const signer = new ethers.Wallet(PRIVATE_KEY!, provider); const myAddress = await signer.getAddress(); console.log(`🔗 RPC: ${networkConfig.rpcUrl}`); console.log(`👤 My Address: ${myAddress}`); // Get contract address const networkKey = networkConfig.name === 'Base Sepolia' ? 'base_sepolia' : 'base'; const contractAddress = getContractAddress(networkKey as string, 'videoPayment'); console.log(`📍 VideoPayment Contract: ${contractAddress}`); // Contract ABI const abi = [ 'function owner() view returns (address)', 'function addAdmin(address admin) external', 'function admins(address) view returns (bool)' ]; const contract = new ethers.Contract(contractAddress!, abi, signer); // 1. Check if we are the owner console.log('\n1️⃣ Checking owner status...'); const owner = await contract.owner(); console.log(`Contract Owner: ${owner}`); const isOwner = owner.toLowerCase() === myAddress.toLowerCase(); console.log(`Am I Owner: ${isOwner ? '✅ Yes' : '❌ No'}`); if (!isOwner) { console.error('❌ You are not the owner. Cannot add admin.'); process.exit(1); } // 2. Check current admin status console.log('\n2️⃣ Checking current admin status...'); const isCurrentlyAdmin = await contract.admins(myAddress); console.log(`Currently Admin: ${isCurrentlyAdmin ? '✅ Yes' : '❌ No'}`); if (isCurrentlyAdmin) { console.log('✅ Already an admin! No action needed.'); return; } // 3. Add self as admin console.log('\n3️⃣ Adding self as admin...'); const tx = await contract.addAdmin(myAddress, { gasLimit: 100000 }); console.log(`📤 Transaction sent: ${tx.hash}`); console.log('⏳ Waiting for confirmation...'); const receipt = await tx.wait(); if (receipt.status === 1) { console.log('✅ Successfully added as admin!'); console.log(`✅ Block: ${receipt.blockNumber}`); console.log(`✅ Gas used: ${receipt.gasUsed?.toString()}`); // Verify admin status console.log('\n4️⃣ Verifying admin status...'); const isNowAdmin = await contract.admins(myAddress); console.log(`Admin Status: ${isNowAdmin ? '✅ Confirmed' : '❌ Failed'}`); } else { console.log('❌ Transaction failed'); process.exit(1); } } catch (error) { console.error('❌ Error:', (error as any).message); process.exit(1); } } addSelfAsAdmin();