sui_rosetta/
block.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use axum::extract::State;
5use axum::{Extension, Json};
6use axum_extra::extract::WithRejection;
7use tracing::debug;
8
9use crate::operations::Operations;
10use crate::types::{
11    BlockRequest, BlockResponse, BlockTransactionRequest, BlockTransactionResponse, Transaction,
12    TransactionIdentifier,
13};
14use crate::{Error, OnlineServerContext, SuiEnv};
15use sui_json_rpc_types::SuiTransactionBlockResponseOptions;
16
17// This module implements the [Rosetta Block API](https://www.rosetta-api.org/docs/BlockApi.html)
18
19/// Get a block by its Block Identifier.
20/// [Rosetta API Spec](https://www.rosetta-api.org/docs/BlockApi.html#block)
21pub async fn block(
22    State(state): State<OnlineServerContext>,
23    Extension(env): Extension<SuiEnv>,
24    WithRejection(Json(request), _): WithRejection<Json<BlockRequest>, Error>,
25) -> Result<BlockResponse, Error> {
26    debug!("Called /block endpoint: {:?}", request.block_identifier);
27    env.check_network_identifier(&request.network_identifier)?;
28    let blocks = state.blocks();
29    if let Some(index) = request.block_identifier.index {
30        blocks.get_block_by_index(index).await
31    } else if let Some(hash) = request.block_identifier.hash {
32        blocks.get_block_by_hash(hash).await
33    } else {
34        blocks.current_block().await
35    }
36}
37
38/// Get a transaction in a block by its Transaction Identifier.
39/// [Rosetta API Spec](https://www.rosetta-api.org/docs/BlockApi.html#blocktransaction)
40pub async fn transaction(
41    State(context): State<OnlineServerContext>,
42    Extension(env): Extension<SuiEnv>,
43    WithRejection(Json(request), _): WithRejection<Json<BlockTransactionRequest>, Error>,
44) -> Result<BlockTransactionResponse, Error> {
45    env.check_network_identifier(&request.network_identifier)?;
46    let digest = request.transaction_identifier.hash;
47    let response = context
48        .client
49        .read_api()
50        .get_transaction_with_options(
51            digest,
52            SuiTransactionBlockResponseOptions::new()
53                .with_input()
54                .with_events()
55                .with_effects()
56                .with_balance_changes(),
57        )
58        .await?;
59    let hash = response.digest;
60
61    let operations = Operations::try_from_response(response, &context.coin_metadata_cache).await?;
62
63    let transaction = Transaction {
64        transaction_identifier: TransactionIdentifier { hash },
65        operations,
66        related_transactions: vec![],
67        metadata: None,
68    };
69
70    Ok(BlockTransactionResponse { transaction })
71}