11use basic_solana:: {
2- client, solana_wallet:: SolanaWallet , spl, state :: init_state , validate_caller_not_anonymous ,
3- InitArg ,
2+ client, solana_wallet:: SolanaWallet , spl:: transfer_instruction_with_program_id ,
3+ state :: init_state , validate_caller_not_anonymous , InitArg ,
44} ;
55use candid:: { Nat , Principal } ;
66use ic_cdk:: { init, post_upgrade, update} ;
@@ -12,6 +12,10 @@ use solana_message::Message;
1212use solana_pubkey:: Pubkey ;
1313use solana_system_interface:: instruction;
1414use solana_transaction:: Transaction ;
15+ use spl_associated_token_account_interface:: {
16+ address:: get_associated_token_address_with_program_id,
17+ instruction:: create_associated_token_account_idempotent,
18+ } ;
1519use std:: str:: FromStr ;
1620
1721#[ init]
@@ -45,7 +49,7 @@ pub async fn associated_token_account(owner: Option<Principal>, mint_account: St
4549 let owner = owner. unwrap_or_else ( validate_caller_not_anonymous) ;
4650 let wallet = SolanaWallet :: new ( owner) . await ;
4751 let mint = Pubkey :: from_str ( & mint_account) . unwrap ( ) ;
48- spl :: get_associated_token_address (
52+ get_associated_token_address_with_program_id (
4953 wallet. solana_account ( ) . as_ref ( ) ,
5054 & mint,
5155 & get_account_owner ( & mint) . await ,
@@ -175,30 +179,15 @@ pub async fn create_associated_token_account(
175179 let payer = wallet. solana_account ( ) ;
176180 let mint = Pubkey :: from_str ( & mint_account) . unwrap ( ) ;
177181
178- let ( associated_token_account, instruction) = spl:: create_associated_token_account_instruction (
182+ let account_owner = get_account_owner ( & mint) . await ;
183+
184+ let instruction = create_associated_token_account_idempotent (
179185 payer. as_ref ( ) ,
180186 payer. as_ref ( ) ,
181187 & mint,
182- & get_account_owner ( & mint ) . await ,
188+ & account_owner ,
183189 ) ;
184190
185- if let Some ( _account) = client
186- . get_account_info ( associated_token_account)
187- . with_encoding ( GetAccountInfoEncoding :: Base64 )
188- . send ( )
189- . await
190- . expect_consistent ( )
191- . unwrap_or_else ( |e| {
192- panic ! ( "Call to `getAccountInfo` for {associated_token_account} failed: {e}" )
193- } )
194- {
195- ic_cdk:: println!(
196- "[create_associated_token_account]: Account {} already exists. Skipping creation of associated token account" ,
197- associated_token_account
198- ) ;
199- return associated_token_account. to_string ( ) ;
200- }
201-
202191 let message = Message :: new_with_blockhash (
203192 & [ instruction] ,
204193 Some ( payer. as_ref ( ) ) ,
@@ -219,7 +208,7 @@ pub async fn create_associated_token_account(
219208 . expect ( "Call to `sendTransaction` failed" )
220209 . to_string ( ) ;
221210
222- associated_token_account . to_string ( )
211+ get_associated_token_address_with_program_id ( payer . as_ref ( ) , & mint , & account_owner ) . to_string ( )
223212}
224213
225214#[ update]
@@ -317,16 +306,11 @@ pub async fn send_spl_token(
317306
318307 let token_program = get_account_owner ( & mint) . await ;
319308
320- let from = spl :: get_associated_token_address ( payer. as_ref ( ) , & mint, & token_program) ;
321- let to = spl :: get_associated_token_address ( & recipient, & mint, & token_program) ;
309+ let from = get_associated_token_address_with_program_id ( payer. as_ref ( ) , & mint, & token_program) ;
310+ let to = get_associated_token_address_with_program_id ( & recipient, & mint, & token_program) ;
322311
323- let instruction = spl:: transfer_instruction_with_program_id (
324- & from,
325- & to,
326- payer. as_ref ( ) ,
327- amount,
328- & token_program,
329- ) ;
312+ let instruction =
313+ transfer_instruction_with_program_id ( & from, & to, payer. as_ref ( ) , amount, & token_program) ;
330314
331315 let message = Message :: new_with_blockhash (
332316 & [ instruction] ,
0 commit comments