78 lines
2.0 KiB
JavaScript
78 lines
2.0 KiB
JavaScript
![]() |
import { SystemProgram, Transaction } from "@solana/web3.js"
|
||
|
import {
|
||
|
createAssociatedTokenAccountInstruction,
|
||
|
createTransferInstruction,
|
||
|
getAccount,
|
||
|
getAssociatedTokenAddress,
|
||
|
getMint
|
||
|
} from "@solana/spl-token"
|
||
|
|
||
|
import { LAMPORTS_PER_SOL } from "@solana/web3.js"
|
||
|
import { sendTx } from "../../utils/send_tx"
|
||
|
|
||
|
/**
|
||
|
* Transfer SOL or SPL tokens to a recipient
|
||
|
* @param agent SolanaAgentKit instance
|
||
|
* @param to Recipient's public key
|
||
|
* @param amount Amount to transfer
|
||
|
* @param mint Optional mint address for SPL tokens
|
||
|
* @returns Transaction signature
|
||
|
*/
|
||
|
export async function transfer(agent, to, amount, mint) {
|
||
|
try {
|
||
|
let tx
|
||
|
|
||
|
if (!mint) {
|
||
|
// Transfer native SOL
|
||
|
const transaction = new Transaction().add(
|
||
|
SystemProgram.transfer({
|
||
|
fromPubkey: agent.wallet_address,
|
||
|
toPubkey: to,
|
||
|
lamports: amount * LAMPORTS_PER_SOL
|
||
|
})
|
||
|
)
|
||
|
|
||
|
tx = await sendTx(agent, transaction.instructions)
|
||
|
} else {
|
||
|
const transaction = new Transaction()
|
||
|
// Transfer SPL token
|
||
|
const fromAta = await getAssociatedTokenAddress(
|
||
|
mint,
|
||
|
agent.wallet_address
|
||
|
)
|
||
|
const toAta = await getAssociatedTokenAddress(mint, to)
|
||
|
try {
|
||
|
const toAtaAcc = await getAccount(agent.connection, toAta)
|
||
|
} catch {
|
||
|
transaction.add(
|
||
|
createAssociatedTokenAccountInstruction(
|
||
|
agent.wallet_address,
|
||
|
toAta,
|
||
|
to,
|
||
|
mint
|
||
|
)
|
||
|
)
|
||
|
}
|
||
|
|
||
|
// Get mint info to determine decimals
|
||
|
const mintInfo = await getMint(agent.connection, mint)
|
||
|
const adjustedAmount = amount * Math.pow(10, mintInfo.decimals)
|
||
|
|
||
|
transaction.add(
|
||
|
createTransferInstruction(
|
||
|
fromAta,
|
||
|
toAta,
|
||
|
agent.wallet_address,
|
||
|
adjustedAmount
|
||
|
)
|
||
|
)
|
||
|
|
||
|
tx = await sendTx(agent, transaction.instructions)
|
||
|
}
|
||
|
|
||
|
return tx
|
||
|
} catch (error) {
|
||
|
throw new Error(`Transfer failed: ${error.message}`)
|
||
|
}
|
||
|
}
|