Skip to content

Commit 559e267

Browse files
committed
feat: transfer
1 parent 14c71d3 commit 559e267

File tree

1 file changed

+9
-79
lines changed

1 file changed

+9
-79
lines changed

packages/plugin-solana/src/actions/transfer.ts

+9-79
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import {
2-
getAssociatedTokenAddressSync,
3-
createTransferInstruction,
4-
} from "@solana/spl-token";
5-
import { elizaLogger, settings } from "@elizaos/core";
1+
2+
import { elizaLogger, } from "@elizaos/core";
63

74
import {
8-
Connection,
95
PublicKey,
10-
TransactionMessage,
11-
VersionedTransaction,
6+
127
} from "@solana/web3.js";
138

149
import {
@@ -22,9 +17,9 @@ import {
2217
type Action,
2318
} from "@elizaos/core";
2419
import { composeContext } from "@elizaos/core";
25-
import { getWalletKey } from "../keypairUtils";
2620
import { generateObjectDeprecated } from "@elizaos/core";
2721
import { ACTIONS } from "solana-agent-kit";
22+
import { getSAK } from "../utils";
2823

2924
const TRANSFER_ACTION = ACTIONS.TRANSFER_ACTION;
3025

@@ -100,6 +95,7 @@ export default {
10095
callback?: HandlerCallback
10196
): Promise<boolean> => {
10297
elizaLogger.log("Starting SEND_TOKEN handler...");
98+
const sak = await getSAK(runtime);
10399

104100
// Initialize or update state
105101
if (!state) {
@@ -134,87 +130,21 @@ export default {
134130
}
135131

136132
try {
137-
const { keypair: senderKeypair } = await getWalletKey(
138-
runtime,
139-
true
140-
);
141-
142-
const connection = new Connection(settings.RPC_URL!);
143133

144134
const mintPubkey = new PublicKey(content.tokenAddress);
145135
const recipientPubkey = new PublicKey(content.recipient);
146136

147-
// Get decimals (simplest way)
148-
const mintInfo = await connection.getParsedAccountInfo(mintPubkey);
149-
const decimals =
150-
(mintInfo.value?.data as any)?.parsed?.info?.decimals ?? 9;
151-
152-
// Adjust amount with decimals
153-
const adjustedAmount = BigInt(
154-
Number(content.amount) * Math.pow(10, decimals)
155-
);
156-
console.log(
157-
`Transferring: ${content.amount} tokens (${adjustedAmount} base units)`
158-
);
159-
160-
// Rest of the existing working code...
161-
const senderATA = getAssociatedTokenAddressSync(
162-
mintPubkey,
163-
senderKeypair.publicKey
164-
);
165-
const recipientATA = getAssociatedTokenAddressSync(
166-
mintPubkey,
167-
recipientPubkey
168-
);
169-
170-
const instructions = [];
171-
172-
const recipientATAInfo =
173-
await connection.getAccountInfo(recipientATA);
174-
if (!recipientATAInfo) {
175-
const { createAssociatedTokenAccountInstruction } =
176-
await import("@solana/spl-token");
177-
instructions.push(
178-
createAssociatedTokenAccountInstruction(
179-
senderKeypair.publicKey,
180-
recipientATA,
181-
recipientPubkey,
182-
mintPubkey
183-
)
184-
);
185-
}
186-
187-
instructions.push(
188-
createTransferInstruction(
189-
senderATA,
190-
recipientATA,
191-
senderKeypair.publicKey,
192-
adjustedAmount
193-
)
194-
);
195-
196-
// Create and sign versioned transaction
197-
const messageV0 = new TransactionMessage({
198-
payerKey: senderKeypair.publicKey,
199-
recentBlockhash: (await connection.getLatestBlockhash())
200-
.blockhash,
201-
instructions,
202-
}).compileToV0Message();
203-
204-
const transaction = new VersionedTransaction(messageV0);
205-
transaction.sign([senderKeypair]);
206137

207-
// Send transaction
208-
const signature = await connection.sendTransaction(transaction);
138+
const txId = await sak.transfer(recipientPubkey, Number(content.amount), mintPubkey)
209139

210-
console.log("Transfer successful:", signature);
140+
console.log("Transfer successful:", txId);
211141

212142
if (callback) {
213143
callback({
214-
text: `Successfully transferred ${content.amount} tokens to ${content.recipient}\nTransaction: ${signature}`,
144+
text: `Successfully transferred ${content.amount} tokens to ${content.recipient}\nTransaction: ${txId}`,
215145
content: {
216146
success: true,
217-
signature,
147+
signature : txId,
218148
amount: content.amount,
219149
recipient: content.recipient,
220150
},

0 commit comments

Comments
 (0)