mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-19 23:26:45 +00:00
chore: lint
This commit is contained in:
@@ -356,21 +356,15 @@ export class SolanaAgentKit {
|
||||
async tensorListNFT(
|
||||
nftMint: PublicKey,
|
||||
price: number,
|
||||
expirySeconds?: number
|
||||
): Promise<string> {
|
||||
return listNFTForSale(this, nftMint, price, expirySeconds);
|
||||
return listNFTForSale(this, nftMint, price);
|
||||
}
|
||||
|
||||
async tensorBuyNFT(
|
||||
nftMint: PublicKey,
|
||||
maxPrice: number
|
||||
): Promise<string> {
|
||||
async tensorBuyNFT(nftMint: PublicKey, maxPrice: number): Promise<string> {
|
||||
return buyNFT(this, nftMint, maxPrice);
|
||||
}
|
||||
|
||||
async tensorCancelListing(
|
||||
nftMint: PublicKey
|
||||
): Promise<string> {
|
||||
async tensorCancelListing(nftMint: PublicKey): Promise<string> {
|
||||
return cancelListing(this, nftMint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1332,8 +1332,7 @@ export class SolanaListNFTForSaleTool extends Tool {
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
nftMint: string, the mint address of the NFT (required)
|
||||
price: number, price in SOL (required)
|
||||
expirySeconds: number, expiry time in seconds (optional)`;
|
||||
price: number, price in SOL (required)`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
@@ -1342,25 +1341,26 @@ export class SolanaListNFTForSaleTool extends Tool {
|
||||
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) }
|
||||
);
|
||||
|
||||
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"
|
||||
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,
|
||||
parsedInput.expirySeconds
|
||||
);
|
||||
|
||||
return JSON.stringify({
|
||||
@@ -1368,13 +1368,13 @@ export class SolanaListNFTForSaleTool extends Tool {
|
||||
message: "NFT listed for sale successfully",
|
||||
transaction: tx,
|
||||
price: parsedInput.price,
|
||||
nftMint: parsedInput.nftMint
|
||||
nftMint: parsedInput.nftMint,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR"
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1395,10 +1395,10 @@ export class SolanaBuyNFTTool extends Tool {
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
|
||||
const tx = await this.solanaKit.tensorBuyNFT(
|
||||
new PublicKey(parsedInput.nftMint),
|
||||
parsedInput.maxPrice
|
||||
parsedInput.maxPrice,
|
||||
);
|
||||
|
||||
return JSON.stringify({
|
||||
@@ -1406,13 +1406,13 @@ export class SolanaBuyNFTTool extends Tool {
|
||||
message: "NFT purchased successfully",
|
||||
transaction: tx,
|
||||
maxPrice: parsedInput.maxPrice,
|
||||
nftMint: parsedInput.nftMint
|
||||
nftMint: parsedInput.nftMint,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR"
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1432,22 +1432,22 @@ export class SolanaCancelNFTListingTool extends Tool {
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
|
||||
const tx = await this.solanaKit.tensorCancelListing(
|
||||
new PublicKey(parsedInput.nftMint)
|
||||
new PublicKey(parsedInput.nftMint),
|
||||
);
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "NFT listing cancelled successfully",
|
||||
transaction: tx,
|
||||
nftMint: parsedInput.nftMint
|
||||
nftMint: parsedInput.nftMint,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR"
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,20 @@ 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';
|
||||
import {
|
||||
getAssociatedTokenAddress,
|
||||
TOKEN_PROGRAM_ID,
|
||||
getAccount,
|
||||
} from "@solana/spl-token";
|
||||
|
||||
export async function listNFTForSale(
|
||||
agent: SolanaAgentKit,
|
||||
nftMint: PublicKey,
|
||||
price: number,
|
||||
expirySeconds?: number
|
||||
): Promise<string> {
|
||||
try {
|
||||
if (!PublicKey.isOnCurve(nftMint)) {
|
||||
throw new Error('Invalid NFT mint address');
|
||||
throw new Error("Invalid NFT mint address");
|
||||
}
|
||||
|
||||
const mintInfo = await agent.connection.getAccountInfo(nftMint);
|
||||
@@ -21,33 +24,32 @@ export async function listNFTForSale(
|
||||
throw new Error(`NFT mint ${nftMint.toString()} does not exist`);
|
||||
}
|
||||
|
||||
const ata = await getAssociatedTokenAddress(
|
||||
nftMint,
|
||||
agent.wallet_address
|
||||
);
|
||||
const ata = await getAssociatedTokenAddress(nftMint, agent.wallet_address);
|
||||
|
||||
try {
|
||||
const tokenAccount = await getAccount(
|
||||
agent.connection,
|
||||
ata
|
||||
);
|
||||
|
||||
const tokenAccount = await getAccount(agent.connection, ata);
|
||||
|
||||
if (!tokenAccount || tokenAccount.amount <= 0) {
|
||||
throw new Error(`You don't own this NFT (${nftMint.toString()})`);
|
||||
}
|
||||
} catch (e) {
|
||||
throw new Error(`No token account found for mint ${nftMint.toString()}. Make sure you own this NFT.`);
|
||||
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()
|
||||
AnchorProvider.defaultOptions(),
|
||||
);
|
||||
|
||||
|
||||
const tensorSwapSdk = new TensorSwapSDK({ provider });
|
||||
const priceInLamports = new BN(price * 1e9);
|
||||
const nftSource = await getAssociatedTokenAddress(nftMint, agent.wallet_address);
|
||||
const nftSource = await getAssociatedTokenAddress(
|
||||
nftMint,
|
||||
agent.wallet_address,
|
||||
);
|
||||
|
||||
const { tx } = await tensorSwapSdk.list({
|
||||
nftMint,
|
||||
@@ -55,12 +57,15 @@ export async function listNFTForSale(
|
||||
owner: agent.wallet_address,
|
||||
price: priceInLamports,
|
||||
tokenProgram: TOKEN_PROGRAM_ID,
|
||||
payer: agent.wallet_address
|
||||
payer: agent.wallet_address,
|
||||
});
|
||||
|
||||
const transaction = new Transaction();
|
||||
transaction.add(...tx.ixs);
|
||||
return await agent.connection.sendTransaction(transaction, [agent.wallet, ...tx.extraSigners]);
|
||||
return await agent.connection.sendTransaction(transaction, [
|
||||
agent.wallet,
|
||||
...tx.extraSigners,
|
||||
]);
|
||||
} catch (error: any) {
|
||||
console.error("Full error details:", error);
|
||||
throw error;
|
||||
@@ -70,17 +75,20 @@ export async function listNFTForSale(
|
||||
export async function buyNFT(
|
||||
agent: SolanaAgentKit,
|
||||
nftMint: PublicKey,
|
||||
maxPrice: number
|
||||
maxPrice: number,
|
||||
): Promise<string> {
|
||||
const provider = new AnchorProvider(
|
||||
agent.connection,
|
||||
new Wallet(agent.wallet),
|
||||
AnchorProvider.defaultOptions()
|
||||
AnchorProvider.defaultOptions(),
|
||||
);
|
||||
|
||||
|
||||
const tensorSwapSdk = new TensorSwapSDK({ provider });
|
||||
const maxPriceInLamports = new BN(maxPrice * 1e9);
|
||||
const nftBuyerAcc = await getAssociatedTokenAddress(nftMint, agent.wallet_address);
|
||||
const nftBuyerAcc = await getAssociatedTokenAddress(
|
||||
nftMint,
|
||||
agent.wallet_address,
|
||||
);
|
||||
|
||||
const { tx } = await tensorSwapSdk.buySingleListingT22({
|
||||
nftMint,
|
||||
@@ -91,42 +99,48 @@ export async function buyNFT(
|
||||
takerBroker: null,
|
||||
compute: null,
|
||||
priorityMicroLamports: null,
|
||||
transferHook: null
|
||||
transferHook: null,
|
||||
});
|
||||
|
||||
const transaction = new Transaction();
|
||||
transaction.add(...tx.ixs);
|
||||
return await agent.connection.sendTransaction(transaction, [agent.wallet, ...tx.extraSigners]);
|
||||
return await agent.connection.sendTransaction(transaction, [
|
||||
agent.wallet,
|
||||
...tx.extraSigners,
|
||||
]);
|
||||
}
|
||||
|
||||
export async function cancelListing(
|
||||
agent: SolanaAgentKit,
|
||||
nftMint: PublicKey
|
||||
nftMint: PublicKey,
|
||||
): Promise<string> {
|
||||
const provider = new AnchorProvider(
|
||||
agent.connection,
|
||||
new Wallet(agent.wallet),
|
||||
AnchorProvider.defaultOptions()
|
||||
AnchorProvider.defaultOptions(),
|
||||
);
|
||||
|
||||
|
||||
const tensorSwapSdk = new TensorSwapSDK({ provider });
|
||||
const nftDest = await getAssociatedTokenAddress(
|
||||
nftMint,
|
||||
nftMint,
|
||||
agent.wallet_address,
|
||||
false,
|
||||
TOKEN_PROGRAM_ID
|
||||
TOKEN_PROGRAM_ID,
|
||||
);
|
||||
|
||||
|
||||
const { tx } = await tensorSwapSdk.delist({
|
||||
nftMint,
|
||||
nftDest,
|
||||
owner: agent.wallet_address,
|
||||
tokenProgram: TOKEN_PROGRAM_ID,
|
||||
payer: agent.wallet_address,
|
||||
authData: null
|
||||
authData: null,
|
||||
});
|
||||
|
||||
const transaction = new Transaction();
|
||||
transaction.add(...tx.ixs);
|
||||
return await agent.connection.sendTransaction(transaction, [agent.wallet, ...tx.extraSigners]);
|
||||
}
|
||||
return await agent.connection.sendTransaction(transaction, [
|
||||
agent.wallet,
|
||||
...tx.extraSigners,
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user