简体   繁体   English

如何使用 javascript 将交易发送到 solana 中的链上程序?

[英]How to send a transaction to an onchain program in solana with javascript?

I try to send a transaction with an instruction to an onchain program in SOLANA.我尝试向 SOLANA 中的链上程序发送带有指令的交易。 I get this error: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: instruction spent from the balance of an account it does not own我收到此错误:错误:发送交易失败:交易模拟失败:错误处理指令 0:指令从它不拥有的帐户余额中花费

My Javascript code is:我的 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)});

Could you help me please?请问你能帮帮我吗?

Thx Reza谢谢礼萨

you're taking away lamports from an account that the program doesn't own.您正在从该程序不拥有的帐户中取走灯。 You can only deduct lamports from accounts owned by the program, ie my token-swap program can deduct lamports on all accounts that it owns, and no others.您只能从程序拥有的帐户中扣除灯,即我的代币交换程序可以在它拥有的所有帐户上扣除灯,而不能从其他帐户中扣除灯。 If you want to move lamports within your program, you need to invoke system_instruction::transfer如果你想在你的程序中移动灯,你需要调用 system_instruction::transfer

The error message basically means the program is not the owner of the account whose balance being spent.该错误消息基本上意味着该程序不是其余额被花费的帐户的所有者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM