feat:updated domains methods, added fixes

This commit is contained in:
adpthegreat
2024-12-20 20:12:16 +01:00
parent 3a29794ea9
commit 32fef1da8f
3 changed files with 102 additions and 1 deletions

View File

@@ -0,0 +1,65 @@
import {
NameRegistryState,
getAllDomains,
ROOT_DOMAIN_ACCOUNT,
} from "@bonfida/spl-name-service";
import { Connection, PublicKey } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
/**
* Get all active top-level domains (TLDs) in the Solana name service
* @param agent SolanaAgentKit instance
* @returns Array of active TLD strings
*/
export async function getAllDomainsTLDs(
agent: SolanaAgentKit
): Promise<string[]> {
try {
// Get the root domain record
const rootAccount = await NameRegistryState.retrieve(
agent.connection,
ROOT_DOMAIN_ACCOUNT
);
if (!rootAccount) {
throw new Error("Root domain account not found");
}
// Fetch all TLD records under the root domain
const tldAccounts = await agent.connection.getProgramAccounts(
new PublicKey("namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX"),
{
filters: [
{
memcmp: {
offset: 0,
bytes: rootAccount.registry.owner.toBase58(),
},
},
],
}
);
// Parse the TLD names from the accounts
const tlds: string[] = [];
for (const account of tldAccounts) {
const registry = await NameRegistryState.retrieve(
agent.connection,
account.pubkey
);
if (registry && registry.registry.data) {
const tldName = Buffer.from(registry.registry.data)
.toString()
.replace(/\0/g, "");
if (tldName) {
tlds.push(tldName);
}
}
}
return tlds;
} catch (error: any) {
throw new Error(`Failed to fetch TLDs: ${error.message}`);
}
}

View 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 "./get_all_active_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}`);
}
}

View File

@@ -14,7 +14,7 @@ export async function getOwnedAllDomains(
): Promise<string[]> {
try {
const domains = await getAllDomains(agent.connection, owner);
return domains.map(domain => domain.name);
return domains.map(domain => domain.toString());
} catch (error: any) {
throw new Error(`Failed to fetch owned domains: ${error.message}`);
}