1use axum::{Extension, Json, extract::State};
8use axum_extra::extract::WithRejection;
9use iota_json_rpc_types::IotaTransactionBlockResponseOptions;
10use tracing::debug;
11
12use crate::{
13 Error, IotaEnv, OnlineServerContext,
14 types::{
15 BlockRequest, BlockResponse, BlockTransactionRequest, BlockTransactionResponse,
16 Transaction, TransactionIdentifier,
17 },
18};
19
20pub async fn block(
23 State(state): State<OnlineServerContext>,
24 Extension(env): Extension<IotaEnv>,
25 WithRejection(Json(request), _): WithRejection<Json<BlockRequest>, Error>,
26) -> Result<BlockResponse, Error> {
27 debug!("Called /block endpoint: {:?}", request.block_identifier);
28 env.check_network_identifier(&request.network_identifier)?;
29 let blocks = state.blocks();
30 if let Some(index) = request.block_identifier.index {
31 blocks.get_block_by_index(index).await
32 } else if let Some(hash) = request.block_identifier.hash {
33 blocks.get_block_by_hash(hash).await
34 } else {
35 blocks.current_block().await
36 }
37}
38
39pub async fn transaction(
42 State(context): State<OnlineServerContext>,
43 Extension(env): Extension<IotaEnv>,
44 WithRejection(Json(request), _): WithRejection<Json<BlockTransactionRequest>, Error>,
45) -> Result<BlockTransactionResponse, Error> {
46 env.check_network_identifier(&request.network_identifier)?;
47 let digest = request.transaction_identifier.hash;
48 let response = context
49 .client
50 .read_api()
51 .get_transaction_with_options(
52 digest,
53 IotaTransactionBlockResponseOptions::new()
54 .with_input()
55 .with_events()
56 .with_effects()
57 .with_balance_changes(),
58 )
59 .await?;
60 let hash = response.digest;
61
62 let operations = response.try_into()?;
63
64 let transaction = Transaction {
65 transaction_identifier: TransactionIdentifier { hash },
66 operations,
67 related_transactions: vec![],
68 metadata: None,
69 };
70
71 Ok(BlockTransactionResponse { transaction })
72}