|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use alloy::primitives::B256; |
1 | 5 | use axum::{ |
2 | 6 | http::StatusCode, |
3 | 7 | response::IntoResponse, |
4 | 8 | routing::{get, post}, |
5 | 9 | Router, |
6 | 10 | }; |
| 11 | +use indexer_common::prelude::{ |
| 12 | + Allocation, AllocationStatus, AttestationSigner, SubgraphDeployment, |
| 13 | +}; |
| 14 | +use thegraph_core::{address, DeploymentId}; |
7 | 15 | use tokio::net::TcpListener; |
| 16 | +use tracing::info; |
8 | 17 |
|
9 | | -use crate::subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE}; |
| 18 | +use crate::{ |
| 19 | + keys::{get_indexer_address_from_toml, get_mnemonic_from_toml, Signer}, |
| 20 | + subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE}, |
| 21 | +}; |
10 | 22 |
|
11 | 23 | const HOST: &str = "0.0.0.0"; |
12 | 24 | const PORT: &str = "8000"; |
| 25 | +pub const CREATED_AT_BLOCK_HASH: &str = |
| 26 | + "0x0000000000000000000000000000000000000000000000000000000000000000"; |
13 | 27 |
|
14 | 28 | pub async fn start_server() -> anyhow::Result<()> { |
| 29 | + let signer = Config::signer()?; |
| 30 | + info!("Starting server on {HOST}:{PORT}"); |
15 | 31 | let port = dotenvy::var("API_PORT").unwrap_or(PORT.into()); |
16 | 32 | let listener = TcpListener::bind(&format!("{HOST}:{port}")).await?; |
17 | 33 |
|
18 | | - let router = Router::new() |
19 | | - .route("/health", get(health_check)) |
20 | | - .route(NETWORK_SUBGRAPH_ROUTE, post(network_subgraph)); |
| 34 | + let router = Router::new().route("/health", get(health_check)).route( |
| 35 | + NETWORK_SUBGRAPH_ROUTE, |
| 36 | + post(move || network_subgraph(signer)), |
| 37 | + ); |
21 | 38 |
|
22 | 39 | Ok(axum::serve(listener, router).await?) |
23 | 40 | } |
24 | 41 |
|
25 | 42 | async fn health_check() -> impl IntoResponse { |
26 | 43 | StatusCode::OK |
27 | 44 | } |
| 45 | + |
| 46 | +struct Config; |
| 47 | + |
| 48 | +impl Config { |
| 49 | + fn signer() -> anyhow::Result<Signer> { |
| 50 | + let mnemonic = get_mnemonic_from_toml("indexer", "operator_mnemonic")?; |
| 51 | + |
| 52 | + let id = address!("5BcFE6215cbeB2D75cc3b09e01243cd7Ac55B3a7"); |
| 53 | + |
| 54 | + let subgraph_deployment_id: [u8; 32] = |
| 55 | + "QmUhiH6Z5xo6o3GNzsSvqpGKLmCt6w5A".as_bytes().try_into()?; |
| 56 | + |
| 57 | + let subgraph_deployment = SubgraphDeployment { |
| 58 | + id: DeploymentId::new(B256::from(subgraph_deployment_id)), |
| 59 | + denied_at: None, |
| 60 | + }; |
| 61 | + |
| 62 | + let allocation = Allocation { |
| 63 | + id, |
| 64 | + status: AllocationStatus::Active, |
| 65 | + subgraph_deployment, |
| 66 | + indexer: get_indexer_address_from_toml("indexer", "indexer_address")?, |
| 67 | + allocated_tokens: Default::default(), |
| 68 | + created_at_epoch: 1, |
| 69 | + created_at_block_hash: CREATED_AT_BLOCK_HASH.into(), |
| 70 | + closed_at_epoch: None, |
| 71 | + closed_at_epoch_start_block_hash: None, |
| 72 | + previous_epoch_start_block_hash: None, |
| 73 | + poi: None, |
| 74 | + query_fee_rebates: None, |
| 75 | + query_fees_collected: None, |
| 76 | + }; |
| 77 | + |
| 78 | + let dispute_address = address!("33f9E93266ce0E108fc85DdE2f71dab555A0F05a"); |
| 79 | + |
| 80 | + let signer = Signer::new( |
| 81 | + AttestationSigner::new(&mnemonic, &allocation, 42161, dispute_address)?, |
| 82 | + allocation, |
| 83 | + ); |
| 84 | + |
| 85 | + Ok(signer) |
| 86 | + } |
| 87 | +} |
0 commit comments