From 8927ac4130e9e38be48316f3015c6212bdee92bc Mon Sep 17 00:00:00 2001 From: fm2055 <48504961+fm2055@users.noreply.github.com> Date: Mon, 16 Dec 2024 03:23:17 +0000 Subject: [PATCH] feat: add get_primary_domain --- src/tools/get_primary_domain.ts | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/tools/get_primary_domain.ts diff --git a/src/tools/get_primary_domain.ts b/src/tools/get_primary_domain.ts new file mode 100644 index 0000000..1fe3892 --- /dev/null +++ b/src/tools/get_primary_domain.ts @@ -0,0 +1,37 @@ +import { getPrimaryDomain } from "@bonfida/spl-name-service"; +import { PublicKey } from "@solana/web3.js"; +import { SolanaAgentKit } from "../index"; + +/** + * Retrieves the primary .sol domain associated with a given Solana public key. + * + * This function queries the Bonfida SPL Name Service to get the primary .sol domain for + * a specified Solana public key. If the primary domain is stale or an error occurs during + * the resolution, it throws an error. + * + * @param agent SolanaAgentKit instance + * @param account The Solana public key for which to retrieve the primary domain + * @returns A promise that resolves to the primary .sol domain as a string + * @throws Error if the domain is stale or if the domain resolution fails + */ +export async function get_primary_domain( + agent: SolanaAgentKit, + account: PublicKey +): Promise { + try { + const { reverse, stale } = await getPrimaryDomain( + agent.connection, + account + ); + if (stale) { + throw new Error( + `Primary domain is stale for account: ${account.toBase58()}` + ); + } + return reverse; + } catch (error) { + throw new Error( + `Failed to get primary domain for account: ${account.toBase58()}` + ); + } +}