// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface AggregatorV3Interface {
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
function getRoundData(
uint80 _roundId
) external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.4.21 <0.9.0;
interface ArbGasInfo {
// return gas prices in wei, assuming the specified aggregator is used
// (
// per L2 tx,
// per L1 calldata unit, (zero byte = 4 units, nonzero byte = 16 units)
// per storage allocation,
// per ArbGas base,
// per ArbGas congestion,
// per ArbGas total
// )
function getPricesInWeiWithAggregator(address aggregator) external view returns (uint, uint, uint, uint, uint, uint);
// return gas prices in wei, as described above, assuming the caller's preferred aggregator is used
// if the caller hasn't specified a preferred aggregator, the default aggregator is assumed
function getPricesInWei() external view returns (uint, uint, uint, uint, uint, uint);
// return prices in ArbGas (per L2 tx, per L1 calldata unit, per storage allocation),
// assuming the specified aggregator is used
function getPricesInArbGasWithAggregator(address aggregator) external view returns (uint, uint, uint);
// return gas prices in ArbGas, as described above, assuming the caller's preferred aggregator is used
// if the caller hasn't specified a preferred aggregator, the default aggregator is assumed
function getPricesInArbGas() external view returns (uint, uint, uint);
// return gas accounting parameters (speedLimitPerSecond, gasPoolMax, maxTxGasLimit)
function getGasAccountingParams() external view returns (uint, uint, uint);
// get ArbOS's estimate of the L1 gas price in wei
function getL1GasPriceEstimate() external view returns(uint);
// set ArbOS's estimate of the L1 gas price in wei
// reverts unless called by chain owner or designated gas oracle (if any)
function setL1GasPriceEstimate(uint priceInWei) external;
// get L1 gas fees paid by the current transaction (txBaseFeeWei, calldataFeeWei)
function getCurrentTxL1GasFees() external view returns(uint);
}
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.4.21 <0.9.0;
/**
* @title System level functionality
* @notice For use by contracts to interact with core L2-specific functionality.
* Precompiled contract that exists in every Arbitrum chain at address(100), 0x0000000000000000000000000000000000000064.
*/
interface ArbSys {
/**
* @notice Get Arbitrum block number (distinct from L1 block number; Arbitrum genesis block has block number 0)
* @return block number as int
*/
function arbBlockNumber() external view returns (uint256);
/**
* @notice Get Arbitrum block hash (reverts unless currentBlockNum-256 <= arbBlockNum < currentBlockNum)
* @return block hash
*/
function arbBlockHash(uint256 arbBlockNum) external view returns (bytes32);
/**
* @notice Gets the rollup's unique chain identifier
* @return Chain identifier as int
*/
function arbChainID() external view returns (uint256);
/**
* @notice Get internal version number identifying an ArbOS build
* @return version number as int
*/
function arbOSVersion() external view returns (uint256);
/**
* @notice Returns 0 since Nitro has no concept of storage gas
* @return uint 0
*/
function getStorageGasAvailable() external view returns (uint256);
/**
* @notice (deprecated) check if current call is top level (meaning it was triggered by an EoA or a L1 contract)
* @dev this call has been deprecated and may be removed in a future release
* @return true if current execution frame is not a call by another L2 contract
*/
function isTopLevelCall() external view returns (bool);
/**
* @notice map L1 sender contract address to its L2 alias
* @param sender sender address
* @param unused argument no longer used
* @return aliased sender address
*/
function mapL1SenderContractAddressToL2Alias(address sender, address unused)
external
pure
returns (address);
/**
* @notice check if the caller (of this caller of this) is an aliased L1 contract address
* @return true iff the caller's address is an alias for an L1 contract address
*/
function wasMyCallersAddressAliased() external view returns (bool);
/**
* @notice return the address of the caller (of this caller of this), without applying L1 contract address aliasing
* @return address of the caller's caller, without applying L1 contract address aliasing
*/
function myCallersAddressWithoutAliasing() external view returns (address);
/**
* @notice Send given amount of Eth to dest from sender.
* This is a convenience function, which is equivalent to calling sendTxToL1 with empty data.
* @param destination recipient address on L1
* @return unique identifier for this L2-to-L1 transaction.
*/
function withdrawEth(address destination)
external
payable
returns (uint256);
/**
* @notice Send a transaction to L1
* @dev it is not possible to execute on the L1 any L2-to-L1 transaction which contains data
* to a contract address without any code (as enforced by the Bridge contract).
* @param destination recipient address on L1
* @param data (optional) calldata for L1 contract call
* @return a unique identifier for this L2-to-L1 transaction.
*/
function sendTxToL1(address destination, bytes calldata data)
external
payable
returns (uint256);
/**
* @notice Get send Merkle tree state
* @return size number of sends in the history
* @return root root hash of the send history
* @return partials hashes of partial subtrees in the send history tree
*/
function sendMerkleTreeState()
external
view
returns (
uint256 size,
bytes32 root,
bytes32[] memory partials
);
/**
* @notice creates a send txn from L2 to L1
* @param position = (level << 192) + leaf = (0 << 192) + leaf = leaf
*/
event L2ToL1Tx(
address caller,
address indexed destination,
uint256 indexed hash,
uint256 indexed position,
uint256 arbBlockNum,
uint256 ethBlockNum,
uint256 timestamp,
uint256 callvalue,
bytes data
);
/// @dev DEPRECATED in favour of the new L2ToL1Tx event above after the nitro upgrade
event L2ToL1Transaction(
address caller,
address indexed destination,
uint256 indexed uniqueId,
uint256 indexed batchNumber,
uint256 indexInBatch,
uint256 arbBlockNum,
uint256 ethBlockNum,
uint256 timestamp,
uint256 callvalue,
bytes data
);
/**
* @notice logs a merkle branch for proof synthesis
* @param reserved an index meant only to align the 4th index with L2ToL1Transaction's 4th event
* @param hash the merkle hash
* @param position = (level << 192) + leaf
*/
event SendMerkleUpdate(
uint256 indexed reserved,
bytes32 indexed hash,
uint256 indexed position
);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import {ArbSys} from "./vendor/@arbitrum/nitro-contracts/src/precompiles/ArbSys.sol";
import {ArbGasInfo} from "./vendor/@arbitrum/nitro-contracts/src/precompiles/ArbGasInfo.sol";
//@dev A library that abstracts out opcodes that behave differently across chains.
//@dev The methods below return values that are pertinent to the given chain.
//@dev For instance, ChainSpecificUtil.getBlockNumber() returns L2 block number in L2 chains
library ChainSpecificUtil {
address private constant ARBSYS_ADDR = address(0x0000000000000000000000000000000000000064);
ArbSys private constant ARBSYS = ArbSys(ARBSYS_ADDR);
address private constant ARBGAS_ADDR = address(0x000000000000000000000000000000000000006C);
ArbGasInfo private constant ARBGAS = ArbGasInfo(ARBGAS_ADDR);
uint256 private constant ARB_MAINNET_CHAIN_ID = 42161;
uint256 private constant ARB_GOERLI_TESTNET_CHAIN_ID = 421613;
function getBlockhash(uint64 blockNumber) internal view returns (bytes32) {
uint256 chainid = block.chainid;
if (chainid == ARB_MAINNET_CHAIN_ID || chainid == ARB_GOERLI_TESTNET_CHAIN_ID) {
if ((getBlockNumber() - blockNumber) > 256 || blockNumber >= getBlockNumber()) {
return "";
}
return ARBSYS.arbBlockHash(blockNumber);
}
return blockhash(blockNumber);
}
function getBlockNumber() internal view returns (uint256) {
uint256 chainid = block.chainid;
if (chainid == ARB_MAINNET_CHAIN_ID || chainid == ARB_GOERLI_TESTNET_CHAIN_ID) {
return ARBSYS.arbBlockNumber();
}
return block.number;
}
function getCurrentTxL1GasFees() internal view returns (uint256) {
uint256 chainid = block.chainid;
if (chainid == ARB_MAINNET_CHAIN_ID || chainid == ARB_GOERLI_TESTNET_CHAIN_ID) {
return ARBGAS.getCurrentTxL1GasFees();
}
return 0;
}
/**
* @notice Returns the gas cost in wei of calldataSizeBytes of calldata being posted
* @notice to L1.
*/
function getL1CalldataGasCost(uint256 calldataSizeBytes) internal view returns (uint256) {
uint256 chainid = block.chainid;
if (chainid == ARB_MAINNET_CHAIN_ID || chainid == ARB_GOERLI_TESTNET_CHAIN_ID) {
(, uint256 l1PricePerByte, , , , ) = ARBGAS.getPricesInWei();
// see https://developer.arbitrum.io/devs-how-tos/how-to-estimate-gas#where-do-we-get-all-this-information-from
// for the justification behind the 140 number.
return l1PricePerByte * (calldataSizeBytes + 140);
}
return 0;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import { VRFV2WrapperConsumerBase } from "chainlink/vrf/VRFV2WrapperConsumerBase.sol";
import { Ownable } from "owner-manager/Ownable.sol";
import { VRFV2Wrapper } from "chainlink/vrf/VRFV2Wrapper.sol";
import { LinkTokenInterface } from "chainlink/interfaces/LinkTokenInterface.sol";
import { RNGInterface } from "rng-contracts/RNGInterface.sol";
/**
* @title PoolTogether V5 ChainlinkVRFV2Direct
* @author G9 Software Inc.
* @notice This is an RNG service contract that interfaces with the Chainlink VRF V2
* service to trigger direct-funded RNG requests.
*/
contract ChainlinkVRFV2Direct is VRFV2WrapperConsumerBase, Ownable, RNGInterface {
/* ============ Global Variables ============ */
/// @notice A counter for the number of requests made used for request ids
uint32 internal _requestCounter;
/// @notice The callback gas limit
uint32 internal _callbackGasLimit;
/// @notice The number of confirmations to wait before fulfilling the request
uint16 internal _requestConfirmations;
/// @notice A list of random numbers from past requests mapped by request id
mapping(uint32 => uint256) internal _randomNumbers;
/// @notice A list of random number completion timestamps mapped by request id
mapping(uint32 => uint64) internal _requestCompletedAt;
/// @notice A mapping from Chainlink request ids to internal request ids
mapping(uint256 => uint32) internal _chainlinkRequestIds;
/* ============ Custom Errors ============ */
/// @notice Thrown when the LINK token contract address is set to the zero address.
error LinkTokenZeroAddress();
/// @notice Thrown when the VRFV2Wrapper address is set to the zero address.
error VRFV2WrapperZeroAddress();
/// @notice Thrown when the callback gas limit is set to zero.
error CallbackGasLimitZero();
/// @notice Thrown when the number of request confirmations is set to zero.
error RequestConfirmationsZero();
/// @notice Thrown when the chainlink VRF request ID does not match any stored request IDs.
/// @param vrfRequestId The chainlink ID for the VRF Request
error InvalidVrfRequestId(uint256 vrfRequestId);
/* ============ Custom Events ============ */
/// @notice Emitted when the callback gas limit is set
/// @param callbackGasLimit The new callback gas limit
event SetCallbackGasLimit(uint32 callbackGasLimit);
/// @notice Emitted when the number of request confirmations is set.
/// @param requestConfirmations The new request confirmations
event SetRequestConfirmations(uint16 requestConfirmations);
/* ============ Constructor ============ */
/**
* @notice Constructor of the contract
* @param _owner Address of the contract owner
* @param _vrfV2Wrapper Address of the VRF V2 Wrapper
* @param callbackGasLimit_ Gas limit for the fulfillRandomWords callback
* @param requestConfirmations_ The number of confirmations to wait before fulfilling the request
*/
constructor(
address _owner,
VRFV2Wrapper _vrfV2Wrapper,
uint32 callbackGasLimit_,
uint16 requestConfirmations_
) VRFV2WrapperConsumerBase(address(_vrfV2Wrapper.LINK()), address(_vrfV2Wrapper)) Ownable(_owner) {
if (address(_vrfV2Wrapper) == address(0)) revert VRFV2WrapperZeroAddress();
_setCallbackGasLimit(callbackGasLimit_);
_setRequestConfirmations(requestConfirmations_);
}
/* ============ External Functions ============ */
/// @inheritdoc RNGInterface
function requestRandomNumber()
external
returns (uint32 requestId, uint32 lockBlock)
{
uint256 _vrfRequestId = requestRandomness(
_callbackGasLimit,
_requestConfirmations,
1 // num words
);
requestId = _requestCounter + 1;
_requestCounter = requestId;
_chainlinkRequestIds[_vrfRequestId] = requestId;
lockBlock = uint32(block.number);
emit RandomNumberRequested(requestId, msg.sender);
}
/// @inheritdoc RNGInterface
function isRequestComplete(uint32 _internalRequestId)
external
view
override
returns (bool isCompleted)
{
return _randomNumbers[_internalRequestId] != 0;
}
/// @inheritdoc RNGInterface
function randomNumber(uint32 _internalRequestId)
external
view
override
returns (uint256 randomNum)
{
return _randomNumbers[_internalRequestId];
}
/**
* @notice Returns the timestamp at which the passed `requestId` was completed.
* @dev Returns zero if not completed or if the request doesn't exist
* @param requestId The ID of the request
*/
function completedAt(uint32 requestId) external view returns (uint64 completedAtTimestamp) {
return _requestCompletedAt[requestId];
}
/// @inheritdoc RNGInterface
function getLastRequestId() external view override returns (uint32 requestId) {
return _requestCounter;
}
/// @inheritdoc RNGInterface
function getRequestFee() external view override returns (address feeToken, uint256 requestFee) {
return (address(LINK), VRF_V2_WRAPPER.calculateRequestPrice(_callbackGasLimit));
}
/// @notice Returns the current callback gas limit.
/// @return The current callback gas limit
function getCallbackGasLimit() external view returns (uint32) {
return _callbackGasLimit;
}
/// @notice Returns the current request confirmation count.
/// @return The current request confirmation count
function getRequestConfirmations() external view returns (uint16) {
return _requestConfirmations;
}
/// @notice Returns the VRF V2 Wrapper contract that this contract uses.
/// @return The VRFV2Wrapper contract
function vrfV2Wrapper() external view returns (VRFV2Wrapper) {
return VRFV2Wrapper(address(VRF_V2_WRAPPER));
}
/* ============ External Setters ============ */
/// @notice Sets a new callback gat limit.
/// @param callbackGasLimit_ The new callback gat limit
function setCallbackGasLimit(uint32 callbackGasLimit_) external onlyOwner {
_setCallbackGasLimit(callbackGasLimit_);
}
/// @notice Sets a new request confirmation count.
/// @param requestConfirmations_ The new request confirmation count
function setRequestConfirmations(uint16 requestConfirmations_) external onlyOwner {
_setRequestConfirmations(requestConfirmations_);
}
/* ============ Internal Functions ============ */
/**
* @notice Callback function called by VRF Wrapper
* @dev The VRF Wrapper 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
*/
function fulfillRandomWords(uint256 _vrfRequestId, uint256[] memory _randomWords)
internal
override
{
uint32 _internalRequestId = _chainlinkRequestIds[_vrfRequestId];
if (_internalRequestId == 0) revert InvalidVrfRequestId(_vrfRequestId);
uint256 _randomNumber = _randomWords[0];
_randomNumbers[_internalRequestId] = _randomNumber;
_requestCompletedAt[_internalRequestId] = uint64(block.timestamp);
emit RandomNumberCompleted(_internalRequestId, _randomNumber);
}
/* ============ Internal Setters ============ */
/// @notice Sets a new callback gat limit.
/// @param callbackGasLimit_ The new callback gat limit
function _setCallbackGasLimit(uint32 callbackGasLimit_) internal {
if (callbackGasLimit_ == 0) revert CallbackGasLimitZero();
_callbackGasLimit = callbackGasLimit_;
emit SetCallbackGasLimit(_callbackGasLimit);
}
/// @notice Sets a new request confirmation count.
/// @param requestConfirmations_ The new request confirmation count
function _setRequestConfirmations(uint16 requestConfirmations_) internal {
if (requestConfirmations_ == 0) revert RequestConfirmationsZero();
_requestConfirmations = requestConfirmations_;
emit SetRequestConfirmations(_requestConfirmations);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import { IRngAuction } from "./interfaces/IRngAuction.sol";
import { ChainlinkVRFV2Direct } from "./ChainlinkVRFV2Direct.sol";
import { IERC20 } from "openzeppelin-contracts/contracts/interfaces/IERC20.sol";
import { VRFV2Wrapper } from "chainlink/vrf/VRFV2Wrapper.sol";
/// @notice Thrown if the ChainlinkVRFV2Direct contract is set to the zero address.
error ChainlinkVRFV2DirectZeroAddress();
/// @notice Thrown if the RngAuction contract is set to the zero address.
error RngAuctionZeroAddress();
/**
* @notice Thrown if the active RNG service of the RngAuction doesn't match the address of
* the ChainlinkVRFV2Direct contract.
* @param chainlinkVrfV2Direct The ChainlinkVRFV2Direct contract address
* @param activeRngService The active RNG service of the RngAuction
*/
error RngServiceNotActive(address chainlinkVrfV2Direct, address activeRngService);
/**
* @title PoolTogether V5 ChainlinkVRFV2DirectRngAuctionHelper
* @author G9 Software Inc.
* @notice This is a helper contract to provide clients a simplified interface to interact
* with the RNGAuction if a fee needs to be transferred before starting the RNG request.
*/
contract ChainlinkVRFV2DirectRngAuctionHelper {
/// @notice The ChainlinkVRFV2Direct contract that the fee will be transferred to.
ChainlinkVRFV2Direct public immutable chainlinkVrfV2Direct;
/// @notice The RngAuction that will be completed after the fee is transferred.
IRngAuction public immutable rngAuction;
/**
* @notice Initializes the contract with the target ChainlinkVRFV2Direct and RngAuction
* contracts.
* @param _chainlinkVrfV2Direct The ChainlinkVRFV2Direct contract that the fee will be transferred to.
* @param _rngAuction The RngAuction contract that will be completed after the fee is transferred.
*/
constructor (ChainlinkVRFV2Direct _chainlinkVrfV2Direct, IRngAuction _rngAuction) {
if (address(_chainlinkVrfV2Direct) == address(0)) revert ChainlinkVRFV2DirectZeroAddress();
if (address(_rngAuction) == address(0)) revert RngAuctionZeroAddress();
chainlinkVrfV2Direct = _chainlinkVrfV2Direct;
rngAuction = _rngAuction;
}
/**
* @notice Transfers the RNG fee from the caller to the ChainlinkVRFV2Direct contract before
* completing the RNG auction by starting the RNG request.
* @dev Will revert if the active RNG service of the RngAuction does not match the ChainlinkVRFV2Direct
* contract address.
* @dev To estimate the request fee, use the `estimateRequestFee(...)` function on this contract.
* @dev DO NOT USE THE `getRequestFee()` FUNCTION ON THE RNG SERVICE TO PREDICT THE FEE AS IT REQUIRES A
* TX GAS PRICE TO CALCULATE THE CORRECT VALUE!
* @param _rewardRecipient Address that will receive the auction reward for starting the RNG request
*/
function transferFeeAndStartRngRequest(address _rewardRecipient) external {
if (address(rngAuction.getNextRngService()) != address(chainlinkVrfV2Direct)) {
revert RngServiceNotActive(address(chainlinkVrfV2Direct), address(rngAuction.getNextRngService()));
}
(address _feeToken, uint256 _requestFee) = chainlinkVrfV2Direct.getRequestFee();
IERC20(_feeToken).transferFrom(msg.sender, address(chainlinkVrfV2Direct), _requestFee);
rngAuction.startRngRequest(_rewardRecipient);
}
/**
* @notice Estimates the RNG request fee in LINK based on the expected gas price.
* @dev Use this function instead of `RNGInterface.getRequestFee()` when estimating request fees offchain.
* @param _gasPrice The gas price to calculate the request fee for
* @return _feeToken The LINK address
* @return _requestFee The estimated request fee based on the given gas price
*/
function estimateRequestFee(uint256 _gasPrice) external view returns (address _feeToken, uint256 _requestFee) {
VRFV2Wrapper wrapper = chainlinkVrfV2Direct.vrfV2Wrapper();
_feeToken = address(wrapper.LINK());
_requestFee = wrapper.estimateRequestPrice(chainlinkVrfV2Direct.getCallbackGasLimit(), _gasPrice);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./ConfirmedOwnerWithProposal.sol";
/**
* @title The ConfirmedOwner contract
* @notice A contract with helpers for basic contract ownership.
*/
contract ConfirmedOwner is ConfirmedOwnerWithProposal {
constructor(address newOwner) ConfirmedOwnerWithProposal(newOwner, address(0)) {}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./interfaces/OwnableInterface.sol";
/**
* @title The ConfirmedOwner contract
* @notice A contract with helpers for basic contract ownership.
*/
contract ConfirmedOwnerWithProposal is OwnableInterface {
address private s_owner;
address private s_pendingOwner;
event OwnershipTransferRequested(address indexed from, address indexed to);
event OwnershipTransferred(address indexed from, address indexed to);
constructor(address newOwner, address pendingOwner) {
require(newOwner != address(0), "Cannot set owner to zero");
s_owner = newOwner;
if (pendingOwner != address(0)) {
_transferOwnership(pendingOwner);
}
}
/**
* @notice Allows an owner to begin transferring ownership to a new address,
* pending.
*/
function transferOwnership(address to) public override onlyOwner {
_transferOwnership(to);
}
/**
* @notice Allows an ownership transfer to be completed by the recipient.
*/
function acceptOwnership() external override {
require(msg.sender == s_pendingOwner, "Must be proposed owner");
address oldOwner = s_owner;
s_owner = msg.sender;
s_pendingOwner = address(0);
emit OwnershipTransferred(oldOwner, msg.sender);
}
/**
* @notice Get the current owner
*/
function owner() public view override returns (address) {
return s_owner;
}
/**
* @notice validate, transfer ownership, and emit relevant events
*/
function _transferOwnership(address to) private {
require(to != msg.sender, "Cannot transfer to self");
s_pendingOwner = to;
emit OwnershipTransferRequested(s_owner, to);
}
/**
* @notice validate access
*/
function _validateOwnership() internal view {
require(msg.sender == s_owner, "Only callable by owner");
}
/**
* @notice Reverts if called by anyone other than the contract owner.
*/
modifier onlyOwner() {
_validateOwnership();
_;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import { RNGInterface } from "rng-contracts/RNGInterface.sol";
interface IRngAuction {
function startRngRequest(address _rewardRecipient) external;
function getNextRngService() external view returns (RNGInterface);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface LinkTokenInterface {
function allowance(address owner, address spender) external view returns (uint256 remaining);
function approve(address spender, uint256 value) external returns (bool success);
function balanceOf(address owner) external view returns (uint256 balance);
function decimals() external view returns (uint8 decimalPlaces);
function decreaseApproval(address spender, uint256 addedValue) external returns (bool success);
function increaseApproval(address spender, uint256 subtractedValue) external;
function name() external view returns (string memory tokenName);
function symbol() external view returns (string memory tokenSymbol);
function totalSupply() external view returns (uint256 totalTokensIssued);
function transfer(address to, uint256 value) external returns (bool success);
function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool success);
function transferFrom(address from, address to, uint256 value) external returns (bool success);
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
/**
* @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 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 {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 {
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.
*/
constructor(address _initialOwner) {
_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");
_;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface OwnableInterface {
function owner() external returns (address);
function transferOwnership(address recipient) external;
function acceptOwnership() external;
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.6;
/**
* @title Random Number Generator Interface
* @notice Provides an interface for requesting random numbers from 3rd-party RNG services (Chainlink VRF, Starkware VDF, etc..)
*/
interface RNGInterface {
/**
* @notice Emitted when a new request for a random number has been submitted
* @param requestId The indexed ID of the request used to get the results of the RNG service
* @param sender The indexed address of the sender of the request
*/
event RandomNumberRequested(uint32 indexed requestId, address indexed sender);
/**
* @notice Emitted when an existing request for a random number has been completed
* @param requestId The indexed ID of the request used to get the results of the RNG service
* @param randomNumber The random number produced by the 3rd-party service
*/
event RandomNumberCompleted(uint32 indexed requestId, uint256 randomNumber);
/**
* @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 number 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
* @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.
* The calling contract should "lock" all activity until the result is available via the `requestId`
*/
function requestRandomNumber() 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 isRequestComplete(uint32 requestId) external view returns (bool isCompleted);
/**
* @notice Gets the random number produced by the 3rd-party service
* @param requestId The ID of the request used to get the results of the RNG service
* @return randomNum The random number
*/
function randomNumber(uint32 requestId) external returns (uint256 randomNum);
/**
* @notice Returns the timestamps at which the request was completed
* @param requestId The ID of the request used to get the results of the RNG service
* @return completedAtTimestamp The timestamp at which the request was completed
*/
function completedAt(uint32 requestId) external view returns (uint64 completedAtTimestamp);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
abstract contract TypeAndVersionInterface {
function typeAndVersion() external pure virtual returns (string memory);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/** ****************************************************************************
* @notice Interface for contracts using VRF randomness
* *****************************************************************************
* @dev PURPOSE
*
* @dev Reggie the Random Oracle (not his real job) wants to provide randomness
* @dev to Vera the verifier in such a way that Vera can be sure he's not
* @dev making his output up to suit himself. Reggie provides Vera a public key
* @dev to which he knows the secret key. Each time Vera provides a seed to
* @dev Reggie, he gives back a value which is computed completely
* @dev deterministically from the seed and the secret key.
*
* @dev Reggie provides a proof by which Vera can verify that the output was
* @dev correctly computed once Reggie tells it to her, but without that proof,
* @dev the output is indistinguishable to her from a uniform random sample
* @dev from the output space.
*
* @dev The purpose of this contract is to make it easy for unrelated contracts
* @dev to talk to Vera the verifier about the work Reggie is doing, to provide
* @dev simple access to a verifiable source of randomness. It ensures 2 things:
* @dev 1. The fulfillment came from the VRFCoordinator
* @dev 2. The consumer contract implements fulfillRandomWords.
* *****************************************************************************
* @dev USAGE
*
* @dev Calling contracts must inherit from VRFConsumerBase, and can
* @dev initialize VRFConsumerBase's attributes in their constructor as
* @dev shown:
*
* @dev contract VRFConsumer {
* @dev constructor(<other arguments>, address _vrfCoordinator, address _link)
* @dev VRFConsumerBase(_vrfCoordinator) public {
* @dev <initialization with other arguments goes here>
* @dev }
* @dev }
*
* @dev The oracle will have given you an ID for the VRF keypair they have
* @dev committed to (let's call it keyHash). Create subscription, fund it
* @dev and your consumer contract as a consumer of it (see VRFCoordinatorInterface
* @dev subscription management functions).
* @dev Call requestRandomWords(keyHash, subId, minimumRequestConfirmations,
* @dev callbackGasLimit, numWords),
* @dev see (VRFCoordinatorInterface for a description of the arguments).
*
* @dev Once the VRFCoordinator has received and validated the oracle's response
* @dev to your request, it will call your contract's fulfillRandomWords method.
*
* @dev The randomness argument to fulfillRandomWords is a set of random words
* @dev generated from your requestId and the blockHash of the request.
*
* @dev If your contract could have concurrent requests open, you can use the
* @dev requestId returned from requestRandomWords to track which response is associated
* @dev with which randomness request.
* @dev See "SECURITY CONSIDERATIONS" for principles to keep in mind,
* @dev if your contract could have multiple requests in flight simultaneously.
*
* @dev Colliding `requestId`s are cryptographically impossible as long as seeds
* @dev differ.
*
* *****************************************************************************
* @dev SECURITY CONSIDERATIONS
*
* @dev A method with the ability to call your fulfillRandomness method directly
* @dev could spoof a VRF response with any random value, so it's critical that
* @dev it cannot be directly called by anything other than this base contract
* @dev (specifically, by the VRFConsumerBase.rawFulfillRandomness method).
*
* @dev For your users to trust that your contract's random behavior is free
* @dev from malicious interference, it's best if you can write it so that all
* @dev behaviors implied by a VRF response are executed *during* your
* @dev fulfillRandomness method. If your contract must store the response (or
* @dev anything derived from it) and use it later, you must ensure that any
* @dev user-significant behavior which depends on that stored value cannot be
* @dev manipulated by a subsequent VRF request.
*
* @dev Similarly, both miners and the VRF oracle itself have some influence
* @dev over the order in which VRF responses appear on the blockchain, so if
* @dev your contract could have multiple VRF requests in flight simultaneously,
* @dev you must ensure that the order in which the VRF responses arrive cannot
* @dev be used to manipulate your contract's user-significant behavior.
*
* @dev Since the block hash of the block which contains the requestRandomness
* @dev call is mixed into the input to the VRF *last*, a sufficiently powerful
* @dev miner could, in principle, fork the blockchain to evict the block
* @dev containing the request, forcing the request to be included in a
* @dev different block with a different hash, and therefore a different input
* @dev to the VRF. However, such an attack would incur a substantial economic
* @dev cost. This cost scales with the number of blocks the VRF oracle waits
* @dev until it calls responds to a request. It is for this reason that
* @dev that you can signal to an oracle you'd like them to wait longer before
* @dev responding to the request (however this is not enforced in the contract
* @dev and so remains effective only in the case of unmodified oracle software).
*/
abstract contract VRFConsumerBaseV2 {
error OnlyCoordinatorCanFulfill(address have, address want);
address private immutable vrfCoordinator;
/**
* @param _vrfCoordinator address of VRFCoordinator contract
*/
constructor(address _vrfCoordinator) {
vrfCoordinator = _vrfCoordinator;
}
/**
* @notice fulfillRandomness handles the VRF response. Your contract must
* @notice implement it. See "SECURITY CONSIDERATIONS" above for important
* @notice principles to keep in mind when implementing your fulfillRandomness
* @notice method.
*
* @dev VRFConsumerBaseV2 expects its subcontracts to have a method with this
* @dev signature, and will call it once it has verified the proof
* @dev associated with the randomness. (It is triggered via a call to
* @dev rawFulfillRandomness, below.)
*
* @param requestId The Id initially returned by requestRandomness
* @param randomWords the VRF output expanded to the requested number of words
*/
function fulfillRandomWords(uint256 requestId, uint256[] memory randomWords) internal virtual;
// rawFulfillRandomness is called by VRFCoordinator when it receives a valid VRF
// proof. rawFulfillRandomness 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);
}
}
// 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);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;
import "../ConfirmedOwner.sol";
import "../interfaces/TypeAndVersionInterface.sol";
import "./VRFConsumerBaseV2.sol";
import "../interfaces/LinkTokenInterface.sol";
import "../interfaces/AggregatorV3Interface.sol";
import "../interfaces/VRFCoordinatorV2Interface.sol";
import "../interfaces/VRFV2WrapperInterface.sol";
import "./VRFV2WrapperConsumerBase.sol";
import "../ChainSpecificUtil.sol";
/**
* @notice A wrapper for VRFCoordinatorV2 that provides an interface better suited to one-off
* @notice requests for randomness.
*/
contract VRFV2Wrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsumerBaseV2, VRFV2WrapperInterface {
event WrapperFulfillmentFailed(uint256 indexed requestId, address indexed consumer);
LinkTokenInterface public immutable LINK;
AggregatorV3Interface public immutable LINK_ETH_FEED;
ExtendedVRFCoordinatorV2Interface public immutable COORDINATOR;
uint64 public immutable SUBSCRIPTION_ID;
/// @dev this is the size of a VRF v2 fulfillment's calldata abi-encoded in bytes.
/// @dev proofSize = 13 words = 13 * 256 = 3328 bits
/// @dev commitmentSize = 5 words = 5 * 256 = 1280 bits
/// @dev dataSize = proofSize + commitmentSize = 4608 bits
/// @dev selector = 32 bits
/// @dev total data size = 4608 bits + 32 bits = 4640 bits = 580 bytes
uint32 public s_fulfillmentTxSizeBytes = 580;
// 5k is plenty for an EXTCODESIZE call (2600) + warm CALL (100)
// and some arithmetic operations.
uint256 private constant GAS_FOR_CALL_EXACT_CHECK = 5_000;
// lastRequestId is the request ID of the most recent VRF V2 request made by this wrapper. This
// should only be relied on within the same transaction the request was made.
uint256 public override lastRequestId;
// Configuration fetched from VRFCoordinatorV2
// s_configured tracks whether this contract has been configured. If not configured, randomness
// requests cannot be made.
bool public s_configured;
// s_disabled disables the contract when true. When disabled, new VRF requests cannot be made
// but existing ones can still be fulfilled.
bool public s_disabled;
// s_fallbackWeiPerUnitLink is the backup LINK exchange rate used when the LINK/NATIVE feed is
// stale.
int256 private s_fallbackWeiPerUnitLink;
// s_stalenessSeconds is the number of seconds before we consider the feed price to be stale and
// fallback to fallbackWeiPerUnitLink.
uint32 private s_stalenessSeconds;
// s_fulfillmentFlatFeeLinkPPM is the flat fee in millionths of LINK that VRFCoordinatorV2
// charges.
uint32 private s_fulfillmentFlatFeeLinkPPM;
// Other configuration
// s_wrapperGasOverhead reflects the gas overhead of the wrapper's fulfillRandomWords
// function. The cost for this gas is passed to the user.
uint32 private s_wrapperGasOverhead;
// s_coordinatorGasOverhead reflects the gas overhead of the coordinator's fulfillRandomWords
// function. The cost for this gas is billed to the subscription, and must therefor be included
// in the pricing for wrapped requests. This includes the gas costs of proof verification and
// payment calculation in the coordinator.
uint32 private s_coordinatorGasOverhead;
// s_wrapperPremiumPercentage is the premium ratio in percentage. For example, a value of 0
// indicates no premium. A value of 15 indicates a 15 percent premium.
uint8 private s_wrapperPremiumPercentage;
// s_keyHash is the key hash to use when requesting randomness. Fees are paid based on current gas
// fees, so this should be set to the highest gas lane on the network.
bytes32 s_keyHash;
// s_maxNumWords is the max number of words that can be requested in a single wrapped VRF request.
uint8 s_maxNumWords;
struct Callback {
address callbackAddress;
uint32 callbackGasLimit;
uint256 requestGasPrice;
int256 requestWeiPerUnitLink;
uint256 juelsPaid;
}
mapping(uint256 => Callback) /* requestID */ /* callback */ public s_callbacks;
constructor(
address _link,
address _linkEthFeed,
address _coordinator
) ConfirmedOwner(msg.sender) VRFConsumerBaseV2(_coordinator) {
LINK = LinkTokenInterface(_link);
LINK_ETH_FEED = AggregatorV3Interface(_linkEthFeed);
COORDINATOR = ExtendedVRFCoordinatorV2Interface(_coordinator);
// Create this wrapper's subscription and add itself as a consumer.
uint64 subId = ExtendedVRFCoordinatorV2Interface(_coordinator).createSubscription();
SUBSCRIPTION_ID = subId;
ExtendedVRFCoordinatorV2Interface(_coordinator).addConsumer(subId, address(this));
}
/**
* @notice setFulfillmentTxSize sets the size of the fulfillment transaction in bytes.
* @param size is the size of the fulfillment transaction in bytes.
*/
function setFulfillmentTxSize(uint32 size) external onlyOwner {
s_fulfillmentTxSizeBytes = size;
}
/**
* @notice setConfig configures VRFV2Wrapper.
*
* @dev Sets wrapper-specific configuration based on the given parameters, and fetches any needed
* @dev VRFCoordinatorV2 configuration from the coordinator.
*
* @param _wrapperGasOverhead reflects the gas overhead of the wrapper's fulfillRandomWords
* function.
*
* @param _coordinatorGasOverhead reflects the gas overhead of the coordinator's
* fulfillRandomWords function.
*
* @param _wrapperPremiumPercentage is the premium ratio in percentage for wrapper requests.
*
* @param _keyHash to use for requesting randomness.
*/
function setConfig(
uint32 _wrapperGasOverhead,
uint32 _coordinatorGasOverhead,
uint8 _wrapperPremiumPercentage,
bytes32 _keyHash,
uint8 _maxNumWords
) external onlyOwner {
s_wrapperGasOverhead = _wrapperGasOverhead;
s_coordinatorGasOverhead = _coordinatorGasOverhead;
s_wrapperPremiumPercentage = _wrapperPremiumPercentage;
s_keyHash = _keyHash;
s_maxNumWords = _maxNumWords;
s_configured = true;
// Get other configuration from coordinator
(, , s_stalenessSeconds, ) = COORDINATOR.getConfig();
s_fallbackWeiPerUnitLink = COORDINATOR.getFallbackWeiPerUnitLink();
(s_fulfillmentFlatFeeLinkPPM, , , , , , , , ) = COORDINATOR.getFeeConfig();
}
/**
* @notice getConfig returns the current VRFV2Wrapper configuration.
*
* @return fallbackWeiPerUnitLink is the backup LINK exchange rate used when the LINK/NATIVE feed
* is stale.
*
* @return stalenessSeconds is the number of seconds before we consider the feed price to be stale
* and fallback to fallbackWeiPerUnitLink.
*
* @return fulfillmentFlatFeeLinkPPM is the flat fee in millionths of LINK that VRFCoordinatorV2
* charges.
*
* @return wrapperGasOverhead reflects the gas overhead of the wrapper's fulfillRandomWords
* function. The cost for this gas is passed to the user.
*
* @return coordinatorGasOverhead reflects the gas overhead of the coordinator's
* fulfillRandomWords function.
*
* @return wrapperPremiumPercentage is the premium ratio in percentage. For example, a value of 0
* indicates no premium. A value of 15 indicates a 15 percent premium.
*
* @return keyHash is the key hash to use when requesting randomness. Fees are paid based on
* current gas fees, so this should be set to the highest gas lane on the network.
*
* @return maxNumWords is the max number of words that can be requested in a single wrapped VRF
* request.
*/
function getConfig()
external
view
returns (
int256 fallbackWeiPerUnitLink,
uint32 stalenessSeconds,
uint32 fulfillmentFlatFeeLinkPPM,
uint32 wrapperGasOverhead,
uint32 coordinatorGasOverhead,
uint8 wrapperPremiumPercentage,
bytes32 keyHash,
uint8 maxNumWords
)
{
return (
s_fallbackWeiPerUnitLink,
s_stalenessSeconds,
s_fulfillmentFlatFeeLinkPPM,
s_wrapperGasOverhead,
s_coordinatorGasOverhead,
s_wrapperPremiumPercentage,
s_keyHash,
s_maxNumWords
);
}
/**
* @notice Calculates the price of a VRF request with the given callbackGasLimit at the current
* @notice block.
*
* @dev This function relies on the transaction gas price which is not automatically set during
* @dev simulation. To estimate the price at a specific gas price, use the estimatePrice function.
*
* @param _callbackGasLimit is the gas limit used to estimate the price.
*/
function calculateRequestPrice(
uint32 _callbackGasLimit
) external view override onlyConfiguredNotDisabled returns (uint256) {
int256 weiPerUnitLink = getFeedData();
return calculateRequestPriceInternal(_callbackGasLimit, tx.gasprice, weiPerUnitLink);
}
/**
* @notice Estimates the price of a VRF request with a specific gas limit and gas price.
*
* @dev This is a convenience function that can be called in simulation to better understand
* @dev pricing.
*
* @param _callbackGasLimit is the gas limit used to estimate the price.
* @param _requestGasPriceWei is the gas price in wei used for the estimation.
*/
function estimateRequestPrice(
uint32 _callbackGasLimit,
uint256 _requestGasPriceWei
) external view override onlyConfiguredNotDisabled returns (uint256) {
int256 weiPerUnitLink = getFeedData();
return calculateRequestPriceInternal(_callbackGasLimit, _requestGasPriceWei, weiPerUnitLink);
}
function calculateRequestPriceInternal(
uint256 _gas,
uint256 _requestGasPrice,
int256 _weiPerUnitLink
) internal view returns (uint256) {
// costWei is the base fee denominated in wei (native)
// costWei takes into account the L1 posting costs of the VRF fulfillment
// transaction, if we are on an L2.
uint256 costWei = (_requestGasPrice *
(_gas + s_wrapperGasOverhead + s_coordinatorGasOverhead) +
ChainSpecificUtil.getL1CalldataGasCost(s_fulfillmentTxSizeBytes));
// (1e18 juels/link) * ((wei/gas * (gas)) + l1wei) / (wei/link) == 1e18 juels * wei/link / (wei/link) == 1e18 juels * wei/link * link/wei == juels
// baseFee is the base fee denominated in juels (link)
uint256 baseFee = (1e18 * costWei) / uint256(_weiPerUnitLink);
// feeWithPremium is the fee after the percentage premium is applied
uint256 feeWithPremium = (baseFee * (s_wrapperPremiumPercentage + 100)) / 100;
// feeWithFlatFee is the fee after the flat fee is applied on top of the premium
uint256 feeWithFlatFee = feeWithPremium + (1e12 * uint256(s_fulfillmentFlatFeeLinkPPM));
return feeWithFlatFee;
}
/**
* @notice onTokenTransfer is called by LinkToken upon payment for a VRF request.
*
* @dev Reverts if payment is too low.
*
* @param _sender is the sender of the payment, and the address that will receive a VRF callback
* upon fulfillment.
*
* @param _amount is the amount of LINK paid in Juels.
*
* @param _data is the abi-encoded VRF request parameters: uint32 callbackGasLimit,
* uint16 requestConfirmations, and uint32 numWords.
*/
function onTokenTransfer(address _sender, uint256 _amount, bytes calldata _data) external onlyConfiguredNotDisabled {
require(msg.sender == address(LINK), "only callable from LINK");
(uint32 callbackGasLimit, uint16 requestConfirmations, uint32 numWords) = abi.decode(
_data,
(uint32, uint16, uint32)
);
uint32 eip150Overhead = getEIP150Overhead(callbackGasLimit);
int256 weiPerUnitLink = getFeedData();
uint256 price = calculateRequestPriceInternal(callbackGasLimit, tx.gasprice, weiPerUnitLink);
require(_amount >= price, "fee too low");
require(numWords <= s_maxNumWords, "numWords too high");
uint256 requestId = COORDINATOR.requestRandomWords(
s_keyHash,
SUBSCRIPTION_ID,
requestConfirmations,
callbackGasLimit + eip150Overhead + s_wrapperGasOverhead,
numWords
);
s_callbacks[requestId] = Callback({
callbackAddress: _sender,
callbackGasLimit: callbackGasLimit,
requestGasPrice: tx.gasprice,
requestWeiPerUnitLink: weiPerUnitLink,
juelsPaid: _amount
});
lastRequestId = requestId;
}
/**
* @notice withdraw is used by the VRFV2Wrapper's owner to withdraw LINK revenue.
*
* @param _recipient is the address that should receive the LINK funds.
*
* @param _amount is the amount of LINK in Juels that should be withdrawn.
*/
function withdraw(address _recipient, uint256 _amount) external onlyOwner {
LINK.transfer(_recipient, _amount);
}
/**
* @notice enable this contract so that new requests can be accepted.
*/
function enable() external onlyOwner {
s_disabled = false;
}
/**
* @notice disable this contract so that new requests will be rejected. When disabled, new requests
* @notice will revert but existing requests can still be fulfilled.
*/
function disable() external onlyOwner {
s_disabled = true;
}
function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override {
Callback memory callback = s_callbacks[_requestId];
delete s_callbacks[_requestId];
require(callback.callbackAddress != address(0), "request not found"); // This should never happen
VRFV2WrapperConsumerBase c;
bytes memory resp = abi.encodeWithSelector(c.rawFulfillRandomWords.selector, _requestId, _randomWords);
bool success = callWithExactGas(callback.callbackGasLimit, callback.callbackAddress, resp);
if (!success) {
emit WrapperFulfillmentFailed(_requestId, callback.callbackAddress);
}
}
function getFeedData() private view returns (int256) {
bool staleFallback = s_stalenessSeconds > 0;
uint256 timestamp;
int256 weiPerUnitLink;
(, weiPerUnitLink, , timestamp, ) = LINK_ETH_FEED.latestRoundData();
// solhint-disable-next-line not-rely-on-time
if (staleFallback && s_stalenessSeconds < block.timestamp - timestamp) {
weiPerUnitLink = s_fallbackWeiPerUnitLink;
}
require(weiPerUnitLink >= 0, "Invalid LINK wei price");
return weiPerUnitLink;
}
/**
* @dev Calculates extra amount of gas required for running an assembly call() post-EIP150.
*/
function getEIP150Overhead(uint32 gas) private pure returns (uint32) {
return gas / 63 + 1;
}
/**
* @dev calls target address with exactly gasAmount gas and data as calldata
* or reverts if at least gasAmount gas is not available.
*/
function callWithExactGas(uint256 gasAmount, address target, bytes memory data) private returns (bool success) {
// solhint-disable-next-line no-inline-assembly
assembly {
let g := gas()
// Compute g -= GAS_FOR_CALL_EXACT_CHECK and check for underflow
// The gas actually passed to the callee is min(gasAmount, 63//64*gas available).
// We want to ensure that we revert if gasAmount > 63//64*gas available
// as we do not want to provide them with less, however that check itself costs
// gas. GAS_FOR_CALL_EXACT_CHECK ensures we have at least enough gas to be able
// to revert if gasAmount > 63//64*gas available.
if lt(g, GAS_FOR_CALL_EXACT_CHECK) {
revert(0, 0)
}
g := sub(g, GAS_FOR_CALL_EXACT_CHECK)
// if g - g//64 <= gasAmount, revert
// (we subtract g//64 because of EIP-150)
if iszero(gt(sub(g, div(g, 64)), gasAmount)) {
revert(0, 0)
}
// solidity calls check that a contract actually exists at the destination, so we do the same
if iszero(extcodesize(target)) {
revert(0, 0)
}
// call and return whether we succeeded. ignore return data
// call(gas,addr,value,argsOffset,argsLength,retOffset,retLength)
success := call(gasAmount, target, 0, add(data, 0x20), mload(data), 0, 0)
}
return success;
}
function typeAndVersion() external pure virtual override returns (string memory) {
return "VRFV2Wrapper 1.0.0";
}
modifier onlyConfiguredNotDisabled() {
require(s_configured, "wrapper is not configured");
require(!s_disabled, "wrapper is disabled");
_;
}
}
interface ExtendedVRFCoordinatorV2Interface is VRFCoordinatorV2Interface {
function getConfig()
external
view
returns (
uint16 minimumRequestConfirmations,
uint32 maxGasLimit,
uint32 stalenessSeconds,
uint32 gasAfterPaymentCalculation
);
function getFallbackWeiPerUnitLink() external view returns (int256);
function getFeeConfig()
external
view
returns (
uint32 fulfillmentFlatFeeLinkPPMTier1,
uint32 fulfillmentFlatFeeLinkPPMTier2,
uint32 fulfillmentFlatFeeLinkPPMTier3,
uint32 fulfillmentFlatFeeLinkPPMTier4,
uint32 fulfillmentFlatFeeLinkPPMTier5,
uint24 reqsForTier2,
uint24 reqsForTier3,
uint24 reqsForTier4,
uint24 reqsForTier5
);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../interfaces/LinkTokenInterface.sol";
import "../interfaces/VRFV2WrapperInterface.sol";
/** *******************************************************************************
* @notice Interface for contracts using VRF randomness through the VRF V2 wrapper
* ********************************************************************************
* @dev PURPOSE
*
* @dev Create VRF V2 requests without the need for subscription management. Rather than creating
* @dev and funding a VRF V2 subscription, a user can use this wrapper to create one off requests,
* @dev paying up front rather than at fulfillment.
*
* @dev Since the price is determined using the gas price of the request transaction rather than
* @dev the fulfillment transaction, the wrapper charges an additional premium on callback gas
* @dev usage, in addition to some extra overhead costs associated with the VRFV2Wrapper contract.
* *****************************************************************************
* @dev USAGE
*
* @dev Calling contracts must inherit from VRFV2WrapperConsumerBase. The consumer must be funded
* @dev with enough LINK to make the request, otherwise requests will revert. To request randomness,
* @dev call the 'requestRandomness' function with the desired VRF parameters. This function handles
* @dev paying for the request based on the current pricing.
*
* @dev Consumers must implement the fullfillRandomWords function, which will be called during
* @dev fulfillment with the randomness result.
*/
abstract contract VRFV2WrapperConsumerBase {
LinkTokenInterface internal immutable LINK;
VRFV2WrapperInterface internal immutable VRF_V2_WRAPPER;
/**
* @param _link is the address of LinkToken
* @param _vrfV2Wrapper is the address of the VRFV2Wrapper contract
*/
constructor(address _link, address _vrfV2Wrapper) {
LINK = LinkTokenInterface(_link);
VRF_V2_WRAPPER = VRFV2WrapperInterface(_vrfV2Wrapper);
}
/**
* @dev Requests randomness from the VRF V2 wrapper.
*
* @param _callbackGasLimit is the gas limit that should be used when calling the consumer's
* fulfillRandomWords function.
* @param _requestConfirmations is the number of confirmations to wait before fulfilling the
* request. A higher number of confirmations increases security by reducing the likelihood
* that a chain re-org changes a published randomness outcome.
* @param _numWords is the number of random words to request.
*
* @return requestId is the VRF V2 request ID of the newly created randomness request.
*/
function requestRandomness(
uint32 _callbackGasLimit,
uint16 _requestConfirmations,
uint32 _numWords
) internal returns (uint256 requestId) {
LINK.transferAndCall(
address(VRF_V2_WRAPPER),
VRF_V2_WRAPPER.calculateRequestPrice(_callbackGasLimit),
abi.encode(_callbackGasLimit, _requestConfirmations, _numWords)
);
return VRF_V2_WRAPPER.lastRequestId();
}
/**
* @notice fulfillRandomWords handles the VRF V2 wrapper response. The consuming contract must
* @notice implement it.
*
* @param _requestId is the VRF V2 request ID.
* @param _randomWords is the randomness result.
*/
function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal virtual;
function rawFulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) external {
require(msg.sender == address(VRF_V2_WRAPPER), "only VRF V2 wrapper can fulfill");
fulfillRandomWords(_requestId, _randomWords);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface VRFV2WrapperInterface {
/**
* @return the request ID of the most recent VRF V2 request made by this wrapper. This should only
* be relied option within the same transaction that the request was made.
*/
function lastRequestId() external view returns (uint256);
/**
* @notice Calculates the price of a VRF request with the given callbackGasLimit at the current
* @notice block.
*
* @dev This function relies on the transaction gas price which is not automatically set during
* @dev simulation. To estimate the price at a specific gas price, use the estimatePrice function.
*
* @param _callbackGasLimit is the gas limit used to estimate the price.
*/
function calculateRequestPrice(uint32 _callbackGasLimit) external view returns (uint256);
/**
* @notice Estimates the price of a VRF request with a specific gas limit and gas price.
*
* @dev This is a convenience function that can be called in simulation to better understand
* @dev pricing.
*
* @param _callbackGasLimit is the gas limit used to estimate the price.
* @param _requestGasPriceWei is the gas price in wei used for the estimation.
*/
function estimateRequestPrice(uint32 _callbackGasLimit, uint256 _requestGasPriceWei) external view returns (uint256);
}
{
"compilationTarget": {
"lib/pt-v5-chainlink-vrf-v2-direct/src/ChainlinkVRFV2DirectRngAuctionHelper.sol": "ChainlinkVRFV2DirectRngAuctionHelper"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": [
":@aave/core-v3/=lib/aave-address-book/lib/aave-v3-core/",
":@aave/periphery-v3/=lib/aave-address-book/lib/aave-v3-periphery/",
":@openzeppelin/=lib/pt-v5-draw-auction/lib/openzeppelin-contracts/",
":@prb/test/=lib/pt-v5-vault-boost/lib/prb-math/lib/prb-test/src/",
":aave-address-book/=lib/aave-address-book/src/",
":aave-v3-core/=lib/aave-address-book/lib/aave-v3-core/",
":aave-v3-periphery/=lib/aave-address-book/lib/aave-v3-periphery/",
":brokentoken/=lib/pt-v5-vault/lib/brokentoken/src/",
":chainlink/=lib/pt-v5-chainlink-vrf-v2-direct/lib/chainlink/contracts/src/v0.8/",
":create3-factory/=lib/yield-daddy/lib/create3-factory/",
":ds-test/=lib/forge-std/lib/ds-test/src/",
":erc4626-tests/=lib/pt-v5-vault/lib/erc4626-tests/",
":erc5164-interfaces/=lib/pt-v5-draw-auction/lib/erc5164-interfaces/src/",
":forge-std/=lib/forge-std/src/",
":openzeppelin-contracts/=lib/openzeppelin-contracts/",
":openzeppelin/=lib/openzeppelin-contracts/contracts/",
":owner-manager-contracts/=lib/pt-v5-chainlink-vrf-v2-direct/lib/owner-manager-contracts/contracts/",
":owner-manager/=lib/pt-v5-chainlink-vrf-v2-direct/lib/owner-manager-contracts/contracts/",
":prb-math/=lib/pt-v5-cgda-liquidator/lib/prb-math/src/",
":prb-test/=lib/pt-v5-vault-boost/lib/prb-math/lib/prb-test/src/",
":pt-v5-cgda-liquidator/=lib/pt-v5-cgda-liquidator/src/",
":pt-v5-chainlink-vrf-v2-direct/=lib/pt-v5-chainlink-vrf-v2-direct/src/",
":pt-v5-claimable-interface/=lib/pt-v5-vault/lib/pt-v5-claimable-interface/src/",
":pt-v5-claimer/=lib/pt-v5-claimer/src/",
":pt-v5-draw-auction/=lib/pt-v5-draw-auction/src/",
":pt-v5-liquidator-interfaces/=lib/pt-v5-cgda-liquidator/lib/pt-v5-liquidator-interfaces/src/interfaces/",
":pt-v5-prize-pool/=lib/pt-v5-prize-pool/src/",
":pt-v5-rng-contracts/=lib/pt-v5-rng-contracts/contracts/",
":pt-v5-twab-controller/=lib/pt-v5-twab-controller/src/",
":pt-v5-twab-delegator/=lib/pt-v5-twab-delegator/src/",
":pt-v5-vault-boost/=lib/pt-v5-vault-boost/src/",
":pt-v5-vault-mock/=lib/pt-v5-vault/test/contracts/mock/",
":pt-v5-vault/=lib/pt-v5-vault/src/",
":remote-owner/=lib/pt-v5-draw-auction/lib/remote-owner/src/",
":ring-buffer-lib/=lib/pt-v5-twab-controller/lib/ring-buffer-lib/src/",
":rng-contracts/=lib/pt-v5-draw-auction/lib/pt-v5-rng-contracts/contracts/",
":rng/=lib/pt-v5-draw-auction/lib/pt-v5-rng-contracts/contracts/",
":solidity-stringutils/=lib/solidity-stringutils/src/",
":solmate/=lib/yield-daddy/lib/solmate/src/",
":uniform-random-number/=lib/pt-v5-prize-pool/lib/uniform-random-number/src/",
":weird-erc20/=lib/pt-v5-vault/lib/brokentoken/lib/weird-erc20/src/",
":yield-daddy/=lib/yield-daddy/src/"
],
"viaIR": true
}
[{"inputs":[{"internalType":"contract ChainlinkVRFV2Direct","name":"_chainlinkVrfV2Direct","type":"address"},{"internalType":"contract IRngAuction","name":"_rngAuction","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ChainlinkVRFV2DirectZeroAddress","type":"error"},{"inputs":[],"name":"RngAuctionZeroAddress","type":"error"},{"inputs":[{"internalType":"address","name":"chainlinkVrfV2Direct","type":"address"},{"internalType":"address","name":"activeRngService","type":"address"}],"name":"RngServiceNotActive","type":"error"},{"inputs":[],"name":"chainlinkVrfV2Direct","outputs":[{"internalType":"contract ChainlinkVRFV2Direct","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_gasPrice","type":"uint256"}],"name":"estimateRequestFee","outputs":[{"internalType":"address","name":"_feeToken","type":"address"},{"internalType":"uint256","name":"_requestFee","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rngAuction","outputs":[{"internalType":"contract IRngAuction","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_rewardRecipient","type":"address"}],"name":"transferFeeAndStartRngRequest","outputs":[],"stateMutability":"nonpayable","type":"function"}]