getRegistrationForSubdomainName method

Future<(dynamic, dynamic)?> getRegistrationForSubdomainName(
  1. String domain,
  2. String subdomain
)

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

Implementation

Future<(dynamic target, dynamic expirationTimestampSeconds)?> getRegistrationForSubdomainName(String domain, String subdomain) async {
  if (!nameComponentPattern.hasMatch(domain)) return null;
  if (!nameComponentPattern.hasMatch(subdomain)) 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": [subdomain] },
      "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;
  }
}