Merge branch 'main' into manifest

This commit is contained in:
DonDuala
2024-12-31 09:07:47 -04:00
committed by GitHub
20 changed files with 875 additions and 32 deletions

View File

@@ -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({

View File

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

View File

@@ -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
View 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,
]);
}

View File

@@ -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();