This document explains the simplified process for generating and utilizing random numbers in the Crash game. The system ensures fairness and consistency by coordinating interactions between the app, backend, and smart contract.
Player Makes a Bet: The app sends a request to the smart contract via the placeBet function to store the player's choices.
Backend Finalizes the Bet:
transaction to placeBet is mined, the app notifies the backend.
The backend generates a random number and calls the smart contract function _settleBet to resolve the bet.
Result Delivery: The backend returns the final result (win/loss and multiplier outcome) to the app for display to the user.
The random number used for the game outcome is generated by the backend. Here are the key steps:
Backend Random Logic: A pseudo-random number is generated using:
const randomNumber = Math.floor(Math.random() * 10000);
This ensures a number within the range [0, 9999]
Contract Settlement Call: The random number, along with the pendingId of the bet, is passed to the _settleBet function in the Crash smart contract:
_settleBet(pendingId, randomNumber);
The _settleBet function processes the random number to determine the outcome of a bet:
Random Seeding: A more secure random number is calculated within the contract using:
uint randomNumber = uint(keccak256(abi.encode(
seed,
block.timestamp,
block.prevrandao,
blockhash(bet.placeBlockNumber)
)));
Explosion Check: The random number modulo 100 determines whether the bet "explodes" (fails early):
uint exploted = randomNumber % 100;
Multiplier Outcome: The random number determines the multiplier outcome for the bet:
uint H = randomNumber % (maxMultiplier - minMultiplier + 1);
uint E = maxMultiplier / 100;
uint multiplierOutcome = (E * maxMultiplier - H) / (E * 100 - H);