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