This commit is contained in:
Blockiosaurus
2024-12-18 19:56:04 -05:00
22 changed files with 380 additions and 46 deletions

View File

@@ -0,0 +1,37 @@
import { getPrimaryDomain as _getPrimaryDomain } from "@bonfida/spl-name-service";
import { PublicKey } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
/**
* 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) {
throw new Error(
`Failed to get primary domain for account: ${account.toBase58()}`
);
}
}

View File

@@ -0,0 +1,70 @@
import { PublicKey } from "@solana/web3.js";
import { JupiterTokenData } from "../types";
export async function getTokenDataByAddress(
mint: PublicKey,
): Promise<JupiterTokenData | undefined> {
try {
if (!mint) {
throw new Error("Mint address is required");
}
const response = await fetch("https://tokens.jup.ag/tokens?tags=verified", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const data = (await response.json()) as JupiterTokenData[];
const token = data.find((token: JupiterTokenData) => {
return token.address === mint.toBase58();
});
return token;
} catch (error: any) {
throw new Error(`Error fetching token data: ${error.message}`);
}
}
export async function getTokenAddressFromTicker(
ticker: string
): Promise<string | null> {
try {
const response = await fetch(
`https://api.dexscreener.com/latest/dex/search?q=${ticker}`
);
const data = await response.json();
if (!data.pairs || data.pairs.length === 0) {
return null;
}
// Filter for Solana pairs only and sort by FDV
let solanaPairs = data.pairs
.filter((pair: any) => pair.chainId === "solana")
.sort((a: any, b: any) => (b.fdv || 0) - (a.fdv || 0));
console.log("solanaPairs", solanaPairs);
solanaPairs = solanaPairs.filter(
(pair: any) =>
pair.baseToken.symbol.toLowerCase() === ticker.toLowerCase()
);
// Return the address of the highest FDV Solana pair
return solanaPairs[0].baseToken.address;
} catch (error) {
console.error("Error fetching token address from DexScreener:", error);
return null;
}
}
export async function getTokenDataByTicker(
ticker: string
): Promise<JupiterTokenData | undefined> {
const address = await getTokenAddressFromTicker(ticker);
if (!address) {
throw new Error(`Token address not found for ticker: ${ticker}`);
}
return getTokenDataByAddress(new PublicKey(address));
}

View File

@@ -6,8 +6,11 @@ export * from "./mint_nft";
export * from "./transfer";
export * from "./trade";
export * from "./register_domain";
export * from "./resolve_sol_domain";
export * from "./get_primary_domain";
export * from "./launch_pumpfun_token";
export * from "./lend";
export * from "./get_tps";
export * from "./get_token_data";
export * from './stake_with_jup';
export * from "./fetch_price";
export * from "./fetch_price";

View File

@@ -0,0 +1,30 @@
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) {
throw new Error(`Failed to resolve domain: ${domain}`);
}
}