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