-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,368 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
[package] | ||
description = "Madara client rpc service" | ||
name = "mc-gateway" | ||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
version.workspace = true | ||
homepage.workspace = true | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
|
||
# Deoxys | ||
mc-db = { workspace = true } | ||
mp-block = { workspace = true } | ||
mp-gateway = { workspace = true } | ||
mp-state-update = { workspace = true } | ||
mp-transactions = { workspace = true } | ||
mp-utils = { workspace = true } | ||
|
||
# Starknet | ||
starknet-core = { workspace = true } | ||
starknet-types-core = { workspace = true } | ||
|
||
# Other | ||
anyhow = { workspace = true } | ||
hyper = { workspace = true } | ||
log = { workspace = true } | ||
reqwest = { workspace = true } | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json = { workspace = true } | ||
thiserror = { workspace = true } | ||
tokio = { workspace = true } | ||
url = { workspace = true } | ||
|
||
[dev-dependencies] | ||
tokio = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::collections::HashMap; | ||
|
||
use reqwest::Client; | ||
use url::Url; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct FeederClient { | ||
pub(crate) client: Client, | ||
pub(crate) gateway_url: Url, | ||
pub(crate) feeder_gateway_url: Url, | ||
pub(crate) headers: HashMap<String, String>, | ||
} | ||
|
||
impl FeederClient { | ||
pub fn new(gateway_url: Url, feeder_gateway_url: Url) -> Self { | ||
Self { client: Client::new(), gateway_url, feeder_gateway_url, headers: HashMap::new() } | ||
} | ||
|
||
pub fn new_with_headers(gateway_url: Url, feeder_gateway_url: Url, headers: &[(String, String)]) -> Self { | ||
let headers = headers.iter().cloned().collect(); | ||
Self { client: Client::new(), gateway_url, feeder_gateway_url, headers } | ||
} | ||
|
||
pub fn add_header(&mut self, key: &str, value: &str) { | ||
self.headers.insert(key.to_string(), value.to_string()); | ||
} | ||
|
||
pub fn remove_header(&mut self, key: &str) -> Option<String> { | ||
self.headers.remove(key) | ||
} | ||
|
||
pub fn starknet_alpha_mainnet() -> Self { | ||
Self::new( | ||
Url::parse("https://alpha-mainnet.starknet.io/gateway/").unwrap(), | ||
Url::parse("https://alpha-mainnet.starknet.io/feeder_gateway/").unwrap(), | ||
) | ||
} | ||
|
||
pub fn starknet_alpha_sepolia() -> Self { | ||
Self::new( | ||
Url::parse("https://alpha-sepolia.starknet.io/gateway/").unwrap(), | ||
Url::parse("https://alpha-sepolia.starknet.io/feeder_gateway/").unwrap(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use mp_block::{BlockId, BlockTag}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::error::SequencerError; | ||
|
||
use super::{builder::FeederClient, request_builder::RequestBuilder}; | ||
|
||
use mp_gateway::{ | ||
block::{BlockProvider, PendingBlockProvider, ProviderMaybePendingBlock}, | ||
state_update::{PendingStateUpdateProvider, ProviderMaybePendingStateUpdate, StateUpdateProvider}, | ||
}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct StateUpdateWithBlock { | ||
pub state_update: StateUpdateProvider, | ||
pub block: BlockProvider, | ||
} | ||
|
||
impl FeederClient { | ||
pub async fn get_block(&self, block_id: BlockId) -> Result<ProviderMaybePendingBlock, SequencerError> { | ||
let request = RequestBuilder::new(&self.client, self.feeder_gateway_url.clone()) | ||
.add_uri_segment("get_block") | ||
.unwrap() | ||
.with_block_id(block_id); | ||
|
||
match block_id { | ||
BlockId::Tag(BlockTag::Pending) => { | ||
Ok(ProviderMaybePendingBlock::Pending(request.send_get::<PendingBlockProvider>().await?)) | ||
} | ||
_ => Ok(ProviderMaybePendingBlock::Block(request.send_get::<BlockProvider>().await?)), | ||
} | ||
} | ||
|
||
pub async fn get_state_update(&self, block_id: BlockId) -> Result<ProviderMaybePendingStateUpdate, SequencerError> { | ||
let request = RequestBuilder::new(&self.client, self.feeder_gateway_url.clone()) | ||
.add_uri_segment("get_state_update") | ||
.unwrap() | ||
.with_block_id(block_id); | ||
|
||
match block_id { | ||
BlockId::Tag(BlockTag::Pending) => { | ||
Ok(ProviderMaybePendingStateUpdate::Pending(request.send_get::<PendingStateUpdateProvider>().await?)) | ||
} | ||
_ => Ok(ProviderMaybePendingStateUpdate::Update(request.send_get::<StateUpdateProvider>().await?)), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use mp_block::BlockTag; | ||
use starknet_core::types::Felt; | ||
|
||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_get_block() { | ||
let client = FeederClient::starknet_alpha_mainnet(); | ||
|
||
let block = client.get_block(BlockId::Number(0)).await.unwrap(); | ||
println!("parent_block_hash: 0x{:x}", block.parent_block_hash()); | ||
let block = client | ||
.get_block(BlockId::Hash(Felt::from_hex_unchecked( | ||
"0x47c3637b57c2b079b93c61539950c17e868a28f46cdef28f88521067f21e943", | ||
))) | ||
.await | ||
.unwrap(); | ||
println!("parent_block_hash: 0x{:x}", block.parent_block_hash()); | ||
let block = client.get_block(BlockId::Tag(BlockTag::Latest)).await.unwrap(); | ||
println!("parent_block_hash: 0x{:x}", block.parent_block_hash()); | ||
let block = client.get_block(BlockId::Tag(BlockTag::Pending)).await.unwrap(); | ||
println!("parent_block_hash: 0x{:x}", block.parent_block_hash()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_state_update() { | ||
let client = FeederClient::starknet_alpha_mainnet(); | ||
|
||
let block = client.get_state_update(BlockId::Number(0)).await.unwrap(); | ||
let block = client | ||
.get_state_update(BlockId::Hash(Felt::from_hex_unchecked( | ||
"0x47c3637b57c2b079b93c61539950c17e868a28f46cdef28f88521067f21e943", | ||
))) | ||
.await | ||
.unwrap(); | ||
let block = client.get_state_update(BlockId::Tag(BlockTag::Latest)).await.unwrap(); | ||
let block = client.get_state_update(BlockId::Tag(BlockTag::Pending)).await.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod builder; | ||
mod methods; | ||
mod request_builder; |
Oops, something went wrong.