Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/shared/src/price_estimation/native/coingecko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ impl CoinGecko {
Chain::Lens => "lens".to_string(),
Chain::Linea => "linea".to_string(),
Chain::Plasma => "plasma".to_string(),
Chain::Sepolia | Chain::Goerli | Chain::Hardhat => {
// Hardhat/Anvil is a local Ethereum fork, use ethereum pricing for offline development
Chain::Hardhat => "ethereum".to_string(),
Chain::Sepolia | Chain::Goerli => {
anyhow::bail!("unsupported network {}", chain.name())
}
};
Expand Down
30 changes: 26 additions & 4 deletions playground/docker-compose.offline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ services:
retries: 10
start_period: 2s

# Mock Coingecko API for price fetching in offline mode
coingecko-mock:
build:
context: ./mocks/coingecko
dockerfile: Dockerfile
restart: always
environment:
- PORT=3000
ports:
- 3001:3000
healthcheck:
test: ["CMD-SHELL", "wget -q --spider http://localhost:3000/health || exit 1"]
interval: 5s
timeout: 3s
retries: 5
start_period: 5s

db:
image: postgres:16
restart: always
Expand Down Expand Up @@ -87,15 +104,16 @@ services:
- NODE_URL=http://chain:8545
- DB_WRITE_URL=postgres://db:5432/?user=${POSTGRES_USER}&password=${POSTGRES_PASSWORD}
- DB_READ_URL=postgres://db:5432/?user=${POSTGRES_USER}&password=${POSTGRES_PASSWORD}
- LOG_FILTER=orderbook=trace,shared=trace
- LOG_FILTER=orderbook=trace,shared=trace,shared::price_estimation=trace,shared::price_estimation::native=trace
- ACCOUNT_BALANCES_SIMULATION=false
- ACCOUNT_BALANCES_SIMULATOR=Web3
- SIMULATION_NODE_URL=http://chain:8545
- EIP1271_SKIP_CREATION_VALIDATION=true
- ENABLE_EIP1271_ORDERS=true
- PRICE_ESTIMATORS=None
- PRICE_ESTIMATION_DRIVERS=baseline|http://driver/baseline
- NATIVE_PRICE_ESTIMATORS=baseline|http://driver/baseline
- NATIVE_PRICE_ESTIMATORS=baseline|http://driver/baseline,CoinGecko
- COIN_GECKO_URL=http://coingecko-mock:3000/api/v3/simple/token_price
- DRIVERS=baseline|http://driver/baseline
- BIND_ADDRESS=0.0.0.0:80
- BASELINE_SOURCES=UniswapV2
Expand All @@ -115,6 +133,8 @@ services:
condition: service_completed_successfully
chain:
condition: service_healthy
coingecko-mock:
condition: service_healthy
ports:
- 8080:80 # API
- 9586:9586 # metrics
Expand Down Expand Up @@ -153,14 +173,14 @@ services:
- SETTLE_INTERVAL=15s
- GAS_ESTIMATORS=Native,Web3
- PRICE_ESTIMATORS=None
- NATIVE_PRICE_ESTIMATORS=baseline|http://driver/baseline
- NATIVE_PRICE_ESTIMATORS=baseline|http://driver/baseline,CoinGecko
- COIN_GECKO_URL=http://coingecko-mock:3000/api/v3/simple/token_price
- BLOCK_STREAM_POLL_INTERVAL=1s
- NATIVE_PRICE_CACHE_MAX_UPDATE_SIZE=100
- NATIVE_PRICE_CACHE_MAX_AGE=20m
- SOLVER_TIME_LIMIT=5
- PRICE_ESTIMATION_DRIVERS=baseline|http://driver/baseline
- RUN_LOOP_NATIVE_PRICE_TIMEOUT=5s
- NATIVE_PRICE_ESTIMATORS=baseline|http://driver/baseline
- NATIVE_PRICE_ESTIMATION_RESULTS_REQUIRED=1
- DRIVERS=baseline|http://driver/baseline|0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
- BASELINE_SOURCES=UniswapV2
Expand All @@ -181,6 +201,8 @@ services:
condition: service_healthy
chain:
condition: service_healthy
coingecko-mock:
condition: service_healthy
ports:
- 9589:9589 # metrics
- 6670:6669 # tokio console
Expand Down
54 changes: 54 additions & 0 deletions playground/mocks/README.md
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Mock Services

This directory contains mock services used for offline development and testing of the CoW Protocol playground.

## Available Mocks

### Coingecko (`./coingecko/`)

Mock implementation of the Coingecko API for price fetching in offline mode.

- **Technology**: Node.js + TypeScript + Hono API
- **Endpoint**: `/api/v3/simple/token_price/ethereum`
- **Purpose**: Provides configurable token prices in ETH denomination without requiring external API access
- **Configuration**: Token prices can be configured in `./coingecko/src/tokens.config.ts`

**Supported Tokens** (offline mode):
- WETH
- DAI
- USDC
- USDT
- GNO

See `./coingecko/README.md` for detailed documentation.

## Architecture

Mock services are organized at the playground level to:
- Maintain clear separation from deployment infrastructure (`offline-mode/`)
- Improve discoverability of available mocks
- Enable easy addition of new mock services (e.g., block explorer, subgraph, etc.)
- Support reusability across different playground configurations

## Adding New Mocks

To add a new mock service:

1. Create a new directory: `mocks/<service-name>/`
2. Implement the mock service with a Dockerfile
3. Add the service to `docker-compose.offline.yml`
4. Update this README with documentation

## Usage

Mock services are automatically started when running the offline playground:

```bash
docker-compose -f docker-compose.offline.yml up
```

Individual mocks can be built and run separately:

```bash
docker-compose -f docker-compose.offline.yml up coingecko-mock
```
6 changes: 6 additions & 0 deletions playground/mocks/coingecko/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
dist
*.log
.env
.DS_Store
README.md
5 changes: 5 additions & 0 deletions playground/mocks/coingecko/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
*.log
.env
.DS_Store
22 changes: 22 additions & 0 deletions playground/mocks/coingecko/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:20-alpine

WORKDIR /app

# Copy package files
COPY package*.json ./
COPY tsconfig.json ./

# Install dependencies
RUN npm install

# Copy source code
COPY src ./src

# Build TypeScript
RUN npm run build

# Expose port
EXPOSE 3000

# Start the server
CMD ["npm", "start"]
118 changes: 118 additions & 0 deletions playground/mocks/coingecko/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Coingecko Mock API

Mock implementation of the Coingecko API for offline development and testing.

## Overview

This service provides a lightweight mock of the Coingecko API, specifically the `/api/v3/simple/token_price` endpoint. It returns configurable prices for the 5 tokens deployed in offline mode.

## Supported Tokens

The following tokens are supported (configured in `src/tokens.config.ts`):

| Symbol | Address | Price (ETH) |
|--------|---------|-------------|
| WETH | `0xb3af08c783c4d9c380893257980b5e26657f2317` | 1.0 |
| DAI | `0xb12812c0cad46d18b669b31059d485fe90b1a839` | 0.0004 |
| USDC | `0xb04afbcd351a0a7e4ff658b3772ee5f3f5b6e4ae` | 0.0004 |
| USDT | `0x171a30524fd943df1a12cbb9da291bf4e34ac84b` | 0.0004 |
| GNO | `0x51a53858a4a8b81814da35c4604eb9003d56a895` | 0.05 |

## API Endpoints

### Price Fetching

```bash
GET /api/v3/simple/token_price/ethereum?contract_addresses=<addresses>&vs_currencies=eth&precision=full
```

**Query Parameters:**
- `contract_addresses`: Comma-separated list of token contract addresses
- `vs_currencies`: Currency denomination (only `eth` is supported)
- `precision`: Precision level (e.g., `full`)

**Example Request:**
```bash
curl "http://localhost:3000/api/v3/simple/token_price/ethereum?contract_addresses=0xb3af08c783c4d9c380893257980b5e26657f2317,0xb12812c0cad46d18b669b31059d485fe90b1a839&vs_currencies=eth&precision=full"
```

**Example Response:**
```json
{
"0xb3af08c783c4d9c380893257980b5e26657f2317": {
"eth": 1.0
},
"0xb12812c0cad46d18b669b31059d485fe90b1a839": {
"eth": 0.0004
}
}
```

### Health Check

```bash
GET /health
```

Returns the service status.

### List Supported Tokens

```bash
GET /api/v3/tokens
```

Returns a list of all supported token addresses.

## Local Development

### Prerequisites

- Node.js 20+
- npm or yarn

### Installation

```bash
npm install
```

### Running

```bash
# Development mode with hot reload
npm run dev

# Production build
npm run build
npm start
```

### Environment Variables

- `PORT`: Server port (default: 3000)

## Docker

### Build

```bash
docker build -t coingecko-mock .
```

### Run

```bash
docker run -p 3000:3000 coingecko-mock
```

## Configuration

To add or modify token prices, edit `src/tokens.config.ts` and update the `TOKENS` object.

## Limitations

- Only supports Ethereum platform (`ethereum`)
- Only supports ETH denomination (`vs_currencies=eth`)
- Only returns prices for tokens defined in `tokens.config.ts`
- Does not support historical prices, market caps, or other Coingecko features
23 changes: 23 additions & 0 deletions playground/mocks/coingecko/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "coingecko-mock",
"version": "1.0.0",
"description": "Mock Coingecko API for offline development",
"main": "dist/index.js",
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc",
"start": "node dist/index.js"
},
"keywords": ["coingecko", "mock", "api"],
"author": "",
"license": "MIT",
"dependencies": {
"hono": "^4.0.0",
"@hono/node-server": "^1.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"tsx": "^4.0.0",
"typescript": "^5.0.0"
}
}
Loading