Skip to content

Conversation

mzkrasner
Copy link
Contributor

@mzkrasner mzkrasner commented Aug 27, 2025

Database Schema for Live Trading Support with Envio Integration

Summary

Added database schema changes to support live trading competitions where agents trade with real funds on-chain. This implementation uses Envio HyperIndex for efficient blockchain indexing, allowing us to track on-chain activity retroactively without direct RPC scanning.

Changes

1. Extended trades table

  • Added tradeType enum to distinguish between simulated and on_chain trades
  • Added fields for on-chain tracking: onChainTxHash, blockNumber, gasUsed, gasPrice, gasCostUsd, indexedAt
  • Added comprehensive indexes for efficient querying of on-chain trades

2. New selfFundingAlerts table

  • Tracks suspicious balance increases not matching indexed trades
  • Includes fields for manual review workflow
  • Properly indexed for monitoring queries

3. New liveCompetitionConfig table

  • Stores live trading competition settings
  • Uses PostgreSQL array for supportedChains with new liveTradingChains enum
  • Defaults to 5 EVM chains: eth, base, arbitrum, optimism, polygon
  • Includes thresholds for manual review processes

4. New indexerSyncProgress table

  • Tracks sync progress with Envio's GraphQL API
  • Simple design: one record per competition (not per agent/chain)
  • Uses timestamps instead of block numbers for querying
  • Much simpler than traditional block scanning approaches

Technical Notes

  • All tables use proper foreign keys with cascade delete
  • Comprehensive indexes added for expected query patterns
  • Uses Drizzle ORM following existing patterns in trading_comps schema
  • Designed to work with Envio HyperIndex for efficient multi-chain indexing
  • Supports retroactive detection of violations (self-funding, chain exits)

Architecture Overview

This schema supports a "super minimal" live trading implementation where:

  1. Envio continuously indexes all blockchain activity
  2. Our cron job queries Envio's GraphQL API for new trades
  3. Data is synced to our database for portfolio calculations
  4. Existing services (PortfolioSnapshotter, PriceTracker) work unchanged
  5. Manual review processes handle violations retroactively

The indexerSyncProgress table is key to this approach - it tracks the last synced timestamp per competition, allowing efficient incremental syncing without re-querying old data.

Copy link
Contributor

vercel bot commented Aug 27, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
comps Ready Ready Preview Comment Sep 3, 2025 9:35pm
faucet Ready Ready Preview Comment Sep 3, 2025 9:35pm
portal Ready Ready Preview Comment Sep 3, 2025 9:35pm
registration Ready Ready Preview Comment Sep 3, 2025 9:35pm

@mzkrasner mzkrasner changed the title feat: kick off live trading integration with schema updates feat: integrate live trading (main PR) Aug 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant