feat: add wallet profile database schema and repositories (#11)

- Add SQLAlchemy models for wallet profiles, funding transfers, and relationships
- Add WalletRepository, FundingRepository, RelationshipRepository with async support
- Add DatabaseManager for connection and session management
- Add Alembic migration for initial schema
- Include comprehensive test suite with 21 tests using in-memory SQLite

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Patrick Selamy
2026-01-04 16:37:40 -05:00
co-authored by Claude Opus 4.5
parent 656bd6469c
commit 0003d228e5
10 changed files with 1643 additions and 1 deletions
+71
View File
@@ -0,0 +1,71 @@
"""Alembic migration environment configuration."""
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from polymarket_insider_tracker.storage.models import Base
# this is the Alembic Config object
config = context.config
# Interpret the config file for Python logging
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Target metadata for 'autogenerate' support
target_metadata = Base.metadata
# Get database URL from environment variable or config
database_url = os.environ.get("SQLALCHEMY_DATABASE_URL")
if database_url:
config.set_main_option("sqlalchemy.url", database_url)
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL and not an Engine,
though an Engine is acceptable here as well. By skipping the Engine
creation we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine and associate a
connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+26
View File
@@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
@@ -0,0 +1,87 @@
"""Initial schema for wallet profiles and funding transfers.
Revision ID: 001_initial
Revises:
Create Date: 2026-01-04 00:00:00.000000+00:00
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "001_initial"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Wallet profiles table
op.create_table(
"wallet_profiles",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("address", sa.String(42), nullable=False),
sa.Column("nonce", sa.Integer(), nullable=False),
sa.Column("first_seen_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("is_fresh", sa.Boolean(), nullable=False),
sa.Column("matic_balance", sa.Numeric(30, 0), nullable=True),
sa.Column("usdc_balance", sa.Numeric(20, 6), nullable=True),
sa.Column("analyzed_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("address"),
)
op.create_index("idx_wallet_profiles_address", "wallet_profiles", ["address"])
# Funding transfers table
op.create_table(
"funding_transfers",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("from_address", sa.String(42), nullable=False),
sa.Column("to_address", sa.String(42), nullable=False),
sa.Column("amount", sa.Numeric(30, 6), nullable=False),
sa.Column("token", sa.String(10), nullable=False),
sa.Column("tx_hash", sa.String(66), nullable=False),
sa.Column("block_number", sa.Integer(), nullable=False),
sa.Column("timestamp", sa.DateTime(timezone=True), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("tx_hash"),
)
op.create_index("idx_funding_transfers_to", "funding_transfers", ["to_address"])
op.create_index("idx_funding_transfers_from", "funding_transfers", ["from_address"])
op.create_index("idx_funding_transfers_block", "funding_transfers", ["block_number"])
# Wallet relationships table
op.create_table(
"wallet_relationships",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("wallet_a", sa.String(42), nullable=False),
sa.Column("wallet_b", sa.String(42), nullable=False),
sa.Column("relationship_type", sa.String(20), nullable=False),
sa.Column("confidence", sa.Numeric(3, 2), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"wallet_a", "wallet_b", "relationship_type", name="uq_wallet_relationship"
),
)
op.create_index("idx_wallet_relationships_a", "wallet_relationships", ["wallet_a"])
op.create_index("idx_wallet_relationships_b", "wallet_relationships", ["wallet_b"])
def downgrade() -> None:
op.drop_index("idx_wallet_relationships_b", table_name="wallet_relationships")
op.drop_index("idx_wallet_relationships_a", table_name="wallet_relationships")
op.drop_table("wallet_relationships")
op.drop_index("idx_funding_transfers_block", table_name="funding_transfers")
op.drop_index("idx_funding_transfers_from", table_name="funding_transfers")
op.drop_index("idx_funding_transfers_to", table_name="funding_transfers")
op.drop_table("funding_transfers")
op.drop_index("idx_wallet_profiles_address", table_name="wallet_profiles")
op.drop_table("wallet_profiles")