Skip to content

Commit a3b9cb9

Browse files
chore: Use JSON RPC APIs for fetching data
fix order
1 parent 1f31742 commit a3b9cb9

File tree

3 files changed

+60
-61
lines changed

3 files changed

+60
-61
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/iota/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ iota-graphql-rpc = { workspace = true, optional = true }
6969
iota-graphql-rpc-client = { workspace = true, optional = true }
7070
iota-indexer = { workspace = true, optional = true }
7171
iota-json.workspace = true
72+
iota-json-rpc-api.workspace = true
7273
iota-json-rpc-types.workspace = true
7374
iota-keys.workspace = true
7475
iota-move.workspace = true

crates/iota/src/name_commands.rs

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use std::{
5+
collections::BTreeMap,
56
str::FromStr,
67
time::{Duration, SystemTime, UNIX_EPOCH},
78
};
89

10+
use anyhow::anyhow;
911
use chrono::{Utc, prelude::DateTime};
1012
use clap::Parser;
1113
use iota_graphql_rpc_client::simple_client::{GraphqlQueryVariable, SimpleClient};
1214
use iota_json::IotaJsonValue;
15+
use iota_json_rpc_api::IndexerApiClient;
1316
use iota_json_rpc_types::{
1417
IotaData, IotaObjectDataFilter, IotaObjectDataOptions, IotaObjectResponse,
1518
IotaObjectResponseQuery,
1619
};
1720
use iota_names::{
18-
IotaNamesNft, IotaNamesRegistration, SubdomainRegistration,
19-
config::IotaNamesConfig,
21+
IotaNamesNft, IotaNamesRegistration, SubdomainRegistration, config::IotaNamesConfig,
2022
domain::Domain,
21-
registry::{RegistryEntry, ReverseRegistryEntry},
2223
};
2324
use iota_protocol_config::Chain;
2425
use iota_sdk::{IotaClient, wallet_context::WalletContext};
@@ -327,37 +328,60 @@ impl NameCommand {
327328
)
328329
}
329330
Self::GetUserData { domain, key } => {
330-
let entry = get_registry_entry(&domain, context).await?;
331+
let client = context.get_client().await?;
332+
let mut res = client
333+
.http()
334+
.iota_names_lookup(&domain.to_string())
335+
.await?
336+
.ok_or_else(|| anyhow!("no record found for {domain}"))?
337+
.data;
331338

332339
if let Some(key) = key {
333-
let Some(value) = entry
334-
.name_record
335-
.data
336-
.contents
337-
.into_iter()
338-
.find(|entry| entry.key == key)
339-
else {
340+
if let Some((key, value)) = res.remove_entry(&key) {
341+
res = BTreeMap::new();
342+
res.insert(key, value);
343+
} else {
340344
anyhow::bail!("no value found for key `{key}`");
341-
};
342-
NameCommandResult::UserData(VecMap {
343-
contents: vec![value],
344-
})
345-
} else {
346-
NameCommandResult::UserData(entry.name_record.data)
345+
}
347346
}
347+
NameCommandResult::UserData(res)
348348
}
349349
Self::List { address } => {
350-
let mut nfts = get_owned_nfts::<IotaNamesRegistration>(address, context).await?;
351-
let subdomain_nfts =
352-
get_owned_nfts::<SubdomainRegistration>(address, context).await?;
353-
nfts.extend(subdomain_nfts.into_iter().map(|nft| nft.into_inner()));
350+
let client = context.get_client().await?;
351+
let address = get_identity_address(address.map(KeyIdentity::Address), context)?;
352+
let mut cursor = None;
353+
let mut nfts = Vec::new();
354+
loop {
355+
let res = client
356+
.http()
357+
.iota_names_find_all_registration_nfts(
358+
address,
359+
cursor,
360+
None,
361+
Some(IotaObjectDataOptions::new().with_bcs()),
362+
)
363+
.await?;
364+
for obj in res.data {
365+
nfts.push(deserialize_move_object_from_bcs(obj)?);
366+
}
367+
if res.has_next_page {
368+
cursor = res.next_cursor;
369+
} else {
370+
break;
371+
}
372+
}
354373
NameCommandResult::List(nfts)
355374
}
356375
Self::Lookup { domain } => {
357-
let entry = get_registry_entry(&domain, context).await?;
376+
let client = context.get_client().await?;
377+
let res = client
378+
.http()
379+
.iota_names_lookup(&domain.to_string())
380+
.await?
381+
.ok_or_else(|| anyhow!("no record found for {domain}"))?;
358382
NameCommandResult::Lookup {
359383
domain,
360-
target_address: entry.name_record.target_address,
384+
target_address: res.target_address,
361385
}
362386
}
363387
Self::Register { domain, coin, opts } => {
@@ -458,13 +482,16 @@ impl NameCommand {
458482
)
459483
}
460484
Self::ReverseLookup { address } => {
485+
let client = context.get_client().await?;
461486
let address = get_identity_address(address.map(KeyIdentity::Address), context)?;
462-
let entry = get_reverse_registry_entry(address, context).await?;
487+
let domain = client
488+
.http()
489+
.iota_names_reverse_lookup(address)
490+
.await?
491+
.map(|s| s.parse())
492+
.transpose()?;
463493

464-
NameCommandResult::ReverseLookup {
465-
address,
466-
domain: entry.map(|e| e.domain),
467-
}
494+
NameCommandResult::ReverseLookup { address, domain }
468495
}
469496
Self::SetReverseLookup { domain, opts } => {
470497
// Check ownership of the name off-chain to avoid potentially wasting gas
@@ -887,7 +914,7 @@ pub enum NameCommandResult {
887914
address: IotaAddress,
888915
domain: Option<Domain>,
889916
},
890-
UserData(VecMap<String, String>),
917+
UserData(BTreeMap<String, String>),
891918
List(Vec<IotaNamesRegistration>),
892919
}
893920

@@ -940,8 +967,8 @@ impl std::fmt::Display for NameCommandResult {
940967
let mut table_builder = TableBuilder::default();
941968
table_builder.set_header(["key", "value"]);
942969

943-
for entry in &entries.contents {
944-
table_builder.push_record([&entry.key, &entry.value]);
970+
for (key, value) in entries {
971+
table_builder.push_record([key, value]);
945972
}
946973

947974
let mut table = table_builder.build();
@@ -1101,36 +1128,6 @@ async fn get_proxy_nft_by_name(
11011128
})
11021129
}
11031130

1104-
async fn get_registry_entry(
1105-
domain: &Domain,
1106-
context: &mut WalletContext,
1107-
) -> anyhow::Result<RegistryEntry> {
1108-
let client = context.get_client().await?;
1109-
let iota_names_config = get_iota_names_config(&client).await?;
1110-
let object_id = iota_names_config.record_field_id(domain);
1111-
1112-
get_object_from_bcs(&client, object_id).await
1113-
}
1114-
1115-
async fn get_reverse_registry_entry(
1116-
address: IotaAddress,
1117-
context: &mut WalletContext,
1118-
) -> anyhow::Result<Option<ReverseRegistryEntry>> {
1119-
let client = context.get_client().await?;
1120-
let iota_names_config = get_iota_names_config(&client).await?;
1121-
let object_id = iota_names_config.reverse_record_field_id(&address);
1122-
let response = client
1123-
.read_api()
1124-
.get_object_with_options(object_id, IotaObjectDataOptions::new().with_bcs())
1125-
.await?;
1126-
1127-
if response.data.is_some() {
1128-
Ok(Some(deserialize_move_object_from_bcs(response)?))
1129-
} else {
1130-
Ok(None)
1131-
}
1132-
}
1133-
11341131
async fn get_iota_names_config(client: &IotaClient) -> anyhow::Result<IotaNamesConfig> {
11351132
let chain_identifier = client.read_api().get_chain_identifier().await?;
11361133
let chain = ChainIdentifier::from_chain_short_id(&chain_identifier)

0 commit comments

Comments
 (0)