Skip to content

Create baseline solver#37

Open
jeffersonBastos wants to merge 41 commits intocowprotocol:mainfrom
bleu:refactor/create-baseline
Open

Create baseline solver#37
jeffersonBastos wants to merge 41 commits intocowprotocol:mainfrom
bleu:refactor/create-baseline

Conversation

@jeffersonBastos
Copy link

Overview

This PR introduces the baseline solver implementation in Python, ported from the Rust version, and refactors the template into a multi-engine architecture.
The goal is to provide Python developers with:

  • A ready-to-use baseline solver for comparison and testing,
  • A mysolver/solve endpoint as a clean starting point for new solver implementations,
  • A modernized, modular, and testable project structure.
  • Improved documentation with a Quick Start guide on integrating and extending this solver.

Key Changes

🆕 New Features

  • Baseline Solver Engine

    • Added BaselineEngine (/baseline/solve) implementing a contract-correct, minimal solver.
    • Includes core modules: matcher, pathfinding, pool management, solution builder, contract interactions.
  • Multi-Engine System

    • Introduced SolverEngine base class for pluggable engines.
    • Added mysolver/solve endpoint (stubbed for user implementations).
  • New Domain & Utility Modules

    • Domain: auction, order, solution, liquidity, common.
    • Utilities: AMM math, bytes/hex conversion, serialization.
  • Infrastructure Improvements

    • Pydantic v2 models with Rust-aligned DTOs.
    • Structured logging, Prometheus metrics, and health checks.
    • CLI entrypoints for local development.

🔄 Refactors & Migrations

  • Single App: Consolidated legacy server into one FastAPI app.
  • Routers: Modular API routing (/baseline, /mysolver).
  • Project Structure: Full reorganization to domain-driven architecture.
  • Removed Legacy Models & Utils: Old src/models and src/util replaced with domain + utils modules.

🗑️ Deletions

  • Removed outdated legacy models (batch_auction, order, token, etc.).
  • Removed old test files, replaced with comprehensive new suite.
  • Dropped unused Docker setup (development now CLI-based).

Motivation

  • Provide Python developers with the same onboarding experience Rust solvers already have.
  • Allow baseline vs custom solver races locally, without requiring Rust knowledge.
  • Improve maintainability, modularity, and testability for long-term growth.

Next Steps

  • Add more comprehensive validation tests with complete auctions, similar to Rust’s coverage.
  • Ensure parity validation with the Rust baseline solver.
  • Explore performance improvements.
  • We are open to feedback and suggestions — this is just the first version.

- Fix fundamental schema incompatibility with current CoW infrastructure
- Update models to use snake_case with Pydantic aliases for camelCase JSON
- Replace legacy BatchAuction with CowAuction matching solvers_dto
- Add proper field aliases (sellToken -> sell_token, etc.)
- Remove incompatible legacy models and utilities
- Update server endpoint to use new schema structure
- Organize template structure with examples and reference materials

Resolves schema validation errors (422) when receiving auctions from driver.
Template now works seamlessly with current CoW Protocol infrastructure.
- Add typer for CLI support
- Add prometheus-client for metrics
- Update version constraints for better compatibility
- Organize dependencies by category (core, dev, optional)
- Add multi-engine solver architecture with baseline and mysolver engines
- Implement FastAPI application with health, metrics, and solve endpoints
- Add comprehensive model definitions for auction, order, liquidity, and solution
- Include utility modules for serialization, math operations, and byte conversion
- Add CLI interface with typer for running the solver server
- Implement dependency injection and settings management
- Add comprehensive test suite for API, models, and multi-engine functionality
- Include logging and metrics infrastructure with Prometheus support
- Replace custom FastAPI implementation with python_baseline.api.app
- Remove duplicate endpoint definitions and middleware setup
- Simplify server configuration by delegating to main solver application
- Maintain backward compatibility for existing server startup process
- Add comprehensive documentation for new multi-engine architecture
- Include examples for baseline, mysolver, and default engine endpoints
- Update health check endpoint from /health to /healthz
- Add Prometheus metrics endpoint documentation
- Update driver configuration examples with new port (11088)
- Add autopilot configuration examples for different solver engines
- Maintain backward compatibility documentation for legacy server
- Move python_baseline/ to src/ with clean structure
- Separate API routers (baseline, mysolver)
- Move models to domain/
- Update all imports from python_baseline.* to src.*
- Remove legacy files and duplicates
- Improve project organization and maintainability
- Add Web3 and blockchain dependencies (web3, eth-typing, eth-utils, eth-abi, hexbytes)
- Add optimization libraries (networkx for pathfinding, scipy for math)
- Implement baseline solver engine with two-phase approach:
  * CoW (Coincidence of Wants) matching for direct order settlement
  * AMM routing through liquidity pools using NetworkX pathfinding
- Add comprehensive AMM math utilities for Uniswap V2, Balancer, and Curve pools
- Create solution builder for transaction construction and settlement
- Add partial fill optimization framework and gas estimation utilities
- Add comprehensive test coverage for baseline engine
- Remove obsolete test files and update domain models
- Fix pydantic field_validator imports and type annotations
jeffersonBastos and others added 10 commits September 29, 2025 10:57
- Simplify domain models by removing custom U256/Address types
- Migrate to Pydantic v2 field validators
- Add centralized fee conversion utilities
- Update solver configurations with Python and baseline endpoints
- Expand base token list with popular tokens (WETH, DAI, USDC, USDT, etc.)
- Add Uniswap V2 configuration support
- Integrate Web3 for robust Ethereum address validation
- Remove deprecated utility modules (hexbytes, u256)
- Update interaction encoding to use HexBytes
- Improve type safety with proper address validation
…ver-on-python-template

Add Python Baseline Solver & Multi-Engine Architecture
…ests

Update README and create QUICKSTART doc
@jeffersonBastos
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

…eline

- solution: Trade as fulfillment (kind, order, executedAmount), camelCase
  aliases and field_serializer for callData; Interaction/Allowance/Asset;
  JitTrade, LiquidityInteraction, Solution alias and populate_by_name
- api: return JSONResponse with model_dump(by_alias=True) for correct
  serialization; simplify notify handler
- engine: pass orders dict to PriceFinder; Trade creation with fulfillment
  format; matched_uids from trade.order
- cow_matcher: build Trade with kind/order/executed_amount
- price_finder: accept orders dict, resolve sell/buy token and amounts from
  Order for price graph; TYPE_CHECKING for Order
- path_finder: liquidity kind constantProduct/weightedProduct (lowercase)
- solution_builder: drop per-trade price backfill; validate order UID and
  executed_amount instead of sell/buy token prices
- domain: Order.fee_amount (optional); auction/solution model_config and
  aliases
- data: large_example.json numeric and trailing newline fixes
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