"metadata": "{\"compiler\":{\"version\":\"0.8.24+commit.e11b9ed9\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"block\",\"type\":\"uint256\"}],\"name\":\"randomNumbers\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"number\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_block\",\"type\":\"uint256\"}],\"name\":\"receiveRandomness\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"randomNumber\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_block\",\"type\":\"uint256\"}],\"name\":\"requestRandomness\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}],\"devdoc\":{\"author\":\"Cl\\u00e9ment Lesaege - <
[email protected]>\",\"details\":\"Random Number Generator returning the blockhash with a fallback behaviour. In case no one called it within the 256 blocks, it returns the previous blockhash. This contract must be used when returning 0 is a worse failure mode than returning another blockhash. Allows saving the random number for use in the future. It allows the contract to still access the blockhash even after 256 blocks.\",\"kind\":\"dev\",\"methods\":{\"receiveRandomness(uint256)\":{\"details\":\"Return the random number. If it has not been saved and is still computable compute it.\",\"params\":{\"_block\":\"Block the random number is linked to.\"},\"returns\":{\"randomNumber\":\"The random number or 0 if it is not ready or has not been requested.\"}},\"requestRandomness(uint256)\":{\"details\":\"Request a random number.\",\"params\":{\"_block\":\"Block the random number is linked to.\"}}},\"title\":\"Random Number Generator using blockhash with fallback.\",\"version\":1},\"userdoc\":{\"kind\":\"user\",\"methods\":{},\"version\":1}},\"settings\":{\"compilationTarget\":{\"src/rng/BlockhashRNG.sol\":\"BlockHashRNG\"},\"evmVersion\":\"paris\",\"libraries\":{},\"metadata\":{\"bytecodeHash\":\"ipfs\",\"useLiteralContent\":true},\"optimizer\":{\"enabled\":true,\"runs\":100},\"remappings\":[]},\"sources\":{\"src/rng/BlockhashRNG.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n\\npragma solidity 0.8.24;\\n\\nimport \\\"./RNG.sol\\\";\\n\\n/// @title Random Number Generator using blockhash with fallback.\\n/// @author Cl\\u00e9ment Lesaege - <
[email protected]>\\n/// @dev\\n/// Random Number Generator returning the blockhash with a fallback behaviour.\\n/// In case no one called it within the 256 blocks, it returns the previous blockhash.\\n/// This contract must be used when returning 0 is a worse failure mode than returning another blockhash.\\n/// Allows saving the random number for use in the future. It allows the contract to still access the blockhash even after 256 blocks.\\ncontract BlockHashRNG is RNG {\\n mapping(uint256 block => uint256 number) public randomNumbers; // randomNumbers[block] is the random number for this block, 0 otherwise.\\n\\n /// @dev Request a random number.\\n /// @param _block Block the random number is linked to.\\n function requestRandomness(uint256 _block) external override {\\n // nop\\n }\\n\\n /// @dev Return the random number. If it has not been saved and is still computable compute it.\\n /// @param _block Block the random number is linked to.\\n /// @return randomNumber The random number or 0 if it is not ready or has not been requested.\\n function receiveRandomness(uint256 _block) external override returns (uint256 randomNumber) {\\n randomNumber = randomNumbers[_block];\\n if (randomNumber != 0) {\\n return randomNumber;\\n }\\n\\n if (_block < block.number) {\\n // The random number is not already set and can be.\\n if (blockhash(_block) != 0x0) {\\n // Normal case.\\n randomNumber = uint256(blockhash(_block));\\n } else {\\n // The contract was not called in time. Fallback to returning previous blockhash.\\n randomNumber = uint256(blockhash(block.number - 1));\\n }\\n }\\n randomNumbers[_block] = randomNumber;\\n }\\n}\\n\",\"keccak256\":\"0xb2ae9b40700c5915c1cbe57dde68486b669cfea1fdfec18d5b42ffbde44031de\",\"license\":\"MIT\"},\"src/rng/RNG.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n\\npragma solidity 0.8.24;\\n\\ninterface RNG {\\n /// @dev Request a random number.\\n /// @param _block Block linked to the request.\\n function requestRandomness(uint256 _block) external;\\n\\n /// @dev Receive the random number.\\n /// @param _block Block the random number is linked to.\\n /// @return randomNumber Random Number. If the number is not ready or has not been required 0 instead.\\n function receiveRandomness(uint256 _block) external returns (uint256 randomNumber);\\n}\\n\",\"keccak256\":\"0xf92e0cf768afefc5cc6ef786c263b67dd00c021aa5753213dbbc33014adb68c5\",\"license\":\"MIT\"}},\"version\":1}",
0 commit comments