kabu_rpc_handler/handler/flashbots.rs
1use crate::dto::flashbots::{BundleRequest, BundleResponse, SendBundleResponse};
2use axum::extract::State;
3use axum::http::StatusCode;
4use axum::Json;
5use kabu_rpc_state::AppState;
6use kabu_types_blockchain::ChainParameters;
7use revm::{DatabaseCommit, DatabaseRef};
8use std::fmt::Debug;
9use tracing::info;
10
11pub async fn flashbots<DB>(
12 State(app_state): State<AppState<DB>>,
13 Json(bundle_request): Json<BundleRequest>,
14) -> Result<Json<SendBundleResponse>, (StatusCode, String)>
15where
16 DB: DatabaseRef + DatabaseCommit + Send + Sync + Clone + 'static,
17 <DB as DatabaseRef>::Error: Debug,
18{
19 for (bundle_idx, bundle_param) in bundle_request.params.iter().enumerate() {
20 info!(
21 "Flashbots bundle({bundle_idx}): target_block={:?}, transactions_len={:?}",
22 bundle_param.target_block,
23 bundle_param.transactions.len()
24 );
25 let last_block_header = app_state.bc.latest_block().read().await.block_header.clone().unwrap_or_default();
26 let target_block = bundle_param.target_block.unwrap_or_default().to::<u64>();
27 if target_block <= last_block_header.number {
28 return Err((
29 StatusCode::BAD_REQUEST,
30 format!("Target block is target_block={} <= last_block={}", target_block, last_block_header.number),
31 ));
32 }
33 let _next_block_timestamp = last_block_header.timestamp + 12 * (target_block - last_block_header.number);
34 let _next_block_base_fee = ChainParameters::ethereum().calc_next_block_base_fee(
35 last_block_header.gas_used,
36 last_block_header.gas_limit,
37 last_block_header.base_fee_per_gas.unwrap_or_default(),
38 );
39 //TODO : rewrite
40 /*
41 let evm_env = Env {
42 block: BlockEnv {
43 number: U256::from(target_block),
44 timestamp: U256::from(next_block_timestamp),
45 basefee: U256::from(next_block_base_fee),
46 ..BlockEnv::default()
47 },
48 ..Env::default()
49 };
50 let db = app_state.state.market_state().read().await.state_db.clone();
51 let mut evm = Evm::builder().with_spec_id(CANCUN).with_ref_db(db).with_env(Box::new(evm_env)).build();
52 for (tx_idx, tx) in bundle_param.transactions.iter().enumerate() {
53 let tx_hash = keccak256(tx);
54
55 let tx_env = env_from_signed_tx(tx.clone()).map_err(|e| (StatusCode::BAD_REQUEST, format!("Error: {}", e)))?;
56 info!("Flashbots bundle({bundle_idx}) -> tx({tx_idx}): caller={:?}, transact_to={:?}, data={:?}, value={:?}, gas_price={:?}, gas_limit={:?}, nonce={:?}, chain_id={:?}, access_list_len={}",
57 tx_env.caller, tx_env.transact_to, tx_env.data, tx_env.value, tx_env.gas_price, tx_env.gas_limit, tx_env.nonce, tx_env.chain_id, tx_env.access_list.len());
58
59 evm.context.evm.env.tx = tx_env;
60
61 let (result, gas_used) = evm_transact(&mut evm).map_err(|e| {
62 error!("Flashbot tx error latest_block={}, tx_hash={}, err={}/{:?}", last_block_header.number, tx_hash, e, e);
63 (StatusCode::BAD_REQUEST, format!("Error: {}", e))
64 })?;
65 info!("result={}, gas_used={}", hex::encode_prefixed(result), gas_used);
66 }
67
68 */
69 }
70
71 Ok(Json(SendBundleResponse { jsonrpc: "2.0".to_string(), id: 1, result: BundleResponse { bundle_hash: None } }))
72}