3

I'm new to Solana development and I'm currently trying to initialize an Account using the following code:


import { 
    Connection, 
    Keypair, 
    PublicKey, 
    Transaction, 
    SystemProgram,
    sendAndConfirmTransaction,
    SYSVAR_RENT_PUBKEY
} from "@solana/web3.js";

import { 
    createInitializeAccountInstruction,
    TOKEN_PROGRAM_ID 
} from "@solana/spl-token";

import bs58 from "bs58";

const secret = new Uint8Array(bs58.decode("..."));
const payer = Keypair.fromSecretKey(secret);

async function main(signer=payer){
    const connection = new Connection("https://api.devnet.solana.com");
    const blockInfo = connection.getLatestBlockhash();
    const transaction = new Transaction({
        recentBlockhash: (await blockInfo).blockhash,
    });

    const tokenAccount = Keypair.generate();
    console.log(bs58.encode(tokenAccount.publicKey.toBuffer()));
    const rent = await connection.getMinimumBalanceForRentExemption(165);

    const tokenSwapStateAccountInstruction = await SystemProgram.createAccount({
        newAccountPubkey: tokenAccount.publicKey,
        fromPubkey: payer.publicKey,
        lamports: rent,
        space: 165,
        programId: TOKEN_PROGRAM_ID,
    });

    transaction.add(tokenSwapStateAccountInstruction);

    const initInstruction = createInitializeAccountInstruction(
        tokenAccount.publicKey,
        new PublicKey("4ThReWAbAVZjNVgs5Ui9Pk3cZ5TYaD9u6Y89fp6EFzoF"),
        signer.publicKey,
        TOKEN_PROGRAM_ID
    );

    transaction.add( initInstruction );
    transaction.sign( signer);
    const response = await sendAndConfirmTransaction(connection, transaction, [signer, tokenAccount]);
    console.log(response);
};

main();

However, I encountered an error message that says:

  logs: [
    'Program 11111111111111111111111111111111 invoke [1]',
    'Program 11111111111111111111111111111111 success',
    'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]',
    'Program log: Instruction: InitializeAccount',
    'Program log: Error: IncorrectProgramId',
    'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2926 of 400000 compute units',
    'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA failed: incorrect program id for instruction'
  ]```

Could someone please help me understand what might be causing this error and how I can fix it?

Any assistance or insights would be greatly appreciated. Thank you in advance for your help!
2
  • Hey. This is likely related to the mint address parameter which you passed the value new PublicKey("4T..zoF") to. Where is this value from?
    – Ademola
    Jul 31, 2023 at 20:21
  • Thank you for your reply. I've resolved the issue now. I apologize for leaving it as it was, not knowing how to close it.
    – K T
    Aug 5, 2023 at 11:09

1 Answer 1

5

I think this error is being thrown because the Mint address you are providing ("4ThReWAbAVZjNVgs5Ui9Pk3cZ5TYaD9u6Y89fp6EFzoF") does not exist (and therefore not a valid Mint account owned by the token program).

So you have a few account here:

  • Signer/Payer: this is the wallet that is creating a token account
  • TokenAccount: this is the address of the new token account being created
  • Mint: is the specific token type that the token account will utilize (e.g., wBTC, SAMO, mSOL, etc.).

The account you provided is not a Mint account (it's a standard System Account or wallet owned by the System Program, not the Token Program). You can see that here.

Here's an example (USDC) of a mint account (details about a specific token: address, supply, etc.)

I suspect if you replace your mint address with an existing devnet token mint (e.g., "EofYstMevXcYTuNZ14gsJfVFtdFSBtShP3mr77rEqtZr") (a fake USDC coin), your script will run. Ultimately what you will have created is a fUSDC token account owned by your signer.

If you want to make your own token account you can do something like this:

import { 
    createMint,
} from "@solana/spl-token";
//...
     const newMint = await createMint(connection, signer, signer.publicKey, null, 2)

This will generate a new token mint that has 2 decimals.

Also, if you're just creating a simple token account, Solana has a program called the Associated Token Program that makes it a little easier to create token accounts associated with a user/wallet:

import { 
    createAssociatedTokenAccountIdempotent
} from "@solana/spl-token";
//...
     await createAssociatedTokenAccountIdempotent(connection, payer, mint, signer.publicKey)

This will ultimately create a token account (for token mint owned by your signer).

Token accounts are tricky when you're getting started--please follow-up if this doesn't work or if you have other questions.

2
  • 1
    this visual may be helpful: quicknode.com/guides/solana-development/spl-tokens/…
    – AMilz
    Jul 31, 2023 at 22:33
  • 1
    Thank you so much for the detailed response! I followed your advice and it worked! The article you shared with me was informative and helped me understand the subject even better.Thank you so much for your kindness!
    – K T
    Aug 4, 2023 at 15:01

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.