diff --git a/kalypso-cli/Cargo.toml b/kalypso-cli/Cargo.toml index 18aa5f3..25f63ec 100644 --- a/kalypso-cli/Cargo.toml +++ b/kalypso-cli/Cargo.toml @@ -18,7 +18,7 @@ generator = {path = "../generator", package = "generator"} hex = "0.4.3" kalypso_helper = {path = "../helper", package = "helper"} log = "0.4" -matching_engine_helpers = { path = "../matching_engine", package = "matching_engine" } +matching_engine_helpers = { path = "../matching_engine", package = "matching_engine", features = ["use_l1_block_numbers"]} reqwest = { version = "0.11", features = ["stream"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/kalypso-cli/src/common_deps.rs b/kalypso-cli/src/common_deps.rs index 1c93519..9a27bdf 100644 --- a/kalypso-cli/src/common_deps.rs +++ b/kalypso-cli/src/common_deps.rs @@ -578,6 +578,8 @@ pub struct NonConfidentialRequest { pub max_proof_generation_time: U256, pub inputs: ethers::types::Bytes, pub market_id: U256, + pub provider_http: Provider, + pub kalypso_rpc_url: String, } impl CommonDeps { @@ -635,6 +637,9 @@ impl CommonDeps { let market_id = U256::from_dec_str(market_id.as_str()) .map_err(|e| format!("Invalid Market Id: {}", e))?; + let provider_http = + Provider::::try_from(rpc_url).map_err(|e| format!("Invalid RPC URL: {}", e))?; + Ok(NonConfidentialRequest { private_key_signer, proof_marketplace, @@ -643,6 +648,8 @@ impl CommonDeps { max_proof_generation_time, inputs, market_id, + provider_http, + kalypso_rpc_url: rpc_url.clone(), }) } } @@ -911,6 +918,8 @@ pub struct ConfidentialRequest { pub entity_registry: bindings::entity_key_registry::EntityKeyRegistry< SignerMiddleware, LocalWallet>, >, + pub provider_http: Provider, + pub kalypso_rpc_url: String, } impl CommonDeps { @@ -999,6 +1008,9 @@ impl CommonDeps { rpc_url, )?; + let provider_http = + Provider::::try_from(rpc_url).map_err(|e| format!("Invalid RPC URL: {}", e))?; + Ok(ConfidentialRequest { private_key_signer, proof_marketplace, @@ -1009,6 +1021,8 @@ impl CommonDeps { market_id, private_inputs, entity_registry, + provider_http, + kalypso_rpc_url: rpc_url.clone(), }) } } diff --git a/kalypso-cli/src/operations/create_request.rs b/kalypso-cli/src/operations/create_request.rs index 4f86996..75fe7c9 100644 --- a/kalypso-cli/src/operations/create_request.rs +++ b/kalypso-cli/src/operations/create_request.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use async_trait::async_trait; +use ethers::providers::Middleware; use ethers::{core::rand, signers::Signer, types::U256}; use crate::{common_deps::CommonDeps, operations::compute_pcrs::non_confidential_pcrs}; @@ -94,6 +95,27 @@ impl Operation for ConfidentialRequest { ) .map_err(|e| format!("Failed Encryption: {}", e))?; + let latest_l2_block = confidential_request_info + .provider_http + .get_block_number() + .await + .map_err(|e| { + format!( + "Failed fetching latest kalypso block number from chain: {}", + e + ) + })?; + + let latest_l1_block = matching_engine_helpers::utility::get_l1_block_from_l2_block( + &confidential_request_info.kalypso_rpc_url, + latest_l2_block.as_u64().into(), + ) + .await; + + if latest_l1_block.is_none() { + return Err("Failed fetching latest 11 block".into()); + } + let proof_request_transaction = CommonDeps::send_and_confirm( confidential_request_info .proof_marketplace @@ -101,7 +123,7 @@ impl Operation for ConfidentialRequest { bindings::proof_marketplace::Ask { market_id: confidential_request_info.market_id, reward: confidential_request_info.max_proof_generation_cost, - expiry: (10000000000000 as u64).into(), + expiry: (latest_l1_block.unwrap().as_u64() + 200).into(), time_taken_for_proof_generation: confidential_request_info .max_proof_generation_time, deadline: U256::zero(), @@ -185,6 +207,27 @@ impl Operation for NonConfidentialRequest { println!("Token Approval: {}", token_approval_transaction); } + let latest_l2_block = non_confidential_request_info + .provider_http + .get_block_number() + .await + .map_err(|e| { + format!( + "Failed fetching latest kalypso block number from chain: {}", + e + ) + })?; + + let latest_l1_block = matching_engine_helpers::utility::get_l1_block_from_l2_block( + &non_confidential_request_info.kalypso_rpc_url, + latest_l2_block.as_u64().into(), + ) + .await; + + if latest_l1_block.is_none() { + return Err("Failed fetching latest 11 block".into()); + } + let proof_request_transaction = CommonDeps::send_and_confirm( non_confidential_request_info .proof_marketplace @@ -192,7 +235,7 @@ impl Operation for NonConfidentialRequest { bindings::proof_marketplace::Ask { market_id: non_confidential_request_info.market_id, reward: non_confidential_request_info.max_proof_generation_cost, - expiry: (200 as u64).into(), + expiry: (latest_l1_block.unwrap().as_u64() + 200).into(), time_taken_for_proof_generation: non_confidential_request_info .max_proof_generation_time, deadline: U256::zero(),