【问题标题】:index out of bounds error in my Solana program我的 Solana 程序中的索引越界错误
【发布时间】:2022-08-24 01:31:47
【问题描述】:

我正在学习如何在 Solana 的区块链上编程,但是我被卡住了……

我正在尝试运行此代码


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()

我有这个错误

/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
}

我要做的就是发送这个交易并通过分析终端中的solana log 9mkRQSS5a25cKuYPnvDQFEGA5aKHkauVHf6SmiTASvqp 来查看它是否有效。

我错过了什么?

    标签: typescript solana solana-web3js


    【解决方案1】:

    您的指令缺少数据有效负载,该有效负载会通知程序您希望它执行什么指令。

    此外,您必须确保您正在与部署您的(或该)程序的正确集群进行通信。

    最后,我看到您在交易指令中使用programId,但该帐户不是可执行的 Solana 程序。如果您有一个在 devnet 中运行的程序,您需要使用它的 ID 而不是您提供的帐户之一。

    【讨论】:

    • 奇怪...我添加了一些数据,但仍然得到同样的错误。
    • @teal'c 我在执行您的代码时添加了更多信息,但发现您使用的 programId 不是 Solana 程序帐户,它只是一个系统帐户。
    • 就是这样,谢谢。
    猜你喜欢
    • 2022-01-13
    • 2014-06-06
    • 2015-03-22
    • 1970-01-01
    • 2013-01-17
    • 2013-10-13
    • 2015-01-10
    相关资源
    最近更新 更多