33 lines
1.1 KiB
JavaScript
Raw Normal View History

2025-02-17 15:21:20 +07:00
import { PublicKey } from '@solana/web3.js';
import { tool } from "@langchain/core/tools";
import { z } from 'zod';
export const transferSolanaTool = (agent) => tool(
async ({ recipient, amount, debug = false }) => {
if (!debug) {
return `transferSolanaTool call failed. User denied the requiest`;
}
try {
const recipientPubKey = new PublicKey(recipient);
const signature = await agent.transfer(recipientPubKey, amount);
return `Transfer to ${recipient} successful. Signature: ${signature}`;
} catch (error) {
if (debug) {
return `Transfer failed with error: ${error.message}`;
}
return 'Transfer failed.';
}
},
{
name: 'transferSolanaTool',
description: 'Transfers a specified amount of Solana to the provided recipient address.',
schema: z.object({
recipient: z.string().describe('The recipient public key as a string'),
amount: z.number().min(0).describe('The amount of Solana to transfer'),
debug: z.boolean().default(false).describe('Keep false'),
}),
}
);