Skip to content

Commit

Permalink
wip: update progress on allocation query response server
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Oct 31, 2024
1 parent 0a595fd commit 9116886
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 77 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions local-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ path = "src/main.rs"
path = "src/lib.rs"

[dependencies]
alloy = { workspace = true, features = ["k256"] }
anyhow.workspace = true
axum = { version = "0.7.5", features = ["macros"] }
dotenvy = "0.15.7"
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
indexer-tap-agent = { path = "../tap-agent" }
tokio = { workspace = true, features = ["full"] }
tracing.workspace = true
tracing-subscriber.workspace = true
74 changes: 74 additions & 0 deletions local-service/src/allocations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use serde::{Deserialize, Serialize};

use crate::keys;

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct Allocations {
meta: Meta,
allocations: Vec<AllocationFragment>,
}

impl Allocations {
pub fn new(indexer: keys::Indexer) -> Self {
Allocations {
meta: Meta {
block: Block {
number: 123,
hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
.into(),
timestamp: "2021-01-01T00:00:00Z".into(),
},
},
allocations: vec![AllocationFragment {
id: "0x123".into(), // TODO: "<allocation id address>"
indexer: Indexer {
id: indexer.address.to_string(),
},
allocated_tokens: "0".into(),
created_at_block_hash:
"0x0000000000000000000000000000000000000000000000000000000000000000".into(),
created_at_epoch: "1".into(),
closed_at_epoch: None,
subgraph_deployment: SubgraphDeployment {
id: "QmUhiH6Z5xo6o3GNzsSvqpGKLmCt6w5WzKQ1yHk6C8AA8S".into(),
denied_at: "0".into(),
},
}],
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AllocationFragment {
id: String,
indexer: Indexer,
allocated_tokens: String,
created_at_block_hash: String,
created_at_epoch: String,
closed_at_epoch: Option<String>,
subgraph_deployment: SubgraphDeployment,
}

#[derive(Debug, Serialize, Deserialize)]
struct Indexer {
id: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct SubgraphDeployment {
id: String,
denied_at: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct Meta {
block: Block,
}

#[derive(Debug, Serialize, Deserialize)]
struct Block {
number: u128,
hash: String,
timestamp: String,
}
14 changes: 10 additions & 4 deletions local-service/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ use axum::{
};
use tokio::net::TcpListener;

use crate::subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE};
use crate::{
keys::manage_keys,
subgraph::{network_subgraph, NETWORK_SUBGRAPH_ROUTE},
};

const HOST: &str = "0.0.0.0";
const PORT: &str = "8000";

pub async fn start_server() -> anyhow::Result<()> {
let (indexer, _sender) = manage_keys()?;

let port = dotenvy::var("API_PORT").unwrap_or(PORT.into());
let listener = TcpListener::bind(&format!("{HOST}:{port}")).await?;

let router = Router::new()
.route("/health", get(health_check))
.route(NETWORK_SUBGRAPH_ROUTE, post(network_subgraph));
let router = Router::new().route("/health", get(health_check)).route(
NETWORK_SUBGRAPH_ROUTE,
post(move || network_subgraph(indexer)),
);

Ok(axum::serve(listener, router).await?)
}
Expand Down
43 changes: 43 additions & 0 deletions local-service/src/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![allow(dead_code)]

use std::{
fs::File,
io::{self, Write},
};

use alloy::{
primitives::Address,
signers::{k256::ecdsa::SigningKey, local::LocalSigner},
};
use indexer_tap_agent::tap::test_utils::wallet;

#[derive(Clone, Debug)]
pub struct Indexer {
pub signer: LocalSigner<SigningKey>,
pub address: Address,
}

#[derive(Clone, Debug)]
pub struct QuerySender(pub Address);

pub fn manage_keys() -> io::Result<(Indexer, QuerySender)> {
let (signer1, address1) = wallet(0);
let (signer2, address2) = wallet(1);

write_to_file("indexer_wallet", &signer1)?;
write_to_file("sender_wallet", &signer2)?;

let indexer = Indexer {
signer: signer1,
address: address1,
};
let sender = QuerySender(address2);

Ok((indexer, sender))
}

fn write_to_file(path: &str, signer: &LocalSigner<SigningKey>) -> io::Result<()> {
let mut file = File::create(path)?;
file.write_all(signer.to_bytes().as_slice())?;
Ok(())
}
2 changes: 2 additions & 0 deletions local-service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
mod allocations;
pub mod bootstrap;
mod keys;
mod subgraph;
1 change: 1 addition & 0 deletions local-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use local_service::bootstrap::start_server;
#[tokio::main]
async fn main() -> ExitCode {
tracing_subscriber::fmt::init();

if let Err(e) = start_server().await {
tracing::error!("Local Service error: {e}");
return ExitCode::from(1);
Expand Down
77 changes: 5 additions & 72 deletions local-service/src/subgraph.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,15 @@
use axum::{response::IntoResponse, Json};
use serde::{Deserialize, Serialize};

use crate::{allocations::Allocations, keys::Indexer};

pub(crate) const NETWORK_SUBGRAPH_ROUTE: &str = "/network_subgraph";

pub(crate) async fn network_subgraph() -> impl IntoResponse {
Json(GraphqlResponse::Allocations(Allocations {
..Default::default()
}))
pub(crate) async fn network_subgraph(indexer: Indexer) -> impl IntoResponse {
Json(GraphqlResponse::Allocations(Allocations::new(indexer)))
}

#[derive(Debug, Serialize, Deserialize)]
enum GraphqlResponse {
pub(crate) enum GraphqlResponse {
Allocations(Allocations),
}

#[derive(Debug, Serialize, Deserialize)]
struct Allocations {
meta: Meta,
allocations: Vec<AllocationFragment>,
}

impl Default for Allocations {
fn default() -> Self {
Allocations {
meta: Meta {
block: Block {
number: 1,
hash: "0x123".into(),
timestamp: "2021-01-01T00:00:00Z".into(),
},
},
allocations: vec![AllocationFragment {
id: "0x123".into(),
indexer: Indexer { id: "0x456".into() },
allocated_tokens: "100".into(),
created_at_block_hash: "0x789".into(),
created_at_epoch: "1".into(),
closed_at_epoch: None,
subgraph_deployment: SubgraphDeployment {
id: "0xabc".into(),
denied_at: "0".into(),
},
}],
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AllocationFragment {
id: String,
indexer: Indexer,
allocated_tokens: String,
created_at_block_hash: String,
created_at_epoch: String,
closed_at_epoch: Option<String>,
subgraph_deployment: SubgraphDeployment,
}

#[derive(Debug, Serialize, Deserialize)]
struct Indexer {
id: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct SubgraphDeployment {
id: String,
denied_at: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct Meta {
block: Block,
}

#[derive(Debug, Serialize, Deserialize)]
struct Block {
number: u128,
hash: String,
timestamp: String,
}
2 changes: 1 addition & 1 deletion tap-agent/src/tap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use indexer_common::escrow_accounts::EscrowAccounts;
pub mod context;
pub mod escrow_adapter;

#[cfg(test)]
// #[cfg(test)]
pub mod test_utils;

pub async fn signers_trimmed(
Expand Down

0 comments on commit 9116886

Please sign in to comment.