2

What does the get_associated_token_address() function return if the associated token addresshas not yet been created (function create_associated_token_account() has not yet been executed) ?

use spl_associated_token_account::{
    get_associated_token_address,
    .....
};

//this code in pub fn process_instruction
let test_res = get_associated_token_address(test_account.key, &pubkey_test_token_mint);
msg!("test associated_token_address {}", test_res.to_string());

when executing this code, in log I see:

test associated_token_address 4MMJzbf9FYcWgQSbSMCHojUBVbJZkNoE3YBYXG1UtTwm

if I change test_account I still get the same message. What kind of address is this and how can I check that the associated token address has not yet been created?

2 Answers 2

1

the return will be the same regardless if the associated token account has been created or not.

When i use it with javascript, i use getParsedAccountInfo to check if the account had been created or not. If needed, i add an instruction in my Tx to create it.

If you want to do it with pure rust, i guess there is a similar method (like getParsedAccountInfo, but i don't know it)

If you want to do it with rust + anchor, anyway you will have to include the account in your transaction's accounts if you want to be able use it. If it is not created, you will get an error, account not initialized.

1

Elaborating on Effe's answer, get_associated_token_address is a function that is run locally. It does not touch the chain and it does not care whether the associated token account (ATA) has actually been created or not.

If you pass the ATA account key into an endpoint that expects the account to be initialized (like transferring an SPL token), then you will get an error like Effe said.

If you pass the ATA account key into an endpoint that expects the account not to be initialized (such as initializing an ATA account), then the account will be initialized at the ATA address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.