Here's a step-by-step explanation of the GetRandomNumberExample contract:
Inherit the GeneralRandcastConsumerBase contract:
Define a mapping randomResults to store randomness associated with each request ID:
Define an array randomnessResults to store all received randomness values:
Implement the constructor to initialize the adapter address from the GeneralRandcastConsumerBase contract:
Provide the getRandomNumber() function to request randomness:
This function calls the requestRandomness() function from the GeneralRandcastConsumerBase contract, which sends the randomness request to the adapter.
Override the fulfillRandomness() callback function:
When the adapter fulfills the randomness request, this function is called with the requestId and the generated randomness. The randomness value is stored in both the randomResults mapping and the randomnessResults array.
Provides helper functions to retrieve the length of the randomnessResults array and the last randomness result:
These functions allow users to query the length of the randomnessResults array and retrieve the last received randomness value.
Next, we will look at another example that uses the received random number as an entropy to throw a dice.