iota_indexer/apis/
write_api.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use async_trait::async_trait;
6use fastcrypto::encoding::Base64;
7use iota_json_rpc::IotaRpcModule;
8use iota_json_rpc_api::{WriteApiClient, WriteApiServer, error_object_from_rpc};
9use iota_json_rpc_types::{
10    DevInspectArgs, DevInspectResults, DryRunTransactionBlockResponse,
11    IotaTransactionBlockResponse, IotaTransactionBlockResponseOptions,
12};
13use iota_open_rpc::Module;
14use iota_types::{
15    base_types::IotaAddress, iota_serde::BigInt, quorum_driver_types::ExecuteTransactionRequestType,
16};
17use jsonrpsee::{RpcModule, core::RpcResult, http_client::HttpClient};
18
19use crate::types::IotaTransactionBlockResponseWithOptions;
20
21pub(crate) struct WriteApi {
22    fullnode: HttpClient,
23}
24
25impl WriteApi {
26    pub fn new(fullnode_client: HttpClient) -> Self {
27        Self {
28            fullnode: fullnode_client,
29        }
30    }
31}
32
33#[async_trait]
34impl WriteApiServer for WriteApi {
35    async fn execute_transaction_block(
36        &self,
37        tx_bytes: Base64,
38        signatures: Vec<Base64>,
39        options: Option<IotaTransactionBlockResponseOptions>,
40        request_type: Option<ExecuteTransactionRequestType>,
41    ) -> RpcResult<IotaTransactionBlockResponse> {
42        let iota_transaction_response = self
43            .fullnode
44            .execute_transaction_block(tx_bytes, signatures, options.clone(), request_type)
45            .await
46            .map_err(error_object_from_rpc)?;
47        Ok(IotaTransactionBlockResponseWithOptions {
48            response: iota_transaction_response,
49            options: options.unwrap_or_default(),
50        }
51        .into())
52    }
53
54    async fn dev_inspect_transaction_block(
55        &self,
56        sender_address: IotaAddress,
57        tx_bytes: Base64,
58        gas_price: Option<BigInt<u64>>,
59        epoch: Option<BigInt<u64>>,
60        additional_args: Option<DevInspectArgs>,
61    ) -> RpcResult<DevInspectResults> {
62        self.fullnode
63            .dev_inspect_transaction_block(
64                sender_address,
65                tx_bytes,
66                gas_price,
67                epoch,
68                additional_args,
69            )
70            .await
71            .map_err(error_object_from_rpc)
72    }
73
74    async fn dry_run_transaction_block(
75        &self,
76        tx_bytes: Base64,
77    ) -> RpcResult<DryRunTransactionBlockResponse> {
78        self.fullnode
79            .dry_run_transaction_block(tx_bytes)
80            .await
81            .map_err(error_object_from_rpc)
82    }
83}
84
85impl IotaRpcModule for WriteApi {
86    fn rpc(self) -> RpcModule<Self> {
87        self.into_rpc()
88    }
89
90    fn rpc_doc_module() -> Module {
91        iota_json_rpc_api::WriteApiOpenRpc::module_doc()
92    }
93}