diff --git a/src/agent/index.ts b/src/agent/index.ts index 02c4cba..17f0152 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -9,6 +9,8 @@ import { transfer, trade, registerDomain, + resolveSolDomain, + getPrimaryDomain, launchPumpFunToken, lendAsset, getTPS, @@ -81,6 +83,14 @@ export class SolanaAgentKit { return registerDomain(this, name, spaceKB); } + async resolveSolDomain(domain:string ){ + return resolveSolDomain(this, domain) + } + + async getPrimaryDomain(account: PublicKey){ + return getPrimaryDomain(this, account) + } + async trade( outputMint: PublicKey, inputAmount: number, diff --git a/src/langchain/index.ts b/src/langchain/index.ts index e3cd3dc..8fa356d 100644 --- a/src/langchain/index.ts +++ b/src/langchain/index.ts @@ -392,6 +392,72 @@ export class SolanaRegisterDomainTool extends Tool { } } +export class SolanaResolveDomainTool extends Tool { + name = "solana_resolve_domain"; + description = `Resolve a .sol domain to a Solana PublicKey. + + Inputs: + domain: string, eg "pumpfun.sol" or "pumpfun"(required) + `; + + constructor(private solanaKit: SolanaAgentKit) { + super(); + } + + protected async _call(input: string): Promise { + try { + const domain = input.trim(); + const publicKey = await this.solanaKit.resolveSolDomain(domain); + + return JSON.stringify({ + status: "success", + message: "Domain resolved successfully", + publicKey: publicKey.toBase58(), + }); + } catch (error: any) { + return JSON.stringify({ + status: "error", + message: error.message, + code: error.code || "UNKNOWN_ERROR", + }); + } + } +} + + +export class SolanaGetDomainTool extends Tool { + name = "solana_get_domain"; + description = `Retrieve the .sol domain associated for a given account address. + + Inputs: + account: string, eg "4Be9CvxqHW6BYiRAxW9Q3xu1ycTMWaL5z8NX4HR3ha7t" (required) + `; + + constructor(private solanaKit: SolanaAgentKit) { + super(); + } + + + protected async _call(input: string): Promise { + try { + const account = new PublicKey(input.trim()); + const domain = await this.solanaKit.getPrimaryDomain(account); + + return JSON.stringify({ + status: "success", + message: "Primary domain retrieved successfully", + domain, + }); + } catch (error: any) { + return JSON.stringify({ + status: "error", + message: error.message, + code: error.code || "UNKNOWN_ERROR", + }); + } + } +} + export class SolanaGetWalletAddressTool extends Tool { name = "solana_get_wallet_address"; description = `Get the wallet address of the agent`; @@ -712,6 +778,8 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) { new SolanaTPSCalculatorTool(solanaKit), new SolanaStakeTool(solanaKit), new SolanaFetchPriceTool(solanaKit), + new SolanaResolveDomainTool(solanaKit), + new SolanaGetDomainTool(solanaKit), new SolanaTokenDataTool(solanaKit), new SolanaTokenDataByTickerTool(solanaKit), ]; diff --git a/src/tools/get_primary_domain.ts b/src/tools/get_primary_domain.ts new file mode 100644 index 0000000..775af4b --- /dev/null +++ b/src/tools/get_primary_domain.ts @@ -0,0 +1,37 @@ +import { getPrimaryDomain as _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 getPrimaryDomain( + 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()}` + ); + } +} diff --git a/src/tools/index.ts b/src/tools/index.ts index 9436e66..57764b8 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -6,6 +6,8 @@ export * from "./mint_nft"; export * from "./transfer"; export * from "./trade"; export * from "./register_domain"; +export * from "./resolve_sol_domain"; +export * from "./get_primary_domain"; export * from "./launch_pumpfun_token"; export * from "./lend"; export * from "./get_tps"; diff --git a/src/tools/resolve_sol_domain.ts b/src/tools/resolve_sol_domain.ts new file mode 100644 index 0000000..d8764cc --- /dev/null +++ b/src/tools/resolve_sol_domain.ts @@ -0,0 +1,30 @@ +import { resolve } from "@bonfida/spl-name-service"; +import { PublicKey } from "@solana/web3.js"; +import { SolanaAgentKit } from "../index"; + +/** + * Resolves a .sol domain to a Solana PublicKey. + * + * This function uses the Bonfida SPL Name Service to resolve a given .sol domain + * to the corresponding Solana PublicKey. The domain can be provided with or without + * the .sol suffix. + * + * @param agent SolanaAgentKit instance + * @param domain The .sol domain to resolve. This can be provided with or without the .sol TLD suffix + * @returns A promise that resolves to the corresponding Solana PublicKey + * @throws Error if the domain resolution fails + */ +export async function resolveSolDomain( + agent: SolanaAgentKit, + domain: string +): Promise { + if (!domain || typeof domain !== "string") { + throw new Error("Invalid domain. Expected a non-empty string."); + } + + try { + return await resolve(agent.connection, domain); + } catch (error) { + throw new Error(`Failed to resolve domain: ${domain}`); + } +}