我正试图建立一个简单的游戏测试,你输入sol金额和程序只是发送两倍的金额(不是在主网,ofc),但我只是没有得到一些关于solana的东西。
现在我没有代码,因为我正试图理解工作流程
在这里我头脑风暴我的程序应该是什么样子
我不知道如何创建这个国库钱包帐户?它会属于我的程序吗?
你能不能给我看一段代码,比如这个play函数,让我可以和它交互?
我的猜测是,我将只写公共地址,然后在程序中写一个from/to函数来完成交易。对吗?
我将使用锚。谢谢你的帮助:)
您的国库钱包,因为它持有SOL,可以简单地使用您的程序id派生PDA,其中不包含任何数据。它仍然由系统程序拥有,但是由于它是从您的程序派生出来的,所以只有您的程序可以为它签名。在您的程序中,您将执行如下操作:
fn transfer_one_token_from_escrow(
program_id: &Pubkey,
accounts: &[AccountInfo],
) -> ProgramResult {
// User supplies the destination
let alice_pubkey = accounts[1].key;
// Iteratively derive the escrow pubkey
let (escrow_pubkey, bump_seed) = Pubkey::find_program_address(&[&["escrow"]], program_id);
// Create the transfer instruction
let instruction = token_instruction::transfer(&escrow_pubkey, alice_pubkey, 1);
// Include the generated bump seed to the list of all seeds
invoke_signed(&instruction, accounts, &[&["escrow", &[bump_seed]]])
}
你可能需要做更多的研究来确切地理解如何实现这些位。以下是一些资源:
- Solana Account Model: https://solanacookbook.com/core-concepts/accounts.html#account-model
- 使用pda: https://docs.solana.com/developing/programming-model/calling-between-programs#using-program-addresses
- 带锚的pda: https://book.anchor-lang.com/chapter_3/PDAs.html