Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Solver Directory Overview

The solver directory contains the implementation of the Intent Solver, a TypeScript application designed to listen to blockchain events and process intents accordingly. This application plays a crucial role in handling events from different sources and executing the necessary actions based on those events.

Table of Contents

Directory Structure

solver/
├── index.ts
├── logger.ts
├── NonceKeeperWallet.ts
├── patch-bigint-buffer-warn.js
├── test/
├── config/
│  ├── index.ts
│  ├── allowBlockLists.ts
│  ├── chainMetadata.ts       # Picks a profile from config/chains/
│  ├── chains/
│  ├── types.ts
│  └── tradingPairs/
│      ├── handler.ts
│      ├── pairs.ts            # Pair type + reversible expansion
│      ├── pairs.json          # Working config (gitignored)
│      ├── pairs.testnet.json  # Template per environment (committed)
│      └── pairs.mainnet.json
└── solvers/
    ├── index.ts
    ├── BaseFiller.ts
    ├── BaseListener.ts
    ├── types.ts
    ├── utils.ts
    ├── contracts/
    └── <eco|hyperlane7683|router>/
        ├── index.ts
        ├── listener.ts
        ├── prepare.ts       # Optional: pre-fill strategy (e.g., auction logic)
        ├── auction.ts       # Auction manager (commit-reveal logic)
        ├── filler.ts
        ├── types.ts
        ├── utils.ts
        ├── contracts/
        ├── rules/
        │   └── index.ts
        └── config/
            ├── index.ts
            ├── metadata.ts
            └── allowBlockLists.ts

Description of Key Files and Directories

  • solver/index.ts: The main entry point of the solver application. It initializes and starts the listeners and fillers for different solvers.
  • logger.ts: Contains the Logger class used for logging messages with various formats and levels.
  • NonceKeeperWallet.ts: A class that extends ethers Wallet and prevents nonces race conditions when the solver needs to fill different intents (from different solutions) in the same network.
  • patch-bigint-buffer-warn.js: A script to suppress specific warnings related to BigInt and Buffer, ensuring cleaner console output.
  • config/: Global configuration for the solver.
    • tradingPairs/: Trading pairs configuration.
      • handler.ts: Loads pairs from JSON, resolves oracle/URL exchange rates.
      • pairs.ts: Trading pair type, expands reversible pairs into both directions.
      • pairs.json: Working pairs config (gitignored, created via yarn pairs:init).
      • pairs.testnet.json / pairs.mainnet.json: Templates, one per deployment.
  • scripts/solver-escrow/: CLI for solver collateral — balance.ts, deposit.ts, withdraw.ts, plus common.ts (provider/wallet, escrow lookup via router.SOLVER_ESCROW(), token decimals & symbol).
  • solvers/: Contains implementations of different solvers and common utilities.
    • BaseListener.ts: An abstract base class that provides common functionality for event listeners. It handles setting up contract connections and defines the interface for parsing event arguments.
    • BaseFiller.ts: An abstract base class that provides common functionality for fillers. It handles the solver's lifecycle prepareIntent, fill, and settle.
      • prepareIntent: evaluate allow/block lists, balances, and run the defined rules to decide whether to fill or not an intent.
      • fill: The actual filling.
      • settle: The settlement step, can be avoided.
    • <eco|hyperlane7683|router>/: Implements the solvers for different protocols.
      • listener.ts: Extends BaseListener to handle domain-specific events.
      • prepare.ts: (Optional) Implements pre-fill strategy. For Router, handles commit-reveal auction and order claiming.
      • auction.ts: Auction manager — commit/reveal phases, winner detection, auction restart handling.
      • filler.ts: Extends BaseFiller to handle domain-specific intents.
      • rules/: Custom validation rules for deciding whether to fill an intent.
      • contracts/: Contains contract ABI and type definitions for interacting with domain-specific contracts.
    • index.ts: Exports the solvers to be used in the main application.

Installation

Create a .env file before running (see Configuration).

Option 1: Local

Prerequisites: Node.js 20+, Yarn

yarn install
yarn build

Option 2: Docker

Prerequisites: Docker, Docker Compose

# Build and start
docker compose up -d --build

# Live PM2 logs (JSON)
docker compose exec solver pm2 logs

# Live PM2 logs (formatted)
tail -f ./logs/solver-out.log | npx pino-pretty

# PM2 process status
docker compose exec solver pm2 list

# Enter container
docker compose exec solver sh

