Search code examples
typescriptsolanasolana-web3js

index out of bounds error in my Solana program


I'm learning how to program on Solana's blockchain and well I'm stuck...

I'm trying to run this code


import web3 = require('@solana/web3.js');

const connection = new web3.Connection(web3.clusterApiUrl("devnet"));

const key: Uint8Array = Uint8Array.from([10,120,79,226,98,171,155,229,251,24,251,52,63,137,153,185,225,17,178,76,68,30,12,227,44,21,185,250,30,103,169,167,130,84,37,163,36,71,217,34,1,45,246,144,207,196,155,167,108,57,114,39,142,246,59,169,109,97,182,246,227,57,26,123]);
const programId = new web3.PublicKey("9mkRQSS5a25cKuYPnvDQFEGA5aKHkauVHf6SmiTASvqp");

console.log('hello');


async function main() {

    // Create signer
    const signer: web3.Keypair = web3.Keypair.fromSecretKey(key);

    // Verify balance
    await connection.getBalance(signer.publicKey).then(balance => {
        console.log("SOL: ", balance / web3.LAMPORTS_PER_SOL);
    })

    // To call our Solana program we need to collect a 
    // series of instructions into a transaction
    const transaction: web3.Transaction = new web3.Transaction().add(
        new web3.TransactionInstruction({
            keys: [],
            programId
        })
    );

    await web3
        .sendAndConfirmTransaction(connection, transaction, [signer])
        .then((sig) => {
            console.log("sig: {}", sig);
        });

}

main()

I have this error

/home/zen/solana_test/cli/node_modules/@solana/web3.js/src/connection.ts:3689
      throw new SendTransactionError(
            ^
SendTransactionError: failed to send transaction: invalid transaction: index out of bounds
    at Connection.sendEncodedTransaction (/home/zen/solana_test/cli/node_modules/@solana/web3.js/src/connection.ts:3689:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Connection.sendRawTransaction (/home/zen/solana_test/cli/node_modules/@solana/web3.js/src/connection.ts:3649:20)
    at async Connection.sendTransaction (/home/zen/solana_test/cli/node_modules/@solana/web3.js/src/connection.ts:3637:12)
    at async Object.sendAndConfirmTransaction (/home/zen/solana_test/cli/node_modules/@solana/web3.js/src/util/send-and-confirm-transaction.ts:29:21)
    at async main (/home/zen/solana_test/cli/index.ts:41:5) {
  logs: undefined
}

All that I'm trying to do is send this transaction and look at whether or not it is working by analyzing solana log 9mkRQSS5a25cKuYPnvDQFEGA5aKHkauVHf6SmiTASvqp in the terminal.

What am I missing?


Solution

  • Your instruction is missing the data payload which informs the program what instruction you want it to execute.

    Also, you have to ensure that your are communicating to the correct cluster where your (or the) program is deployed.

    Finally, I see what your using as programId in the transaction instruction but that account is not an executable Solana program. If you have a program running in devnet you need to use its ID not the one of the account you provided.