stones/scripts/utils/resolve_ens_for_all_contact...

90 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Resolve ENS for All Contacts
This script resolves ENS names for all contacts in the database and
updates their profiles with additional information from ENS.
Usage:
python resolve_ens_for_all_contacts.py
"""
import os
import sys
import time
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.ens_resolver import ENSResolver
from utils.logger import setup_logger
from web3 import Web3
# Load environment variables
load_dotenv()
# Setup logging
logger = setup_logger("resolve_ens_for_all_contacts")
def resolve_ens_for_all_contacts():
"""
Resolve ENS names for all contacts in the database and update their profiles.
"""
logger.info("Resolving ENS names for all contacts")
db = DatabaseConnector()
# Initialize Web3 and ENS resolver
alchemy_api_key = os.getenv("ALCHEMY_API_KEY")
if not alchemy_api_key:
logger.error("ALCHEMY_API_KEY not found in environment variables")
sys.exit(1)
web3 = Web3(Web3.HTTPProvider(f"https://eth-mainnet.g.alchemy.com/v2/{alchemy_api_key}"))
ens_resolver = ENSResolver(web3)
# Get all contacts with Ethereum addresses
query = """
SELECT id, "ethereumAddress"
FROM "Contact"
WHERE "ethereumAddress" IS NOT NULL
"""
contacts = db.execute_query(query)
logger.info(f"Found {len(contacts)} contacts with Ethereum addresses")
# Resolve ENS names for contacts
contacts_updated = 0
ens_names_found = 0
for contact in contacts:
contact_id = contact["id"]
ethereum_address = contact["ethereumAddress"]
try:
# Try to resolve ENS name
ens_name = ens_resolver.get_ens_name(ethereum_address)
if ens_name:
logger.info(f"Found ENS name {ens_name} for address {ethereum_address}")
ens_names_found += 1
# Update contact with ENS name
db.update_contact(contact_id, {"ensName": ens_name})
# Get ENS profile and update contact
ens_resolver.update_contact_from_ens(contact_id, ens_name)
contacts_updated += 1
# Rate limiting to avoid API throttling
time.sleep(0.2)
except Exception as e:
logger.error(f"Error resolving ENS for {ethereum_address}: {str(e)}")
logger.info(f"Found {ens_names_found} ENS names")
logger.info(f"Updated {contacts_updated} contact profiles")
logger.info("ENS resolution completed")
if __name__ == "__main__":
resolve_ens_for_all_contacts()