简体   繁体   English

"solana@web3.js \/ 程序传输"

[英]solana@web3.js / program transfer

I'm trying to have a simple program written in Solana that, on user approval, runs and transfers a given amount of SOL into the program's owner account.我正在尝试用 Solana 编写一个简单的程序,该程序在用户批准后运行并将给定数量的 SOL 转移到程序的所有者帐户中。

From JS side I've this based on the HelloWorld example from Solana repo在 JS 方面,我基于 Solana repo 中的 HelloWorld 示例

        ...
        const secretKeyString = await fs.readFile(filePath, {encoding: 'utf8'});
        const secretKey = Uint8Array.from(JSON.parse(secretKeyString))
        const keypair = Keypair.fromSecretKey(secretKey)

        const instruction = new TransactionInstruction({
            keys: [
              {pubkey: publicKey, isSigner: false, isWritable: true}, // from wallet
              {pubkey: keypair.publicKey, isSigner: false, isWritable: true} // from json file, program owner
            ],
            programId,
            data: Buffer.alloc(0)
          });

        const transaction = new Transaction().add(instruction);
        signature = await sendTransaction(transaction, connection);
        //toast('Transaction Sent');

The system level SOL transfer<\/code> has a constraint that the from<\/code> PublicKey must be a system owned account<\/strong><\/em> .系统级 SOL transfer<\/code>有一个约束,即from<\/code> PublicKey 必须是系统拥有的帐户<\/strong><\/em>。 This means accounts owned by programs or PDA will fail as they are owned by your program.这意味着程序或 PDA 拥有的帐户将失败,因为它们归您的程序所有。https:\/\/solanacookbook.com\/references\/accounts.html#transfer<\/a>https:\/\/solanacookbook.com\/references\/accounts.html#transfer<\/a>

The transfer of lamports that your code shows can only happen if the from<\/code> PublicKey is a program owned account<\/strong><\/em> .仅当from<\/code> PublicKey 的帐户是程序拥有的帐户<\/strong><\/em>时,您的代码显示的灯的传输才能发生。 https:\/\/solanacookbook.com\/references\/programs.html#transferring-lamports<\/a> https:\/\/solanacookbook.com\/references\/programs.html#transferring-lamports<\/a>

"

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

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