mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-22 15:10:43 +00:00
feat: added langchain class, refactored methods to use tlb parser, wrote and ran tests
This commit is contained in:
@@ -43,6 +43,7 @@ import {
|
||||
PumpFunTokenOptions,
|
||||
} from "../types";
|
||||
import { BN } from "@coral-xyz/anchor";
|
||||
import { NameAccountAndDomain } from "@onsol/tldparser";
|
||||
|
||||
/**
|
||||
* Main class for interacting with Solana blockchain
|
||||
@@ -211,22 +212,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);
|
||||
}
|
||||
|
||||
@@ -242,7 +247,7 @@ export class SolanaAgentKit {
|
||||
marketId: PublicKey,
|
||||
baseAmount: BN,
|
||||
quoteAmount: BN,
|
||||
startTime: BN,
|
||||
startTime: BN
|
||||
): Promise<string> {
|
||||
return raydiumCreateAmmV4(
|
||||
this,
|
||||
@@ -251,8 +256,8 @@ export class SolanaAgentKit {
|
||||
baseAmount,
|
||||
quoteAmount,
|
||||
|
||||
startTime,
|
||||
);
|
||||
startTime
|
||||
);;
|
||||
}
|
||||
|
||||
async raydiumCreateClmm(
|
||||
@@ -260,7 +265,7 @@ export class SolanaAgentKit {
|
||||
mint2: PublicKey,
|
||||
configId: PublicKey,
|
||||
initialPrice: Decimal,
|
||||
startTime: BN,
|
||||
startTime: BN
|
||||
): Promise<string> {
|
||||
return raydiumCreateClmm(
|
||||
this,
|
||||
@@ -278,7 +283,7 @@ export class SolanaAgentKit {
|
||||
configId: PublicKey,
|
||||
mintAAmount: BN,
|
||||
mintBAmount: BN,
|
||||
startTime: BN,
|
||||
startTime: BN
|
||||
): Promise<string> {
|
||||
return raydiumCreateCpmm(
|
||||
this,
|
||||
@@ -287,7 +292,8 @@ export class SolanaAgentKit {
|
||||
configId,
|
||||
mintAAmount,
|
||||
mintBAmount,
|
||||
startTime,
|
||||
|
||||
startTime
|
||||
);
|
||||
}
|
||||
|
||||
@@ -295,7 +301,7 @@ export class SolanaAgentKit {
|
||||
baseMint: PublicKey,
|
||||
quoteMint: PublicKey,
|
||||
lotSize: number = 1,
|
||||
tickSize: number = 0.01,
|
||||
tickSize: number = 0.01
|
||||
): Promise<string[]> {
|
||||
return openbookCreateMarket(
|
||||
this,
|
||||
|
||||
@@ -1040,6 +1040,12 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaOpenbookCreateMarket(solanaKit),
|
||||
new SolanaCreateSingleSidedWhirlpoolTool(solanaKit),
|
||||
new SolanaPythFetchPrice(solanaKit),
|
||||
new SolanaResolveDomain(solanaKit),
|
||||
new SolanaGetOwnedDomains(solanaKit),
|
||||
new SolanaGetOwnedTldDomains(solanaKit),
|
||||
new SolanaGetAllTlds(solanaKit),
|
||||
new SolanaGetAllRegisteredDomains(solanaKit),
|
||||
new SolanaGetMainDomain(solanaKit),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
}
|
||||
21
src/tools/get_all_domains_tlds.ts
Normal file
21
src/tools/get_all_domains_tlds.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
20
src/tools/get_owned_all_domains.ts
Normal file
20
src/tools/get_owned_all_domains.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
@@ -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}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,11 @@ export * from "./stake_with_jup";
|
||||
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";
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
}
|
||||
@@ -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}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user