首页 > 解决方案 > 如何使用 javascript 将交易发送到 solana 中的链上程序?

问题描述

我尝试向 SOLANA 中的链上程序发送带有指令的交易。我收到此错误: 错误:发送交易失败:交易模拟失败:错误处理指令 0:指令从它不拥有的帐户余额中花费

我的 Javascript 代码是:

var solana_web3 = require('@solana/web3.js');
const { SSL_OP_EPHEMERAL_RSA } = require('constants');
const url = 'https://devnet.solana.com' ;
const sigSecretKey1  = [39,90,157,237,...] ;
const sigSecretKey2  = [39,90,157,237,...] ;
const progID = 'FpbcvcsCB...' ;
connection = new solana_web3.Connection(url, 'singleGossip');
const account_signer_source = new solana_web3.Account( Buffer.from(sigSecretKey1) );
const account_signer_destination = new solana_web3.Account( Buffer.from(sigSecretKey2) );
const pub_programId = new solana_web3.PublicKey(progID) ;
const instruction = new solana_web3.TransactionInstruction({
    keys: [ 
      {pubkey : account_signer_source.publicKey,        isSigner : true,  isWritable: true},  
      {pubkey : account_signer_destination.publicKey,   isSigner : false, isWritable: true},
    ],
    programId : pub_programId,
    data: Buffer.from([]),
  });
  const transaction = new solana_web3.Transaction().add(instruction);
  //transaction.addSignature(account_signer_source.publicKey, Buffer.from(sigSecretKey1));
  await solana_web3.sendAndConfirmTransaction(
    connection,
    transaction,
    [account_signer_source],
    {commitment: 'singleGossip', preflightCommitment: 'singleGossip',}
).then(()=>{console.log('done')}).catch((e)=>{console.log(e)});

请问你能帮帮我吗 ?

谢谢礼萨

标签: javascriptblockchain

解决方案


您正在从该程序不拥有的帐户中取走灯。您只能从程序拥有的帐户中扣除灯,即我的代币交换程序可以在它拥有的所有帐户上扣除灯,而不能从其他帐户中扣除灯。如果你想在你的程序中移动灯,你需要调用 system_instruction::transfer


推荐阅读