getRegistrationForDomainName method

Future<(String?, int)?> getRegistrationForDomainName(
  1. String domain
)

e.g. if name is aptos.apt, domain = aptos

Implementation

Future<(String? target, int expirationTimestampSeconds)?> getRegistrationForDomainName(String domain) async {
  if (!nameComponentPattern.hasMatch(domain)) return null;
  final ansResource = await aptosClient.getAccountResource(
    contractAddress,
    "$contractAddress::domains::NameRegistryV1",
  );

  final handle = ansResource["data"]["registry"]["handle"];
  final domainsTableItemRequest = TableItem(
    "$contractAddress::domains::NameRecordKeyV1",
    "$contractAddress::domains::NameRecordV1",
    {
      "subdomain_name": { "vec": [] },
      "domain_name": domain,
    }
  );

  try {
    final item = await aptosClient.queryTableItem(handle, domainsTableItemRequest);
    final vec = item["target_address"]["vec"] as List;
    return (
      vec.isNotEmpty ? vec.first.toString() : null,
      int.parse(item["expiration_time_sec"].toString())
    );
  } catch (e) {
    // response is 404 error - meaning item not found
    dynamic err = e;
    if (err.response?.statusCode == 404) {
      return null;
    }
    rethrow;
  }
}