You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Solana Devnet质押SOL报错:指令程序ID不正确

Solana Devnet质押委托报错:incorrect program id for instruction 解决方案

问题场景

在Solana Devnet执行SOL质押委托操作,钱包内有2 SOL,计划质押1 SOL时触发交易失败,错误日志如下:

SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 1: incorrect program id for instruction
    at Connection.sendEncodedTransaction (.../node_modules/@solana/web3.js/src/connection.ts:4243:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Connection.sendRawTransaction (.../node_modules/@solana/web3.js/src/connection.ts:4205:20)
    at async Object.sendAndConfirmRawTransaction (.../node_modules/@solana/web3.js/src/util/send-and-confirm-raw-transaction.ts:73:21) {
  logs: [
    'Program 11111111111111111111111111111111 invoke [1]',
    'Program 11111111111111111111111111111111 success',
    'Program Stake11111111111111111111111111111111111111 invoke [1]',
    'Program Stake11111111111111111111111111111111111111 failed: incorrect program id for instruction'
  ]
}

错误原因

这个错误的核心是交易中的指令与指定的程序ID不匹配,具体到质押场景,常见原因包括:

  • 手动构造质押指令时,错误使用了不符合Stake程序要求的指令格式或参数
  • 调用Stake程序时,传入的指令类型并非该程序支持的指令(比如把系统程序的指令发给了Stake程序)
  • 指令布局错误,导致Stake程序无法识别指令内容

解决步骤

  1. 使用官方SDK构造质押指令
    不要手动拼接指令数据,直接用@solana/web3.js提供的StakeProgram工具类生成合规的质押相关指令,比如创建质押账户、委托质押等。

  2. 验证程序ID正确性
    确保所有质押相关指令都使用正确的Stake程序ID:Stake11111111111111111111111111111111111111,避免拼写错误或混淆主网/测试网程序ID。

  3. 检查交易指令顺序与关联性
    质押委托的完整流程通常需要先创建质押账户(若不存在),再执行委托操作。确保每个指令对应正确的程序,且指令间的账户依赖关系正确。

  4. 提前模拟交易验证
    在发送真实交易前,使用connection.simulateTransaction()方法模拟执行,提前排查指令错误,避免消耗手续费。

正确的质押委托示例代码

import { Connection, Keypair, PublicKey, Transaction, StakeProgram, LAMPORTS_PER_SOL } from '@solana/web3.js';

async function delegateStakeToValidator() {
  // 初始化Devnet连接
  const connection = new Connection('https://api.devnet.solana.com', 'confirmed');
  // 加载钱包密钥(替换为你的钱包私钥)
  const wallet = Keypair.fromSecretKey(Uint8Array.from(/* 你的私钥数组 */));
  // 替换为目标验证者的投票账户地址
  const validatorVotePubkey = new PublicKey('YOUR_VALIDATOR_VOTE_ADDRESS');

  // 1. 生成新的质押账户密钥对
  const stakeAccount = Keypair.generate();

  // 构建创建质押账户的指令
  const createStakeAccountIx = StakeProgram.createAccount({
    fromPubkey: wallet.publicKey,
    stakePubkey: stakeAccount.publicKey,
    authorized: {
      staker: wallet.publicKey,
      withdrawer: wallet.publicKey,
    },
    lamports: LAMPORTS_PER_SOL * 1, // 质押1 SOL
  });

  // 构建委托质押的指令
  const delegateStakeIx = StakeProgram.delegate({
    stakePubkey: stakeAccount.publicKey,
    authorizedPubkey: wallet.publicKey,
    votePubkey: validatorVotePubkey,
  });

  // 组装交易并发送
  const transaction = new Transaction().add(createStakeAccountIx, delegateStakeIx);
  const signature = await connection.sendTransaction(transaction, [wallet, stakeAccount]);
  await connection.confirmTransaction(signature);
  
  console.log('质押委托完成,交易签名:', signature);
}

// 执行函数
delegateStakeToValidator();

内容的提问来源于stack exchange,提问作者Rudraksh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 12:25:27