Latest 25 from a total of 10,415 transactions
| Transaction Hash |
|
Block
|
From
|
To
|
|||||
|---|---|---|---|---|---|---|---|---|---|
| Claim Rewards | 25518503 | 9 mins ago | IN | 0 HYPE | 0.00004254 | ||||
| Claim Rewards | 25517494 | 25 mins ago | IN | 0 HYPE | 0.00006402 | ||||
| Claim Rewards | 25503432 | 4 hrs ago | IN | 0 HYPE | 0.00014501 | ||||
| Claim Rewards | 25496632 | 6 hrs ago | IN | 0 HYPE | 0.00038531 | ||||
| Claim Rewards | 25448543 | 19 hrs ago | IN | 0 HYPE | 0.00003595 | ||||
| Claim Rewards | 25448531 | 19 hrs ago | IN | 0 HYPE | 0.00003766 | ||||
| Claim Rewards | 25442603 | 20 hrs ago | IN | 0 HYPE | 0.0000424 | ||||
| Claim Rewards | 25442592 | 20 hrs ago | IN | 0 HYPE | 0.00004472 | ||||
| Claim Rewards | 25434355 | 23 hrs ago | IN | 0 HYPE | 0.00003783 | ||||
| Claim Rewards | 25427587 | 24 hrs ago | IN | 0 HYPE | 0.00004062 | ||||
| Claim Rewards | 25402401 | 31 hrs ago | IN | 0 HYPE | 0.00003547 | ||||
| Claim Rewards | 25402391 | 31 hrs ago | IN | 0 HYPE | 0.0000407 | ||||
| Claim Rewards | 25341360 | 2 days ago | IN | 0 HYPE | 0.00006108 | ||||
| Claim Rewards | 25341352 | 2 days ago | IN | 0 HYPE | 0.00005445 | ||||
| Claim Rewards | 25341342 | 2 days ago | IN | 0 HYPE | 0.00026379 | ||||
| Claim Rewards | 25341189 | 2 days ago | IN | 0 HYPE | 0.00005924 | ||||
| Claim Rewards | 25289682 | 2 days ago | IN | 0 HYPE | 0.00003595 | ||||
| Claim Rewards | 25279868 | 2 days ago | IN | 0 HYPE | 0.00006071 | ||||
| Claim Rewards | 25279856 | 2 days ago | IN | 0 HYPE | 0.00005393 | ||||
| Claim Rewards | 25244219 | 3 days ago | IN | 0 HYPE | 0.00027134 | ||||
| Claim Rewards | 25244210 | 3 days ago | IN | 0 HYPE | 0.00032732 | ||||
| Claim Rewards | 25221031 | 3 days ago | IN | 0 HYPE | 0.00003878 | ||||
| Claim Rewards | 25221021 | 3 days ago | IN | 0 HYPE | 0.00003888 | ||||
| Vote | 25213623 | 3 days ago | IN | 0 HYPE | 0.00017188 | ||||
| Claim Rewards | 25213590 | 3 days ago | IN | 0 HYPE | 0.00011334 |
View more zero value Internal Transactions in Advanced View mode
Advanced mode:
Cross-Chain Transactions
Loading...
Loading
Contract Name:
Voter
Compiler Version
v0.8.28+commit.7893614a
Optimization Enabled:
Yes with 200 runs
Other Settings:
cancun EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ExternalBribe} from "./ExternalBribe.sol";
import {SystemEpoch} from "./SystemEpoch.sol";
import {IBribe} from "../interfaces/IBribe.sol";
import {IGauge} from "../interfaces/IGauge.sol";
import {IMinter} from "../interfaces/IMinter.sol";
import {IVoter} from "../interfaces/IVoter.sol";
import {IVotingEscrow} from "../interfaces/IVotingEscrow.sol";
import {ICurveAddressRegistry} from "../interfaces/ICurveAddressRegistry.sol";
import {ICurveMetaRegistry} from "../interfaces/ICurveMetaRegistry.sol";
import {IGaugeProvider} from "../interfaces/IGaugeProvider.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
contract Voter is IVoter, SystemEpoch {
using SafeTransferLib for address;
address public immutable _ve; // the ve token that governs these contracts
address internal immutable base;
address public minter;
address public governor; // should be set to an IGovernor
address public emergencyCouncil; // credibly neutral party similar to Curve's Emergency DAO
address public gaugeProvider;
uint256 public totalWeight; // total voting weight
address[] public pools; // all pools viable for incentives
mapping(address => address) public gauges; // pool => gauge
mapping(address => address) public poolForGauge; // gauge => pool
mapping(address => address) public internal_bribes; // gauge => internal bribe (only fees)
mapping(address => address) public external_bribes; // gauge => external bribe (real bribes)
mapping(address => uint256) public weights; // pool => weight
mapping(uint256 => mapping(address => uint256)) public votes; // nft => pool => votes
mapping(uint256 => address[]) public poolVote; // nft => pools
mapping(uint256 => uint256) public usedWeights; // nft => total voting weight of user
mapping(uint256 => uint256) public lastVoted; // nft => timestamp of last vote, to ensure one vote per epoch
mapping(address => bool) public isGauge;
mapping(address => bool) public isWhitelisted;
mapping(address => bool) public isAlive;
event GaugeCreated(
address indexed gauge,
address creator,
address internal_bribe,
address indexed external_bribe,
address indexed pool
);
event GaugeKilled(address indexed gauge);
event GaugeRevived(address indexed gauge);
event Voted(address indexed voter, uint256 tokenId, uint256 weight);
event Abstained(uint256 tokenId, uint256 weight);
event Deposit(address indexed lp, address indexed gauge, uint256 tokenId, uint256 amount);
event Withdraw(address indexed lp, address indexed gauge, uint256 tokenId, uint256 amount);
event NotifyReward(address indexed sender, address indexed reward, uint256 amount);
event DistributeReward(address indexed sender, address indexed gauge, uint256 amount);
event Attach(address indexed owner, address indexed gauge, uint256 tokenId);
event Detach(address indexed owner, address indexed gauge, uint256 tokenId);
event Whitelisted(address indexed whitelister, address indexed token);
error PoolIsNotRegistered();
constructor(address __ve) {
_ve = __ve;
base = IVotingEscrow(__ve).token();
minter = msg.sender;
governor = msg.sender;
emergencyCouncil = msg.sender;
}
// simple re-entrancy check
uint256 internal _unlocked = 1;
modifier lock() {
require(_unlocked == 1);
_unlocked = 2;
_;
_unlocked = 1;
}
modifier onlyNewEpoch(uint256 _tokenId) {
// ensure new epoch since last vote
require((block.timestamp / EPOCH) * EPOCH > lastVoted[_tokenId], "TOKEN_ALREADY_VOTED_THIS_EPOCH");
_;
}
function initialize(address[] memory _tokens, address _minter, address _gaugeProvider) external {
require(msg.sender == minter);
for (uint256 i = 0; i < _tokens.length; i++) {
_whitelist(_tokens[i], true);
}
minter = _minter;
gaugeProvider = _gaugeProvider;
}
function setGovernor(address _governor) public {
require(msg.sender == governor);
governor = _governor;
}
function setEmergencyCouncil(address _council) public {
require(msg.sender == emergencyCouncil);
emergencyCouncil = _council;
}
function reset(uint256 _tokenId) external onlyNewEpoch(_tokenId) {
require(IVotingEscrow(_ve).isApprovedOrOwner(msg.sender, _tokenId));
lastVoted[_tokenId] = block.timestamp;
_reset(_tokenId);
IVotingEscrow(_ve).abstain(_tokenId);
}
function _reset(uint256 _tokenId) internal {
address[] storage _poolVote = poolVote[_tokenId];
uint256 _poolVoteCnt = _poolVote.length;
uint256 _totalWeight = 0;
for (uint256 i = 0; i < _poolVoteCnt; i++) {
address _pool = _poolVote[i];
uint256 _votes = votes[_tokenId][_pool];
if (_votes != 0) {
_updateFor(gauges[_pool]);
weights[_pool] -= _votes;
votes[_tokenId][_pool] -= _votes;
if (_votes > 0) {
IBribe(internal_bribes[gauges[_pool]])._withdraw(uint256(_votes), _tokenId);
IBribe(external_bribes[gauges[_pool]])._withdraw(uint256(_votes), _tokenId);
_totalWeight += _votes;
} else {
_totalWeight -= _votes;
}
emit Abstained(_tokenId, _votes);
}
}
totalWeight -= uint256(_totalWeight);
usedWeights[_tokenId] = 0;
delete poolVote[_tokenId];
}
function poke(uint256 _tokenId) external {
address[] memory _poolVote = poolVote[_tokenId];
uint256 _poolCnt = _poolVote.length;
uint256[] memory _weights = new uint256[](_poolCnt);
for (uint256 i = 0; i < _poolCnt; i++) {
_weights[i] = votes[_tokenId][_poolVote[i]];
}
_vote(_tokenId, _poolVote, _weights);
}
function _vote(uint256 _tokenId, address[] memory _poolVote, uint256[] memory _weights) internal {
_reset(_tokenId);
uint256 _poolCnt = _poolVote.length;
uint256 _weight = IVotingEscrow(_ve).balanceOfNFT(_tokenId);
uint256 _totalVoteWeight = 0;
uint256 _totalWeight = 0;
uint256 _usedWeight = 0;
for (uint256 i = 0; i < _poolCnt; i++) {
_totalVoteWeight += _weights[i];
}
for (uint256 i = 0; i < _poolCnt; i++) {
address _pool = _poolVote[i];
address _gauge = gauges[_pool];
if (!isAlive[_gauge]) {
continue;
}
if (isGauge[_gauge]) {
uint256 _poolWeight = _weights[i] * _weight / _totalVoteWeight;
if (_poolWeight == 0) {
continue;
}
require(votes[_tokenId][_pool] == 0);
_updateFor(_gauge);
poolVote[_tokenId].push(_pool);
weights[_pool] += _poolWeight;
votes[_tokenId][_pool] += _poolWeight;
IBribe(internal_bribes[_gauge])._deposit(uint256(_poolWeight), _tokenId);
IBribe(external_bribes[_gauge])._deposit(uint256(_poolWeight), _tokenId);
_usedWeight += _poolWeight;
_totalWeight += _poolWeight;
emit Voted(msg.sender, _tokenId, _poolWeight);
}
}
if (_usedWeight > 0) IVotingEscrow(_ve).voting(_tokenId);
totalWeight += uint256(_totalWeight);
usedWeights[_tokenId] = uint256(_usedWeight);
}
function vote(uint256 tokenId, address[] calldata _poolVote, uint256[] calldata _weights)
external
onlyNewEpoch(tokenId)
{
require(IVotingEscrow(_ve).isApprovedOrOwner(msg.sender, tokenId));
require(_poolVote.length == _weights.length);
lastVoted[tokenId] = block.timestamp;
_vote(tokenId, _poolVote, _weights);
}
function whitelist(address _token, bool _enable) public {
require(msg.sender == governor);
_whitelist(_token, _enable);
}
function _whitelist(address _token, bool _enable) internal {
require(!isWhitelisted[_token]);
isWhitelisted[_token] = _enable;
emit Whitelisted(msg.sender, _token);
}
function createGauge(uint8 _type, address _pool) external returns (address) {
require(msg.sender == governor);
require(gauges[_pool] == address(0x0), "exists");
(address gauge, address internalBribe, address externalBribe) =
IGaugeProvider(gaugeProvider).deployGauge(_type, _pool);
IERC20(base).approve(gauge, type(uint256).max);
internal_bribes[gauge] = internalBribe;
external_bribes[gauge] = externalBribe;
gauges[_pool] = gauge;
poolForGauge[gauge] = _pool;
isGauge[gauge] = true;
isAlive[gauge] = true;
_updateFor(gauge);
pools.push(_pool);
emit GaugeCreated(gauge, msg.sender, internalBribe, externalBribe, _pool);
return gauge;
}
function killGauge(address _gauge) external {
require(msg.sender == emergencyCouncil, "not emergency council");
require(isAlive[_gauge], "gauge already dead");
isAlive[_gauge] = false;
uint256 leftover = claimable[_gauge];
if (leftover > 0) {
base.safeTransfer(minter, leftover);
claimable[_gauge] = 0;
}
IERC20(base).approve(_gauge, 0);
emit GaugeKilled(_gauge);
}
function reviveGauge(address _gauge) external {
require(msg.sender == emergencyCouncil, "not emergency council");
require(!isAlive[_gauge], "gauge already alive");
isAlive[_gauge] = true;
IERC20(base).approve(_gauge, type(uint256).max);
emit GaugeRevived(_gauge);
}
function attachTokenToGauge(uint256 tokenId, address account) external {
require(isGauge[msg.sender]);
require(isAlive[msg.sender]); // killed gauges cannot attach tokens to themselves
if (tokenId > 0) IVotingEscrow(_ve).attach(tokenId);
emit Attach(account, msg.sender, tokenId);
}
function emitDeposit(uint256 tokenId, address account, uint256 amount) external {
require(isGauge[msg.sender]);
require(isAlive[msg.sender]);
emit Deposit(account, msg.sender, tokenId, amount);
}
function detachTokenFromGauge(uint256 tokenId, address account) external {
require(isGauge[msg.sender]);
if (tokenId > 0) IVotingEscrow(_ve).detach(tokenId);
emit Detach(account, msg.sender, tokenId);
}
function emitWithdraw(uint256 tokenId, address account, uint256 amount) external {
require(isGauge[msg.sender]);
emit Withdraw(account, msg.sender, tokenId, amount);
}
function length() external view returns (uint256) {
return pools.length;
}
uint256 internal index;
mapping(address => uint256) internal supplyIndex;
mapping(address => uint256) public claimable;
function notifyRewardAmount(uint256 amount) external {
_safeTransferFrom(base, msg.sender, address(this), amount); // transfer the distro in
uint256 _ratio = amount * 1e18 / totalWeight; // 1e18 adjustment is removed during claim
if (_ratio > 0) {
index += _ratio;
}
emit NotifyReward(msg.sender, base, amount);
}
function updateFor(address[] memory _gauges) external {
for (uint256 i = 0; i < _gauges.length; i++) {
_updateFor(_gauges[i]);
}
}
function updateForRange(uint256 start, uint256 end) public {
for (uint256 i = start; i < end; i++) {
_updateFor(gauges[pools[i]]);
}
}
function updateAll() external {
updateForRange(0, pools.length);
}
function updateGauge(address _gauge) external {
_updateFor(_gauge);
}
function _updateFor(address _gauge) internal {
address _pool = poolForGauge[_gauge];
uint256 _supplied = weights[_pool];
if (_supplied > 0) {
uint256 _supplyIndex = supplyIndex[_gauge];
uint256 _index = index; // get global index0 for accumulated distro
supplyIndex[_gauge] = _index; // update _gauge current position to global position
uint256 _delta = _index - _supplyIndex; // see if there is any difference that need to be accrued
if (_delta > 0) {
uint256 _share = uint256(_supplied) * _delta / 1e18; // add accrued difference for each supplied token
if (isAlive[_gauge]) {
claimable[_gauge] += _share;
} else {
base.safeTransfer(minter, _share);
}
}
} else {
supplyIndex[_gauge] = index; // new users are set to the default global state
}
}
function claimRewards(address[] memory _gauges, address[][] memory _tokens) external {
for (uint256 i = 0; i < _gauges.length; i++) {
IGauge(_gauges[i]).getReward(msg.sender, _tokens[i]);
}
}
function claimBribes(address[] memory _bribes, address[][] memory _tokens, uint256 _tokenId) external {
require(IVotingEscrow(_ve).isApprovedOrOwner(msg.sender, _tokenId));
for (uint256 i = 0; i < _bribes.length; i++) {
IBribe(_bribes[i]).getRewardForOwner(_tokenId, _tokens[i]);
}
}
function claimFees(address[] memory _fees, address[][] memory _tokens, uint256 _tokenId) external {
require(IVotingEscrow(_ve).isApprovedOrOwner(msg.sender, _tokenId));
for (uint256 i = 0; i < _fees.length; i++) {
IBribe(_fees[i]).getRewardForOwner(_tokenId, _tokens[i]);
}
}
function distribute(address _gauge) public lock {
IMinter(minter).updatePeriod();
_updateFor(_gauge); // should set claimable to 0 if killed
uint256 _claimable = claimable[_gauge];
if (_claimable > IGauge(_gauge).left(base) && _claimable / EPOCH > 0) {
claimable[_gauge] = 0;
IGauge(_gauge).notifyRewardAmount(base, _claimable);
emit DistributeReward(msg.sender, _gauge, _claimable);
}
}
function distro() external {
distribute(0, pools.length);
}
function distribute() external {
distribute(0, pools.length);
}
function distribute(uint256 start, uint256 finish) public {
for (uint256 x = start; x < finish; x++) {
distribute(gauges[pools[x]]);
}
}
function distribute(address[] memory _gauges) external {
for (uint256 x = 0; x < _gauges.length; x++) {
distribute(_gauges[x]);
}
}
function _safeTransferFrom(address token, address from, address to, uint256 value) internal {
require(token.code.length > 0);
(bool success, bytes memory data) =
token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))));
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.20;
/**
* @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]
* ```solidity
* 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 Storage of the initializable contract.
*
* It's implemented on a custom ERC-7201 namespace to reduce the risk of storage collisions
* when using with upgradeable contracts.
*
* @custom:storage-location erc7201:openzeppelin.storage.Initializable
*/
struct InitializableStorage {
/**
* @dev Indicates that the contract has been initialized.
*/
uint64 _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool _initializing;
}
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Initializable")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant INITIALIZABLE_STORAGE = 0xf0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00;
/**
* @dev The contract is already initialized.
*/
error InvalidInitialization();
/**
* @dev The contract is not initializing.
*/
error NotInitializing();
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint64 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 in the context of a constructor an `initializer` may be invoked any
* number of times. This behavior in the constructor can be useful during testing and is not expected to be used in
* production.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
// Cache values to avoid duplicated sloads
bool isTopLevelCall = !$._initializing;
uint64 initialized = $._initialized;
// Allowed calls:
// - initialSetup: the contract is not in the initializing state and no previous version was
// initialized
// - construction: the contract is initialized at version 1 (no reinitialization) and the
// current contract is just being deployed
bool initialSetup = initialized == 0 && isTopLevelCall;
bool construction = initialized == 1 && address(this).code.length == 0;
if (!initialSetup && !construction) {
revert InvalidInitialization();
}
$._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 2**64 - 1 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint64 version) {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
if ($._initializing || $._initialized >= version) {
revert InvalidInitialization();
}
$._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() {
_checkInitializing();
_;
}
/**
* @dev Reverts if the contract is not in an initializing state. See {onlyInitializing}.
*/
function _checkInitializing() internal view virtual {
if (!_isInitializing()) {
revert NotInitializing();
}
}
/**
* @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 {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
if ($._initializing) {
revert InvalidInitialization();
}
if ($._initialized != type(uint64).max) {
$._initialized = type(uint64).max;
emit Initialized(type(uint64).max);
}
}
/**
* @dev Returns the highest version that has been initialized. See {reinitializer}.
*/
function _getInitializedVersion() internal view returns (uint64) {
return _getInitializableStorage()._initialized;
}
/**
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
*/
function _isInitializing() internal view returns (bool) {
return _getInitializableStorage()._initializing;
}
/**
* @dev Pointer to storage slot. Allows integrators to override it with a custom storage location.
*
* NOTE: Consider following the ERC-7201 formula to derive storage locations.
*/
function _initializableStorageSlot() internal pure virtual returns (bytes32) {
return INITIALIZABLE_STORAGE;
}
/**
* @dev Returns a pointer to the storage namespace.
*/
// solhint-disable-next-line var-name-mixedcase
function _getInitializableStorage() private pure returns (InitializableStorage storage $) {
bytes32 slot = _initializableStorageSlot();
assembly {
$.slot := slot
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (proxy/utils/UUPSUpgradeable.sol)
pragma solidity ^0.8.22;
import {IERC1822Proxiable} from "@openzeppelin/contracts/interfaces/draft-IERC1822.sol";
import {ERC1967Utils} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol";
import {Initializable} from "./Initializable.sol";
/**
* @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
* {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
*
* A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
* reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
* `UUPSUpgradeable` with a custom implementation of upgrades.
*
* The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
*/
abstract contract UUPSUpgradeable is Initializable, IERC1822Proxiable {
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address private immutable __self = address(this);
/**
* @dev The version of the upgrade interface of the contract. If this getter is missing, both `upgradeTo(address)`
* and `upgradeToAndCall(address,bytes)` are present, and `upgradeTo` must be used if no function should be called,
* while `upgradeToAndCall` will invoke the `receive` function if the second argument is the empty byte string.
* If the getter returns `"5.0.0"`, only `upgradeToAndCall(address,bytes)` is present, and the second argument must
* be the empty byte string if no function should be called, making it impossible to invoke the `receive` function
* during an upgrade.
*/
string public constant UPGRADE_INTERFACE_VERSION = "5.0.0";
/**
* @dev The call is from an unauthorized context.
*/
error UUPSUnauthorizedCallContext();
/**
* @dev The storage `slot` is unsupported as a UUID.
*/
error UUPSUnsupportedProxiableUUID(bytes32 slot);
/**
* @dev Check that the execution is being performed through a delegatecall call and that the execution context is
* a proxy contract with an implementation (as defined in ERC-1967) pointing to self. This should only be the case
* for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
* function through ERC-1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
* fail.
*/
modifier onlyProxy() {
_checkProxy();
_;
}
/**
* @dev Check that the execution is not being performed through a delegate call. This allows a function to be
* callable on the implementing contract but not through proxies.
*/
modifier notDelegated() {
_checkNotDelegated();
_;
}
function __UUPSUpgradeable_init() internal onlyInitializing {
}
function __UUPSUpgradeable_init_unchained() internal onlyInitializing {
}
/**
* @dev Implementation of the ERC-1822 {proxiableUUID} function. This returns the storage slot used by the
* implementation. It is used to validate the implementation's compatibility when performing an upgrade.
*
* IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
* bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
* function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
*/
function proxiableUUID() external view virtual notDelegated returns (bytes32) {
return ERC1967Utils.IMPLEMENTATION_SLOT;
}
/**
* @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
* encoded in `data`.
*
* Calls {_authorizeUpgrade}.
*
* Emits an {Upgraded} event.
*
* @custom:oz-upgrades-unsafe-allow-reachable delegatecall
*/
function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
_authorizeUpgrade(newImplementation);
_upgradeToAndCallUUPS(newImplementation, data);
}
/**
* @dev Reverts if the execution is not performed via delegatecall or the execution
* context is not of a proxy with an ERC-1967 compliant implementation pointing to self.
*/
function _checkProxy() internal view virtual {
if (
address(this) == __self || // Must be called through delegatecall
ERC1967Utils.getImplementation() != __self // Must be called through an active proxy
) {
revert UUPSUnauthorizedCallContext();
}
}
/**
* @dev Reverts if the execution is performed via delegatecall.
* See {notDelegated}.
*/
function _checkNotDelegated() internal view virtual {
if (address(this) != __self) {
// Must not be called through delegatecall
revert UUPSUnauthorizedCallContext();
}
}
/**
* @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
* {upgradeToAndCall}.
*
* Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
*
* ```solidity
* function _authorizeUpgrade(address) internal onlyOwner {}
* ```
*/
function _authorizeUpgrade(address newImplementation) internal virtual;
/**
* @dev Performs an implementation upgrade with a security check for UUPS proxies, and additional setup call.
*
* As a security check, {proxiableUUID} is invoked in the new implementation, and the return value
* is expected to be the implementation slot in ERC-1967.
*
* Emits an {IERC1967-Upgraded} event.
*/
function _upgradeToAndCallUUPS(address newImplementation, bytes memory data) private {
try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
if (slot != ERC1967Utils.IMPLEMENTATION_SLOT) {
revert UUPSUnsupportedProxiableUUID(slot);
}
ERC1967Utils.upgradeToAndCall(newImplementation, data);
} catch {
// The implementation is not UUPS
revert ERC1967Utils.ERC1967InvalidImplementation(newImplementation);
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
import {Initializable} from "../proxy/utils/Initializable.sol";
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuardUpgradeable is Initializable {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
/// @custom:storage-location erc7201:openzeppelin.storage.ReentrancyGuard
struct ReentrancyGuardStorage {
uint256 _status;
}
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant ReentrancyGuardStorageLocation = 0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
function _getReentrancyGuardStorage() private pure returns (ReentrancyGuardStorage storage $) {
assembly {
$.slot := ReentrancyGuardStorageLocation
}
}
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
function __ReentrancyGuard_init() internal onlyInitializing {
__ReentrancyGuard_init_unchained();
}
function __ReentrancyGuard_init_unchained() internal onlyInitializing {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
$._status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
// On the first call to nonReentrant, _status will be NOT_ENTERED
if ($._status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
$._status = ENTERED;
}
function _nonReentrantAfter() private {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
$._status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
return $._status == ENTERED;
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC1967.sol)
pragma solidity ^0.8.20;
/**
* @dev ERC-1967: Proxy Storage Slots. This interface contains the events defined in the ERC.
*/
interface IERC1967 {
/**
* @dev Emitted when the implementation is upgraded.
*/
event Upgraded(address indexed implementation);
/**
* @dev Emitted when the admin account has changed.
*/
event AdminChanged(address previousAdmin, address newAdmin);
/**
* @dev Emitted when the beacon is changed.
*/
event BeaconUpgraded(address indexed beacon);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (interfaces/draft-IERC1822.sol)
pragma solidity ^0.8.20;
/**
* @dev ERC-1822: Universal Upgradeable Proxy Standard (UUPS) documents a method for upgradeability through a simplified
* proxy whose upgrades are fully controlled by the current implementation.
*/
interface IERC1822Proxiable {
/**
* @dev Returns the storage slot that the proxiable contract assumes is being used to store the implementation
* address.
*
* IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
* bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
* function revert if invoked through a proxy.
*/
function proxiableUUID() external view returns (bytes32);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.2.0) (proxy/ERC1967/ERC1967Utils.sol)
pragma solidity ^0.8.22;
import {IBeacon} from "../beacon/IBeacon.sol";
import {IERC1967} from "../../interfaces/IERC1967.sol";
import {Address} from "../../utils/Address.sol";
import {StorageSlot} from "../../utils/StorageSlot.sol";
/**
* @dev This library provides getters and event emitting update functions for
* https://eips.ethereum.org/EIPS/eip-1967[ERC-1967] slots.
*/
library ERC1967Utils {
/**
* @dev Storage slot with the address of the current implementation.
* This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1.
*/
// solhint-disable-next-line private-vars-leading-underscore
bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
/**
* @dev The `implementation` of the proxy is invalid.
*/
error ERC1967InvalidImplementation(address implementation);
/**
* @dev The `admin` of the proxy is invalid.
*/
error ERC1967InvalidAdmin(address admin);
/**
* @dev The `beacon` of the proxy is invalid.
*/
error ERC1967InvalidBeacon(address beacon);
/**
* @dev An upgrade function sees `msg.value > 0` that may be lost.
*/
error ERC1967NonPayable();
/**
* @dev Returns the current implementation address.
*/
function getImplementation() internal view returns (address) {
return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
}
/**
* @dev Stores a new address in the ERC-1967 implementation slot.
*/
function _setImplementation(address newImplementation) private {
if (newImplementation.code.length == 0) {
revert ERC1967InvalidImplementation(newImplementation);
}
StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
}
/**
* @dev Performs implementation upgrade with additional setup call if data is nonempty.
* This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
* to avoid stuck value in the contract.
*
* Emits an {IERC1967-Upgraded} event.
*/
function upgradeToAndCall(address newImplementation, bytes memory data) internal {
_setImplementation(newImplementation);
emit IERC1967.Upgraded(newImplementation);
if (data.length > 0) {
Address.functionDelegateCall(newImplementation, data);
} else {
_checkNonPayable();
}
}
/**
* @dev Storage slot with the admin of the contract.
* This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1.
*/
// solhint-disable-next-line private-vars-leading-underscore
bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
/**
* @dev Returns the current admin.
*
* TIP: To get this value clients can read directly from the storage slot shown below (specified by ERC-1967) using
* the https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
* `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
*/
function getAdmin() internal view returns (address) {
return StorageSlot.getAddressSlot(ADMIN_SLOT).value;
}
/**
* @dev Stores a new address in the ERC-1967 admin slot.
*/
function _setAdmin(address newAdmin) private {
if (newAdmin == address(0)) {
revert ERC1967InvalidAdmin(address(0));
}
StorageSlot.getAddressSlot(ADMIN_SLOT).value = newAdmin;
}
/**
* @dev Changes the admin of the proxy.
*
* Emits an {IERC1967-AdminChanged} event.
*/
function changeAdmin(address newAdmin) internal {
emit IERC1967.AdminChanged(getAdmin(), newAdmin);
_setAdmin(newAdmin);
}
/**
* @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
* This is the keccak-256 hash of "eip1967.proxy.beacon" subtracted by 1.
*/
// solhint-disable-next-line private-vars-leading-underscore
bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
/**
* @dev Returns the current beacon.
*/
function getBeacon() internal view returns (address) {
return StorageSlot.getAddressSlot(BEACON_SLOT).value;
}
/**
* @dev Stores a new beacon in the ERC-1967 beacon slot.
*/
function _setBeacon(address newBeacon) private {
if (newBeacon.code.length == 0) {
revert ERC1967InvalidBeacon(newBeacon);
}
StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
address beaconImplementation = IBeacon(newBeacon).implementation();
if (beaconImplementation.code.length == 0) {
revert ERC1967InvalidImplementation(beaconImplementation);
}
}
/**
* @dev Change the beacon and trigger a setup call if data is nonempty.
* This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
* to avoid stuck value in the contract.
*
* Emits an {IERC1967-BeaconUpgraded} event.
*
* CAUTION: Invoking this function has no effect on an instance of {BeaconProxy} since v5, since
* it uses an immutable beacon without looking at the value of the ERC-1967 beacon slot for
* efficiency.
*/
function upgradeBeaconToAndCall(address newBeacon, bytes memory data) internal {
_setBeacon(newBeacon);
emit IERC1967.BeaconUpgraded(newBeacon);
if (data.length > 0) {
Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
} else {
_checkNonPayable();
}
}
/**
* @dev Reverts if `msg.value` is not zero. It can be used to avoid `msg.value` stuck in the contract
* if an upgrade doesn't perform an initialization call.
*/
function _checkNonPayable() private {
if (msg.value > 0) {
revert ERC1967NonPayable();
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (proxy/beacon/IBeacon.sol)
pragma solidity ^0.8.20;
/**
* @dev This is the interface that {BeaconProxy} expects of its beacon.
*/
interface IBeacon {
/**
* @dev Must return an address that can be used as a delegate call target.
*
* {UpgradeableBeacon} will check that this address is a contract.
*/
function implementation() external view returns (address);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of 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 value) 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 a `value` amount of tokens 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 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` 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 value) external returns (bool);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.2.0) (utils/Address.sol)
pragma solidity ^0.8.20;
import {Errors} from "./Errors.sol";
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev There's no code at `target` (it is not a contract).
*/
error AddressEmptyCode(address target);
/**
* @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://consensys.net/diligence/blog/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.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
if (address(this).balance < amount) {
revert Errors.InsufficientBalance(address(this).balance, amount);
}
(bool success, bytes memory returndata) = recipient.call{value: amount}("");
if (!success) {
_revert(returndata);
}
}
/**
* @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 or custom error, it is bubbled
* up by this function (like regular Solidity function calls). However, if
* the call reverted with no returned reason, this function reverts with a
* {Errors.FailedCall} error.
*
* 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.
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0);
}
/**
* @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`.
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
if (address(this).balance < value) {
revert Errors.InsufficientBalance(address(this).balance, value);
}
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target
* was not a contract or bubbling up the revert reason (falling back to {Errors.FailedCall}) in case
* of an unsuccessful call.
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata
) internal view returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
// only check if target is a contract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
if (returndata.length == 0 && target.code.length == 0) {
revert AddressEmptyCode(target);
}
return returndata;
}
}
/**
* @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the
* revert reason or with a default {Errors.FailedCall} error.
*/
function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
return returndata;
}
}
/**
* @dev Reverts with returndata if present. Otherwise reverts with {Errors.FailedCall}.
*/
function _revert(bytes memory returndata) 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
assembly ("memory-safe") {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert Errors.FailedCall();
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/Errors.sol)
pragma solidity ^0.8.20;
/**
* @dev Collection of common custom errors used in multiple contracts
*
* IMPORTANT: Backwards compatibility is not guaranteed in future versions of the library.
* It is recommended to avoid relying on the error API for critical functionality.
*
* _Available since v5.1._
*/
library Errors {
/**
* @dev The ETH balance of the account is not enough to perform the operation.
*/
error InsufficientBalance(uint256 balance, uint256 needed);
/**
* @dev A call to an address target failed. The target may have reverted.
*/
error FailedCall();
/**
* @dev The deployment failed.
*/
error FailedDeployment();
/**
* @dev A necessary precompile is missing.
*/
error MissingPrecompile(address);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
pragma solidity ^0.8.20;
/**
* @dev Library for reading and writing primitive types to specific storage slots.
*
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
* This library helps with reading and writing to such slots without the need for inline assembly.
*
* The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
*
* Example usage to set ERC-1967 implementation slot:
* ```solidity
* contract ERC1967 {
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
*
* function _getImplementation() internal view returns (address) {
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
* }
*
* function _setImplementation(address newImplementation) internal {
* require(newImplementation.code.length > 0);
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
* }
* }
* ```
*
* TIP: Consider using this library along with {SlotDerivation}.
*/
library StorageSlot {
struct AddressSlot {
address value;
}
struct BooleanSlot {
bool value;
}
struct Bytes32Slot {
bytes32 value;
}
struct Uint256Slot {
uint256 value;
}
struct Int256Slot {
int256 value;
}
struct StringSlot {
string value;
}
struct BytesSlot {
bytes value;
}
/**
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
*/
function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `BooleanSlot` with member `value` located at `slot`.
*/
function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Bytes32Slot` with member `value` located at `slot`.
*/
function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Uint256Slot` with member `value` located at `slot`.
*/
function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Int256Slot` with member `value` located at `slot`.
*/
function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `StringSlot` with member `value` located at `slot`.
*/
function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns an `StringSlot` representation of the string storage pointer `store`.
*/
function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
assembly ("memory-safe") {
r.slot := store.slot
}
}
/**
* @dev Returns a `BytesSlot` with member `value` located at `slot`.
*/
function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
*/
function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
assembly ("memory-safe") {
r.slot := store.slot
}
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Arithmetic library with operations for fixed-point numbers.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/FixedPointMathLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/FixedPointMathLib.sol)
library FixedPointMathLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The operation failed, as the output exceeds the maximum value of uint256.
error ExpOverflow();
/// @dev The operation failed, as the output exceeds the maximum value of uint256.
error FactorialOverflow();
/// @dev The operation failed, due to an overflow.
error RPowOverflow();
/// @dev The mantissa is too big to fit.
error MantissaOverflow();
/// @dev The operation failed, due to an multiplication overflow.
error MulWadFailed();
/// @dev The operation failed, due to an multiplication overflow.
error SMulWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error DivWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error SDivWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error MulDivFailed();
/// @dev The division failed, as the denominator is zero.
error DivFailed();
/// @dev The full precision multiply-divide operation failed, either due
/// to the result being larger than 256 bits, or a division by a zero.
error FullMulDivFailed();
/// @dev The output is undefined, as the input is less-than-or-equal to zero.
error LnWadUndefined();
/// @dev The input outside the acceptable domain.
error OutOfDomain();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The scalar of ETH and most ERC20s.
uint256 internal constant WAD = 1e18;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* SIMPLIFIED FIXED POINT OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `(x * y) / WAD` rounded down.
function mulWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if gt(x, div(not(0), y)) {
if y {
mstore(0x00, 0xbac65e5b) // `MulWadFailed()`.
revert(0x1c, 0x04)
}
}
z := div(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down.
function sMulWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require((x == 0 || z / x == y) && !(x == -1 && y == type(int256).min))`.
if iszero(gt(or(iszero(x), eq(sdiv(z, x), y)), lt(not(x), eq(y, shl(255, 1))))) {
mstore(0x00, 0xedcd4dd4) // `SMulWadFailed()`.
revert(0x1c, 0x04)
}
z := sdiv(z, WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down, but without overflow checks.
function rawMulWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down, but without overflow checks.
function rawSMulWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded up.
function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if iszero(eq(div(z, y), x)) {
if y {
mstore(0x00, 0xbac65e5b) // `MulWadFailed()`.
revert(0x1c, 0x04)
}
}
z := add(iszero(iszero(mod(z, WAD))), div(z, WAD))
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded up, but without overflow checks.
function rawMulWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(iszero(iszero(mod(mul(x, y), WAD))), div(mul(x, y), WAD))
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down.
function divWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y != 0 && x <= type(uint256).max / WAD)`.
if iszero(mul(y, lt(x, add(1, div(not(0), WAD))))) {
mstore(0x00, 0x7c5f487d) // `DivWadFailed()`.
revert(0x1c, 0x04)
}
z := div(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down.
function sDivWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, WAD)
// Equivalent to `require(y != 0 && ((x * WAD) / WAD == x))`.
if iszero(mul(y, eq(sdiv(z, WAD), x))) {
mstore(0x00, 0x5c43740d) // `SDivWadFailed()`.
revert(0x1c, 0x04)
}
z := sdiv(z, y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down, but without overflow and divide by zero checks.
function rawDivWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down, but without overflow and divide by zero checks.
function rawSDivWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded up.
function divWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y != 0 && x <= type(uint256).max / WAD)`.
if iszero(mul(y, lt(x, add(1, div(not(0), WAD))))) {
mstore(0x00, 0x7c5f487d) // `DivWadFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(mul(x, WAD), y))), div(mul(x, WAD), y))
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded up, but without overflow and divide by zero checks.
function rawDivWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(iszero(iszero(mod(mul(x, WAD), y))), div(mul(x, WAD), y))
}
}
/// @dev Equivalent to `x` to the power of `y`.
/// because `x ** y = (e ** ln(x)) ** y = e ** (ln(x) * y)`.
/// Note: This function is an approximation.
function powWad(int256 x, int256 y) internal pure returns (int256) {
// Using `ln(x)` means `x` must be greater than 0.
return expWad((lnWad(x) * y) / int256(WAD));
}
/// @dev Returns `exp(x)`, denominated in `WAD`.
/// Credit to Remco Bloemen under MIT license: https://2π.com/22/exp-ln
/// Note: This function is an approximation. Monotonically increasing.
function expWad(int256 x) internal pure returns (int256 r) {
unchecked {
// When the result is less than 0.5 we return zero.
// This happens when `x <= (log(1e-18) * 1e18) ~ -4.15e19`.
if (x <= -41446531673892822313) return r;
/// @solidity memory-safe-assembly
assembly {
// When the result is greater than `(2**255 - 1) / 1e18` we can not represent it as
// an int. This happens when `x >= floor(log((2**255 - 1) / 1e18) * 1e18) ≈ 135`.
if iszero(slt(x, 135305999368893231589)) {
mstore(0x00, 0xa37bfec9) // `ExpOverflow()`.
revert(0x1c, 0x04)
}
}
// `x` is now in the range `(-42, 136) * 1e18`. Convert to `(-42, 136) * 2**96`
// for more intermediate precision and a binary basis. This base conversion
// is a multiplication by 1e18 / 2**96 = 5**18 / 2**78.
x = (x << 78) / 5 ** 18;
// Reduce range of x to (-½ ln 2, ½ ln 2) * 2**96 by factoring out powers
// of two such that exp(x) = exp(x') * 2**k, where k is an integer.
// Solving this gives k = round(x / log(2)) and x' = x - k * log(2).
int256 k = ((x << 96) / 54916777467707473351141471128 + 2 ** 95) >> 96;
x = x - k * 54916777467707473351141471128;
// `k` is in the range `[-61, 195]`.
// Evaluate using a (6, 7)-term rational approximation.
// `p` is made monic, we'll multiply by a scale factor later.
int256 y = x + 1346386616545796478920950773328;
y = ((y * x) >> 96) + 57155421227552351082224309758442;
int256 p = y + x - 94201549194550492254356042504812;
p = ((p * y) >> 96) + 28719021644029726153956944680412240;
p = p * x + (4385272521454847904659076985693276 << 96);
// We leave `p` in `2**192` basis so we don't need to scale it back up for the division.
int256 q = x - 2855989394907223263936484059900;
q = ((q * x) >> 96) + 50020603652535783019961831881945;
q = ((q * x) >> 96) - 533845033583426703283633433725380;
q = ((q * x) >> 96) + 3604857256930695427073651918091429;
q = ((q * x) >> 96) - 14423608567350463180887372962807573;
q = ((q * x) >> 96) + 26449188498355588339934803723976023;
/// @solidity memory-safe-assembly
assembly {
// Div in assembly because solidity adds a zero check despite the unchecked.
// The q polynomial won't have zeros in the domain as all its roots are complex.
// No scaling is necessary because p is already `2**96` too large.
r := sdiv(p, q)
}
// r should be in the range `(0.09, 0.25) * 2**96`.
// We now need to multiply r by:
// - The scale factor `s ≈ 6.031367120`.
// - The `2**k` factor from the range reduction.
// - The `1e18 / 2**96` factor for base conversion.
// We do this all at once, with an intermediate result in `2**213`
// basis, so the final right shift is always by a positive amount.
r = int256(
(uint256(r) * 3822833074963236453042738258902158003155416615667) >> uint256(195 - k)
);
}
}
/// @dev Returns `ln(x)`, denominated in `WAD`.
/// Credit to Remco Bloemen under MIT license: https://2π.com/22/exp-ln
/// Note: This function is an approximation. Monotonically increasing.
function lnWad(int256 x) internal pure returns (int256 r) {
/// @solidity memory-safe-assembly
assembly {
// We want to convert `x` from `10**18` fixed point to `2**96` fixed point.
// We do this by multiplying by `2**96 / 10**18`. But since
// `ln(x * C) = ln(x) + ln(C)`, we can simply do nothing here
// and add `ln(2**96 / 10**18)` at the end.
// Compute `k = log2(x) - 96`, `r = 159 - k = 255 - log2(x) = 255 ^ log2(x)`.
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// We place the check here for more optimal stack operations.
if iszero(sgt(x, 0)) {
mstore(0x00, 0x1615e638) // `LnWadUndefined()`.
revert(0x1c, 0x04)
}
// forgefmt: disable-next-item
r := xor(r, byte(and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)),
0xf8f9f9faf9fdfafbf9fdfcfdfafbfcfef9fafdfafcfcfbfefafafcfbffffffff))
// Reduce range of x to (1, 2) * 2**96
// ln(2^k * x) = k * ln(2) + ln(x)
x := shr(159, shl(r, x))
// Evaluate using a (8, 8)-term rational approximation.
// `p` is made monic, we will multiply by a scale factor later.
// forgefmt: disable-next-item
let p := sub( // This heavily nested expression is to avoid stack-too-deep for via-ir.
sar(96, mul(add(43456485725739037958740375743393,
sar(96, mul(add(24828157081833163892658089445524,
sar(96, mul(add(3273285459638523848632254066296,
x), x))), x))), x)), 11111509109440967052023855526967)
p := sub(sar(96, mul(p, x)), 45023709667254063763336534515857)
p := sub(sar(96, mul(p, x)), 14706773417378608786704636184526)
p := sub(mul(p, x), shl(96, 795164235651350426258249787498))
// We leave `p` in `2**192` basis so we don't need to scale it back up for the division.
// `q` is monic by convention.
let q := add(5573035233440673466300451813936, x)
q := add(71694874799317883764090561454958, sar(96, mul(x, q)))
q := add(283447036172924575727196451306956, sar(96, mul(x, q)))
q := add(401686690394027663651624208769553, sar(96, mul(x, q)))
q := add(204048457590392012362485061816622, sar(96, mul(x, q)))
q := add(31853899698501571402653359427138, sar(96, mul(x, q)))
q := add(909429971244387300277376558375, sar(96, mul(x, q)))
// `p / q` is in the range `(0, 0.125) * 2**96`.
// Finalization, we need to:
// - Multiply by the scale factor `s = 5.549…`.
// - Add `ln(2**96 / 10**18)`.
// - Add `k * ln(2)`.
// - Multiply by `10**18 / 2**96 = 5**18 >> 78`.
// The q polynomial is known not to have zeros in the domain.
// No scaling required because p is already `2**96` too large.
p := sdiv(p, q)
// Multiply by the scaling factor: `s * 5**18 * 2**96`, base is now `5**18 * 2**192`.
p := mul(1677202110996718588342820967067443963516166, p)
// Add `ln(2) * k * 5**18 * 2**192`.
// forgefmt: disable-next-item
p := add(mul(16597577552685614221487285958193947469193820559219878177908093499208371, sub(159, r)), p)
// Add `ln(2**96 / 10**18) * 5**18 * 2**192`.
p := add(600920179829731861736702779321621459595472258049074101567377883020018308, p)
// Base conversion: mul `2**18 / 2**192`.
r := sar(174, p)
}
}
/// @dev Returns `W_0(x)`, denominated in `WAD`.
/// See: https://en.wikipedia.org/wiki/Lambert_W_function
/// a.k.a. Product log function. This is an approximation of the principal branch.
/// Note: This function is an approximation. Monotonically increasing.
function lambertW0Wad(int256 x) internal pure returns (int256 w) {
// forgefmt: disable-next-item
unchecked {
if ((w = x) <= -367879441171442322) revert OutOfDomain(); // `x` less than `-1/e`.
(int256 wad, int256 p) = (int256(WAD), x);
uint256 c; // Whether we need to avoid catastrophic cancellation.
uint256 i = 4; // Number of iterations.
if (w <= 0x1ffffffffffff) {
if (-0x4000000000000 <= w) {
i = 1; // Inputs near zero only take one step to converge.
} else if (w <= -0x3ffffffffffffff) {
i = 32; // Inputs near `-1/e` take very long to converge.
}
} else if (uint256(w >> 63) == uint256(0)) {
/// @solidity memory-safe-assembly
assembly {
// Inline log2 for more performance, since the range is small.
let v := shr(49, w)
let l := shl(3, lt(0xff, v))
l := add(or(l, byte(and(0x1f, shr(shr(l, v), 0x8421084210842108cc6318c6db6d54be)),
0x0706060506020504060203020504030106050205030304010505030400000000)), 49)
w := sdiv(shl(l, 7), byte(sub(l, 31), 0x0303030303030303040506080c13))
c := gt(l, 60)
i := add(2, add(gt(l, 53), c))
}
} else {
int256 ll = lnWad(w = lnWad(w));
/// @solidity memory-safe-assembly
assembly {
// `w = ln(x) - ln(ln(x)) + b * ln(ln(x)) / ln(x)`.
w := add(sdiv(mul(ll, 1023715080943847266), w), sub(w, ll))
i := add(3, iszero(shr(68, x)))
c := iszero(shr(143, x))
}
if (c == uint256(0)) {
do { // If `x` is big, use Newton's so that intermediate values won't overflow.
int256 e = expWad(w);
/// @solidity memory-safe-assembly
assembly {
let t := mul(w, div(e, wad))
w := sub(w, sdiv(sub(t, x), div(add(e, t), wad)))
}
if (p <= w) break;
p = w;
} while (--i != uint256(0));
/// @solidity memory-safe-assembly
assembly {
w := sub(w, sgt(w, 2))
}
return w;
}
}
do { // Otherwise, use Halley's for faster convergence.
int256 e = expWad(w);
/// @solidity memory-safe-assembly
assembly {
let t := add(w, wad)
let s := sub(mul(w, e), mul(x, wad))
w := sub(w, sdiv(mul(s, wad), sub(mul(e, t), sdiv(mul(add(t, wad), s), add(t, t)))))
}
if (p <= w) break;
p = w;
} while (--i != c);
/// @solidity memory-safe-assembly
assembly {
w := sub(w, sgt(w, 2))
}
// For certain ranges of `x`, we'll use the quadratic-rate recursive formula of
// R. Iacono and J.P. Boyd for the last iteration, to avoid catastrophic cancellation.
if (c == uint256(0)) return w;
int256 t = w | 1;
/// @solidity memory-safe-assembly
assembly {
x := sdiv(mul(x, wad), t)
}
x = (t * (wad + lnWad(x)));
/// @solidity memory-safe-assembly
assembly {
w := sdiv(x, add(wad, t))
}
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* GENERAL NUMBER UTILITIES */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns `a * b == x * y`, with full precision.
function fullMulEq(uint256 a, uint256 b, uint256 x, uint256 y)
internal
pure
returns (bool result)
{
/// @solidity memory-safe-assembly
assembly {
result := and(eq(mul(a, b), mul(x, y)), eq(mulmod(x, y, not(0)), mulmod(a, b, not(0))))
}
}
/// @dev Calculates `floor(x * y / d)` with full precision.
/// Throws if result overflows a uint256 or when `d` is zero.
/// Credit to Remco Bloemen under MIT license: https://2π.com/21/muldiv
function fullMulDiv(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// 512-bit multiply `[p1 p0] = x * y`.
// Compute the product mod `2**256` and mod `2**256 - 1`
// then use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. The result is stored in two 256
// variables such that `product = p1 * 2**256 + p0`.
// Temporarily use `z` as `p0` to save gas.
z := mul(x, y) // Lower 256 bits of `x * y`.
for {} 1 {} {
// If overflows.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(z, lt(mm, z))) // Upper 256 bits of `x * y`.
/*------------------- 512 by 256 division --------------------*/
// Make division exact by subtracting the remainder from `[p1 p0]`.
let r := mulmod(x, y, d) // Compute remainder using mulmod.
let t := and(d, sub(0, d)) // The least significant bit of `d`. `t >= 1`.
// Make sure `z` is less than `2**256`. Also prevents `d == 0`.
// Placing the check here seems to give more optimal stack operations.
if iszero(gt(d, p1)) {
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
d := div(d, t) // Divide `d` by `t`, which is a power of two.
// Invert `d mod 2**256`
// Now that `d` is an odd number, it has an inverse
// modulo `2**256` such that `d * inv = 1 mod 2**256`.
// Compute the inverse by starting with a seed that is correct
// correct for four bits. That is, `d * inv = 1 mod 2**4`.
let inv := xor(2, mul(3, d))
// Now use Newton-Raphson iteration to improve the precision.
// Thanks to Hensel's lifting lemma, this also works in modular
// arithmetic, doubling the correct bits in each step.
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**8
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**16
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**32
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**64
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**128
z :=
mul(
// Divide [p1 p0] by the factors of two.
// Shift in bits from `p1` into `p0`. For this we need
// to flip `t` such that it is `2**256 / t`.
or(mul(sub(p1, gt(r, z)), add(div(sub(0, t), t), 1)), div(sub(z, r), t)),
mul(sub(2, mul(d, inv)), inv) // inverse mod 2**256
)
break
}
z := div(z, d)
break
}
}
}
/// @dev Calculates `floor(x * y / d)` with full precision.
/// Behavior is undefined if `d` is zero or the final result cannot fit in 256 bits.
/// Performs the full 512 bit calculation regardless.
function fullMulDivUnchecked(uint256 x, uint256 y, uint256 d)
internal
pure
returns (uint256 z)
{
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(z, lt(mm, z)))
let t := and(d, sub(0, d))
let r := mulmod(x, y, d)
d := div(d, t)
let inv := xor(2, mul(3, d))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
z :=
mul(
or(mul(sub(p1, gt(r, z)), add(div(sub(0, t), t), 1)), div(sub(z, r), t)),
mul(sub(2, mul(d, inv)), inv)
)
}
}
/// @dev Calculates `floor(x * y / d)` with full precision, rounded up.
/// Throws if result overflows a uint256 or when `d` is zero.
/// Credit to Uniswap-v3-core under MIT license:
/// https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/FullMath.sol
function fullMulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
z = fullMulDiv(x, y, d);
/// @solidity memory-safe-assembly
assembly {
if mulmod(x, y, d) {
z := add(z, 1)
if iszero(z) {
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
}
}
}
/// @dev Calculates `floor(x * y / 2 ** n)` with full precision.
/// Throws if result overflows a uint256.
/// Credit to Philogy under MIT license:
/// https://github.com/SorellaLabs/angstrom/blob/main/contracts/src/libraries/X128MathLib.sol
function fullMulDivN(uint256 x, uint256 y, uint8 n) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Temporarily use `z` as `p0` to save gas.
z := mul(x, y) // Lower 256 bits of `x * y`. We'll call this `z`.
for {} 1 {} {
if iszero(or(iszero(x), eq(div(z, x), y))) {
let k := and(n, 0xff) // `n`, cleaned.
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(z, lt(mm, z))) // Upper 256 bits of `x * y`.
// | p1 | z |
// Before: | p1_0 ¦ p1_1 | z_0 ¦ z_1 |
// Final: | 0 ¦ p1_0 | p1_1 ¦ z_0 |
// Check that final `z` doesn't overflow by checking that p1_0 = 0.
if iszero(shr(k, p1)) {
z := add(shl(sub(256, k), p1), shr(k, z))
break
}
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
z := shr(and(n, 0xff), z)
break
}
}
}
/// @dev Returns `floor(x * y / d)`.
/// Reverts if `x * y` overflows, or `d` is zero.
function mulDiv(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(d != 0 && (y == 0 || x <= type(uint256).max / y))`.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
mstore(0x00, 0xad251c27) // `MulDivFailed()`.
revert(0x1c, 0x04)
}
z := div(z, d)
}
}
/// @dev Returns `ceil(x * y / d)`.
/// Reverts if `x * y` overflows, or `d` is zero.
function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(d != 0 && (y == 0 || x <= type(uint256).max / y))`.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
mstore(0x00, 0xad251c27) // `MulDivFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(z, d))), div(z, d))
}
}
/// @dev Returns `x`, the modular multiplicative inverse of `a`, such that `(a * x) % n == 1`.
function invMod(uint256 a, uint256 n) internal pure returns (uint256 x) {
/// @solidity memory-safe-assembly
assembly {
let g := n
let r := mod(a, n)
for { let y := 1 } 1 {} {
let q := div(g, r)
let t := g
g := r
r := sub(t, mul(r, q))
let u := x
x := y
y := sub(u, mul(y, q))
if iszero(r) { break }
}
x := mul(eq(g, 1), add(x, mul(slt(x, 0), n)))
}
}
/// @dev Returns `ceil(x / d)`.
/// Reverts if `d` is zero.
function divUp(uint256 x, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
if iszero(d) {
mstore(0x00, 0x65244e4e) // `DivFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(x, d))), div(x, d))
}
}
/// @dev Returns `max(0, x - y)`. Alias for `saturatingSub`.
function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(gt(x, y), sub(x, y))
}
}
/// @dev Returns `max(0, x - y)`.
function saturatingSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(gt(x, y), sub(x, y))
}
}
/// @dev Returns `min(2 ** 256 - 1, x + y)`.
function saturatingAdd(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(sub(0, lt(add(x, y), x)), add(x, y))
}
}
/// @dev Returns `min(2 ** 256 - 1, x * y)`.
function saturatingMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(sub(or(iszero(x), eq(div(mul(x, y), x), y)), 1), mul(x, y))
}
}
/// @dev Returns `condition ? x : y`, without branching.
function ternary(bool condition, uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), iszero(condition)))
}
}
/// @dev Returns `condition ? x : y`, without branching.
function ternary(bool condition, bytes32 x, bytes32 y) internal pure returns (bytes32 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), iszero(condition)))
}
}
/// @dev Returns `condition ? x : y`, without branching.
function ternary(bool condition, address x, address y) internal pure returns (address z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), iszero(condition)))
}
}
/// @dev Returns `x != 0 ? x : y`, without branching.
function coalesce(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(x, mul(y, iszero(x)))
}
}
/// @dev Returns `x != bytes32(0) ? x : y`, without branching.
function coalesce(bytes32 x, bytes32 y) internal pure returns (bytes32 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(x, mul(y, iszero(x)))
}
}
/// @dev Returns `x != address(0) ? x : y`, without branching.
function coalesce(address x, address y) internal pure returns (address z) {
/// @solidity memory-safe-assembly
assembly {
z := or(x, mul(y, iszero(shl(96, x))))
}
}
/// @dev Exponentiate `x` to `y` by squaring, denominated in base `b`.
/// Reverts if the computation overflows.
function rpow(uint256 x, uint256 y, uint256 b) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(b, iszero(y)) // `0 ** 0 = 1`. Otherwise, `0 ** n = 0`.
if x {
z := xor(b, mul(xor(b, x), and(y, 1))) // `z = isEven(y) ? scale : x`
let half := shr(1, b) // Divide `b` by 2.
// Divide `y` by 2 every iteration.
for { y := shr(1, y) } y { y := shr(1, y) } {
let xx := mul(x, x) // Store x squared.
let xxRound := add(xx, half) // Round to the nearest number.
// Revert if `xx + half` overflowed, or if `x ** 2` overflows.
if or(lt(xxRound, xx), shr(128, x)) {
mstore(0x00, 0x49f7642b) // `RPowOverflow()`.
revert(0x1c, 0x04)
}
x := div(xxRound, b) // Set `x` to scaled `xxRound`.
// If `y` is odd:
if and(y, 1) {
let zx := mul(z, x) // Compute `z * x`.
let zxRound := add(zx, half) // Round to the nearest number.
// If `z * x` overflowed or `zx + half` overflowed:
if or(xor(div(zx, x), z), lt(zxRound, zx)) {
// Revert if `x` is non-zero.
if x {
mstore(0x00, 0x49f7642b) // `RPowOverflow()`.
revert(0x1c, 0x04)
}
}
z := div(zxRound, b) // Return properly scaled `zxRound`.
}
}
}
}
}
/// @dev Returns the square root of `x`, rounded down.
function sqrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// `floor(sqrt(2**15)) = 181`. `sqrt(2**15) - 181 = 2.84`.
z := 181 // The "correct" value is 1, but this saves a multiplication later.
// This segment is to get a reasonable initial estimate for the Babylonian method. With a bad
// start, the correct # of bits increases ~linearly each iteration instead of ~quadratically.
// Let `y = x / 2**r`. We check `y >= 2**(k + 8)`
// but shift right by `k` bits to ensure that if `x >= 256`, then `y >= 256`.
let r := shl(7, lt(0xffffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffffff, shr(r, x))))
z := shl(shr(1, r), z)
// Goal was to get `z*z*y` within a small factor of `x`. More iterations could
// get y in a tighter range. Currently, we will have y in `[256, 256*(2**16))`.
// We ensured `y >= 256` so that the relative difference between `y` and `y+1` is small.
// That's not possible if `x < 256` but we can just verify those cases exhaustively.
// Now, `z*z*y <= x < z*z*(y+1)`, and `y <= 2**(16+8)`, and either `y >= 256`, or `x < 256`.
// Correctness can be checked exhaustively for `x < 256`, so we assume `y >= 256`.
// Then `z*sqrt(y)` is within `sqrt(257)/sqrt(256)` of `sqrt(x)`, or about 20bps.
// For `s` in the range `[1/256, 256]`, the estimate `f(s) = (181/1024) * (s+1)`
// is in the range `(1/2.84 * sqrt(s), 2.84 * sqrt(s))`,
// with largest error when `s = 1` and when `s = 256` or `1/256`.
// Since `y` is in `[256, 256*(2**16))`, let `a = y/65536`, so that `a` is in `[1/256, 256)`.
// Then we can estimate `sqrt(y)` using
// `sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2**18`.
// There is no overflow risk here since `y < 2**136` after the first branch above.
z := shr(18, mul(z, add(shr(r, x), 65536))) // A `mul()` is saved from starting `z` at 181.
// Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough.
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
// If `x+1` is a perfect square, the Babylonian method cycles between
// `floor(sqrt(x))` and `ceil(sqrt(x))`. This statement ensures we return floor.
// See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
z := sub(z, lt(div(x, z), z))
}
}
/// @dev Returns the cube root of `x`, rounded down.
/// Credit to bout3fiddy and pcaversaccio under AGPLv3 license:
/// https://github.com/pcaversaccio/snekmate/blob/main/src/snekmate/utils/math.vy
/// Formally verified by xuwinnie:
/// https://github.com/vectorized/solady/blob/main/audits/xuwinnie-solady-cbrt-proof.pdf
function cbrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
let r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// Makeshift lookup table to nudge the approximate log2 result.
z := div(shl(div(r, 3), shl(lt(0xf, shr(r, x)), 0xf)), xor(7, mod(r, 3)))
// Newton-Raphson's.
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
// Round down.
z := sub(z, lt(div(x, mul(z, z)), z))
}
}
/// @dev Returns the square root of `x`, denominated in `WAD`, rounded down.
function sqrtWad(uint256 x) internal pure returns (uint256 z) {
unchecked {
if (x <= type(uint256).max / 10 ** 18) return sqrt(x * 10 ** 18);
z = (1 + sqrt(x)) * 10 ** 9;
z = (fullMulDivUnchecked(x, 10 ** 18, z) + z) >> 1;
}
/// @solidity memory-safe-assembly
assembly {
z := sub(z, gt(999999999999999999, sub(mulmod(z, z, x), 1))) // Round down.
}
}
/// @dev Returns the cube root of `x`, denominated in `WAD`, rounded down.
/// Formally verified by xuwinnie:
/// https://github.com/vectorized/solady/blob/main/audits/xuwinnie-solady-cbrt-proof.pdf
function cbrtWad(uint256 x) internal pure returns (uint256 z) {
unchecked {
if (x <= type(uint256).max / 10 ** 36) return cbrt(x * 10 ** 36);
z = (1 + cbrt(x)) * 10 ** 12;
z = (fullMulDivUnchecked(x, 10 ** 36, z * z) + z + z) / 3;
}
/// @solidity memory-safe-assembly
assembly {
let p := x
for {} 1 {} {
if iszero(shr(229, p)) {
if iszero(shr(199, p)) {
p := mul(p, 100000000000000000) // 10 ** 17.
break
}
p := mul(p, 100000000) // 10 ** 8.
break
}
if iszero(shr(249, p)) { p := mul(p, 100) }
break
}
let t := mulmod(mul(z, z), z, p)
z := sub(z, gt(lt(t, shr(1, p)), iszero(t))) // Round down.
}
}
/// @dev Returns the factorial of `x`.
function factorial(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := 1
if iszero(lt(x, 58)) {
mstore(0x00, 0xaba0f2a2) // `FactorialOverflow()`.
revert(0x1c, 0x04)
}
for {} x { x := sub(x, 1) } { z := mul(z, x) }
}
}
/// @dev Returns the log2 of `x`.
/// Equivalent to computing the index of the most significant bit (MSB) of `x`.
/// Returns 0 if `x` is zero.
function log2(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// forgefmt: disable-next-item
r := or(r, byte(and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)),
0x0706060506020504060203020504030106050205030304010505030400000000))
}
}
/// @dev Returns the log2 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log2Up(uint256 x) internal pure returns (uint256 r) {
r = log2(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(shl(r, 1), x))
}
}
/// @dev Returns the log10 of `x`.
/// Returns 0 if `x` is zero.
function log10(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
if iszero(lt(x, 100000000000000000000000000000000000000)) {
x := div(x, 100000000000000000000000000000000000000)
r := 38
}
if iszero(lt(x, 100000000000000000000)) {
x := div(x, 100000000000000000000)
r := add(r, 20)
}
if iszero(lt(x, 10000000000)) {
x := div(x, 10000000000)
r := add(r, 10)
}
if iszero(lt(x, 100000)) {
x := div(x, 100000)
r := add(r, 5)
}
r := add(r, add(gt(x, 9), add(gt(x, 99), add(gt(x, 999), gt(x, 9999)))))
}
}
/// @dev Returns the log10 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log10Up(uint256 x) internal pure returns (uint256 r) {
r = log10(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(exp(10, r), x))
}
}
/// @dev Returns the log256 of `x`.
/// Returns 0 if `x` is zero.
function log256(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(shr(3, r), lt(0xff, shr(r, x)))
}
}
/// @dev Returns the log256 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log256Up(uint256 x) internal pure returns (uint256 r) {
r = log256(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(shl(shl(3, r), 1), x))
}
}
/// @dev Returns the scientific notation format `mantissa * 10 ** exponent` of `x`.
/// Useful for compressing prices (e.g. using 25 bit mantissa and 7 bit exponent).
function sci(uint256 x) internal pure returns (uint256 mantissa, uint256 exponent) {
/// @solidity memory-safe-assembly
assembly {
mantissa := x
if mantissa {
if iszero(mod(mantissa, 1000000000000000000000000000000000)) {
mantissa := div(mantissa, 1000000000000000000000000000000000)
exponent := 33
}
if iszero(mod(mantissa, 10000000000000000000)) {
mantissa := div(mantissa, 10000000000000000000)
exponent := add(exponent, 19)
}
if iszero(mod(mantissa, 1000000000000)) {
mantissa := div(mantissa, 1000000000000)
exponent := add(exponent, 12)
}
if iszero(mod(mantissa, 1000000)) {
mantissa := div(mantissa, 1000000)
exponent := add(exponent, 6)
}
if iszero(mod(mantissa, 10000)) {
mantissa := div(mantissa, 10000)
exponent := add(exponent, 4)
}
if iszero(mod(mantissa, 100)) {
mantissa := div(mantissa, 100)
exponent := add(exponent, 2)
}
if iszero(mod(mantissa, 10)) {
mantissa := div(mantissa, 10)
exponent := add(exponent, 1)
}
}
}
}
/// @dev Convenience function for packing `x` into a smaller number using `sci`.
/// The `mantissa` will be in bits [7..255] (the upper 249 bits).
/// The `exponent` will be in bits [0..6] (the lower 7 bits).
/// Use `SafeCastLib` to safely ensure that the `packed` number is small
/// enough to fit in the desired unsigned integer type:
/// ```
/// uint32 packed = SafeCastLib.toUint32(FixedPointMathLib.packSci(777 ether));
/// ```
function packSci(uint256 x) internal pure returns (uint256 packed) {
(x, packed) = sci(x); // Reuse for `mantissa` and `exponent`.
/// @solidity memory-safe-assembly
assembly {
if shr(249, x) {
mstore(0x00, 0xce30380c) // `MantissaOverflow()`.
revert(0x1c, 0x04)
}
packed := or(shl(7, x), packed)
}
}
/// @dev Convenience function for unpacking a packed number from `packSci`.
function unpackSci(uint256 packed) internal pure returns (uint256 unpacked) {
unchecked {
unpacked = (packed >> 7) * 10 ** (packed & 0x7f);
}
}
/// @dev Returns the average of `x` and `y`. Rounds towards zero.
function avg(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = (x & y) + ((x ^ y) >> 1);
}
}
/// @dev Returns the average of `x` and `y`. Rounds towards negative infinity.
function avg(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = (x >> 1) + (y >> 1) + (x & y & 1);
}
}
/// @dev Returns the absolute value of `x`.
function abs(int256 x) internal pure returns (uint256 z) {
unchecked {
z = (uint256(x) + uint256(x >> 255)) ^ uint256(x >> 255);
}
}
/// @dev Returns the absolute distance between `x` and `y`.
function dist(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(xor(sub(0, gt(x, y)), sub(y, x)), gt(x, y))
}
}
/// @dev Returns the absolute distance between `x` and `y`.
function dist(int256 x, int256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(xor(sub(0, sgt(x, y)), sub(y, x)), sgt(x, y))
}
}
/// @dev Returns the minimum of `x` and `y`.
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), lt(y, x)))
}
}
/// @dev Returns the minimum of `x` and `y`.
function min(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), slt(y, x)))
}
}
/// @dev Returns the maximum of `x` and `y`.
function max(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), gt(y, x)))
}
}
/// @dev Returns the maximum of `x` and `y`.
function max(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), sgt(y, x)))
}
}
/// @dev Returns `x`, bounded to `minValue` and `maxValue`.
function clamp(uint256 x, uint256 minValue, uint256 maxValue)
internal
pure
returns (uint256 z)
{
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, minValue), gt(minValue, x)))
z := xor(z, mul(xor(z, maxValue), lt(maxValue, z)))
}
}
/// @dev Returns `x`, bounded to `minValue` and `maxValue`.
function clamp(int256 x, int256 minValue, int256 maxValue) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, minValue), sgt(minValue, x)))
z := xor(z, mul(xor(z, maxValue), slt(maxValue, z)))
}
}
/// @dev Returns greatest common divisor of `x` and `y`.
function gcd(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
for { z := x } y {} {
let t := y
y := mod(z, y)
z := t
}
}
}
/// @dev Returns `a + (b - a) * (t - begin) / (end - begin)`,
/// with `t` clamped between `begin` and `end` (inclusive).
/// Agnostic to the order of (`a`, `b`) and (`end`, `begin`).
/// If `begins == end`, returns `t <= begin ? a : b`.
function lerp(uint256 a, uint256 b, uint256 t, uint256 begin, uint256 end)
internal
pure
returns (uint256)
{
if (begin > end) (t, begin, end) = (~t, ~begin, ~end);
if (t <= begin) return a;
if (t >= end) return b;
unchecked {
if (b >= a) return a + fullMulDiv(b - a, t - begin, end - begin);
return a - fullMulDiv(a - b, t - begin, end - begin);
}
}
/// @dev Returns `a + (b - a) * (t - begin) / (end - begin)`.
/// with `t` clamped between `begin` and `end` (inclusive).
/// Agnostic to the order of (`a`, `b`) and (`end`, `begin`).
/// If `begins == end`, returns `t <= begin ? a : b`.
function lerp(int256 a, int256 b, int256 t, int256 begin, int256 end)
internal
pure
returns (int256)
{
if (begin > end) (t, begin, end) = (~t, ~begin, ~end);
if (t <= begin) return a;
if (t >= end) return b;
// forgefmt: disable-next-item
unchecked {
if (b >= a) return int256(uint256(a) + fullMulDiv(uint256(b - a),
uint256(t - begin), uint256(end - begin)));
return int256(uint256(a) - fullMulDiv(uint256(a - b),
uint256(t - begin), uint256(end - begin)));
}
}
/// @dev Returns if `x` is an even number. Some people may need this.
function isEven(uint256 x) internal pure returns (bool) {
return x & uint256(1) == uint256(0);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* RAW NUMBER OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns `x + y`, without checking for overflow.
function rawAdd(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x + y;
}
}
/// @dev Returns `x + y`, without checking for overflow.
function rawAdd(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x + y;
}
}
/// @dev Returns `x - y`, without checking for underflow.
function rawSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x - y;
}
}
/// @dev Returns `x - y`, without checking for underflow.
function rawSub(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x - y;
}
}
/// @dev Returns `x * y`, without checking for overflow.
function rawMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x * y;
}
}
/// @dev Returns `x * y`, without checking for overflow.
function rawMul(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x * y;
}
}
/// @dev Returns `x / y`, returning 0 if `y` is zero.
function rawDiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(x, y)
}
}
/// @dev Returns `x / y`, returning 0 if `y` is zero.
function rawSDiv(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(x, y)
}
}
/// @dev Returns `x % y`, returning 0 if `y` is zero.
function rawMod(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mod(x, y)
}
}
/// @dev Returns `x % y`, returning 0 if `y` is zero.
function rawSMod(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := smod(x, y)
}
}
/// @dev Returns `(x + y) % d`, return 0 if `d` if zero.
function rawAddMod(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := addmod(x, y, d)
}
}
/// @dev Returns `(x * y) % d`, return 0 if `d` if zero.
function rawMulMod(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mulmod(x, y, d)
}
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @author Permit2 operations from (https://github.com/Uniswap/permit2/blob/main/src/libraries/Permit2Lib.sol)
///
/// @dev Note:
/// - For ETH transfers, please use `forceSafeTransferETH` for DoS protection.
library SafeTransferLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ETH transfer has failed.
error ETHTransferFailed();
/// @dev The ERC20 `transferFrom` has failed.
error TransferFromFailed();
/// @dev The ERC20 `transfer` has failed.
error TransferFailed();
/// @dev The ERC20 `approve` has failed.
error ApproveFailed();
/// @dev The ERC20 `totalSupply` query has failed.
error TotalSupplyQueryFailed();
/// @dev The Permit2 operation has failed.
error Permit2Failed();
/// @dev The Permit2 amount must be less than `2**160 - 1`.
error Permit2AmountOverflow();
/// @dev The Permit2 approve operation has failed.
error Permit2ApproveFailed();
/// @dev The Permit2 lockdown operation has failed.
error Permit2LockdownFailed();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Suggested gas stipend for contract receiving ETH that disallows any storage writes.
uint256 internal constant GAS_STIPEND_NO_STORAGE_WRITES = 2300;
/// @dev Suggested gas stipend for contract receiving ETH to perform a few
/// storage reads and writes, but low enough to prevent griefing.
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;
/// @dev The unique EIP-712 domain separator for the DAI token contract.
bytes32 internal constant DAI_DOMAIN_SEPARATOR =
0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;
/// @dev The address for the WETH9 contract on Ethereum mainnet.
address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
/// @dev The canonical Permit2 address.
/// [Github](https://github.com/Uniswap/permit2)
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ETH OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// If the ETH transfer MUST succeed with a reasonable gas budget, use the force variants.
//
// The regular variants:
// - Forwards all remaining gas to the target.
// - Reverts if the target reverts.
// - Reverts if the current contract has insufficient balance.
//
// The force variants:
// - Forwards with an optional gas stipend
// (defaults to `GAS_STIPEND_NO_GRIEF`, which is sufficient for most cases).
// - If the target reverts, or if the gas stipend is exhausted,
// creates a temporary contract to force send the ETH via `SELFDESTRUCT`.
// Future compatible with `SENDALL`: https://eips.ethereum.org/EIPS/eip-4758.
// - Reverts if the current contract has insufficient balance.
//
// The try variants:
// - Forwards with a mandatory gas stipend.
// - Instead of reverting, returns whether the transfer succeeded.
/// @dev Sends `amount` (in wei) ETH to `to`.
function safeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gas(), to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Sends all the ETH in the current contract to `to`.
function safeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// Transfer all the ETH and check if it succeeded or not.
if iszero(call(gas(), to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function forceSafeTransferETH(address to, uint256 amount, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with a `gasStipend`.
function forceSafeTransferAllETH(address to, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(GAS_STIPEND_NO_GRIEF, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// forgefmt: disable-next-item
if iszero(call(GAS_STIPEND_NO_GRIEF, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function trySafeTransferETH(address to, uint256 amount, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)
}
}
/// @dev Sends all the ETH in the current contract to `to`, with a `gasStipend`.
function trySafeTransferAllETH(address to, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for
/// the current contract to manage.
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function trySafeTransferFrom(address token, address from, address to, uint256 amount)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
success := lt(or(iszero(extcodesize(token)), returndatasize()), success)
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends all of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have their entire balance approved for the current contract to manage.
function safeTransferAllFrom(address token, address from, address to)
internal
returns (uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x60, 0x20)
)
) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
mstore(0x00, 0x23b872dd) // `transferFrom(address,address,uint256)`.
amount := mload(0x60) // The `amount` is already at 0x60. We'll need to return it.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransfer(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sends all of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransferAll(address token, address to) internal returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x70a08231) // Store the function selector of `balanceOf(address)`.
mstore(0x20, address()) // Store the address of the current contract.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x34, 0x20)
)
) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
mstore(0x14, to) // Store the `to` argument.
amount := mload(0x34) // The `amount` is already at 0x34. We'll need to return it.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// Reverts upon failure.
function safeApprove(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// If the initial attempt to approve fails, attempts to reset the approved amount to zero,
/// then retries the approval again (some tokens, e.g. USDT, requires this).
/// Reverts upon failure.
function safeApproveWithRetry(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
// Perform the approval, retrying upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x34, 0) // Store 0 for the `amount`.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
pop(call(gas(), token, 0, 0x10, 0x44, codesize(), 0x00)) // Reset the approval.
mstore(0x34, amount) // Store back the original `amount`.
// Retry the approval, reverting upon failure.
success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
// Check the `extcodesize` again just in case the token selfdestructs lol.
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Returns the amount of ERC20 `token` owned by `account`.
/// Returns zero if the `token` does not exist.
function balanceOf(address token, address account) internal view returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
amount :=
mul( // The arguments of `mul` are evaluated from right to left.
mload(0x20),
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
)
}
}
/// @dev Performs a `token.balanceOf(account)` check.
/// `implemented` denotes whether the `token` does not implement `balanceOf`.
/// `amount` is zero if the `token` does not implement `balanceOf`.
function checkBalanceOf(address token, address account)
internal
view
returns (bool implemented, uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
implemented :=
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
amount := mul(mload(0x20), implemented)
}
}
/// @dev Returns the total supply of the `token`.
/// Reverts if the token does not exist or does not implement `totalSupply()`.
function totalSupply(address token) internal view returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x18160ddd) // `totalSupply()`.
if iszero(
and(gt(returndatasize(), 0x1f), staticcall(gas(), token, 0x1c, 0x04, 0x00, 0x20))
) {
mstore(0x00, 0x54cd9435) // `TotalSupplyQueryFailed()`.
revert(0x1c, 0x04)
}
result := mload(0x00)
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// If the initial attempt fails, try to use Permit2 to transfer the token.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function safeTransferFrom2(address token, address from, address to, uint256 amount) internal {
if (!trySafeTransferFrom(token, from, to, amount)) {
permit2TransferFrom(token, from, to, amount);
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to` via Permit2.
/// Reverts upon failure.
function permit2TransferFrom(address token, address from, address to, uint256 amount)
internal
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(add(m, 0x74), shr(96, shl(96, token)))
mstore(add(m, 0x54), amount)
mstore(add(m, 0x34), to)
mstore(add(m, 0x20), shl(96, from))
// `transferFrom(address,address,uint160,address)`.
mstore(m, 0x36c78516000000000000000000000000)
let p := PERMIT2
let exists := eq(chainid(), 1)
if iszero(exists) { exists := iszero(iszero(extcodesize(p))) }
if iszero(
and(
call(gas(), p, 0, add(m, 0x10), 0x84, codesize(), 0x00),
lt(iszero(extcodesize(token)), exists) // Token has code and Permit2 exists.
)
) {
mstore(0x00, 0x7939f4248757f0fd) // `TransferFromFailed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(iszero(shr(160, amount))))), 0x04)
}
}
}
/// @dev Permit a user to spend a given amount of
/// another user's tokens via native EIP-2612 permit if possible, falling
/// back to Permit2 if native permit fails or is not implemented on the token.
function permit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
bool success;
/// @solidity memory-safe-assembly
assembly {
for {} shl(96, xor(token, WETH9)) {} {
mstore(0x00, 0x3644e515) // `DOMAIN_SEPARATOR()`.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.
// Gas stipend to limit gas burn for tokens that don't refund gas when
// an non-existing function is called. 5K should be enough for a SLOAD.
staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)
)
) { break }
// After here, we can be sure that token is a contract.
let m := mload(0x40)
mstore(add(m, 0x34), spender)
mstore(add(m, 0x20), shl(96, owner))
mstore(add(m, 0x74), deadline)
if eq(mload(0x00), DAI_DOMAIN_SEPARATOR) {
mstore(0x14, owner)
mstore(0x00, 0x7ecebe00000000000000000000000000) // `nonces(address)`.
mstore(
add(m, 0x94),
lt(iszero(amount), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20))
)
mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`.
// `nonces` is already at `add(m, 0x54)`.
// `amount != 0` is already stored at `add(m, 0x94)`.
mstore(add(m, 0xb4), and(0xff, v))
mstore(add(m, 0xd4), r)
mstore(add(m, 0xf4), s)
success := call(gas(), token, 0, add(m, 0x10), 0x104, codesize(), 0x00)
break
}
mstore(m, 0xd505accf000000000000000000000000) // `IERC20Permit.permit`.
mstore(add(m, 0x54), amount)
mstore(add(m, 0x94), and(0xff, v))
mstore(add(m, 0xb4), r)
mstore(add(m, 0xd4), s)
success := call(gas(), token, 0, add(m, 0x10), 0xe4, codesize(), 0x00)
break
}
}
if (!success) simplePermit2(token, owner, spender, amount, deadline, v, r, s);
}
/// @dev Simple permit on the Permit2 contract.
function simplePermit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0x927da105) // `allowance(address,address,address)`.
{
let addressMask := shr(96, not(0))
mstore(add(m, 0x20), and(addressMask, owner))
mstore(add(m, 0x40), and(addressMask, token))
mstore(add(m, 0x60), and(addressMask, spender))
mstore(add(m, 0xc0), and(addressMask, spender))
}
let p := mul(PERMIT2, iszero(shr(160, amount)))
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x5f), // Returns 3 words: `amount`, `expiration`, `nonce`.
staticcall(gas(), p, add(m, 0x1c), 0x64, add(m, 0x60), 0x60)
)
) {
mstore(0x00, 0x6b836e6b8757f0fd) // `Permit2Failed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(p))), 0x04)
}
mstore(m, 0x2b67b570) // `Permit2.permit` (PermitSingle variant).
// `owner` is already `add(m, 0x20)`.
// `token` is already at `add(m, 0x40)`.
mstore(add(m, 0x60), amount)
mstore(add(m, 0x80), 0xffffffffffff) // `expiration = type(uint48).max`.
// `nonce` is already at `add(m, 0xa0)`.
// `spender` is already at `add(m, 0xc0)`.
mstore(add(m, 0xe0), deadline)
mstore(add(m, 0x100), 0x100) // `signature` offset.
mstore(add(m, 0x120), 0x41) // `signature` length.
mstore(add(m, 0x140), r)
mstore(add(m, 0x160), s)
mstore(add(m, 0x180), shl(248, v))
if iszero( // Revert if token does not have code, or if the call fails.
mul(extcodesize(token), call(gas(), p, 0, add(m, 0x1c), 0x184, codesize(), 0x00))) {
mstore(0x00, 0x6b836e6b) // `Permit2Failed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Approves `spender` to spend `amount` of `token` for `address(this)`.
function permit2Approve(address token, address spender, uint160 amount, uint48 expiration)
internal
{
/// @solidity memory-safe-assembly
assembly {
let addressMask := shr(96, not(0))
let m := mload(0x40)
mstore(m, 0x87517c45) // `approve(address,address,uint160,uint48)`.
mstore(add(m, 0x20), and(addressMask, token))
mstore(add(m, 0x40), and(addressMask, spender))
mstore(add(m, 0x60), and(addressMask, amount))
mstore(add(m, 0x80), and(0xffffffffffff, expiration))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {
mstore(0x00, 0x324f14ae) // `Permit2ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Revokes an approval for `token` and `spender` for `address(this)`.
function permit2Lockdown(address token, address spender) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0xcc53287f) // `Permit2.lockdown`.
mstore(add(m, 0x20), 0x20) // Offset of the `approvals`.
mstore(add(m, 0x40), 1) // `approvals.length`.
mstore(add(m, 0x60), shr(96, shl(96, token)))
mstore(add(m, 0x80), shr(96, shl(96, spender)))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {
mstore(0x00, 0x96b3de23) // `Permit2LockdownFailed()`.
revert(0x1c, 0x04)
}
}
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {SystemEpoch} from "./SystemEpoch.sol";
import {IGauge} from "../interfaces/IGauge.sol";
import {IBribe} from "../interfaces/IBribe.sol";
import {IVoter} from "../interfaces/IVoter.sol";
import {IVotingEscrow} from "../interfaces/IVotingEscrow.sol";
// Bribes pay out rewards for a given pool based on the votes that were received from the user (goes hand in hand with Voter.vote())
contract ExternalBribe is IBribe, UUPSUpgradeable, ReentrancyGuardUpgradeable, SystemEpoch {
/// @notice A checkpoint for marking balance
struct Checkpoint {
uint256 timestamp;
uint256 balanceOf;
}
/// @notice A checkpoint for marking supply
struct SupplyCheckpoint {
uint256 timestamp;
uint256 supply;
}
address public immutable VOTER; // only voter can modify balances (since it only happens on vote())
address public immutable VE; // 天使のたまご
uint256 public constant MAX_REWARD_TOKENS = 16;
uint256 public constant PRECISION = 1e18;
uint256 public totalSupply;
mapping(uint256 => uint256) public balanceOf;
mapping(address => mapping(uint256 => uint256)) public tokenRewardsPerEpoch;
mapping(address => uint256) public periodFinish;
mapping(address => mapping(uint256 => uint256)) public lastEarn;
address[] public rewards;
mapping(address => bool) public isReward;
/// @notice A record of balance checkpoints for each account, by index
mapping(uint256 => mapping(uint256 => Checkpoint)) public checkpoints;
/// @notice The number of checkpoints for each account
mapping(uint256 => uint256) public numCheckpoints;
/// @notice A record of balance checkpoints for each token, by index
mapping(uint256 => SupplyCheckpoint) public supplyCheckpoints;
/// @notice The number of checkpoints
uint256 public supplyNumCheckpoints;
uint256[49] __gap;
event Deposit(address indexed from, uint256 tokenId, uint256 amount);
event Withdraw(address indexed from, uint256 tokenId, uint256 amount);
event NotifyReward(address indexed from, address indexed reward, uint256 epoch, uint256 amount);
event ClaimRewards(address indexed from, address indexed reward, uint256 amount);
error OnlyTeam();
constructor(address _voter) {
VOTER = _voter;
VE = IVoter(_voter)._ve();
_disableInitializers();
}
modifier onlyTeam() {
if (msg.sender != IVotingEscrow(VE).team()) {
revert OnlyTeam();
}
_;
}
function initialize(address[] calldata _allowedRewardTokens) external initializer {
__UUPSUpgradeable_init();
__ReentrancyGuard_init();
for (uint256 i; i < _allowedRewardTokens.length; i++) {
if (_allowedRewardTokens[i] != address(0)) {
isReward[_allowedRewardTokens[i]] = true;
rewards.push(_allowedRewardTokens[i]);
}
}
}
// This is an external function, but internal notation is used since it can only be called "internally" from Gauges
function _deposit(uint256 amount, uint256 tokenId) external {
require(msg.sender == VOTER);
totalSupply += amount;
balanceOf[tokenId] += amount;
_writeCheckpoint(tokenId, balanceOf[tokenId]);
_writeSupplyCheckpoint();
emit Deposit(msg.sender, tokenId, amount);
}
function _withdraw(uint256 amount, uint256 tokenId) external {
require(msg.sender == VOTER);
totalSupply -= amount;
balanceOf[tokenId] -= amount;
_writeCheckpoint(tokenId, balanceOf[tokenId]);
_writeSupplyCheckpoint();
emit Withdraw(msg.sender, tokenId, amount);
}
function getEpochStart(uint256 timestamp) public pure returns (uint256) {
uint256 bribeStart = _bribeStart(timestamp);
uint256 bribeEnd = bribeStart + EPOCH;
return timestamp < bribeEnd ? bribeStart : bribeStart + EPOCH;
}
/**
* @notice Determine the prior balance for an account as of a block number
* @dev Block number must be a finalized block or else this function will revert to prevent misinformation.
* @param tokenId The token of the NFT to check
* @param timestamp The timestamp to get the balance at
* @return The balance the account had as of the given block
*/
function getPriorBalanceIndex(uint256 tokenId, uint256 timestamp) public view returns (uint256) {
uint256 nCheckpoints = numCheckpoints[tokenId];
if (nCheckpoints == 0) {
return 0;
}
// First check most recent balance
if (checkpoints[tokenId][nCheckpoints - 1].timestamp <= timestamp) {
return (nCheckpoints - 1);
}
// Next check implicit zero balance
if (checkpoints[tokenId][0].timestamp > timestamp) {
return 0;
}
uint256 lower = 0;
uint256 upper = nCheckpoints - 1;
while (upper > lower) {
uint256 center = upper - (upper - lower) / 2; // ceil, avoiding overflow
Checkpoint memory cp = checkpoints[tokenId][center];
if (cp.timestamp == timestamp) {
return center;
} else if (cp.timestamp < timestamp) {
lower = center;
} else {
upper = center - 1;
}
}
return lower;
}
function getPriorSupplyIndex(uint256 timestamp) public view returns (uint256) {
uint256 nCheckpoints = supplyNumCheckpoints;
if (nCheckpoints == 0) {
return 0;
}
// First check most recent balance
if (supplyCheckpoints[nCheckpoints - 1].timestamp <= timestamp) {
return (nCheckpoints - 1);
}
// Next check implicit zero balance
if (supplyCheckpoints[0].timestamp > timestamp) {
return 0;
}
uint256 lower = 0;
uint256 upper = nCheckpoints - 1;
while (upper > lower) {
uint256 center = upper - (upper - lower) / 2; // ceil, avoiding overflow
SupplyCheckpoint memory cp = supplyCheckpoints[center];
if (cp.timestamp == timestamp) {
return center;
} else if (cp.timestamp < timestamp) {
lower = center;
} else {
upper = center - 1;
}
}
return lower;
}
function rewardsListLength() external view returns (uint256) {
return rewards.length;
}
// returns the last time the reward was modified or periodFinish if the reward has ended
function lastTimeRewardApplicable(address token) public view returns (uint256) {
return FixedPointMathLib.min(block.timestamp, periodFinish[token]);
}
// allows a user to claim rewards for a given token
function getReward(uint256 tokenId, address[] memory tokens) external nonReentrant {
require(IVotingEscrow(VE).isApprovedOrOwner(msg.sender, tokenId));
for (uint256 i = 0; i < tokens.length; i++) {
uint256 _reward = earned(tokens[i], tokenId);
lastEarn[tokens[i]][tokenId] = block.timestamp;
if (_reward > 0) _safeTransfer(tokens[i], msg.sender, _reward);
emit ClaimRewards(msg.sender, tokens[i], _reward);
}
}
// used by Voter to allow batched reward claims
function getRewardForOwner(uint256 tokenId, address[] memory tokens) external nonReentrant {
require(msg.sender == VOTER);
address _owner = IVotingEscrow(VE).ownerOf(tokenId);
for (uint256 i = 0; i < tokens.length; i++) {
uint256 _reward = earned(tokens[i], tokenId);
lastEarn[tokens[i]][tokenId] = block.timestamp;
if (_reward > 0) _safeTransfer(tokens[i], _owner, _reward);
emit ClaimRewards(_owner, tokens[i], _reward);
}
}
function earned(address token, uint256 tokenId) public view returns (uint256) {
if (numCheckpoints[tokenId] == 0) {
// No deposits => No rewards
return 0;
}
uint256 earnedRewards = 0;
uint256 epochStart = _bribeStart(lastEarn[token][tokenId]);
uint256 index = getPriorBalanceIndex(tokenId, epochStart);
Checkpoint memory lastPoint = checkpoints[tokenId][index];
// Current timestamp or last checkpoint timestamp
epochStart = FixedPointMathLib.max(epochStart, _bribeStart(lastPoint.timestamp));
// A second before the current epoch ends
uint256 epochEnd = epochStart + EPOCH - 1;
uint256 lastClaimableEpoch = (_bribeStart(block.timestamp) - epochStart) / EPOCH;
if (lastClaimableEpoch > 0) {
for (uint256 i = 0; i < lastClaimableEpoch; i++) {
// update index for the current epoch
index = getPriorBalanceIndex(tokenId, epochEnd);
// get checkpoint in this epoch
lastPoint = checkpoints[tokenId][index];
// Get last supply snapshot on epoch or `1` (prevents division by zero)
uint256 supply = FixedPointMathLib.max(supplyCheckpoints[getPriorSupplyIndex(epochEnd)].supply, 1);
// Accumulate earned rewards
earnedRewards +=
(lastPoint.balanceOf * tokenRewardsPerEpoch[token][epochStart]) * PRECISION / supply / PRECISION;
// Advance to next epoch
epochStart += EPOCH;
epochEnd = epochStart + EPOCH - 1;
}
}
return earnedRewards;
}
function left(address token) external view returns (uint256) {
uint256 adjustedTstamp = getEpochStart(block.timestamp);
return tokenRewardsPerEpoch[token][adjustedTstamp];
}
function notifyRewardAmount(address token, uint256 amount) external nonReentrant {
require(amount > 0);
if (!isReward[token]) {
require(IVoter(VOTER).isWhitelisted(token), "bribe tokens must be whitelisted");
require(rewards.length < MAX_REWARD_TOKENS, "too many rewards tokens");
}
// bribes kick in at the start of next bribe period
uint256 adjustedTstamp = getEpochStart(block.timestamp);
uint256 epochRewards = tokenRewardsPerEpoch[token][adjustedTstamp];
_safeTransferFrom(token, msg.sender, address(this), amount);
tokenRewardsPerEpoch[token][adjustedTstamp] = epochRewards + amount;
periodFinish[token] = adjustedTstamp + EPOCH;
if (!isReward[token]) {
isReward[token] = true;
rewards.push(token);
}
emit NotifyReward(msg.sender, token, adjustedTstamp, amount);
}
function swapOutRewardToken(uint256 i, address oldToken, address newToken) external onlyTeam {
require(rewards[i] == oldToken);
isReward[oldToken] = false;
isReward[newToken] = true;
rewards[i] = newToken;
}
function _bribeStart(uint256 timestamp) internal pure returns (uint256) {
return timestamp - (timestamp % (EPOCH));
}
function _writeCheckpoint(uint256 tokenId, uint256 balance) internal {
uint256 _timestamp = block.timestamp;
uint256 _nCheckPoints = numCheckpoints[tokenId];
if (_nCheckPoints > 0 && checkpoints[tokenId][_nCheckPoints - 1].timestamp == _timestamp) {
checkpoints[tokenId][_nCheckPoints - 1].balanceOf = balance;
} else {
checkpoints[tokenId][_nCheckPoints] = Checkpoint(_timestamp, balance);
numCheckpoints[tokenId] = _nCheckPoints + 1;
}
}
function _writeSupplyCheckpoint() internal {
uint256 _nCheckPoints = supplyNumCheckpoints;
uint256 _timestamp = block.timestamp;
if (_nCheckPoints > 0 && supplyCheckpoints[_nCheckPoints - 1].timestamp == _timestamp) {
supplyCheckpoints[_nCheckPoints - 1].supply = totalSupply;
} else {
supplyCheckpoints[_nCheckPoints] = SupplyCheckpoint(_timestamp, totalSupply);
supplyNumCheckpoints = _nCheckPoints + 1;
}
}
function _safeTransfer(address token, address to, uint256 value) internal {
require(token.code.length > 0);
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.transfer.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))));
}
function _safeTransferFrom(address token, address from, address to, uint256 value) internal {
require(token.code.length > 0);
(bool success, bytes memory data) =
token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))));
}
function _authorizeUpgrade(address) internal override onlyTeam {}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
abstract contract SystemEpoch {
uint256 public constant EPOCH = 7 days;
}//// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface IBribe {
function _deposit(uint256 amount, uint256 tokenId) external;
function _withdraw(uint256 amount, uint256 tokenId) external;
function getRewardForOwner(uint256 tokenId, address[] memory tokens) external;
function notifyRewardAmount(address token, uint256 amount) external;
function left(address token) external view returns (uint256);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface ICurveAddressRegistry {
function get_address(uint256 _index) external returns (address);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface ICurveMetaRegistry {
function is_registered(address _pool) external view returns (bool);
function get_n_coins(address _pool) external view returns (uint256);
function get_coins(address _pool) external view returns (address[8] memory);
function get_lp_token(address _pool) external view returns (address);
function get_gauge(address _pool) external view returns (address);
}//// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface IGauge {
function notifyRewardAmount(address token, uint256 amount) external;
function getReward(address account, address[] memory tokens) external;
function left(address token) external view returns (uint256);
function isForPair() external view returns (bool);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface IGaugeProvider {
function deployGauge(uint8 gaugeType, address pool) external returns (address, address, address);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface IMinter {
function updatePeriod() external returns (uint256);
}//// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface IVoter {
function _ve() external view returns (address);
function governor() external view returns (address);
function emergencyCouncil() external view returns (address);
function attachTokenToGauge(uint256 _tokenId, address account) external;
function detachTokenFromGauge(uint256 _tokenId, address account) external;
function emitDeposit(uint256 _tokenId, address account, uint256 amount) external;
function emitWithdraw(uint256 _tokenId, address account, uint256 amount) external;
function isWhitelisted(address token) external view returns (bool);
function notifyRewardAmount(uint256 amount) external;
function distribute(address _gauge) external;
}//// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface IVotingEscrow {
struct Point {
int128 bias;
int128 slope; // # -dweight / dt
uint256 ts;
uint256 blk; // block
}
struct LockedBalance {
int128 amount;
uint256 end;
bool perpetuallyLocked;
}
function token() external view returns (address);
function team() external returns (address);
function epoch() external view returns (uint256);
function point_history(uint256 loc) external view returns (Point memory);
function user_point_history(uint256 tokenId, uint256 loc) external view returns (Point memory);
function user_point_epoch(uint256 tokenId) external view returns (uint256);
function ownerOf(uint256) external view returns (address);
function isApprovedOrOwner(address, uint256) external view returns (bool);
function transferFrom(address, address, uint256) external;
function voting(uint256 tokenId) external;
function abstain(uint256 tokenId) external;
function attach(uint256 tokenId) external;
function detach(uint256 tokenId) external;
function checkpoint() external;
function deposit_for(uint256 tokenId, uint256 value) external;
function create_lock_for(uint256, uint256, address) external returns (uint256);
function balanceOfNFT(uint256) external view returns (uint256);
function totalSupply() external view returns (uint256);
function locked(uint256 id) external view returns (LockedBalance memory);
}{
"evmVersion": "cancun",
"libraries": {},
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"remappings": [
"@openzeppelin/contracts/=node_modules/@openzeppelin/contracts/",
"@openzeppelin/contracts-upgradeable/=node_modules/@openzeppelin/contracts-upgradeable/",
"solady/=node_modules/solady/src/",
"forge-std/=node_modules/forge-std/src/"
],
"viaIR": true
}Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[{"internalType":"address","name":"__ve","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"PoolIsNotRegistered","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"weight","type":"uint256"}],"name":"Abstained","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"gauge","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Attach","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"lp","type":"address"},{"indexed":true,"internalType":"address","name":"gauge","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"gauge","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Detach","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"gauge","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"DistributeReward","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"gauge","type":"address"},{"indexed":false,"internalType":"address","name":"creator","type":"address"},{"indexed":false,"internalType":"address","name":"internal_bribe","type":"address"},{"indexed":true,"internalType":"address","name":"external_bribe","type":"address"},{"indexed":true,"internalType":"address","name":"pool","type":"address"}],"name":"GaugeCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"gauge","type":"address"}],"name":"GaugeKilled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"gauge","type":"address"}],"name":"GaugeRevived","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"reward","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"NotifyReward","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"voter","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"weight","type":"uint256"}],"name":"Voted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"whitelister","type":"address"},{"indexed":true,"internalType":"address","name":"token","type":"address"}],"name":"Whitelisted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"lp","type":"address"},{"indexed":true,"internalType":"address","name":"gauge","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdraw","type":"event"},{"inputs":[],"name":"EPOCH","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_ve","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"address","name":"account","type":"address"}],"name":"attachTokenToGauge","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"_bribes","type":"address[]"},{"internalType":"address[][]","name":"_tokens","type":"address[][]"},{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"claimBribes","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"_fees","type":"address[]"},{"internalType":"address[][]","name":"_tokens","type":"address[][]"},{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"claimFees","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"_gauges","type":"address[]"},{"internalType":"address[][]","name":"_tokens","type":"address[][]"}],"name":"claimRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"claimable","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint8","name":"_type","type":"uint8"},{"internalType":"address","name":"_pool","type":"address"}],"name":"createGauge","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"address","name":"account","type":"address"}],"name":"detachTokenFromGauge","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"_gauges","type":"address[]"}],"name":"distribute","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_gauge","type":"address"}],"name":"distribute","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"start","type":"uint256"},{"internalType":"uint256","name":"finish","type":"uint256"}],"name":"distribute","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"distribute","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"distro","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"emergencyCouncil","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"emitDeposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"emitWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"external_bribes","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gaugeProvider","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"gauges","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"governor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"_tokens","type":"address[]"},{"internalType":"address","name":"_minter","type":"address"},{"internalType":"address","name":"_gaugeProvider","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"internal_bribes","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isAlive","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isGauge","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isWhitelisted","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_gauge","type":"address"}],"name":"killGauge","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"lastVoted","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"length","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"notifyRewardAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"poke","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"poolForGauge","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"poolVote","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"pools","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"reset","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_gauge","type":"address"}],"name":"reviveGauge","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_council","type":"address"}],"name":"setEmergencyCouncil","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_governor","type":"address"}],"name":"setGovernor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"totalWeight","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"updateAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"_gauges","type":"address[]"}],"name":"updateFor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"start","type":"uint256"},{"internalType":"uint256","name":"end","type":"uint256"}],"name":"updateForRange","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_gauge","type":"address"}],"name":"updateGauge","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"usedWeights","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"address[]","name":"_poolVote","type":"address[]"},{"internalType":"uint256[]","name":"_weights","type":"uint256[]"}],"name":"vote","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"name":"votes","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"weights","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"bool","name":"_enable","type":"bool"}],"name":"whitelist","outputs":[],"stateMutability":"nonpayable","type":"function"}]Contract Creation Code
60c0806040523461015657602061002e600492612e878038038091610024828561015a565b8339810190610191565b60016012556080819052604051637e062a3560e11b815292839182906001600160a01b03165afa90811561014b575f9161011c575b5060a0525f8054336001600160a01b0319918216811790925560018054821683179055600280549091169091179055604051612cd690816101b18239608051818181610b7701528181610f300152818161130d0152818161182201528181611bde0152818161223901526127d2015260a05181818161078b0152818161094c015281816109cf01528181610c7f01528181611072015281816113f60152818161157d015281816118de01528181611fc60152612bf60152f35b61013e915060203d602011610144575b610136818361015a565b810190610191565b5f610063565b503d61012c565b6040513d5f823e3d90fd5b5f80fd5b601f909101601f19168101906001600160401b0382119082101761017d57604052565b634e487b7160e01b5f52604160045260245ffd5b9081602091031261015657516001600160a01b0381168103610156579056fe6080806040526004361015610012575f80fd5b5f3560e01c90816306d6a1b214611e17575080630754617214611df05780630c340a2414611dc85780631703e5f914611d8b5780631f7b6d3214611d6e57806320b1cb6f14611c8f578063310bd74b14611b7257806332145f9014611a775780633af32abf14611a3a5780633c6b16ab146118c8578063402914f514611890578063411b1f77146117ae57806347b3c6ba146103eb57806353d78693146117555780636138889b1461154b57806363453ae11461137b578063666256aa1461105d578063698473e3146112845780636ecbe38a146112635780637625391a146110625780637715ee751461105d5780637778960e1461103557806379e938241461100b5780637ac09bf714610e7c5780638a9e992714610ba65780638dd598fb14610b6257806390482d7214610a5e57806396c82e5714610a41578063992a7933146108ae5780639b6a9d721461085a5780639f06247b14610701578063a0dc2758146106e4578063a61c713a1461066d578063a7cac84614610635578063a86a366d14610606578063aa79979b146105c9578063ac4afa3814610587578063ae21c4cb14610547578063b9a09fd514610507578063c42cf535146104be578063d23254b41461047a578063d560b0d714610418578063e499c239146103f0578063e4fc6b6d146103eb578063e586875f146103a2578063ea94ee4414610340578063eae40f2614610300578063f3594be0146102d65763f59c370814610237575f80fd5b346102d25760403660031901126102d257610250611e56565b6024359081151582036102d2576001546001600160a01b031633036102d2576001600160a01b03165f8181526010602052604090205490919060ff166102d257815f52601060205260405f209060ff801983541691151516179055337f6661a7108aecd07864384529117d96c319c1163e3010c01390f6b704726e07de5f80a3005b5f80fd5b346102d25760203660031901126102d2576004355f52600e602052602060405f2054604051908152f35b346102d25760203660031901126102d2576001600160a01b03610321611e56565b165f526008602052602060018060a01b0360405f205416604051908152f35b346102d25761034e36612363565b919091335f52600f60205260ff60405f205416156102d25760405191825260208201527ff341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb5676040339360018060a01b031692a3005b346102d25760203660031901126102d2576103bb611e56565b600254906001600160a01b03821633036102d2576001600160a01b03166001600160a01b03199190911617600255005b611fb1565b346102d2575f3660031901126102d2576003546040516001600160a01b039091168152602090f35b346102d25760203660031901126102d2576004356001600160401b0381116102d257610448903690600401611f15565b5f5b8151811015610478576001906104726001600160a01b0361046b83866123ce565b5116612b44565b0161044a565b005b346102d25760403660031901126102d257610493611e6c565b6004355f52600b60205260405f209060018060a01b03165f52602052602060405f2054604051908152f35b346102d25760203660031901126102d2576104d7611e56565b600154906001600160a01b03821633036102d2576001600160a01b03166001600160a01b03199190911617600155005b346102d25760203660031901126102d2576001600160a01b03610528611e56565b165f526006602052602060018060a01b0360405f205416604051908152f35b346102d25760203660031901126102d2576001600160a01b03610568611e56565b165f526009602052602060018060a01b0360405f205416604051908152f35b346102d25760203660031901126102d2576004356005548110156102d2576105b060209161238d565b905460405160039290921b1c6001600160a01b03168152f35b346102d25760203660031901126102d2576001600160a01b036105ea611e56565b165f52600f602052602060ff60405f2054166040519015158152f35b346102d2576106143661231d565b905f52600c60205260405f2080548210156102d2576020916105b0916123b9565b346102d25760203660031901126102d2576001600160a01b03610656611e56565b165f52600a602052602060405f2054604051908152f35b346102d25761067b36612363565b919091335f52600f60205260ff60405f205416156102d257335f52601160205260ff60405f205416156102d25760405191825260208201527fdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d76040339360018060a01b031692a3005b346102d2575f3660031901126102d257602060405162093a808152f35b346102d25760203660031901126102d25761071a611e56565b61072f60018060a01b036002541633146124d4565b6001600160a01b03165f8181526011602052604090205460ff1661081f575f818152601160209081526040808320805460ff191660011790555163095ea7b360e01b8152600481018490525f19602482015291829060449082907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165af18015610814576107e7575b507fed18e9faa3dccfd8aa45f69c4de40546b2ca9cccc4538a2323531656516db1aa5f80a2005b6108089060203d60201161080d575b6108008183611e82565b81019061249b565b6107c0565b503d6107f6565b6040513d5f823e3d90fd5b60405162461bcd60e51b8152602060048201526013602482015272676175676520616c726561647920616c69766560681b6044820152606490fd5b346102d2576108683661231d565b905b81811061087357005b8061087f60019261238d565b838060a01b0391549060031b1c165f5260066020526108a8828060a01b0360405f205416612b44565b0161086a565b346102d25760203660031901126102d2576108c7611e56565b6108dc60018060a01b036002541633146124d4565b6001600160a01b03165f8181526011602052604090205460ff1615610a0757805f52601160205260405f2060ff198154169055805f52601560205260405f2054806109bd575b5060405163095ea7b360e01b81528160048201525f60248201526020816044815f60018060a01b037f0000000000000000000000000000000000000000000000000000000000000000165af18015610814576109a0575b507f04a5d3f5d80d22d9345acc80618f4a4e7e663cf9e1aed23b57d975acec002ba75f80a2005b6109b89060203d60201161080d576108008183611e82565b610979565b5f546109f391906001600160a01b03167f0000000000000000000000000000000000000000000000000000000000000000612c2f565b805f5260156020525f604081205581610922565b60405162461bcd60e51b815260206004820152601260248201527119d85d59d948185b1c9958591e481919585960721b6044820152606490fd5b346102d2575f3660031901126102d2576020600454604051908152f35b346102d25760603660031901126102d2576004356001600160401b0381116102d257610a8e903690600401611f15565b610a96611e6c565b906044356001600160a01b038116908190036102d2575f546001600160a01b031633036102d2575f5b8251811015610b35576001600160a01b03610ada82856123ce565b511690815f52601060205260ff60405f2054166102d257816001925f52601060205260405f208360ff19825416179055337f6661a7108aecd07864384529117d96c319c1163e3010c01390f6b704726e07de5f80a301610abf565b505f80546001600160a01b03199081166001600160a01b0386161790915560038054909116919091179055005b346102d2575f3660031901126102d2576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b346102d25760403660031901126102d25760043560ff81168091036102d257610bcd611e6c565b6001546001600160a01b031633036102d2576001600160a01b039081165f8181526006602052604090205490929116610e4e5760035460405163e94410b360e01b8152600481019290925260248201839052606090829060449082905f906001600160a01b03165af1918215610814575f915f905f94610df6575b5060405163095ea7b360e01b81526001600160a01b03848116600483018190525f1960248401529490602090839060449082905f907f0000000000000000000000000000000000000000000000000000000000000000165af191821561081457610d4792610dd9575b505f85815260086020908152604080832080546001600160a01b038881166001600160a01b031992831617909255600984528285208054928c169282169290921790915587845260068352818420805482168a17905588845260078352818420805490911688179055600f8252808320805460ff1990811660019081179092556011909352922080549091169091179055612b44565b600554600160401b811015610dc5577fa4d97e9e7c65249b4cd01acb82add613adea98af32daf092366982f0a0d4e4536040602096610d8f84600189960160055560056123b9565b81546001600160a01b0360039290921b82811b199091169089901b179091558251338152958116898701521693a4604051908152f35b634e487b7160e01b5f52604160045260245ffd5b610df19060203d60201161080d576108008183611e82565b610cb1565b92505091506060813d606011610e46575b81610e1460609383611e82565b810103126102d257610e25816124c0565b91610e3e6040610e37602085016124c0565b93016124c0565b929184610c48565b3d9150610e07565b60405162461bcd60e51b815260206004820152600660248201526565786973747360d01b6044820152606490fd5b346102d25760603660031901126102d2576004356024356001600160401b0381116102d257610eaf903690600401612333565b916044356001600160401b0381116102d257610ecf903690600401612333565b93909262093a80420462093a8081029080820462093a801490151715610ff757610f0690845f52600e60205260405f20541061244f565b60405163430c208160e01b8152336004820152602481018490526020816044816001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000165afa908115610814575f91610fd8575b50156102d2578482036102d257610f8691835f52600e6020524260405f20553691611eba565b90610f9084611ea3565b92610f9e6040519485611e82565b848452602084019460051b8101903682116102d257945b818610610fc857505061047893506127af565b8535815260209586019501610fb5565b610ff1915060203d60201161080d576108008183611e82565b86610f60565b634e487b7160e01b5f52601160045260245ffd5b346102d25760203660031901126102d2576004355f52600d602052602060405f2054604051908152f35b346102d2575f3660031901126102d2576002546040516001600160a01b039091168152602090f35b6121b7565b346102d2576110703661231d565b7f0000000000000000000000000000000000000000000000000000000000000000916001600160a01b038316905b8281106110a757005b6110b08161238d565b90546001600160a01b0360039290921b1c81165f90815260066020526040902054601254911691905f19016102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af1801561081457611235575b5061112082612b44565b815f52601560205260405f205491604051634cde602960e11b8152846004820152602081602481855afa908115610814575f91611204575b508311806111f6575b611175575b5060018092506012550161109e565b805f5260156020525f6040812055803b156102d25760405163b66503cf60e01b81526001600160a01b038716600482015260248101849052925f8460448183865af1938415610814576001946111e6575b506040519081525f516020612c815f395f51905f5260203392a385611166565b5f6111f091611e82565b876111c6565b5062093a8083041515611161565b90506020813d821161122d575b8161121e60209383611e82565b810103126102d2575187611158565b3d9150611211565b6020813d821161125b575b8161124d60209383611e82565b810103126102d25751611116565b3d9150611240565b346102d25760203660031901126102d25761047861127f611e56565b612b44565b346102d25760403660031901126102d2576004356112a0611e6c565b90335f52600f60205260ff60405f205416156102d257335f52601160205260ff60405f205416156102d2578061130b575b60405190815233916001600160a01b0316907f60940192810a6fb3bce3fd3e2e3a13fd6ccc7605e963fb87ee971aba829989bd90602090a3005b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316803b156102d2575f809160246040518094819363fbd3a29d60e01b83528760048401525af180156108145761136b575b506112d1565b5f61137591611e82565b82611365565b346102d25760203660031901126102d257611394611e56565b6001601254036102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af180156108145761151c575b506113df81612b44565b60018060a01b0316805f52601560205260405f20547f0000000000000000000000000000000000000000000000000000000000000000604051634cde602960e11b815260018060a01b0382166004820152602081602481875afa908115610814575f916114ea575b508211806114dc575b61145c575b6001601255005b825f5260156020525f6040812055823b156102d25760405163b66503cf60e01b81526001600160a01b03919091166004820152602481018290525f8160448183875af18015610814576114cc575b506040519081525f516020612c815f395f51905f5260203392a3808080611455565b5f6114d691611e82565b826114aa565b5062093a8082041515611450565b90506020813d602011611514575b8161150560209383611e82565b810103126102d2575184611447565b3d91506114f8565b6020813d602011611543575b8161153560209383611e82565b810103126102d257516113d5565b3d9150611528565b346102d25760203660031901126102d2576004356001600160401b0381116102d25761157b903690600401611f15565b7f0000000000000000000000000000000000000000000000000000000000000000906001600160a01b0382165f5b8251811015610478576001600160a01b036115c482856123ce565b5116906001601254036102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af1801561081457611727575b5061161282612b44565b815f52601560205260405f205491604051634cde602960e11b8152846004820152602081602481855afa908115610814575f916116f6575b508311806116e8575b611667575b506001809250601255016115a9565b805f5260156020525f6040812055803b156102d25760405163b66503cf60e01b81526001600160a01b038716600482015260248101849052925f8460448183865af1938415610814576001946116d8575b506040519081525f516020612c815f395f51905f5260203392a385611658565b5f6116e291611e82565b876116b8565b5062093a8083041515611653565b90506020813d821161171f575b8161171060209383611e82565b810103126102d257518761164a565b3d9150611703565b6020813d821161174d575b8161173f60209383611e82565b810103126102d25751611608565b3d9150611732565b346102d2575f3660031901126102d2576005545f5b81811061177357005b8061177f60019261238d565b838060a01b0391549060031b1c165f5260066020526117a8828060a01b0360405f205416612b44565b0161176a565b346102d25760403660031901126102d2576004356117ca611e6c565b90335f52600f60205260ff60405f205416156102d25780611820575b60405190815233916001600160a01b0316907fae268d9aab12f3605f58efd74fd3801fa812b03fdb44317eb70f46dff0e19e2290602090a3005b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316803b156102d2575f8091602460405180948193634c35bec560e11b83528760048401525af1801561081457611880575b506117e6565b5f61188a91611e82565b8261187a565b346102d25760203660031901126102d2576001600160a01b036118b1611e56565b165f526015602052602060405f2054604051908152f35b346102d25760203660031901126102d2576004357f0000000000000000000000000000000000000000000000000000000000000000803b156102d2575f8060405160208101906323b872dd60e01b825233602482015230604482015285606482015260648152611939608482611e82565b519082855af13d15611a33573d6001600160401b038111610dc5576040519061196c601f8201601f191660200183611e82565b81523d5f602083013e5b81611a04575b50156102d257670de0b6b3a76400008202828104670de0b6b3a76400001483151715610ff7576004546119ae9161241e565b806119ef575b506040519182526001600160a01b03169033907ff70d5c697de7ea828df48e5c4573cb2194c659f1901f70110c52b066dcf5082690602090a3005b6119fb906013546124b3565b601355826119b4565b8051801592508215611a19575b50508361197c565b611a2c925060208091830101910161249b565b8380611a11565b6060611976565b346102d25760203660031901126102d2576001600160a01b03611a5b611e56565b165f526010602052602060ff60405f2054166040519015158152f35b346102d25760203660031901126102d257600435805f52600c60205260405f20604051808260208294549384815201905f5260205f20925f5b818110611b50575050611ac592500382611e82565b805190611ad182611ea3565b91611adf6040519384611e82565b808352601f19611aee82611ea3565b013660208501375f5b818110611b09575050610478926127af565b5f858152600b60205260409020600191906001600160a01b03611b2c83876123ce565b5116838060a01b03165f5260205260405f2054611b4982876123ce565b5201611af7565b84546001600160a01b0316835260019485019486945060209093019201611ab0565b346102d25760203660031901126102d25760043562093a80420462093a8081029080820462093a801490151715610ff757611bba90825f52600e60205260405f20541061244f565b60405163430c208160e01b8152336004820152602481018290526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169190602081604481865afa908115610814575f91611c70575b50156102d257805f52600e6020524260405f2055611c3481612525565b813b156102d2575f9160248392604051948593849263c1f0fb9f60e01b845260048401525af1801561081457611c6657005b5f61047891611e82565b611c89915060203d60201161080d576108008183611e82565b83611c17565b346102d25760403660031901126102d2576004356001600160401b0381116102d257611cbf903690600401611f15565b6024356001600160401b0381116102d257611cde903690600401611f33565b905f5b8151811015610478576001600160a01b03611cfc82846123ce565b511690611d0981856123ce565b5191803b156102d257611d445f939184926040519586809481936331279d3d60e01b83523360048401526040602484015260448301906123e2565b03925af191821561081457600192611d5e575b5001611ce1565b5f611d6891611e82565b84611d57565b346102d2575f3660031901126102d2576020600554604051908152f35b346102d25760203660031901126102d2576001600160a01b03611dac611e56565b165f526011602052602060ff60405f2054166040519015158152f35b346102d2575f3660031901126102d2576001546040516001600160a01b039091168152602090f35b346102d2575f3660031901126102d2575f546040516001600160a01b039091168152602090f35b346102d25760203660031901126102d2576020906001600160a01b03611e3b611e56565b165f9081526007835260409020546001600160a01b03168152f35b600435906001600160a01b03821682036102d257565b602435906001600160a01b03821682036102d257565b90601f801991011681019081106001600160401b03821117610dc557604052565b6001600160401b038111610dc55760051b60200190565b9291611ec582611ea3565b93611ed36040519586611e82565b602085848152019260051b81019182116102d257915b818310611ef557505050565b82356001600160a01b03811681036102d257815260209283019201611ee9565b9080601f830112156102d257816020611f3093359101611eba565b90565b9080601f830112156102d2578135611f4a81611ea3565b92611f586040519485611e82565b81845260208085019260051b820101918383116102d25760208201905b838210611f8457505050505090565b81356001600160401b0381116102d257602091611fa687848094880101611f15565b815201910190611f75565b346102d2575f3660031901126102d2576005547f0000000000000000000000000000000000000000000000000000000000000000906001600160a01b0382165f5b828110611ffb57005b6120048161238d565b90546001600160a01b0360039290921b1c81165f90815260066020526040902054601254911691905f19016102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af1801561081457612189575b5061207482612b44565b815f52601560205260405f205491604051634cde602960e11b8152846004820152602081602481855afa908115610814575f91612158575b5083118061214a575b6120c9575b50600180925060125501611ff2565b805f5260156020525f6040812055803b156102d25760405163b66503cf60e01b81526001600160a01b038716600482015260248101849052925f8460448183865af19384156108145760019461213a575b506040519081525f516020612c815f395f51905f5260203392a35f6120ba565b5f61214491611e82565b5f61211a565b5062093a80830415156120b5565b90506020813d8211612181575b8161217260209383611e82565b810103126102d257515f6120ac565b3d9150612165565b6020813d82116121af575b816121a160209383611e82565b810103126102d2575161206a565b3d9150612194565b346102d25760603660031901126102d2576004356001600160401b0381116102d2576121e7903690600401611f15565b6024356001600160401b0381116102d257612206903690600401611f33565b60405163430c208160e01b8152336004820152604480356024830181905292939291906020908290816001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000165afa908115610814575f916122fe575b50156102d2575f5b8251811015610478576001600160a01b0361228c82856123ce565b51169061229981866123ce565b5191803b156102d2576122d45f939184926040519586809481936353c2957d60e11b83528a60048401526040602484015260448301906123e2565b03925af1918215610814576001926122ee575b5001612271565b5f6122f891611e82565b5f6122e7565b612317915060203d60201161080d576108008183611e82565b5f612269565b60409060031901126102d2576004359060243590565b9181601f840112156102d2578235916001600160401b0383116102d2576020808501948460051b0101116102d257565b60609060031901126102d257600435906024356001600160a01b03811681036102d2579060443590565b6005548110156123a55760055f5260205f2001905f90565b634e487b7160e01b5f52603260045260245ffd5b80548210156123a5575f5260205f2001905f90565b80518210156123a55760209160051b010190565b90602080835192838152019201905f5b8181106123ff5750505090565b82516001600160a01b03168452602093840193909201916001016123f2565b8115612428570490565b634e487b7160e01b5f52601260045260245ffd5b81810292918115918404141715610ff757565b1561245657565b60405162461bcd60e51b815260206004820152601e60248201527f544f4b454e5f414c52454144595f564f5445445f544849535f45504f434800006044820152606490fd5b908160209103126102d2575180151581036102d25790565b91908201809211610ff757565b51906001600160a01b03821682036102d257565b156124db57565b60405162461bcd60e51b81526020600482015260156024820152741b9bdd08195b595c99d95b98de4818dbdd5b98da5b605a1b6044820152606490fd5b91908203918211610ff757565b805f52600c60205260405f2080545f915f5b8281106125995750505061254d90600454612518565b600455805f52600d6020525f60408120555f52600c60205260405f208054905f815581612578575050565b5f5260205f20908101905b81811061258e575050565b5f8155600101612583565b6125a381836123b9565b90545f878152600b6020908152604080832060039590951b9390931c6001600160a01b0316808352939052205490811515806125e5575b505050600101612537565b9180969392825f52600660205261260760018060a01b0360405f205416612b44565b825f52600a60205260405f2061261e838254612518565b9055885f52600b60205260405f2060018060a01b0384165f5260205260405f20612649838254612518565b90551561277857505f818152600660209081526040808320546001600160a01b03908116845260089092529091205416803b156102d2575f809160446040518094819363278afc8b60e21b83528c60048401528d60248401525af1801561081457612768575b505f908152600660209081526040808320546001600160a01b0390811684526009909252909120541691823b156102d2575f809360446040518096819363278afc8b60e21b83528b60048401528c60248401525af190811561081457612742876040926001967fa9f3ca5f8a9e1580edb2741e0ba560084ec72e0067ba3423f9e9327a176882db95612758575b506124b3565b965b8151908982526020820152a1905f806125da565b5f61276291611e82565b5f61273c565b5f61277291611e82565b5f6126af565b7fa9f3ca5f8a9e1580edb2741e0ba560084ec72e0067ba3423f9e9327a176882db91506127a9604091600195612518565b96612744565b90916127ba82612525565b82516040516339f890b560e21b8152600481018490527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316929190602081602481875afa908115610814575f91612b12575b505f925f935f975f5b858110612af357505f5b8581106128a35750505050505083612858575b61284791506004546124b3565b6004555f52600d60205260405f2055565b813b156102d2575f809260246040518095819363fd4a77f160e01b83528860048401525af19182156108145761284792612893575b5061283a565b5f61289d91611e82565b5f61288d565b6001600160a01b036128b582846123ce565b5116805f52600660205260018060a01b0360405f205416805f52601160205260ff60405f20541615612ae957805f52600f60205260ff60405f205416612902575b50506001905b01612827565b6129248561291f8986959c9f96612919908b6123ce565b5161243c565b61241e565b988915612adc578b5f52600b60205260405f2060018060a01b0382165f5260205260405f20546102d25761295782612b44565b8b5f52600c60205260405f208054600160401b811015610dc557612980916001820181556123b9565b81549060031b9083821b9160018060a01b03901b1916179055805f52600a60205260405f206129b08b82546124b3565b90558b5f52600b60205260405f209060018060a01b03165f5260205260405f206129db8a82546124b3565b90555f818152600860205260409020546001600160a01b0316803b156102d2575f8c60448c83604051958694859363f320772360e01b8552600485015260248401525af1801561081457612acc575b505f908152600960205260409020546001600160a01b031691823b156102d2575f8b60448b83604051978894859363f320772360e01b8552600485015260248401525af190811561081457612a8c8a8092600196612a929561275857506124b3565b9c6124b3565b97604051908b825260208201527fea66f58e474bc09f580000e81f31b334d171db387d0c6098ba47bd897741679b60403392a2905f6128f6565b5f612ad691611e82565b5f612a2a565b50506001919a97506128fc565b50506001906128fc565b91612b0b600191612b0485876123ce565b51906124b3565b920161281d565b90506020813d602011612b3c575b81612b2d60209383611e82565b810103126102d257515f612814565b3d9150612b20565b6001600160a01b039081165f818152600760209081526040808320549094168252600a905291909120548015612c1c575f828152601460205260409020805460135491829055612b9391612518565b9081612b9e57505050565b670de0b6b3a764000091612bb19161243c565b0490805f52601160205260ff60405f2054165f14612be3575f526015602052612bdf60405f209182546124b3565b9055565b505f54612c1a91906001600160a01b03167f0000000000000000000000000000000000000000000000000000000000000000612c2f565b565b50601354905f52601460205260405f2055565b919060145260345263a9059cbb60601b5f5260205f6044601082855af1908160015f51141615612c62575b50505f603452565b3b153d171015612c73575f80612c5a565b6390b8ec185f526004601cfdfe4fa9693cae526341d334e2862ca2413b2e503f1266255f9e0869fb36e6d89b17a2646970667358221220f5d605f8c8377a32638b17f8f2c423c902f43765a60479cfe89ed89c3b653ee264736f6c634300081c0033000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e23
Deployed Bytecode
0x6080806040526004361015610012575f80fd5b5f3560e01c90816306d6a1b214611e17575080630754617214611df05780630c340a2414611dc85780631703e5f914611d8b5780631f7b6d3214611d6e57806320b1cb6f14611c8f578063310bd74b14611b7257806332145f9014611a775780633af32abf14611a3a5780633c6b16ab146118c8578063402914f514611890578063411b1f77146117ae57806347b3c6ba146103eb57806353d78693146117555780636138889b1461154b57806363453ae11461137b578063666256aa1461105d578063698473e3146112845780636ecbe38a146112635780637625391a146110625780637715ee751461105d5780637778960e1461103557806379e938241461100b5780637ac09bf714610e7c5780638a9e992714610ba65780638dd598fb14610b6257806390482d7214610a5e57806396c82e5714610a41578063992a7933146108ae5780639b6a9d721461085a5780639f06247b14610701578063a0dc2758146106e4578063a61c713a1461066d578063a7cac84614610635578063a86a366d14610606578063aa79979b146105c9578063ac4afa3814610587578063ae21c4cb14610547578063b9a09fd514610507578063c42cf535146104be578063d23254b41461047a578063d560b0d714610418578063e499c239146103f0578063e4fc6b6d146103eb578063e586875f146103a2578063ea94ee4414610340578063eae40f2614610300578063f3594be0146102d65763f59c370814610237575f80fd5b346102d25760403660031901126102d257610250611e56565b6024359081151582036102d2576001546001600160a01b031633036102d2576001600160a01b03165f8181526010602052604090205490919060ff166102d257815f52601060205260405f209060ff801983541691151516179055337f6661a7108aecd07864384529117d96c319c1163e3010c01390f6b704726e07de5f80a3005b5f80fd5b346102d25760203660031901126102d2576004355f52600e602052602060405f2054604051908152f35b346102d25760203660031901126102d2576001600160a01b03610321611e56565b165f526008602052602060018060a01b0360405f205416604051908152f35b346102d25761034e36612363565b919091335f52600f60205260ff60405f205416156102d25760405191825260208201527ff341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb5676040339360018060a01b031692a3005b346102d25760203660031901126102d2576103bb611e56565b600254906001600160a01b03821633036102d2576001600160a01b03166001600160a01b03199190911617600255005b611fb1565b346102d2575f3660031901126102d2576003546040516001600160a01b039091168152602090f35b346102d25760203660031901126102d2576004356001600160401b0381116102d257610448903690600401611f15565b5f5b8151811015610478576001906104726001600160a01b0361046b83866123ce565b5116612b44565b0161044a565b005b346102d25760403660031901126102d257610493611e6c565b6004355f52600b60205260405f209060018060a01b03165f52602052602060405f2054604051908152f35b346102d25760203660031901126102d2576104d7611e56565b600154906001600160a01b03821633036102d2576001600160a01b03166001600160a01b03199190911617600155005b346102d25760203660031901126102d2576001600160a01b03610528611e56565b165f526006602052602060018060a01b0360405f205416604051908152f35b346102d25760203660031901126102d2576001600160a01b03610568611e56565b165f526009602052602060018060a01b0360405f205416604051908152f35b346102d25760203660031901126102d2576004356005548110156102d2576105b060209161238d565b905460405160039290921b1c6001600160a01b03168152f35b346102d25760203660031901126102d2576001600160a01b036105ea611e56565b165f52600f602052602060ff60405f2054166040519015158152f35b346102d2576106143661231d565b905f52600c60205260405f2080548210156102d2576020916105b0916123b9565b346102d25760203660031901126102d2576001600160a01b03610656611e56565b165f52600a602052602060405f2054604051908152f35b346102d25761067b36612363565b919091335f52600f60205260ff60405f205416156102d257335f52601160205260ff60405f205416156102d25760405191825260208201527fdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d76040339360018060a01b031692a3005b346102d2575f3660031901126102d257602060405162093a808152f35b346102d25760203660031901126102d25761071a611e56565b61072f60018060a01b036002541633146124d4565b6001600160a01b03165f8181526011602052604090205460ff1661081f575f818152601160209081526040808320805460ff191660011790555163095ea7b360e01b8152600481018490525f19602482015291829060449082907f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db6001600160a01b03165af18015610814576107e7575b507fed18e9faa3dccfd8aa45f69c4de40546b2ca9cccc4538a2323531656516db1aa5f80a2005b6108089060203d60201161080d575b6108008183611e82565b81019061249b565b6107c0565b503d6107f6565b6040513d5f823e3d90fd5b60405162461bcd60e51b8152602060048201526013602482015272676175676520616c726561647920616c69766560681b6044820152606490fd5b346102d2576108683661231d565b905b81811061087357005b8061087f60019261238d565b838060a01b0391549060031b1c165f5260066020526108a8828060a01b0360405f205416612b44565b0161086a565b346102d25760203660031901126102d2576108c7611e56565b6108dc60018060a01b036002541633146124d4565b6001600160a01b03165f8181526011602052604090205460ff1615610a0757805f52601160205260405f2060ff198154169055805f52601560205260405f2054806109bd575b5060405163095ea7b360e01b81528160048201525f60248201526020816044815f60018060a01b037f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db165af18015610814576109a0575b507f04a5d3f5d80d22d9345acc80618f4a4e7e663cf9e1aed23b57d975acec002ba75f80a2005b6109b89060203d60201161080d576108008183611e82565b610979565b5f546109f391906001600160a01b03167f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db612c2f565b805f5260156020525f604081205581610922565b60405162461bcd60e51b815260206004820152601260248201527119d85d59d948185b1c9958591e481919585960721b6044820152606490fd5b346102d2575f3660031901126102d2576020600454604051908152f35b346102d25760603660031901126102d2576004356001600160401b0381116102d257610a8e903690600401611f15565b610a96611e6c565b906044356001600160a01b038116908190036102d2575f546001600160a01b031633036102d2575f5b8251811015610b35576001600160a01b03610ada82856123ce565b511690815f52601060205260ff60405f2054166102d257816001925f52601060205260405f208360ff19825416179055337f6661a7108aecd07864384529117d96c319c1163e3010c01390f6b704726e07de5f80a301610abf565b505f80546001600160a01b03199081166001600160a01b0386161790915560038054909116919091179055005b346102d2575f3660031901126102d2576040517f000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e236001600160a01b03168152602090f35b346102d25760403660031901126102d25760043560ff81168091036102d257610bcd611e6c565b6001546001600160a01b031633036102d2576001600160a01b039081165f8181526006602052604090205490929116610e4e5760035460405163e94410b360e01b8152600481019290925260248201839052606090829060449082905f906001600160a01b03165af1918215610814575f915f905f94610df6575b5060405163095ea7b360e01b81526001600160a01b03848116600483018190525f1960248401529490602090839060449082905f907f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db165af191821561081457610d4792610dd9575b505f85815260086020908152604080832080546001600160a01b038881166001600160a01b031992831617909255600984528285208054928c169282169290921790915587845260068352818420805482168a17905588845260078352818420805490911688179055600f8252808320805460ff1990811660019081179092556011909352922080549091169091179055612b44565b600554600160401b811015610dc5577fa4d97e9e7c65249b4cd01acb82add613adea98af32daf092366982f0a0d4e4536040602096610d8f84600189960160055560056123b9565b81546001600160a01b0360039290921b82811b199091169089901b179091558251338152958116898701521693a4604051908152f35b634e487b7160e01b5f52604160045260245ffd5b610df19060203d60201161080d576108008183611e82565b610cb1565b92505091506060813d606011610e46575b81610e1460609383611e82565b810103126102d257610e25816124c0565b91610e3e6040610e37602085016124c0565b93016124c0565b929184610c48565b3d9150610e07565b60405162461bcd60e51b815260206004820152600660248201526565786973747360d01b6044820152606490fd5b346102d25760603660031901126102d2576004356024356001600160401b0381116102d257610eaf903690600401612333565b916044356001600160401b0381116102d257610ecf903690600401612333565b93909262093a80420462093a8081029080820462093a801490151715610ff757610f0690845f52600e60205260405f20541061244f565b60405163430c208160e01b8152336004820152602481018490526020816044816001600160a01b037f000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e23165afa908115610814575f91610fd8575b50156102d2578482036102d257610f8691835f52600e6020524260405f20553691611eba565b90610f9084611ea3565b92610f9e6040519485611e82565b848452602084019460051b8101903682116102d257945b818610610fc857505061047893506127af565b8535815260209586019501610fb5565b610ff1915060203d60201161080d576108008183611e82565b86610f60565b634e487b7160e01b5f52601160045260245ffd5b346102d25760203660031901126102d2576004355f52600d602052602060405f2054604051908152f35b346102d2575f3660031901126102d2576002546040516001600160a01b039091168152602090f35b6121b7565b346102d2576110703661231d565b7f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db916001600160a01b038316905b8281106110a757005b6110b08161238d565b90546001600160a01b0360039290921b1c81165f90815260066020526040902054601254911691905f19016102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af1801561081457611235575b5061112082612b44565b815f52601560205260405f205491604051634cde602960e11b8152846004820152602081602481855afa908115610814575f91611204575b508311806111f6575b611175575b5060018092506012550161109e565b805f5260156020525f6040812055803b156102d25760405163b66503cf60e01b81526001600160a01b038716600482015260248101849052925f8460448183865af1938415610814576001946111e6575b506040519081525f516020612c815f395f51905f5260203392a385611166565b5f6111f091611e82565b876111c6565b5062093a8083041515611161565b90506020813d821161122d575b8161121e60209383611e82565b810103126102d2575187611158565b3d9150611211565b6020813d821161125b575b8161124d60209383611e82565b810103126102d25751611116565b3d9150611240565b346102d25760203660031901126102d25761047861127f611e56565b612b44565b346102d25760403660031901126102d2576004356112a0611e6c565b90335f52600f60205260ff60405f205416156102d257335f52601160205260ff60405f205416156102d2578061130b575b60405190815233916001600160a01b0316907f60940192810a6fb3bce3fd3e2e3a13fd6ccc7605e963fb87ee971aba829989bd90602090a3005b7f000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e236001600160a01b0316803b156102d2575f809160246040518094819363fbd3a29d60e01b83528760048401525af180156108145761136b575b506112d1565b5f61137591611e82565b82611365565b346102d25760203660031901126102d257611394611e56565b6001601254036102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af180156108145761151c575b506113df81612b44565b60018060a01b0316805f52601560205260405f20547f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db604051634cde602960e11b815260018060a01b0382166004820152602081602481875afa908115610814575f916114ea575b508211806114dc575b61145c575b6001601255005b825f5260156020525f6040812055823b156102d25760405163b66503cf60e01b81526001600160a01b03919091166004820152602481018290525f8160448183875af18015610814576114cc575b506040519081525f516020612c815f395f51905f5260203392a3808080611455565b5f6114d691611e82565b826114aa565b5062093a8082041515611450565b90506020813d602011611514575b8161150560209383611e82565b810103126102d2575184611447565b3d91506114f8565b6020813d602011611543575b8161153560209383611e82565b810103126102d257516113d5565b3d9150611528565b346102d25760203660031901126102d2576004356001600160401b0381116102d25761157b903690600401611f15565b7f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db906001600160a01b0382165f5b8251811015610478576001600160a01b036115c482856123ce565b5116906001601254036102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af1801561081457611727575b5061161282612b44565b815f52601560205260405f205491604051634cde602960e11b8152846004820152602081602481855afa908115610814575f916116f6575b508311806116e8575b611667575b506001809250601255016115a9565b805f5260156020525f6040812055803b156102d25760405163b66503cf60e01b81526001600160a01b038716600482015260248101849052925f8460448183865af1938415610814576001946116d8575b506040519081525f516020612c815f395f51905f5260203392a385611658565b5f6116e291611e82565b876116b8565b5062093a8083041515611653565b90506020813d821161171f575b8161171060209383611e82565b810103126102d257518761164a565b3d9150611703565b6020813d821161174d575b8161173f60209383611e82565b810103126102d25751611608565b3d9150611732565b346102d2575f3660031901126102d2576005545f5b81811061177357005b8061177f60019261238d565b838060a01b0391549060031b1c165f5260066020526117a8828060a01b0360405f205416612b44565b0161176a565b346102d25760403660031901126102d2576004356117ca611e6c565b90335f52600f60205260ff60405f205416156102d25780611820575b60405190815233916001600160a01b0316907fae268d9aab12f3605f58efd74fd3801fa812b03fdb44317eb70f46dff0e19e2290602090a3005b7f000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e236001600160a01b0316803b156102d2575f8091602460405180948193634c35bec560e11b83528760048401525af1801561081457611880575b506117e6565b5f61188a91611e82565b8261187a565b346102d25760203660031901126102d2576001600160a01b036118b1611e56565b165f526015602052602060405f2054604051908152f35b346102d25760203660031901126102d2576004357f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db803b156102d2575f8060405160208101906323b872dd60e01b825233602482015230604482015285606482015260648152611939608482611e82565b519082855af13d15611a33573d6001600160401b038111610dc5576040519061196c601f8201601f191660200183611e82565b81523d5f602083013e5b81611a04575b50156102d257670de0b6b3a76400008202828104670de0b6b3a76400001483151715610ff7576004546119ae9161241e565b806119ef575b506040519182526001600160a01b03169033907ff70d5c697de7ea828df48e5c4573cb2194c659f1901f70110c52b066dcf5082690602090a3005b6119fb906013546124b3565b601355826119b4565b8051801592508215611a19575b50508361197c565b611a2c925060208091830101910161249b565b8380611a11565b6060611976565b346102d25760203660031901126102d2576001600160a01b03611a5b611e56565b165f526010602052602060ff60405f2054166040519015158152f35b346102d25760203660031901126102d257600435805f52600c60205260405f20604051808260208294549384815201905f5260205f20925f5b818110611b50575050611ac592500382611e82565b805190611ad182611ea3565b91611adf6040519384611e82565b808352601f19611aee82611ea3565b013660208501375f5b818110611b09575050610478926127af565b5f858152600b60205260409020600191906001600160a01b03611b2c83876123ce565b5116838060a01b03165f5260205260405f2054611b4982876123ce565b5201611af7565b84546001600160a01b0316835260019485019486945060209093019201611ab0565b346102d25760203660031901126102d25760043562093a80420462093a8081029080820462093a801490151715610ff757611bba90825f52600e60205260405f20541061244f565b60405163430c208160e01b8152336004820152602481018290526001600160a01b037f000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e23169190602081604481865afa908115610814575f91611c70575b50156102d257805f52600e6020524260405f2055611c3481612525565b813b156102d2575f9160248392604051948593849263c1f0fb9f60e01b845260048401525af1801561081457611c6657005b5f61047891611e82565b611c89915060203d60201161080d576108008183611e82565b83611c17565b346102d25760403660031901126102d2576004356001600160401b0381116102d257611cbf903690600401611f15565b6024356001600160401b0381116102d257611cde903690600401611f33565b905f5b8151811015610478576001600160a01b03611cfc82846123ce565b511690611d0981856123ce565b5191803b156102d257611d445f939184926040519586809481936331279d3d60e01b83523360048401526040602484015260448301906123e2565b03925af191821561081457600192611d5e575b5001611ce1565b5f611d6891611e82565b84611d57565b346102d2575f3660031901126102d2576020600554604051908152f35b346102d25760203660031901126102d2576001600160a01b03611dac611e56565b165f526011602052602060ff60405f2054166040519015158152f35b346102d2575f3660031901126102d2576001546040516001600160a01b039091168152602090f35b346102d2575f3660031901126102d2575f546040516001600160a01b039091168152602090f35b346102d25760203660031901126102d2576020906001600160a01b03611e3b611e56565b165f9081526007835260409020546001600160a01b03168152f35b600435906001600160a01b03821682036102d257565b602435906001600160a01b03821682036102d257565b90601f801991011681019081106001600160401b03821117610dc557604052565b6001600160401b038111610dc55760051b60200190565b9291611ec582611ea3565b93611ed36040519586611e82565b602085848152019260051b81019182116102d257915b818310611ef557505050565b82356001600160a01b03811681036102d257815260209283019201611ee9565b9080601f830112156102d257816020611f3093359101611eba565b90565b9080601f830112156102d2578135611f4a81611ea3565b92611f586040519485611e82565b81845260208085019260051b820101918383116102d25760208201905b838210611f8457505050505090565b81356001600160401b0381116102d257602091611fa687848094880101611f15565b815201910190611f75565b346102d2575f3660031901126102d2576005547f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db906001600160a01b0382165f5b828110611ffb57005b6120048161238d565b90546001600160a01b0360039290921b1c81165f90815260066020526040902054601254911691905f19016102d25760026012555f805460405163541b13ef60e11b81529160209183916004918391906001600160a01b03165af1801561081457612189575b5061207482612b44565b815f52601560205260405f205491604051634cde602960e11b8152846004820152602081602481855afa908115610814575f91612158575b5083118061214a575b6120c9575b50600180925060125501611ff2565b805f5260156020525f6040812055803b156102d25760405163b66503cf60e01b81526001600160a01b038716600482015260248101849052925f8460448183865af19384156108145760019461213a575b506040519081525f516020612c815f395f51905f5260203392a35f6120ba565b5f61214491611e82565b5f61211a565b5062093a80830415156120b5565b90506020813d8211612181575b8161217260209383611e82565b810103126102d257515f6120ac565b3d9150612165565b6020813d82116121af575b816121a160209383611e82565b810103126102d2575161206a565b3d9150612194565b346102d25760603660031901126102d2576004356001600160401b0381116102d2576121e7903690600401611f15565b6024356001600160401b0381116102d257612206903690600401611f33565b60405163430c208160e01b8152336004820152604480356024830181905292939291906020908290816001600160a01b037f000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e23165afa908115610814575f916122fe575b50156102d2575f5b8251811015610478576001600160a01b0361228c82856123ce565b51169061229981866123ce565b5191803b156102d2576122d45f939184926040519586809481936353c2957d60e11b83528a60048401526040602484015260448301906123e2565b03925af1918215610814576001926122ee575b5001612271565b5f6122f891611e82565b5f6122e7565b612317915060203d60201161080d576108008183611e82565b5f612269565b60409060031901126102d2576004359060243590565b9181601f840112156102d2578235916001600160401b0383116102d2576020808501948460051b0101116102d257565b60609060031901126102d257600435906024356001600160a01b03811681036102d2579060443590565b6005548110156123a55760055f5260205f2001905f90565b634e487b7160e01b5f52603260045260245ffd5b80548210156123a5575f5260205f2001905f90565b80518210156123a55760209160051b010190565b90602080835192838152019201905f5b8181106123ff5750505090565b82516001600160a01b03168452602093840193909201916001016123f2565b8115612428570490565b634e487b7160e01b5f52601260045260245ffd5b81810292918115918404141715610ff757565b1561245657565b60405162461bcd60e51b815260206004820152601e60248201527f544f4b454e5f414c52454144595f564f5445445f544849535f45504f434800006044820152606490fd5b908160209103126102d2575180151581036102d25790565b91908201809211610ff757565b51906001600160a01b03821682036102d257565b156124db57565b60405162461bcd60e51b81526020600482015260156024820152741b9bdd08195b595c99d95b98de4818dbdd5b98da5b605a1b6044820152606490fd5b91908203918211610ff757565b805f52600c60205260405f2080545f915f5b8281106125995750505061254d90600454612518565b600455805f52600d6020525f60408120555f52600c60205260405f208054905f815581612578575050565b5f5260205f20908101905b81811061258e575050565b5f8155600101612583565b6125a381836123b9565b90545f878152600b6020908152604080832060039590951b9390931c6001600160a01b0316808352939052205490811515806125e5575b505050600101612537565b9180969392825f52600660205261260760018060a01b0360405f205416612b44565b825f52600a60205260405f2061261e838254612518565b9055885f52600b60205260405f2060018060a01b0384165f5260205260405f20612649838254612518565b90551561277857505f818152600660209081526040808320546001600160a01b03908116845260089092529091205416803b156102d2575f809160446040518094819363278afc8b60e21b83528c60048401528d60248401525af1801561081457612768575b505f908152600660209081526040808320546001600160a01b0390811684526009909252909120541691823b156102d2575f809360446040518096819363278afc8b60e21b83528b60048401528c60248401525af190811561081457612742876040926001967fa9f3ca5f8a9e1580edb2741e0ba560084ec72e0067ba3423f9e9327a176882db95612758575b506124b3565b965b8151908982526020820152a1905f806125da565b5f61276291611e82565b5f61273c565b5f61277291611e82565b5f6126af565b7fa9f3ca5f8a9e1580edb2741e0ba560084ec72e0067ba3423f9e9327a176882db91506127a9604091600195612518565b96612744565b90916127ba82612525565b82516040516339f890b560e21b8152600481018490527f000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e236001600160a01b0316929190602081602481875afa908115610814575f91612b12575b505f925f935f975f5b858110612af357505f5b8581106128a35750505050505083612858575b61284791506004546124b3565b6004555f52600d60205260405f2055565b813b156102d2575f809260246040518095819363fd4a77f160e01b83528860048401525af19182156108145761284792612893575b5061283a565b5f61289d91611e82565b5f61288d565b6001600160a01b036128b582846123ce565b5116805f52600660205260018060a01b0360405f205416805f52601160205260ff60405f20541615612ae957805f52600f60205260ff60405f205416612902575b50506001905b01612827565b6129248561291f8986959c9f96612919908b6123ce565b5161243c565b61241e565b988915612adc578b5f52600b60205260405f2060018060a01b0382165f5260205260405f20546102d25761295782612b44565b8b5f52600c60205260405f208054600160401b811015610dc557612980916001820181556123b9565b81549060031b9083821b9160018060a01b03901b1916179055805f52600a60205260405f206129b08b82546124b3565b90558b5f52600b60205260405f209060018060a01b03165f5260205260405f206129db8a82546124b3565b90555f818152600860205260409020546001600160a01b0316803b156102d2575f8c60448c83604051958694859363f320772360e01b8552600485015260248401525af1801561081457612acc575b505f908152600960205260409020546001600160a01b031691823b156102d2575f8b60448b83604051978894859363f320772360e01b8552600485015260248401525af190811561081457612a8c8a8092600196612a929561275857506124b3565b9c6124b3565b97604051908b825260208201527fea66f58e474bc09f580000e81f31b334d171db387d0c6098ba47bd897741679b60403392a2905f6128f6565b5f612ad691611e82565b5f612a2a565b50506001919a97506128fc565b50506001906128fc565b91612b0b600191612b0485876123ce565b51906124b3565b920161281d565b90506020813d602011612b3c575b81612b2d60209383611e82565b810103126102d257515f612814565b3d9150612b20565b6001600160a01b039081165f818152600760209081526040808320549094168252600a905291909120548015612c1c575f828152601460205260409020805460135491829055612b9391612518565b9081612b9e57505050565b670de0b6b3a764000091612bb19161243c565b0490805f52601160205260ff60405f2054165f14612be3575f526015602052612bdf60405f209182546124b3565b9055565b505f54612c1a91906001600160a01b03167f00000000000000000000000028245ab01298eaef7933bc90d35bd9dbca5c89db612c2f565b565b50601354905f52601460205260405f2055565b919060145260345263a9059cbb60601b5f5260205f6044601082855af1908160015f51141615612c62575b50505f603452565b3b153d171015612c73575f80612c5a565b6390b8ec185f526004601cfdfe4fa9693cae526341d334e2862ca2413b2e503f1266255f9e0869fb36e6d89b17a2646970667358221220f5d605f8c8377a32638b17f8f2c423c902f43765a60479cfe89ed89c3b653ee264736f6c634300081c0033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e23
-----Decoded View---------------
Arg [0] : __ve (address): 0xdB9A1bdc443dd11366b8a6dc8038144eCc4D4E23
-----Encoded View---------------
1 Constructor Arguments found :
Arg [0] : 000000000000000000000000db9a1bdc443dd11366b8a6dc8038144ecc4d4e23
Loading...
Loading
Loading...
Loading
Loading...
Loading
Net Worth in USD
$0.00
Net Worth in HYPE
Multichain Portfolio | 35 Chains
| Chain | Token | Portfolio % | Price | Amount | Value |
|---|
Loading...
Loading
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.