mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-16 15:10:26 +00:00
Merge branch 'main' into manifest
This commit is contained in:
@@ -2,6 +2,7 @@ import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
||||
import bs58 from "bs58";
|
||||
import Decimal from "decimal.js";
|
||||
import { DEFAULT_OPTIONS } from "../constants";
|
||||
import { Config } from "../types";
|
||||
import {
|
||||
deploy_collection,
|
||||
deploy_token,
|
||||
@@ -47,6 +48,8 @@ import {
|
||||
orcaFetchPositions,
|
||||
rock_paper_scissor,
|
||||
create_TipLink,
|
||||
listNFTForSale,
|
||||
cancelListing,
|
||||
} from "../tools";
|
||||
|
||||
import {
|
||||
@@ -73,17 +76,17 @@ export class SolanaAgentKit {
|
||||
public connection: Connection;
|
||||
public wallet: Keypair;
|
||||
public wallet_address: PublicKey;
|
||||
public openai_api_key: string | null;
|
||||
public config: Config;
|
||||
|
||||
constructor(
|
||||
private_key: string,
|
||||
rpc_url = "https://api.mainnet-beta.solana.com",
|
||||
openai_api_key: string | null = null,
|
||||
config: Config,
|
||||
) {
|
||||
this.connection = new Connection(rpc_url);
|
||||
this.wallet = Keypair.fromSecretKey(bs58.decode(private_key));
|
||||
this.wallet_address = this.wallet.publicKey;
|
||||
this.openai_api_key = openai_api_key;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
// Tool methods
|
||||
@@ -316,8 +319,7 @@ export class SolanaAgentKit {
|
||||
return getOwnedDomainsForTLD(this, tld);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
async getAllDomainsTLDs(): Promise<String[]> {
|
||||
async getAllDomainsTLDs(): Promise<string[]> {
|
||||
return getAllDomainsTLDs(this);
|
||||
}
|
||||
|
||||
@@ -440,4 +442,12 @@ export class SolanaAgentKit {
|
||||
async createTiplink(amount: number, splmintAddress?: PublicKey) {
|
||||
return create_TipLink(this, amount, splmintAddress);
|
||||
}
|
||||
|
||||
async tensorListNFT(nftMint: PublicKey, price: number): Promise<string> {
|
||||
return listNFTForSale(this, nftMint, price);
|
||||
}
|
||||
|
||||
async tensorCancelListing(nftMint: PublicKey): Promise<string> {
|
||||
return cancelListing(this, nftMint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,12 @@ export const TOKENS = {
|
||||
export const DEFAULT_OPTIONS = {
|
||||
SLIPPAGE_BPS: 300,
|
||||
TOKEN_DECIMALS: 9,
|
||||
RERERRAL_FEE: 200,
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Jupiter API URL
|
||||
*/
|
||||
export const JUP_API = "https://quote-api.jup.ag/v6";
|
||||
export const JUP_REFERRAL_ADDRESS =
|
||||
"REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3";
|
||||
|
||||
@@ -46,7 +46,7 @@ export class SolanaBalanceTool extends Tool {
|
||||
|
||||
export class SolanaBalanceOtherTool extends Tool {
|
||||
name = "solana_balance_other";
|
||||
description = `Get the balance of a Solana wallet or token account different from the agent's wallet.
|
||||
description = `Get the balance of a Solana wallet or token account which is different from the agent's wallet.
|
||||
|
||||
If no tokenAddress is provided, the SOL balance of the wallet will be returned.
|
||||
|
||||
@@ -904,7 +904,7 @@ export class SolanaCompressedAirdropTool extends Tool {
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaClosePostition extends Tool {
|
||||
export class SolanaClosePosition extends Tool {
|
||||
name = "orca_close_position";
|
||||
description = `Closes an existing liquidity position in an Orca Whirlpool. This function fetches the position
|
||||
details using the provided mint address and closes the position with a 1% slippage.
|
||||
@@ -1186,7 +1186,7 @@ export class SolanaOrcaOpenSingleSidedPosition extends Tool {
|
||||
|
||||
export class SolanaRaydiumCreateAmmV4 extends Tool {
|
||||
name = "raydium_create_ammV4";
|
||||
description = `Raydium's Legacy AMM that requiers an OpenBook marketID
|
||||
description = `Raydium's Legacy AMM that requires an OpenBook marketID
|
||||
|
||||
Inputs (input is a json string):
|
||||
marketId: string (required)
|
||||
@@ -1212,7 +1212,7 @@ export class SolanaRaydiumCreateAmmV4 extends Tool {
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Create raydium amm v4 pool successfully",
|
||||
message: "Raydium amm v4 pool created successfully",
|
||||
transaction: tx,
|
||||
});
|
||||
} catch (error: any) {
|
||||
@@ -1257,7 +1257,7 @@ export class SolanaRaydiumCreateClmm extends Tool {
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Create raydium clmm pool successfully",
|
||||
message: "Raydium clmm pool created successfully",
|
||||
transaction: tx,
|
||||
});
|
||||
} catch (error: any) {
|
||||
@@ -1305,7 +1305,7 @@ export class SolanaRaydiumCreateCpmm extends Tool {
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Create raydium cpmm pool successfully",
|
||||
message: "Raydium cpmm pool created successfully",
|
||||
transaction: tx,
|
||||
});
|
||||
} catch (error: any) {
|
||||
@@ -1347,7 +1347,7 @@ export class SolanaOpenbookCreateMarket extends Tool {
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Create openbook market successfully",
|
||||
message: "Openbook market created successfully",
|
||||
transaction: tx,
|
||||
});
|
||||
} catch (error: any) {
|
||||
@@ -1736,6 +1736,95 @@ export class SolanaTipLinkTool extends Tool {
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaListNFTForSaleTool extends Tool {
|
||||
name = "solana_list_nft_for_sale";
|
||||
description = `List an NFT for sale on Tensor Trade.
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
nftMint: string, the mint address of the NFT (required)
|
||||
price: number, price in SOL (required)`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
// Validate NFT ownership first
|
||||
const nftAccount =
|
||||
await this.solanaKit.connection.getTokenAccountsByOwner(
|
||||
this.solanaKit.wallet_address,
|
||||
{ mint: new PublicKey(parsedInput.nftMint) },
|
||||
);
|
||||
|
||||
if (nftAccount.value.length === 0) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message:
|
||||
"NFT not found in wallet. Please make sure you own this NFT.",
|
||||
code: "NFT_NOT_FOUND",
|
||||
});
|
||||
}
|
||||
|
||||
const tx = await this.solanaKit.tensorListNFT(
|
||||
new PublicKey(parsedInput.nftMint),
|
||||
parsedInput.price,
|
||||
);
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "NFT listed for sale successfully",
|
||||
transaction: tx,
|
||||
price: parsedInput.price,
|
||||
nftMint: parsedInput.nftMint,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaCancelNFTListingTool extends Tool {
|
||||
name = "solana_cancel_nft_listing";
|
||||
description = `Cancel an NFT listing on Tensor Trade.
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
nftMint: string, the mint address of the NFT (required)`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
const tx = await this.solanaKit.tensorCancelListing(
|
||||
new PublicKey(parsedInput.nftMint),
|
||||
);
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "NFT listing cancelled successfully",
|
||||
transaction: tx,
|
||||
nftMint: parsedInput.nftMint,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
return [
|
||||
new SolanaBalanceTool(solanaKit),
|
||||
@@ -1782,5 +1871,7 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaCreateGibworkTask(solanaKit),
|
||||
new SolanaRockPaperScissorsTool(solanaKit),
|
||||
new SolanaTipLinkTool(solanaKit),
|
||||
new SolanaListNFTForSaleTool(solanaKit),
|
||||
new SolanaCancelNFTListingTool(solanaKit),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -16,12 +16,12 @@ export async function create_image(
|
||||
n: number = 1,
|
||||
) {
|
||||
try {
|
||||
if (!agent.openai_api_key) {
|
||||
if (!agent.config.OPENAI_API_KEY) {
|
||||
throw new Error("OpenAI API key not found in agent configuration");
|
||||
}
|
||||
|
||||
const openai = new OpenAI({
|
||||
apiKey: agent.openai_api_key,
|
||||
apiKey: agent.config.OPENAI_API_KEY,
|
||||
});
|
||||
|
||||
const response = await openai.images.generate({
|
||||
|
||||
@@ -8,11 +8,10 @@ import { getAllTld } from "@onsol/tldparser";
|
||||
*/
|
||||
export async function getAllDomainsTLDs(
|
||||
agent: SolanaAgentKit,
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
): Promise<String[]> {
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const tlds = await getAllTld(agent.connection);
|
||||
return tlds.map((tld) => tld.tld);
|
||||
return tlds.map((tld) => String(tld.tld));
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to fetch TLDs: ${error.message}`);
|
||||
}
|
||||
|
||||
@@ -50,3 +50,5 @@ export * from "./create_gibwork_task";
|
||||
|
||||
export * from "./rock_paper_scissor";
|
||||
export * from "./create_tiplinks";
|
||||
|
||||
export * from "./tensor_trade";
|
||||
|
||||
108
src/tools/tensor_trade.ts
Normal file
108
src/tools/tensor_trade.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import { TensorSwapSDK } from "@tensor-oss/tensorswap-sdk";
|
||||
import { PublicKey, Transaction } from "@solana/web3.js";
|
||||
import { AnchorProvider, Wallet } from "@coral-xyz/anchor";
|
||||
import { BN } from "bn.js";
|
||||
import {
|
||||
getAssociatedTokenAddress,
|
||||
TOKEN_PROGRAM_ID,
|
||||
getAccount,
|
||||
} from "@solana/spl-token";
|
||||
|
||||
export async function listNFTForSale(
|
||||
agent: SolanaAgentKit,
|
||||
nftMint: PublicKey,
|
||||
price: number,
|
||||
): Promise<string> {
|
||||
try {
|
||||
if (!PublicKey.isOnCurve(nftMint)) {
|
||||
throw new Error("Invalid NFT mint address");
|
||||
}
|
||||
|
||||
const mintInfo = await agent.connection.getAccountInfo(nftMint);
|
||||
if (!mintInfo) {
|
||||
throw new Error(`NFT mint ${nftMint.toString()} does not exist`);
|
||||
}
|
||||
|
||||
const ata = await getAssociatedTokenAddress(nftMint, agent.wallet_address);
|
||||
|
||||
try {
|
||||
const tokenAccount = await getAccount(agent.connection, ata);
|
||||
|
||||
if (!tokenAccount || tokenAccount.amount <= 0) {
|
||||
throw new Error(`You don't own this NFT (${nftMint.toString()})`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
throw new Error(
|
||||
`No token account found for mint ${nftMint.toString()}. Make sure you own this NFT.`,
|
||||
);
|
||||
}
|
||||
|
||||
const provider = new AnchorProvider(
|
||||
agent.connection,
|
||||
new Wallet(agent.wallet),
|
||||
AnchorProvider.defaultOptions(),
|
||||
);
|
||||
|
||||
const tensorSwapSdk = new TensorSwapSDK({ provider });
|
||||
const priceInLamports = new BN(price * 1e9);
|
||||
const nftSource = await getAssociatedTokenAddress(
|
||||
nftMint,
|
||||
agent.wallet_address,
|
||||
);
|
||||
|
||||
const { tx } = await tensorSwapSdk.list({
|
||||
nftMint,
|
||||
nftSource,
|
||||
owner: agent.wallet_address,
|
||||
price: priceInLamports,
|
||||
tokenProgram: TOKEN_PROGRAM_ID,
|
||||
payer: agent.wallet_address,
|
||||
});
|
||||
|
||||
const transaction = new Transaction();
|
||||
transaction.add(...tx.ixs);
|
||||
return await agent.connection.sendTransaction(transaction, [
|
||||
agent.wallet,
|
||||
...tx.extraSigners,
|
||||
]);
|
||||
} catch (error: any) {
|
||||
console.error("Full error details:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function cancelListing(
|
||||
agent: SolanaAgentKit,
|
||||
nftMint: PublicKey,
|
||||
): Promise<string> {
|
||||
const provider = new AnchorProvider(
|
||||
agent.connection,
|
||||
new Wallet(agent.wallet),
|
||||
AnchorProvider.defaultOptions(),
|
||||
);
|
||||
|
||||
const tensorSwapSdk = new TensorSwapSDK({ provider });
|
||||
const nftDest = await getAssociatedTokenAddress(
|
||||
nftMint,
|
||||
agent.wallet_address,
|
||||
false,
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
|
||||
const { tx } = await tensorSwapSdk.delist({
|
||||
nftMint,
|
||||
nftDest,
|
||||
owner: agent.wallet_address,
|
||||
tokenProgram: TOKEN_PROGRAM_ID,
|
||||
payer: agent.wallet_address,
|
||||
authData: null,
|
||||
});
|
||||
|
||||
const transaction = new Transaction();
|
||||
transaction.add(...tx.ixs);
|
||||
return await agent.connection.sendTransaction(transaction, [
|
||||
agent.wallet,
|
||||
...tx.extraSigners,
|
||||
]);
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import { VersionedTransaction, PublicKey } from "@solana/web3.js";
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import { TOKENS, DEFAULT_OPTIONS, JUP_API } from "../constants";
|
||||
import {
|
||||
TOKENS,
|
||||
DEFAULT_OPTIONS,
|
||||
JUP_API,
|
||||
JUP_REFERRAL_ADDRESS,
|
||||
} from "../constants";
|
||||
import { getMint } from "@solana/spl-token";
|
||||
/**
|
||||
* Swap tokens using Jupiter Exchange
|
||||
@@ -11,6 +16,7 @@ import { getMint } from "@solana/spl-token";
|
||||
* @param slippageBps Slippage tolerance in basis points (default: 300 = 3%)
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
|
||||
export async function trade(
|
||||
agent: SolanaAgentKit,
|
||||
outputMint: PublicKey,
|
||||
@@ -38,11 +44,24 @@ export async function trade(
|
||||
`&amount=${scaledAmount}` +
|
||||
`&slippageBps=${slippageBps}` +
|
||||
`&onlyDirectRoutes=true` +
|
||||
`&maxAccounts=20`,
|
||||
`&maxAccounts=20` +
|
||||
`${agent.config.JUPITER_FEE_BPS ? `&platformFeeBps=${agent.config.JUPITER_FEE_BPS}` : ""}`,
|
||||
)
|
||||
).json();
|
||||
|
||||
// Get serialized transaction
|
||||
let feeAccount;
|
||||
if (agent.config.JUPITER_REFERRAL_ACCOUNT) {
|
||||
[feeAccount] = PublicKey.findProgramAddressSync(
|
||||
[
|
||||
Buffer.from("referral_ata"),
|
||||
new PublicKey(agent.config.JUPITER_REFERRAL_ACCOUNT).toBuffer(),
|
||||
TOKENS.SOL.toBuffer(),
|
||||
],
|
||||
new PublicKey(JUP_REFERRAL_ADDRESS),
|
||||
);
|
||||
}
|
||||
|
||||
const { swapTransaction } = await (
|
||||
await fetch("https://quote-api.jup.ag/v6/swap", {
|
||||
method: "POST",
|
||||
@@ -55,6 +74,7 @@ export async function trade(
|
||||
wrapAndUnwrapSol: true,
|
||||
dynamicComputeUnitLimit: true,
|
||||
prioritizationFeeLamports: "auto",
|
||||
feeAccount: feeAccount ? feeAccount.toString() : null,
|
||||
}),
|
||||
})
|
||||
).json();
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
|
||||
export interface Config {
|
||||
OPENAI_API_KEY?: string;
|
||||
JUPITER_REFERRAL_ACCOUNT?: string;
|
||||
JUPITER_FEE_BPS?: number;
|
||||
}
|
||||
|
||||
export interface Creator {
|
||||
address: string;
|
||||
percentage: number;
|
||||
|
||||
Reference in New Issue
Block a user