# Graceful restart PM2 processes
docker compose exec solver pm2 reload all

# Stop
docker compose down

Configuration

Environment Variables

Create a .env file in the solver directory with the following required and optional variables:

# Required: Private key for solver wallet (used to sign transactions)
PRIVATE_KEY=0x...

# Required: Router contract address (same for all chains)
ROUTER_CONTRACT=0x3448f63B27161cEE72781319e6b579132d905d08

# Optional: Log level (debug, info, warn, error)
LOG_LEVEL=info

# Select testnet (Outbe/BSC Testnet/Sepolia) or mainnet (Outbe/Ethereum)
NETWORK=testnet

Important:

  • PRIVATE_KEY is required - the solver wallet must have sufficient funds on all chains where it will operate
  • ROUTER_CONTRACT address is used for all chains in the selected profile
  • LOG_LEVEL defaults to info if not specified

Chain Configuration

NETWORK (testnet by default) picks one of two profiles, each a plain literal you can read end to end:

  • config/chains/testnet.ts — Outbe Testnet, BSC Testnet, Sepolia
  • config/chains/mainnet.ts — Outbe Mainnet, Ethereum

config/chainMetadata.ts chooses between them. Testnet RPCs can be overridden at runtime with BSC_RPC_URLS, OUTBE_RPC_URLS, and SEPOLIA_RPC_URLS; each value may contain one URL or a comma-separated fallback list. Without an override, the profile's public RPC URLs are used.

Trading Pairs Configuration

Trading pairs live in config/tradingPairs/pairs.json (gitignored), created by yarn pairs:init from the template of the active network — pairs.testnet.json or pairs.mainnet.json. Manage them via CLI commands:

yarn pairs:init      # Create pairs.json from the environment's template
yarn pairs:list      # Show configured pairs
yarn pairs:add       # Add a pair interactively
yarn pairs:remove    # Remove a pair interactively
yarn pairs:oracle    # List available oracle pairs and current rates

Example pairs.json:

[
  {
    "originChain": "outbetestnet",
    "destinationChain": "sepolia",
    "inputToken": "0x0000000000000000000000000000000000000000",
    "outputToken": "0xe6AE7EBD5b5c34ed3E696e6Ced7f2A1660F20454",
    "rate": "0x0000000000000000000000000000000000000000/0x00000000000000000000000000000000000CC840",
    "quoteTolerance": 0.01,
    "reversible": true
  },
  {
    "originChain": "outbetestnet",
    "destinationChain": "sepolia",
    "inputToken": "0xe6AE7EBD5b5c34ed3E696e6Ced7f2A1660F20454",
    "outputToken": "0xe6AE7EBD5b5c34ed3E696e6Ced7f2A1660F20454",
    "rate": 1,
    "quoteTolerance": 0,
    "reversible": true
  }
]

Parameters:

  • rate: One of three formats:
    • Fixed: number, e.g. 1
    • Oracle: "<baseAddr>/<quoteAddr>", or "1/<baseAddr>/<quoteAddr>" for the inverse
    • URL: e.g. "https://my-api.com/rate" — must return JSON {"exchangeRate": "0.023271"}
  • quoteTolerance: Extra % added to output to increase winning chances (e.g., 0.01 = 1% more)
  • inputToken / outputToken: Token addresses. Use 0x0000000000000000000000000000000000000000 for native tokens (COEN, BNB).
  • reversible: Optional. true also trades the pair the other way round — chains and tokens swapped, rate inverted (4 → 0.25, "A/B" → "1/A/B"). One entry, two traded directions, nothing to keep in sync. Omitted or false = one-way only.

yarn pairs:list marks reversible pairs with ↔ and prints how many directions are actually traded.

Oracle rates are fetched on-chain at each order. Use yarn pairs:oracle to see available oracle pairs.

Intent Filtering

Configure which intents to fill or ignore using allow/block lists at config/allowBlockLists.ts:

const allowBlockLists: AllowBlockLists = {
  allowList: [],
  blockList: [],
};

Solver Collateral (SolverEscrow)

The solver has to post collateral before it can fill anything — the escrow address is read from router.SOLVER_ESCROW() on the chain you pass, so the router must be deployed there.

yarn escrow:balance  [chain] [token|native[,token2,...]]            # total / locked / available
yarn escrow:deposit  [chain] [token|native] [amount]                # approve + deposit
yarn escrow:withdraw [chain] [token|native] [amount|all]

