我正在尝试用 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 = ¶ms->ka[0];
SolAccountInfo *destination_info = ¶ms->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, ¶ms, SOL_ARRAY_SIZE(accounts))) {
return ERROR_INVALID_ARGUMENT;
}
return transfer(¶ms);
}
但是,这一直失败,并出现错误“-32003 事务创建失败。”