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

@@ -35,7 +35,7 @@ import {
} from "../tools";
import { CollectionOptions, PumpFunTokenOptions } from "../types";
import { BN } from "@coral-xyz/anchor";
import Decimal from "decimal.js";
import { NameAccountAndDomain } from "@onsol/tldparser";
/**
* Main class for interacting with Solana blockchain
@@ -194,22 +194,26 @@ export class SolanaAgentKit {
);
}
async resolveAllDomains(domain: string): Promise<PublicKey | null> {
async resolveAllDomains(domain: string): Promise<PublicKey | undefined> {
return resolveAllDomains(this, domain);
}
async getOwnedAllDomains(owner: PublicKey): Promise<string[]> {
async getOwnedAllDomains(
owner: PublicKey
): Promise<NameAccountAndDomain[]> {
return getOwnedAllDomains(this, owner);
}
async getOwnedDomainsForTLD(
owner: PublicKey,
tld: string
): Promise<string[]> {
return getOwnedDomainsForTLD(this, owner, tld);
):Promise<NameAccountAndDomain[]> {
return getOwnedDomainsForTLD(this, tld);
}
async getAllDomainsTLDs(): Promise<string[]> {
async getAllDomainsTLDs(): Promise<Array<{
tld: String;
parentAccount: PublicKey;
}>> {
return getAllDomainsTLDs(this);
}
@@ -227,7 +231,7 @@ export class SolanaAgentKit {
baseAmount: BN,
quoteAmount: BN,
startTime: BN,
startTime: BN
) {
return raydiumCreateAmmV4(
this,
@@ -236,8 +240,8 @@ export class SolanaAgentKit {
baseAmount,
quoteAmount,
startTime,
)
startTime
);
}
async raydiumCreateClmm(
@@ -247,7 +251,7 @@ export class SolanaAgentKit {
configId: PublicKey,
initialPrice: Decimal,
startTime: BN,
startTime: BN
) {
return raydiumCreateClmm(
this,
@@ -258,8 +262,8 @@ export class SolanaAgentKit {
configId,
initialPrice,
startTime,
)
startTime
);
}
async raydiumCreateCpmm(
@@ -271,7 +275,7 @@ export class SolanaAgentKit {
mintAAmount: BN,
mintBAmount: BN,
startTime: BN,
startTime: BN
) {
return raydiumCreateCpmm(
this,
@@ -284,8 +288,8 @@ export class SolanaAgentKit {
mintAAmount,
mintBAmount,
startTime,
)
startTime
);
}
async openbookCreateMarket(
@@ -293,7 +297,7 @@ export class SolanaAgentKit {
quoteMint: PublicKey,
lotSize: number = 1,
tickSize: number = 0.01,
tickSize: number = 0.01
) {
return openbookCreateMarket(
this,
@@ -301,7 +305,7 @@ export class SolanaAgentKit {
quoteMint,
lotSize,
tickSize,
)
tickSize
);
}
}

View File

@@ -980,6 +980,189 @@ export class SolanaOpenbookCreateMarket extends Tool {
}
}
export class SolanaResolveDomain extends Tool {
name = "solana_resolve_domain";
description = `Resolve a Solana domain name to its owner's public key
Inputs (input is a json string):
domain: string (required) - The domain name to resolve (e.g., "mydomain.sol")
`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
let inputFormat = JSON.parse(input);
const owner = await this.solanaKit.resolveAllDomains(inputFormat.domain);
return JSON.stringify({
status: "success",
message: "Domain resolved successfully",
owner: owner?.toString(),
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "DOMAIN_RESOLUTION_ERROR",
});
}
}
}
export class SolanaGetOwnedDomains extends Tool {
name = "solana_get_owned_domains";
description = `Get all domains owned by a specific wallet address
Inputs (input is a json string):
owner: string (required) - The public key of the domain owner
`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
let inputFormat = JSON.parse(input);
const ownerPubkey = new PublicKey(inputFormat.owner);
const domains = await this.solanaKit.getOwnedAllDomains(ownerPubkey);
return JSON.stringify({
status: "success",
message: "Owned domains fetched successfully",
domains: domains,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "FETCH_OWNED_DOMAINS_ERROR",
});
}
}
}
export class SolanaGetOwnedTldDomains extends Tool {
name = "solana_get_owned_tld_domains";
description = `Get all domains owned by the agent's wallet for a specific TLD
Inputs (input is a json string):
tld: string (required) - The top-level domain to search (e.g., "sol")
`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
let inputFormat = JSON.parse(input);
const domains = await this.solanaKit.getOwnedDomainsForTLD(
inputFormat.tld
);
return JSON.stringify({
status: "success",
message: "TLD domains fetched successfully",
domains: domains,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "FETCH_TLD_DOMAINS_ERROR",
});
}
}
}
export class SolanaGetAllTlds extends Tool {
name = "solana_get_all_tlds";
description = `Get all active top-level domains (TLDs) in the Solana name service
Inputs (input is a json string): {}
No additional parameters required`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
const tlds = await this.solanaKit.getAllDomainsTLDs();
return JSON.stringify({
status: "success",
message: "TLDs fetched successfully",
tlds: tlds,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "FETCH_TLDS_ERROR",
});
}
}
}
export class SolanaGetAllRegisteredDomains extends Tool {
name = "solana_get_all_registered_domains";
description = `Get all registered domains across all TLDs in the Solana name service
Inputs (input is a json string): {}
No additional parameters required`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
const domains = await this.solanaKit.getAllRegisteredAllDomains();
return JSON.stringify({
status: "success",
message: "All registered domains fetched successfully",
domains: domains,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "FETCH_ALL_DOMAINS_ERROR",
});
}
}
}
export class SolanaGetMainDomain extends Tool {
name = "solana_get_main_domain";
description = `Get the user's main/favorite domain
Inputs (input is a json string):
owner: string (required) - The public key of the domain owner
`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
let inputFormat = JSON.parse(input);
const ownerPubkey = new PublicKey(inputFormat.owner);
const mainDomain = await this.solanaKit.getMainAllDomainsDomain(
ownerPubkey
);
return JSON.stringify({
status: "success",
message: "Main domain fetched successfully",
domain: mainDomain,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "FETCH_MAIN_DOMAIN_ERROR",
});
}
}
}
export function createSolanaTools(solanaKit: SolanaAgentKit) {
return [
new SolanaBalanceTool(solanaKit),
@@ -1007,5 +1190,11 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
new SolanaRaydiumCreateCpmm(solanaKit),
new SolanaOpenbookCreateMarket(solanaKit),
new SolanaCreateSingleSidedWhirlpoolTool(solanaKit),
new SolanaResolveDomain(solanaKit),
new SolanaGetOwnedDomains(solanaKit),
new SolanaGetOwnedTldDomains(solanaKit),
new SolanaGetAllTlds(solanaKit),
new SolanaGetAllRegisteredDomains(solanaKit),
new SolanaGetMainDomain(solanaKit),
];
}

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}`);
}
}