Verified contractExactGPL-3.0
RNGServiceChainlinkV3
Address0xdc168456bca362d864aa2a32a65b90c8aab282cf
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.10
- Same code
Only this contract
Details
- Compiler
- solc 0.8.10+commit.fc410830
- Language
- solidity
- Optimization
- Yes, 2,000 runs
- EVM version
- london
- License
- GPL-3.0
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 21,321,752
- Creation transaction
- 0xb9a5dd6f4a…1d303a6b56
9 files
- contracts/rng-service/RNGServiceChainlinkV3.solmain8.79 KB
- @chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol4.83 KB
- @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol6.63 KB
- @openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol8.28 KB
- contracts/owner-manager/Manageable.sol3.38 KB
- contracts/owner-manager/Ownable.sol4.42 KB
- contracts/rng-service/chainlink/VRFConsumerBaseV2.sol1.83 KB
- contracts/rng-service/interfaces/IRNGService.sol3.65 KB
- contracts/rng-service/interfaces/IRNGServiceChainlinkV2.sol1.53 KB
contracts/rng-service/RNGServiceChainlinkV3.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "./interfaces/IRNGServiceChainlinkV2.sol";
import "./chainlink/VRFConsumerBaseV2.sol";
import "../owner-manager/Manageable.sol";
contract RNGServiceChainlinkV3 is IRNGServiceChainlinkV2, VRFConsumerBaseV2, Manageable {
/* ========== Global Variables ========== */
/// @dev Reference to the VRFCoordinatorV2 deployed contract
VRFCoordinatorV2Interface internal vrfCoordinator;
/// @dev A counter for the number of requests made used for request IDs
uint32 internal requestCounter;
/// @dev Chainlink VRF subscription ID
uint64 internal subscriptionId;
/// @dev Hash of the public key used to verify the VRF proof
bytes32 internal keyHash;
/// @dev A lists of random numbers from past requests mapped by internal
/// request ID
mapping(uint32 => uint256[]) internal randomNumbers;
/// @dev A list of flags that indicates if internal request ID is completed
/// or not
mapping(uint32 => bool) internal requestCompleted;
/// @dev A list of blocks to be locked at based on past requests mapped by
/// request ID
mapping(uint32 => uint32) internal requestLockBlock;
/// @dev A mapping from Chainlink request IDs to internal request IDs
mapping(uint256 => uint32) internal chainlinkRequestIds;
/* ============ Events ============ */
/**
* @notice Emitted when the Chainlink VRF key hash is set
* @param keyHash Chainlink VRF key hash
*/
event KeyHashSet(bytes32 keyHash);
/**
* @notice Emitted when the Chainlink VRF subscription ID is set
* @param subscriptionId Chainlink VRF subscription ID
*/
event SubscriptionIdSet(uint64 subscriptionId);
/**
* @notice Emitted when the Chainlink VRF Coordinator address is set
* @param vrfCoordinator Address of the VRF Coordinator
*/
event VrfCoordinatorSet(VRFCoordinatorV2Interface indexed vrfCoordinator);
/* ============ Constructor ============ */
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/* ============ Initialize ============ */
/**
* @notice Constructs RNGServiceChainlinkV2 with passed parameters.
* @param _owner Owner of the contract
* @param _vrfCoordinator Address of the VRF Coordinator
* @param _subscriptionId Chainlink VRF subscription ID
* @param _keyHash Hash of the public key used to verify the VRF proof
*/
function initialize(
address _owner,
VRFCoordinatorV2Interface _vrfCoordinator,
uint64 _subscriptionId,
bytes32 _keyHash
) external virtual initializer {
__RNGServiceChainlinkV2_init(_owner, _vrfCoordinator, _subscriptionId, _keyHash);
}
/* ============== External ============== */
/// @inheritdoc IRNGService
function requestRandomNumbers(
uint32 numWords
) external override onlyManager returns (uint32 requestId, uint32 lockBlock) {
(uint16 minRequestConfirmations, uint32 maxGasLimit, ) = vrfCoordinator.getRequestConfig();
uint256 _vrfRequestId = vrfCoordinator.requestRandomWords(
keyHash,
subscriptionId,
minRequestConfirmations,
maxGasLimit,
numWords
);
++requestCounter;
uint32 _requestCounter = requestCounter;
requestId = _requestCounter;
lockBlock = uint32(block.number);
chainlinkRequestIds[_vrfRequestId] = _requestCounter;
requestLockBlock[_requestCounter] = lockBlock;
emit RandomNumbersRequested(_requestCounter, _vrfRequestId, numWords, msg.sender);
}
/// @inheritdoc IRNGService
function isRequestCompleted(uint32 _internalRequestId) external view returns (bool isCompleted) {
return requestCompleted[_internalRequestId];
}
/// @inheritdoc IRNGService
function getRandomNumbers(uint32 _internalRequestId) external view returns (uint256[] memory numbers) {
return randomNumbers[_internalRequestId];
}
/// @inheritdoc IRNGService
function getLastRequestId() external view returns (uint32 requestId) {
return requestCounter;
}
/// @inheritdoc IRNGService
function getRequestFee() external pure returns (address feeToken, uint256 requestFee) {
return (address(0), 0);
}
/// @inheritdoc IRNGServiceChainlinkV2
function getKeyHash() external view returns (bytes32) {
return keyHash;
}
/// @inheritdoc IRNGServiceChainlinkV2
function getSubscriptionId() external view returns (uint64) {
return subscriptionId;
}
/// @inheritdoc IRNGServiceChainlinkV2
function getVrfCoordinator() external view returns (VRFCoordinatorV2Interface) {
return vrfCoordinator;
}
/// @inheritdoc IRNGServiceChainlinkV2
function setSubscriptionId(uint64 _subscriptionId) external onlyOwner {
_setSubscriptionId(_subscriptionId);
}
/// @inheritdoc IRNGServiceChainlinkV2
function setKeyHash(bytes32 _keyHash) external onlyOwner {
_setKeyHash(_keyHash);
}
function setVRFCoordinator(VRFCoordinatorV2Interface _vrfCoordinator) external onlyOwner {
_setVRFCoordinator(_vrfCoordinator);
}
/* ============== Internal ============== */
/**
* @notice Callback function called by VRF Coordinator
* @dev The VRF Coordinator will only call it once it has verified the proof
* associated with the randomness.
* @param _vrfRequestId Chainlink VRF request ID
* @param _randomWords Chainlink VRF array of random words (numbers)
*/
function fulfillRandomWords(uint256 _vrfRequestId, uint256[] memory _randomWords) internal override {
uint32 _internalRequestId = chainlinkRequestIds[_vrfRequestId];
require(_internalRequestId > 0, "RNGServiceChainlinkV2/requestId-incorrect");
randomNumbers[_internalRequestId] = _randomWords;
requestCompleted[_internalRequestId] = true;
emit RandomNumbersCompleted(_internalRequestId, _randomWords);
}
/**
* @notice Set Chainlink VRF Coordinator contract address.
* @param _vrfCoordinator Chainlink VRF Coordinator contract address
*/
function _setVRFCoordinator(VRFCoordinatorV2Interface _vrfCoordinator) internal {
require(address(_vrfCoordinator) != address(0), "RNGServiceChainlinkV2/vrf-coordinator-not-zero-addr");
vrfCoordinator = _vrfCoordinator;
emit VrfCoordinatorSet(_vrfCoordinator);
}
/**
* @notice Set Chainlink VRF subscription ID associated with this contract.
* @param _subscriptionId Chainlink VRF subscription ID
*/
function _setSubscriptionId(uint64 _subscriptionId) internal {
require(_subscriptionId > 0, "RNGServiceChainlinkV2/subscriptionId-gt-zero");
subscriptionId = _subscriptionId;
emit SubscriptionIdSet(_subscriptionId);
}
/**
* @notice Set Chainlink VRF key hash.
* @param _keyHash Chainlink VRF key hash
*/
function _setKeyHash(bytes32 _keyHash) internal {
require(_keyHash != bytes32(0), "RNGServiceChainlinkV2/keyHash-not-empty");
keyHash = _keyHash;
emit KeyHashSet(_keyHash);
}
/**
* @notice Constructs RNGServiceChainlinkV2 with passed parameters.
* @param _owner Owner of the contract
* @param _vrfCoordinator Address of the VRF Coordinator
* @param _subscriptionId Chainlink VRF subscription ID
* @param _keyHash Hash of the public key used to verify the VRF proof
*/
function __RNGServiceChainlinkV2_init(
address _owner,
VRFCoordinatorV2Interface _vrfCoordinator,
uint64 _subscriptionId,
bytes32 _keyHash
) internal onlyInitializing {
__Manageable_init_unchained(_owner);
__VRFConsumerBaseV2_init_unchained(address(_vrfCoordinator));
__RNGServiceChainlinkV2_init_unchained(_vrfCoordinator, _subscriptionId, _keyHash);
}
/**
* @notice Unchained initialization of RNGServiceChainlinkV2 contract.
* @param _vrfCoordinator Address of the VRF Coordinator
* @param _subscriptionId Chainlink VRF subscription ID
* @param _keyHash Hash of the public key used to verify the VRF proof
*/
function __RNGServiceChainlinkV2_init_unchained(
VRFCoordinatorV2Interface _vrfCoordinator,
uint64 _subscriptionId,
bytes32 _keyHash
) internal onlyInitializing {
_setVRFCoordinator(_vrfCoordinator);
_setSubscriptionId(_subscriptionId);
_setKeyHash(_keyHash);
}
}
@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface VRFCoordinatorV2Interface {
/**
* @notice Get configuration relevant for making requests
* @return minimumRequestConfirmations global min for request confirmations
* @return maxGasLimit global max for request gas limit
* @return s_provingKeyHashes list of registered key hashes
*/
function getRequestConfig()
external
view
returns (
uint16,
uint32,
bytes32[] memory
);
/**
* @notice Request a set of random words.
* @param keyHash - Corresponds to a particular oracle job which uses
* that key for generating the VRF proof. Different keyHash's have different gas price
* ceilings, so you can select a specific one to bound your maximum per request cost.
* @param subId - The ID of the VRF subscription. Must be funded
* with the minimum subscription balance required for the selected keyHash.
* @param minimumRequestConfirmations - How many blocks you'd like the
* oracle to wait before responding to the request. See SECURITY CONSIDERATIONS
* for why you may want to request more. The acceptable range is
* [minimumRequestBlockConfirmations, 200].
* @param callbackGasLimit - How much gas you'd like to receive in your
* fulfillRandomWords callback. Note that gasleft() inside fulfillRandomWords
* may be slightly less than this amount because of gas used calling the function
* (argument decoding etc.), so you may need to request slightly more than you expect
* to have inside fulfillRandomWords. The acceptable range is
* [0, maxGasLimit]
* @param numWords - The number of uint256 random values you'd like to receive
* in your fulfillRandomWords callback. Note these numbers are expanded in a
* secure way by the VRFCoordinator from a single random value supplied by the oracle.
* @return requestId - A unique identifier of the request. Can be used to match
* a request to a response in fulfillRandomWords.
*/
function requestRandomWords(
bytes32 keyHash,
uint64 subId,
uint16 minimumRequestConfirmations,
uint32 callbackGasLimit,
uint32 numWords
) external returns (uint256 requestId);
/**
* @notice Create a VRF subscription.
* @return subId - A unique subscription id.
* @dev You can manage the consumer set dynamically with addConsumer/removeConsumer.
* @dev Note to fund the subscription, use transferAndCall. For example
* @dev LINKTOKEN.transferAndCall(
* @dev address(COORDINATOR),
* @dev amount,
* @dev abi.encode(subId));
*/
function createSubscription() external returns (uint64 subId);
/**
* @notice Get a VRF subscription.
* @param subId - ID of the subscription
* @return balance - LINK balance of the subscription in juels.
* @return reqCount - number of requests for this subscription, determines fee tier.
* @return owner - owner of the subscription.
* @return consumers - list of consumer address which are able to use this subscription.
*/
function getSubscription(uint64 subId)
external
view
returns (
uint96 balance,
uint64 reqCount,
address owner,
address[] memory consumers
);
/**
* @notice Request subscription owner transfer.
* @param subId - ID of the subscription
* @param newOwner - proposed new owner of the subscription
*/
function requestSubscriptionOwnerTransfer(uint64 subId, address newOwner) external;
/**
* @notice Request subscription owner transfer.
* @param subId - ID of the subscription
* @dev will revert if original owner of subId has
* not requested that msg.sender become the new owner.
*/
function acceptSubscriptionOwnerTransfer(uint64 subId) external;
/**
* @notice Add a consumer to a VRF subscription.
* @param subId - ID of the subscription
* @param consumer - New consumer which can use the subscription
*/
function addConsumer(uint64 subId, address consumer) external;
/**
* @notice Remove a consumer from a VRF subscription.
* @param subId - ID of the subscription
* @param consumer - Consumer to remove from the subscription
*/
function removeConsumer(uint64 subId, address consumer) external;
/**
* @notice Cancel a subscription
* @param subId - ID of the subscription
* @param to - Where to send the remaining LINK to
*/
function cancelSubscription(uint64 subId, address to) external;
/*
* @notice Check to see if there exists a request commitment consumers
* for all consumers and keyhashes for a given sub.
* @param subId - ID of the subscription
* @return true if there exists at least one unfulfilled request for the subscription, false
* otherwise.
*/
function pendingRequestExists(uint64 subId) external view returns (bool);
}
@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.1) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.2;
import "../../utils/AddressUpgradeable.sol";
/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
* reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
* case an upgrade adds a module that needs to be initialized.
*
* For example:
*
* [.hljs-theme-light.nopadding]
* ```
* contract MyToken is ERC20Upgradeable {
* function initialize() initializer public {
* __ERC20_init("MyToken", "MTK");
* }
* }
* contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
* function initializeV2() reinitializer(2) public {
* __ERC20Permit_init("MyToken");
* }
* }
* ```
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*
* [CAUTION]
* ====
* Avoid leaving a contract uninitialized.
*
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
* contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
* the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
*
* [.hljs-theme-light.nopadding]
* ```
* /// @custom:oz-upgrades-unsafe-allow constructor
* constructor() {
* _disableInitializers();
* }
* ```
* ====
*/
abstract contract Initializable {
/**
* @dev Indicates that the contract has been initialized.
* @custom:oz-retyped-from bool
*/
uint8 private _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool private _initializing;
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint8 version);
/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
*
* Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
* constructor.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
bool isTopLevelCall = !_initializing;
require(
(isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
_initialized = 1;
if (isTopLevelCall) {
_initializing = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
emit Initialized(1);
}
}
/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* A reinitializer may be used after the original initialization step. This is essential to configure modules that
* are added through upgrades and that require initialization.
*
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
* cannot be nested. If one is invoked in the context of another, execution will revert.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*
* WARNING: setting the version to 255 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint8 version) {
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
_initialized = version;
_initializing = true;
_;
_initializing = false;
emit Initialized(version);
}
/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
require(_initializing, "Initializable: contract is not initializing");
_;
}
/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*
* Emits an {Initialized} event the first time it is successfully executed.
*/
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized < type(uint8).max) {
_initialized = type(uint8).max;
emit Initialized(type(uint8).max);
}
}
/**
* @dev Returns the highest version that has been initialized. See {reinitializer}.
*/
function _getInitializedVersion() internal view returns (uint8) {
return _initialized;
}
/**
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
*/
function _isInitializing() internal view returns (bool) {
return _initializing;
}
}
@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library AddressUpgradeable {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
contracts/owner-manager/Manageable.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "./Ownable.sol";
/**
* @title Abstract manageable contract that can be inherited by other contracts.
* @notice Contract module based on Ownable which provides a basic access
* control mechanism, where there is an owner and a manager that can be
* granted exclusive access to specific functions.
*
* The `owner` is first set by passing the address of the `initialOwner`
* to the Ownable constructor.
*
* The owner account can be transferred through a two steps process:
* 1. The current `owner` calls {transferOwnership} to set a
* `pendingOwner`.
* 2. The `pendingOwner` calls {acceptOwnership} to accept the ownership
* transfer.
*
* The manager account needs to be set using {setManager}.
*
* This module is used through inheritance. It will make available the
* modifiers `onlyManager`, `onlyOwner` and `onlyManagerOrOwner`, which
* can be applied to your functions to restrict their use to the manager
* and/or the owner.
*/
abstract contract Manageable is Ownable {
address private _manager;
/**
* @dev Emitted when `_manager` has been changed.
* @param previousManager previous `_manager` address.
* @param newManager new `_manager` address.
*/
event ManagerTransferred(address indexed previousManager, address indexed newManager);
function __Manageable_init_unchained(address _initialOwner) internal onlyInitializing {
__Ownable_init_unchained(_initialOwner);
}
/* ============ External Functions ============ */
/**
* @notice Gets current `_manager`.
* @return Current `_manager` address.
*/
function manager() public view virtual returns (address) {
return _manager;
}
/**
* @notice Set or change of manager.
* @dev Throws if called by any account other than the owner.
* @param _newManager New _manager address.
* @return Boolean to indicate if the operation was successful or not.
*/
function setManager(address _newManager) external onlyOwner returns (bool) {
return _setManager(_newManager);
}
/* ============ Internal Functions ============ */
/**
* @notice Set or change of manager.
* @param _newManager New _manager address.
* @return Boolean to indicate if the operation was successful or not.
*/
function _setManager(address _newManager) private returns (bool) {
address _previousManager = _manager;
require(_newManager != _previousManager, "Manageable/existing-manager-address");
_manager = _newManager;
emit ManagerTransferred(_previousManager, _newManager);
return true;
}
/* ============ Modifier Functions ============ */
/**
* @dev Throws if called by any account other than the manager.
*/
modifier onlyManager() {
require(manager() == msg.sender, "Manageable/caller-not-manager");
_;
}
/**
* @dev Throws if called by any account other than the manager or the owner.
*/
modifier onlyManagerOrOwner() {
require(manager() == msg.sender || owner() == msg.sender, "Manageable/caller-not-manager-or-owner");
_;
}
uint256[45] private __gap;
}
contracts/owner-manager/Ownable.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
/**
* @title Abstract ownable contract that can be inherited by other contracts.
* @notice Contract module which provides a basic access control mechanism,
* where there is an account (an owner) that can be granted exclusive
* access to specific functions.
*
* The `owner` is first set by passing the address of the `initialOwner`
* to the Ownable Initialize.
*
* The owner account can be transferred through a two steps process:
* 1. The current `owner` calls {transferOwnership} to set a
* `pendingOwner`.
* 2. The `pendingOwner` calls {claimOwnership} to accept the ownership
* transfer.
*
* This module is used through inheritance. It will make available the
* modifier `onlyOwner`, which can be applied to your functions to
* restrict their use to the owner.
*/
abstract contract Ownable is Initializable {
address private _owner;
address private _pendingOwner;
/**
* @dev Emitted when `_pendingOwner` has been changed.
* @param pendingOwner new `_pendingOwner` address.
*/
event OwnershipOffered(address indexed pendingOwner);
/**
* @dev Emitted when `_owner` has been changed.
* @param previousOwner previous `_owner` address.
* @param newOwner new `_owner` address.
*/
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/* ============ Deploy ============ */
/**
* @notice Initializes the contract setting `_initialOwner` as the initial
* owner.
* @param _initialOwner Initial owner of the contract.
*/
function __Ownable_init_unchained(address _initialOwner) internal onlyInitializing {
_setOwner(_initialOwner);
}
/* ============ External Functions ============ */
/**
* @notice Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @notice Gets current `_pendingOwner`.
* @return Current `_pendingOwner` address.
*/
function pendingOwner() external view virtual returns (address) {
return _pendingOwner;
}
/**
* @notice Renounce ownership of the contract.
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() external virtual onlyOwner {
_setOwner(address(0));
}
/**
* @notice Allows current owner to set the `_pendingOwner` address.
* @param _newOwner Address to transfer ownership to.
*/
function transferOwnership(address _newOwner) external onlyOwner {
require(_newOwner != address(0), "Ownable/pendingOwner-not-zero-address");
_pendingOwner = _newOwner;
emit OwnershipOffered(_newOwner);
}
/**
* @notice Allows the `_pendingOwner` address to finalize the transfer.
* @dev This function is only callable by the `_pendingOwner`.
*/
function claimOwnership() external onlyPendingOwner {
_setOwner(_pendingOwner);
_pendingOwner = address(0);
}
/* ============ Internal Functions ============ */
/**
* @notice Internal function to set the `_owner` of the contract.
* @param _newOwner New `_owner` address.
*/
function _setOwner(address _newOwner) private {
address _oldOwner = _owner;
_owner = _newOwner;
emit OwnershipTransferred(_oldOwner, _newOwner);
}
/* ============ Modifier Functions ============ */
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == msg.sender, "Ownable/caller-not-owner");
_;
}
/**
* @dev Throws if called by any account other than the `pendingOwner`.
*/
modifier onlyPendingOwner() {
require(msg.sender == _pendingOwner, "Ownable/caller-not-pendingOwner");
_;
}
uint256[45] private __gap;
}
contracts/rng-service/chainlink/VRFConsumerBaseV2.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
/**
* @notice Interface for contracts using VRF randomness.
*/
abstract contract VRFConsumerBaseV2 is Initializable {
/* =============== Errors =============== */
error OnlyCoordinatorCanFulfill(address have, address want);
/* ========== Global Variables ========== */
address private vrfCoordinator;
/* ============ Constructor ============= */
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {}
/* ============== External ============== */
/**
* @notice Is called by VRFCoordinator when it receives a valid VRF proof.
* Then calls `fulfillRandomness`, after validating the origin of the call.
*/
function rawFulfillRandomWords(uint256 requestId, uint256[] memory randomWords) external {
if (msg.sender != vrfCoordinator) {
revert OnlyCoordinatorCanFulfill(msg.sender, vrfCoordinator);
}
fulfillRandomWords(requestId, randomWords);
}
/* ============== Internal ============== */
/**
* @notice Unchained initialization of VRFCinsumerBaseV2 contract.
* @param _vrfCoordinator An initial VRF Coordinator contract address
*/
function __VRFConsumerBaseV2_init_unchained(address _vrfCoordinator) internal onlyInitializing {
vrfCoordinator = _vrfCoordinator;
}
/**
* @notice Handles the VRF response.
* @param requestId The ID initially returned by `requestRandomWords`
* @param randomWords The VRF output expanded to the requested number of
* words
*/
function fulfillRandomWords(uint256 requestId, uint256[] memory randomWords) internal virtual;
uint256[50] private __gap;
}
contracts/rng-service/interfaces/IRNGService.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
/**
* @title Random Numbers Generator Interface
* @notice Provides an interface for requesting random numbers from 3rd-party
* RNG services (Chainlink VRF, Starkware VDF, etc.).
*/
interface IRNGService {
/**
* @notice Emitted when a new request for a random numbers has been
* submitted.
* @param requestId The indexed ID of the request used to get the results of
* the RNG service
* @param vrfRequestId A request ID from 3rd party random numbers provider
* @param numWords An amount of random numbers to request
* @param sender The indexed address of the sender of the request
*/
event RandomNumbersRequested(
uint32 indexed requestId,
uint256 indexed vrfRequestId,
uint32 numWords,
address indexed sender
);
/**
* @notice Emitted when an existing request for a random numbers has been
* completed.
* @param requestId The indexed ID of the request used to get the results of
* the RNG service
* @param randomNumbers The random numbers produced by the 3rd-party service
*/
event RandomNumbersCompleted(uint32 indexed requestId, uint256[] randomNumbers);
/**
* @notice Gets the last request ID used by the RNG service.
* @return requestId The last request ID used in the last request
*/
function getLastRequestId() external view returns (uint32 requestId);
/**
* @notice Gets the fee for making a request against an RNG service.
* @return feeToken The address of the token that is used to pay fees
* @return requestFee The fee required to be paid to make a request
*/
function getRequestFee() external view returns (address feeToken, uint256 requestFee);
/**
* @notice Sends a request for a random numbers to the 3rd-party service.
* @dev Some services will complete the request immediately, others may have
* a time-delay.
* @dev Some services require payment in the form of a token, such as $LINK
* for Chainlink VRF.
* @dev The calling contract should "lock" all activity until the result is
* available via the `requestId`.
* @param numWords An amount of random numbers to request
* @return requestId The ID of the request used to get the results of the
* RNG service
* @return lockBlock The block number at which the RNG service will start
* generating time-delayed randomness
*/
function requestRandomNumbers(uint32 numWords) external returns (uint32 requestId, uint32 lockBlock);
/**
* @notice Checks if the request for randomness from the 3rd-party service
* has completed.
* @dev For time-delayed requests, this function is used to check/confirm
* completion.
* @param requestId The ID of the request used to get the results of the RNG
* service
* @return isCompleted `true` if the request has completed and a random
* number is available, `false` otherwise
*/
function isRequestCompleted(uint32 requestId) external view returns (bool isCompleted);
/**
* @notice Gets an array of random numbers produced by the 3rd-party service.
* @param requestId The ID of the request used to get the results of the RNG
* service
* @return numbers An array of random numbers
*/
function getRandomNumbers(uint32 requestId) external view returns (uint256[] memory numbers);
}
contracts/rng-service/interfaces/IRNGServiceChainlinkV2.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "./IRNGService.sol";
/**
* @title RNG Chainlink V2 Interface
* @notice Provides an interface for requesting random numbers from Chainlink
* VRF V2.
*/
interface IRNGServiceChainlinkV2 is IRNGService {
/**
* @notice Get Chainlink VRF keyHash associated with this contract.
* @return bytes32 Chainlink VRF keyHash
*/
function getKeyHash() external view returns (bytes32);
/**
* @notice Get Chainlink VRF subscription ID associated with this contract.
* @return uint64 Chainlink VRF subscription ID
*/
function getSubscriptionId() external view returns (uint64);
/**
* @notice Get Chainlink VRF coordinator contract address associated with
* this contract.
* @return address Chainlink VRF coordinator address
*/
function getVrfCoordinator() external view returns (VRFCoordinatorV2Interface);
/**
* @notice Set Chainlink VRF keyHash.
* @dev This function is only callable by the owner.
* @param keyHash Chainlink VRF keyHash
*/
function setKeyHash(bytes32 keyHash) external;
/**
* @notice Set Chainlink VRF subscription ID associated with this contract.
* @dev This function is only callable by the owner.
* @param subscriptionId Chainlink VRF subscription ID
*/
function setSubscriptionId(uint64 subscriptionId) external;
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"libraries": {},
"optimizer": {
"runs": 2000,
"enabled": true
},
"evmVersion": "london",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.