|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.28; |
| 3 | + |
| 4 | +import {RandomnessReceiverBase} from "@randomness-solidity/src/RandomnessReceiverBase.sol"; |
| 5 | + |
| 6 | +/// @title Randomness Consumer contract |
| 7 | +/// @author Randamu developers |
| 8 | +/// @notice A mock RandomnessConsumer contract that makes use of the Randamu randomness-solidity |
| 9 | +/// library to request verifiable randomness |
| 10 | +/// The library is available at: https://github.com/randa-mu/randomness-solidity/tree/main |
| 11 | +/// The library also contains logic for array shuffling using Feistel shuffle |
| 12 | +/// and selecting n items randomly from an array |
| 13 | +/// Deployed at: 0x410354CCce2c9d99bfD1968F91d37767beE97315 |
| 14 | +contract RandomnessConsumer is RandomnessReceiverBase { |
| 15 | + address public constant RANDOMNESS_SENDER = 0x9c789bc7F2B5c6619Be1572A39F2C3d6f33001dC; |
| 16 | + |
| 17 | + bytes32 public randomness; |
| 18 | + uint256 public requestId; |
| 19 | + |
| 20 | + constructor() RandomnessReceiverBase(RANDOMNESS_SENDER) {} |
| 21 | + |
| 22 | + /** |
| 23 | + * @dev Requests randomness. |
| 24 | + * |
| 25 | + * This function calls the `requestRandomness` method to request a random value. |
| 26 | + * The `requestId` is updated with the ID returned from the randomness request. |
| 27 | + */ |
| 28 | + function rollDice() external { |
| 29 | + requestId = requestRandomness(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @dev Callback function that is called when randomness is received. |
| 34 | + * @param requestID The ID of the randomness request that was made. |
| 35 | + * @param _randomness The random value received. |
| 36 | + * |
| 37 | + * This function verifies that the received `requestID` matches the one that |
| 38 | + * was previously stored. If they match, it updates the `randomness` state variable |
| 39 | + * with the newly received random value. |
| 40 | + * |
| 41 | + * Reverts if the `requestID` does not match the stored `requestId`, ensuring that |
| 42 | + * the randomness is received in response to a valid request. |
| 43 | + */ |
| 44 | + function onRandomnessReceived(uint256 requestID, bytes32 _randomness) internal override { |
| 45 | + require(requestId == requestID, "Request ID mismatch"); |
| 46 | + randomness = _randomness; |
| 47 | + } |
| 48 | +} |
0 commit comments