sui_indexer_alt_jsonrpc/api/name_service/
mod.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use jsonrpsee::{core::RpcResult, proc_macros::rpc};
5use sui_json_rpc_types::Page as PageResponse;
6use sui_open_rpc::Module;
7use sui_open_rpc_macros::open_rpc;
8use sui_types::base_types::SuiAddress;
9
10use crate::{context::Context, error::InternalContext as _};
11
12use super::rpc_module::RpcModule;
13
14use self::error::Error;
15
16mod error;
17mod response;
18
19#[open_rpc(namespace = "suix", tag = "Name Service API")]
20#[rpc(server, namespace = "suix")]
21trait NameServiceApi {
22    /// Resolve a SuiNS name to its address
23    #[method(name = "resolveNameServiceAddress")]
24    async fn resolve_name_service_address(
25        &self,
26        /// The name to resolve
27        name: String,
28    ) -> RpcResult<Option<SuiAddress>>;
29
30    /// Find the SuiNS name that points to this address.
31    ///
32    /// Although this method's response is paginated, it will only ever return at most one name.
33    #[method(name = "resolveNameServiceNames")]
34    async fn resolve_name_service_names(
35        &self,
36        /// The address to resolve
37        address: SuiAddress,
38        /// Unused pagination cursor
39        cursor: Option<String>,
40        /// Unused pagination limit
41        limit: Option<usize>,
42    ) -> RpcResult<PageResponse<String, String>>;
43}
44
45pub(crate) struct NameService(pub Context);
46
47#[async_trait::async_trait]
48impl NameServiceApiServer for NameService {
49    async fn resolve_name_service_address(&self, name: String) -> RpcResult<Option<SuiAddress>> {
50        let Self(ctx) = self;
51        Ok(response::resolved_address(ctx, &name)
52            .await
53            .with_internal_context(|| format!("Resolving SuiNS name {name:?}"))?)
54    }
55
56    async fn resolve_name_service_names(
57        &self,
58        address: SuiAddress,
59        _cursor: Option<String>,
60        _limit: Option<usize>,
61    ) -> RpcResult<PageResponse<String, String>> {
62        let Self(ctx) = self;
63
64        let mut page = PageResponse::empty();
65        if let Some(name) = response::resolved_name(ctx, address)
66            .await
67            .with_internal_context(|| format!("Resolving address {address}"))?
68        {
69            page.data.push(name);
70        }
71
72        Ok(page)
73    }
74}
75
76impl RpcModule for NameService {
77    fn schema(&self) -> Module {
78        NameServiceApiOpenRpc::module_doc()
79    }
80
81    fn into_impl(self) -> jsonrpsee::RpcModule<Self> {
82        self.into_rpc()
83    }
84}