feat: added langchain class, refactored methods to use tlb parser, wrote and ran tests

This commit is contained in:
adpthegreat
2024-12-21 20:53:00 +01:00
parent beaf061929
commit cdec8ed32d
15 changed files with 884 additions and 154 deletions

View File

@@ -1,65 +0,0 @@
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,21 @@
import { Connection, PublicKey } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { getAllTld } from "@onsol/tldparser";
/**
* 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<Array<{
tld: String;
parentAccount: PublicKey;
}>> {
try {
return getAllTld(agent.connection)
} catch (error: any) {
throw new Error(`Failed to fetch TLDs: ${error.message}`);
}
}

View File

@@ -1,7 +1,7 @@
import { getAllDomains } from "@bonfida/spl-name-service";
import { SolanaAgentKit } from "../agent";
import { PublicKey } from "@solana/web3.js";
import { getAllDomainsTLDs } from "./get_all_active_tlds";
import { getAllDomainsTLDs } from "./get_all_domains_tlds";
/**
* Get all registered domains across all TLDs
@@ -20,7 +20,7 @@ export async function getAllRegisteredAllDomains(
for (const tld of tlds) {
const domains = await getAllDomains(
agent.connection,
new PublicKey("namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX"),
new PublicKey("namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX")
);
// Add domains with TLD suffix

View File

@@ -6,16 +6,19 @@ 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 set
* @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 {
const favDomain = await _getFavoriteDomain(agent.connection, owner);
return favDomain.stale ? null : favDomain.reverse;
mainDomain = await _getFavoriteDomain(agent.connection, owner);
return mainDomain.stale ? null : mainDomain.reverse;
} catch (error: any) {
throw new Error(`Failed to fetch main domain: ${error.message}`);
console.log("No main/favorite domain found");
}
}
return null
}

View File

@@ -0,0 +1,20 @@
import { SolanaAgentKit } from "../agent";
import { PublicKey } from "@solana/web3.js";
import { NameAccountAndDomain, TldParser } from "@onsol/tldparser";
/**
* Get all domains owned domains for a specific TLD for the agent's wallet
* @param agent SolanaAgentKit instance
* @param owner - PublicKey of the owner
* @returns Promise resolving to an array of owned domains or an empty array if none are found
*/
export async function getOwnedAllDomains(
agent: SolanaAgentKit,
owner:PublicKey
): Promise<NameAccountAndDomain[]> {
try {
return new TldParser(agent.connection).getParsedAllUserDomains(owner);
} catch (error: any) {
throw new Error(`Failed to fetch owned domains: ${error.message}`);
}
}

View File

@@ -1,23 +1,25 @@
import { NameAccountAndDomain, TldParser } from "@onsol/tldparser";
import { SolanaAgentKit } from "../agent";
import { getOwnedAllDomains } from "./lookup_owner";
import { PublicKey } from "@solana/web3.js";
/**
* Get all domains owned by an address for a specific TLD
* @param agent SolanaAgentKit instance
* @param owner Owner's public key
* @param tld Top-level domain (e.g., "sol")
* @returns Array of owned domain names for the specified TLD
* @returns Promise resolving to an array of owned domain names for the specified TLD or an empty array if none are found
*/
export async function getOwnedDomainsForTLD(
agent: SolanaAgentKit,
owner: PublicKey,
tld: string
): Promise<string[]> {
): Promise<NameAccountAndDomain[]> {
try {
const allDomains = await getOwnedAllDomains(agent, owner);
return allDomains.filter(domain => domain.endsWith(`.${tld}`));
return new TldParser(agent.connection)
.getParsedAllUserDomainsFromTld(
agent.wallet_address,
tld
)
} catch (error: any) {
throw new Error(`Failed to fetch domains for TLD: ${error.message}`);
}
}

View File

@@ -17,14 +17,14 @@ export * from "./fetch_price";
export * from "./send_compressed_airdrop";
export * from "./create_orca_single_sided_whirlpool";
export * from "./get_all_active_tlds";
export * from "./get_all_domains_tlds";
export * from "./get_all_registered_all_domains";
export * from "./get_domains_on_tld";
export * from "./get_main_domain";
export * from "./lookup_owner";
export * from "./get_owned_domains_for_tld";
export * from "./get_main_all_domains_domain";
export * from "./get_owned_all_domains";
export * from "./resolve_domain";
export * from "./raydium_create_ammV4";
export * from "./raydium_create_clmm";
export * from "./raydium_create_cpmm";
export * from "./openbook_create_market";
export * from "./openbook_create_market";

View File

@@ -1,21 +0,0 @@
import { getAllDomains } from "@bonfida/spl-name-service";
import { SolanaAgentKit } from "../agent";
import { PublicKey } from "@solana/web3.js";
/**
* Get all domains owned by a specific address
* @param agent SolanaAgentKit instance
* @param owner Owner's public key
* @returns Array of owned domain names
*/
export async function getOwnedAllDomains(
agent: SolanaAgentKit,
owner: PublicKey
): Promise<string[]> {
try {
const domains = await getAllDomains(agent.connection, owner);
return domains.map(domain => domain.toString());
} catch (error: any) {
throw new Error(`Failed to fetch owned domains: ${error.message}`);
}
}

View File

@@ -1,42 +1,22 @@
import { Buffer } from "buffer";
import { PublicKey } from "@solana/web3.js";
import {
getNameAccountKeySync,
NAME_PROGRAM_ID,
} from "@bonfida/spl-name-service";
import { TldParser } from "@onsol/tldparser";
import { SolanaAgentKit } from "../index";
import { PublicKey } from "@solana/web3.js";
/**
* Resolve a domain to its public key
* Resolve all domains for a given agent and domain
* @param agent SolanaAgentKit instance
* @param domain Domain name to resolve
* @returns Associated public key or null if not found
* @returns Promise resolving to the domain or undefined if not found
*/
export async function resolveAllDomains(
agent: SolanaAgentKit,
domain: string
): Promise<PublicKey | null> {
): Promise<PublicKey | undefined> {
try {
// Convert domain name to buffer for hashing
const hashedDomain = Buffer.from(domain);
// Get the name account key using the new sync method
const nameAccountKey = getNameAccountKeySync(
hashedDomain,
undefined, // nameClass
undefined // nameParent
);
// Get the account info to retrieve the owner
const owner = await agent.connection.getAccountInfo(nameAccountKey);
if (!owner) {
return null;
}
// The owner's public key is stored in the data buffer starting at offset 32
return new PublicKey(owner.data.slice(32, 64));
let tld = new TldParser(agent.connection).getOwnerFromDomainTld(domain)
return tld;
} catch (error: any) {
throw new Error(`Domain resolution failed: ${error.message}`);
}
}