File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55 getAssociatedTokenAddress ,
66 createTransferInstruction ,
77 getMint ,
8+ getAccount ,
9+ createAssociatedTokenAccountInstruction ,
810} from "@solana/spl-token" ;
911
1012/**
@@ -36,18 +38,33 @@ export async function transfer(
3638
3739 tx = await agent . connection . sendTransaction ( transaction , [ agent . wallet ] ) ;
3840 } else {
41+ const transaction = new Transaction ( ) ;
3942 // Transfer SPL token
4043 const fromAta = await getAssociatedTokenAddress (
4144 mint ,
4245 agent . wallet_address ,
4346 ) ;
4447 const toAta = await getAssociatedTokenAddress ( mint , to ) ;
4548
49+ try {
50+ await getAccount ( agent . connection , toAta ) ;
51+ } catch {
52+ // Error is thrown if the tokenAccount doesn't exist
53+ transaction . add (
54+ createAssociatedTokenAccountInstruction (
55+ agent . wallet_address ,
56+ toAta ,
57+ to ,
58+ mint ,
59+ ) ,
60+ ) ;
61+ }
62+
4663 // Get mint info to determine decimals
4764 const mintInfo = await getMint ( agent . connection , mint ) ;
4865 const adjustedAmount = amount * Math . pow ( 10 , mintInfo . decimals ) ;
4966
50- const transaction = new Transaction ( ) . add (
67+ transaction . add (
5168 createTransferInstruction (
5269 fromAta ,
5370 toAta ,
You can’t perform that action at this time.
0 commit comments