#!/usr/bin/env python3 """ Update Raid Guild Member Names This script updates the names of Raid Guild members that were previously imported with the generic name "Raid Guild Member". It sets better names based on ENS names or addresses. Usage: python update_raid_guild_names.py """ import os import sys import logging from typing import Dict, Any, List, Optional from dotenv import load_dotenv # Add parent directory to path to import utils sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from utils.db_connector import DatabaseConnector from utils.logger import setup_logger # Load environment variables load_dotenv() # Setup logging logger = setup_logger("raid_guild_name_updater") class RaidGuildNameUpdater: """Updater for Raid Guild member names""" def __init__(self): """Initialize the updater""" # Initialize database self.db = DatabaseConnector() def generate_name_for_contact(self, address: str, ens_name: Optional[str] = None) -> str: """Generate a better name for the contact based on ENS or address""" if ens_name: # Use ENS name without .eth suffix as the name if ens_name.endswith('.eth'): return ens_name[:-4] # Remove .eth suffix return ens_name # Use shortened address if no ENS name return f"RG_{address[:6]}...{address[-4:]}" def get_raid_guild_members_with_generic_names(self) -> List[Dict[str, Any]]: """Get all Raid Guild members with generic names""" query = """ SELECT c.id, c."ethereumAddress", c."ensName", c.name FROM "Contact" c JOIN "DaoMembership" dm ON c.id = dm."contactId" WHERE dm."daoName" = 'Raid Guild' AND c.name = 'Raid Guild Member' """ return self.db.execute_query(query) def update_member_name(self, contact_id: str, ethereum_address: str, ens_name: Optional[str] = None) -> bool: """ Update the name of a member. Args: contact_id: ID of the contact ethereum_address: Ethereum address of the member ens_name: ENS name of the member, if any Returns: True if the name was updated, False otherwise """ # Generate a better name name = self.generate_name_for_contact(ethereum_address, ens_name) # Update the contact query = """ UPDATE "Contact" SET name = %(name)s, "updatedAt" = NOW() WHERE id = %(contact_id)s """ rows_updated = self.db.execute_update(query, { "contact_id": contact_id, "name": name }) if rows_updated > 0: logger.info(f"Updated name for contact {contact_id} to '{name}'") return True else: logger.warning(f"Failed to update name for contact {contact_id}") return False def run(self): """Run the updater""" logger.info("Starting Raid Guild member name update") # Get all Raid Guild members with generic names members = self.get_raid_guild_members_with_generic_names() logger.info(f"Found {len(members)} Raid Guild members with generic names") # Update names updated_count = 0 for member in members: if self.update_member_name( member["id"], member["ethereumAddress"], member.get("ensName") ): updated_count += 1 logger.info(f"Updated names for {updated_count} out of {len(members)} members") return updated_count def main(): """Main function""" try: updater = RaidGuildNameUpdater() updated_count = updater.run() logger.info(f"Name update completed successfully. Updated {updated_count} members.") return 0 except Exception as e: logger.exception(f"Error updating names: {e}") return 1 if __name__ == "__main__": sys.exit(main())