|
| 1 | +use reqwest::Client; |
| 2 | +use serde::de::DeserializeOwned; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + aggregation_layer::gateway::types::{ |
| 6 | + GatewayResponse, NonceResponse, Receipt, ReceiptsQuery, ReceiptsResponse, |
| 7 | + }, |
| 8 | + common::types::Network, |
| 9 | +}; |
| 10 | + |
| 11 | +pub struct AggregationModeGatewayProvider { |
| 12 | + gateway_url: String, |
| 13 | + http_client: Client, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +pub enum AggregationModeError { |
| 18 | + UnsupportedNetwork, |
| 19 | + Request(String), |
| 20 | + Api { status: u16, message: String }, |
| 21 | +} |
| 22 | + |
| 23 | +impl AggregationModeGatewayProvider { |
| 24 | + pub fn new(network: Network) -> Result<Self, AggregationModeError> { |
| 25 | + let gateway_url = match network { |
| 26 | + Network::Devnet => "http://127.0.0.1:8089".into(), |
| 27 | + |
| 28 | + _ => return Err(AggregationModeError::UnsupportedNetwork), |
| 29 | + }; |
| 30 | + |
| 31 | + Ok(Self { |
| 32 | + gateway_url, |
| 33 | + http_client: Client::new(), |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + pub fn new_with_signer() {} |
| 38 | + |
| 39 | + pub fn signer() {} |
| 40 | +} |
| 41 | + |
| 42 | +impl AggregationModeGatewayProvider { |
| 43 | + pub async fn gateway_url(&self) -> &str { |
| 44 | + &self.gateway_url |
| 45 | + } |
| 46 | + |
| 47 | + pub async fn get_nonce_for(&self, address: String) -> Result<u64, AggregationModeError> { |
| 48 | + let url = format!("{}/nonce/{}", self.gateway_url, address); |
| 49 | + let response: NonceResponse = self.send_request(self.http_client.get(url)).await?; |
| 50 | + |
| 51 | + Ok(response.nonce) |
| 52 | + } |
| 53 | + |
| 54 | + pub async fn get_receipts_for( |
| 55 | + &self, |
| 56 | + address: String, |
| 57 | + nonce: Option<u64>, |
| 58 | + ) -> Result<Vec<Receipt>, AggregationModeError> { |
| 59 | + let query = ReceiptsQuery { |
| 60 | + address: address, |
| 61 | + nonce, |
| 62 | + }; |
| 63 | + |
| 64 | + let request = self |
| 65 | + .http_client |
| 66 | + .get(format!("{}/receipts", self.gateway_url)) |
| 67 | + .query(&query); |
| 68 | + |
| 69 | + let response: ReceiptsResponse = self.send_request(request).await?; |
| 70 | + |
| 71 | + Ok(response.receipts) |
| 72 | + } |
| 73 | + |
| 74 | + pub async fn submit_sp1_proof(&self, serialized_proof: Vec<u8>, serialized_vk: Vec<u8>) {} |
| 75 | + |
| 76 | + // TODO: verify proof from receipt merkle path |
| 77 | + |
| 78 | + async fn send_request<T: DeserializeOwned>( |
| 79 | + &self, |
| 80 | + request: reqwest::RequestBuilder, |
| 81 | + ) -> Result<T, AggregationModeError> { |
| 82 | + let response = request |
| 83 | + .send() |
| 84 | + .await |
| 85 | + .map_err(|e| AggregationModeError::Request(e.to_string()))?; |
| 86 | + |
| 87 | + let payload: GatewayResponse<T> = response |
| 88 | + .json() |
| 89 | + .await |
| 90 | + .map_err(|e| AggregationModeError::Request(e.to_string()))?; |
| 91 | + |
| 92 | + if payload.status != 200 { |
| 93 | + return Err(AggregationModeError::Api { |
| 94 | + status: payload.status, |
| 95 | + message: payload.message, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + Ok(payload.data) |
| 100 | + } |
| 101 | +} |
0 commit comments