sui_rpc_api/grpc/
v2alpha.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::pin::Pin;

use tap::Pipe;

use crate::field_mask::FieldMaskTree;
use crate::message::MessageMergeFrom;
use crate::proto::google::rpc::bad_request::FieldViolation;
use crate::proto::rpc::v2alpha::live_data_service_server::LiveDataService;
use crate::proto::rpc::v2alpha::subscription_service_server::SubscriptionService;
use crate::proto::rpc::v2alpha::GetCoinInfoRequest;
use crate::proto::rpc::v2alpha::GetCoinInfoResponse;
use crate::proto::rpc::v2alpha::ListDynamicFieldsRequest;
use crate::proto::rpc::v2alpha::ListDynamicFieldsResponse;
use crate::proto::rpc::v2alpha::ListOwnedObjectsRequest;
use crate::proto::rpc::v2alpha::ListOwnedObjectsResponse;
use crate::proto::rpc::v2alpha::ResolveTransactionRequest;
use crate::proto::rpc::v2alpha::ResolveTransactionResponse;
use crate::proto::rpc::v2alpha::SimulateTransactionRequest;
use crate::proto::rpc::v2alpha::SimulateTransactionResponse;
use crate::proto::rpc::v2alpha::SubscribeCheckpointsRequest;
use crate::proto::rpc::v2alpha::SubscribeCheckpointsResponse;
use crate::proto::rpc::v2beta::Checkpoint;
use crate::proto::rpc::v2beta::ExecutedTransaction;
use crate::proto::rpc::v2beta::Transaction;
use crate::proto::rpc::v2beta::TransactionEffects;
use crate::proto::rpc::v2beta::TransactionEvents;
use crate::subscription::SubscriptionServiceHandle;
use crate::ErrorReason;
use crate::RpcError;
use crate::RpcService;

#[tonic::async_trait]
impl SubscriptionService for SubscriptionServiceHandle {
    /// Server streaming response type for the SubscribeCheckpoints method.
    type SubscribeCheckpointsStream = Pin<
        Box<
            dyn tokio_stream::Stream<Item = Result<SubscribeCheckpointsResponse, tonic::Status>>
                + Send,
        >,
    >;

    async fn subscribe_checkpoints(
        &self,
        request: tonic::Request<SubscribeCheckpointsRequest>,
    ) -> Result<tonic::Response<Self::SubscribeCheckpointsStream>, tonic::Status> {
        let read_mask = request.into_inner().read_mask.unwrap_or_default();
        let read_mask = FieldMaskTree::from(read_mask);

        let Some(mut receiver) = self.register_subscription().await else {
            return Err(tonic::Status::unavailable(
                "too many existing subscriptions",
            ));
        };

        let response = Box::pin(async_stream::stream! {
            while let Some(checkpoint) = receiver.recv().await {
                let Some(cursor) = checkpoint.sequence_number else {
                    yield Err(tonic::Status::internal("unable to determine cursor"));
                    break;
                };

                let checkpoint = Checkpoint::merge_from(checkpoint.as_ref(), &read_mask);
                let response = SubscribeCheckpointsResponse {
                    cursor: Some(cursor),
                    checkpoint: Some(checkpoint),
                };

                yield Ok(response);
            }
        });

        Ok(tonic::Response::new(response))
    }
}

mod get_coin_info;
mod list_dynamic_fields;
mod list_owned_objects;

#[tonic::async_trait]
impl LiveDataService for RpcService {
    async fn list_dynamic_fields(
        &self,
        request: tonic::Request<ListDynamicFieldsRequest>,
    ) -> Result<tonic::Response<ListDynamicFieldsResponse>, tonic::Status> {
        list_dynamic_fields::list_dynamic_fields(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn list_owned_objects(
        &self,
        request: tonic::Request<ListOwnedObjectsRequest>,
    ) -> Result<tonic::Response<ListOwnedObjectsResponse>, tonic::Status> {
        list_owned_objects::list_owned_objects(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn get_coin_info(
        &self,
        request: tonic::Request<GetCoinInfoRequest>,
    ) -> Result<tonic::Response<GetCoinInfoResponse>, tonic::Status> {
        get_coin_info::get_coin_info(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn simulate_transaction(
        &self,
        request: tonic::Request<SimulateTransactionRequest>,
    ) -> Result<tonic::Response<SimulateTransactionResponse>, tonic::Status> {
        let request = request.into_inner();
        let read_mask = FieldMaskTree::new_wildcard();
        //TODO use provided read_mask
        let parameters = crate::types::SimulateTransactionQueryParameters {
            balance_changes: false,
            input_objects: false,
            output_objects: false,
        };
        let transaction = request
            .transaction
            .as_ref()
            .ok_or_else(|| {
                FieldViolation::new("transaction").with_reason(ErrorReason::FieldMissing)
            })
            .map_err(RpcError::from)?
            .pipe(sui_sdk_types::Transaction::try_from)
            .map_err(|e| {
                FieldViolation::new("transaction")
                    .with_description(format!("invalid transaction: {e}"))
                    .with_reason(ErrorReason::FieldInvalid)
            })
            .map_err(RpcError::from)?;

        let response = self.simulate_transaction(&parameters, transaction)?;

        let balance_changes = response
            .balance_changes
            .map(|balance_changes| balance_changes.into_iter().map(Into::into).collect())
            .unwrap_or_default();
        let response = crate::proto::rpc::v2alpha::SimulateTransactionResponse {
            transaction: Some(ExecutedTransaction {
                effects: Some(TransactionEffects::merge_from(
                    &response.effects,
                    &read_mask,
                )),
                events: response
                    .events
                    .map(|events| TransactionEvents::merge_from(events, &read_mask)),
                balance_changes,
                ..Default::default()
            }),
        };
        Ok(tonic::Response::new(response))
    }

    async fn resolve_transaction(
        &self,
        request: tonic::Request<ResolveTransactionRequest>,
    ) -> Result<tonic::Response<ResolveTransactionResponse>, tonic::Status> {
        let request = request.into_inner();
        let read_mask = request.read_mask.unwrap_or_default();
        //TODO use provided read_mask
        let simulate_parameters = crate::types::SimulateTransactionQueryParameters {
            balance_changes: false,
            input_objects: false,
            output_objects: false,
        };
        let parameters = crate::types::ResolveTransactionQueryParameters {
            simulate: read_mask
                .paths
                .iter()
                .any(|path| path.starts_with("simulation")),
            simulate_transaction_parameters: simulate_parameters,
        };
        let unresolved_transaction = serde_json::from_str(
            &request.unresolved_transaction.unwrap_or_default(),
        )
        .map_err(|_| {
            tonic::Status::new(
                tonic::Code::InvalidArgument,
                "invalid unresolved_transaction",
            )
        })?;

        let response = self.resolve_transaction(parameters, unresolved_transaction)?;

        let read_mask = FieldMaskTree::new_wildcard();
        let simulation = response.simulation.map(|simulation| {
            let balance_changes = simulation
                .balance_changes
                .map(|balance_changes| balance_changes.into_iter().map(Into::into).collect())
                .unwrap_or_default();
            crate::proto::rpc::v2alpha::SimulateTransactionResponse {
                transaction: Some(ExecutedTransaction {
                    effects: Some(TransactionEffects::merge_from(
                        &simulation.effects,
                        &read_mask,
                    )),
                    events: simulation
                        .events
                        .map(|events| TransactionEvents::merge_from(events, &read_mask)),
                    balance_changes,
                    ..Default::default()
                }),
            }
        });

        let response = crate::proto::rpc::v2alpha::ResolveTransactionResponse {
            transaction: Some(Transaction::merge_from(response.transaction, &read_mask)),
            simulation,
        };

        Ok(tonic::Response::new(response))
    }
}