diff --git a/flake.nix b/flake.nix index d4304b80..db783169 100644 --- a/flake.nix +++ b/flake.nix @@ -178,7 +178,6 @@ pkgs.postgresql ]; - CASPER_CHAIN_NAME = "cspr-dev-cctl"; PATH_TO_WASM_BINARIES = "${self'.packages.kairos-contracts}/bin"; PATH_TO_SESSION_BINARIES = "${self'.packages.kairos-session-code}/bin"; CCTL_CONFIG = "${cctlConfig.config}"; @@ -193,7 +192,6 @@ # Rust Analyzer needs to be able to find the path to default crate RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library"; CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld"; - CASPER_CHAIN_NAME = "cspr-dev-cctl"; PATH_TO_WASM_BINARIES = "${self'.packages.kairos-contracts}/bin"; PATH_TO_SESSION_BINARIES = "${self'.packages.kairos-session-code}/bin"; CCTL_CONFIG = "${cctlConfig.config}"; diff --git a/kairos-server/src/routes/get_chain_name.rs b/kairos-server/src/routes/get_chain_name.rs index e8c9e07a..27a8b2a5 100644 --- a/kairos-server/src/routes/get_chain_name.rs +++ b/kairos-server/src/routes/get_chain_name.rs @@ -1,5 +1,6 @@ use axum::{extract::State, Json}; use axum_extra::routing::TypedPath; +use reqwest::Url; use tracing::*; use crate::{state::ServerState, AppErr}; @@ -8,11 +9,27 @@ use crate::{state::ServerState, AppErr}; #[typed_path("/api/v1/chain_name")] pub struct GetChainNamePath; -#[instrument(level = "trace", skip(_state), ret)] +#[instrument(level = "trace", skip(state), ret)] pub async fn get_chain_name_handler( _: GetChainNamePath, - _state: State, + state: State, ) -> Result, AppErr> { - let chain_name = env!("CASPER_CHAIN_NAME"); // NOTE: This should be obtained from RPC, rather than from hardcoded value. - Ok(Json(String::from(chain_name))) + // Call RPC to get chain name. + let rpc_url = &state.server_config.casper_rpc; + let chain_name = get_chain_name_from_rpc(rpc_url) + .await + .expect("RPC request failed"); + + Ok(Json(chain_name)) +} + +pub async fn get_chain_name_from_rpc(rpc_url: &Url) -> Result { + let request_id = casper_client::JsonRpcId::Number(1); + let verbosity = casper_client::Verbosity::Low; + let response = casper_client::get_node_status(request_id, rpc_url.as_str(), verbosity) + .await + .expect("RPC request failed"); + let chain_name = response.result.chainspec_name; + + Ok(chain_name) } diff --git a/kairos-server/src/state/submit_batch.rs b/kairos-server/src/state/submit_batch.rs index 1b76b2d8..6dd3d74b 100644 --- a/kairos-server/src/state/submit_batch.rs +++ b/kairos-server/src/state/submit_batch.rs @@ -12,6 +12,8 @@ use casper_client_types::{ use rand::random; use reqwest::Url; +use crate::routes::get_chain_name::get_chain_name_from_rpc; + pub const MAX_GAS_FEE_PAYMENT_AMOUNT: u64 = 10_000_000_000_000; // TODO: retry request on failure, improve error handling @@ -29,7 +31,10 @@ pub async fn submit_proof_to_contract( }, }; - let deploy = DeployBuilder::new(env!("CASPER_CHAIN_NAME"), submit_batch, signer) + let chain_name = get_chain_name_from_rpc(&casper_rpc) + .await + .expect("RPC request failed"); + let deploy = DeployBuilder::new(chain_name, submit_batch, signer) .with_standard_payment(MAX_GAS_FEE_PAYMENT_AMOUNT) .with_timestamp(Timestamp::now()) .with_ttl(TimeDiff::from_millis(60_000))