fix:handled spl token address

This commit is contained in:
shivaji43
2024-12-26 12:46:35 +05:30
parent 18ddd247c3
commit 3f0b6e13e5

View File

@@ -1231,12 +1231,13 @@ export class SolanaCreateGibworkTask extends Tool {
export class SolanaTipLinkTool extends Tool { export class SolanaTipLinkTool extends Tool {
name = "solana_tiplink"; name = "solana_tiplink";
description = `Create a TipLink for transferring SOL. description = `Create a TipLink for transferring SOL or SPL tokens.
Provide the amount of SOL you want to transfer. The tool will generate a TipLink URL for the recipient to claim the funds. Input should be a JSON string with the following format:
{
Inputs: "amountSol": string, // Amount to transfer as string (e.g., "1")
amountSol: number, e.g., 1 (Amount of SOL to transfer)`; "splmintAddress": string // (Optional) SPL token mint address
}`;
constructor(private solanaKit: SolanaAgentKit) { constructor(private solanaKit: SolanaAgentKit) {
super(); super();
@@ -1244,25 +1245,49 @@ export class SolanaTipLinkTool extends Tool {
protected async _call(input: string): Promise<string> { protected async _call(input: string): Promise<string> {
try { try {
const amountSol : number = parseFloat(input); let parsedInput: { amountSol: string; splmintAddress?: string };
try {
if (isNaN(amountSol) || amountSol <= 0) { parsedInput = JSON.parse(input);
throw new Error("Invalid amount. Please provide a valid amount of SOL."); } catch (e) {
throw new Error("Invalid input format. Please provide a valid JSON string.");
}
const amount = parseFloat(parsedInput.amountSol);
if (isNaN(amount) || amount <= 0) {
throw new Error("Invalid amount. Please provide a valid positive number as a string.");
} }
const { url, signature } = await this.solanaKit.createTiplink(amountSol); // Handle optional SPL mint address
let splmintPublicKey: PublicKey | undefined;
if (parsedInput.splmintAddress) {
try {
splmintPublicKey = new PublicKey(parsedInput.splmintAddress);
} catch (e) {
throw new Error("Invalid SPL mint address. Please provide a valid Solana public key.");
}
}
// Create TipLink using the imported create_TipLink function
const { url, signature } = await this.solanaKit.createTiplink(
amount,
splmintPublicKey
);
// Return success response
return JSON.stringify({ return JSON.stringify({
status: "success", status: "success",
url, url,
signature, signature,
message: "TipLink created successfully.", tokenType: splmintPublicKey ? "SPL" : "SOL",
amount,
message: `TipLink created successfully for ${amount} ${splmintPublicKey ? "tokens" : "SOL"}.`
}); });
} catch (error: any) { } catch (error: any) {
// Return error response
return JSON.stringify({ return JSON.stringify({
status: "error", status: "error",
message: error.message, message: error.message,
code: error.code || "UNKNOWN_ERROR", code: error.code || "UNKNOWN_ERROR"
}); });
} }
} }