0

我正在尝试用 Solana 编写一个简单的程序,该程序在用户批准后运行并将给定数量的 SOL 转移到程序的所有者帐户中。

在 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');

在程序方面,我在 Solana repo 的 C 示例中简单地使用了一个示例(没有编辑)-因此假设一切都很好

/**
 * @brief A program demonstrating the transfer of lamports
 */
#include <solana_sdk.h>

extern uint64_t transfer(SolParameters *params) {
  // As part of the program specification the first account is the source
  // account and the second is the destination account
  if (params->ka_num != 2) {
    return ERROR_NOT_ENOUGH_ACCOUNT_KEYS;
  }
  SolAccountInfo *source_info = &params->ka[0];
  SolAccountInfo *destination_info = &params->ka[1];

  // Withdraw five lamports from the source
  *source_info->lamports -= 100000000;
  // Deposit five lamports into the destination
  *destination_info->lamports += 100000000;

  return SUCCESS;
}

extern uint64_t entrypoint(const uint8_t *input) {
  SolAccountInfo accounts[2];
  SolParameters params = (SolParameters){.ka = accounts};

  if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(accounts))) {
    return ERROR_INVALID_ARGUMENT;
  }

  return transfer(&params);
}

但是,这一直失败,并出现错误“-32003 事务创建失败。”

4

1 回答 1

0

系统级 SOLtransfer有一个约束,即fromPublicKey 必须是系统拥有的帐户。这意味着程序或 PDA 拥有的帐户将失败,因为它们归您的程序所有。https://solanacookbook.com/references/accounts.html#transfer

from仅当PublicKey 是程序拥有的帐户时,您的代码显示的灯的传输才能发生。https://solanacookbook.com/references/programs.html#transferring-lamports

于 2022-02-07T09:05:11.247 回答