Example - Roll a Dice
The RollDiceExample
contract is an example of how to use the GeneralRandcastConsumerBase
to request and consume randomness of type RandomWords
simulating dice rolls.
Here's a step-by-step explanation of the RollDiceExample
contract and RandcastSDK:
Import
GeneralRandcastConsumerBase
contract:
Inherit the
GeneralRandcastConsumerBase
contract:
Defines two mappings to store the random results for each requestId and the processed dice roll results:
Implement the constructor, which takes the
adapter
address as an argument and passes it to the base contract:
Provide the
rollDice()
function that takes abunch
argument, representing the number of random words (dice rolls) to request, and sends the randomness request using the _requestRandomness()
function:
Override the
_fulfillRandomWords()
function fromBasicRandcastConsumerBase
to handle the received random words and useRandcastSDK.roll
to process them into dice roll results:
Implement a
lengthOfDiceResults()
function to return the length of thediceResults
array:
The RollDiceExample
contract showcases how to request randomness and process the received random words to simulate dice rolls. The contract leverages the capabilities provided by the GeneralRandcastConsumerBase
contract to interact with the randomness Adapter
.
In the next example, we will try to shuffle a deck of cards using the entropy retrieved.
Last updated