A simple coin flip game using feed FEED PROTOCOL RANDOM NUMBER GENERATOR PROGRAM with anchor framework
Implementing FEED PROTOCOL RANDOM NUMBER GENERATOR PROGRAM (FPRNG) to your program is very easy. You derive the needed accounts and pass into the instruction. And then in your program make a CPI to FPRNG. In these simple example program we will cover every step of the implamentation. Lets say you want to build an on-chain coin flip game. First user chooses heads or tails and send this decision to your coinflip program. Your coin flip program calls FPRNG. FPRNG return a random number to your program. You compare the returned random number with the user's decision in coinflip program. Finally coin flip program logs a message according to result. THIS ALL HAPPENS IN ONE TRANSACTION. You can store the random number in an account in your program. You can also try coinflip program on Devnet and Testnet.
Now lets take a look at how we use FPRNG in coinflip game program
Derivation of accounts
FPRNG addresses(It is the same address for devnet, testnet and mainnet-beta)
const rngProgram = new anchor.web3.PublicKey('FEED1qspts3SRuoEyG29NMNpsTKX8yG9NGMinNC4GeYB');
Creating Instruction
Player's decision(head or tails) is serialized to pass as instruction data.
const playersDecision = { decision: new anchor.BN(decision) };
We create our instruction, then build it and finally send. Anchor handles entropy_account, fee_account and rng_program_account for us, because we defined them as constant accounts
we get the mod 2 of the random number. It is either one or zero then we compare with the player's decision just log a message. you can put here your program logic
if random_number.random_number % 2 == players_decision.decision{
msg!("congragulations you win");
}else{
msg!("you lost");
}
Accounts' signer and writable properties are necessary when we call FPRNG credits_account is optional when you call FPRNG program. You don't need to pass into CPI. If you call FPRNG program with credits, the program will not charge per request and instead it decrease your credits. You can take a look at feedprotocol.xyz to get more info about credits