Verified contractExactGPL-3.0
PrizeFlush
Address0xd360c4eed8d02c6d43dab90466159f580c0f68bb
- 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
- 17,093,070
- Creation transaction
- 0x9937a117f5…100983e5c3
20 files
- contracts/periphery/PrizeFlush.solmain9.44 KB
- @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol6.63 KB
- @openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol2.78 KB
- @openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol8.28 KB
- @openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol35.09 KB
- contracts/core/external/compound/ICompLike.sol317 B
- contracts/core/interfaces/IControlledToken.sol1.76 KB
- contracts/core/interfaces/IDrawBeacon.sol5.22 KB
- contracts/core/interfaces/IDrawBuffer.sol4.15 KB
- contracts/core/interfaces/IPrizePool.sol14.53 KB
- contracts/core/interfaces/IReserve.sol1.94 KB
- contracts/core/interfaces/ITicket.sol7.96 KB
- contracts/core/libraries/ExtendedSafeCastLib.sol2.36 KB
- contracts/core/libraries/ObservationLib.sol4.56 KB
- contracts/core/libraries/OverflowSafeComparatorLib.sol2.94 KB
- contracts/core/libraries/RingBufferLib.sol2.46 KB
- contracts/core/libraries/TwabLib.sol20.14 KB
- contracts/owner-manager/Manageable.sol3.48 KB
- contracts/owner-manager/Ownable.sol4.49 KB
- contracts/periphery/interfaces/IPrizeFlush.sol4.03 KB
contracts/periphery/PrizeFlush.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "../owner-manager/Manageable.sol";
import "./interfaces/IPrizeFlush.sol";
/**
* @title Asymetrix Protocol V1 PrizeFlush
* @author Asymetrix Protocol Inc Team
* @notice The PrizeFlush contract helps capture interest from the PrizePool and
* move collected funds to a designated PrizeDistributor contract. When
* deployed, the destination, reserve addresses are set and used as
* static parameters during every "flush" execution. The parameters can
* be reset by the owner if necessary. The protocol can charge a fee
* during the movement of the funds.
*/
contract PrizeFlush is Initializable, IPrizeFlush, Manageable {
/**
* @notice Destination address for captured interest.
* @dev Should be set to the PrizeDistributor address.
*/
address internal destination;
/// @notice Fee percentage.
uint16 internal protocolFeePercentage;
/// @notice Representation of 100% with 3 decimal places, used to split the
/// obtained funds.
uint16 public constant ONE_AS_FIXED_POINT_3 = 1000;
/// @notice Receives the protocol fee.
address internal protocolFeeRecipient;
/// @notice Reserve address.
IReserve internal reserve;
/// @notice Prize Pool address.
IPrizePool internal prizePool;
/**
* @notice Emitted when contract has been deployed.
* @param destination Destination address.
* @param reserve Reserve contract address.
*
*/
event Deployed(address indexed destination, IReserve indexed reserve);
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/* ============ Initialize ============ */
/**
* @notice Deploy Prize Flush.
* @param _owner Prize Flush owner address.
* @param _destination Destination address.
* @param _reserve Reserve address.
* @param _prizePool PrizePool address.
*/
function initialize(
address _owner,
address _destination,
IReserve _reserve,
IPrizePool _prizePool
) external initializer {
__Manageable_init_unchained(_owner);
_setDestination(_destination);
_setReserve(_reserve);
_setPrizePool(_prizePool);
emit Deployed(_destination, _reserve);
}
/* ============ External Functions ============ */
/// @inheritdoc IPrizeFlush
function getDestination() external view override returns (address) {
return destination;
}
/// @inheritdoc IPrizeFlush
function getReserve() external view override returns (IReserve) {
return reserve;
}
/// @inheritdoc IPrizeFlush
function getPrizePool() external view override returns (IPrizePool) {
return prizePool;
}
/// @inheritdoc IPrizeFlush
function getProtocolFeeRecipient()
external
view
override
returns (address)
{
return protocolFeeRecipient;
}
/// @inheritdoc IPrizeFlush
function getProtocolFeePercentage()
external
view
override
returns (uint16)
{
return protocolFeePercentage;
}
/// @inheritdoc IPrizeFlush
function setDestination(
address _destination
) external override onlyOwner returns (address) {
_setDestination(_destination);
emit DestinationSet(_destination);
return _destination;
}
/// @inheritdoc IPrizeFlush
function setReserve(
IReserve _reserve
) external override onlyOwner returns (IReserve) {
_setReserve(_reserve);
emit ReserveSet(_reserve);
return _reserve;
}
/// @inheritdoc IPrizeFlush
function setPrizePool(
IPrizePool _prizePool
) external override onlyOwner returns (IPrizePool) {
_setPrizePool(_prizePool);
emit PrizePoolSet(_prizePool);
return _prizePool;
}
/// @inheritdoc IPrizeFlush
function setProtocolFeeRecipient(
address _protocolFeeRecipient
) external override onlyOwner returns (address) {
_setProtocolFeeRecipient(_protocolFeeRecipient);
emit ProtocolFeeRecipientSet(_protocolFeeRecipient);
return _protocolFeeRecipient;
}
/// @inheritdoc IPrizeFlush
function setProtocolFeePercentage(
uint16 _protocolFeePercentage
) external onlyOwner returns (uint16) {
_setProtocolFeePercentage(_protocolFeePercentage);
emit ProtocolPercentageSet(_protocolFeePercentage);
return _protocolFeePercentage;
}
/// @inheritdoc IPrizeFlush
function flush() external override onlyManagerOrOwner returns (bool) {
_distributeFunds();
// After funds are distributed, we EXPECT funds to be located in the
// Reserve contract.
IReserve _reserve = reserve;
IERC20Upgradeable _token = _reserve.getToken();
uint256 _amount = _token.balanceOf(address(_reserve));
// IF the tokens were successfully moved to the Reserve, now move them
// to the destination (PrizeDistributor) address.
if (_amount > 0) {
address _destination = destination;
// Create checkpoint and transfers new total balance to
// PrizeDistributor
_reserve.withdrawTo(_destination, _amount);
emit Flushed(_destination, _amount);
return true;
}
return false;
}
/* ============ Internal Functions ============ */
/**
* @notice Set global destination variable.
* @dev `_destination` cannot be the zero address.
* @param _destination Destination address.
*/
function _setDestination(address _destination) internal {
require(
_destination != address(0),
"Flush/destination-not-zero-address"
);
destination = _destination;
}
/**
* @notice Set global reserve variable.
* @dev `_reserve` cannot be the zero address.
* @param _reserve Reserve address.
*/
function _setReserve(IReserve _reserve) internal {
require(
address(_reserve) != address(0),
"Flush/reserve-not-zero-address"
);
reserve = _reserve;
}
/**
* @notice Set global prizePool variable.
* @dev `_prizePool` cannot be the zero address.
* @param _prizePool PrizePool address.
*/
function _setPrizePool(IPrizePool _prizePool) internal {
require(
address(_prizePool) != address(0),
"Flush/prizePool-not-zero-address"
);
prizePool = _prizePool;
}
/**
* @notice Set global protocolFeeRecipient variable.
* @dev `_protocolFeeRecipient` cannot be the zero address.
* @param _protocolFeeRecipient ProtocolFeeRecipient address.
*/
function _setProtocolFeeRecipient(address _protocolFeeRecipient) internal {
require(
address(_protocolFeeRecipient) != address(0),
"Flush/feeRecipient-not-zero-address"
);
protocolFeeRecipient = _protocolFeeRecipient;
}
/**
* @notice Set global protocolFeePercentage variable.
* @dev `_protocolFeePercentage` cannot be the zero address.
* @param _protocolFeePercentage ProtocolFeePercentage address.
*/
function _setProtocolFeePercentage(uint16 _protocolFeePercentage) internal {
require(
_protocolFeePercentage <= ONE_AS_FIXED_POINT_3,
"Flush/feePercentage-greater-100%"
);
protocolFeePercentage = _protocolFeePercentage;
}
/**
* @notice Distribute ticket tokens to prize to the reserve and protocol fee
* recipient.
* @dev Transfers the minted ticket tokens to the reserve and protocol fee
* recipient.
*/
function _distributeFunds() internal {
// Captures interest from PrizePool and distributes funds.
uint256 prize = prizePool.captureAwardBalance();
if (prize == 0) return;
if (protocolFeeRecipient == address(0)) {
_awardPrizeAmount(address(reserve), prize);
} else {
uint256 amountForReserve = (prize *
(ONE_AS_FIXED_POINT_3 - protocolFeePercentage)) /
ONE_AS_FIXED_POINT_3;
uint256 amountForProtocolFee = prize - amountForReserve;
if (amountForReserve > 0) {
_awardPrizeAmount(address(reserve), amountForReserve);
}
if (amountForProtocolFee > 0) {
_awardPrizeAmount(protocolFeeRecipient, amountForProtocolFee);
}
}
emit Distributed(prize);
}
/**
* @notice Award ticket tokens to prize split recipient.
* @dev Award ticket tokens to prize split recipient via the linked
* PrizePool contract.
* @param _to Recipient of minted tokens.
* @param _amount Amount of minted tokens.
*/
function _awardPrizeAmount(address _to, uint256 _amount) internal {
IControlledToken _ticket = prizePool.getTicket();
prizePool.award(_to, _amount);
emit PrizeAwarded(_to, _amount, _ticket);
}
}
@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/token/ERC20/IERC20Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20Upgradeable {
/**
* @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);
}
@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);
}
}
}
@openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.
pragma solidity ^0.8.0;
/**
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on `uint256` and `int256` and then downcasting.
*/
library SafeCastUpgradeable {
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toUint248(uint256 value) internal pure returns (uint248) {
require(value <= type(uint248).max, "SafeCast: value doesn't fit in 248 bits");
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toUint240(uint256 value) internal pure returns (uint240) {
require(value <= type(uint240).max, "SafeCast: value doesn't fit in 240 bits");
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toUint232(uint256 value) internal pure returns (uint232) {
require(value <= type(uint232).max, "SafeCast: value doesn't fit in 232 bits");
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.2._
*/
function toUint224(uint256 value) internal pure returns (uint224) {
require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toUint216(uint256 value) internal pure returns (uint216) {
require(value <= type(uint216).max, "SafeCast: value doesn't fit in 216 bits");
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toUint208(uint256 value) internal pure returns (uint208) {
require(value <= type(uint208).max, "SafeCast: value doesn't fit in 208 bits");
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toUint200(uint256 value) internal pure returns (uint200) {
require(value <= type(uint200).max, "SafeCast: value doesn't fit in 200 bits");
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toUint192(uint256 value) internal pure returns (uint192) {
require(value <= type(uint192).max, "SafeCast: value doesn't fit in 192 bits");
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toUint184(uint256 value) internal pure returns (uint184) {
require(value <= type(uint184).max, "SafeCast: value doesn't fit in 184 bits");
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toUint176(uint256 value) internal pure returns (uint176) {
require(value <= type(uint176).max, "SafeCast: value doesn't fit in 176 bits");
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toUint168(uint256 value) internal pure returns (uint168) {
require(value <= type(uint168).max, "SafeCast: value doesn't fit in 168 bits");
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toUint160(uint256 value) internal pure returns (uint160) {
require(value <= type(uint160).max, "SafeCast: value doesn't fit in 160 bits");
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toUint152(uint256 value) internal pure returns (uint152) {
require(value <= type(uint152).max, "SafeCast: value doesn't fit in 152 bits");
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toUint144(uint256 value) internal pure returns (uint144) {
require(value <= type(uint144).max, "SafeCast: value doesn't fit in 144 bits");
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toUint136(uint256 value) internal pure returns (uint136) {
require(value <= type(uint136).max, "SafeCast: value doesn't fit in 136 bits");
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v2.5._
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toUint120(uint256 value) internal pure returns (uint120) {
require(value <= type(uint120).max, "SafeCast: value doesn't fit in 120 bits");
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toUint112(uint256 value) internal pure returns (uint112) {
require(value <= type(uint112).max, "SafeCast: value doesn't fit in 112 bits");
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toUint104(uint256 value) internal pure returns (uint104) {
require(value <= type(uint104).max, "SafeCast: value doesn't fit in 104 bits");
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.2._
*/
function toUint96(uint256 value) internal pure returns (uint96) {
require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toUint88(uint256 value) internal pure returns (uint88) {
require(value <= type(uint88).max, "SafeCast: value doesn't fit in 88 bits");
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toUint80(uint256 value) internal pure returns (uint80) {
require(value <= type(uint80).max, "SafeCast: value doesn't fit in 80 bits");
return uint80(value);
}
/**
* @dev Returns the downcasted uint72 from uint256, reverting on
* overflow (when the input is greater than largest uint72).
*
* Counterpart to Solidity's `uint72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toUint72(uint256 value) internal pure returns (uint72) {
require(value <= type(uint72).max, "SafeCast: value doesn't fit in 72 bits");
return uint72(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v2.5._
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toUint56(uint256 value) internal pure returns (uint56) {
require(value <= type(uint56).max, "SafeCast: value doesn't fit in 56 bits");
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toUint48(uint256 value) internal pure returns (uint48) {
require(value <= type(uint48).max, "SafeCast: value doesn't fit in 48 bits");
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toUint40(uint256 value) internal pure returns (uint40) {
require(value <= type(uint40).max, "SafeCast: value doesn't fit in 40 bits");
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v2.5._
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toUint24(uint256 value) internal pure returns (uint24) {
require(value <= type(uint24).max, "SafeCast: value doesn't fit in 24 bits");
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v2.5._
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v2.5._
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*
* _Available since v3.0._
*/
function toUint256(int256 value) internal pure returns (uint256) {
require(value >= 0, "SafeCast: value must be positive");
return uint256(value);
}
/**
* @dev Returns the downcasted int248 from int256, reverting on
* overflow (when the input is less than smallest int248 or
* greater than largest int248).
*
* Counterpart to Solidity's `int248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
require(downcasted == value, "SafeCast: value doesn't fit in 248 bits");
}
/**
* @dev Returns the downcasted int240 from int256, reverting on
* overflow (when the input is less than smallest int240 or
* greater than largest int240).
*
* Counterpart to Solidity's `int240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
require(downcasted == value, "SafeCast: value doesn't fit in 240 bits");
}
/**
* @dev Returns the downcasted int232 from int256, reverting on
* overflow (when the input is less than smallest int232 or
* greater than largest int232).
*
* Counterpart to Solidity's `int232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
require(downcasted == value, "SafeCast: value doesn't fit in 232 bits");
}
/**
* @dev Returns the downcasted int224 from int256, reverting on
* overflow (when the input is less than smallest int224 or
* greater than largest int224).
*
* Counterpart to Solidity's `int224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.7._
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
require(downcasted == value, "SafeCast: value doesn't fit in 224 bits");
}
/**
* @dev Returns the downcasted int216 from int256, reverting on
* overflow (when the input is less than smallest int216 or
* greater than largest int216).
*
* Counterpart to Solidity's `int216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
require(downcasted == value, "SafeCast: value doesn't fit in 216 bits");
}
/**
* @dev Returns the downcasted int208 from int256, reverting on
* overflow (when the input is less than smallest int208 or
* greater than largest int208).
*
* Counterpart to Solidity's `int208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
require(downcasted == value, "SafeCast: value doesn't fit in 208 bits");
}
/**
* @dev Returns the downcasted int200 from int256, reverting on
* overflow (when the input is less than smallest int200 or
* greater than largest int200).
*
* Counterpart to Solidity's `int200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
require(downcasted == value, "SafeCast: value doesn't fit in 200 bits");
}
/**
* @dev Returns the downcasted int192 from int256, reverting on
* overflow (when the input is less than smallest int192 or
* greater than largest int192).
*
* Counterpart to Solidity's `int192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
require(downcasted == value, "SafeCast: value doesn't fit in 192 bits");
}
/**
* @dev Returns the downcasted int184 from int256, reverting on
* overflow (when the input is less than smallest int184 or
* greater than largest int184).
*
* Counterpart to Solidity's `int184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
require(downcasted == value, "SafeCast: value doesn't fit in 184 bits");
}
/**
* @dev Returns the downcasted int176 from int256, reverting on
* overflow (when the input is less than smallest int176 or
* greater than largest int176).
*
* Counterpart to Solidity's `int176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
require(downcasted == value, "SafeCast: value doesn't fit in 176 bits");
}
/**
* @dev Returns the downcasted int168 from int256, reverting on
* overflow (when the input is less than smallest int168 or
* greater than largest int168).
*
* Counterpart to Solidity's `int168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
require(downcasted == value, "SafeCast: value doesn't fit in 168 bits");
}
/**
* @dev Returns the downcasted int160 from int256, reverting on
* overflow (when the input is less than smallest int160 or
* greater than largest int160).
*
* Counterpart to Solidity's `int160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
require(downcasted == value, "SafeCast: value doesn't fit in 160 bits");
}
/**
* @dev Returns the downcasted int152 from int256, reverting on
* overflow (when the input is less than smallest int152 or
* greater than largest int152).
*
* Counterpart to Solidity's `int152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
require(downcasted == value, "SafeCast: value doesn't fit in 152 bits");
}
/**
* @dev Returns the downcasted int144 from int256, reverting on
* overflow (when the input is less than smallest int144 or
* greater than largest int144).
*
* Counterpart to Solidity's `int144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
require(downcasted == value, "SafeCast: value doesn't fit in 144 bits");
}
/**
* @dev Returns the downcasted int136 from int256, reverting on
* overflow (when the input is less than smallest int136 or
* greater than largest int136).
*
* Counterpart to Solidity's `int136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
require(downcasted == value, "SafeCast: value doesn't fit in 136 bits");
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v3.1._
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
require(downcasted == value, "SafeCast: value doesn't fit in 128 bits");
}
/**
* @dev Returns the downcasted int120 from int256, reverting on
* overflow (when the input is less than smallest int120 or
* greater than largest int120).
*
* Counterpart to Solidity's `int120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
require(downcasted == value, "SafeCast: value doesn't fit in 120 bits");
}
/**
* @dev Returns the downcasted int112 from int256, reverting on
* overflow (when the input is less than smallest int112 or
* greater than largest int112).
*
* Counterpart to Solidity's `int112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
require(downcasted == value, "SafeCast: value doesn't fit in 112 bits");
}
/**
* @dev Returns the downcasted int104 from int256, reverting on
* overflow (when the input is less than smallest int104 or
* greater than largest int104).
*
* Counterpart to Solidity's `int104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
require(downcasted == value, "SafeCast: value doesn't fit in 104 bits");
}
/**
* @dev Returns the downcasted int96 from int256, reverting on
* overflow (when the input is less than smallest int96 or
* greater than largest int96).
*
* Counterpart to Solidity's `int96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.7._
*/
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
require(downcasted == value, "SafeCast: value doesn't fit in 96 bits");
}
/**
* @dev Returns the downcasted int88 from int256, reverting on
* overflow (when the input is less than smallest int88 or
* greater than largest int88).
*
* Counterpart to Solidity's `int88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
require(downcasted == value, "SafeCast: value doesn't fit in 88 bits");
}
/**
* @dev Returns the downcasted int80 from int256, reverting on
* overflow (when the input is less than smallest int80 or
* greater than largest int80).
*
* Counterpart to Solidity's `int80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
require(downcasted == value, "SafeCast: value doesn't fit in 80 bits");
}
/**
* @dev Returns the downcasted int72 from int256, reverting on
* overflow (when the input is less than smallest int72 or
* greater than largest int72).
*
* Counterpart to Solidity's `int72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
require(downcasted == value, "SafeCast: value doesn't fit in 72 bits");
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v3.1._
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
require(downcasted == value, "SafeCast: value doesn't fit in 64 bits");
}
/**
* @dev Returns the downcasted int56 from int256, reverting on
* overflow (when the input is less than smallest int56 or
* greater than largest int56).
*
* Counterpart to Solidity's `int56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
require(downcasted == value, "SafeCast: value doesn't fit in 56 bits");
}
/**
* @dev Returns the downcasted int48 from int256, reverting on
* overflow (when the input is less than smallest int48 or
* greater than largest int48).
*
* Counterpart to Solidity's `int48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
require(downcasted == value, "SafeCast: value doesn't fit in 48 bits");
}
/**
* @dev Returns the downcasted int40 from int256, reverting on
* overflow (when the input is less than smallest int40 or
* greater than largest int40).
*
* Counterpart to Solidity's `int40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
require(downcasted == value, "SafeCast: value doesn't fit in 40 bits");
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v3.1._
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
require(downcasted == value, "SafeCast: value doesn't fit in 32 bits");
}
/**
* @dev Returns the downcasted int24 from int256, reverting on
* overflow (when the input is less than smallest int24 or
* greater than largest int24).
*
* Counterpart to Solidity's `int24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
require(downcasted == value, "SafeCast: value doesn't fit in 24 bits");
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v3.1._
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
require(downcasted == value, "SafeCast: value doesn't fit in 16 bits");
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v3.1._
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
require(downcasted == value, "SafeCast: value doesn't fit in 8 bits");
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*
* _Available since v3.0._
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
return int256(value);
}
}
contracts/core/external/compound/ICompLike.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
interface ICompLike is IERC20Upgradeable {
function getCurrentVotes(address account) external view returns (uint96);
function delegate(address delegate) external;
}
contracts/core/interfaces/IControlledToken.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
/** @title IControlledToken
* @author Asymetrix Protocol Inc Team
* @notice ERC20 Tokens with a controller for minting & burning.
*/
interface IControlledToken is IERC20Upgradeable {
/**
@notice Interface to the contract responsible for controlling mint/burn
*/
function controller() external view returns (address);
/**
* @notice Allows the controller to mint tokens for a user account
* @dev May be overridden to provide more granular control over minting
* @param user Address of the receiver of the minted tokens
* @param amount Amount of tokens to mint
*/
function controllerMint(address user, uint256 amount) external;
/**
* @notice Allows the controller to burn tokens from a user account
* @dev May be overridden to provide more granular control over burning
* @param user Address of the holder account to burn tokens from
* @param amount Amount of tokens to burn
*/
function controllerBurn(address user, uint256 amount) external;
/**
* @notice Allows an operator via the controller to burn tokens on behalf of
* a user account
* @dev May be overridden to provide more granular control over
* operator-burning
* @param operator Address of the operator performing the burn action via
* the controller contract
* @param user Address of the holder account to burn tokens from
* @param amount Amount of tokens to burn
*/
function controllerBurnFrom(
address operator,
address user,
uint256 amount
) external;
}
contracts/core/interfaces/IDrawBeacon.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "./IDrawBuffer.sol";
/**
* @title IDrawBeacon
* @author Asymetrix Protocol Inc Team
* @notice The DrawBeacon interface.
*/
interface IDrawBeacon {
/// @notice Draw struct created every draw
/// @param drawId The monotonically increasing drawId for each draw
/// @param timestamp Unix timestamp of the draw. Recorded when the draw is
/// created by the DrawBeacon.
/// @param beaconPeriodStartedAt Unix timestamp of when the draw started
/// @param beaconPeriodSeconds Unix timestamp of the beacon draw period for
/// this draw.
/// @param rngRequestInternalId An internal ID of RNG service request.
/// @param participantsHash A unique hash of participants of the draw.
/// @param picksNumber A number of picks for the draw.
/// @param randomness An array of random numbers for the draw.
/// @param isEmpty A flag that indicates if the draw doesn't have
/// participants.
/// @param paid A flag that indicates if prizes for the draw are paid or not.
struct Draw {
uint32 drawId;
uint64 timestamp;
uint64 beaconPeriodStartedAt;
uint32 beaconPeriodSeconds;
uint32 rngRequestInternalId;
bytes participantsHash;
uint256[] randomness;
uint256 picksNumber;
bool isEmpty;
bool paid;
}
/**
* @notice Emit when a new DrawBuffer has been set.
* @param newDrawBuffer The new DrawBuffer address
*/
event DrawBufferUpdated(IDrawBuffer indexed newDrawBuffer);
/**
* @notice Emit when a draw has opened.
* @param startedAt Start timestamp
*/
event BeaconPeriodStarted(uint64 indexed startedAt);
/**
* @notice Emit when a draw has started.
* @param drawId Draw id
*/
event DrawStarted(uint32 indexed drawId);
/**
* @notice Emit when the drawPeriodSeconds is set.
* @param drawPeriodSeconds Time between draw
*/
event BeaconPeriodSecondsUpdated(uint32 drawPeriodSeconds);
/**
* @notice Returns the number of seconds remaining until the beacon period
* can be complete.
* @return The number of seconds remaining until the beacon period can be
* complete.
*/
function beaconPeriodRemainingSeconds() external view returns (uint64);
/**
* @notice Returns beacon period seconds.
* @return The number of seconds of the beacon period.
*/
function getBeaconPeriodSeconds() external view returns (uint32);
/**
* @notice Returns DrawBuffer contract address.
* @return DrawBuffer contract address.
*/
function getDrawBuffer() external view returns (IDrawBuffer);
/**
* @notice Returns the time when the beacon period started at.
* @return The time when the beacon period started at.
*/
function getBeaconPeriodStartedAt() external view returns (uint64);
/**
* @notice Returns the timestamp at which the beacon period ends.
* @return The timestamp at which the beacon period ends.
*/
function beaconPeriodEndAt() external view returns (uint64);
/**
* @notice Returns the next draw ID.
* @return The ID of the next draw to start.
*/
function getNextDrawId() external view returns (uint32);
/**
* @notice Calculates the next beacon start time, assuming all beacon
* periods have occurred between the last and now.
* @return The next beacon period start time.
*/
function calculateNextBeaconPeriodStartTimeFromCurrentTime()
external
view
returns (uint64);
/**
* @notice Calculates when the next beacon period will start.
* @param time The timestamp to use as the current time.
* @return The timestamp at which the next beacon period would start.
*/
function calculateNextBeaconPeriodStartTime(
uint64 time
) external view returns (uint64);
/**
* @notice Returns whether the beacon period is over.
* @return True if the beacon period is over, false otherwise.
*/
function isBeaconPeriodOver() external view returns (bool);
/**
* @notice Returns whether the draw can start
* @return True if the beacon period is over, false otherwise
*/
function canStartDraw() external view returns (bool);
/**
* @notice Allows the owner to set the beacon period in seconds.
* @param beaconPeriodSeconds The new beacon period in seconds. Must be
* greater than zero.
*/
function setBeaconPeriodSeconds(uint32 beaconPeriodSeconds) external;
/**
* @notice Starts the Draw process. The previous beacon period must have
* ended.
*/
function startDraw() external;
/**
* @notice Set global DrawBuffer variable.
* @dev All subsequent Draw requests/completions will be pushed to the
* new DrawBuffer.
* @param newDrawBuffer DrawBuffer address
* @return DrawBuffer
*/
function setDrawBuffer(
IDrawBuffer newDrawBuffer
) external returns (IDrawBuffer);
}
contracts/core/interfaces/IDrawBuffer.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "./IDrawBeacon.sol";
/**
* @title IDrawBuffer
* @author Asymetrix Protocol Inc Team
* @notice The DrawBuffer interface.
*/
interface IDrawBuffer {
/**
* @notice Emit when a new draw has been created.
* @param drawId Draw id
* @param draw The Draw struct
*/
event DrawSet(uint32 indexed drawId, IDrawBeacon.Draw draw);
/**
* @notice Emit when a new PrizeDistributor contract address is set.
* @param prizeDistributor A new PrizeDistributor contract address
*/
event PrizeDistributorSet(address prizeDistributor);
/**
* @notice Emit when draw is marked is paid.
* @param drawId An ID of a draw that was marked as paid
*/
event DrawMarkedAsPaid(uint32 drawId);
/**
* @notice Read a PrizeDistributor contract address.
* @return A PrizeDistributor contract address.
*/
function getPrizeDistributor() external view returns (address);
/**
* @notice Read a ring buffer cardinality.
* @return Ring buffer cardinality.
*/
function getBufferCardinality() external view returns (uint32);
/**
* @notice Read a Draw from the draws ring buffer.
* @dev Read a Draw using the Draw.drawId to calculate position in the
* draws ring buffer.
* @param drawId Draw.drawId
* @return IDrawBeacon.Draw
*/
function getDraw(
uint32 drawId
) external view returns (IDrawBeacon.Draw memory);
/**
* @notice Read multiple Draws from the draws ring buffer.
* @dev Read multiple Draws using each drawId to calculate position in
* the draws ring buffer.
* @param drawIds Array of drawIds
* @return IDrawBeacon.Draw[] array with the draw information of the
* requested draw ids
*/
function getDraws(
uint32[] calldata drawIds
) external view returns (IDrawBeacon.Draw[] memory);
/**
* @notice Gets the number of Draws held in the draw ring buffer.
* @dev If no Draws have been pushed, it will return 0.
* @dev If the ring buffer is full, it will return the cardinality.
* @dev Otherwise, it will return the NewestDraw index + 1.
* @return Number of Draws held in the draw ring buffer.
*/
function getDrawCount() external view returns (uint32);
/**
* @notice Read newest Draw from draws ring buffer.
* @dev Uses the nextDrawIndex to calculate the most recently added Draw.
* @return IDrawBeacon.Draw
*/
function getNewestDraw() external view returns (IDrawBeacon.Draw memory);
/**
* @notice Read oldest Draw from draws ring buffer.
* @dev Finds the oldest Draw by comparing and/or diffing totalDraws with
* the cardinality.
* @return IDrawBeacon.Draw
*/
function getOldestDraw() external view returns (IDrawBeacon.Draw memory);
/**
* @notice Push Draw onto draws ring buffer history.
* @dev Push new draw onto draws history via authorized manager or owner.
* @param draw IDrawBeacon.Draw
* @return Draw.drawId
*/
function pushDraw(IDrawBeacon.Draw calldata draw) external returns (uint32);
/**
* @notice Set existing Draw in draws ring buffer with new parameters.
* @dev Updating a Draw should be used sparingly and only in the event an
* incorrect Draw parameter has been stored.
* @param newDraw IDrawBeacon.Draw
* @return Draw.drawId
*/
function setDraw(
IDrawBeacon.Draw calldata newDraw
) external returns (uint32);
/**
* @notice Set a new PrizeDistributor contract address.
* @param prizeDistributor A new PrizeDistributor contract address
*/
function setPrizeDistributor(address prizeDistributor) external;
/**
* @notice Mark a draw as paid.
* @dev It means that the winners of the draw were paid and the draw can
* not be paid again.
* @param drawId An ID of a draw that should be marked as paid
*/
function markDrawAsPaid(uint32 drawId) external;
}
contracts/core/interfaces/IPrizePool.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "../external/compound/ICompLike.sol";
import "./IDrawBeacon.sol";
import "./ITicket.sol";
interface IPrizePool {
/**
* @notice The user stake info struct.
* @dev The user stake info struct used to store information about users'
* stakes and rewards.
* @param reward Claimable reward that belongs to a user (with 18 decimals
* accuracy)
* @param former Coefficient that is used to calculate the reward with
* different staked amount (when staked amount changes, with
* 18 decimals accuracy)
* @param lastClaimed Timestamp of last claimed
*/
struct UserStakeInfo {
uint256 reward;
uint256 former;
uint32 lastClaimed;
}
/// @dev Event emitted when controlled token is added
event ControlledTokenAdded(ITicket indexed token);
event AwardCaptured(uint256 amount);
/// @dev Event emitted when assets are deposited
event Deposited(
address indexed operator,
address indexed to,
ITicket indexed token,
uint256 amount
);
/// @dev Event emitted when interest is awarded to a winner
event Awarded(
address indexed winner,
ITicket indexed token,
uint256 amount
);
/// @dev Event emitted when external ERC20s are awarded to a winner
event AwardedExternalERC20(
address indexed winner,
address indexed token,
uint256 amount
);
/// @dev Event emitted when external ERC20s are transferred out
event TransferredExternalERC20(
address indexed to,
address indexed token,
uint256 amount
);
/// @dev Event emitted when external ERC721s are awarded to a winner
event AwardedExternalERC721(
address indexed winner,
address indexed token,
uint256[] tokenIds
);
/// @dev Event emitted when assets are withdrawn
event Withdrawal(
address indexed operator,
address indexed from,
ITicket indexed token,
uint256 amount,
uint256 redeemed,
uint256 fee
);
/// @dev Event emitted when the Balance Cap is set
event BalanceCapSet(uint256 balanceCap);
/// @dev Event emitted when the Liquidity Cap is set
event LiquidityCapSet(uint256 liquidityCap);
/// @dev Event emitted when the Prize Flush is set
event PrizeFlushSet(address indexed prizeFlush);
/// @dev Event emitted when the Ticket is set
event TicketSet(ITicket indexed ticket);
/// @dev Event emitted when the DrawBeacon is set
event DrawBeaconSet(IDrawBeacon indexed drawBeacond);
/// @dev Emitted when there was an error thrown awarding an External ERC721
event ErrorAwardingExternalERC721(bytes error);
/// @dev Event emitted when the Reward Token is set
event RewardTokenSet(IERC20Upgradeable indexed rewardToken);
/// @dev Event emitted when the Reward Per Second is set
event RewardPerSecondSet(uint256 rewardPerSecond);
/// @dev Event emitted when the reward updated
event RewardUpdated(uint64 indexed lastUpdated);
/// @dev Event emitted when the Maximum Claim Interval is set
event MaxClaimIntervalSet(uint32 maxClaimInterval);
/// @dev Event emitted when the Claim Interval is set
event ClaimIntervalSet(uint32 claimInterval);
/// @dev Event emitted when the First Lido Rebase Timestamp is set
event FirstLidoRebaseTimestampSet(uint32 firstLidoRebaseTimestamp);
/// @dev Event emmited when the Lido APR is set
event LidoAPRSet(uint16 lidoAPR);
/// @dev Event emmited when the Free Exit Duration is set
event FreeExitDurationSet(uint32 freeExitDuration);
/// @notice Deposit assets into the Prize Pool in exchange for tokens
/// @param to The address receiving the newly minted tokens
/// @param amount The amount of assets to deposit
function depositTo(address to, uint256 amount) external;
/// @notice Deposit assets into the Prize Pool in exchange for tokens,
/// then sets the delegate on behalf of the caller.
/// @param to The address receiving the newly minted tokens
/// @param amount The amount of assets to deposit
/// @param delegate The address to delegate to for the caller
function depositToAndDelegate(
address to,
uint256 amount,
address delegate
) external;
/// @notice Withdraw assets from the Prize Pool instantly.
/// @param from The address to redeem tokens from.
/// @param amount The amount of tokens to redeem for assets.
/// @return The actual amount withdrawn
function withdrawFrom(
address from,
uint256 amount
) external returns (uint256);
/// @notice Called by the ticket to update user's reward and former in time
/// of transfer.
/// @param _user The user where need to update reward and former.
/// @param _beforeBalance The balance of user before transfer.
/// @param _afterBalance The balance of user after transfer.
function updateUserRewardAndFormer(
address _user,
uint256 _beforeBalance,
uint256 _afterBalance
) external;
/// @notice Claim reward in ASX tokens that distributes on users' deposits.
/// @param _user The user for whom to claim reward.
function claim(address _user) external;
/// @notice Called by the prize flush to award prizes.
/// @dev The amount awarded must be less than the awardBalance()
/// @param to The address of the winner that receives the award
/// @param amount The amount of assets to be awarded
function award(address to, uint256 amount) external;
/// @notice Returns the balance that is available to award.
/// @dev captureAwardBalance() should be called first
/// @return The total amount of assets to be awarded for the current prize
function awardBalance() external view returns (uint256);
/// @notice Captures any available interest as award balance.
/// @dev This function also captures the reserve fees.
/// @return The total amount of assets to be awarded for the current prize
function captureAwardBalance() external returns (uint256);
/// @dev Checks with the Prize Pool if a specific token type may be awarded
/// as an external prize
/// @param externalToken The address of the token to check
/// @return True if the token may be awarded, false otherwise
function canAwardExternal(
address externalToken
) external view returns (bool);
// @dev Returns the total underlying balance of all assets. This includes
/// both principal and interest.
/// @return The underlying balance of assets
function balance() external returns (uint256);
/**
* @notice Read internal Ticket accounted balance.
* @return uint256 accountBalance
*/
function getAccountedBalance() external view returns (uint256);
/**
* @notice Read internal balanceCap variable
*/
function getBalanceCap() external view returns (uint256);
/**
* @notice Read internal liquidityCap variable
*/
function getLiquidityCap() external view returns (uint256);
/**
* @notice Read ticket variable
*/
function getTicket() external view returns (ITicket);
/**
* @notice Read drawBeacond variable
*/
function getDrawBeacon() external view returns (IDrawBeacon);
/**
* @notice Read prize flush variable
*/
function getPrizeFlush() external view returns (address);
/**
* @notice Read rewardToken variable
*/
function getRewardToken() external view returns (IERC20Upgradeable);
/**
* @notice Read token variable
*/
function getToken() external view returns (address);
/**
* @notice Read lastUpdated variable
*/
function getLastUpdated() external view returns (uint64);
/**
* @notice Read rewardPerSecond variable
*/
function getRewardPerSecond() external view returns (uint256);
/**
* @notice Read rewardPerShare variable
*/
function getRewardPerShare() external view returns (uint256);
/**
* @notice Read maxClaimInterval variable
*/
function getMaxClaimInterval() external view returns (uint32);
/**
* @notice Read claimInterval variable
*/
function getClaimInterval() external view returns (uint32);
/**
* @notice Read freeExitDuration variable
*/
function getFreeExitDuration() external view returns (uint32);
/**
* @notice Read deploymentTimestamp variable
*/
function getDeploymentTimestamp() external view returns (uint32);
/**
* @notice Read firstLidoRebaseTimestamp variable
*/
function getFirstLidoRebaseTimestamp() external view returns (uint32);
/**
* @notice Read lidoAPR variable
*/
function getLidoAPR() external view returns (uint16);
/**
* @notice Read userStakeInfo variable
* @param user The address of the user to retrieve stake info about
*/
function getUserStakeInfo(
address user
) external view returns (UserStakeInfo memory);
/**
* @notice Read distributionEnd variable
*/
function getDistributionEnd() external view returns (uint32);
/**
* Calculates actual claimable amount of ASX tokens for the user
*/
function getClaimableReward(address user) external view returns (uint256);
/// @dev Checks if a specific token is controlled by the Prize Pool
/// @param controlledToken The address of the token to check
/// @return True if the token is a controlled token, false otherwise
function isControlled(ITicket controlledToken) external view returns (bool);
/// @notice Called by the Prize-Flush to transfer out external ERC20 tokens
/// @dev Used to transfer out tokens held by the Prize Pool. Could be
/// liquidated, or anything.
/// @param to The address of the winner that receives the award
/// @param externalToken The address of the external asset token being
/// awarded
/// @param amount The amount of external assets to be awarded
function transferExternalERC20(
address to,
address externalToken,
uint256 amount
) external;
/// @notice Called by the Prize-Flush to award external ERC20 prizes
/// @dev Used to award any arbitrary tokens held by the Prize Pool
/// @param to The address of the winner that receives the award
/// @param externalToken The address of the external asset token being
/// awarded
/// @param amount The amount of external assets to be awarded
function awardExternalERC20(
address to,
address externalToken,
uint256 amount
) external;
/// @notice Called by the prize flush to award external ERC721 prizes
/// @dev Used to award any arbitrary NFTs held by the Prize Pool
/// @param to The address of the winner that receives the award
/// @param externalToken The address of the external NFT token being awarded
/// @param tokenIds An array of NFT Token IDs to be transferred
function awardExternalERC721(
address to,
address externalToken,
uint256[] calldata tokenIds
) external;
/// @notice Allows the owner to set a balance cap per `token` for the pool.
/// @dev If a user wins, his balance can go over the cap. He will be able to
/// withdraw the excess but not deposit.
/// @dev Needs to be called after deploying a prize pool to be able to
/// deposit into it.
/// @param balanceCap New balance cap.
/// @return True if new balance cap has been successfully set.
function setBalanceCap(uint256 balanceCap) external returns (bool);
/// @notice Allows the Governor to set a cap on the amount of liquidity that
/// the pool can hold
/// @param liquidityCap The new liquidity cap for the prize pool
function setLiquidityCap(uint256 liquidityCap) external;
/// @notice Sets the reward per second for the prize pool that will be used
/// for ASX tokens distribution. Only callable by the owner.
/// @param rewardPerSecond The new reward per second.
function setRewardPerSecond(uint256 rewardPerSecond) external;
/// @notice Sets the maximum claim interval for the prize pool that will be
/// used in time of claim interval sets. Only callable by the owner.
/// @param maxClaimInterval The new maximum claim interval, in seconds.
function setMaxClaimInterval(uint32 maxClaimInterval) external;
/// @notice Sets the claim interval for the prize pool that will be used in
/// time of claiming of ASX tokens. Only callable by the owner.
/// @param claimInterval The new claim interval, in seconds.
function setClaimInterval(uint32 claimInterval) external;
/// @notice Set prize pool ticket.
/// @param ticket Address of the ticket to set.
/// @return True if ticket has been successfully set.
function setTicket(ITicket ticket) external returns (bool);
/// @notice Set draw beacon.
/// @param drawBeacon DrawBeacon address to set.
function setDrawBeacon(IDrawBeacon drawBeacon) external;
/// @notice Set prize flush.
/// @param prizeFlush PrizeFlush address to set.
function setPrizeFlush(address prizeFlush) external;
/// @notice Set the free exit duration, in seconds. Only callable by the
/// owner.
/// @param freeExitDuration The duration after finishing of a draw when
/// user can leave the protocol without fee
/// charging (in stETH)
function setFreeExitDuration(uint32 freeExitDuration) external;
/// @notice Set APR of the Lido protocol. Only callable by the owner.
/// @param lidoAPR An APR of the Lido protocol.
function setLidoAPR(uint16 lidoAPR) external;
/// @notice Delegate the votes for a Compound COMP-like token held by the
/// prize pool.
/// @param compLike The COMP-like token held by the prize pool that should
/// be delegated.
/// @param to The address to delegate to.
function compLikeDelegate(ICompLike compLike, address to) external;
}
contracts/core/interfaces/IReserve.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
interface IReserve {
/**
* @notice Emit when checkpoint is created.
* @param reserveAccumulated Total depsosited
* @param withdrawAccumulated Total withdrawn
*/
event Checkpoint(uint256 reserveAccumulated, uint256 withdrawAccumulated);
/**
* @notice Emit when the withdrawTo function has executed.
* @param recipient Address receiving funds
* @param amount Amount of tokens transfered.
*/
event Withdrawn(address indexed recipient, uint256 amount);
/**
* @notice Create observation checkpoint in ring bufferr.
* @dev Calculates total desposited tokens since last checkpoint and
* creates new accumulator checkpoint.
*/
function checkpoint() external;
/**
* @notice Read global token value. It is the token that the reserve will
* accept.
* @return IERC20Upgradeable
*/
function getToken() external view returns (IERC20Upgradeable);
/**
* @notice Calculate token accumulation beween timestamp range.
* @dev Search the ring buffer for two checkpoint observations and diffs
* accumulator amount.
* @param startTimestamp Account address
* @param endTimestamp Transfer amount
* @return accumulated between input amount
*/
function getReserveAccumulatedBetween(
uint32 startTimestamp,
uint32 endTimestamp
) external returns (uint224);
/**
* @notice Transfer Reserve token balance to recipient address.
* @dev Creates checkpoint before token transfer. Increments
* withdrawAccumulator with amount.
* @param recipient Account address
* @param amount Transfer amount
*/
function withdrawTo(address recipient, uint256 amount) external;
}
contracts/core/interfaces/ITicket.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "../libraries/TwabLib.sol";
import "./IControlledToken.sol";
interface ITicket is IControlledToken {
/**
* @notice A struct containing details for an Account.
* @param balance The current balance for an Account.
* @param nextTwabIndex The next available index to store a new twab.
* @param cardinality The number of recorded twabs (plus one!).
*/
struct AccountDetails {
uint224 balance;
uint16 nextTwabIndex;
uint16 cardinality;
}
/**
* @notice Combines account details with their twab history.
* @param details The account details.
* @param twabs The history of twabs for this account.
*/
struct Account {
AccountDetails details;
ObservationLib.Observation[65535] twabs;
}
/**
* @notice Emitted when TWAB balance has been delegated to another user.
* @param delegator Address of the delegator.
* @param delegate Address of the delegate.
*/
event Delegated(address indexed delegator, address indexed delegate);
/**
* @notice Emitted when ticket is initialized.
* @param name Ticket name (eg: Asymetrix Protocol Dai Ticket (Compound)).
* @param symbol Ticket symbol (eg: AsxDAI).
* @param decimals Ticket decimals.
* @param controller Token controller address.
*/
event TicketInitialized(
string name,
string symbol,
uint8 decimals,
address indexed controller
);
/**
* @notice Emitted when a new TWAB has been recorded.
* @param delegate The recipient of the ticket power (may be the same as the
* user).
* @param newTwab Updated TWAB of a ticket holder after a successful TWAB
* recording.
*/
event NewUserTwab(
address indexed delegate,
ObservationLib.Observation newTwab
);
/**
* @notice Emitted when a new total supply TWAB has been recorded.
* @param newTotalSupplyTwab Updated TWAB of tickets total supply after a
* successful total supply TWAB recording.
*/
event NewTotalSupplyTwab(ObservationLib.Observation newTotalSupplyTwab);
/**
* @notice Retrieves the address of the delegate to whom `user` has
* delegated their tickets.
* @dev Address of the delegate will be the zero address if `user` has not
* delegated their tickets.
* @param user Address of the delegator.
* @return Address of the delegate.
*/
function delegateOf(address user) external view returns (address);
/**
* @notice Delegate time-weighted average balances to an alternative address.
* @dev Transfers (including mints) trigger the storage of a TWAB in
* delegate(s) account, instead of the targetted sender and/or
* recipient address(s).
* @dev To reset the delegate, pass the zero address (0x000.000) as `to`
* parameter.
* @dev Current delegate address should be different from the new
* delegate address `to`.
* @param to Recipient of delegated TWAB.
*/
function delegate(address to) external;
/**
* @notice Allows the controller to delegate on a users behalf.
* @param user The user for whom to delegate
* @param delegate The new delegate
*/
function controllerDelegateFor(address user, address delegate) external;
/**
* @notice Allows a user to delegate via signature
* @param user The user who is delegating
* @param delegate The new delegate
* @param deadline The timestamp by which this must be submitted
* @param v The v portion of the ECDSA sig
* @param r The r portion of the ECDSA sig
* @param s The s portion of the ECDSA sig
*/
function delegateWithSignature(
address user,
address delegate,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @notice Gets a users twab context. This is a struct with their balance,
* next twab index, and cardinality.
* @param user The user for whom to fetch the TWAB context.
* @return The TWAB context, which includes { balance, nextTwabIndex,
* cardinality }
*/
function getAccountDetails(
address user
) external view returns (TwabLib.AccountDetails memory);
/**
* @notice Gets the TWAB at a specific index for a user.
* @param user The user for whom to fetch the TWAB.
* @param index The index of the TWAB to fetch.
* @return The TWAB, which includes the twab amount and the timestamp.
*/
function getTwab(
address user,
uint16 index
) external view returns (ObservationLib.Observation memory);
/**
* @notice Retrieves `user` TWAB balance.
* @param user Address of the user whose TWAB is being fetched.
* @param timestamp Timestamp at which we want to retrieve the TWAB balance.
* @return The TWAB balance at the given timestamp.
*/
function getBalanceAt(
address user,
uint64 timestamp
) external view returns (uint256);
/**
* @notice Retrieves `user` TWAB balances.
* @param user Address of the user whose TWABs are being fetched.
* @param timestamps Timestamps range at which we want to retrieve the
* TWAB balances.
* @return `user` TWAB balances.
*/
function getBalancesAt(
address user,
uint64[] calldata timestamps
) external view returns (uint256[] memory);
/**
* @notice Retrieves the average balance held by a user for a given time
* frame.
* @param user The user whose balance is checked.
* @param startTime The start time of the time frame.
* @param endTime The end time of the time frame.
* @return The average balance that the user held during the time frame.
*/
function getAverageBalanceBetween(
address user,
uint64 startTime,
uint64 endTime
) external view returns (uint256);
/**
* @notice Retrieves the average balances held by a user for a given time
* frame.
* @param user The user whose balance is checked.
* @param startTimes The start time of the time frame.
* @param endTimes The end time of the time frame.
* @return The average balance that the user held during the time frame.
*/
function getAverageBalancesBetween(
address user,
uint64[] calldata startTimes,
uint64[] calldata endTimes
) external view returns (uint256[] memory);
/**
* @notice Retrieves the total supply TWAB balance at the given timestamp.
* @param timestamp Timestamp at which we want to retrieve the total supply
* TWAB balance.
* @return The total supply TWAB balance at the given timestamp.
*/
function getTotalSupplyAt(uint64 timestamp) external view returns (uint256);
/**
* @notice Retrieves the total supply TWAB balance between the given
* timestamps range.
* @param timestamps Timestamps range at which we want to retrieve the total
* supply TWAB balance.
* @return Total supply TWAB balances.
*/
function getTotalSuppliesAt(
uint64[] calldata timestamps
) external view returns (uint256[] memory);
/**
* @notice Retrieves the average total supply balance for a set of given
* time frames.
* @param startTimes Array of start times.
* @param endTimes Array of end times.
* @return The average total supplies held during the time frame.
*/
function getAverageTotalSuppliesBetween(
uint64[] calldata startTimes,
uint64[] calldata endTimes
) external view returns (uint256[] memory);
}
contracts/core/libraries/ExtendedSafeCastLib.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
/**
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added
* overflow checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This
* can easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller
* types, by performing all math on `uint256` and `int256` and then downcasting.
*/
library ExtendedSafeCastLib {
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toUint104(uint256 _value) internal pure returns (uint104) {
require(
_value <= type(uint104).max,
"SafeCast: value doesn't fit in 104 bits"
);
return uint104(_value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toUint208(uint256 _value) internal pure returns (uint208) {
require(
_value <= type(uint208).max,
"SafeCast: value doesn't fit in 208 bits"
);
return uint208(_value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toUint224(uint256 _value) internal pure returns (uint224) {
require(
_value <= type(uint224).max,
"SafeCast: value doesn't fit in 224 bits"
);
return uint224(_value);
}
}
contracts/core/libraries/ObservationLib.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "@openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol";
import "./OverflowSafeComparatorLib.sol";
import "./RingBufferLib.sol";
/**
* @title Observation Library
* @notice This library allows one to store an array of timestamped values and
* efficiently binary search them.
* @dev Largely pulled from Uniswap V3 Oracle.sol: https://github.com/Uniswap/v3-core/blob/c05a0e2c8c08c460fb4d05cfdda30b3ad8deeaac/contracts/libraries/Oracle.sol
* @author Asymetrix Protocol Inc.
*/
library ObservationLib {
using OverflowSafeComparatorLib for uint32;
using SafeCastUpgradeable for uint256;
/// @notice The maximum number of observations
uint24 public constant MAX_CARDINALITY = 16777215; // 2**24
/**
* @notice Observation, which includes an amount and timestamp.
* @param amount `amount` at `timestamp`.
* @param timestamp Recorded `timestamp`.
*/
struct Observation {
uint224 amount;
uint32 timestamp;
}
/**
* @notice Fetches Observations `beforeOrAt` and `atOrAfter` a `_target`, eg: where [`beforeOrAt`, `atOrAfter`] is satisfied.
* The result may be the same Observation, or adjacent Observations.
* @dev The answer must be contained in the array used when the target is located within the stored Observation.
* boundaries: older than the most recent Observation and younger, or the same age as, the oldest Observation.
* @dev If `_newestObservationIndex` is less than `_oldestObservationIndex`, it means that we've wrapped around the circular buffer.
* So the most recent observation will be at `_oldestObservationIndex + _cardinality - 1`, at the beginning of the circular buffer.
* @param _observations List of Observations to search through.
* @param _newestObservationIndex Index of the newest Observation. Right side of the circular buffer.
* @param _oldestObservationIndex Index of the oldest Observation. Left side of the circular buffer.
* @param _target Timestamp at which we are searching the Observation.
* @param _cardinality Cardinality of the circular buffer we are searching through.
* @param _time Timestamp at which we perform the binary search.
* @return beforeOrAt Observation recorded before, or at, the target.
* @return atOrAfter Observation recorded at, or after, the target.
*/
function binarySearch(
Observation[MAX_CARDINALITY] storage _observations,
uint24 _newestObservationIndex,
uint24 _oldestObservationIndex,
uint32 _target,
uint24 _cardinality,
uint32 _time
)
internal
view
returns (Observation memory beforeOrAt, Observation memory atOrAfter)
{
uint256 leftSide = _oldestObservationIndex;
uint256 rightSide = _newestObservationIndex < leftSide
? leftSide + _cardinality - 1
: _newestObservationIndex;
uint256 currentIndex;
while (true) {
// We start our search in the middle of the `leftSide` and `rightSide`.
// After each iteration, we narrow down the search to the left or the right side while still starting our search in the middle.
currentIndex = (leftSide + rightSide) / 2;
beforeOrAt = _observations[
uint24(RingBufferLib.wrap(currentIndex, _cardinality))
];
uint32 beforeOrAtTimestamp = beforeOrAt.timestamp;
// We've landed on an uninitialized timestamp, keep searching higher (more recently).
if (beforeOrAtTimestamp == 0) {
leftSide = currentIndex + 1;
continue;
}
atOrAfter = _observations[
uint24(RingBufferLib.nextIndex(currentIndex, _cardinality))
];
bool targetAtOrAfter = beforeOrAtTimestamp.lte(_target, _time);
// Check if we've found the corresponding Observation.
if (targetAtOrAfter && _target.lte(atOrAfter.timestamp, _time)) {
break;
}
// If `beforeOrAtTimestamp` is greater than `_target`, then we keep searching lower. To the left of the current index.
if (!targetAtOrAfter) {
rightSide = currentIndex - 1;
} else {
// Otherwise, we keep searching higher. To the left of the current index.
leftSide = currentIndex + 1;
}
}
}
}
contracts/core/libraries/OverflowSafeComparatorLib.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
/// @title OverflowSafeComparatorLib library to share comparator functions between contracts
/// @dev Code taken from Uniswap V3 Oracle.sol: https://github.com/Uniswap/v3-core/blob/3e88af408132fc957e3e406f65a0ce2b1ca06c3d/contracts/libraries/Oracle.sol
/// @author Asymetrix Protocol Inc.
library OverflowSafeComparatorLib {
/// @notice 32-bit timestamps comparator.
/// @dev safe for 0 or 1 overflows, `_a` and `_b` must be chronologically before or equal to time.
/// @param _a A comparison timestamp from which to determine the relative position of `_timestamp`.
/// @param _b Timestamp to compare against `_a`.
/// @param _timestamp A timestamp truncated to 32 bits.
/// @return bool Whether `_a` is chronologically < `_b`.
function lt(
uint32 _a,
uint32 _b,
uint32 _timestamp
) internal pure returns (bool) {
// No need to adjust if there hasn't been an overflow
if (_a <= _timestamp && _b <= _timestamp) return _a < _b;
uint256 aAdjusted = _a > _timestamp ? _a : _a + 2 ** 32;
uint256 bAdjusted = _b > _timestamp ? _b : _b + 2 ** 32;
return aAdjusted < bAdjusted;
}
/// @notice 32-bit timestamps comparator.
/// @dev safe for 0 or 1 overflows, `_a` and `_b` must be chronologically before or equal to time.
/// @param _a A comparison timestamp from which to determine the relative position of `_timestamp`.
/// @param _b Timestamp to compare against `_a`.
/// @param _timestamp A timestamp truncated to 32 bits.
/// @return bool Whether `_a` is chronologically <= `_b`.
function lte(
uint32 _a,
uint32 _b,
uint32 _timestamp
) internal pure returns (bool) {
// No need to adjust if there hasn't been an overflow
if (_a <= _timestamp && _b <= _timestamp) return _a <= _b;
uint256 aAdjusted = _a > _timestamp ? _a : _a + 2 ** 32;
uint256 bAdjusted = _b > _timestamp ? _b : _b + 2 ** 32;
return aAdjusted <= bAdjusted;
}
/// @notice 32-bit timestamp subtractor
/// @dev safe for 0 or 1 overflows, where `_a` and `_b` must be chronologically before or equal to time
/// @param _a The subtraction left operand
/// @param _b The subtraction right operand
/// @param _timestamp The current time. Expected to be chronologically after both.
/// @return The difference between a and b, adjusted for overflow
function checkedSub(
uint32 _a,
uint32 _b,
uint32 _timestamp
) internal pure returns (uint32) {
// No need to adjust if there hasn't been an overflow
if (_a <= _timestamp && _b <= _timestamp) return _a - _b;
uint256 aAdjusted = _a > _timestamp ? _a : _a + 2 ** 32;
uint256 bAdjusted = _b > _timestamp ? _b : _b + 2 ** 32;
return uint32(aAdjusted - bAdjusted);
}
}
contracts/core/libraries/RingBufferLib.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
library RingBufferLib {
/**
* @notice Returns wrapped TWAB index.
* @dev In order to navigate the TWAB circular buffer, we need to use the modulo operator.
* @dev For example, if `_index` is equal to 32 and the TWAB circular buffer is of `_cardinality` 32,
* it will return 0 and will point to the first element of the array.
* @param _index Index used to navigate through the TWAB circular buffer.
* @param _cardinality TWAB buffer cardinality.
* @return TWAB index.
*/
function wrap(
uint256 _index,
uint256 _cardinality
) internal pure returns (uint256) {
return _index % _cardinality;
}
/**
* @notice Computes the negative offset from the given index, wrapped by the cardinality.
* @dev We add `_cardinality` to `_index` to be able to offset even if `_amount` is superior to `_cardinality`.
* @param _index The index from which to offset
* @param _amount The number of indices to offset. This is subtracted from the given index.
* @param _cardinality The number of elements in the ring buffer
* @return Offsetted index.
*/
function offset(
uint256 _index,
uint256 _amount,
uint256 _cardinality
) internal pure returns (uint256) {
return wrap(_index + _cardinality - _amount, _cardinality);
}
/// @notice Returns the index of the last recorded TWAB
/// @param _nextIndex The next available twab index. This will be recorded to next.
/// @param _cardinality The cardinality of the TWAB history.
/// @return The index of the last recorded TWAB
function newestIndex(
uint256 _nextIndex,
uint256 _cardinality
) internal pure returns (uint256) {
if (_cardinality == 0) {
return 0;
}
return wrap(_nextIndex + _cardinality - 1, _cardinality);
}
/// @notice Computes the ring buffer index that follows the given one, wrapped by cardinality
/// @param _index The index to increment
/// @param _cardinality The number of elements in the Ring Buffer
/// @return The next index relative to the given index. Will wrap around to 0 if the next index == cardinality
function nextIndex(
uint256 _index,
uint256 _cardinality
) internal pure returns (uint256) {
return wrap(_index + 1, _cardinality);
}
}
contracts/core/libraries/TwabLib.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "./ExtendedSafeCastLib.sol";
import "./OverflowSafeComparatorLib.sol";
import "./RingBufferLib.sol";
import "./ObservationLib.sol";
/**
* @title Asymetrix Protocol V1 TwabLib (Library)
* @author Asymetrix Protocol Inc Team
* @dev Time-Weighted Average Balance Library for ERC20 tokens.
* @notice This TwabLib adds on-chain historical lookups to a user(s) time-weighted average balance.
Each user is mapped to an Account struct containing the TWAB history (ring buffer) and
ring buffer parameters. Every token.transfer() creates a new TWAB checkpoint. The new TWAB
checkpoint is stored in the circular ring buffer, as either a new checkpoint or rewriting
a previous checkpoint with new parameters. The TwabLib (using existing blocktimes of 1block/15sec)
guarantees minimum 7.4 years of search history.
*/
library TwabLib {
using OverflowSafeComparatorLib for uint32;
using ExtendedSafeCastLib for uint256;
/**
* @notice Sets max ring buffer length in the Account.twabs Observation list.
As users transfer/mint/burn tickets new Observation checkpoints are
recorded. The current max cardinality guarantees a seven year minimum,
of accurate historical lookups with current estimates of 1 new block
every 15 seconds - assuming each block contains a transfer to trigger an
observation write to storage.
* @dev The user Account.AccountDetails.cardinality parameter can NOT exceed
the max cardinality variable. Preventing "corrupted" ring buffer lookup
pointers and new observation checkpoints.
The MAX_CARDINALITY in fact guarantees at least 7.4 years of records:
If 14 = block time in seconds
(2**24) * 14 = 234881024 seconds of history
234881024 / (365 * 24 * 60 * 60) ~= 7.44 years
*/
uint24 public constant MAX_CARDINALITY = 16777215; // 2**24
/** @notice Struct ring buffer parameters for single user Account
* @param balance Current balance for an Account
* @param nextTwabIndex Next uninitialized or updatable ring buffer checkpoint storage slot
* @param cardinality Current total "initialized" ring buffer checkpoints for single user AccountDetails.
Used to set initial boundary conditions for an efficient binary search.
*/
struct AccountDetails {
uint208 balance;
uint24 nextTwabIndex;
uint24 cardinality;
}
/// @notice Combines account details with their twab history
/// @param details The account details
/// @param twabs The history of twabs for this account
struct Account {
AccountDetails details;
ObservationLib.Observation[MAX_CARDINALITY] twabs;
}
/// @notice Increases an account's balance and records a new twab.
/// @param _account The account whose balance will be increased
/// @param _amount The amount to increase the balance by
/// @param _currentTime The current time
/// @return accountDetails The new AccountDetails
/// @return twab The user's latest TWAB
/// @return isNew Whether the TWAB is new
function increaseBalance(
Account storage _account,
uint208 _amount,
uint32 _currentTime
)
internal
returns (
AccountDetails memory accountDetails,
ObservationLib.Observation memory twab,
bool isNew
)
{
AccountDetails memory _accountDetails = _account.details;
(accountDetails, twab, isNew) = _nextTwab(
_account.twabs,
_accountDetails,
_currentTime
);
accountDetails.balance = _accountDetails.balance + _amount;
}
/** @notice Calculates the next TWAB checkpoint for an account with a decreasing balance.
* @dev With Account struct and amount decreasing calculates the next TWAB observable checkpoint.
* @param _account Account whose balance will be decreased
* @param _amount Amount to decrease the balance by
* @param _revertMessage Revert message for insufficient balance
* @return accountDetails Updated Account.details struct
* @return twab TWAB observation (with decreasing average)
* @return isNew Whether TWAB is new or calling twice in the same block
*/
function decreaseBalance(
Account storage _account,
uint208 _amount,
string memory _revertMessage,
uint32 _currentTime
)
internal
returns (
AccountDetails memory accountDetails,
ObservationLib.Observation memory twab,
bool isNew
)
{
AccountDetails memory _accountDetails = _account.details;
require(_accountDetails.balance >= _amount, _revertMessage);
(accountDetails, twab, isNew) = _nextTwab(
_account.twabs,
_accountDetails,
_currentTime
);
unchecked {
accountDetails.balance -= _amount;
}
}
/** @notice Calculates the average balance held by a user for a given time frame.
* @dev Finds the average balance between start and end timestamp epochs.
Validates the supplied end time is within the range of elapsed time i.e. less then timestamp of now.
* @param _twabs Individual user Observation recorded checkpoints passed as storage pointer
* @param _accountDetails User AccountDetails struct loaded in memory
* @param _startTime Start of timestamp range as an epoch
* @param _endTime End of timestamp range as an epoch
* @param _currentTime Block.timestamp
* @return Average balance of user held between epoch timestamps start and end
*/
function getAverageBalanceBetween(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails,
uint32 _startTime,
uint32 _endTime,
uint32 _currentTime
) internal view returns (uint256) {
uint32 endTime = _endTime > _currentTime ? _currentTime : _endTime;
return
_getAverageBalanceBetween(
_twabs,
_accountDetails,
_startTime,
endTime,
_currentTime
);
}
/// @notice Retrieves the oldest TWAB
/// @param _twabs The storage array of twabs
/// @param _accountDetails The TWAB account details
/// @return index The index of the oldest TWAB in the twabs array
/// @return twab The oldest TWAB
function oldestTwab(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails
)
internal
view
returns (uint24 index, ObservationLib.Observation memory twab)
{
index = _accountDetails.nextTwabIndex;
twab = _twabs[index];
// If the TWAB is not initialized we go to the beginning of the TWAB circular buffer at index 0
if (twab.timestamp == 0) {
index = 0;
twab = _twabs[0];
}
}
/// @notice Retrieves the newest TWAB
/// @param _twabs The storage array of twabs
/// @param _accountDetails The TWAB account details
/// @return index The index of the newest TWAB in the twabs array
/// @return twab The newest TWAB
function newestTwab(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails
)
internal
view
returns (uint24 index, ObservationLib.Observation memory twab)
{
index = uint24(
RingBufferLib.newestIndex(
_accountDetails.nextTwabIndex,
MAX_CARDINALITY
)
);
twab = _twabs[index];
}
/// @notice Retrieves amount at `_targetTime` timestamp
/// @param _twabs List of TWABs to search through.
/// @param _accountDetails Accounts details
/// @param _targetTime Timestamp at which the reserved TWAB should be for.
/// @return uint256 TWAB amount at `_targetTime`.
function getBalanceAt(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails,
uint32 _targetTime,
uint32 _currentTime
) internal view returns (uint256) {
uint32 timeToTarget = _targetTime > _currentTime
? _currentTime
: _targetTime;
return
_getBalanceAt(_twabs, _accountDetails, timeToTarget, _currentTime);
}
/// @notice Calculates the average balance held by a user for a given time frame.
/// @param _startTime The start time of the time frame.
/// @param _endTime The end time of the time frame.
/// @return The average balance that the user held during the time frame.
function _getAverageBalanceBetween(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails,
uint32 _startTime,
uint32 _endTime,
uint32 _currentTime
) private view returns (uint256) {
(
uint24 oldestTwabIndex,
ObservationLib.Observation memory oldTwab
) = oldestTwab(_twabs, _accountDetails);
(
uint24 newestTwabIndex,
ObservationLib.Observation memory newTwab
) = newestTwab(_twabs, _accountDetails);
ObservationLib.Observation memory startTwab = _calculateTwab(
_twabs,
_accountDetails,
newTwab,
oldTwab,
newestTwabIndex,
oldestTwabIndex,
_startTime,
_currentTime
);
ObservationLib.Observation memory endTwab = _calculateTwab(
_twabs,
_accountDetails,
newTwab,
oldTwab,
newestTwabIndex,
oldestTwabIndex,
_endTime,
_currentTime
);
// Difference in amount / time
return
(endTwab.amount - startTwab.amount) /
OverflowSafeComparatorLib.checkedSub(
endTwab.timestamp,
startTwab.timestamp,
_currentTime
);
}
/** @notice Searches TWAB history and calculate the difference between amount(s)/timestamp(s) to return average balance
between the Observations closes to the supplied targetTime.
* @param _twabs Individual user Observation recorded checkpoints passed as storage pointer
* @param _accountDetails User AccountDetails struct loaded in memory
* @param _targetTime Target timestamp to filter Observations in the ring buffer binary search
* @param _currentTime Block.timestamp
* @return uint256 Time-weighted average amount between two closest observations.
*/
function _getBalanceAt(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails,
uint32 _targetTime,
uint32 _currentTime
) private view returns (uint256) {
uint24 newestTwabIndex;
ObservationLib.Observation memory afterOrAt;
ObservationLib.Observation memory beforeOrAt;
(newestTwabIndex, beforeOrAt) = newestTwab(_twabs, _accountDetails);
// If `_targetTime` is chronologically after the newest TWAB, we can simply return the current balance
if (beforeOrAt.timestamp.lte(_targetTime, _currentTime)) {
return _accountDetails.balance;
}
uint24 oldestTwabIndex;
// Now, set before to the oldest TWAB
(oldestTwabIndex, beforeOrAt) = oldestTwab(_twabs, _accountDetails);
// If `_targetTime` is chronologically before the oldest TWAB, we can early return
if (_targetTime.lt(beforeOrAt.timestamp, _currentTime)) {
return 0;
}
// Otherwise, we perform the `binarySearch`
(beforeOrAt, afterOrAt) = ObservationLib.binarySearch(
_twabs,
newestTwabIndex,
oldestTwabIndex,
_targetTime,
_accountDetails.cardinality,
_currentTime
);
// Sum the difference in amounts and divide by the difference in timestamps.
// The time-weighted average balance uses time measured between two epoch timestamps as
// a constaint on the measurement when calculating the time weighted average balance.
return
(afterOrAt.amount - beforeOrAt.amount) /
OverflowSafeComparatorLib.checkedSub(
afterOrAt.timestamp,
beforeOrAt.timestamp,
_currentTime
);
}
/** @notice Calculates a user TWAB for a target timestamp using the historical TWAB records.
The balance is linearly interpolated: amount differences / timestamp differences
using the simple (after.amount - before.amount / end.timestamp - start.timestamp) formula.
/** @dev Binary search in _calculateTwab fails when searching out of bounds. Thus, before
searching we exclude target timestamps out of range of newest/oldest TWAB(s).
IF a search is before or after the range we "extrapolate" a Observation from the expected state.
* @param _twabs Individual user Observation recorded checkpoints passed as storage pointer
* @param _accountDetails User AccountDetails struct loaded in memory
* @param _newestTwab Newest TWAB in history (end of ring buffer)
* @param _oldestTwab Olderst TWAB in history (end of ring buffer)
* @param _newestTwabIndex Pointer in ring buffer to newest TWAB
* @param _oldestTwabIndex Pointer in ring buffer to oldest TWAB
* @param _targetTimestamp Epoch timestamp to calculate for time (T) in the TWAB
* @param _time Block.timestamp
* @return accountDetails Updated Account.details struct
*/
function _calculateTwab(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails,
ObservationLib.Observation memory _newestTwab,
ObservationLib.Observation memory _oldestTwab,
uint24 _newestTwabIndex,
uint24 _oldestTwabIndex,
uint32 _targetTimestamp,
uint32 _time
) private view returns (ObservationLib.Observation memory) {
// If `_targetTimestamp` is chronologically after the newest TWAB, we extrapolate a new one
if (_newestTwab.timestamp.lt(_targetTimestamp, _time)) {
return
_computeNextTwab(
_newestTwab,
_accountDetails.balance,
_targetTimestamp
);
}
if (_newestTwab.timestamp == _targetTimestamp) {
return _newestTwab;
}
if (_oldestTwab.timestamp == _targetTimestamp) {
return _oldestTwab;
}
// If `_targetTimestamp` is chronologically before the oldest TWAB, we create a zero twab
if (_targetTimestamp.lt(_oldestTwab.timestamp, _time)) {
return
ObservationLib.Observation({
amount: 0,
timestamp: _targetTimestamp
});
}
// Otherwise, both timestamps must be surrounded by twabs.
(
ObservationLib.Observation memory beforeOrAtStart,
ObservationLib.Observation memory afterOrAtStart
) = ObservationLib.binarySearch(
_twabs,
_newestTwabIndex,
_oldestTwabIndex,
_targetTimestamp,
_accountDetails.cardinality,
_time
);
uint224 heldBalance = (afterOrAtStart.amount - beforeOrAtStart.amount) /
OverflowSafeComparatorLib.checkedSub(
afterOrAtStart.timestamp,
beforeOrAtStart.timestamp,
_time
);
return _computeNextTwab(beforeOrAtStart, heldBalance, _targetTimestamp);
}
/**
* @notice Calculates the next TWAB using the newestTwab and updated balance.
* @dev Storage of the TWAB obersation is managed by the calling function and not _computeNextTwab.
* @param _currentTwab Newest Observation in the Account.twabs list
* @param _currentBalance User balance at time of most recent (newest) checkpoint write
* @param _time Current block.timestamp
* @return TWAB Observation
*/
function _computeNextTwab(
ObservationLib.Observation memory _currentTwab,
uint224 _currentBalance,
uint32 _time
) private pure returns (ObservationLib.Observation memory) {
// New twab amount = last twab amount (or zero) + (current amount * elapsed seconds)
return
ObservationLib.Observation({
amount: _currentTwab.amount +
_currentBalance *
(_time.checkedSub(_currentTwab.timestamp, _time)),
timestamp: _time
});
}
/// @notice Sets a new TWAB Observation at the next available index and returns the new account details.
/// @dev Note that if _currentTime is before the last observation timestamp, it appears as an overflow
/// @param _twabs The twabs array to insert into
/// @param _accountDetails The current account details
/// @param _currentTime The current time
/// @return accountDetails The new account details
/// @return twab The newest twab (may or may not be brand-new)
/// @return isNew Whether the newest twab was created by this call
function _nextTwab(
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails,
uint32 _currentTime
)
private
returns (
AccountDetails memory accountDetails,
ObservationLib.Observation memory twab,
bool isNew
)
{
(, ObservationLib.Observation memory _newestTwab) = newestTwab(
_twabs,
_accountDetails
);
// if we're in the same block, return
if (_newestTwab.timestamp == _currentTime) {
return (_accountDetails, _newestTwab, false);
}
ObservationLib.Observation memory newTwab = _computeNextTwab(
_newestTwab,
_accountDetails.balance,
_currentTime
);
_twabs[_accountDetails.nextTwabIndex] = newTwab;
AccountDetails memory nextAccountDetails = push(_accountDetails);
return (nextAccountDetails, newTwab, true);
}
/// @notice "Pushes" a new element on the AccountDetails ring buffer, and returns the new AccountDetails
/// @param _accountDetails The account details from which to pull the cardinality and next index
/// @return The new AccountDetails
function push(
AccountDetails memory _accountDetails
) internal pure returns (AccountDetails memory) {
_accountDetails.nextTwabIndex = uint24(
RingBufferLib.nextIndex(
_accountDetails.nextTwabIndex,
MAX_CARDINALITY
)
);
// Prevent the Account specific cardinality from exceeding the MAX_CARDINALITY.
// The ring buffer length is limited by MAX_CARDINALITY. IF the account.cardinality
// exceeds the max cardinality, new observations would be incorrectly set or the
// observation would be out of "bounds" of the ring buffer. Once reached the
// AccountDetails.cardinality will continue to be equal to max cardinality.
if (_accountDetails.cardinality < MAX_CARDINALITY) {
_accountDetails.cardinality += 1;
}
return _accountDetails;
}
}
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/periphery/interfaces/IPrizeFlush.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
import "../../core/interfaces/IControlledToken.sol";
import "../../core/interfaces/IPrizePool.sol";
import "../../core/interfaces/IReserve.sol";
interface IPrizeFlush {
/**
* @notice Emit when the flush function has executed.
* @param destination Address receiving funds
* @param amount Amount of tokens transferred
*/
event Flushed(address indexed destination, uint256 amount);
/**
* @notice Emit when destination is set.
* @param destination Destination address
*/
event DestinationSet(address destination);
/**
* @notice Emit when capturing the award amount from PrizePool.
* @param totalPrizeCaptured Total prize captured from the PrizePool
*/
event Distributed(uint256 totalPrizeCaptured);
/**
* @notice Emit when an individual prize split is awarded.
* @param user User address being awarded
* @param prizeAwarded Awarded prize amount
* @param token Token address
*/
event PrizeAwarded(
address indexed user,
uint256 prizeAwarded,
IControlledToken indexed token
);
/**
* @notice Emit when reserve is set.
* @param reserve Reserve address
*/
event ReserveSet(IReserve reserve);
/**
* @notice Emit when prizePool is set.
* @param prizePool PrizePool address
*/
event PrizePoolSet(IPrizePool prizePool);
/**
* @notice Emit when protocolFeeRecipient is set.
* @param protocolFeeRecipient ProtocolFeeRecipient address
*/
event ProtocolFeeRecipientSet(address protocolFeeRecipient);
/**
* @notice Emit when protocolFeePercentage is set.
* @param protocolFeePercentage ProtocolFeePercentage number
*/
event ProtocolPercentageSet(uint16 protocolFeePercentage);
/// @notice Read global destination variable. It shows where the funds will
/// be moved at the end.
function getDestination() external view returns (address);
/// @notice Read global reserve variable. Contract that tracks funds earned
/// over time periods.
function getReserve() external view returns (IReserve);
/// @notice Read global prizePool variable. Pool from where to capture the
/// award balance.
function getPrizePool() external view returns (IPrizePool);
/// @notice Read global ProtocolFeeRecipient variable. Address that will
/// receive the protocolFee.
function getProtocolFeeRecipient() external view returns (address);
/// @notice Read global ProtocolFeePercentage variable. Portion of the
///abi obtained yield that will be paid to the protocol as a fee.
function getProtocolFeePercentage() external view returns (uint16);
/// @notice Set global destination variable. PrizeDistributor contract that
/// will receive the obtained yield.
function setDestination(address _destination) external returns (address);
/// @notice Set global reserve variable.
function setReserve(IReserve _reserve) external returns (IReserve);
/// @notice Set global prizePool variable. PrizePool from where to get the
///abi yield from.
function setPrizePool(IPrizePool _prizePool) external returns (IPrizePool);
/// @notice Set global protocolFeeRecipient variable.
function setProtocolFeeRecipient(
address _protocolFeeRecipient
) external returns (address);
/// @notice Set global protocolFeePercentage variable.
function setProtocolFeePercentage(
uint16 _protocolFeePercentage
) external returns (uint16);
/**
* @notice Migrate interest from PrizePool to PrizeDistributor and the
* protocolFeeRecipient in a single transaction.
* @dev Captures interest, checkpoint data and transfers tokens to final
* destination.
* @return True if operation is successful.
*/
function flush() external returns (bool);
}
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.