Skip to content

chore(fortuna)-add-network-id-api #2904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2025
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion apps/fortuna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fortuna"
version = "8.2.4"
version = "8.2.5"
edition = "2021"

[lib]
Expand Down
83 changes: 72 additions & 11 deletions apps/fortuna/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,9 @@ mod test {

#[tokio::test]
async fn test_chain_configs_with_data() {
use crate::api::get_chain_configs;
use axum::{routing::get, Router};

// Create a config with actual chain data
let mut config_chains = HashMap::new();
config_chains.insert(
Expand Down Expand Up @@ -673,16 +676,72 @@ mod test {
},
};

let metrics_registry = Arc::new(RwLock::new(Registry::default()));
let api_state = ApiState::new(
Arc::new(RwLock::new(HashMap::new())),
metrics_registry,
Arc::new(History::new().await.unwrap()),
&config,
)
.await;
// Create initialized blockchain states with network IDs
let eth_read = Arc::new(MockEntropyReader::with_requests(10, &[]));
let avax_read = Arc::new(MockEntropyReader::with_requests(10, &[]));

let app = api::routes(api_state);
let eth_state = MonitoredHashChainState::new(
ETH_CHAIN.clone(),
Default::default(),
"ethereum".into(),
PROVIDER,
);

let eth_blockchain_state = BlockchainState {
id: "ethereum".into(),
network_id: 1, // Ethereum mainnet
state: Arc::new(eth_state),
contract: eth_read.clone(),
provider_address: PROVIDER,
reveal_delay_blocks: 1,
confirmed_block_status: BlockStatus::Latest,
};

let avax_state = MonitoredHashChainState::new(
AVAX_CHAIN.clone(),
Default::default(),
"avalanche".into(),
PROVIDER,
);

let avax_blockchain_state = BlockchainState {
id: "avalanche".into(),
network_id: 43114, // Avalanche C-Chain
state: Arc::new(avax_state),
contract: avax_read.clone(),
provider_address: PROVIDER,
reveal_delay_blocks: 2,
confirmed_block_status: BlockStatus::Latest,
};

// Create chains HashMap with initialized states
let mut chains = HashMap::new();
chains.insert(
"ethereum".into(),
ApiBlockChainState::Initialized(eth_blockchain_state),
);
chains.insert(
"avalanche".into(),
ApiBlockChainState::Initialized(avax_blockchain_state),
);

// Minimal ApiState for this endpoint
let api_state = ApiState {
chains: Arc::new(RwLock::new(chains)),
history: Arc::new(History::new().await.unwrap()),
metrics_registry: Arc::new(RwLock::new(Registry::default())),
metrics: Arc::new(crate::api::ApiMetrics {
http_requests: prometheus_client::metrics::family::Family::default(),
}),
explorer_metrics: Arc::new(
crate::api::ExplorerMetrics::new(Arc::new(RwLock::new(Registry::default()))).await,
),
config,
};

let app = Router::new()
.route("/v1/chains/configs", get(get_chain_configs))
.with_state(api_state);
let server = TestServer::new(app).unwrap();

// Test the chain configs endpoint
Expand All @@ -706,7 +765,8 @@ mod test {
);
assert_eq!(eth_config.reveal_delay_blocks, 1);
assert_eq!(eth_config.gas_limit, 500000);
assert_eq!(eth_config.fee, 1500000000000000);
assert_eq!(eth_config.default_fee, 1500000000000000);
assert_eq!(eth_config.network_id, 1); // Ethereum mainnet

// Find avalanche config
let avax_config = configs
Expand All @@ -719,6 +779,7 @@ mod test {
);
assert_eq!(avax_config.reveal_delay_blocks, 2);
assert_eq!(avax_config.gas_limit, 600000);
assert_eq!(avax_config.fee, 2000000000000000);
assert_eq!(avax_config.default_fee, 2000000000000000);
assert_eq!(avax_config.network_id, 43114); // Avalanche C-Chain
}
}
12 changes: 9 additions & 3 deletions apps/fortuna/src/api/config.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
use {
crate::api::{ApiState, RestError},
crate::api::{ApiBlockChainState, ApiState, RestError},
axum::{extract::State, Json},
serde::Serialize,
};

#[derive(Serialize, serde::Deserialize)]
pub struct ChainConfigSummary {
pub name: String,
pub network_id: u64,
pub contract_addr: String,
pub reveal_delay_blocks: u64,
pub gas_limit: u32,
pub fee: u128,
pub default_fee: u128,
}

pub async fn get_chain_configs(
State(state): State<ApiState>,
) -> Result<Json<Vec<ChainConfigSummary>>, RestError> {
let mut configs = Vec::new();
for (name, chain) in state.config.chains.iter() {
let network_id = match state.chains.read().await.get(name) {
Some(ApiBlockChainState::Initialized(blockchain_state)) => blockchain_state.network_id,
_ => 0,
};
configs.push(ChainConfigSummary {
name: name.clone(),
network_id,
contract_addr: format!("0x{:x}", chain.contract_addr),
reveal_delay_blocks: chain.reveal_delay_blocks,
gas_limit: chain.gas_limit,
fee: chain.fee,
default_fee: chain.fee,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the default fee going to be used for? shouldn't we show up to date fees using getFeeV2()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the default fee won't be used. I will use getFeeV2(). I realized it today after our discussion. But keeping it here won't be harmful.

});
}
Ok(Json(configs))
Expand Down