50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
![]() |
import { PublicKey } from "@solana/web3.js"
|
||
|
import { Tool } from "langchain/tools"
|
||
|
|
||
|
export class SolanaTransferTool extends Tool {
|
||
|
name = "solana_transfer"
|
||
|
description = `Transfer tokens or SOL to another address ( also called as wallet address ).
|
||
|
|
||
|
Inputs ( input is a JSON string ):
|
||
|
to: string, eg "8x2dR8Mpzuz2YqyZyZjUbYWKSWesBo5jMx2Q9Y86udVk" (required)
|
||
|
amount: number, eg 1 (required)
|
||
|
mint?: string, eg "So11111111111111111111111111111111111111112" or "SENDdRQtYMWaQrBroBrJ2Q53fgVuq95CV9UPGEvpCxa" (optional)`
|
||
|
|
||
|
constructor(solanaKit) {
|
||
|
super()
|
||
|
this.solanaKit = solanaKit
|
||
|
}
|
||
|
|
||
|
async _call(input) {
|
||
|
try {
|
||
|
const parsedInput = JSON.parse(input)
|
||
|
|
||
|
const recipient = new PublicKey(parsedInput.to)
|
||
|
const mintAddress = parsedInput.mint
|
||
|
? new PublicKey(parsedInput.mint)
|
||
|
: undefined
|
||
|
|
||
|
const tx = await this.solanaKit.transfer(
|
||
|
recipient,
|
||
|
parsedInput.amount,
|
||
|
mintAddress
|
||
|
)
|
||
|
|
||
|
return JSON.stringify({
|
||
|
status: "success",
|
||
|
message: "Transfer completed successfully",
|
||
|
amount: parsedInput.amount,
|
||
|
recipient: parsedInput.to,
|
||
|
token: parsedInput.mint || "SOL",
|
||
|
transaction: tx
|
||
|
})
|
||
|
} catch (error) {
|
||
|
return JSON.stringify({
|
||
|
status: "error",
|
||
|
message: error.message,
|
||
|
code: error.code || "UNKNOWN_ERROR"
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|