Chain names come from chainMetadata.ts, amounts are human-readable (0.1, 100), everything acts on the PRIVATE_KEY address. Deposit sets the escrow as an ERC6909 operator on The Compact on first run and approves the ERC20 when needed; withdraw takes all.

Arguments are optional: run any command bare and it asks for chain, token and amount interactively (like pairs:add), offering the tokens from pairs.json for that chain.

yarn escrow:deposit bsctestnet 0xe6AE7EBD5b5c34ed3E696e6Ced7f2A1660F20454 100

The router in solvers/router/config/metadata.ts currently uses one address (ROUTER_CONTRACT) for every chain, and it is deployed on bsctestnet only — escrow commands on outbetestnet and sepolia fail on SOLVER_ESCROW() until the router is deployed there or given a per-chain address.

Usage

Running the Solver Application

Start the solver application:

yarn solver

This will run the compiled JavaScript code from the dist directory, initializing and starting all enabled solvers as defined in config/solvers.json. Each solver's status (enabled/disabled) can be configured in this JSON file.

Development Mode

Run in watch mode for development:

yarn dev

Router Auction Solver

The Router solver targets the shared Router settler contract, which abstracts the underlying cross-chain transport (Hyperlane / LayerZero) behind a single bridge(). It implements a commit-reveal Vickrey auction where multiple solvers compete by first committing a hash of their quote, then revealing the actual amount. The highest bidder wins but pays the second-highest price.

Architecture

The solver uses a four-module architecture:

  1. Listener (listener.ts): Detects Open events from Router contracts
  2. Prepare (prepare.ts): Orchestrates auction flow and claims winning orders on the router
  3. Auction (auction.ts): Manages commit-reveal phases, winner detection, and auction restart handling
  4. Filler (filler.ts): Executes fills when solver wins the auction

Auction Flow

Phase 1: Commit

When an order is detected, the solver:

  1. Calculates competitive output amount using trading pair config (exchange rate + quote tolerance)
  2. Runs pre-commit checks (escrow collateral, token balance)
  3. Generates a random salt and commits a hash: keccak256(abi.encode(orderId, outputAmount, salt))
  4. Waits for the commit deadline (a block number of the auction chain — phases count blocks, not seconds)

Phase 2: Reveal

After commit phase ends:

  1. Solver reveals the actual outputAmount, salt, and originData (encoded order data from fillInstructions)
  2. Contract verifies the reveal matches the committed hash
  3. Waits for auction to end (reveal deadline, also a block number)

Phase 3: Winner & Claim

After auction ends:

  1. Contract determines winner (Vickrey: highest bidder, second-highest price)
  2. Solver checks if it won via auction.winner()
  3. If winner, claims the order on the router contract via router.claimOrder()
  4. If claim triggers AuctionRestarted (winner disqualified for insufficient collateral), solver does not retry

Auction Restart

If the solver loses the auction, it waits up to 30 seconds for an AuctionRestarted event (emitted when the winner is disqualified). If restarted, the solver enters a new auction round automatically.

Key Features

  • Commit-reveal scheme: Prevents front-running — quotes are hidden until reveal phase
  • Vickrey pricing: Winner pays second-highest price, incentivizing truthful bidding
  • Escrow collateral check: Verifies solver has sufficient collateral before committing
  • Oracle exchange rates: Supports on-chain oracle rates (e.g., COEN/USDC) alongside fixed rates
  • Auction restart handling: Automatically re-enters auction if winner is disqualified
  • Precise phase timing: Uses on-chain deadlines for phase transitions to avoid missed reveals

Example Flow

User creates order: 100 COEN → wants minimum 1 USDC

TradingPair: exchangeRate = "COEN/USDC" (oracle), quoteTolerance = 0.01
Oracle returns: COEN/USDC = 0.023

1. Calculate output:
   - Base: 100 * 0.023 = 2.3 USDC
   - Boost: 2.3 * (1 + 0.01) = 2.323 USDC
   - Check: 2.323 >= 1 ✅

2. Commit phase:
   - Generate salt
   - Commit hash of (orderId, 2.323 USDC, salt)
   - Wait for commit deadline

3. Reveal phase:
   - Reveal 2.323 USDC + salt
   - Wait for auction end

4. Winner check:
   - If won → claim order on router → fill
   - If lost → wait 30s for possible restart

Logging

The application uses a custom Logger class. Default: stdout with INFO level.

Customize using pino transports. See pino transports docs. There's an example for logging to a Syslog server running on localhost commented in logger.ts.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages