Skip to main content

sui_rpc_api/grpc/v2/state_service/
mod.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::RpcService;
5use sui_rpc::proto::sui::rpc::v2::GetBalanceRequest;
6use sui_rpc::proto::sui::rpc::v2::GetBalanceResponse;
7use sui_rpc::proto::sui::rpc::v2::GetCoinInfoRequest;
8use sui_rpc::proto::sui::rpc::v2::GetCoinInfoResponse;
9use sui_rpc::proto::sui::rpc::v2::ListBalancesRequest;
10use sui_rpc::proto::sui::rpc::v2::ListBalancesResponse;
11use sui_rpc::proto::sui::rpc::v2::ListDynamicFieldsRequest;
12use sui_rpc::proto::sui::rpc::v2::ListDynamicFieldsResponse;
13use sui_rpc::proto::sui::rpc::v2::ListOwnedObjectsRequest;
14use sui_rpc::proto::sui::rpc::v2::ListOwnedObjectsResponse;
15use sui_rpc::proto::sui::rpc::v2::state_service_server::StateService;
16
17mod get_balance;
18mod get_coin_info;
19mod list_balances;
20mod list_dynamic_fields;
21mod list_owned_objects;
22
23#[tonic::async_trait]
24impl StateService for RpcService {
25    async fn list_dynamic_fields(
26        &self,
27        request: tonic::Request<ListDynamicFieldsRequest>,
28    ) -> Result<tonic::Response<ListDynamicFieldsResponse>, tonic::Status> {
29        let service = self.clone();
30        tokio::task::spawn_blocking(move || {
31            list_dynamic_fields::list_dynamic_fields(&service, request.into_inner())
32        })
33        .await
34        .map_err(|e| tonic::Status::internal(format!("list_dynamic_fields task failed: {e}")))?
35        .map(tonic::Response::new)
36        .map_err(Into::into)
37    }
38
39    async fn list_owned_objects(
40        &self,
41        request: tonic::Request<ListOwnedObjectsRequest>,
42    ) -> Result<tonic::Response<ListOwnedObjectsResponse>, tonic::Status> {
43        let service = self.clone();
44        tokio::task::spawn_blocking(move || {
45            list_owned_objects::list_owned_objects(&service, request.into_inner())
46        })
47        .await
48        .map_err(|e| tonic::Status::internal(format!("list_owned_objects task failed: {e}")))?
49        .map(tonic::Response::new)
50        .map_err(Into::into)
51    }
52
53    async fn get_coin_info(
54        &self,
55        request: tonic::Request<GetCoinInfoRequest>,
56    ) -> Result<tonic::Response<GetCoinInfoResponse>, tonic::Status> {
57        get_coin_info::get_coin_info(self, request.into_inner())
58            .map(tonic::Response::new)
59            .map_err(Into::into)
60    }
61
62    async fn get_balance(
63        &self,
64        request: tonic::Request<GetBalanceRequest>,
65    ) -> Result<tonic::Response<GetBalanceResponse>, tonic::Status> {
66        get_balance::get_balance(self, request.into_inner())
67            .map(tonic::Response::new)
68            .map_err(Into::into)
69    }
70
71    async fn list_balances(
72        &self,
73        request: tonic::Request<ListBalancesRequest>,
74    ) -> Result<tonic::Response<ListBalancesResponse>, tonic::Status> {
75        let service = self.clone();
76        tokio::task::spawn_blocking(move || {
77            list_balances::list_balances(&service, request.into_inner())
78        })
79        .await
80        .map_err(|e| tonic::Status::internal(format!("list_balances task failed: {e}")))?
81        .map(tonic::Response::new)
82        .map_err(Into::into)
83    }
84}