Skip to content
Merged
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
124 changes: 123 additions & 1 deletion API_ENDPOINTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,136 @@ GET /pectra_data_gas?batcher_address=0x5050F69a9786F081509234F1a7F4684b5E5b76C9&
}
```

## Aggregated L2 Analytics Endpoints (All Batchers)

The following endpoints return data for all batchers at once, eliminating the need to make separate API calls for each batcher address.

### 1. All Daily Transactions

**Endpoint:** `GET /all_daily_txs`

**Parameters:**
- `start_timestamp` (i64) - Start timestamp (Unix timestamp)
- `end_timestamp` (i64) - End timestamp (Unix timestamp)

**Example:**
```
GET /all_daily_txs?start_timestamp=1640995200&end_timestamp=1641081600
```

**Response:**
```json
{
"batchers": [
{
"batcher_address": "0x5050F69a9786F081509234F1a7F4684b5E5b76C9",
"tx_count": 42
},
{
"batcher_address": "0x6887246668a3b87F54DeB3b94Ba47a6f63F32985",
"tx_count": 38
}
]
}
```

### 2. All ETH Saved

**Endpoint:** `GET /all_eth_saved`

**Parameters:**
- `start_timestamp` (i64) - Start timestamp (Unix timestamp)
- `end_timestamp` (i64) - End timestamp (Unix timestamp)

**Example:**
```
GET /all_eth_saved?start_timestamp=1640995200&end_timestamp=1641081600
```

**Response:**
```json
{
"batchers": [
{
"batcher_address": "0x5050F69a9786F081509234F1a7F4684b5E5b76C9",
"total_eth_saved_wei": "1234567890123456789"
},
{
"batcher_address": "0x6887246668a3b87F54DeB3b94Ba47a6f63F32985",
"total_eth_saved_wei": "987654321098765432"
}
]
}
```

### 3. All Blob Data Gas

**Endpoint:** `GET /all_blob_data_gas`

**Parameters:**
- `start_timestamp` (i64) - Start timestamp (Unix timestamp)
- `end_timestamp` (i64) - End timestamp (Unix timestamp)

**Example:**
```
GET /all_blob_data_gas?start_timestamp=1640995200&end_timestamp=1641081600
```

**Response:**
```json
{
"batchers": [
{
"batcher_address": "0x5050F69a9786F081509234F1a7F4684b5E5b76C9",
"total_blob_data_gas": 1234567890
},
{
"batcher_address": "0x6887246668a3b87F54DeB3b94Ba47a6f63F32985",
"total_blob_data_gas": 876543210
}
]
}
```

### 4. All Pectra Data Gas (EIP-7623)

**Endpoint:** `GET /all_pectra_data_gas`

**Parameters:**
- `start_timestamp` (i64) - Start timestamp (Unix timestamp)
- `end_timestamp` (i64) - End timestamp (Unix timestamp)

**Example:**
```
GET /all_pectra_data_gas?start_timestamp=1640995200&end_timestamp=1641081600
```

**Response:**
```json
{
"batchers": [
{
"batcher_address": "0x5050F69a9786F081509234F1a7F4684b5E5b76C9",
"total_pectra_data_gas": 9876543210
},
{
"batcher_address": "0x6887246668a3b87F54DeB3b94Ba47a6f63F32985",
"total_pectra_data_gas": 5432109876
}
]
}
```

## Technical Notes

- All timestamps are in Unix timestamp format (seconds since January 1, 1970)
- Gas values are in gas units
- ETH values are in wei (1 ETH = 10^18 wei)
- The endpoints query the SQLite database created by the L2 monitoring service
- The database is automatically populated by the `run_l2_batches_monitoring_service`
- All analytics endpoints now require a `batcher_address` parameter to filter results for a specific batcher
- **Individual endpoints** require a `batcher_address` parameter to filter results for a specific batcher
- **Aggregated endpoints** (`/all_*`) return data for all batchers and only require timestamp parameters
- Aggregated endpoints are more efficient when you need data for multiple batchers at once

## Monitored Batcher Addresses

Expand Down
24 changes: 18 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use pectralizer::{
server::{
AppState,
handlers::{
blob_data_gas_handler, contract_handler, daily_txs_handler, eth_saved_handler,
pectra_data_gas_handler, root_handler, tx_handler,
all_blob_data_gas_handler, all_daily_txs_handler, all_eth_saved_handler,
all_pectra_data_gas_handler, blob_data_gas_handler, contract_handler,
daily_txs_handler, eth_saved_handler, pectra_data_gas_handler, root_handler,
tx_handler,
},
},
tracker::{
Expand Down Expand Up @@ -110,6 +112,10 @@ async fn main() -> eyre::Result<()> {
.route("/eth_saved", get(eth_saved_handler))
.route("/blob_data_gas", get(blob_data_gas_handler))
.route("/pectra_data_gas", get(pectra_data_gas_handler))
.route("/all_daily_txs", get(all_daily_txs_handler))
.route("/all_eth_saved", get(all_eth_saved_handler))
.route("/all_blob_data_gas", get(all_blob_data_gas_handler))
.route("/all_pectra_data_gas", get(all_pectra_data_gas_handler))
.layer(CorsLayer::permissive())
.with_state(app_state.clone());

Expand All @@ -120,13 +126,19 @@ async fn main() -> eyre::Result<()> {
info!("📡 Ethereum provider configured");
info!("🌐 Server listening on http://0.0.0.0:{}", port);
info!("📝 Available endpoints:");
info!(" Individual Batcher Endpoints:");
info!(" - GET / - Welcome message");
info!(" - GET /tx - Transaction analysis");
info!(" - GET /contract - Contract analysis");
info!(" - GET /daily_txs - Daily transactions analysis");
info!(" - GET /eth_saved - Ethereum saved analysis");
info!(" - GET /blob_data_gas - Blob data gas analysis");
info!(" - GET /pectra_data_gas - Pectra data gas analysis");
info!(" - GET /daily_txs - Daily transactions analysis (specific batcher)");
info!(" - GET /eth_saved - Ethereum saved analysis (specific batcher)");
info!(" - GET /blob_data_gas - Blob data gas analysis (specific batcher)");
info!(" - GET /pectra_data_gas - Pectra data gas analysis (specific batcher)");
info!(" Aggregated Endpoints (All Batchers):");
info!(" - GET /all_daily_txs - Daily transactions for all batchers");
info!(" - GET /all_eth_saved - ETH saved data for all batchers");
info!(" - GET /all_blob_data_gas - Blob data gas for all batchers");
info!(" - GET /all_pectra_data_gas - Pectra data gas for all batchers");

// run both services concurrently
tokio::select! {
Expand Down
71 changes: 68 additions & 3 deletions src/server/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::{
error::HandlerError,
types::{
BlobDataGasResponse, ContractAnalysisResponse, ContractQuery, DailyTxsQuery,
DailyTxsResponse, EthSavedQuery, EthSavedResponse, GasUsageQuery, PectraDataGasResponse,
TxAnalysisResponse, TxHashQuery,
AggregatedQuery, AllBlobDataGasResponse, AllDailyTxsResponse, AllEthSavedResponse,
AllPectraDataGasResponse, BlobDataGasResponse, ContractAnalysisResponse, ContractQuery,
DailyTxsQuery, DailyTxsResponse, EthSavedQuery, EthSavedResponse, GasUsageQuery,
PectraDataGasResponse, TxAnalysisResponse, TxHashQuery,
},
};
use crate::{
Expand Down Expand Up @@ -298,3 +299,67 @@ pub async fn pectra_data_gas_handler(
total_pectra_data_gas,
}))
}

/// Handler for aggregated daily transactions endpoint (all batchers)
pub async fn all_daily_txs_handler(
State(app_state): State<super::AppState>,
Query(query): Query<AggregatedQuery>,
) -> Result<Json<AllDailyTxsResponse>, HandlerError> {
let batchers = app_state
.db
.get_all_daily_transactions(query.start_timestamp, query.end_timestamp)
.await
.map_err(|e| {
HandlerError::DatabaseError(format!("Failed to get all daily transactions: {}", e))
})?;

Ok(Json(AllDailyTxsResponse { batchers }))
}

/// Handler for aggregated ETH saved endpoint (all batchers)
pub async fn all_eth_saved_handler(
State(app_state): State<super::AppState>,
Query(query): Query<AggregatedQuery>,
) -> Result<Json<AllEthSavedResponse>, HandlerError> {
let batchers = app_state
.db
.get_all_eth_saved_data(query.start_timestamp, query.end_timestamp)
.await
.map_err(|e| {
HandlerError::DatabaseError(format!("Failed to get all ETH saved data: {}", e))
})?;

Ok(Json(AllEthSavedResponse { batchers }))
}

/// Handler for aggregated blob data gas endpoint (all batchers)
pub async fn all_blob_data_gas_handler(
State(app_state): State<super::AppState>,
Query(query): Query<AggregatedQuery>,
) -> Result<Json<AllBlobDataGasResponse>, HandlerError> {
let batchers = app_state
.db
.get_all_total_blob_data_gas(query.start_timestamp, query.end_timestamp)
.await
.map_err(|e| {
HandlerError::DatabaseError(format!("Failed to get all blob data gas: {}", e))
})?;

Ok(Json(AllBlobDataGasResponse { batchers }))
}

/// Handler for aggregated Pectra data gas endpoint (all batchers)
pub async fn all_pectra_data_gas_handler(
State(app_state): State<super::AppState>,
Query(query): Query<AggregatedQuery>,
) -> Result<Json<AllPectraDataGasResponse>, HandlerError> {
let batchers = app_state
.db
.get_all_total_pectra_data_gas(query.start_timestamp, query.end_timestamp)
.await
.map_err(|e| {
HandlerError::DatabaseError(format!("Failed to get all Pectra data gas: {}", e))
})?;

Ok(Json(AllPectraDataGasResponse { batchers }))
}
73 changes: 73 additions & 0 deletions src/server/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,76 @@ pub struct PectraDataGasResponse {
/// Total Pectra (EIP-7623) calldata gas used.
pub total_pectra_data_gas: u64,
}

/// Query parameters for aggregated endpoints (all batchers).
#[derive(Deserialize, Debug)]
pub struct AggregatedQuery {
/// Timestamp start (Unix timestamp).
pub start_timestamp: i64,
/// Timestamp end (Unix timestamp).
pub end_timestamp: i64,
}

/// Individual batcher data for daily transactions.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct BatcherDailyTxs {
/// The batcher address.
pub batcher_address: String,
/// The number of transactions.
pub tx_count: u64,
}

/// Response structure for aggregated daily transactions endpoint.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct AllDailyTxsResponse {
/// List of batcher transaction data.
pub batchers: Vec<BatcherDailyTxs>,
}

/// Individual batcher data for ETH saved.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct BatcherEthSaved {
/// The batcher address.
pub batcher_address: String,
/// Total ETH saved in wei.
pub total_eth_saved_wei: u128,
}

/// Response structure for aggregated ETH saved endpoint.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct AllEthSavedResponse {
/// List of batcher ETH saved data.
pub batchers: Vec<BatcherEthSaved>,
}

/// Individual batcher data for blob data gas.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct BatcherBlobDataGas {
/// The batcher address.
pub batcher_address: String,
/// Total blob data gas used.
pub total_blob_data_gas: u64,
}

/// Response structure for aggregated blob data gas endpoint.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct AllBlobDataGasResponse {
/// List of batcher blob data gas.
pub batchers: Vec<BatcherBlobDataGas>,
}

/// Individual batcher data for Pectra data gas.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct BatcherPectraDataGas {
/// The batcher address.
pub batcher_address: String,
/// Total Pectra (EIP-7623) calldata gas used.
pub total_pectra_data_gas: u64,
}

/// Response structure for aggregated Pectra data gas endpoint.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct AllPectraDataGasResponse {
/// List of batcher Pectra data gas.
pub batchers: Vec<BatcherPectraDataGas>,
}
Loading