mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-22 23:26:45 +00:00
feat: add image support
This commit is contained in:
@@ -27,14 +27,17 @@ export class SolanaAgentKit {
|
||||
public connection: Connection;
|
||||
public wallet: Keypair;
|
||||
public wallet_address: PublicKey;
|
||||
public openai_api_key: string;
|
||||
|
||||
constructor(
|
||||
private_key: string,
|
||||
rpc_url = "https://api.mainnet-beta.solana.com"
|
||||
rpc_url = "https://api.mainnet-beta.solana.com",
|
||||
openai_api_key: string
|
||||
) {
|
||||
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;
|
||||
}
|
||||
|
||||
// Tool methods
|
||||
|
||||
@@ -3,6 +3,7 @@ import { SolanaAgentKit } from "../index";
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
import { PumpFunTokenOptions } from "../types";
|
||||
import { toJSON } from "../utils/toJSON";
|
||||
import { create_image } from "../tools/create_image";
|
||||
export class SolanaBalanceTool extends Tool {
|
||||
name = "solana_balance";
|
||||
description =
|
||||
@@ -193,59 +194,80 @@ export class SolanaGetWalletAddressTool extends Tool {
|
||||
|
||||
export class SolanaPumpfunTokenLaunchTool extends Tool {
|
||||
name = "solana_launch_pumpfun_token";
|
||||
description = "Launch a new token on Pump.fun via Solana Agent Kit. Requires a JSON input with tokenName and tokenTicker, with optional fields for description, twitter, telegram, website, imageUrl, initialLiquiditySOL, and mintAddress.";
|
||||
description =
|
||||
"Launch a new token on Pump.fun via Solana Agent Kit. Requires a JSON input with tokenName and tokenTicker, with optional fields for description, twitter, telegram, website, imageUrl, initialLiquiditySOL, and mintAddress.";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
super();
|
||||
}
|
||||
|
||||
private validateInput(input : any): void {
|
||||
if (!input.tokenName || typeof input.tokenName !== 'string') {
|
||||
throw new Error('tokenName is required and must be a string');
|
||||
}
|
||||
if (!input.tokenTicker || typeof input.tokenTicker !== 'string') {
|
||||
throw new Error('tokenTicker is required and must be a string');
|
||||
}
|
||||
if (input.initialLiquiditySOL !== undefined && typeof input.initialLiquiditySOL !== 'number') {
|
||||
throw new Error('initialLiquiditySOL must be a number when provided');
|
||||
}
|
||||
private validateInput(input: any): void {
|
||||
if (!input.tokenName || typeof input.tokenName !== "string") {
|
||||
throw new Error("tokenName is required and must be a string");
|
||||
}
|
||||
if (!input.tokenTicker || typeof input.tokenTicker !== "string") {
|
||||
throw new Error("tokenTicker is required and must be a string");
|
||||
}
|
||||
if (
|
||||
input.initialLiquiditySOL !== undefined &&
|
||||
typeof input.initialLiquiditySOL !== "number"
|
||||
) {
|
||||
throw new Error("initialLiquiditySOL must be a number when provided");
|
||||
}
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
// Parse and normalize input
|
||||
const parsedInput = toJSON(input);
|
||||
// Validate the input
|
||||
this.validateInput(parsedInput);
|
||||
try {
|
||||
// Parse and normalize input
|
||||
const parsedInput = toJSON(input);
|
||||
// Validate the input
|
||||
this.validateInput(parsedInput);
|
||||
|
||||
// Launch token with validated input
|
||||
await this.solanaKit.launchPumpFunToken(
|
||||
parsedInput.tokenName,
|
||||
parsedInput.tokenTicker,
|
||||
{
|
||||
description: parsedInput.description,
|
||||
twitter: parsedInput.twitter,
|
||||
telegram: parsedInput.telegram,
|
||||
website: parsedInput.website,
|
||||
imageUrl: parsedInput.imageUrl,
|
||||
initialLiquiditySOL: parsedInput.initialLiquiditySOL,
|
||||
}
|
||||
);
|
||||
// Launch token with validated input
|
||||
await this.solanaKit.launchPumpFunToken(
|
||||
parsedInput.tokenName,
|
||||
parsedInput.tokenTicker,
|
||||
{
|
||||
description: parsedInput.description,
|
||||
twitter: parsedInput.twitter,
|
||||
telegram: parsedInput.telegram,
|
||||
website: parsedInput.website,
|
||||
imageUrl: parsedInput.imageUrl,
|
||||
initialLiquiditySOL: parsedInput.initialLiquiditySOL,
|
||||
}
|
||||
);
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Token launched successfully on Pump.fun",
|
||||
tokenName: parsedInput.tokenName,
|
||||
tokenTicker: parsedInput.tokenTicker
|
||||
});
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Token launched successfully on Pump.fun",
|
||||
tokenName: parsedInput.tokenName,
|
||||
tokenTicker: parsedInput.tokenTicker,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR"
|
||||
});
|
||||
}
|
||||
export class SolanaCreateImageTool extends Tool {
|
||||
name = "solana_create_image";
|
||||
description = "Create an image using OpenAI's DALL-E";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const result = await create_image(this.solanaKit, input);
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return `Error creating image: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,5 +283,6 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaRegisterDomainTool(solanaKit),
|
||||
new SolanaGetWalletAddressTool(solanaKit),
|
||||
new SolanaPumpfunTokenLaunchTool(solanaKit),
|
||||
new SolanaCreateImageTool(solanaKit),
|
||||
];
|
||||
}
|
||||
|
||||
40
src/tools/create_image.ts
Normal file
40
src/tools/create_image.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import OpenAI from "openai";
|
||||
|
||||
/**
|
||||
* Generate an image using OpenAI's DALL-E
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param prompt Text description of the image to generate
|
||||
* @param size Image size ('256x256', '512x512', or '1024x1024') (default: '1024x1024')
|
||||
* @param n Number of images to generate (default: 1)
|
||||
* @returns Object containing the generated image URLs
|
||||
*/
|
||||
export async function create_image(
|
||||
agent: SolanaAgentKit,
|
||||
prompt: string,
|
||||
size: "256x256" | "512x512" | "1024x1024" = "1024x1024",
|
||||
n: number = 1
|
||||
) {
|
||||
try {
|
||||
if (!agent.openai_api_key) {
|
||||
throw new Error("OpenAI API key not found in agent configuration");
|
||||
}
|
||||
|
||||
const openai = new OpenAI({
|
||||
apiKey: agent.openai_api_key
|
||||
});
|
||||
|
||||
const response = await openai.images.generate({
|
||||
prompt,
|
||||
n,
|
||||
size,
|
||||
});
|
||||
|
||||
return {
|
||||
images: response.data.map((img) => img.url),
|
||||
};
|
||||
|
||||
} catch (error: any) {
|
||||
throw new Error(`Image generation failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
import { TOKENS } from "../constants";
|
||||
|
||||
export interface Creator {
|
||||
address: string;
|
||||
@@ -40,3 +41,24 @@ export interface PumpfunLaunchResponse {
|
||||
metadataUri?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mint addresses of supported tokens for lending on Lulo
|
||||
*/
|
||||
export type LuloDepositAssetMint = (typeof TOKENS)[keyof typeof TOKENS];
|
||||
|
||||
/**
|
||||
* Lulo Account Details response format
|
||||
*/
|
||||
export interface LuloAccountDetailsResponse {
|
||||
totalValue: number;
|
||||
interestEarned: number;
|
||||
realtimeApy: number;
|
||||
settings: {
|
||||
owner: string;
|
||||
allowedProtocols: string | null;
|
||||
homebase: string | null;
|
||||
minimumRate: string;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user