sui_rpc_api/grpc/v2/state_service/
mod.rs1use 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}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89 use crate::reader::{NotServedHereCause, assert_not_served_here};
90 use crate::stub_store::StubStore;
91
92 #[test]
95 fn every_method_is_not_served_without_indexes() {
96 let service = StubStore::new().into_service();
97 let no_rpc_indexes = NotServedHereCause::NoRpcIndexes;
98
99 assert_not_served_here(
100 get_balance::get_balance(&service, GetBalanceRequest::default()).unwrap_err(),
101 no_rpc_indexes,
102 );
103 assert_not_served_here(
104 get_coin_info::get_coin_info(&service, GetCoinInfoRequest::default()).unwrap_err(),
105 no_rpc_indexes,
106 );
107 assert_not_served_here(
108 list_balances::list_balances(&service, ListBalancesRequest::default()).unwrap_err(),
109 no_rpc_indexes,
110 );
111 assert_not_served_here(
112 list_dynamic_fields::list_dynamic_fields(&service, ListDynamicFieldsRequest::default())
113 .unwrap_err(),
114 no_rpc_indexes,
115 );
116 assert_not_served_here(
117 list_owned_objects::list_owned_objects(&service, ListOwnedObjectsRequest::default())
118 .unwrap_err(),
119 no_rpc_indexes,
120 );
121 }
122}