mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-06-02 15:11:29 +00:00
Updated protocol name instead of library name
This commit is contained in:
36
src/tools/sns/get_all_registered_all_domains.ts
Normal file
36
src/tools/sns/get_all_registered_all_domains.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { getAllDomains } from "@bonfida/spl-name-service";
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
import { getAllDomainsTLDs } from "../alldomains/get_all_domains_tlds";
|
||||
|
||||
/**
|
||||
* Get all registered domains across all TLDs
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @returns Array of all registered domain names with their TLDs
|
||||
*/
|
||||
export async function getAllRegisteredAllDomains(
|
||||
agent: SolanaAgentKit,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
// First get all TLDs
|
||||
const tlds = await getAllDomainsTLDs(agent);
|
||||
const allDomains: string[] = [];
|
||||
|
||||
// For each TLD, fetch all registered domains
|
||||
for (const tld of tlds) {
|
||||
const domains = await getAllDomains(
|
||||
agent.connection,
|
||||
new PublicKey("namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX"),
|
||||
);
|
||||
|
||||
// Add domains with TLD suffix
|
||||
domains.forEach((domain) => {
|
||||
allDomains.push(`${domain}.${tld}`);
|
||||
});
|
||||
}
|
||||
|
||||
return allDomains;
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to fetch all registered domains: ${error.message}`);
|
||||
}
|
||||
}
|
||||
22
src/tools/sns/get_main_all_domains_domain.ts
Normal file
22
src/tools/sns/get_main_all_domains_domain.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { getFavoriteDomain as _getFavoriteDomain } from "@bonfida/spl-name-service";
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
|
||||
/**
|
||||
* Get the user's main/favorite domain for a SolanaAgentKit instance
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param owner Owner's public key
|
||||
* @returns Promise resolving to the main domain name or null if not found
|
||||
*/
|
||||
export async function getMainAllDomainsDomain(
|
||||
agent: any,
|
||||
owner: PublicKey,
|
||||
): Promise<string | null> {
|
||||
let mainDomain = null;
|
||||
try {
|
||||
mainDomain = await _getFavoriteDomain(agent.connection, owner);
|
||||
return mainDomain.stale ? null : mainDomain.reverse;
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
38
src/tools/sns/get_primary_domain.ts
Normal file
38
src/tools/sns/get_primary_domain.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { getPrimaryDomain as _getPrimaryDomain } from "@bonfida/spl-name-service";
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
|
||||
/**
|
||||
* 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<string> {
|
||||
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: any) {
|
||||
console.error(error);
|
||||
throw new Error(
|
||||
`Failed to get primary domain for account: ${account.toBase58()}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
5
src/tools/sns/index.ts
Normal file
5
src/tools/sns/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from "./get_all_registered_all_domains";
|
||||
export * from "./get_main_all_domains_domain";
|
||||
export * from "./get_primary_domain";
|
||||
export * from "./register_domain";
|
||||
export * from "./resolve_sol_domain";
|
||||
58
src/tools/sns/register_domain.ts
Normal file
58
src/tools/sns/register_domain.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { registerDomainNameV2 } from "@bonfida/spl-name-service";
|
||||
import { Transaction } from "@solana/web3.js";
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
import { getAssociatedTokenAddressSync } from "@solana/spl-token";
|
||||
import { TOKENS } from "../../constants";
|
||||
|
||||
/**
|
||||
* Register a .sol domain name using Bonfida Name Service
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param name Domain name to register (without .sol)
|
||||
* @param spaceKB Space allocation in KB (max 10KB)
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
export async function registerDomain(
|
||||
agent: SolanaAgentKit,
|
||||
name: string,
|
||||
spaceKB: number = 1,
|
||||
): Promise<string> {
|
||||
try {
|
||||
// Validate space size
|
||||
if (spaceKB > 10) {
|
||||
throw new Error("Maximum domain size is 10KB");
|
||||
}
|
||||
|
||||
// Convert KB to bytes
|
||||
const space = spaceKB * 1_000;
|
||||
|
||||
const buyerTokenAccount = await getAssociatedTokenAddressSync(
|
||||
agent.wallet_address,
|
||||
TOKENS.USDC,
|
||||
);
|
||||
|
||||
// Create registration instruction
|
||||
const instruction = await registerDomainNameV2(
|
||||
agent.connection,
|
||||
name,
|
||||
space,
|
||||
agent.wallet_address,
|
||||
buyerTokenAccount,
|
||||
);
|
||||
|
||||
// Create and sign transaction
|
||||
const transaction = new Transaction().add(...instruction);
|
||||
transaction.recentBlockhash = (
|
||||
await agent.connection.getLatestBlockhash()
|
||||
).blockhash;
|
||||
transaction.feePayer = agent.wallet_address;
|
||||
|
||||
// Sign and send transaction
|
||||
const signature = await agent.connection.sendTransaction(transaction, [
|
||||
agent.wallet,
|
||||
]);
|
||||
|
||||
return signature;
|
||||
} catch (error: any) {
|
||||
throw new Error(`Domain registration failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
31
src/tools/sns/resolve_sol_domain.ts
Normal file
31
src/tools/sns/resolve_sol_domain.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
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<PublicKey> {
|
||||
if (!domain || typeof domain !== "string") {
|
||||
throw new Error("Invalid domain. Expected a non-empty string.");
|
||||
}
|
||||
|
||||
try {
|
||||
return await resolve(agent.connection, domain);
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
throw new Error(`Failed to resolve domain: ${domain}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user