How to check instruction in Solana on-chain program?

Viewed 344

I am developing game, which guesses number and get reward if they success. This is summary of my program. First, user send amount of sol and his guessing number. Second, Program get random number and store user's sol to vault. Third, Program make random number, if user is right, gives him reward.

Here, how can I check if the user sent correct amount of sol in program?

This is test code for calling program.

const result = await program.rpc.play(
    new anchor.BN(40), 
    new anchor.BN(0),
    new anchor.BN(20000000), 
    _nonce, {
        accounts: {
        vault: vaultPDA,
        user: provider.wallet.publicKey, // User wallet
        storage: storageAccount.publicKey,
        systemProgram: systemProgram
       },
       instructions: [
           SystemProgram.transfer({
               fromPubkey: provider.wallet.publicKey,
               toPubkey: vaultPDA,`enter code here`
               lamports: 20000000`enter code here`
           })
      ],
      signers: [storageAccount]`enter code here`
   }
)
1 Answers

The best solution would be to directly transfer the lamports inside of your program using a cross-program invocation, like this program: Cross-program invocation with unauthorized signer or writable account

Otherwise, from within your program, you can check the lamports on the AccountInfo passed, and make sure it's the proper number, similar to this example: https://solanacookbook.com/references/programs.html#transferring-lamports

The difference there is that you don't need to move the lamports.

Related