Source Code
Overview
MON Balance
MON Value
$2.19 (@ $0.02/MON)Latest 10 from a total of 10 transactions
| Transaction Hash |
Method
|
Block
|
From
|
To
|
|||||
|---|---|---|---|---|---|---|---|---|---|
| Update Priority ... | 60292354 | 7 days ago | IN | 0 MON | 0.00568646 | ||||
| Update Priority ... | 60292316 | 7 days ago | IN | 0 MON | 0.00568778 | ||||
| Update Commissio... | 60292274 | 7 days ago | IN | 0 MON | 0.01743143 | ||||
| Update Priority ... | 60292235 | 7 days ago | IN | 0 MON | 0.00568778 | ||||
| Update MEV Commi... | 60292210 | 7 days ago | IN | 0 MON | 0.00480646 | ||||
| Update Sh Monad ... | 60292151 | 7 days ago | IN | 0 MON | 0.00567819 | ||||
| Update Sh Monad ... | 54541098 | 33 days ago | IN | 0 MON | 0.0051551 | ||||
| Update Commissio... | 54499802 | 34 days ago | IN | 0 MON | 0.00435141 | ||||
| Update Sh Monad ... | 54496349 | 34 days ago | IN | 0 MON | 0.00554621 | ||||
| Update Commissio... | 54493466 | 34 days ago | IN | 0 MON | 0.01747692 |
Latest 25 internal transactions (View All)
Advanced mode:
| Parent Transaction Hash | Block | From | To | |||
|---|---|---|---|---|---|---|
| 61855070 | 1 hr ago | 13.22067727 MON | ||||
| 61855070 | 1 hr ago | 22.58458924 MON | ||||
| 61855070 | 1 hr ago | 528.80946459 MON | ||||
| 61855070 | 1 hr ago | 234.09779928 MON | ||||
| 61805063 | 7 hrs ago | 9.72994788 MON | ||||
| 61805063 | 7 hrs ago | 12.63305448 MON | ||||
| 61805063 | 7 hrs ago | 293.4633597 MON | ||||
| 61805063 | 7 hrs ago | 72.57766501 MON | ||||
| 61755060 | 12 hrs ago | 16.69902324 MON | ||||
| 61755060 | 12 hrs ago | 29.31172762 MON | ||||
| 61755060 | 12 hrs ago | 686.78243964 MON | ||||
| 61755060 | 12 hrs ago | 315.31760934 MON | ||||
| 61705063 | 18 hrs ago | 10.47815132 MON | ||||
| 61705063 | 18 hrs ago | 17.2019474 MON | ||||
| 61705063 | 18 hrs ago | 402.36858642 MON | ||||
| 61705063 | 18 hrs ago | 168.09490191 MON | ||||
| 61655061 | 23 hrs ago | 5.45964599 MON | ||||
| 61655061 | 23 hrs ago | 10.48886339 MON | ||||
| 61655061 | 23 hrs ago | 246.27307559 MON | ||||
| 61655061 | 23 hrs ago | 125.73043503 MON | ||||
| 61605061 | 29 hrs ago | 10.88090245 MON | ||||
| 61605061 | 29 hrs ago | 21.01679039 MON | ||||
| 61605061 | 29 hrs ago | 493.52206703 MON | ||||
| 61605061 | 29 hrs ago | 253.39719853 MON | ||||
| 61555069 | 35 hrs ago | 10.46922297 MON |
Loading...
Loading
Similar Match Source Code This contract matches the deployed Bytecode of the Source Code for Contract 0x50c6158F...80F1a00E3 The constructor portion of the code might be different and could alter the actual behaviour of the contract
Contract Name:
Coinbase
Compiler Version
v0.8.30+commit.73712a01
Contract Source Code (Solidity Standard Json-Input format)
//SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.28 <0.9.0;
import { SafeTransferLib } from "@solady/utils/SafeTransferLib.sol";
import { SafeCastLib } from "@solady/utils/SafeCastLib.sol";
import { FixedPointMathLib as Math } from "@solady/utils/FixedPointMathLib.sol";
import { ReentrancyGuardTransient } from "@openzeppelin/contracts/utils/ReentrancyGuardTransient.sol";
import { IMonadStaking } from "./interfaces/IMonadStaking.sol";
import { ICoinbase } from "./interfaces/ICoinbase.sol";
import {
STAKING,
MIN_VALIDATOR_DEPOSIT,
MAX_EXTERNAL_REWARD,
SCALE,
TRANSFER_GAS_LIMIT,
STAKING_GAS_BUFFER,
STAKING_GAS_EXTERNAL_REWARD,
STAKING_GAS_GET_VALIDATOR
} from "./Constants.sol";
struct CoinbaseConfig {
address commissionRecipient; // receives validator commission sent in `process()`
uint96 priorityCommissionRate; // as a fraction of 1e18
uint96 donationRate; // as a fraction of 1e18. donations count as validator revenue to shMON
uint96 mevCommissionRate; // as a fraction of 1e18
address authAddress; // the last authAddress of the validator according to staking precompile
}
struct UnpaidBalances {
uint120 commission;
uint120 rewards;
bool alwaysTrue;
}
contract Coinbase is ICoinbase, ReentrancyGuardTransient {
using SafeTransferLib for address;
using SafeCastLib for uint256;
uint64 public immutable VAL_ID;
address public immutable SHMONAD;
address public immutable SELF;
CoinbaseConfig internal s_config;
UnpaidBalances internal s_unpaid;
/// @custom:selector 0x688065ed
error OnlyShMonadCaller();
/// @custom:selector 0x9bfdc6ff
error OnlyAuthAddress();
/// @custom:selector 0x8e238934
error OnlySelfCaller();
/// @custom:selector 0x8fe552e0
error InvalidCommissionRate();
/// @custom:selector 0x57471710
error InvalidDonationRate();
/// @custom:selector 0x0721e756
error AuthAddressNotChanged();
/// @custom:selector 0xd48c2547
error RewardFailed();
/// @custom:selector 0x4cd921bb
error CommissionFailed();
/// @custom:selector 0x77f4312b
error DonationFailed();
/// @custom:selector 0xd8707052
error RecipientCannotBeZeroAddress();
/// @custom:selector 0x19708e71
error ValidatorNotFoundInPrecompile(uint64 validatorId);
/// @custom:selector 0x193baad9
error MEVMustExceedZero();
/// @custom:selector 0x36914845
event PriorityCommissionRateUpdated(uint256 oldCommissionRate, uint256 newCommissionRate);
/// @custom:selector 0xd2bbd200
event MEVCommissionRateUpdated(uint256 oldCommissionRate, uint256 newCommissionRate);
/// @custom:selector 0x8013f423
event DonationRateUpdated(uint256 oldDonationRate, uint256 newDonationRate);
/// @custom:selector 0x2c40de6e
event CommissionRecipientUpdated(address oldRecipient, address newRecipient);
/// @custom:selector 0xc0bb63ce
event AuthAddressUpdated(address oldAuthAddress, address newAuthAddress);
constructor(uint64 valId) {
VAL_ID = valId;
SHMONAD = msg.sender;
SELF = address(this);
(address _authAddress, uint256 _commissionRate) = _getValidator(valId);
s_config = CoinbaseConfig({
commissionRecipient: _authAddress,
priorityCommissionRate: _commissionRate.toUint96(),
donationRate: 0,
mevCommissionRate: _commissionRate.toUint96(),
authAddress: _authAddress
});
s_unpaid.alwaysTrue = true;
emit PriorityCommissionRateUpdated(0, _commissionRate);
emit MEVCommissionRateUpdated(0, _commissionRate);
emit CommissionRecipientUpdated(address(0), _authAddress);
}
receive() external payable { }
modifier onlyAuthAddress() {
require(msg.sender == s_config.authAddress, OnlyAuthAddress());
_;
}
modifier onlyShMonad() {
require(msg.sender == SHMONAD, OnlyShMonadCaller());
_;
}
modifier onlySelf() {
require(msg.sender == SELF, OnlySelfCaller());
_;
}
/// @custom:selector 0xbeac7323
function handleMEVPayable() external payable onlyShMonad nonReentrant {
require(msg.value > 0, MEVMustExceedZero());
uint256 _grossMEV = msg.value;
uint256 _mevCommissionRate = s_config.mevCommissionRate;
uint256 _mevCommissionToValidator = _grossMEV * _mevCommissionRate / SCALE;
uint256 _netMEV = _grossMEV - _mevCommissionToValidator;
UnpaidBalances memory _unpaid = s_unpaid;
_unpaid.commission += _mevCommissionToValidator.toUint120();
_unpaid.rewards += _netMEV.toUint120();
s_unpaid = _unpaid;
}
/// @dev This is called during `_crankValidator()` in ShMonad, so should never revert.
/// @custom:selector 0xc33fb877
function process() external onlyShMonad nonReentrant returns (bool success) {
// Strategy:
// 1) Split current balance (excluding previously unpaid) into commission, donation, rewards.
// 2) Cap rewards to a single externalReward call under the precompile limit; store remainder as unpaid.
// 3) Attempt rewards and commission/donation independently; record unpaid on failure.
CoinbaseConfig memory _config = s_config;
UnpaidBalances memory _unpaid = s_unpaid;
uint256 _unpaidCommission = _unpaid.commission;
uint256 _unpaidRewards = _unpaid.rewards;
bool _updateUnpaidBalances = false;
// All balances other than previously unpaid rewards are accrued priority fees
uint256 _currentBalance = address(this).balance;
// Allow exact-paydown of arrears without requiring extra headroom.
uint256 _arrearsTotal = _unpaidCommission + _unpaidRewards;
if (_currentBalance < _arrearsTotal) return false;
if (_currentBalance == 0) return false;
if (_config.priorityCommissionRate + _config.donationRate > SCALE) return false;
_currentBalance -= _arrearsTotal;
// Calculate the commission
uint256 _validatorCommission = (_currentBalance * _config.priorityCommissionRate / SCALE);
uint256 _shMonadDonation = _currentBalance * _config.donationRate / SCALE;
uint256 _rewardPortion = _currentBalance - _validatorCommission - _shMonadDonation;
_validatorCommission += _unpaidCommission;
_rewardPortion += _unpaidRewards;
// Compute reward payout cap upfront (single externalReward call)
bool _canPayRewards = _rewardPortion >= MIN_VALIDATOR_DEPOSIT;
uint256 _rewardPayout;
uint256 _rewardRemainder;
if (_canPayRewards) {
// stay 1 MON below the externalReward hard limit to avoid reverts
_rewardPayout = Math.min(MAX_EXTERNAL_REWARD - 1e18, _rewardPortion);
_rewardRemainder = _rewardPortion - _rewardPayout;
}
// Attempt rewards (single call, capped) and commission/donation independently; track partial success.
bool _rewardsSucceeded = false;
if (_canPayRewards) {
try this.sendRewardsToDelegates(_rewardPayout) {
_rewardsSucceeded = true;
_unpaidRewards = _rewardRemainder.toUint128();
if (_unpaidRewards > 0 || _unpaid.rewards > 0) _updateUnpaidBalances = true;
} catch {
_unpaidRewards = _rewardPortion.toUint128();
_updateUnpaidBalances = true;
}
} else {
// Not enough to pay out; record and keep success=false for rewards.
_unpaidRewards = _rewardPortion.toUint128();
if (_unpaidRewards > _unpaid.rewards) _updateUnpaidBalances = true;
}
bool _commissionSucceeded = true;
bool _donationSucceeded = true;
if (_validatorCommission > 0 || _shMonadDonation > 0) {
try this.sendCommissionAndDonation(_config.commissionRecipient, _validatorCommission, _shMonadDonation)
returns (bool commissionSucceeded, bool donationSucceeded) {
_commissionSucceeded = commissionSucceeded;
_donationSucceeded = donationSucceeded;
} catch {
_commissionSucceeded = false;
_donationSucceeded = false;
}
if (_commissionSucceeded) {
if (_unpaidCommission > 0) {
_unpaidCommission = 0;
_updateUnpaidBalances = true;
}
} else {
if (_validatorCommission > _unpaidCommission) {
_unpaidCommission = _validatorCommission;
_updateUnpaidBalances = true;
}
}
}
// Success only if all intended payouts succeeded; partial effects are kept via unpaid balances.
success = _rewardsSucceeded && _commissionSucceeded && _donationSucceeded;
if (_updateUnpaidBalances) {
s_unpaid = UnpaidBalances({
commission: _unpaidCommission.toUint120(),
rewards: _unpaidRewards.toUint120(),
alwaysTrue: true
});
}
}
/// @custom:selector 0x97751992
function sendRewardsToDelegates(uint256 rewardPortion) external onlySelf {
bool _sendRewardsSucceeded = _sendRewards(VAL_ID, rewardPortion);
require(_sendRewardsSucceeded, RewardFailed());
}
/// @custom:selector 0x5f69d439
function sendCommissionAndDonation(
address commissionRecipient,
uint256 validatorCommission,
uint256 shMonadDonation
)
external
onlySelf
returns (bool commissionSucceeded, bool donationSucceeded)
{
// Send commission to recipient and donation to shMonad (where it counts as revenue).
// Transfers are independent; failures are reported via return values for unpaid tracking.
commissionSucceeded = true;
donationSucceeded = true;
if (validatorCommission > 0) {
commissionSucceeded = commissionRecipient.trySafeTransferETH(validatorCommission, TRANSFER_GAS_LIMIT);
}
if (shMonadDonation > 0) {
donationSucceeded = SHMONAD.trySafeTransferETH(shMonadDonation, TRANSFER_GAS_LIMIT);
}
}
/// @custom:selector 0x8982e4ee
function updateAuthAddress() external {
address _previousAuthAddress = s_config.authAddress;
(address _newAuthAddress,) = _getValidator(VAL_ID);
require(msg.sender == _previousAuthAddress || msg.sender == _newAuthAddress, OnlyAuthAddress());
require(_previousAuthAddress != _newAuthAddress, AuthAddressNotChanged());
s_config.authAddress = _newAuthAddress;
emit AuthAddressUpdated(_previousAuthAddress, _newAuthAddress);
}
/// @custom:selector 0x33c2c2c1
function updatePriorityCommissionRate(uint256 newCommissionRate) external onlyAuthAddress {
// donation + commission cannot exceed 100%
uint256 _oldDonationRate = s_config.donationRate;
require(newCommissionRate + _oldDonationRate <= SCALE, InvalidCommissionRate());
uint256 _oldCommissionRate = s_config.priorityCommissionRate;
s_config.priorityCommissionRate = newCommissionRate.toUint96();
emit PriorityCommissionRateUpdated(_oldCommissionRate, newCommissionRate);
}
/// @custom:selector 0xcaefb7df
function updateMEVCommissionRate(uint256 newCommissionRate) external onlyAuthAddress {
// commission cannot exceed 100%
require(newCommissionRate <= SCALE, InvalidCommissionRate());
uint256 _oldCommissionRate = s_config.mevCommissionRate;
s_config.mevCommissionRate = newCommissionRate.toUint96();
emit MEVCommissionRateUpdated(_oldCommissionRate, newCommissionRate);
}
/// @custom:selector 0x1056c7fe
function updateShMonadDonationRate(uint256 newDonationRate) external onlyAuthAddress {
// donation + commission cannot exceed 100%
uint256 _oldCommissionRate = s_config.priorityCommissionRate;
require(_oldCommissionRate + newDonationRate <= SCALE, InvalidDonationRate());
uint256 _oldDonationRate = s_config.donationRate;
s_config.donationRate = newDonationRate.toUint96();
emit DonationRateUpdated(_oldDonationRate, newDonationRate);
}
/// @custom:selector 0x6f7cbce4
function updateCommissionRateFromStakingConfig() external onlyAuthAddress {
(, uint256 _newCommissionRate) = _getValidator(VAL_ID);
require(_newCommissionRate <= SCALE, InvalidCommissionRate());
uint256 _oldCommissionRate = s_config.priorityCommissionRate;
uint256 _oldMEVCommissionRate = s_config.mevCommissionRate;
uint256 _oldDonationRate = s_config.donationRate;
uint256 _newDonationRate = _oldDonationRate;
// If new commission rate + old donation rate > SCALE, adjust donation rate downwards
if (_newCommissionRate + _oldDonationRate > SCALE) {
_newDonationRate = SCALE - _newCommissionRate;
}
s_config.priorityCommissionRate = _newCommissionRate.toUint96();
s_config.mevCommissionRate = _newCommissionRate.toUint96();
if (_newDonationRate != _oldDonationRate) {
s_config.donationRate = _newDonationRate.toUint96();
emit DonationRateUpdated(_oldDonationRate, _newDonationRate);
}
emit PriorityCommissionRateUpdated(_oldCommissionRate, _newCommissionRate);
emit MEVCommissionRateUpdated(_oldMEVCommissionRate, _newCommissionRate);
}
/// @custom:selector 0x3f3b061a
function updateCommissionRecipient(address newRecipient) external onlyAuthAddress {
require(newRecipient != address(0), RecipientCannotBeZeroAddress());
address _oldRecipient = s_config.commissionRecipient;
s_config.commissionRecipient = newRecipient;
emit CommissionRecipientUpdated(_oldRecipient, newRecipient);
}
/// @custom:selector 0x15f6194e
function getPriorityCommissionRate() external view returns (uint256) {
return s_config.priorityCommissionRate;
}
/// @custom:selector 0x9afefbbd
function getMEVCommissionRate() external view returns (uint256) {
return s_config.mevCommissionRate;
}
/// @custom:selector 0x4890f108
function getShMonadDonationRate() external view returns (uint256) {
return s_config.donationRate;
}
/// @custom:selector 0x9b296734
function getCommissionRecipient() external view returns (address) {
return s_config.commissionRecipient;
}
/// @custom:selector 0x7c827bc2
function getUnpaidBalances() external view returns (uint256 commission, uint256 rewards) {
UnpaidBalances memory _unpaid = s_unpaid;
return (_unpaid.commission, _unpaid.rewards);
}
/// @custom:selector 0x7c359fe0
function AUTH_ADDRESS() external view returns (address) {
return s_config.authAddress;
}
function _inEpochDelayPeriod() internal returns (bool) {
(, bool _isInEpochDelayPeriod) = STAKING.getEpoch();
return _isInEpochDelayPeriod;
}
function _sendRewards(uint64 validatorId, uint256 rewardAmount) internal returns (bool) {
try STAKING.externalReward{ value: rewardAmount, gas: STAKING_GAS_EXTERNAL_REWARD + STAKING_GAS_BUFFER }(
validatorId
) returns (bool _precompileSuccess) {
return _precompileSuccess;
} catch {
return false;
}
}
function _getValidator(uint64 validatorId) internal returns (address authAddress, uint256 commissionRate) {
// Note: Real precompile returns zeros for missing, mock reverts with UnknownValidator()
try STAKING.getValidator{ gas: STAKING_GAS_GET_VALIDATOR + STAKING_GAS_BUFFER }(validatorId) returns (
address _authAddress,
uint64,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256 _consensusCommissionRate,
uint256,
uint256 _snapshotCommissionRate,
bytes memory,
bytes memory
) {
authAddress = _authAddress;
commissionRate = _inEpochDelayPeriod() ? _snapshotCommissionRate : _consensusCommissionRate;
} catch { }
require(authAddress != address(0), ValidatorNotFoundInPrecompile(validatorId));
}
}// 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;
/// @dev The canonical address of the `SELFDESTRUCT` ETH mover.
/// See: https://gist.github.com/Vectorized/1cb8ad4cf393b1378e08f23f79bd99fa
/// [Etherscan](https://etherscan.io/address/0x00000000000073c48c8055bD43D1A53799176f0D)
address internal constant ETH_MOVER = 0x00000000000073c48c8055bD43D1A53799176f0D;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* 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)
}
}
/// @dev Force transfers ETH to `to`, without triggering the fallback (if any).
/// This method attempts to use a separate contract to send via `SELFDESTRUCT`,
/// and upon failure, deploys a minimal vault to accrue the ETH.
function safeMoveETH(address to, uint256 amount) internal returns (address vault) {
/// @solidity memory-safe-assembly
assembly {
to := shr(96, shl(96, to)) // Clean upper 96 bits.
for { let mover := ETH_MOVER } iszero(eq(to, address())) {} {
let selfBalanceBefore := selfbalance()
if or(lt(selfBalanceBefore, amount), eq(to, mover)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if extcodesize(mover) {
let balanceBefore := balance(to) // Check via delta, in case `SELFDESTRUCT` is bricked.
mstore(0x00, to)
pop(call(gas(), mover, amount, 0x00, 0x20, codesize(), 0x00))
// If `address(to).balance >= amount + balanceBefore`, skip vault workflow.
if iszero(lt(balance(to), add(amount, balanceBefore))) { break }
// Just in case `SELFDESTRUCT` is changed to not revert and do nothing.
if lt(selfBalanceBefore, selfbalance()) { invalid() }
}
let m := mload(0x40)
// If the mover is missing or bricked, deploy a minimal vault
// that withdraws all ETH to `to` when being called only by `to`.
// forgefmt: disable-next-item
mstore(add(m, 0x20), 0x33146025575b600160005260206000f35b3d3d3d3d47335af1601a5760003dfd)
mstore(m, or(to, shl(160, 0x6035600b3d3960353df3fe73)))
// Compute and store the bytecode hash.
mstore8(0x00, 0xff) // Write the prefix.
mstore(0x35, keccak256(m, 0x40))
mstore(0x01, shl(96, address())) // Deployer.
mstore(0x15, 0) // Salt.
vault := keccak256(0x00, 0x55)
pop(call(gas(), vault, amount, codesize(), 0x00, codesize(), 0x00))
// The vault returns a single word on success. Failure reverts with empty data.
if iszero(returndatasize()) {
if iszero(create2(0, m, 0x40, 0)) { revert(codesize(), codesize()) } // For gas estimation.
}
mstore(0x40, m) // Restore the free memory pointer.
break
}
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* 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.4;
/// @notice Safe integer casting library that reverts on overflow.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeCastLib.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeCast.sol)
/// @dev Optimized for runtime gas for very high number of optimizer runs (i.e. >= 1000000).
library SafeCastLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Unable to cast to the target type due to overflow.
error Overflow();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* UNSIGNED INTEGER SAFE CASTING OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Casts `x` to a uint8. Reverts on overflow.
function toUint8(uint256 x) internal pure returns (uint8) {
if (x >= 1 << 8) _revertOverflow();
return uint8(x);
}
/// @dev Casts `x` to a uint16. Reverts on overflow.
function toUint16(uint256 x) internal pure returns (uint16) {
if (x >= 1 << 16) _revertOverflow();
return uint16(x);
}
/// @dev Casts `x` to a uint24. Reverts on overflow.
function toUint24(uint256 x) internal pure returns (uint24) {
if (x >= 1 << 24) _revertOverflow();
return uint24(x);
}
/// @dev Casts `x` to a uint32. Reverts on overflow.
function toUint32(uint256 x) internal pure returns (uint32) {
if (x >= 1 << 32) _revertOverflow();
return uint32(x);
}
/// @dev Casts `x` to a uint40. Reverts on overflow.
function toUint40(uint256 x) internal pure returns (uint40) {
if (x >= 1 << 40) _revertOverflow();
return uint40(x);
}
/// @dev Casts `x` to a uint48. Reverts on overflow.
function toUint48(uint256 x) internal pure returns (uint48) {
if (x >= 1 << 48) _revertOverflow();
return uint48(x);
}
/// @dev Casts `x` to a uint56. Reverts on overflow.
function toUint56(uint256 x) internal pure returns (uint56) {
if (x >= 1 << 56) _revertOverflow();
return uint56(x);
}
/// @dev Casts `x` to a uint64. Reverts on overflow.
function toUint64(uint256 x) internal pure returns (uint64) {
if (x >= 1 << 64) _revertOverflow();
return uint64(x);
}
/// @dev Casts `x` to a uint72. Reverts on overflow.
function toUint72(uint256 x) internal pure returns (uint72) {
if (x >= 1 << 72) _revertOverflow();
return uint72(x);
}
/// @dev Casts `x` to a uint80. Reverts on overflow.
function toUint80(uint256 x) internal pure returns (uint80) {
if (x >= 1 << 80) _revertOverflow();
return uint80(x);
}
/// @dev Casts `x` to a uint88. Reverts on overflow.
function toUint88(uint256 x) internal pure returns (uint88) {
if (x >= 1 << 88) _revertOverflow();
return uint88(x);
}
/// @dev Casts `x` to a uint96. Reverts on overflow.
function toUint96(uint256 x) internal pure returns (uint96) {
if (x >= 1 << 96) _revertOverflow();
return uint96(x);
}
/// @dev Casts `x` to a uint104. Reverts on overflow.
function toUint104(uint256 x) internal pure returns (uint104) {
if (x >= 1 << 104) _revertOverflow();
return uint104(x);
}
/// @dev Casts `x` to a uint112. Reverts on overflow.
function toUint112(uint256 x) internal pure returns (uint112) {
if (x >= 1 << 112) _revertOverflow();
return uint112(x);
}
/// @dev Casts `x` to a uint120. Reverts on overflow.
function toUint120(uint256 x) internal pure returns (uint120) {
if (x >= 1 << 120) _revertOverflow();
return uint120(x);
}
/// @dev Casts `x` to a uint128. Reverts on overflow.
function toUint128(uint256 x) internal pure returns (uint128) {
if (x >= 1 << 128) _revertOverflow();
return uint128(x);
}
/// @dev Casts `x` to a uint136. Reverts on overflow.
function toUint136(uint256 x) internal pure returns (uint136) {
if (x >= 1 << 136) _revertOverflow();
return uint136(x);
}
/// @dev Casts `x` to a uint144. Reverts on overflow.
function toUint144(uint256 x) internal pure returns (uint144) {
if (x >= 1 << 144) _revertOverflow();
return uint144(x);
}
/// @dev Casts `x` to a uint152. Reverts on overflow.
function toUint152(uint256 x) internal pure returns (uint152) {
if (x >= 1 << 152) _revertOverflow();
return uint152(x);
}
/// @dev Casts `x` to a uint160. Reverts on overflow.
function toUint160(uint256 x) internal pure returns (uint160) {
if (x >= 1 << 160) _revertOverflow();
return uint160(x);
}
/// @dev Casts `x` to a uint168. Reverts on overflow.
function toUint168(uint256 x) internal pure returns (uint168) {
if (x >= 1 << 168) _revertOverflow();
return uint168(x);
}
/// @dev Casts `x` to a uint176. Reverts on overflow.
function toUint176(uint256 x) internal pure returns (uint176) {
if (x >= 1 << 176) _revertOverflow();
return uint176(x);
}
/// @dev Casts `x` to a uint184. Reverts on overflow.
function toUint184(uint256 x) internal pure returns (uint184) {
if (x >= 1 << 184) _revertOverflow();
return uint184(x);
}
/// @dev Casts `x` to a uint192. Reverts on overflow.
function toUint192(uint256 x) internal pure returns (uint192) {
if (x >= 1 << 192) _revertOverflow();
return uint192(x);
}
/// @dev Casts `x` to a uint200. Reverts on overflow.
function toUint200(uint256 x) internal pure returns (uint200) {
if (x >= 1 << 200) _revertOverflow();
return uint200(x);
}
/// @dev Casts `x` to a uint208. Reverts on overflow.
function toUint208(uint256 x) internal pure returns (uint208) {
if (x >= 1 << 208) _revertOverflow();
return uint208(x);
}
/// @dev Casts `x` to a uint216. Reverts on overflow.
function toUint216(uint256 x) internal pure returns (uint216) {
if (x >= 1 << 216) _revertOverflow();
return uint216(x);
}
/// @dev Casts `x` to a uint224. Reverts on overflow.
function toUint224(uint256 x) internal pure returns (uint224) {
if (x >= 1 << 224) _revertOverflow();
return uint224(x);
}
/// @dev Casts `x` to a uint232. Reverts on overflow.
function toUint232(uint256 x) internal pure returns (uint232) {
if (x >= 1 << 232) _revertOverflow();
return uint232(x);
}
/// @dev Casts `x` to a uint240. Reverts on overflow.
function toUint240(uint256 x) internal pure returns (uint240) {
if (x >= 1 << 240) _revertOverflow();
return uint240(x);
}
/// @dev Casts `x` to a uint248. Reverts on overflow.
function toUint248(uint256 x) internal pure returns (uint248) {
if (x >= 1 << 248) _revertOverflow();
return uint248(x);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* SIGNED INTEGER SAFE CASTING OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Casts `x` to a int8. Reverts on overflow.
function toInt8(int256 x) internal pure returns (int8) {
unchecked {
if (((1 << 7) + uint256(x)) >> 8 == uint256(0)) return int8(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int16. Reverts on overflow.
function toInt16(int256 x) internal pure returns (int16) {
unchecked {
if (((1 << 15) + uint256(x)) >> 16 == uint256(0)) return int16(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int24. Reverts on overflow.
function toInt24(int256 x) internal pure returns (int24) {
unchecked {
if (((1 << 23) + uint256(x)) >> 24 == uint256(0)) return int24(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int32. Reverts on overflow.
function toInt32(int256 x) internal pure returns (int32) {
unchecked {
if (((1 << 31) + uint256(x)) >> 32 == uint256(0)) return int32(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int40. Reverts on overflow.
function toInt40(int256 x) internal pure returns (int40) {
unchecked {
if (((1 << 39) + uint256(x)) >> 40 == uint256(0)) return int40(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int48. Reverts on overflow.
function toInt48(int256 x) internal pure returns (int48) {
unchecked {
if (((1 << 47) + uint256(x)) >> 48 == uint256(0)) return int48(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int56. Reverts on overflow.
function toInt56(int256 x) internal pure returns (int56) {
unchecked {
if (((1 << 55) + uint256(x)) >> 56 == uint256(0)) return int56(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int64. Reverts on overflow.
function toInt64(int256 x) internal pure returns (int64) {
unchecked {
if (((1 << 63) + uint256(x)) >> 64 == uint256(0)) return int64(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int72. Reverts on overflow.
function toInt72(int256 x) internal pure returns (int72) {
unchecked {
if (((1 << 71) + uint256(x)) >> 72 == uint256(0)) return int72(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int80. Reverts on overflow.
function toInt80(int256 x) internal pure returns (int80) {
unchecked {
if (((1 << 79) + uint256(x)) >> 80 == uint256(0)) return int80(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int88. Reverts on overflow.
function toInt88(int256 x) internal pure returns (int88) {
unchecked {
if (((1 << 87) + uint256(x)) >> 88 == uint256(0)) return int88(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int96. Reverts on overflow.
function toInt96(int256 x) internal pure returns (int96) {
unchecked {
if (((1 << 95) + uint256(x)) >> 96 == uint256(0)) return int96(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int104. Reverts on overflow.
function toInt104(int256 x) internal pure returns (int104) {
unchecked {
if (((1 << 103) + uint256(x)) >> 104 == uint256(0)) return int104(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int112. Reverts on overflow.
function toInt112(int256 x) internal pure returns (int112) {
unchecked {
if (((1 << 111) + uint256(x)) >> 112 == uint256(0)) return int112(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int120. Reverts on overflow.
function toInt120(int256 x) internal pure returns (int120) {
unchecked {
if (((1 << 119) + uint256(x)) >> 120 == uint256(0)) return int120(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int128. Reverts on overflow.
function toInt128(int256 x) internal pure returns (int128) {
unchecked {
if (((1 << 127) + uint256(x)) >> 128 == uint256(0)) return int128(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int136. Reverts on overflow.
function toInt136(int256 x) internal pure returns (int136) {
unchecked {
if (((1 << 135) + uint256(x)) >> 136 == uint256(0)) return int136(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int144. Reverts on overflow.
function toInt144(int256 x) internal pure returns (int144) {
unchecked {
if (((1 << 143) + uint256(x)) >> 144 == uint256(0)) return int144(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int152. Reverts on overflow.
function toInt152(int256 x) internal pure returns (int152) {
unchecked {
if (((1 << 151) + uint256(x)) >> 152 == uint256(0)) return int152(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int160. Reverts on overflow.
function toInt160(int256 x) internal pure returns (int160) {
unchecked {
if (((1 << 159) + uint256(x)) >> 160 == uint256(0)) return int160(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int168. Reverts on overflow.
function toInt168(int256 x) internal pure returns (int168) {
unchecked {
if (((1 << 167) + uint256(x)) >> 168 == uint256(0)) return int168(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int176. Reverts on overflow.
function toInt176(int256 x) internal pure returns (int176) {
unchecked {
if (((1 << 175) + uint256(x)) >> 176 == uint256(0)) return int176(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int184. Reverts on overflow.
function toInt184(int256 x) internal pure returns (int184) {
unchecked {
if (((1 << 183) + uint256(x)) >> 184 == uint256(0)) return int184(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int192. Reverts on overflow.
function toInt192(int256 x) internal pure returns (int192) {
unchecked {
if (((1 << 191) + uint256(x)) >> 192 == uint256(0)) return int192(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int200. Reverts on overflow.
function toInt200(int256 x) internal pure returns (int200) {
unchecked {
if (((1 << 199) + uint256(x)) >> 200 == uint256(0)) return int200(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int208. Reverts on overflow.
function toInt208(int256 x) internal pure returns (int208) {
unchecked {
if (((1 << 207) + uint256(x)) >> 208 == uint256(0)) return int208(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int216. Reverts on overflow.
function toInt216(int256 x) internal pure returns (int216) {
unchecked {
if (((1 << 215) + uint256(x)) >> 216 == uint256(0)) return int216(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int224. Reverts on overflow.
function toInt224(int256 x) internal pure returns (int224) {
unchecked {
if (((1 << 223) + uint256(x)) >> 224 == uint256(0)) return int224(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int232. Reverts on overflow.
function toInt232(int256 x) internal pure returns (int232) {
unchecked {
if (((1 << 231) + uint256(x)) >> 232 == uint256(0)) return int232(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int240. Reverts on overflow.
function toInt240(int256 x) internal pure returns (int240) {
unchecked {
if (((1 << 239) + uint256(x)) >> 240 == uint256(0)) return int240(x);
_revertOverflow();
}
}
/// @dev Casts `x` to a int248. Reverts on overflow.
function toInt248(int256 x) internal pure returns (int248) {
unchecked {
if (((1 << 247) + uint256(x)) >> 248 == uint256(0)) return int248(x);
_revertOverflow();
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* OTHER SAFE CASTING OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Casts `x` to a int8. Reverts on overflow.
function toInt8(uint256 x) internal pure returns (int8) {
if (x >= 1 << 7) _revertOverflow();
return int8(int256(x));
}
/// @dev Casts `x` to a int16. Reverts on overflow.
function toInt16(uint256 x) internal pure returns (int16) {
if (x >= 1 << 15) _revertOverflow();
return int16(int256(x));
}
/// @dev Casts `x` to a int24. Reverts on overflow.
function toInt24(uint256 x) internal pure returns (int24) {
if (x >= 1 << 23) _revertOverflow();
return int24(int256(x));
}
/// @dev Casts `x` to a int32. Reverts on overflow.
function toInt32(uint256 x) internal pure returns (int32) {
if (x >= 1 << 31) _revertOverflow();
return int32(int256(x));
}
/// @dev Casts `x` to a int40. Reverts on overflow.
function toInt40(uint256 x) internal pure returns (int40) {
if (x >= 1 << 39) _revertOverflow();
return int40(int256(x));
}
/// @dev Casts `x` to a int48. Reverts on overflow.
function toInt48(uint256 x) internal pure returns (int48) {
if (x >= 1 << 47) _revertOverflow();
return int48(int256(x));
}
/// @dev Casts `x` to a int56. Reverts on overflow.
function toInt56(uint256 x) internal pure returns (int56) {
if (x >= 1 << 55) _revertOverflow();
return int56(int256(x));
}
/// @dev Casts `x` to a int64. Reverts on overflow.
function toInt64(uint256 x) internal pure returns (int64) {
if (x >= 1 << 63) _revertOverflow();
return int64(int256(x));
}
/// @dev Casts `x` to a int72. Reverts on overflow.
function toInt72(uint256 x) internal pure returns (int72) {
if (x >= 1 << 71) _revertOverflow();
return int72(int256(x));
}
/// @dev Casts `x` to a int80. Reverts on overflow.
function toInt80(uint256 x) internal pure returns (int80) {
if (x >= 1 << 79) _revertOverflow();
return int80(int256(x));
}
/// @dev Casts `x` to a int88. Reverts on overflow.
function toInt88(uint256 x) internal pure returns (int88) {
if (x >= 1 << 87) _revertOverflow();
return int88(int256(x));
}
/// @dev Casts `x` to a int96. Reverts on overflow.
function toInt96(uint256 x) internal pure returns (int96) {
if (x >= 1 << 95) _revertOverflow();
return int96(int256(x));
}
/// @dev Casts `x` to a int104. Reverts on overflow.
function toInt104(uint256 x) internal pure returns (int104) {
if (x >= 1 << 103) _revertOverflow();
return int104(int256(x));
}
/// @dev Casts `x` to a int112. Reverts on overflow.
function toInt112(uint256 x) internal pure returns (int112) {
if (x >= 1 << 111) _revertOverflow();
return int112(int256(x));
}
/// @dev Casts `x` to a int120. Reverts on overflow.
function toInt120(uint256 x) internal pure returns (int120) {
if (x >= 1 << 119) _revertOverflow();
return int120(int256(x));
}
/// @dev Casts `x` to a int128. Reverts on overflow.
function toInt128(uint256 x) internal pure returns (int128) {
if (x >= 1 << 127) _revertOverflow();
return int128(int256(x));
}
/// @dev Casts `x` to a int136. Reverts on overflow.
function toInt136(uint256 x) internal pure returns (int136) {
if (x >= 1 << 135) _revertOverflow();
return int136(int256(x));
}
/// @dev Casts `x` to a int144. Reverts on overflow.
function toInt144(uint256 x) internal pure returns (int144) {
if (x >= 1 << 143) _revertOverflow();
return int144(int256(x));
}
/// @dev Casts `x` to a int152. Reverts on overflow.
function toInt152(uint256 x) internal pure returns (int152) {
if (x >= 1 << 151) _revertOverflow();
return int152(int256(x));
}
/// @dev Casts `x` to a int160. Reverts on overflow.
function toInt160(uint256 x) internal pure returns (int160) {
if (x >= 1 << 159) _revertOverflow();
return int160(int256(x));
}
/// @dev Casts `x` to a int168. Reverts on overflow.
function toInt168(uint256 x) internal pure returns (int168) {
if (x >= 1 << 167) _revertOverflow();
return int168(int256(x));
}
/// @dev Casts `x` to a int176. Reverts on overflow.
function toInt176(uint256 x) internal pure returns (int176) {
if (x >= 1 << 175) _revertOverflow();
return int176(int256(x));
}
/// @dev Casts `x` to a int184. Reverts on overflow.
function toInt184(uint256 x) internal pure returns (int184) {
if (x >= 1 << 183) _revertOverflow();
return int184(int256(x));
}
/// @dev Casts `x` to a int192. Reverts on overflow.
function toInt192(uint256 x) internal pure returns (int192) {
if (x >= 1 << 191) _revertOverflow();
return int192(int256(x));
}
/// @dev Casts `x` to a int200. Reverts on overflow.
function toInt200(uint256 x) internal pure returns (int200) {
if (x >= 1 << 199) _revertOverflow();
return int200(int256(x));
}
/// @dev Casts `x` to a int208. Reverts on overflow.
function toInt208(uint256 x) internal pure returns (int208) {
if (x >= 1 << 207) _revertOverflow();
return int208(int256(x));
}
/// @dev Casts `x` to a int216. Reverts on overflow.
function toInt216(uint256 x) internal pure returns (int216) {
if (x >= 1 << 215) _revertOverflow();
return int216(int256(x));
}
/// @dev Casts `x` to a int224. Reverts on overflow.
function toInt224(uint256 x) internal pure returns (int224) {
if (x >= 1 << 223) _revertOverflow();
return int224(int256(x));
}
/// @dev Casts `x` to a int232. Reverts on overflow.
function toInt232(uint256 x) internal pure returns (int232) {
if (x >= 1 << 231) _revertOverflow();
return int232(int256(x));
}
/// @dev Casts `x` to a int240. Reverts on overflow.
function toInt240(uint256 x) internal pure returns (int240) {
if (x >= 1 << 239) _revertOverflow();
return int240(int256(x));
}
/// @dev Casts `x` to a int248. Reverts on overflow.
function toInt248(uint256 x) internal pure returns (int248) {
if (x >= 1 << 247) _revertOverflow();
return int248(int256(x));
}
/// @dev Casts `x` to a int256. Reverts on overflow.
function toInt256(uint256 x) internal pure returns (int256) {
if (int256(x) >= 0) return int256(x);
_revertOverflow();
}
/// @dev Casts `x` to a uint256. Reverts on overflow.
function toUint256(int256 x) internal pure returns (uint256) {
if (x >= 0) return uint256(x);
_revertOverflow();
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PRIVATE HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
function _revertOverflow() private pure {
/// @solidity memory-safe-assembly
assembly {
// Store the function selector of `Overflow()`.
mstore(0x00, 0x35278d12)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
}
}// 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 `sqrt(x * y)`. Also called the geometric mean.
function mulSqrt(uint256 x, uint256 y) internal pure returns (uint256 z) {
if (x == y) return x;
uint256 p = rawMul(x, y);
if (y == rawDiv(p, x)) return sqrt(p);
for (z = saturatingMul(rawAdd(sqrt(x), 1), rawAdd(sqrt(y), 1));; z = avg(z, p)) {
if ((p = fullMulDivUnchecked(x, y, z)) >= z) break;
}
}
/// @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
// OpenZeppelin Contracts (last updated v5.3.0) (utils/ReentrancyGuardTransient.sol)
pragma solidity ^0.8.24;
import {TransientSlot} from "./TransientSlot.sol";
/**
* @dev Variant of {ReentrancyGuard} that uses transient storage.
*
* NOTE: This variant only works on networks where EIP-1153 is available.
*
* _Available since v5.1._
*/
abstract contract ReentrancyGuardTransient {
using TransientSlot for *;
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant REENTRANCY_GUARD_STORAGE =
0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
/**
* @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 {
// On the first call to nonReentrant, REENTRANCY_GUARD_STORAGE.asBoolean().tload() will be false
if (_reentrancyGuardEntered()) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true);
}
function _nonReentrantAfter() private {
REENTRANCY_GUARD_STORAGE.asBoolean().tstore(false);
}
/**
* @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) {
return REENTRANCY_GUARD_STORAGE.asBoolean().tload();
}
}//SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.28 <0.9.0;
struct ValidatorView {
address authAddress;
uint64 flags;
uint256 executionStake;
uint256 executionAccumulator;
uint256 executionCommission;
uint256 executionUnclaimedRewards;
uint256 consensusStake;
uint256 consensusCommission;
uint256 snapshotStake;
uint256 snapshotCommission;
bytes secpPubkey;
bytes blsPubkey;
}
struct DelInfo {
uint256 stake;
uint256 lastAccumulator;
uint256 rewards;
uint256 deltaStake;
uint256 nextDeltaStake;
uint64 deltaEpoch;
uint64 nextDeltaEpoch;
}
struct WithdrawalRequest {
uint256 amount;
uint256 accumulator;
uint64 epoch;
}
interface IMonadStaking {
function addValidator(
bytes calldata payload,
bytes calldata signedSecpMessage,
bytes calldata signedBlsMessage
)
external
payable
returns (uint64 validatorId);
function delegate(uint64 validatorId) external payable returns (bool success);
function undelegate(uint64 validatorId, uint256 amount, uint8 withdrawId) external returns (bool success);
function compound(uint64 validatorId) external returns (bool success);
function withdraw(uint64 validatorId, uint8 withdrawId) external returns (bool success);
function claimRewards(uint64 validatorId) external returns (bool success);
function changeCommission(uint64 validatorId, uint256 commission) external returns (bool success);
function externalReward(uint64 validatorId) external payable returns (bool success);
function getValidator(uint64 validatorId)
external
returns (
address authAddress,
uint64 flags,
uint256 stake,
uint256 accRewardPerToken,
uint256 commission,
uint256 unclaimedRewards,
uint256 consensusStake,
uint256 consensusCommission,
uint256 snapshotStake,
uint256 snapshotCommission,
bytes memory secpPubkey,
bytes memory blsPubkey
);
function getDelegator(
uint64 validatorId,
address delegator
)
external
returns (
uint256 stake,
uint256 accRewardPerToken,
uint256 unclaimedRewards,
uint256 deltaStake,
uint256 nextDeltaStake,
uint64 deltaEpoch,
uint64 nextDeltaEpoch
);
function getWithdrawalRequest(
uint64 validatorId,
address delegator,
uint8 withdrawId
)
external
returns (uint256 withdrawalAmount, uint256 accRewardPerToken, uint64 withdrawEpoch);
function getConsensusValidatorSet(uint32 startIndex)
external
returns (bool isDone, uint32 nextIndex, uint64[] memory valIds);
function getSnapshotValidatorSet(uint32 startIndex)
external
returns (bool isDone, uint32 nextIndex, uint64[] memory valIds);
function getExecutionValidatorSet(uint32 startIndex)
external
returns (bool isDone, uint32 nextIndex, uint64[] memory valIds);
function getDelegations(
address delegator,
uint64 startValId
)
external
returns (bool isDone, uint64 nextValId, uint64[] memory valIds);
function getDelegators(
uint64 validatorId,
address startDelegator
)
external
returns (bool isDone, address nextDelegator, address[] memory delegators);
function getEpoch() external returns (uint64 epoch, bool inEpochDelayPeriod);
/// @notice Returns the validator ID of the current block proposer/author for this block
/// @dev Temporary method name used by ShMonad to avoid relying on block.coinbase; mocked in tests
function getProposerValId() external returns (uint64 val_id);
function syscallOnEpochChange(uint64 epoch) external;
function syscallReward(address blockAuthor) external;
function syscallSnapshot() external;
// ================================ //
// Constants //
// ================================ //
// NOTE: The precompile has these constants internally but does NOT expose them as view functions.
// Production code uses DUST_THRESHOLD and WITHDRAWAL_DELAY from Constants.sol.
// Test code uses all constants from MockMonadStakingPrecompile.sol:
// - MON = 1e18
// - MIN_VALIDATE_STAKE = 100_000 * 1e18
// - ACTIVE_VALIDATOR_STAKE = 25_000_000 * 1e18
// - UNIT_BIAS = 1e36
// - DUST_THRESHOLD = 1e9 (used in production)
// - MAX_EXTERNAL_REWARD = 1e25
// - WITHDRAWAL_DELAY = 1 (used in production)
// - PAGINATED_RESULTS_SIZE = 100
event ValidatorCreated(uint64 indexed validatorId, address indexed authAddress);
event ValidatorStatusChanged(uint64 indexed validatorId, address indexed authAddress, uint64 flags);
event Delegate(uint64 indexed validatorId, address indexed delegator, uint256 amount, uint64 activationEpoch);
event Undelegate(
uint64 indexed validatorId, address indexed delegator, uint8 withdrawId, uint256 amount, uint64 activationEpoch
);
event Withdraw(
uint64 indexed validatorId, address indexed delegator, uint8 withdrawId, uint256 amount, uint64 withdrawEpoch
);
event ClaimRewards(uint256 indexed validatorId, address indexed delegator, uint256 amount);
event CommissionChanged(uint256 indexed validatorId, uint256 oldCommission, uint256 newCommission);
}//SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.28 <0.9.0;
interface ICoinbase {
// ================================ //
// Constants //
// ================================ //
// Public immutables surfaced via autogenerated getters
function VAL_ID() external view returns (uint64);
function SHMONAD() external view returns (address);
function AUTH_ADDRESS() external view returns (address);
// ================================ //
// Core Function //
// ================================ //
function process() external returns (bool success);
function handleMEVPayable() external payable;
// ================================ //
// onlyAuth Setters //
// ================================ //
function updatePriorityCommissionRate(uint256 newCommissionRate) external;
function updateMEVCommissionRate(uint256 newCommissionRate) external;
function updateCommissionRateFromStakingConfig() external;
function updateShMonadDonationRate(uint256 newDonationRate) external;
function updateCommissionRecipient(address newRecipient) external;
function updateAuthAddress() external;
// ================================ //
// Views //
// ================================ //
function getPriorityCommissionRate() external view returns (uint256);
function getMEVCommissionRate() external view returns (uint256);
function getShMonadDonationRate() external view returns (uint256);
function getCommissionRecipient() external view returns (address);
function getUnpaidBalances() external view returns (uint256 commission, uint256 rewards);
}//SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.28 <0.9.0;
import { IMonadStaking } from "./interfaces/IMonadStaking.sol";
IMonadStaking constant STAKING = IMonadStaking(0x0000000000000000000000000000000000001000);
// NOTE: These constants exist internally in the precompile but are NOT exposed as view functions.
// Only constants used in production code are defined here. Test-only constants remain in MockMonadStakingPrecompile.
uint256 constant DUST_THRESHOLD = 1e9; // Minimum stake amount (1 gwei)
uint256 constant WITHDRAWAL_DELAY = 1; // Epochs
uint256 constant MAX_EXTERNAL_REWARD = 1_000_000 * 1e18; // Precompile upper bound for externalReward()
uint256 constant VALIDATOR_CRANK_LIMIT = 1_500_000; // Stop loops when gasleft() drops below this
address constant NATIVE_TOKEN = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
address constant OWNER_COMMISSION_ACCOUNT = address(0x1111111111111111111111111111111111111111);
// Deterministic deployment salt namespace for Coinbase contracts
uint256 constant COINBASE_PROCESS_GAS_LIMIT = 175_000;
uint256 constant COINBASE_HANDLE_MEV_GAS_LIMIT = 75_000;
bytes32 constant COINBASE_SALT = keccak256("SHMONAD_COINBASE");
uint256 constant TRANSFER_GAS_LIMIT = 50_000; // gas headroom for multisig recipient support
// Monad staking precompile gas guidance (docs.monad.xyz) + adjustable buffer for mocks/in-flight gas bumps
uint256 constant STAKING_GAS_BUFFER = 10_000;
uint256 constant STAKING_GAS_CLAIM_REWARDS = 155_375;
uint256 constant STAKING_GAS_EXTERNAL_REWARD = 62_300;
uint256 constant STAKING_GAS_DELEGATE = 260_850;
uint256 constant STAKING_GAS_UNDELEGATE = 147_750;
uint256 constant STAKING_GAS_WITHDRAW = 68_675;
uint256 constant STAKING_GAS_GET_WITHDRAWAL_REQUEST = 24_300;
uint256 constant STAKING_GAS_GET_DELEGATOR = 184_900;
uint256 constant STAKING_GAS_GET_VALIDATOR = 97_200;
uint256 constant STAKING_GAS_PROPOSER_VAL_ID = 100;
// EIP-1967 admin slot: bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1)
bytes32 constant EIP1967_ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
// NOTE: Top-up period duration is compared against `block.number` in policy logic and is measured in blocks.
// Set to ~1 day on Monad (0.4s blocks): 24*60*60 / 0.4 = 216_000 blocks.
uint32 constant MIN_TOP_UP_PERIOD_BLOCKS = 216_000;
bytes32 constant PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
uint256 constant ZERO = 0;
uint16 constant BPS_SCALE = 10_000; // Still used for percentage weights/targets outside fee curve math
uint256 constant MIN_VALIDATOR_DEPOSIT = 1e18; // Min amount to do a validator deposit
uint256 constant STAKE_ITERATION_MIN_GAS = 100_000; // Min gas for 1 stake iteration in loop
uint88 constant UNSTAKE_BLOCK_DELAY = 1_209_600; // 7 days if block time is 0.5 seconds
uint256 constant SLASHING_FREEZE_THRESHOLD = 7e16; // 7% of total equity, out of SCALE
uint256 constant ATOMIC_MIN_FEE_WEI = 1e9; // 1 gwei floor for atomic paths
uint256 constant EPOCHS_TRACKED = 8; // Must be a power of 2
uint256 constant MONAD_EPOCH_LENGTH = 50_000; // Measured in blocks
uint256 constant MONAD_EPOCH_DELAY_PERIOD = 5000; // Measured in blocks
uint256 constant MIN_FREE_WITHDRAW_IDS = 8; // Min free withdraw IDs to keep available per validator
uint256 constant SHMONAD_VALIDATOR_DEACTIVATION_PERIOD = 7; // How long it takes to cycle out an inactive validator
uint256 constant SCALE = 1e18; // 100%
uint256 constant TARGET_FLOAT = 2e16; // 2%
uint256 constant FLOAT_REBALANCE_SENSITIVITY = 1e14; // 1bp
uint256 constant FLOAT_PLACEHOLDER = 1;
uint64 constant UNKNOWN_VAL_ID = type(uint64).max - 1;
address constant UNKNOWN_VAL_ADDRESS = address(uint160(type(uint160).max - 1));
// ID-based sentinels for validator crank linked list
uint64 constant FIRST_VAL_ID = 1_111_111_111_111_111_111; // 1.111e18 sentinel (fits in uint64)
uint64 constant LAST_VAL_ID = 9_999_999_999_999_999_999; // 9.999e18 sentinel (fits in uint64)
uint256 constant UINT120_MASK = type(uint120).max;
// feeLib constant
uint256 constant RAY = 1e27;
// Default affine fee curve parameters (Ray precision)
// Fee curve: r(u) = m*u + c with
// - c = 8% / 1600 = 1/20000 = 0.005%
// - m chosen so r(1) = 1% + c => m = 1%
// Therefore: at u = 0 => 0.005%; at u = 1 => 1.005%
uint128 constant DEFAULT_SLOPE_RATE_RAY = uint128(RAY / 100); // m = 1.00%
uint128 constant DEFAULT_Y_INTERCEPT_RAY = uint128(RAY / 20_000); // c = 0.005%// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (utils/TransientSlot.sol)
// This file was procedurally generated from scripts/generate/templates/TransientSlot.js.
pragma solidity ^0.8.24;
/**
* @dev Library for reading and writing value-types to specific transient storage slots.
*
* Transient slots are often used to store temporary values that are removed after the current transaction.
* This library helps with reading and writing to such slots without the need for inline assembly.
*
* * Example reading and writing values using transient storage:
* ```solidity
* contract Lock {
* using TransientSlot for *;
*
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
* bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
*
* modifier locked() {
* require(!_LOCK_SLOT.asBoolean().tload());
*
* _LOCK_SLOT.asBoolean().tstore(true);
* _;
* _LOCK_SLOT.asBoolean().tstore(false);
* }
* }
* ```
*
* TIP: Consider using this library along with {SlotDerivation}.
*/
library TransientSlot {
/**
* @dev UDVT that represents a slot holding an address.
*/
type AddressSlot is bytes32;
/**
* @dev Cast an arbitrary slot to a AddressSlot.
*/
function asAddress(bytes32 slot) internal pure returns (AddressSlot) {
return AddressSlot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a bool.
*/
type BooleanSlot is bytes32;
/**
* @dev Cast an arbitrary slot to a BooleanSlot.
*/
function asBoolean(bytes32 slot) internal pure returns (BooleanSlot) {
return BooleanSlot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a bytes32.
*/
type Bytes32Slot is bytes32;
/**
* @dev Cast an arbitrary slot to a Bytes32Slot.
*/
function asBytes32(bytes32 slot) internal pure returns (Bytes32Slot) {
return Bytes32Slot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a uint256.
*/
type Uint256Slot is bytes32;
/**
* @dev Cast an arbitrary slot to a Uint256Slot.
*/
function asUint256(bytes32 slot) internal pure returns (Uint256Slot) {
return Uint256Slot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a int256.
*/
type Int256Slot is bytes32;
/**
* @dev Cast an arbitrary slot to a Int256Slot.
*/
function asInt256(bytes32 slot) internal pure returns (Int256Slot) {
return Int256Slot.wrap(slot);
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(AddressSlot slot) internal view returns (address value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(AddressSlot slot, address value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(BooleanSlot slot) internal view returns (bool value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(BooleanSlot slot, bool value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Bytes32Slot slot) internal view returns (bytes32 value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Bytes32Slot slot, bytes32 value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Uint256Slot slot) internal view returns (uint256 value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Uint256Slot slot, uint256 value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Int256Slot slot) internal view returns (int256 value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Int256Slot slot, int256 value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
}{
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"@openzeppelin-upgradeable/contracts/=lib/openzeppelin-contracts-upgradeable/contracts/",
"@solady/=lib/solady/src/",
"forge-std/=lib/forge-std/src/",
"account-abstraction-v7/=lib/account-abstraction-v7/",
"account-abstraction-v8/=lib/account-abstraction-v8/",
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
"erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
"halmos-cheatcodes/=lib/openzeppelin-contracts-upgradeable/lib/halmos-cheatcodes/src/",
"openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"solady/=lib/solady/src/"
],
"optimizer": {
"enabled": true,
"runs": 200
},
"metadata": {
"useLiteralContent": true,
"bytecodeHash": "ipfs",
"appendCBOR": true
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"abi"
]
}
},
"evmVersion": "cancun",
"viaIR": true
}Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[{"internalType":"uint64","name":"valId","type":"uint64"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AuthAddressNotChanged","type":"error"},{"inputs":[],"name":"CommissionFailed","type":"error"},{"inputs":[],"name":"DonationFailed","type":"error"},{"inputs":[],"name":"InvalidCommissionRate","type":"error"},{"inputs":[],"name":"InvalidDonationRate","type":"error"},{"inputs":[],"name":"MEVMustExceedZero","type":"error"},{"inputs":[],"name":"OnlyAuthAddress","type":"error"},{"inputs":[],"name":"OnlySelfCaller","type":"error"},{"inputs":[],"name":"OnlyShMonadCaller","type":"error"},{"inputs":[],"name":"RecipientCannotBeZeroAddress","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[],"name":"RewardFailed","type":"error"},{"inputs":[{"internalType":"uint64","name":"validatorId","type":"uint64"}],"name":"ValidatorNotFoundInPrecompile","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldAuthAddress","type":"address"},{"indexed":false,"internalType":"address","name":"newAuthAddress","type":"address"}],"name":"AuthAddressUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldRecipient","type":"address"},{"indexed":false,"internalType":"address","name":"newRecipient","type":"address"}],"name":"CommissionRecipientUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldDonationRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newDonationRate","type":"uint256"}],"name":"DonationRateUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldCommissionRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newCommissionRate","type":"uint256"}],"name":"MEVCommissionRateUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldCommissionRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newCommissionRate","type":"uint256"}],"name":"PriorityCommissionRateUpdated","type":"event"},{"inputs":[],"name":"AUTH_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SELF","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SHMONAD","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"VAL_ID","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCommissionRecipient","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMEVCommissionRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPriorityCommissionRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getShMonadDonationRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUnpaidBalances","outputs":[{"internalType":"uint256","name":"commission","type":"uint256"},{"internalType":"uint256","name":"rewards","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"handleMEVPayable","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"process","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"commissionRecipient","type":"address"},{"internalType":"uint256","name":"validatorCommission","type":"uint256"},{"internalType":"uint256","name":"shMonadDonation","type":"uint256"}],"name":"sendCommissionAndDonation","outputs":[{"internalType":"bool","name":"commissionSucceeded","type":"bool"},{"internalType":"bool","name":"donationSucceeded","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"rewardPortion","type":"uint256"}],"name":"sendRewardsToDelegates","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"updateAuthAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"updateCommissionRateFromStakingConfig","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newRecipient","type":"address"}],"name":"updateCommissionRecipient","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newCommissionRate","type":"uint256"}],"name":"updateMEVCommissionRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newCommissionRate","type":"uint256"}],"name":"updatePriorityCommissionRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newDonationRate","type":"uint256"}],"name":"updateShMonadDonationRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]Contract Creation Code
0x60e0806040523461030057602081611835803803809161001f82856103ad565b83398101031261030057610032906103d0565b60808190523360a0523060c0526040516315b6b1cd60e11b81526001600160401b03909116600482018190525f9182908181602481836110006201a2c0f15f815f935f93610317575b5061027a575b5050506001600160a01b0316908115610268575061009e82610439565b906100a883610439565b604051909360a08201906001600160401b038211838310176102545760409182528383526001600160601b038086166020808601919091525f8585018190529188166060808701919091526080959095018690526001600160a01b03861660a09790971b6001600160a01b0319908116979097178255600180546001600160c01b0319169890951b600160601b600160c01b0316979097179093556002805490951684179094556003805460ff60f01b1916600160f01b17905580519182529381018390527f2c40de6ec45500fc9fc0e3b8f163c1cacefcd0e5744039e3ed1259551fb98e5b93927fd2bbd200bdc4c39196679610bd4a59bdb2ec8ed9919e595e85ab560533762ddc918491907f369148455bf28205428f12880987a7763ba0be5bc22c4dcc41fac960d5ee06c4908390a18151905f82526020820152a18151905f82526020820152a16040516113cd90816104688239608051818181610438015281816104a201528181610584015261062f015260a0518181816101ca01528181610240015281816103700152610876015260c051818181610402015281816108170152610a370152f35b634e487b7160e01b5f52604160045260245ffd5b6319708e7160e01b5f5260045260245ffd5b60408051630eaf323560e31b815291965091935090816004815f6110005af190811561030c575f916102bf575b50156102b857505b915f8080610081565b90506102af565b90506040813d604011610304575b816102da604093836103ad565b8101031261030057806102ee6020926103d0565b5001518015158103610300575f6102a7565b5f80fd5b3d91506102cd565b6040513d5f823e3d90fd5b93509150503d805f843e61032b81846103ad565b820191610180818403126103005780516001600160a01b038116810361030057610357602083016103d0565b5060e0820151916101208101519461014082015160018060401b03811161030057816103849184016103e4565b506101608201516001600160401b038111610300576103a392016103e4565b509092915f61007b565b601f909101601f19168101906001600160401b0382119082101761025457604052565b51906001600160401b038216820361030057565b81601f82011215610300578051906001600160401b0382116102545760405192610418601f8401601f1916602001856103ad565b8284526020838301011161030057815f9260208093018386015e8301015290565b6c0100000000000000000000000081101561045a576001600160601b031690565b6335278d125f526004601cfdfe6080604052600436101561001a575b3615610018575f80fd5b005b5f3560e01c80631056c7fe14610a8557806315f6194e14610a6657806326bd0c4e14610a2257806333c2c2c11461097e5780633f3b061a146108da5780634890f108146108b45780635f69d439146107f55780636f7cbce4146106085780637c359fe0146105e05780637c827bc2146105a85780637f4e7d7b146105645780638982e4ee1461048157806397751992146103ef5780639afefbbd146103c65780639b2967341461039f578063ae61ebe91461035b578063beac732314610233578063c33fb877146101b85763caefb7df0361000e57346101b45760203660031901126101b457600254600435906001600160a01b031633036101a557670de0b6b3a76400008111610196576001547fd2bbd200bdc4c39196679610bd4a59bdb2ec8ed9919e595e85ab560533762ddc91604091906bffffffffffffffffffffffff60601b61016782611069565b60601b166001600160601b0360601b198316176001556001600160601b0383519260601c1682526020820152a1005b63047f2a9760e51b5f5260045ffd5b639bfdc6ff60e01b5f5260045ffd5b5f80fd5b346101b4575f3660031901126101b4577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316330361022457610200611312565b602061020a610c36565b5f5f5160206113785f395f51905f525d6040519015158152f35b63688065ed60e01b5f5260045ffd5b5f3660031901126101b4577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316330361022457610276611312565b341561034c57670de0b6b3a764000061029d6001600160601b0360015460601c1634610bf6565b046102d86102ab8234610b59565b6001600160781b036102d06102c76102c1610bb8565b95611347565b82865116610c09565b168352611347565b6102f060208301916001600160781b03835116610c09565b906001600160781b03821690526001600160781b0382511660035492604060ff60f01b910151151560f01b169260ff60f81b1617906001600160781b0360781b9060781b1617176003555f5f5160206113785f395f51905f525d005b63193baad960e01b5f5260045ffd5b346101b4575f3660031901126101b4576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b346101b4575f3660031901126101b4575f546040516001600160a01b039091168152602090f35b346101b4575f3660031901126101b45760206001600160601b0360015460601c16604051908152f35b346101b45760203660031901126101b4577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031633036104725761045c6004357f0000000000000000000000000000000000000000000000000000000000000000611293565b1561046357005b63d48c254760e01b5f5260045ffd5b632388e24d60e21b5f5260045ffd5b346101b4575f3660031901126101b4576002546001600160a01b03166104c67f00000000000000000000000000000000000000000000000000000000000000006110f9565b508133148015610552575b156101a5576001600160a01b0381169180831461054357600280546001600160a01b031916909317909255604080516001600160a01b0393841681529290911660208301527fc0bb63ce3c5b13e4918a521d34215bc08febb956464efb22eb2ef6d13374bde79190819081015b0390a1005b630390f3ab60e11b5f5260045ffd5b50336001600160a01b038216146104d1565b346101b4575f3660031901126101b457602060405167ffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152f35b346101b4575f3660031901126101b45760406105c2610bb8565b6001600160781b036020818351169201511682519182526020820152f35b346101b4575f3660031901126101b4576002546040516001600160a01b039091168152602090f35b346101b4575f3660031901126101b4576002546001600160a01b031633036101a5576106537f00000000000000000000000000000000000000000000000000000000000000006110f9565b9050670de0b6b3a76400008111610196575f5490600154906001600160601b0382169283670de0b6b3a764000061068a8285610b4c565b116107b9575b7fd2bbd200bdc4c39196679610bd4a59bdb2ec8ed9919e595e85ab560533762ddc9460409493927f369148455bf28205428f12880987a7763ba0be5bc22c4dcc41fac960d5ee06c49286926001600160a01b03196106ed87611069565b60a01b1660018060a01b038416175f556001600160601b0360601b61071187611069565b60601b1690816001600160601b0360601b1989161760015580830361075c575b50505081519060a01c8152836020820152a16001600160601b0383519260601c1682526020820152a1005b8285926001600160601b036107917f8013f42332c84858f1b88335b82dda1e7dac634e2427bdeb6ebe05c0c97575b996611069565b169067ffffffffffffffff60c01b8b16171760015582519182526020820152a1878080610731565b50919080670de0b6b3a76400000393670de0b6b3a764000085116107e1579391929091610690565b634e487b7160e01b5f52601160045260245ffd5b346101b45760603660031901126101b45761080e610b36565b604435906024357f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03163303610472576040926001926001928061089f575b50508061086d575b508251911515825215156020820152f35b5f8080935038927f000000000000000000000000000000000000000000000000000000000000000061c350f18361085c565b5f809395508091389361c350f1918480610854565b346101b4575f3660031901126101b45760206001600160601b0360015416604051908152f35b346101b45760203660031901126101b4576108f3610b36565b6002546001600160a01b031633036101a5576001600160a01b03811690811561096f575f80546001600160a01b031981169093179055604080516001600160a01b0393841681529290911660208301527f2c40de6ec45500fc9fc0e3b8f163c1cacefcd0e5744039e3ed1259551fb98e5b91908190810161053e565b636c38382960e11b5f5260045ffd5b346101b45760203660031901126101b457600254600435906001600160a01b031633036101a557670de0b6b3a76400006109c36001600160601b036001541683610b4c565b11610196575f547f369148455bf28205428f12880987a7763ba0be5bc22c4dcc41fac960d5ee06c491604091906001600160a01b0319610a0282611069565b60a01b1660018060a01b038316175f5582519160a01c82526020820152a1005b346101b4575f3660031901126101b4576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b346101b4575f3660031901126101b45760205f5460a01c604051908152f35b346101b45760203660031901126101b457600254600435906001600160a01b031633036101a557670de0b6b3a7640000610ac3825f5460a01c610b4c565b11610b275760407f8013f42332c84858f1b88335b82dda1e7dac634e2427bdeb6ebe05c0c97575b991600154906001600160601b03610b0182611069565b166001600160601b03198316176001556001600160601b038351921682526020820152a1005b630574717160e41b5f5260045ffd5b600435906001600160a01b03821682036101b457565b919082018092116107e157565b919082039182116107e157565b6060810190811067ffffffffffffffff821117610b8257604052565b634e487b7160e01b5f52604160045260245ffd5b90601f8019910116810190811067ffffffffffffffff821117610b8257604052565b60405190610bc582610b66565b81604060ff6003546001600160781b03811684526001600160781b038160781c16602085015260f01c161515910152565b818102929181159184041417156107e157565b906001600160781b03809116911601906001600160781b0382116107e157565b519081151582036101b457565b604051905f60a0830167ffffffffffffffff811184821017610b82576040525f549260018060a01b0384168152602081019360a01c8452600154916001600160601b036040830193818116855260601c16606083015260018060a01b03600254166080830152610ca4610bb8565b9160206001600160781b038451169301916001600160781b03835116945f9047610cce8888610b4c565b9081811061105b57801561105b576001600160601b038b51166001600160601b03845116016001600160601b0381116107e1576001600160601b03670de0b6b3a764000091161161105b57879291610d2591610b59565b99516001600160601b0316610d3a908b610bf6565b670de0b6b3a764000090048091516001600160601b0316610d5b908c610bf6565b670de0b6b3a764000090049a8b91610d7291610b59565b90610d7c91610b59565b91610d8691610b4c565b96610d9091610b4c565b90670de0b6b3a764000082101594855f5f91611022575b5f9715610fee57303b156101b45760405190634bba8cc960e11b825260048201525f8160248183305af19081610fd9575b50610f9057505050610df16001600160801b039161135f565b16946001975b6001926001948315801590610f87575b610e9f575b5050505082610e97575b5081610e8f575b5093610e27575050565b610e33610e3991611347565b91611347565b600160406001600160781b03815194610e5186610b66565b16938481526001600160781b03841660208201520152600160f01b9160ff60f81b6003541617906001600160781b0360781b9060781b161717600355565b90505f610e1d565b91505f610e16565b5160408051635f69d43960e01b81526001600160a01b0390921660048301526024820185905260448201939093529294509192508260648184305af1918282918394610f3a575b50610f3357508091509080925b8215610f17575084610f0b575b505b5f808080610e0c565b6001975093505f610f00565b9050848111610f27575b50610f02565b6001975093505f610f21565b9192610ef3565b915092506040813d604011610f7f575b81610f5760409383610b96565b81010312610f7b57610f746020610f6d83610c29565b9201610c29565b925f610ee6565b5080fd5b3d9150610f4a565b50821515610e07565b9550969150976001600160801b03610fa960019661135f565b1696871590811591610fc5575b5015610df75760019850610df7565b6001600160781b039150511615155f610fb6565b610fe69196505f90610b96565b5f945f610dd8565b505096916001600160801b0361100e6001600160781b03929b939b61135f565b16975116871115610df75760019850610df7565b505069d3c20dee1639f99c000083108369d3c20dee1639f99c0000180269d3c20dee1639f99c0000186110558185610b59565b90610da7565b505f99505050505050505050565b600160601b811015611081576001600160601b031690565b6335278d125f526004601cfd5b519067ffffffffffffffff821682036101b457565b81601f820112156101b45780519067ffffffffffffffff8211610b8257604051926110d8601f8401601f191660200185610b96565b828452602083830101116101b457815f9260208093018386015e8301015290565b5f915f9167ffffffffffffffff604051916315b6b1cd60e11b835216908160048201525f81602481836110006201a2c0f15f815f935f936111fa575b50611162575b5050506001600160a01b038416156111505750565b6319708e7160e01b5f5260045260245ffd5b60408051630eaf323560e31b81529197509395509092816004815f6110005af19081156111ef575f916111a8575b50156111a157505b915f808061113b565b9050611198565b90506040813d6040116111e7575b816111c360409383610b96565b810103126101b4576020816111da6111e19361108e565b5001610c29565b5f611190565b3d91506111b6565b6040513d5f823e3d90fd5b93509150503d805f843e61120e8184610b96565b820191610180818403126101b45780516001600160a01b03811681036101b45761123a6020830161108e565b5060e0820151916101208101519461014082015167ffffffffffffffff81116101b457816112699184016110a3565b5061016082015167ffffffffffffffff81116101b45761128992016110a3565b509092915f611135565b67ffffffffffffffff6024602092604051948593849263e4b3303b60e01b845216600483015261100062011a6cf15f91816112d6575b506112d357505f90565b90565b9091506020813d60201161130a575b816112f260209383610b96565b810103126101b45761130390610c29565b905f6112c9565b3d91506112e5565b5f5160206113785f395f51905f525c6113385760015f5160206113785f395f51905f525d565b633ee5aeb560e01b5f5260045ffd5b600160781b811015611081576001600160781b031690565b600160801b811015611081576001600160801b03169056fe9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00a26469706673582212203c5851428c2190f6ae891f5044d22e7e31d0766cce87d3c5d1d2ff41a693202e64736f6c634300081e0033000000000000000000000000000000000000000000000000000000000000005c
Deployed Bytecode
0x6080604052600436101561001a575b3615610018575f80fd5b005b5f3560e01c80631056c7fe14610a8557806315f6194e14610a6657806326bd0c4e14610a2257806333c2c2c11461097e5780633f3b061a146108da5780634890f108146108b45780635f69d439146107f55780636f7cbce4146106085780637c359fe0146105e05780637c827bc2146105a85780637f4e7d7b146105645780638982e4ee1461048157806397751992146103ef5780639afefbbd146103c65780639b2967341461039f578063ae61ebe91461035b578063beac732314610233578063c33fb877146101b85763caefb7df0361000e57346101b45760203660031901126101b457600254600435906001600160a01b031633036101a557670de0b6b3a76400008111610196576001547fd2bbd200bdc4c39196679610bd4a59bdb2ec8ed9919e595e85ab560533762ddc91604091906bffffffffffffffffffffffff60601b61016782611069565b60601b166001600160601b0360601b198316176001556001600160601b0383519260601c1682526020820152a1005b63047f2a9760e51b5f5260045ffd5b639bfdc6ff60e01b5f5260045ffd5b5f80fd5b346101b4575f3660031901126101b4577f0000000000000000000000001b68626dca36c7fe922fd2d55e4f631d962de19c6001600160a01b0316330361022457610200611312565b602061020a610c36565b5f5f5160206113785f395f51905f525d6040519015158152f35b63688065ed60e01b5f5260045ffd5b5f3660031901126101b4577f0000000000000000000000001b68626dca36c7fe922fd2d55e4f631d962de19c6001600160a01b0316330361022457610276611312565b341561034c57670de0b6b3a764000061029d6001600160601b0360015460601c1634610bf6565b046102d86102ab8234610b59565b6001600160781b036102d06102c76102c1610bb8565b95611347565b82865116610c09565b168352611347565b6102f060208301916001600160781b03835116610c09565b906001600160781b03821690526001600160781b0382511660035492604060ff60f01b910151151560f01b169260ff60f81b1617906001600160781b0360781b9060781b1617176003555f5f5160206113785f395f51905f525d005b63193baad960e01b5f5260045ffd5b346101b4575f3660031901126101b4576040517f0000000000000000000000001b68626dca36c7fe922fd2d55e4f631d962de19c6001600160a01b03168152602090f35b346101b4575f3660031901126101b4575f546040516001600160a01b039091168152602090f35b346101b4575f3660031901126101b45760206001600160601b0360015460601c16604051908152f35b346101b45760203660031901126101b4577f000000000000000000000000520fd81dad59a6fd6b7fd87ed9b0bd667719dfe96001600160a01b031633036104725761045c6004357f000000000000000000000000000000000000000000000000000000000000005c611293565b1561046357005b63d48c254760e01b5f5260045ffd5b632388e24d60e21b5f5260045ffd5b346101b4575f3660031901126101b4576002546001600160a01b03166104c67f000000000000000000000000000000000000000000000000000000000000005c6110f9565b508133148015610552575b156101a5576001600160a01b0381169180831461054357600280546001600160a01b031916909317909255604080516001600160a01b0393841681529290911660208301527fc0bb63ce3c5b13e4918a521d34215bc08febb956464efb22eb2ef6d13374bde79190819081015b0390a1005b630390f3ab60e11b5f5260045ffd5b50336001600160a01b038216146104d1565b346101b4575f3660031901126101b457602060405167ffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000005c168152f35b346101b4575f3660031901126101b45760406105c2610bb8565b6001600160781b036020818351169201511682519182526020820152f35b346101b4575f3660031901126101b4576002546040516001600160a01b039091168152602090f35b346101b4575f3660031901126101b4576002546001600160a01b031633036101a5576106537f000000000000000000000000000000000000000000000000000000000000005c6110f9565b9050670de0b6b3a76400008111610196575f5490600154906001600160601b0382169283670de0b6b3a764000061068a8285610b4c565b116107b9575b7fd2bbd200bdc4c39196679610bd4a59bdb2ec8ed9919e595e85ab560533762ddc9460409493927f369148455bf28205428f12880987a7763ba0be5bc22c4dcc41fac960d5ee06c49286926001600160a01b03196106ed87611069565b60a01b1660018060a01b038416175f556001600160601b0360601b61071187611069565b60601b1690816001600160601b0360601b1989161760015580830361075c575b50505081519060a01c8152836020820152a16001600160601b0383519260601c1682526020820152a1005b8285926001600160601b036107917f8013f42332c84858f1b88335b82dda1e7dac634e2427bdeb6ebe05c0c97575b996611069565b169067ffffffffffffffff60c01b8b16171760015582519182526020820152a1878080610731565b50919080670de0b6b3a76400000393670de0b6b3a764000085116107e1579391929091610690565b634e487b7160e01b5f52601160045260245ffd5b346101b45760603660031901126101b45761080e610b36565b604435906024357f000000000000000000000000520fd81dad59a6fd6b7fd87ed9b0bd667719dfe96001600160a01b03163303610472576040926001926001928061089f575b50508061086d575b508251911515825215156020820152f35b5f8080935038927f0000000000000000000000001b68626dca36c7fe922fd2d55e4f631d962de19c61c350f18361085c565b5f809395508091389361c350f1918480610854565b346101b4575f3660031901126101b45760206001600160601b0360015416604051908152f35b346101b45760203660031901126101b4576108f3610b36565b6002546001600160a01b031633036101a5576001600160a01b03811690811561096f575f80546001600160a01b031981169093179055604080516001600160a01b0393841681529290911660208301527f2c40de6ec45500fc9fc0e3b8f163c1cacefcd0e5744039e3ed1259551fb98e5b91908190810161053e565b636c38382960e11b5f5260045ffd5b346101b45760203660031901126101b457600254600435906001600160a01b031633036101a557670de0b6b3a76400006109c36001600160601b036001541683610b4c565b11610196575f547f369148455bf28205428f12880987a7763ba0be5bc22c4dcc41fac960d5ee06c491604091906001600160a01b0319610a0282611069565b60a01b1660018060a01b038316175f5582519160a01c82526020820152a1005b346101b4575f3660031901126101b4576040517f000000000000000000000000520fd81dad59a6fd6b7fd87ed9b0bd667719dfe96001600160a01b03168152602090f35b346101b4575f3660031901126101b45760205f5460a01c604051908152f35b346101b45760203660031901126101b457600254600435906001600160a01b031633036101a557670de0b6b3a7640000610ac3825f5460a01c610b4c565b11610b275760407f8013f42332c84858f1b88335b82dda1e7dac634e2427bdeb6ebe05c0c97575b991600154906001600160601b03610b0182611069565b166001600160601b03198316176001556001600160601b038351921682526020820152a1005b630574717160e41b5f5260045ffd5b600435906001600160a01b03821682036101b457565b919082018092116107e157565b919082039182116107e157565b6060810190811067ffffffffffffffff821117610b8257604052565b634e487b7160e01b5f52604160045260245ffd5b90601f8019910116810190811067ffffffffffffffff821117610b8257604052565b60405190610bc582610b66565b81604060ff6003546001600160781b03811684526001600160781b038160781c16602085015260f01c161515910152565b818102929181159184041417156107e157565b906001600160781b03809116911601906001600160781b0382116107e157565b519081151582036101b457565b604051905f60a0830167ffffffffffffffff811184821017610b82576040525f549260018060a01b0384168152602081019360a01c8452600154916001600160601b036040830193818116855260601c16606083015260018060a01b03600254166080830152610ca4610bb8565b9160206001600160781b038451169301916001600160781b03835116945f9047610cce8888610b4c565b9081811061105b57801561105b576001600160601b038b51166001600160601b03845116016001600160601b0381116107e1576001600160601b03670de0b6b3a764000091161161105b57879291610d2591610b59565b99516001600160601b0316610d3a908b610bf6565b670de0b6b3a764000090048091516001600160601b0316610d5b908c610bf6565b670de0b6b3a764000090049a8b91610d7291610b59565b90610d7c91610b59565b91610d8691610b4c565b96610d9091610b4c565b90670de0b6b3a764000082101594855f5f91611022575b5f9715610fee57303b156101b45760405190634bba8cc960e11b825260048201525f8160248183305af19081610fd9575b50610f9057505050610df16001600160801b039161135f565b16946001975b6001926001948315801590610f87575b610e9f575b5050505082610e97575b5081610e8f575b5093610e27575050565b610e33610e3991611347565b91611347565b600160406001600160781b03815194610e5186610b66565b16938481526001600160781b03841660208201520152600160f01b9160ff60f81b6003541617906001600160781b0360781b9060781b161717600355565b90505f610e1d565b91505f610e16565b5160408051635f69d43960e01b81526001600160a01b0390921660048301526024820185905260448201939093529294509192508260648184305af1918282918394610f3a575b50610f3357508091509080925b8215610f17575084610f0b575b505b5f808080610e0c565b6001975093505f610f00565b9050848111610f27575b50610f02565b6001975093505f610f21565b9192610ef3565b915092506040813d604011610f7f575b81610f5760409383610b96565b81010312610f7b57610f746020610f6d83610c29565b9201610c29565b925f610ee6565b5080fd5b3d9150610f4a565b50821515610e07565b9550969150976001600160801b03610fa960019661135f565b1696871590811591610fc5575b5015610df75760019850610df7565b6001600160781b039150511615155f610fb6565b610fe69196505f90610b96565b5f945f610dd8565b505096916001600160801b0361100e6001600160781b03929b939b61135f565b16975116871115610df75760019850610df7565b505069d3c20dee1639f99c000083108369d3c20dee1639f99c0000180269d3c20dee1639f99c0000186110558185610b59565b90610da7565b505f99505050505050505050565b600160601b811015611081576001600160601b031690565b6335278d125f526004601cfd5b519067ffffffffffffffff821682036101b457565b81601f820112156101b45780519067ffffffffffffffff8211610b8257604051926110d8601f8401601f191660200185610b96565b828452602083830101116101b457815f9260208093018386015e8301015290565b5f915f9167ffffffffffffffff604051916315b6b1cd60e11b835216908160048201525f81602481836110006201a2c0f15f815f935f936111fa575b50611162575b5050506001600160a01b038416156111505750565b6319708e7160e01b5f5260045260245ffd5b60408051630eaf323560e31b81529197509395509092816004815f6110005af19081156111ef575f916111a8575b50156111a157505b915f808061113b565b9050611198565b90506040813d6040116111e7575b816111c360409383610b96565b810103126101b4576020816111da6111e19361108e565b5001610c29565b5f611190565b3d91506111b6565b6040513d5f823e3d90fd5b93509150503d805f843e61120e8184610b96565b820191610180818403126101b45780516001600160a01b03811681036101b45761123a6020830161108e565b5060e0820151916101208101519461014082015167ffffffffffffffff81116101b457816112699184016110a3565b5061016082015167ffffffffffffffff81116101b45761128992016110a3565b509092915f611135565b67ffffffffffffffff6024602092604051948593849263e4b3303b60e01b845216600483015261100062011a6cf15f91816112d6575b506112d357505f90565b90565b9091506020813d60201161130a575b816112f260209383610b96565b810103126101b45761130390610c29565b905f6112c9565b3d91506112e5565b5f5160206113785f395f51905f525c6113385760015f5160206113785f395f51905f525d565b633ee5aeb560e01b5f5260045ffd5b600160781b811015611081576001600160781b031690565b600160801b811015611081576001600160801b03169056fe9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00a26469706673582212203c5851428c2190f6ae891f5044d22e7e31d0766cce87d3c5d1d2ff41a693202e64736f6c634300081e0033
Deployed Bytecode Sourcemap
1244:15275:5:-:0;;;;;;;;;-1:-1:-1;1244:15275:5;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;3832:20;1244:15275;;;;-1:-1:-1;;;;;1244:15275:5;3818:10;:34;1244:15275;;3486:4:6;11598:26:5;;1244:15275;;;;11799:63;;1244:15275;;;-1:-1:-1;;;11755:28:5;1244:15275;11755:28;:::i;:::-;1244:15275;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;11799:63;1244:15275;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;3951:7;-1:-1:-1;;;;;1244:15275:5;3937:10;:21;1244:15275;;1181:103:0;;:::i;:::-;1244:15275:5;4853:4352;;:::i;:::-;1244:15275;-1:-1:-1;;;;;;;;;;;3556:68:1;1244:15275:5;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;3951:7;-1:-1:-1;;;;;1244:15275:5;3937:10;:21;1244:15275;;1181:103:0;;:::i;:::-;4227:9:5;:13;1244:15275;;3486:4:6;4414:30:5;-1:-1:-1;;;;;4341:26:5;1244:15275;;;;4227:9;4414:30;:::i;:::-;1244:15275;4666:19;4480:37;4227:9;;4480:37;:::i;:::-;-1:-1:-1;;;;;4578:59:5;4600:37;1244:15275;;:::i;:::-;4600:37;;:::i;:::-;1244:15275;;;;4578:59;:::i;:::-;1244:15275;;;4666:19;:::i;:::-;4647:38;:15;;;1244:15275;-1:-1:-1;;;;;1244:15275:5;;;4647:38;:::i;:::-;1244:15275;-1:-1:-1;;;;;1244:15275:5;;;;-1:-1:-1;;;;;1244:15275:5;;;4560:8;1244:15275;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;4560:8;1244:15275;;-1:-1:-1;;;;;;;;;;;3556:68:1;1244:15275:5;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;;;1418:32;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;;-1:-1:-1;;;;;14328:26:5;1244:15275;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;4056:4;-1:-1:-1;;;;;1244:15275:5;4042:10;:18;1244:15275;;9359:35;1244:15275;;9372:6;9359:35;:::i;:::-;1244:15275;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;10449:20;1244:15275;-1:-1:-1;;;;;1244:15275:5;10508:21;10522:6;10508:21;:::i;:::-;10548:10;;;:34;:67;;;;1244:15275;;;;-1:-1:-1;;;;;1244:15275:5;;;10653:39;;;1244:15275;;10449:20;1244:15275;;-1:-1:-1;;;;;;1244:15275:5;;;;;;;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;;10783:57;;1244:15275;;;;;10783:57;;;;1244:15275;;;;;;;;;;10548:67;-1:-1:-1;10548:10:5;-1:-1:-1;;;;;1244:15275:5;;10586:29;10548:67;;1244:15275;;;;;;-1:-1:-1;;1244:15275:5;;;;;;;;1382:30;1244:15275;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;;;;:::i;:::-;-1:-1:-1;;;;;14893:15:5;1244:15275;;;;14893:15;;1244:15275;;;;;;;14893:15;1244:15275;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;15031:20;1244:15275;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;3832:20;1244:15275;-1:-1:-1;;;;;1244:15275:5;3818:10;:34;1244:15275;;12560:21;12574:6;12560:21;:::i;:::-;12599:27;;3486:4:6;12599:27:5;;1244:15275;;;;;12765:26;1244:15275;;-1:-1:-1;;;;;1244:15275:5;;;;3486:4:6;13011:37:5;;;;:::i;:::-;:45;13007:121;;1244:15275;13571:67;;1244:15275;;;;13487:69;;1244:15275;;-1:-1:-1;;;;;;13172:29:5;1244:15275;13172:29;:::i;:::-;1244:15275;;;;;;;;;;;;;-1:-1:-1;;;;;1244:15275:5;;13240:29;;;:::i;:::-;1244:15275;;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;12765:26;1244:15275;13284:36;;;13280:192;;1244:15275;;;;;;;;;;;;;;;;13487:69;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;13571:67;1244:15275;13280:192;13360:27;;;-1:-1:-1;;;;;13360:27:5;13406:55;13360:27;;:::i;:::-;1244:15275;3486:4:6;1244:15275:5;3486:4:6;;;;;;12765:26:5;3486:4:6;1244:15275:5;;;;;;;;;13406:55;13280:192;;;;;13007:121;1244:15275;;;;3486:4:6;1244:15275:5;;3486:4:6;1244:15275:5;;;;13072:45;13007:121;;;;;;1244:15275;;;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;;;:::i;:::-;;;;;;4056:4;-1:-1:-1;;;;;1244:15275:5;4042:10;:18;1244:15275;;;9499:829;1244:15275;9991:24;1244:15275;10029:23;;10025:155;;1244:15275;10193:19;;;10189:133;;1244:15275;;;;;;;;;;;;;;;;10189:133;1244:15275;8588:108:4;;;;;10248:7:5;;1232:6:6;8588:108:4;10189:133:5;;;10025:155;1244:15275;8588:108:4;;;;;;;;1232:6:6;8588:108:4;10025:155:5;;;;;1244:15275;;;;;;-1:-1:-1;;1244:15275:5;;;;;-1:-1:-1;;;;;14486:21:5;3486:4:6;1244:15275:5;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;;;:::i;:::-;3832:20;1244:15275;-1:-1:-1;;;;;1244:15275:5;3818:10;:34;1244:15275;;-1:-1:-1;;;;;1244:15275:5;;;13787:26;;1244:15275;;;;;-1:-1:-1;;;;;;1244:15275:5;;;;;;;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;;13977:55;;1244:15275;;;;;13977:55;1244:15275;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;3832:20;1244:15275;;;;-1:-1:-1;;;;;1244:15275:5;3818:10;:34;1244:15275;;3486:4:6;11107:36:5;-1:-1:-1;;;;;1244:15275:5;3486:4:6;1244:15275:5;11107:36;;:::i;:::-;:45;1244:15275;;;;11337:68;;1244:15275;;;-1:-1:-1;;;;;;11293:28:5;1244:15275;11293:28;:::i;:::-;1244:15275;;;;;;;;;;;;;;;;;;;;;;;;11337:68;1244:15275;;;;;;;-1:-1:-1;;1244:15275:5;;;;;;1456:29;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;1244:15275:5;;;;3832:20;1244:15275;;;;-1:-1:-1;;;;;1244:15275:5;3818:10;:34;1244:15275;;3486:4:6;12136:36:5;1244:15275;;;;;12136:36;:::i;:::-;:45;3486:4:6;;1244:15275:5;12340:54;3486:4:6;1244:15275:5;3486:4:6;12298:26:5;-1:-1:-1;;;;;12298:26:5;;;:::i;:::-;1244:15275;-1:-1:-1;;;;;3486:4:6;;;;1244:15275:5;3486:4:6;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;12340:54;1244:15275;3486:4:6;;;;1244:15275:5;3486:4:6;1244:15275:5;;3486:4:6;1244:15275:5;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;:::o;:::-;;;;;;;;;;:::o;:::-;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;:::o;:::-;;;;-1:-1:-1;1244:15275:5;;;;;-1:-1:-1;1244:15275:5;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;:::i;:::-;;;;14847:8;1244:15275;-1:-1:-1;;;;;1244:15275:5;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;;:::o;:::-;;-1:-1:-1;;;;;1244:15275:5;;;;;;;-1:-1:-1;;;;;1244:15275:5;;;;:::o;605:16:6:-;;1244:15275:5;;;;605:16:6;;;;:::o;4853:4352:5:-;1244:15275;;;5302:8;1244:15275;;;;;;;;;;;;;;5302:8;1244:15275;;;;;;;;;;;;;;;;;;;;3486:4:6;1244:15275:5;-1:-1:-1;;;;;1244:15275:5;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;-1:-1:-1;;;;;1244:15275:5;;;5451:15;;1244:15275;-1:-1:-1;;;;;1244:15275:5;;;5476:34;5302:8;5634:21;;5765:34;;;;:::i;:::-;5813:31;;;;5809:49;;5872:20;;5868:38;;-1:-1:-1;;;;;1244:15275:5;;;-1:-1:-1;;;;;1244:15275:5;;;;-1:-1:-1;;;;;1244:15275:5;;;;-1:-1:-1;;;;;3486:4:6;1244:15275:5;;5920:61;5916:79;;6005:32;;;;;;:::i;:::-;1244:15275;;-1:-1:-1;;;;;1244:15275:5;6116:48;;;;:::i;:::-;3486:4:6;1244:15275:5;;;;;-1:-1:-1;;;;;1244:15275:5;6210:38;;;;:::i;:::-;3486:4:6;1244:15275:5;;6291:38;;;;;;:::i;:::-;:57;;;;:::i;:::-;6359:41;;;;:::i;:::-;6410:32;;;;:::i;:::-;6549:39;3486:4:6;6549:39:5;;;6598:21;;5302:8;;6663:255;;;4853:4352;5302:8;;7079:668;;;5642:4;7117:42;;;;1244:15275;;605:16:6;;;;7117:42:5;;;;;1244:15275;5302:8;5642:4;7117:42;5642:4;;;7117:42;;;;;;7079:668;-1:-1:-1;7113:389:5;;7415:26;;;;-1:-1:-1;;;;;7415:26:5;;:::i;:::-;605:16:6;7459:28:5;1244:15275;7113:389;;1244:15275;7799:30;1244:15275;7843:24;;;;;:48;;;7079:668;7839:922;;7079:668;8886:41;;;;;;;7079:668;8886:63;;;;7079:668;8876:73;8960:239;;;4853:4352;;:::o;8960:239::-;9057:29;9113:26;9057:29;;:::i;:::-;9113:26;;:::i;:::-;1244:15275;;-1:-1:-1;;;;;1244:15275:5;;;;;;:::i;:::-;;;;;;-1:-1:-1;;;;;1244:15275:5;;;9012:176;;1244:15275;9012:176;1244:15275;;;;;;;;5352:8;1244:15275;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;;;5352:8;1244:15275;4853:4352::o;8886:63::-;;;;;;:41;;-1:-1:-1;8886:41:5;;;7839:922;605:16:6;1244:15275:5;;;-1:-1:-1;;;7911:99:5;;-1:-1:-1;;;;;1244:15275:5;;;7911:99;;;1244:15275;605:16:6;;;1244:15275:5;;;605:16:6;;;1244:15275:5;;;;;;-1:-1:-1;1244:15275:5;;-1:-1:-1;1244:15275:5;7911:99;1244:15275;;5642:4;7911:99;;;;;5302:8;;7911:99;;;7839:922;-1:-1:-1;7907:418:5;;8238:28;;;;8284:26;;7907:418;;8339:412;;;;8387:21;;8383:139;;8339:412;;;7839:922;;;;;;8383:139;1244:15275;;-1:-1:-1;8432:21:5;-1:-1:-1;8383:139:5;;;8339:412;8564:40;;;;;8560:177;;8339:412;;;;8560:177;1244:15275;;-1:-1:-1;8628:40:5;-1:-1:-1;8560:177:5;;;7907:418;8160:38;7907:418;;;7911:99;;;;;1244:15275;7911:99;;1244:15275;7911:99;;;;;;1244:15275;7911:99;;;:::i;:::-;;;605:16:6;;;;;1244:15275:5;605:16:6;;;:::i;:::-;;;;:::i;:::-;7911:99:5;;;;605:16:6;1244:15275:5;;;7911:99;;;-1:-1:-1;7911:99:5;;7843:48;7871:20;;;;7843:48;;7113:389;7178:24;;;;;;-1:-1:-1;;;;;7237:28:5;1244:15275;7237:28;;:::i;:::-;605:16:6;7287:18:5;;;;;;:41;;;7113:389;7283:75;;7113:389;7283:75;1244:15275;;-1:-1:-1;7113:389:5;;7287:41;-1:-1:-1;;;;;1244:15275:5;;;;7309:19;;7287:41;;;7117:42;;;;;5302:8;7117:42;;:::i;:::-;5302:8;7117:42;;;;7079:668;7630:26;;;;-1:-1:-1;;;;;7630:26:5;-1:-1:-1;;;;;7630:26:5;;;;;:::i;:::-;605:16:6;1244:15275:5;;;7674:32;;7670:66;7079:668;7670:66;1244:15275;;-1:-1:-1;7079:668:5;;6663:255;49025:70:2;;1244:15275:5;49025:70:2;;;1244:15275:5;49025:70:2;;1244:15275:5;49025:70:2;6877:30:5;;;;:::i;:::-;6663:255;;;5916:79;-1:-1:-1;5302:8:5;;-1:-1:-1;;;;;;;;;5983:12:5:o;3395:138:3:-;-1:-1:-1;;;3469:12:3;;;3465:35;;-1:-1:-1;;;;;1244:15275:5;3395:138:3;:::o;3465:35::-;23595:191;;;;;;1434:6:6;;1244:15275:5;;;;1434:6:6;;;;:::o;:::-;;;;;;;;;;;;;;;;;1244:15275:5;;;;;;;-1:-1:-1;;1244:15275:5;1434:6:6;;1244:15275:5;;:::i;:::-;1434:6:6;;;;;;;;;;;;-1:-1:-1;1434:6:6;;;;;;;;;;;;;;:::o;15609:908:5:-;1244:15275;15691:22;1244:15275;;;;;605:16:6;;;;15826:88:5;;1244:15275;15826:88;;;;;1244:15275;;15826:88;;;;182:42:6;1244:15275:5;15826:88;1244:15275;;;;;15826:88;;;15609:908;15822:601;;;15609:908;-1:-1:-1;;;;;;;;1244:15275:5;;16440:25;1434:6:6;;15609:908:5;:::o;1434:6:6:-;;;;1244:15275:5;1434:6:6;15826:88:5;1244:15275;15826:88;1244:15275;1434:6:6;15822:601:5;1244:15275;;;-1:-1:-1;;;15162:18:5;;16271:26;;-1:-1:-1;16271:26:5;;-1:-1:-1;16271:26:5;;1244:15275;15826:88;1244:15275;;182:42:6;15162:18:5;;;;;;;1244:15275;15162:18;;;15822:601;-1:-1:-1;16328:74:5;;;;;15822:601;;;;;;16328:74;;;;;15162:18;;;1244:15275;15162:18;;1244:15275;15162:18;;;;;;1244:15275;15162:18;;;:::i;:::-;;;1244:15275;;;;;1434:6:6;;605:16;1434:6;;:::i;:::-;;1244:15275:5;605:16:6;:::i;:::-;15162:18:5;;;;;;-1:-1:-1;15162:18:5;;;1244:15275;;605:16:6;1244:15275:5;605:16:6;;;;;15826:88:5;;;;;;;;1244:15275;15826:88;;;;;;:::i;:::-;;;1434:6:6;;;;;;;;;;-1:-1:-1;;;;;1244:15275:5;;;;;;1434:6:6;1244:15275:5;1434:6:6;;;:::i;:::-;;605:16;1434:6;;;;;;;;;;;;;1244:15275:5;1434:6:6;;;;;;;;;;:::i;:::-;;;;;;1244:15275:5;1434:6:6;;;;;;;;:::i;:::-;;15826:88:5;;;;;;15231:372;1244:15275;15333:135;1244:15275;15231:372;1244:15275;;605:16:6;;;;;;;;15333:135:5;;1244:15275;15333:135;;;1244:15275;182:42:6;1244:15275:5;15333:135;1244:15275;;15333:135;;;15231:372;-1:-1:-1;15329:268:5;;15574:12;1244:15275;15574:12;:::o;15329:268::-;15517:25;:::o;15333:135::-;;;;1244:15275;15333:135;;1244:15275;15333:135;;;;;;1244:15275;15333:135;;;:::i;:::-;;;1543:6:6;;;;605:16;;;:::i;:::-;15333:135:5;;;;;;;-1:-1:-1;15333:135:5;;1290:377:0;-1:-1:-1;;;;;;;;;;;3327:69:1;1444:93:0;;1655:4;-1:-1:-1;;;;;;;;;;;3556:68:1;1290:377:0:o;1444:93::-;1496:30;;;-1:-1:-1;1496:30:0;;-1:-1:-1;1496:30:0;4009:142:3;-1:-1:-1;;;4085:13:3;;;4081:36;;-1:-1:-1;;;;;1244:15275:5;4009:142:3;:::o;4215:::-;-1:-1:-1;;;4291:13:3;;;4287:36;;-1:-1:-1;;;;;605:16:6;4215:142:3;:::o
Swarm Source
ipfs://3c5851428c2190f6ae891f5044d22e7e31d0766cce87d3c5d1d2ff41a693202e
Loading...
Loading
Loading...
Loading
Net Worth in USD
$2.19
Net Worth in MON
Token Allocations
MON
100.00%
Multichain Portfolio | 33 Chains
| Chain | Token | Portfolio % | Price | Amount | Value |
|---|---|---|---|---|---|
| MONAD | 100.00% | $0.023932 | 91.4027 | $2.19 |
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
[ 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.