-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_agent_wallet.py
More file actions
104 lines (84 loc) · 3.5 KB
/
add_agent_wallet.py
File metadata and controls
104 lines (84 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Migration script to add Base Layer 2 blockchain wallet addresses to existing AI Agents.
"""
import os
import sys
import json
import logging
from datetime import datetime
from sqlalchemy import text
from app import app, db
from models import AIAgent
from modules.crypto_wallet import CryptoWalletManager
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Use the database session from app
try:
session = db.session
logger.info("Connected to database through app context.")
except Exception as e:
logger.error(f"Error connecting to database: {e}")
sys.exit(1)
def add_wallet_columns():
"""Check if wallet columns exist and add them if they don't"""
try:
# For SQLite, query the pragma table info to check if column exists
result = session.execute(text(
"""
PRAGMA table_info(ai_agent)
"""
))
columns = [row[1] for row in result.fetchall()]
if 'wallet_address' not in columns:
logger.info("Adding wallet columns to AIAgent table...")
# Add the wallet columns one by one (SQLite limitation)
session.execute(text("ALTER TABLE ai_agent ADD COLUMN wallet_address VARCHAR(128)"))
session.execute(text("ALTER TABLE ai_agent ADD COLUMN wallet_network VARCHAR(20) DEFAULT 'mainnet'"))
session.execute(text("ALTER TABLE ai_agent ADD COLUMN wallet_created_date TIMESTAMP"))
session.commit()
logger.info("Wallet columns added successfully.")
else:
logger.info("Wallet columns already exist in AIAgent table.")
except Exception as e:
session.rollback()
logger.error(f"Error adding wallet columns: {e}")
sys.exit(1)
def update_existing_agents():
"""Create wallet addresses for existing AI Agents"""
try:
# Get all agents without wallet addresses
agents = session.query(AIAgent).filter(AIAgent.wallet_address.is_(None)).all()
if not agents:
logger.info("No agents found without wallet addresses.")
return
logger.info(f"Found {len(agents)} agents without wallet addresses.")
# Create wallet for each agent
for agent in agents:
try:
network = agent.wallet_network or 'mainnet'
wallet_address = CryptoWalletManager.create_agent_wallet(network=network)
agent.wallet_address = wallet_address
agent.wallet_created_date = datetime.utcnow()
logger.info(f"Created {network} wallet for agent {agent.id} ({agent.name}): {wallet_address}")
except Exception as e:
logger.warning(f"Error creating wallet for agent {agent.id}: {e}")
continue
session.commit()
logger.info("Wallet migration completed successfully.")
except Exception as e:
session.rollback()
logger.error(f"Error updating agents with wallets: {e}")
sys.exit(1)
def run_migration():
"""Run the database migration to add wallet functionality to AI Agents"""
try:
with app.app_context():
add_wallet_columns()
update_existing_agents()
logger.info("Migration completed successfully")
except Exception as e:
logger.error(f"Migration failed: {e}")
sys.exit(1)
if __name__ == "__main__":
run_migration()