Search code examples
solanasolana-web3jsanchor-solana

building a game on solana


I am trying to build a simple game for testing where you enter sol amount and the program just sends double the amount (not on mainnet, ofc) but I just don't get some stuff about solana.

For now i don't have the code cause I am trying to understand the workflow

Here i brainstormed how my program will look like

I don't know how to create this treasury wallet account? Will it be owned by my program?

Could you also show me a piece of code, like this play function that will allow me to interact with it?

My guess rn is that i will just write the public address, and then write a from/to function in the program to do the transaction. Is it correct?

I will be using anchor btw. Thanks for the help :)


Solution

  • Your treasury wallet, since it's holding SOL, can simply be a PDA derived using your program id, with no data in it. It will still be owned by the system program, but since it's derived from your program, only your program can sign for it. In your program, you'll do something like:

    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]]])
    }
    

    You'll likely need to do some more research to understand exactly how to implement a lot of these bits. Here are some resources: