Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction with CreateAssociatedTokenAccount getComputeEstimate returns Invalid Seed in V2 but works in V1 #55

Closed
agrin96 opened this issue Jan 11, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@agrin96
Copy link

agrin96 commented Jan 11, 2025

Overview

While trying to create a simple program that sends buy and sell transactions with the V2 library I get to the stage where I want to simulate the transaction to calculate the compute limit requirements, but it throws an error InvalidSeed. I minimally reproduced the code using the V1 library and it seems to work fine with the same parameters.

Steps to reproduce

This is the V2 Version of the code which is not working

async function testAssociatedTokenAccount(){
    const rpc = createRpc();
    const testMint = address("BRy35xfA7uNbne9A8CxRjQ5hAg3PMhDTGxu5PHaD2YJC")

    const rawBytes = bs58.decode(PERSONAL_SECRET);
    const payer = await createKeyPairFromBytes(rawBytes);
    const signer = await createSignerFromKeyPair(payer);

    const [associatedTokenAddress, bump] = await findAssociatedTokenPda({
        mint: testMint,
        owner: signer.address,
        tokenProgram: address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"),
    })
    const createTokenAccountInstruction = getCreateAssociatedTokenIdempotentInstruction({
        ata: associatedTokenAddress,
        mint: testMint,
        owner: signer.address,
        payer: signer,
    })

    const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
    const transactionmessage = pipe(
        createTransactionMessage({ version: 0}),
        tx => setTransactionMessageFeePayerSigner(signer, tx),
        tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
        tx => appendTransactionMessageInstruction(getSetComputeUnitPriceInstruction({ microLamports: 5000n }), tx),
        tx => appendTransactionMessageInstruction(createTokenAccountInstruction, tx),
    )
    const getComputeUnitEstimate = getComputeUnitEstimateForTransactionMessageFactory({ rpc });
    try{
        const estimate = await getComputeUnitEstimate(transactionmessage)
    }catch(e) {
        console.dir(e.cause, {depth: null})
    }
}

And this is the working V1 version

async function main(){
    const signer = Keypair.fromSecretKey(bs58.decode(PERSONAL_SECRET))
    const mint = new PublicKey('BRy35xfA7uNbne9A8CxRjQ5hAg3PMhDTGxu5PHaD2YJC');
    const associatedTokenAddress = await getAssociatedTokenAddress(
        mint,
        signer.publicKey,
    )
    console.log(associatedTokenAddress.toBase58())

    const createTokenAccountInstruction = await createAssociatedTokenAccountIdempotentInstruction(
        signer.publicKey,
        associatedTokenAddress,
        signer.publicKey,
        mint
    )
    const connection = new Connection(HELIUS_RPC_URL)

    const blockhash = await connection.getLatestBlockhash()
    const transaction = (
        new Transaction({
            feePayer: signer.publicKey,
            blockhash: blockhash.blockhash,
            lastValidBlockHeight: blockhash.lastValidBlockHeight,
        })
        .add(ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 5000n }))
        .add(createTokenAccountInstruction)
    )
    const units = await getSimulationComputeUnits(
        connection,
        transaction.instructions,
        signer.publicKey,
        []
    )
    console.log(units)
}

Description of bug

In both cases, the calculated AssociatedTokenAddress is 7PKtoocp9KBkcaRXPr6j5oib6KMFmjVhfGq1H4Sm33tm, but in the case of V2 the error is

cause: {
  InstructionError: [ 1n, "InvalidSeeds" ],
},
 context: {
  __code: 5663019,
  unitsConsumed: 3609,
}

I believe this is a bug. However I would be happy to be proven wrong and told Im doing it wrong. Appreciate any help figuring this out. Thank you!

@agrin96 agrin96 added the bug Something isn't working label Jan 11, 2025
@steveluscher
Copy link
Collaborator

Is it at all possible that you're using the getCreateAssociatedTokenIdempotentInstruction() function from @solana-program/token-2022 that defaults the token program address (when not supplied) to the token-2022 program, which is incompatible with the ATA you're generating with TokenkegQfe... explicitly overridden?

@agrin96
Copy link
Author

agrin96 commented Jan 13, 2025

Wow! That was it! Thanks so much! Its unfortunate that that isn't very clear in the API/docs of the instruction itself. I pretty much had given up here.

@agrin96 agrin96 closed this as completed Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants