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" ;
6
3
7
4
import {
8
- Connection ,
9
5
PublicKey ,
10
- TransactionMessage ,
11
- VersionedTransaction ,
6
+
12
7
} from "@solana/web3.js" ;
13
8
14
9
import {
@@ -22,9 +17,9 @@ import {
22
17
type Action ,
23
18
} from "@elizaos/core" ;
24
19
import { composeContext } from "@elizaos/core" ;
25
- import { getWalletKey } from "../keypairUtils" ;
26
20
import { generateObjectDeprecated } from "@elizaos/core" ;
27
21
import { ACTIONS } from "solana-agent-kit" ;
22
+ import { getSAK } from "../utils" ;
28
23
29
24
const TRANSFER_ACTION = ACTIONS . TRANSFER_ACTION ;
30
25
@@ -100,6 +95,7 @@ export default {
100
95
callback ?: HandlerCallback
101
96
) : Promise < boolean > => {
102
97
elizaLogger . log ( "Starting SEND_TOKEN handler..." ) ;
98
+ const sak = await getSAK ( runtime ) ;
103
99
104
100
// Initialize or update state
105
101
if ( ! state ) {
@@ -134,87 +130,21 @@ export default {
134
130
}
135
131
136
132
try {
137
- const { keypair : senderKeypair } = await getWalletKey (
138
- runtime ,
139
- true
140
- ) ;
141
-
142
- const connection = new Connection ( settings . RPC_URL ! ) ;
143
133
144
134
const mintPubkey = new PublicKey ( content . tokenAddress ) ;
145
135
const recipientPubkey = new PublicKey ( content . recipient ) ;
146
136
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 ] ) ;
206
137
207
- // Send transaction
208
- const signature = await connection . sendTransaction ( transaction ) ;
138
+ const txId = await sak . transfer ( recipientPubkey , Number ( content . amount ) , mintPubkey )
209
139
210
- console . log ( "Transfer successful:" , signature ) ;
140
+ console . log ( "Transfer successful:" , txId ) ;
211
141
212
142
if ( callback ) {
213
143
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 } ` ,
215
145
content : {
216
146
success : true ,
217
- signature,
147
+ signature : txId ,
218
148
amount : content . amount ,
219
149
recipient : content . recipient ,
220
150
} ,
0 commit comments