feat: config

This commit is contained in:
aryan
2024-12-31 16:28:07 +05:30
parent a20894807b
commit 5048f19c50
8 changed files with 612 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
OPENAI_API_KEY=
RPC_URL=
SOLANA_PRIVATE_KEY=
JUP_REFERRAL_ACCOUNT=
JUP_FEE_BPS=
JUPITER_REFERRAL_ACCOUNT=
JUPITER_FEE_BPS=

590
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -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,
@@ -71,17 +72,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

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

@@ -32,7 +32,7 @@ export async function listNFTForSale(
if (!tokenAccount || tokenAccount.amount <= 0) {
throw new Error(`You don't own this NFT (${nftMint.toString()})`);
}
} catch (e) {
} catch (error: any) {
throw new Error(
`No token account found for mint ${nftMint.toString()}. Make sure you own this NFT.`,
);

View File

@@ -1,10 +1,5 @@
import { VersionedTransaction, PublicKey } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import dotenv from "dotenv";
// Load environment variables
dotenv.config();
import {
TOKENS,
DEFAULT_OPTIONS,
@@ -22,14 +17,6 @@ import { getMint } from "@solana/spl-token";
* @returns Transaction signature
*/
// Get Jupiter fee and referral account from environment variables
const JUP_FEE_BPS = process.env.JUP_FEE_BPS
? parseInt(process.env.JUP_FEE_BPS)
: "";
const JUP_REFERRAL_ACCOUNT = process.env.JUP_REFERRAL_ACCOUNT
? new PublicKey(process.env.JUP_REFERRAL_ACCOUNT)
: "";
export async function trade(
agent: SolanaAgentKit,
outputMint: PublicKey,
@@ -58,17 +45,17 @@ export async function trade(
`&slippageBps=${slippageBps}` +
`&onlyDirectRoutes=true` +
`&maxAccounts=20` +
`${JUP_FEE_BPS ? `&platformFeeBps=${JUP_FEE_BPS}` : ""}`,
`${agent.config.JUPITER_FEE_BPS ? `&platformFeeBps=${agent.config.JUPITER_FEE_BPS}` : ""}`,
)
).json();
// Get serialized transaction
let feeAccount;
if (JUP_REFERRAL_ACCOUNT) {
[feeAccount] = await PublicKey.findProgramAddressSync(
if (agent.config.JUPITER_REFERRAL_ACCOUNT) {
[feeAccount] = PublicKey.findProgramAddressSync(
[
Buffer.from("referral_ata"),
new PublicKey(JUP_REFERRAL_ACCOUNT).toBuffer(),
new PublicKey(agent.config.JUPITER_REFERRAL_ACCOUNT).toBuffer(),
TOKENS.SOL.toBuffer(),
],
new PublicKey(JUP_REFERRAL_ADDRESS),

View File

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

View File

@@ -53,7 +53,9 @@ async function initializeAgent() {
const solanaAgent = new SolanaAgentKit(
process.env.SOLANA_PRIVATE_KEY!,
process.env.RPC_URL,
process.env.OPENAI_API_KEY!,
{
OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
},
);
const tools = createSolanaTools(solanaAgent);