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

使用splToken进行Solana自定义代币转账时签名报错求助

Solana SPL代币转账报错:Error processing instruction 2: invalid account data for instruction 排查与解决

以下是常见问题原因及对应修复方案:

1. 接收方关联代币账户未初始化

SPL代币转账要求目标账户必须是已初始化的有效代币账户。如果接收方还未创建该代币的关联账户(ATA),直接调用Transfer指令会触发该错误。

修复:
转账前检查接收方的ATA是否存在,若不存在则先添加创建ATA的指令到交易中,示例代码已包含该逻辑。

2. 签名者权限不匹配

createTransferInstruction的第四个参数是代币账户的授权者,必须确保该账户是from代币账户的所有者(或已被授权)。如果this.publicKey不是from账户的合法授权者,会导致指令验证失败。

验证方法:

const fromAccount = await this.connection.getParsedAccountInfo(from);
console.log(fromAccount.value.data.parsed.info.owner); // 输出应等于this.publicKey的字符串形式

3. 拼写错误导致无效账户

你的代码中settings.reciver存在拼写错误(正确应为receiver),若变量值因此无效,会生成错误的接收方账户地址,进而触发报错。

修复:
修正变量名拼写为settings.receiver,并确认其值为合法的Solana公钥字符串。

修改后的完整代码

transferToken = async () => {
    const tokenAddress = new solanaWeb3.PublicKey("5jFnsfx36DyGk8uVGrbXnVUMTsBkPXGpx6e69BiGFzko");
    // 修正拼写错误:reciver → receiver
    const receiver = new solanaWeb3.PublicKey(settings.receiver);

    // 使用finalized区块哈希提升交易稳定性
    const blockhash = await this.connection.getRecentBlockhash("finalized");

    const from = await splToken.Token.getAssociatedTokenAddress(
        splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
        splToken.TOKEN_PROGRAM_ID,
        tokenAddress,
        this.publicKey
    );

    const to = await splToken.Token.getAssociatedTokenAddress(
        splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
        splToken.TOKEN_PROGRAM_ID,
        tokenAddress,
        receiver
    );

    const instructions = [];
    // 检查接收方ATA是否存在
    const toAccountInfo = await this.connection.getAccountInfo(to);
    if (!toAccountInfo) {
        // 添加创建ATA的指令
        instructions.push(
            splToken.Token.createAssociatedTokenAccountInstruction(
                splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
                splToken.TOKEN_PROGRAM_ID,
                tokenAddress,
                to,
                receiver,
                this.publicKey // 支付账户创建费用的账户
            )
        );
    }

    // 添加转账指令
    instructions.push(
        splToken.Token.createTransferInstruction(
            splToken.TOKEN_PROGRAM_ID,
            from,
            to,
            this.publicKey,
            [],
            1000000000
        )
    );

    const transaction = new solanaWeb3.Transaction({
        recentBlockhash: blockhash.blockhash,
        feePayer: this.publicKey
    });
    transaction.add(...instructions);

    const signedTransaction = await this.provider.signTransaction(transaction);
    // 序列化交易后发送
    const signature = await this.connection.sendRawTransaction(signedTransaction.serialize());
    // 使用finalized确认级别确保交易完成
    await this.connection.confirmTransaction(signature, "finalized");
    console.log(signature);
}

额外检查点

  • 确认转账金额1000000000与代币的小数位数匹配(例如9位小数对应1个代币单位)。
  • 检查this.connection是否连接到正确的Solana网络(主网、测试网或本地节点)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 11:47:15