sui_indexer_alt_jsonrpc/api/
write.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use fastcrypto::encoding::Base64;
5use jsonrpsee::core::RpcResult;
6use jsonrpsee::http_client::HttpClient;
7use jsonrpsee::proc_macros::rpc;
8use sui_json_rpc_types::DryRunTransactionBlockResponse;
9use sui_json_rpc_types::SuiTransactionBlockResponse;
10use sui_json_rpc_types::SuiTransactionBlockResponseOptions;
11use sui_open_rpc::Module;
12use sui_open_rpc_macros::open_rpc;
13use sui_types::transaction_driver_types::ExecuteTransactionRequestType;
14
15use crate::api::rpc_module::RpcModule;
16use crate::error::client_error_to_error_object;
17use crate::error::invalid_params;
18
19#[open_rpc(namespace = "sui", tag = "Write API")]
20#[rpc(server, client, namespace = "sui")]
21pub trait WriteApi {
22    /// Execute the transaction with options to show different information in the response.
23    /// The only supported request type is `WaitForEffectsCert`: waits for TransactionEffectsCert and then return to client.
24    /// `WaitForLocalExecution` mode has been deprecated.
25    #[method(name = "executeTransactionBlock")]
26    async fn execute_transaction_block(
27        &self,
28        /// BCS serialized transaction data bytes without its type tag, as base-64 encoded string.
29        tx_bytes: Base64,
30        /// A list of signatures (`flag || signature || pubkey` bytes, as base-64 encoded string). Signature is committed to the intent message of the transaction data, as base-64 encoded string.
31        signatures: Vec<Base64>,
32        /// options for specifying the content to be returned
33        options: Option<SuiTransactionBlockResponseOptions>,
34        /// The request type, derived from `SuiTransactionBlockResponseOptions` if None
35        request_type: Option<ExecuteTransactionRequestType>,
36    ) -> RpcResult<SuiTransactionBlockResponse>;
37
38    /// Return transaction execution effects including the gas cost summary,
39    /// while the effects are not committed to the chain.
40    #[method(name = "dryRunTransactionBlock")]
41    async fn dry_run_transaction_block(
42        &self,
43        tx_bytes: Base64,
44    ) -> RpcResult<DryRunTransactionBlockResponse>;
45}
46
47pub(crate) struct Write(pub HttpClient);
48
49#[derive(Debug, thiserror::Error)]
50pub enum Error {
51    #[error("WaitForLocalExecution mode is deprecated")]
52    DeprecatedWaitForLocalExecution,
53}
54
55impl Write {
56    pub(crate) fn new(client: HttpClient) -> Self {
57        Self(client)
58    }
59}
60
61#[async_trait::async_trait]
62impl WriteApiServer for Write {
63    async fn execute_transaction_block(
64        &self,
65        tx_bytes: Base64,
66        signatures: Vec<Base64>,
67        options: Option<SuiTransactionBlockResponseOptions>,
68        request_type: Option<ExecuteTransactionRequestType>,
69    ) -> RpcResult<SuiTransactionBlockResponse> {
70        if let Some(ExecuteTransactionRequestType::WaitForLocalExecution) = request_type {
71            return Err(invalid_params(Error::DeprecatedWaitForLocalExecution).into());
72        }
73        self.0
74            .execute_transaction_block(tx_bytes, signatures, options, request_type)
75            .await
76            .map_err(client_error_to_error_object)
77    }
78
79    async fn dry_run_transaction_block(
80        &self,
81        tx_bytes: Base64,
82    ) -> RpcResult<DryRunTransactionBlockResponse> {
83        self.0
84            .dry_run_transaction_block(tx_bytes)
85            .await
86            .map_err(client_error_to_error_object)
87    }
88}
89
90impl RpcModule for Write {
91    fn schema(&self) -> Module {
92        WriteApiOpenRpc::module_doc()
93    }
94
95    fn into_impl(self) -> jsonrpsee::RpcModule<Self> {
96        self.into_rpc()
97    }
98}