solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.56k stars 2.08k forks source link

[spl-token]: getOrCreateAssociatedTokenAccount throws TokenAccountNotFoundError for different errors #3326

Open GrimLothar opened 2 years ago

GrimLothar commented 2 years ago

I noticed that when calling getOrCreateAssociatedTokenAccount, TokenAccountNotFoundError gets thrown in very different situations. I'm wondering if this can be improved to throw more specific errors?

2 specific scenarios that I found is when the payer account doesn't have enough SOL, it throws this error And also when the method takes too long, it also throws this method even though the account is created after the fact.

cryguy commented 2 years ago

+1, it appears as if in the scenario above, where the payer does not have enough SOL, the usual error about not having enough SOL is caught by the try catch block in the following code

https://github.com/solana-labs/solana-program-library/blob/f487f520bf10ca29bf8d491192b6ff2b4bf89710/token/js/src/actions/getOrCreateAssociatedTokenAccount.ts#L65-L81

After the error is caught, the getAccount will then fail as the account does not actually exist. A better error message would be helpful here.

Any help is much appreciated. Thanks.

GonzaloAraujoC commented 2 years ago

Hi, it doesn't only happen when there is no SOL, it is happening to us in certain occasions, i.e., I can't extract the token account from a specific address... from all the others it does... I don't understand why this happens... for now we have fixed it by placing the account directly in the code, but this is not functional. Please help.

var fromTokenAccount;

    if(payer.publicKey=="X"){

        fromTokenAccount="Y";
        console.log("     Forcing Y as fromTokenAccount");

    }else{

        fromTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
            connection,
            payer.publicKey, 
            TOKEN_pubkey,
            fromWallet.publicKey,
            false,
            splToken.TOKEN_PROGRAM_ID,
            TOKEN_pubkey
        )   
    }
emberist commented 2 years ago

+1 here, I have this error too, but I have SOL in the payer wallet So it's the second case, did anyone found another way to handle this?

sofinico commented 2 years ago

+1 also, I'm having the same problem. Same conditions as @GonzaloAraujoC .

kudarukuni commented 2 years ago

+1 also have the same error though I'm assuming that rent fees are > 0.002, but it could be @GonzaloAraujoC assumption either. please help with this error. wouldn't want to continue losing SOL tokens whilst trying to mint tokens

jesign commented 1 year ago

+1, i have the same problem.. for me i still have plenty of SOLs. it's happening randomly.

aliyukamilu commented 1 year ago

+1, I am having the same problem. I have enough SOL

KartikSoneji commented 1 year ago

@GonzaloAraujoC for the specific public key X, the token account might be an auxiliary token account, not the canonical one. You can read up on auxiliary token accounts here: https://stackoverflow.com/questions/70555227/what-is-solana-spl-token-aux-account

incogiscool commented 1 year ago

Had this issue just fetching the token account. I ended up just switching the function to this:

const accounts = await this.connection.getTokenAccountsByOwner(
      //@ts-ignore
      this.publicKey,
      { nftMint }
    );

    const nftTokenAcc = accounts.value[0].pubkey;
aliyukamilu commented 1 year ago

+1, I am having the same problem. I have enough SOL

I was able to fix my problem. My issue was I made a mistake in the token address, I was using a wrong address.

jordaaash commented 1 year ago

Thanks everyone, I've locked this issue for now since there's enough context on the problem. @grimAgent did a good job of describing what's happening -- the error types being thrown aren't as specific as they could be.