Source Code
Overview
ETH Balance
0 ETH
ETH Value
$0.00
Cross-Chain Transactions
Loading...
Loading
Contract Name:
Bitsave
Compiler Version
v0.8.27+commit.40a35a09
Optimization Enabled:
No with 200 runs
Other Settings:
prague EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./ChildContract.sol";
import "./libraries/bitsaveHelperLib.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
/**
* @title Bitsave
* @notice Implements a user-focused savings system where each user gets a child contract
* to manage savings entries. Supports native and ERC-20 savings, incremental
* deposits, and admin-controlled parameters.
* @dev The contract deploys a per-user ChildBitsave contract on `joinBitsave()` and
* forwards savings operations to that child. Several functions use modifiers to
* restrict access (e.g., `inhouseOnly`, `registeredOnly`, `fromABitsaveChildOnly`).
*/
contract Bitsave is ReentrancyGuard {
// *** Contract parameters ***
IERC20 public stableCoin;
IERC20 public csToken;
address payable public masterAddress;
uint256 public rewardPool;
// *** Fountain ***
uint256 public fountain;
// *** Storage ***
mapping(address => address) addressToUserBS;
uint256 public userCount;
// *** Storage requiring house modifiers ***
uint256 public currentVaultState;
uint256 public currentTotalValueLocked;
// *** savings values ***
// editing value from 0.0001 to 0wei
uint256 public JoinLimitFee = 0;
uint256 public SavingFee = 0.0001 ether;
uint256 public ChildCutPerFee = 50;
uint256 public ChildContractGasFee = SavingFee / ChildCutPerFee;
/**
* @notice Construct a new Bitsave instance
* @param _stableCoin Address of the stablecoin ERC-20 used internally
* @param _csToken Address of the csToken ERC-20 used for rewards/utility
* @dev `msg.sender` becomes the `masterAddress`. The contract is payable so a
* starting `fountain` value may be provided.
*/
constructor(address _stableCoin, address _csToken) payable {
stableCoin = IERC20(_stableCoin);
csToken = IERC20(_csToken);
masterAddress = payable(msg.sender);
rewardPool = 0;
userCount = 0;
// initial values
currentVaultState = 14_000_000;
currentTotalValueLocked = 100_000;
fountain = msg.value;
}
modifier inhouseOnly() {
if (msg.sender != masterAddress) {
revert BitsaveHelperLib.MasterCallRequired();
}
_;
}
modifier registeredOnly(address sender) {
if (addressToUserBS[sender] == address(0)) {
revert BitsaveHelperLib.UserNotRegistered();
}
_;
}
modifier fromABitsaveChildOnly(address childOwnerAddress) {
address fetchedChildAddress = addressToUserBS[childOwnerAddress];
if (
fetchedChildAddress == address(0) || // checks that the child contract exists
// could be merged into one check but for readability
fetchedChildAddress != msg.sender // and that the child contract sent the request
) {
revert BitsaveHelperLib.CallNotFromBitsave();
}
_;
}
/**
* @notice Register the caller with Bitsave and deploy a per-user ChildBitsave contract
* @dev If the caller is already registered the existing child contract address is returned.
* The function is payable to allow a join fee (`JoinLimitFee`) to be sent if configured.
* @return address The address of the caller's ChildBitsave contract
*/
function joinBitsave() public payable returns (address) {
address ownerAddress = msg.sender;
address currAddr = addressToUserBS[ownerAddress];
if (currAddr != address(0)) {
return currAddr;
}
// if (msg.value < JoinLimitFee) {
// revert BitsaveHelperLib.AmountNotEnough();
// }
// deploy child contract for user
address userBSAddress = address(
new ChildBitsave(msg.sender, address(stableCoin))
);
addressToUserBS[ownerAddress] = userBSAddress;
userCount += 1;
emit BitsaveHelperLib.JoinedBitsave(ownerAddress);
return userBSAddress;
}
/**
* @notice Returns the caller's child contract address
* @return address The child contract address associated with msg.sender, or address(0)
* if the caller is not registered
*/
function getUserChildContractAddress() public view returns (address) {
return addressToUserBS[msg.sender];
}
/**
* @notice Retrieve stablecoin held by the child contract and return success
* @dev This function is intended to be called only by a registered child contract
* via the `fromABitsaveChildOnly` modifier. It attempts to retrieve `amount`
* of `stableCoin` from the child and returns the result of the helper call.
* @param originalToken Address of the token the owner expects (not used in current impl.)
* @param amount Amount to retrieve (in stablecoin decimals)
* @param ownerAddress Owner address whose child contract is calling
* @return bool True on success
*/
function sendAsOriginalToken(
address originalToken,
uint256 amount,
address ownerAddress
)
public
payable
fromABitsaveChildOnly(ownerAddress)
nonReentrant
returns (bool)
{
// check amount sent
// if (amount < poolFee) revert BitsaveHelperLib.AmountNotEnough();
// retrieve stable coin used from owner address
return
BitsaveHelperLib.retrieveToken(
msg.sender,
address(stableCoin),
amount
);
// convert to original token using crossChainSwap()
// crossChainSwap(
// stableCoin,
// originalToken,
// amount,
// ownerAddress // send to owner address directly
// );
}
/// Edit internal vault data
/**
* @notice Update internal vault parameters (admin only)
* @dev Callable only by `masterAddress` via the `inhouseOnly` modifier.
* @param _newCurrentVaultState New vault state numerical value used in calculations
* @param _newTotalValueLocked New total value locked used by the protocol
* @param _newCsToken Optional new address for the `csToken` (use address(0) to skip)
*/
function editInternalData(
uint256 _newCurrentVaultState,
uint256 _newTotalValueLocked,
address _newCsToken
) public inhouseOnly {
currentVaultState = _newCurrentVaultState;
currentTotalValueLocked = _newTotalValueLocked;
if (_newCsToken != address(0)) {
csToken = IERC20(_newCsToken);
}
}
/// Edit internal stablecoin data
/**
* @notice Update the stablecoin used by the protocol (admin only)
* @dev Callable only by `masterAddress`. Passing `address(0)` is ignored.
* @param _newStableCoin Address of the new stablecoin ERC-20 contract
*/
function editStableCoin(address _newStableCoin) public inhouseOnly {
if (_newStableCoin != address(0)) {
stableCoin = IERC20(_newStableCoin);
}
}
/// Edit internal vault data
/**
* @notice Update protocol fees and related derived values (admin only)
* @dev Updates `JoinLimitFee`, `SavingFee`, and `ChildCutPerFee`. Recomputes
* `ChildContractGasFee` unless `_childCutPerFee` is zero.
* @param _joinFee Minimum join fee in wei (or token smallest unit)
* @param _savingFee Fee required when creating a saving (in native wei)
* @param _childCutPerFee Divider used to compute child gas fee share
*/
function editFees(
uint256 _joinFee,
uint256 _savingFee,
uint256 _childCutPerFee
) public inhouseOnly {
JoinLimitFee = _joinFee;
SavingFee = _savingFee;
ChildCutPerFee = _childCutPerFee;
// if childCutPerFee == 0, ChildContractGasFee == 0
ChildContractGasFee = (_childCutPerFee == 0)
? 0
: _savingFee / _childCutPerFee;
}
/**
* @notice Drain any excess funds or tokens above the configured `fountain` reserve
* @dev Admin-only. If `token` is address(0) native currency is drained; otherwise
* the token balance is transferred to `masterAddress`.
* @param token Address of token to drip, or address(0) for native
*/
function dripFountain(address token) public inhouseOnly nonReentrant {
if (token == address(0)) {
uint256 balance = address(this).balance;
// send balance - fountain to masterAddress
uint256 drip = 0;
if (balance > fountain) {
drip = balance - fountain;
(bool ok, ) = masterAddress.call{value: drip}("");
require(ok, "transfer failed");
}
emit BitsaveHelperLib.SystemFaucetDrip(token, drip);
return;
}
uint256 tokenBalance = BitsaveHelperLib.tokenBalance(token);
BitsaveHelperLib.transferToken(token, masterAddress, tokenBalance);
emit BitsaveHelperLib.SystemFaucetDrip(token, tokenBalance);
}
/**
* @dev Internal helper to handle native vs ERC-20 saving flows. If `tokenToSave`
* is an ERC-20 address it will attempt to retrieve tokens via the helper
* library and approve the child contract; otherwise it computes value from
* `msg.value` minus the provided `nativeFee`.
* @param amount Amount intended to save (ignored for native flow)
* @param tokenToSave Token address to save, or address(0) for native
* @param userChildContractAddress Address of the user's child contract to approve
* @param nativeFee Fee to deduct from native flows
* @return uint256 The actual amount retrieved or computed for saving
*/
function handleNativeSaving(
uint256 amount,
address tokenToSave,
address userChildContractAddress,
uint256 nativeFee
) private returns (uint256) {
// check if native currency saving
if (tokenToSave != address(0)) {
// savingToken = tokenToSave;
// amountToSave = amount;
// perform withdrawal respective
bool tokenHasBeenWithdrawn = BitsaveHelperLib.retrieveToken(
msg.sender,
tokenToSave,
amount
);
if (!tokenHasBeenWithdrawn) {
revert BitsaveHelperLib.CanNotWithdrawToken(tokenToSave);
}
// let us know you've removed the savings
emit BitsaveHelperLib.TokenWithdrawal(
msg.sender,
address(this),
amount
);
// approve child contract withdrawing token
require(
BitsaveHelperLib.approveAmount(
userChildContractAddress,
amount,
tokenToSave
),
"Savings invalid"
);
} else {
amount = msg.value - nativeFee;
}
return amount;
}
/**
* @notice Create a new saving entry for the caller by forwarding to their child contract
* @dev Caller must be registered (have a child contract). For native savings the
* call must include at least `SavingFee` in `msg.value` and the saved amount
* is taken from `msg.value - SavingFee`. For ERC-20 savings the caller must
* have previously authorized retrieval via the helper library (e.g., `approve`).
* @param nameOfSaving Human-readable name for the saving slot
* @param maturityTime UNIX timestamp when maturity occurs
* @param penaltyPercentage Penalty percentage applied on early withdrawal
* @param safeMode If true, attempt a safe conversion flow (not supported currently)
* @param tokenToSave Token address to save, or address(0) for native coin
* @param amount Amount to save (for ERC-20 flows). Ignored for native flows.
*/
function createSaving(
string memory nameOfSaving,
uint256 maturityTime,
uint8 penaltyPercentage,
bool safeMode,
address tokenToSave, // address 0 for native coin
uint256 amount // discarded for native token; takes msg.value - SavingFee instead
) public payable registeredOnly(msg.sender) {
if (msg.value < SavingFee) {
revert BitsaveHelperLib.NotEnoughToPayGasFee();
}
if (block.timestamp > maturityTime) {
revert BitsaveHelperLib.InvalidTime();
}
// NOTE: For now, no safeMode since no swap contract
if (safeMode) {
revert BitsaveHelperLib.NotSupported("No safe mode yet!");
}
// user's child contract address
address payable userChildContractAddress = getUserChildContractAddress(
msg.sender
);
// Handle token sent
uint256 amountRetrieved = handleNativeSaving(
amount,
tokenToSave,
userChildContractAddress,
SavingFee
);
// TODO: perform conversion for stableCoin
// functionality for safe mode
// if (safeMode) {
// amountToSave = crossChainSwap(
// savingToken,
// stableCoin,
// amount,
// address(this)
// );
// savingToken = stableCoin;
// }
/// send savings request to child contract with a little gas
// Initialize user's child contract
ChildBitsave userChildContract = ChildBitsave(userChildContractAddress);
userChildContract.createSaving{
value: tokenToSave == address(0) ? amountRetrieved : 0
}(
nameOfSaving,
maturityTime,
block.timestamp, // current time
penaltyPercentage,
tokenToSave,
amountRetrieved,
safeMode,
currentVaultState,
currentTotalValueLocked
);
// emit saving created
emit BitsaveHelperLib.SavingCreated(
nameOfSaving,
amountRetrieved,
tokenToSave
);
}
///
/// INCREMENT SAVING
/// the amount to add to saving
///
/// string nameOfSaving
///
/**
* @notice Add funds to an existing saving slot
* @dev Caller must be registered. For native savings, send value as `msg.value`.
* For ERC-20 flows, the child contract must be approved to withdraw the tokens
* (the helper library is used to do approvals/withdrawals).
* @param nameOfSavings Name of the saving slot to increment
* @param tokenToRetrieve Token address used to fund this increment (may be stablecoin)
* @param amount Amount to add (ignored for native where `msg.value` is used)
*/
function incrementSaving(
string memory nameOfSavings,
address tokenToRetrieve,
uint256 amount
) public payable registeredOnly(msg.sender) {
// initialize userChildContract
address payable userChildContractAddress = payable(
addressToUserBS[msg.sender]
);
ChildBitsave userChildContract = ChildBitsave(userChildContractAddress);
address savingToken = userChildContract.getSavingTokenId(nameOfSavings);
bool isNativeToken = savingToken == address(0);
// todo: perform amount conversion and everything
uint256 savingPlusAmount = amount;
// todo: check savings detail by reading the storage of userChildContract
bool isSafeMode = userChildContract.getSavingMode(nameOfSavings);
if (isSafeMode) {
// savingPlusAmount = crossChainSwap(
// userChildContract.getSavingTokenId(nameOfSavings),
// stableCoin,
// savingPlusAmount,
// address(this)
// );
tokenToRetrieve = address(stableCoin);
}
if (!isNativeToken) {
// approve child contract withdrawing token
require(
BitsaveHelperLib.approveAmount(
userChildContractAddress,
savingPlusAmount,
tokenToRetrieve
),
"Savings invalid"
);
} else {
savingPlusAmount = msg.value;
}
uint256 amountRetrieved = handleNativeSaving(
amount,
savingToken,
userChildContractAddress,
0 // inc has no fee
);
userChildContract.incrementSaving{
value: isNativeToken ? amountRetrieved : 0
}(
nameOfSavings,
amountRetrieved,
currentVaultState,
currentTotalValueLocked
);
uint256 savingBalance = userChildContract.getSavingBalance(
nameOfSavings
);
// emit saving updated
emit BitsaveHelperLib.SavingIncremented(
nameOfSavings,
amount,
savingBalance,
tokenToRetrieve
);
}
/// WITHDRAW savings
///
/// string nameOfSaving
///
/**
* @notice Withdraw a saving slot (for the caller)
* @dev Forwards the withdraw request to the caller's child contract. Caller must
* be registered. Emits `SavingWithdrawn` via helper library events.
* @param nameOfSavings Name of the saving slot to withdraw
* @return bool True on success
*/
function withdrawSaving(
string memory nameOfSavings
) public registeredOnly(msg.sender) returns (bool) {
// initialize user's child userChildContract
ChildBitsave userChildContract = ChildBitsave(
payable(addressToUserBS[msg.sender])
);
// call withdraw savings fn
userChildContract.withdrawSaving(nameOfSavings);
emit BitsaveHelperLib.SavingWithdrawn(nameOfSavings);
return true;
}
receive() external payable {}
// ---------- Private functions ---------------
/**
* @dev Internal helper overload: returns the child contract address for `myAddress`.
* @param myAddress The account whose child contract we want
* @return address payable Child contract address (or address(0) if not registered)
*/
function getUserChildContractAddress(
address myAddress
) internal view returns (address payable) {
return payable(addressToUserBS[myAddress]);
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (token/ERC721/ERC721.sol)
pragma solidity ^0.8.24;
import {IERC721} from "./IERC721.sol";
import {IERC721Metadata} from "./extensions/IERC721Metadata.sol";
import {ERC721Utils} from "./utils/ERC721Utils.sol";
import {Context} from "../../utils/Context.sol";
import {Strings} from "../../utils/Strings.sol";
import {IERC165, ERC165} from "../../utils/introspection/ERC165.sol";
import {IERC721Errors} from "../../interfaces/draft-IERC6093.sol";
/**
* @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC-721] Non-Fungible Token Standard, including
* the Metadata extension, but not including the Enumerable extension, which is available separately as
* {ERC721Enumerable}.
*/
abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Errors {
using Strings for uint256;
// Token name
string private _name;
// Token symbol
string private _symbol;
mapping(uint256 tokenId => address) private _owners;
mapping(address owner => uint256) private _balances;
mapping(uint256 tokenId => address) private _tokenApprovals;
mapping(address owner => mapping(address operator => bool)) private _operatorApprovals;
/**
* @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/// @inheritdoc IERC165
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
return
interfaceId == type(IERC721).interfaceId ||
interfaceId == type(IERC721Metadata).interfaceId ||
super.supportsInterface(interfaceId);
}
/// @inheritdoc IERC721
function balanceOf(address owner) public view virtual returns (uint256) {
if (owner == address(0)) {
revert ERC721InvalidOwner(address(0));
}
return _balances[owner];
}
/// @inheritdoc IERC721
function ownerOf(uint256 tokenId) public view virtual returns (address) {
return _requireOwned(tokenId);
}
/// @inheritdoc IERC721Metadata
function name() public view virtual returns (string memory) {
return _name;
}
/// @inheritdoc IERC721Metadata
function symbol() public view virtual returns (string memory) {
return _symbol;
}
/// @inheritdoc IERC721Metadata
function tokenURI(uint256 tokenId) public view virtual returns (string memory) {
_requireOwned(tokenId);
string memory baseURI = _baseURI();
return bytes(baseURI).length > 0 ? string.concat(baseURI, tokenId.toString()) : "";
}
/**
* @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
* token will be the concatenation of the `baseURI` and the `tokenId`. Empty
* by default, can be overridden in child contracts.
*/
function _baseURI() internal view virtual returns (string memory) {
return "";
}
/// @inheritdoc IERC721
function approve(address to, uint256 tokenId) public virtual {
_approve(to, tokenId, _msgSender());
}
/// @inheritdoc IERC721
function getApproved(uint256 tokenId) public view virtual returns (address) {
_requireOwned(tokenId);
return _getApproved(tokenId);
}
/// @inheritdoc IERC721
function setApprovalForAll(address operator, bool approved) public virtual {
_setApprovalForAll(_msgSender(), operator, approved);
}
/// @inheritdoc IERC721
function isApprovedForAll(address owner, address operator) public view virtual returns (bool) {
return _operatorApprovals[owner][operator];
}
/// @inheritdoc IERC721
function transferFrom(address from, address to, uint256 tokenId) public virtual {
if (to == address(0)) {
revert ERC721InvalidReceiver(address(0));
}
// Setting an "auth" arguments enables the `_isAuthorized` check which verifies that the token exists
// (from != 0). Therefore, it is not needed to verify that the return value is not 0 here.
address previousOwner = _update(to, tokenId, _msgSender());
if (previousOwner != from) {
revert ERC721IncorrectOwner(from, tokenId, previousOwner);
}
}
/// @inheritdoc IERC721
function safeTransferFrom(address from, address to, uint256 tokenId) public {
safeTransferFrom(from, to, tokenId, "");
}
/// @inheritdoc IERC721
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual {
transferFrom(from, to, tokenId);
ERC721Utils.checkOnERC721Received(_msgSender(), from, to, tokenId, data);
}
/**
* @dev Returns the owner of the `tokenId`. Does NOT revert if token doesn't exist
*
* IMPORTANT: Any overrides to this function that add ownership of tokens not tracked by the
* core ERC-721 logic MUST be matched with the use of {_increaseBalance} to keep balances
* consistent with ownership. The invariant to preserve is that for any address `a` the value returned by
* `balanceOf(a)` must be equal to the number of tokens such that `_ownerOf(tokenId)` is `a`.
*/
function _ownerOf(uint256 tokenId) internal view virtual returns (address) {
return _owners[tokenId];
}
/**
* @dev Returns the approved address for `tokenId`. Returns 0 if `tokenId` is not minted.
*/
function _getApproved(uint256 tokenId) internal view virtual returns (address) {
return _tokenApprovals[tokenId];
}
/**
* @dev Returns whether `spender` is allowed to manage `owner`'s tokens, or `tokenId` in
* particular (ignoring whether it is owned by `owner`).
*
* WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not verify this
* assumption.
*/
function _isAuthorized(address owner, address spender, uint256 tokenId) internal view virtual returns (bool) {
return
spender != address(0) &&
(owner == spender || isApprovedForAll(owner, spender) || _getApproved(tokenId) == spender);
}
/**
* @dev Checks if `spender` can operate on `tokenId`, assuming the provided `owner` is the actual owner.
* Reverts if:
* - `spender` does not have approval from `owner` for `tokenId`.
* - `spender` does not have approval to manage all of `owner`'s assets.
*
* WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not verify this
* assumption.
*/
function _checkAuthorized(address owner, address spender, uint256 tokenId) internal view virtual {
if (!_isAuthorized(owner, spender, tokenId)) {
if (owner == address(0)) {
revert ERC721NonexistentToken(tokenId);
} else {
revert ERC721InsufficientApproval(spender, tokenId);
}
}
}
/**
* @dev Unsafe write access to the balances, used by extensions that "mint" tokens using an {ownerOf} override.
*
* NOTE: the value is limited to type(uint128).max. This protect against _balance overflow. It is unrealistic that
* a uint256 would ever overflow from increments when these increments are bounded to uint128 values.
*
* WARNING: Increasing an account's balance using this function tends to be paired with an override of the
* {_ownerOf} function to resolve the ownership of the corresponding tokens so that balances and ownership
* remain consistent with one another.
*/
function _increaseBalance(address account, uint128 value) internal virtual {
unchecked {
_balances[account] += value;
}
}
/**
* @dev Transfers `tokenId` from its current owner to `to`, or alternatively mints (or burns) if the current owner
* (or `to`) is the zero address. Returns the owner of the `tokenId` before the update.
*
* The `auth` argument is optional. If the value passed is non 0, then this function will check that
* `auth` is either the owner of the token, or approved to operate on the token (by the owner).
*
* Emits a {Transfer} event.
*
* NOTE: If overriding this function in a way that tracks balances, see also {_increaseBalance}.
*/
function _update(address to, uint256 tokenId, address auth) internal virtual returns (address) {
address from = _ownerOf(tokenId);
// Perform (optional) operator check
if (auth != address(0)) {
_checkAuthorized(from, auth, tokenId);
}
// Execute the update
if (from != address(0)) {
// Clear approval. No need to re-authorize or emit the Approval event
_approve(address(0), tokenId, address(0), false);
unchecked {
_balances[from] -= 1;
}
}
if (to != address(0)) {
unchecked {
_balances[to] += 1;
}
}
_owners[tokenId] = to;
emit Transfer(from, to, tokenId);
return from;
}
/**
* @dev Mints `tokenId` and transfers it to `to`.
*
* WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
*
* Requirements:
*
* - `tokenId` must not exist.
* - `to` cannot be the zero address.
*
* Emits a {Transfer} event.
*/
function _mint(address to, uint256 tokenId) internal {
if (to == address(0)) {
revert ERC721InvalidReceiver(address(0));
}
address previousOwner = _update(to, tokenId, address(0));
if (previousOwner != address(0)) {
revert ERC721InvalidSender(address(0));
}
}
/**
* @dev Mints `tokenId`, transfers it to `to` and checks for `to` acceptance.
*
* Requirements:
*
* - `tokenId` must not exist.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function _safeMint(address to, uint256 tokenId) internal {
_safeMint(to, tokenId, "");
}
/**
* @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
* forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
*/
function _safeMint(address to, uint256 tokenId, bytes memory data) internal virtual {
_mint(to, tokenId);
ERC721Utils.checkOnERC721Received(_msgSender(), address(0), to, tokenId, data);
}
/**
* @dev Destroys `tokenId`.
* The approval is cleared when the token is burned.
* This is an internal function that does not check if the sender is authorized to operate on the token.
*
* Requirements:
*
* - `tokenId` must exist.
*
* Emits a {Transfer} event.
*/
function _burn(uint256 tokenId) internal {
address previousOwner = _update(address(0), tokenId, address(0));
if (previousOwner == address(0)) {
revert ERC721NonexistentToken(tokenId);
}
}
/**
* @dev Transfers `tokenId` from `from` to `to`.
* As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
*
* Emits a {Transfer} event.
*/
function _transfer(address from, address to, uint256 tokenId) internal {
if (to == address(0)) {
revert ERC721InvalidReceiver(address(0));
}
address previousOwner = _update(to, tokenId, address(0));
if (previousOwner == address(0)) {
revert ERC721NonexistentToken(tokenId);
} else if (previousOwner != from) {
revert ERC721IncorrectOwner(from, tokenId, previousOwner);
}
}
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking that contract recipients
* are aware of the ERC-721 standard to prevent tokens from being forever locked.
*
* `data` is additional data, it has no specified format and it is sent in call to `to`.
*
* This internal function is like {safeTransferFrom} in the sense that it invokes
* {IERC721Receiver-onERC721Received} on the receiver, and can be used to e.g.
* implement alternative mechanisms to perform token transfer, such as signature-based.
*
* Requirements:
*
* - `tokenId` token must exist and be owned by `from`.
* - `to` cannot be the zero address.
* - `from` cannot be the zero address.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function _safeTransfer(address from, address to, uint256 tokenId) internal {
_safeTransfer(from, to, tokenId, "");
}
/**
* @dev Same as {xref-ERC721-_safeTransfer-address-address-uint256-}[`_safeTransfer`], with an additional `data` parameter which is
* forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
*/
function _safeTransfer(address from, address to, uint256 tokenId, bytes memory data) internal virtual {
_transfer(from, to, tokenId);
ERC721Utils.checkOnERC721Received(_msgSender(), from, to, tokenId, data);
}
/**
* @dev Approve `to` to operate on `tokenId`
*
* The `auth` argument is optional. If the value passed is non 0, then this function will check that `auth` is
* either the owner of the token, or approved to operate on all tokens held by this owner.
*
* Emits an {Approval} event.
*
* Overrides to this logic should be done to the variant with an additional `bool emitEvent` argument.
*/
function _approve(address to, uint256 tokenId, address auth) internal {
_approve(to, tokenId, auth, true);
}
/**
* @dev Variant of `_approve` with an optional flag to enable or disable the {Approval} event. The event is not
* emitted in the context of transfers.
*/
function _approve(address to, uint256 tokenId, address auth, bool emitEvent) internal virtual {
// Avoid reading the owner unless necessary
if (emitEvent || auth != address(0)) {
address owner = _requireOwned(tokenId);
// We do not use _isAuthorized because single-token approvals should not be able to call approve
if (auth != address(0) && owner != auth && !isApprovedForAll(owner, auth)) {
revert ERC721InvalidApprover(auth);
}
if (emitEvent) {
emit Approval(owner, to, tokenId);
}
}
_tokenApprovals[tokenId] = to;
}
/**
* @dev Approve `operator` to operate on all of `owner` tokens
*
* Requirements:
* - operator can't be the address zero.
*
* Emits an {ApprovalForAll} event.
*/
function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
if (operator == address(0)) {
revert ERC721InvalidOperator(operator);
}
_operatorApprovals[owner][operator] = approved;
emit ApprovalForAll(owner, operator, approved);
}
/**
* @dev Reverts if the `tokenId` doesn't have a current owner (it hasn't been minted, or it has been burned).
* Returns the owner.
*
* Overrides to ownership logic should be done to {_ownerOf}.
*/
function _requireOwned(uint256 tokenId) internal view returns (address) {
address owner = _ownerOf(tokenId);
if (owner == address(0)) {
revert ERC721NonexistentToken(tokenId);
}
return owner;
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC20/IERC20.sol)
pragma solidity >=0.4.16;
/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 value) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 value) external returns (bool);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./libraries/bitsaveHelperLib.sol";
import "./Bitsave.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
/**
* @title ChildBitsave
* @notice Per-user child contract deployed by `Bitsave` to manage individual savings
* entries. Each saving slot contains metadata and accumulated interest (points).
* @dev Functions that modify state are restricted to be callable only by the parent
* `Bitsave` contract using the `bitsaveOnly` modifier.
*/
contract ChildBitsave is ReentrancyGuard {
// *** Contract parameters ***
address payable public bitsaveAddress;
IERC20 public stableCoin;
address public ownerAddress;
// *** Contract Storage ***
// total interests gathered; v1 shows points
uint256 public totalPoints;
// structure of saving data
/**
* @dev SavingDataStruct holds all data for a single saving slot
* @param isValid Whether this saving slot is active
* @param amount Principal amount stored for the saving (in token smallest unit or wei)
* @param tokenId Address of token stored (address(0) for native)
* @param interestAccumulated Interest/points accumulated for this saving
* @param startTime UNIX timestamp when the saving started
* @param penaltyPercentage Penalty applied for early withdrawal (0-100)
* @param maturityTime UNIX timestamp when saving matures
* @param isSafeMode If true, the saving uses the protocol's safe conversion flow
*/
struct SavingDataStruct {
bool isValid;
uint256 amount;
address tokenId;
uint256 interestAccumulated;
uint256 startTime;
uint256 penaltyPercentage;
uint256 maturityTime;
bool isSafeMode;
}
// mapping of name of saving to individual saving
mapping(string => SavingDataStruct) public savings;
struct SavingsNamesObj {
string[] savingsNames;
}
/**
* @dev Update the internal `totalPoints` counter. Private helper.
* @param newPoint Amount of points to add
*/
function updatePoints(uint256 newPoint) private {
totalPoints = totalPoints + newPoint;
}
/**
* @notice Calculate interest (points) for a saving interval and update totals
* @dev Uses `BitsaveHelperLib.calculateInterestWithBTS` to compute accumulated interest
* @param savingAmount Principal amount for the calculation
* @param endTime UNIX timestamp representing the end (maturity) time
* @param startTime UNIX timestamp representing the start time
* @param currentVaultState Vault state parameter used in calculation
* @param currentTotalValueLocked TVL parameter used in calculation
* @return accumulatedInterest The computed interest/points for this interval
*/
function calculateAndUpdatePoints(
uint256 savingAmount,
uint256 endTime,
uint256 startTime,
uint256 currentVaultState,
uint256 currentTotalValueLocked
) internal returns (uint256 accumulatedInterest) {
accumulatedInterest = BitsaveHelperLib.calculateInterestWithBTS(
savingAmount,
endTime - startTime, // time interval
currentVaultState,
currentTotalValueLocked
);
totalPoints += accumulatedInterest;
}
SavingsNamesObj private savingsNamesVar;
/**
* @notice Deploy a ChildBitsave for a single user
* @param _ownerAddress The account that owns this child contract (the user)
* @param _stableCoin Address of the stablecoin used by the protocol
* @dev The `msg.sender` is expected to be the parent `Bitsave` contract which
* becomes `bitsaveAddress` and is authorized to call `bitsaveOnly` methods.
*/
constructor(address _ownerAddress, address _stableCoin) payable {
// save bitsaveAddress first // todo: retrieve correct address
bitsaveAddress = payable(msg.sender);
// store owner's address
ownerAddress = payable(_ownerAddress);
// store stable coin
stableCoin = IERC20(payable(_stableCoin));
// storage
totalPoints = 0;
}
/**
* @dev Restrict function calls to the parent `Bitsave` contract only
*/
modifier bitsaveOnly() {
if (msg.sender != bitsaveAddress) {
revert BitsaveHelperLib.CallNotFromBitsave();
}
_;
}
/**
* @dev Private helper to push a saving name into the `savingsNamesVar` list
* @param _name The name/key of the saving to add
*/
function addSavingName(string memory _name) private {
savingsNamesVar.savingsNames.push(_name);
}
// Contract Getters
/**
* @notice Get whether a saving is in safe mode
* @param nameOfSaving Name of the saving slot
* @return bool True if the saving is in safe mode
*/
function getSavingMode(
string memory nameOfSaving
) external view returns (bool) {
return savings[nameOfSaving].isSafeMode;
}
/**
* @notice Read accumulated interest/points for a saving
* @param nameOfSaving Name of the saving slot
* @return uint256 Accumulated interest/points
*/
function getSavingInterest(
string memory nameOfSaving
) external view returns (uint256) {
return savings[nameOfSaving].interestAccumulated;
}
/**
* @notice Get the token id (address) used by a saving slot
* @param nameOfSaving Name of the saving slot
* @return address Token contract address, or address(0) for native
*/
function getSavingTokenId(
string memory nameOfSaving
) external view returns (address) {
return savings[nameOfSaving].tokenId;
}
/**
* @notice Get the principal balance for a saving slot
* @param nameOfSaving Name of the saving slot
* @return uint256 Principal amount stored
*/
function getSavingBalance(
string memory nameOfSaving
) external view returns (uint256) {
return savings[nameOfSaving].amount;
}
/**
* @notice Return an object containing the list of saving names for this user
* @return SavingsNamesObj Struct holding the array of saving slot names
*/
function getSavingsNames() external view returns (SavingsNamesObj memory) {
return savingsNamesVar;
}
/**
* @notice Get the full saving data struct for a saving slot
* @param nameOfSaving Name of the saving slot
* @return SavingDataStruct Full struct containing saving metadata and amounts
*/
function getSaving(
string memory nameOfSaving
) public view returns (SavingDataStruct memory) {
return savings[nameOfSaving];
}
// functionality to create savings
/**
* @notice Create a new saving slot for the owner via the parent Bitsave contract
* @dev Callable only by the parent `Bitsave` contract. Performs validations on
* maturity time and retrieves tokens/value using the helper library as needed.
* @param name Human-readable unique name for this saving slot
* @param maturityTime UNIX timestamp when the saving matures
* @param startTime UNIX timestamp representing the saving start (passed from parent)
* @param penaltyPercentage Penalty percentage for early withdrawal (0-100)
* @param tokenId Token address used for this saving (address(0) for native)
* @param amountToRetrieve Amount retrieved from the parent for this saving (or 0)
* @param isSafeMode Whether the saving uses the safe conversion flow
* @param currentVaultState Vault state parameter used for interest calculation
* @param currentTotalValueLocked TVL parameter used for interest calculation
* @return uint256 Returns 1 on success (legacy return)
*/
function createSaving(
string memory name,
uint256 maturityTime,
uint256 startTime,
uint8 penaltyPercentage,
address tokenId,
uint256 amountToRetrieve,
bool isSafeMode,
uint256 currentVaultState,
uint256 currentTotalValueLocked
) public payable bitsaveOnly nonReentrant returns (uint256) {
// ensure saving does not exist; !
if (savings[name].isValid) revert BitsaveHelperLib.InvalidSaving();
// check if end time valid
if (maturityTime < startTime) revert BitsaveHelperLib.InvalidTime();
if (maturityTime < block.timestamp)
revert BitsaveHelperLib.InvalidTime();
uint256 savingsAmount = amountToRetrieve;
if (isSafeMode) {
BitsaveHelperLib.retrieveToken(
bitsaveAddress,
address(stableCoin),
amountToRetrieve
);
} else {
if (tokenId != address(0)) {
BitsaveHelperLib.retrieveToken(
bitsaveAddress,
tokenId,
amountToRetrieve
);
} else {
// case native token
savingsAmount = msg.value;
}
}
uint256 accumulatedInterest = calculateAndUpdatePoints(
savingsAmount,
maturityTime,
startTime,
currentVaultState,
currentTotalValueLocked
);
// store saving to map of savings
savings[name] = SavingDataStruct({
amount: savingsAmount,
maturityTime: maturityTime,
interestAccumulated: accumulatedInterest,
startTime: startTime,
tokenId: tokenId,
penaltyPercentage: penaltyPercentage,
isSafeMode: isSafeMode,
isValid: true
});
// addSavingName(name);
addSavingName(name);
emit BitsaveHelperLib.SavingCreated(name, amountToRetrieve, tokenId);
return 1;
}
// functionality to add to savings
/**
* @notice Increment an existing saving slot by adding funds
* @dev Callable only by parent `Bitsave`. Handles native and ERC-20 flows and
* updates accumulated interest accordingly.
* @param name Name of the saving slot to increment
* @param savingPlusAmount Amount to add (for ERC-20 flows). For native flows the
* `msg.value` will be used and must be >= expected amount.
* @param currentVaultState Vault state parameter forwarded for interest calc
* @param currentTotalValueLocked TVL parameter forwarded for interest calc
* @return uint256 The updated accumulated interest for the saving
*/
function incrementSaving(
string memory name,
uint256 savingPlusAmount,
uint256 currentVaultState,
uint256 currentTotalValueLocked
) public payable bitsaveOnly nonReentrant returns (uint256) {
// fetch savings data
SavingDataStruct storage toFundSavings = savings[name];
if (!toFundSavings.isValid) revert BitsaveHelperLib.InvalidSaving();
if (block.timestamp > toFundSavings.maturityTime)
revert BitsaveHelperLib.InvalidTime();
bool isNativeToken = toFundSavings.tokenId == address(0);
// handle retrieving token from contract
if (toFundSavings.isSafeMode) {
BitsaveHelperLib.retrieveToken(
bitsaveAddress,
address(stableCoin),
savingPlusAmount
);
} else {
if (!isNativeToken) {
BitsaveHelperLib.retrieveToken(
bitsaveAddress,
toFundSavings.tokenId,
savingPlusAmount
);
} else {
require(
msg.value >= savingPlusAmount,
"Invalid saving increment value sent"
);
savingPlusAmount = msg.value;
}
}
uint256 extraInterest = calculateAndUpdatePoints(
savingPlusAmount,
toFundSavings.maturityTime,
block.timestamp,
currentVaultState,
currentTotalValueLocked
);
// calculate new interest
toFundSavings.interestAccumulated =
toFundSavings.interestAccumulated +
extraInterest;
toFundSavings.amount = toFundSavings.amount + savingPlusAmount;
// save new savings data
savings[name] = toFundSavings;
emit BitsaveHelperLib.SavingIncremented(
name,
savingPlusAmount,
toFundSavings.amount,
toFundSavings.tokenId
);
return toFundSavings.interestAccumulated;
}
/**
* @notice Withdraw a saving and deliver funds to the owner (handles penalties)
* @dev Callable only by parent `Bitsave`. Applies penalty if withdrawn early and
* transfers the resulting amount to the owner. For safe-mode savings a
* conversion call back to the parent is performed.
* @param name Name of the saving slot to withdraw
* @return string Human-readable success message on successful delivery
*/
function withdrawSaving(
string memory name
) public payable bitsaveOnly nonReentrant returns (string memory) {
SavingDataStruct storage toWithdrawSavings = savings[name];
// check if saving exit
if (!toWithdrawSavings.isValid) revert BitsaveHelperLib.InvalidSaving();
uint256 amountToWithdraw = toWithdrawSavings.amount;
Bitsave bitsave = Bitsave(bitsaveAddress);
// check if saving is mature
if (block.timestamp < toWithdrawSavings.maturityTime) {
// remove penalty from savings
amountToWithdraw =
(toWithdrawSavings.amount *
(100 - toWithdrawSavings.penaltyPercentage)) /
100;
// transfer remnant to main contract
BitsaveHelperLib.transferToken(
toWithdrawSavings.tokenId,
bitsaveAddress,
toWithdrawSavings.amount - amountToWithdraw
);
} else {
// TODO: handle interest point management
// bitsave.handleUsersInterest(
// name,
// address(this),
// ownerAddress
// );
}
// first gate value
savings[name].isValid = false;
// send the savings amount to withdraw
address tokenId = toWithdrawSavings.tokenId;
// function can be abstracted for sending token out
bool isDelivered = false;
if (toWithdrawSavings.isSafeMode) {
// approve withdrawal from parent contract
BitsaveHelperLib.approveAmount(
bitsaveAddress,
amountToWithdraw,
address(stableCoin)
);
// call parent for conversion
isDelivered = bitsave.sendAsOriginalToken(
tokenId,
amountToWithdraw,
ownerAddress
);
} else {
// todo: handle sending penalty to parent contract
if (tokenId == address(0)) {
(bool sent, bytes memory data) = ownerAddress.call{
value: amountToWithdraw
}("");
require(sent, "Couldn't send funds");
isDelivered = sent;
} else {
isDelivered = BitsaveHelperLib.transferToken(
toWithdrawSavings.tokenId,
ownerAddress,
amountToWithdraw
);
}
}
// Delete savings; ensure saving is deleted/made invalid
if (isDelivered) {
savings[name].isValid = false;
emit BitsaveHelperLib.SavingWithdrawn(name);
return "savings withdrawn successfully";
}
revert();
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "prb-math/contracts/PRBMathUD60x18.sol";
/**
* @title BitsaveHelperLib
* @notice Small utility library used by Bitsave and ChildBitsave for token handling,
* approvals and the Bitsave interest calculation helpers.
* @dev Uses PRBMath for fixed-point math and OpenZeppelin's SafeERC20 for safe transfers.
*/
library BitsaveHelperLib {
using PRBMathUD60x18 for uint256;
using SafeERC20 for IERC20;
// Constants
/// @notice Transaction charge used as a default constant (in wei)
uint256 public constant txnCharge = 0.02 ether;
/// @notice Maximum supply used by the bitsave interest algorithm
uint256 public constant maxSupply = 100_000_000;
/// @notice Internal totalSupply constant used by the interest algorithm
uint256 public constant totalSupply = 15_000_000;
/// @notice Seconds in a year (approximation) used to convert intervals to years
uint256 public constant yearInSeconds = 3600 * 24 * 365;
/// @notice Divisor applied in interest math to scale values into fixed point
uint256 public constant divisor = 1_000 ether;
// Errors
error WrongGasContract();
error NotEnoughToPayGasFee();
error AmountNotEnough();
error InvalidTime();
error UserNotRegistered();
error InvalidSaving();
error CanNotWithdrawToken(address);
error NotSupported(string);
error MasterCallRequired();
// child contract specific
error CallNotFromBitsave();
// Events
event JoinedBitsave(address indexed userAddress);
event SavingCreated(
string indexed nameOfSaving,
uint256 amount,
address token
);
event SavingIncremented(
string indexed nameOfSaving,
uint256 amountAdded,
uint256 totalAmountNow,
address token
);
event SavingWithdrawn(string indexed nameOfSaving);
event TokenWithdrawal(
address indexed from,
address indexed to,
uint256 amount
);
event Received(address indexed, uint256);
event SystemFaucetDrip(address indexed token, uint256 value);
/**
* @notice Force-approve `toApproveUserAddress` for `amountToApprove` on `targetToken`
* @dev Uses OpenZeppelin's SafeERC20 `forceApprove` (non-standard helper) to set allowance
* @param toApproveUserAddress The address to approve (spender)
* @param amountToApprove Amount of tokens to allow
* @param targetToken ERC-20 token contract address
* @return bool Always returns true on success
*/
function approveAmount(
address toApproveUserAddress,
uint256 amountToApprove,
address targetToken
) internal returns (bool) {
IERC20 token = IERC20(targetToken);
token.forceApprove(toApproveUserAddress, amountToApprove);
return true;
}
/**
* @notice Retrieve `amountToWithdraw` tokens from `toApproveUserAddress` into this contract
* @dev Requires the user to have previously approved this contract to pull `amountToWithdraw`.
* @param toApproveUserAddress The user address from which tokens are pulled
* @param targetToken ERC-20 token contract address
* @param amountToWithdraw Amount to transfer (token smallest unit)
* @return bool True on success
*/
function retrieveToken(
address toApproveUserAddress,
address targetToken,
uint256 amountToWithdraw
) internal returns (bool) {
// first request approval
require(
// approveAmount(toApproveUserAddress, amountToWithdraw, targetToken),
IERC20(targetToken).allowance(
toApproveUserAddress,
address(this)
) >= amountToWithdraw,
CanNotWithdrawToken(targetToken)
);
IERC20(targetToken).safeTransferFrom(
toApproveUserAddress,
address(this),
amountToWithdraw
);
return true;
}
/**
* @notice Simple fallback interest calculator (1% of amount)
* @param amount Principal amount
* @return accumulatedInterest Computed interest (amount / 100)
*/
function calculateInterest(
uint256 amount
)
internal
pure
returns (
// uint256 currBitsPointValue
uint256 accumulatedInterest
)
{
accumulatedInterest = amount / 100;
}
/**
* @notice Calculate interest/points using the Bitsave formula
* @dev Uses PRBMath fixed-point helpers for part of the calculation. The formula
* uses protocol-wide constants and provided vault/T LV parameters.
* @param principal Principal amount to calculate interest on
* @param timeInterval Time duration in seconds; converted to years for the formula
* @param vaultState Current vault state parameter (internal)
* @param totalValueLocked Current total value locked parameter (internal)
* @return accumulatedInterest Computed accumulated interest / points
*/
function calculateInterestWithBTS(
// External data
uint256 principal,
uint256 timeInterval, // will be converted to years
// Internal data
uint256 vaultState,
uint256 totalValueLocked
) internal pure returns (uint256 accumulatedInterest) {
uint256 crp = ((totalSupply - vaultState).div(vaultState)).mul(100);
uint256 bsRate = maxSupply.div(crp * totalValueLocked);
uint256 yearsTaken = timeInterval.div(yearInSeconds);
accumulatedInterest = (
(principal * bsRate * yearsTaken).div(100 * divisor)
).toUint();
}
/**
* @notice Get the ERC-20 balance of `tokenAddr` for this contract
* @param tokenAddr Token contract address
* @return uint256 Token balance of this contract
*/
function tokenBalance(address tokenAddr) internal view returns (uint256) {
IERC20 token = IERC20(tokenAddr);
return token.balanceOf(address(this));
}
/**
* @notice Transfer `amount` of `token` to `recipient` and emit TokenWithdrawal
* @param token Token contract address
* @param recipient Address receiving tokens
* @param amount Amount to transfer
* @return isDelivered True on successful transfer
*/
function transferToken(
address token,
address recipient,
uint256 amount
) internal returns (bool isDelivered) {
IERC20 Token = IERC20(token);
Token.safeTransfer(recipient, amount);
isDelivered = true;
emit TokenWithdrawal(address(this), recipient, amount);
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
import {StorageSlot} from "./StorageSlot.sol";
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*
* IMPORTANT: Deprecated. This storage-based reentrancy guard will be removed and replaced
* by the {ReentrancyGuardTransient} variant in v6.0.
*
* @custom:stateless
*/
abstract contract ReentrancyGuard {
using StorageSlot for bytes32;
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant REENTRANCY_GUARD_STORAGE =
0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_reentrancyGuardStorageSlot().getUint256Slot().value = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
/**
* @dev A `view` only version of {nonReentrant}. Use to block view functions
* from being called, preventing reading from inconsistent contract state.
*
* CAUTION: This is a "view" modifier and does not change the reentrancy
* status. Use it only on view functions. For payable or non-payable functions,
* use the standard {nonReentrant} modifier instead.
*/
modifier nonReentrantView() {
_nonReentrantBeforeView();
_;
}
function _nonReentrantBeforeView() private view {
if (_reentrancyGuardEntered()) {
revert ReentrancyGuardReentrantCall();
}
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
_nonReentrantBeforeView();
// Any calls to nonReentrant after this point will fail
_reentrancyGuardStorageSlot().getUint256Slot().value = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_reentrancyGuardStorageSlot().getUint256Slot().value = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _reentrancyGuardStorageSlot().getUint256Slot().value == ENTERED;
}
function _reentrancyGuardStorageSlot() internal pure virtual returns (bytes32) {
return REENTRANCY_GUARD_STORAGE;
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC721/IERC721.sol)
pragma solidity >=0.6.2;
import {IERC165} from "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC-721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC-721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or
* {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC-721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the address zero.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC721/extensions/IERC721Metadata.sol)
pragma solidity >=0.6.2;
import {IERC721} from "../IERC721.sol";
/**
* @title ERC-721 Non-Fungible Token Standard, optional metadata extension
* @dev See https://eips.ethereum.org/EIPS/eip-721
*/
interface IERC721Metadata is IERC721 {
/**
* @dev Returns the token collection name.
*/
function name() external view returns (string memory);
/**
* @dev Returns the token collection symbol.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
*/
function tokenURI(uint256 tokenId) external view returns (string memory);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (token/ERC721/utils/ERC721Utils.sol)
pragma solidity ^0.8.20;
import {IERC721Receiver} from "../IERC721Receiver.sol";
import {IERC721Errors} from "../../../interfaces/draft-IERC6093.sol";
/**
* @dev Library that provides common ERC-721 utility functions.
*
* See https://eips.ethereum.org/EIPS/eip-721[ERC-721].
*
* _Available since v5.1._
*/
library ERC721Utils {
/**
* @dev Performs an acceptance check for the provided `operator` by calling {IERC721Receiver-onERC721Received}
* on the `to` address. The `operator` is generally the address that initiated the token transfer (i.e. `msg.sender`).
*
* The acceptance call is not executed and treated as a no-op if the target address doesn't contain code (i.e. an EOA).
* Otherwise, the recipient must implement {IERC721Receiver-onERC721Received} and return the acceptance magic value to accept
* the transfer.
*/
function checkOnERC721Received(
address operator,
address from,
address to,
uint256 tokenId,
bytes memory data
) internal {
if (to.code.length > 0) {
try IERC721Receiver(to).onERC721Received(operator, from, tokenId, data) returns (bytes4 retval) {
if (retval != IERC721Receiver.onERC721Received.selector) {
// Token rejected
revert IERC721Errors.ERC721InvalidReceiver(to);
}
} catch (bytes memory reason) {
if (reason.length == 0) {
// non-IERC721Receiver implementer
revert IERC721Errors.ERC721InvalidReceiver(to);
} else {
assembly ("memory-safe") {
revert(add(reason, 0x20), mload(reason))
}
}
}
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/Strings.sol)
pragma solidity ^0.8.24;
import {Math} from "./math/Math.sol";
import {SafeCast} from "./math/SafeCast.sol";
import {SignedMath} from "./math/SignedMath.sol";
import {Bytes} from "./Bytes.sol";
/**
* @dev String operations.
*/
library Strings {
using SafeCast for *;
bytes16 private constant HEX_DIGITS = "0123456789abcdef";
uint8 private constant ADDRESS_LENGTH = 20;
uint256 private constant SPECIAL_CHARS_LOOKUP =
(1 << 0x08) | // backspace
(1 << 0x09) | // tab
(1 << 0x0a) | // newline
(1 << 0x0c) | // form feed
(1 << 0x0d) | // carriage return
(1 << 0x22) | // double quote
(1 << 0x5c); // backslash
/**
* @dev The `value` string doesn't fit in the specified `length`.
*/
error StringsInsufficientHexLength(uint256 value, uint256 length);
/**
* @dev The string being parsed contains characters that are not in scope of the given base.
*/
error StringsInvalidChar();
/**
* @dev The string being parsed is not a properly formatted address.
*/
error StringsInvalidAddressFormat();
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = Math.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
assembly ("memory-safe") {
ptr := add(add(buffer, 0x20), length)
}
while (true) {
ptr--;
assembly ("memory-safe") {
mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
/**
* @dev Converts a `int256` to its ASCII `string` decimal representation.
*/
function toStringSigned(int256 value) internal pure returns (string memory) {
return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, Math.log256(value) + 1);
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
uint256 localValue = value;
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = HEX_DIGITS[localValue & 0xf];
localValue >>= 4;
}
if (localValue != 0) {
revert StringsInsufficientHexLength(value, length);
}
return string(buffer);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal
* representation.
*/
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its checksummed ASCII `string` hexadecimal
* representation, according to EIP-55.
*/
function toChecksumHexString(address addr) internal pure returns (string memory) {
bytes memory buffer = bytes(toHexString(addr));
// hash the hex part of buffer (skip length + 2 bytes, length 40)
uint256 hashValue;
assembly ("memory-safe") {
hashValue := shr(96, keccak256(add(buffer, 0x22), 40))
}
for (uint256 i = 41; i > 1; --i) {
// possible values for buffer[i] are 48 (0) to 57 (9) and 97 (a) to 102 (f)
if (hashValue & 0xf > 7 && uint8(buffer[i]) > 96) {
// case shift by xoring with 0x20
buffer[i] ^= 0x20;
}
hashValue >>= 4;
}
return string(buffer);
}
/**
* @dev Converts a `bytes` buffer to its ASCII `string` hexadecimal representation.
*/
function toHexString(bytes memory input) internal pure returns (string memory) {
unchecked {
bytes memory buffer = new bytes(2 * input.length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 0; i < input.length; ++i) {
uint8 v = uint8(input[i]);
buffer[2 * i + 2] = HEX_DIGITS[v >> 4];
buffer[2 * i + 3] = HEX_DIGITS[v & 0xf];
}
return string(buffer);
}
}
/**
* @dev Returns true if the two strings are equal.
*/
function equal(string memory a, string memory b) internal pure returns (bool) {
return Bytes.equal(bytes(a), bytes(b));
}
/**
* @dev Parse a decimal string and returns the value as a `uint256`.
*
* Requirements:
* - The string must be formatted as `[0-9]*`
* - The result must fit into an `uint256` type
*/
function parseUint(string memory input) internal pure returns (uint256) {
return parseUint(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseUint-string} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `[0-9]*`
* - The result must fit into an `uint256` type
*/
function parseUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
(bool success, uint256 value) = tryParseUint(input, begin, end);
if (!success) revert StringsInvalidChar();
return value;
}
/**
* @dev Variant of {parseUint-string} that returns false if the parsing fails because of an invalid character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseUint(string memory input) internal pure returns (bool success, uint256 value) {
return _tryParseUintUncheckedBounds(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseUint-string-uint256-uint256} that returns false if the parsing fails because of an invalid
* character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseUint(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, uint256 value) {
if (end > bytes(input).length || begin > end) return (false, 0);
return _tryParseUintUncheckedBounds(input, begin, end);
}
/**
* @dev Implementation of {tryParseUint-string-uint256-uint256} that does not check bounds. Caller should make sure that
* `begin <= end <= input.length`. Other inputs would result in undefined behavior.
*/
function _tryParseUintUncheckedBounds(
string memory input,
uint256 begin,
uint256 end
) private pure returns (bool success, uint256 value) {
bytes memory buffer = bytes(input);
uint256 result = 0;
for (uint256 i = begin; i < end; ++i) {
uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
if (chr > 9) return (false, 0);
result *= 10;
result += chr;
}
return (true, result);
}
/**
* @dev Parse a decimal string and returns the value as a `int256`.
*
* Requirements:
* - The string must be formatted as `[-+]?[0-9]*`
* - The result must fit in an `int256` type.
*/
function parseInt(string memory input) internal pure returns (int256) {
return parseInt(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseInt-string} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `[-+]?[0-9]*`
* - The result must fit in an `int256` type.
*/
function parseInt(string memory input, uint256 begin, uint256 end) internal pure returns (int256) {
(bool success, int256 value) = tryParseInt(input, begin, end);
if (!success) revert StringsInvalidChar();
return value;
}
/**
* @dev Variant of {parseInt-string} that returns false if the parsing fails because of an invalid character or if
* the result does not fit in a `int256`.
*
* NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
*/
function tryParseInt(string memory input) internal pure returns (bool success, int256 value) {
return _tryParseIntUncheckedBounds(input, 0, bytes(input).length);
}
uint256 private constant ABS_MIN_INT256 = 2 ** 255;
/**
* @dev Variant of {parseInt-string-uint256-uint256} that returns false if the parsing fails because of an invalid
* character or if the result does not fit in a `int256`.
*
* NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
*/
function tryParseInt(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, int256 value) {
if (end > bytes(input).length || begin > end) return (false, 0);
return _tryParseIntUncheckedBounds(input, begin, end);
}
/**
* @dev Implementation of {tryParseInt-string-uint256-uint256} that does not check bounds. Caller should make sure that
* `begin <= end <= input.length`. Other inputs would result in undefined behavior.
*/
function _tryParseIntUncheckedBounds(
string memory input,
uint256 begin,
uint256 end
) private pure returns (bool success, int256 value) {
bytes memory buffer = bytes(input);
// Check presence of a negative sign.
bytes1 sign = begin == end ? bytes1(0) : bytes1(_unsafeReadBytesOffset(buffer, begin)); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
bool positiveSign = sign == bytes1("+");
bool negativeSign = sign == bytes1("-");
uint256 offset = (positiveSign || negativeSign).toUint();
(bool absSuccess, uint256 absValue) = tryParseUint(input, begin + offset, end);
if (absSuccess && absValue < ABS_MIN_INT256) {
return (true, negativeSign ? -int256(absValue) : int256(absValue));
} else if (absSuccess && negativeSign && absValue == ABS_MIN_INT256) {
return (true, type(int256).min);
} else return (false, 0);
}
/**
* @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as a `uint256`.
*
* Requirements:
* - The string must be formatted as `(0x)?[0-9a-fA-F]*`
* - The result must fit in an `uint256` type.
*/
function parseHexUint(string memory input) internal pure returns (uint256) {
return parseHexUint(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseHexUint-string} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `(0x)?[0-9a-fA-F]*`
* - The result must fit in an `uint256` type.
*/
function parseHexUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
(bool success, uint256 value) = tryParseHexUint(input, begin, end);
if (!success) revert StringsInvalidChar();
return value;
}
/**
* @dev Variant of {parseHexUint-string} that returns false if the parsing fails because of an invalid character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseHexUint(string memory input) internal pure returns (bool success, uint256 value) {
return _tryParseHexUintUncheckedBounds(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseHexUint-string-uint256-uint256} that returns false if the parsing fails because of an
* invalid character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseHexUint(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, uint256 value) {
if (end > bytes(input).length || begin > end) return (false, 0);
return _tryParseHexUintUncheckedBounds(input, begin, end);
}
/**
* @dev Implementation of {tryParseHexUint-string-uint256-uint256} that does not check bounds. Caller should make sure that
* `begin <= end <= input.length`. Other inputs would result in undefined behavior.
*/
function _tryParseHexUintUncheckedBounds(
string memory input,
uint256 begin,
uint256 end
) private pure returns (bool success, uint256 value) {
bytes memory buffer = bytes(input);
// skip 0x prefix if present
bool hasPrefix = (end > begin + 1) && bytes2(_unsafeReadBytesOffset(buffer, begin)) == bytes2("0x"); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
uint256 offset = hasPrefix.toUint() * 2;
uint256 result = 0;
for (uint256 i = begin + offset; i < end; ++i) {
uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
if (chr > 15) return (false, 0);
result *= 16;
unchecked {
// Multiplying by 16 is equivalent to a shift of 4 bits (with additional overflow check).
// This guarantees that adding a value < 16 will not cause an overflow, hence the unchecked.
result += chr;
}
}
return (true, result);
}
/**
* @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as an `address`.
*
* Requirements:
* - The string must be formatted as `(0x)?[0-9a-fA-F]{40}`
*/
function parseAddress(string memory input) internal pure returns (address) {
return parseAddress(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseAddress-string} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `(0x)?[0-9a-fA-F]{40}`
*/
function parseAddress(string memory input, uint256 begin, uint256 end) internal pure returns (address) {
(bool success, address value) = tryParseAddress(input, begin, end);
if (!success) revert StringsInvalidAddressFormat();
return value;
}
/**
* @dev Variant of {parseAddress-string} that returns false if the parsing fails because the input is not a properly
* formatted address. See {parseAddress-string} requirements.
*/
function tryParseAddress(string memory input) internal pure returns (bool success, address value) {
return tryParseAddress(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseAddress-string-uint256-uint256} that returns false if the parsing fails because input is not a properly
* formatted address. See {parseAddress-string-uint256-uint256} requirements.
*/
function tryParseAddress(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, address value) {
if (end > bytes(input).length || begin > end) return (false, address(0));
bool hasPrefix = (end > begin + 1) && bytes2(_unsafeReadBytesOffset(bytes(input), begin)) == bytes2("0x"); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
uint256 expectedLength = 40 + hasPrefix.toUint() * 2;
// check that input is the correct length
if (end - begin == expectedLength) {
// length guarantees that this does not overflow, and value is at most type(uint160).max
(bool s, uint256 v) = _tryParseHexUintUncheckedBounds(input, begin, end);
return (s, address(uint160(v)));
} else {
return (false, address(0));
}
}
function _tryParseChr(bytes1 chr) private pure returns (uint8) {
uint8 value = uint8(chr);
// Try to parse `chr`:
// - Case 1: [0-9]
// - Case 2: [a-f]
// - Case 3: [A-F]
// - otherwise not supported
unchecked {
if (value > 47 && value < 58) value -= 48;
else if (value > 96 && value < 103) value -= 87;
else if (value > 64 && value < 71) value -= 55;
else return type(uint8).max;
}
return value;
}
/**
* @dev Escape special characters in JSON strings. This can be useful to prevent JSON injection in NFT metadata.
*
* WARNING: This function should only be used in double quoted JSON strings. Single quotes are not escaped.
*
* NOTE: This function escapes all unicode characters, and not just the ones in ranges defined in section 2.5 of
* RFC-4627 (U+0000 to U+001F, U+0022 and U+005C). ECMAScript's `JSON.parse` does recover escaped unicode
* characters that are not in this range, but other tooling may provide different results.
*/
function escapeJSON(string memory input) internal pure returns (string memory) {
bytes memory buffer = bytes(input);
bytes memory output = new bytes(2 * buffer.length); // worst case scenario
uint256 outputLength = 0;
for (uint256 i = 0; i < buffer.length; ++i) {
bytes1 char = bytes1(_unsafeReadBytesOffset(buffer, i));
if (((SPECIAL_CHARS_LOOKUP & (1 << uint8(char))) != 0)) {
output[outputLength++] = "\\";
if (char == 0x08) output[outputLength++] = "b";
else if (char == 0x09) output[outputLength++] = "t";
else if (char == 0x0a) output[outputLength++] = "n";
else if (char == 0x0c) output[outputLength++] = "f";
else if (char == 0x0d) output[outputLength++] = "r";
else if (char == 0x5c) output[outputLength++] = "\\";
else if (char == 0x22) {
// solhint-disable-next-line quotes
output[outputLength++] = '"';
}
} else {
output[outputLength++] = char;
}
}
// write the actual length and deallocate unused memory
assembly ("memory-safe") {
mstore(output, outputLength)
mstore(0x40, add(output, shl(5, shr(5, add(outputLength, 63)))))
}
return string(output);
}
/**
* @dev Reads a bytes32 from a bytes array without bounds checking.
*
* NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
* assembly block as such would prevent some optimizations.
*/
function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
// This is not memory safe in the general case, but all calls to this private function are within bounds.
assembly ("memory-safe") {
value := mload(add(add(buffer, 0x20), offset))
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (utils/introspection/ERC165.sol)
pragma solidity ^0.8.20;
import {IERC165} from "./IERC165.sol";
/**
* @dev Implementation of the {IERC165} interface.
*
* Contracts that want to implement ERC-165 should inherit from this contract and override {supportsInterface} to check
* for the additional interface id that will be supported. For example:
*
* ```solidity
* function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
* return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
* }
* ```
*/
abstract contract ERC165 is IERC165 {
/// @inheritdoc IERC165
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) {
return interfaceId == type(IERC165).interfaceId;
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (interfaces/draft-IERC6093.sol)
pragma solidity >=0.8.4;
/**
* @dev Standard ERC-20 Errors
* Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC-20 tokens.
*/
interface IERC20Errors {
/**
* @dev Indicates an error related to the current `balance` of a `sender`. Used in transfers.
* @param sender Address whose tokens are being transferred.
* @param balance Current balance for the interacting account.
* @param needed Minimum amount required to perform a transfer.
*/
error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed);
/**
* @dev Indicates a failure with the token `sender`. Used in transfers.
* @param sender Address whose tokens are being transferred.
*/
error ERC20InvalidSender(address sender);
/**
* @dev Indicates a failure with the token `receiver`. Used in transfers.
* @param receiver Address to which tokens are being transferred.
*/
error ERC20InvalidReceiver(address receiver);
/**
* @dev Indicates a failure with the `spender`’s `allowance`. Used in transfers.
* @param spender Address that may be allowed to operate on tokens without being their owner.
* @param allowance Amount of tokens a `spender` is allowed to operate with.
* @param needed Minimum amount required to perform a transfer.
*/
error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);
/**
* @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals.
* @param approver Address initiating an approval operation.
*/
error ERC20InvalidApprover(address approver);
/**
* @dev Indicates a failure with the `spender` to be approved. Used in approvals.
* @param spender Address that may be allowed to operate on tokens without being their owner.
*/
error ERC20InvalidSpender(address spender);
}
/**
* @dev Standard ERC-721 Errors
* Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC-721 tokens.
*/
interface IERC721Errors {
/**
* @dev Indicates that an address can't be an owner. For example, `address(0)` is a forbidden owner in ERC-721.
* Used in balance queries.
* @param owner Address of the current owner of a token.
*/
error ERC721InvalidOwner(address owner);
/**
* @dev Indicates a `tokenId` whose `owner` is the zero address.
* @param tokenId Identifier number of a token.
*/
error ERC721NonexistentToken(uint256 tokenId);
/**
* @dev Indicates an error related to the ownership over a particular token. Used in transfers.
* @param sender Address whose tokens are being transferred.
* @param tokenId Identifier number of a token.
* @param owner Address of the current owner of a token.
*/
error ERC721IncorrectOwner(address sender, uint256 tokenId, address owner);
/**
* @dev Indicates a failure with the token `sender`. Used in transfers.
* @param sender Address whose tokens are being transferred.
*/
error ERC721InvalidSender(address sender);
/**
* @dev Indicates a failure with the token `receiver`. Used in transfers.
* @param receiver Address to which tokens are being transferred.
*/
error ERC721InvalidReceiver(address receiver);
/**
* @dev Indicates a failure with the `operator`’s approval. Used in transfers.
* @param operator Address that may be allowed to operate on tokens without being their owner.
* @param tokenId Identifier number of a token.
*/
error ERC721InsufficientApproval(address operator, uint256 tokenId);
/**
* @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals.
* @param approver Address initiating an approval operation.
*/
error ERC721InvalidApprover(address approver);
/**
* @dev Indicates a failure with the `operator` to be approved. Used in approvals.
* @param operator Address that may be allowed to operate on tokens without being their owner.
*/
error ERC721InvalidOperator(address operator);
}
/**
* @dev Standard ERC-1155 Errors
* Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC-1155 tokens.
*/
interface IERC1155Errors {
/**
* @dev Indicates an error related to the current `balance` of a `sender`. Used in transfers.
* @param sender Address whose tokens are being transferred.
* @param balance Current balance for the interacting account.
* @param needed Minimum amount required to perform a transfer.
* @param tokenId Identifier number of a token.
*/
error ERC1155InsufficientBalance(address sender, uint256 balance, uint256 needed, uint256 tokenId);
/**
* @dev Indicates a failure with the token `sender`. Used in transfers.
* @param sender Address whose tokens are being transferred.
*/
error ERC1155InvalidSender(address sender);
/**
* @dev Indicates a failure with the token `receiver`. Used in transfers.
* @param receiver Address to which tokens are being transferred.
*/
error ERC1155InvalidReceiver(address receiver);
/**
* @dev Indicates a failure with the `operator`’s approval. Used in transfers.
* @param operator Address that may be allowed to operate on tokens without being their owner.
* @param owner Address of the current owner of a token.
*/
error ERC1155MissingApprovalForAll(address operator, address owner);
/**
* @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals.
* @param approver Address initiating an approval operation.
*/
error ERC1155InvalidApprover(address approver);
/**
* @dev Indicates a failure with the `operator` to be approved. Used in approvals.
* @param operator Address that may be allowed to operate on tokens without being their owner.
*/
error ERC1155InvalidOperator(address operator);
/**
* @dev Indicates an array length mismatch between ids and values in a safeBatchTransferFrom operation.
* Used in batch transfers.
* @param idsLength Length of the array of token identifiers
* @param valuesLength Length of the array of token amounts
*/
error ERC1155InvalidArrayLength(uint256 idsLength, uint256 valuesLength);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC1363} from "../../../interfaces/IERC1363.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC-20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
/**
* @dev An operation with an ERC-20 token failed.
*/
error SafeERC20FailedOperation(address token);
/**
* @dev Indicates a failed `decreaseAllowance` request.
*/
error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20 token, address to, uint256 value) internal {
if (!_safeTransfer(token, to, value, true)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
if (!_safeTransferFrom(token, from, to, value, true)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Variant of {safeTransfer} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransfer(IERC20 token, address to, uint256 value) internal returns (bool) {
return _safeTransfer(token, to, value, false);
}
/**
* @dev Variant of {safeTransferFrom} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransferFrom(IERC20 token, address from, address to, uint256 value) internal returns (bool) {
return _safeTransferFrom(token, from, to, value, false);
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*
* IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
* smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
* this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
* that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
forceApprove(token, spender, oldAllowance + value);
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no
* value, non-reverting calls are assumed to be successful.
*
* IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
* smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
* this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
* that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
unchecked {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < requestedDecrease) {
revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
}
forceApprove(token, spender, currentAllowance - requestedDecrease);
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
* to be set to zero before setting it to a non-zero value, such as USDT.
*
* NOTE: If the token implements ERC-7674, this function will not modify any temporary allowance. This function
* only sets the "standard" allowance. Any temporary allowance will remain active, in addition to the value being
* set here.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
if (!_safeApprove(token, spender, value, false)) {
if (!_safeApprove(token, spender, 0, true)) revert SafeERC20FailedOperation(address(token));
if (!_safeApprove(token, spender, value, true)) revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Performs an {ERC1363} transferAndCall, with a fallback to the simple {ERC20} transfer if the target has no
* code. This can be used to implement an {ERC721}-like safe transfer that relies on {ERC1363} checks when
* targeting contracts.
*
* Reverts if the returned value is other than `true`.
*/
function transferAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
if (to.code.length == 0) {
safeTransfer(token, to, value);
} else if (!token.transferAndCall(to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Performs an {ERC1363} transferFromAndCall, with a fallback to the simple {ERC20} transferFrom if the target
* has no code. This can be used to implement an {ERC721}-like safe transfer that relies on {ERC1363} checks when
* targeting contracts.
*
* Reverts if the returned value is other than `true`.
*/
function transferFromAndCallRelaxed(
IERC1363 token,
address from,
address to,
uint256 value,
bytes memory data
) internal {
if (to.code.length == 0) {
safeTransferFrom(token, from, to, value);
} else if (!token.transferFromAndCall(from, to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Performs an {ERC1363} approveAndCall, with a fallback to the simple {ERC20} approve if the target has no
* code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
* targeting contracts.
*
* NOTE: When the recipient address (`to`) has no code (i.e. is an EOA), this function behaves as {forceApprove}.
* Oppositely, when the recipient address (`to`) has code, this function only attempts to call {ERC1363-approveAndCall}
* once without retrying, and relies on the returned value to be true.
*
* Reverts if the returned value is other than `true`.
*/
function approveAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
if (to.code.length == 0) {
forceApprove(token, to, value);
} else if (!token.approveAndCall(to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Imitates a Solidity `token.transfer(to, value)` call, relaxing the requirement on the return value: the
* return value is optional (but if data is returned, it must not be false).
*
* @param token The token targeted by the call.
* @param to The recipient of the tokens
* @param value The amount of token to transfer
* @param bubble Behavior switch if the transfer call reverts: bubble the revert reason or return a false boolean.
*/
function _safeTransfer(IERC20 token, address to, uint256 value, bool bubble) private returns (bool success) {
bytes4 selector = IERC20.transfer.selector;
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(0x00, selector)
mstore(0x04, and(to, shr(96, not(0))))
mstore(0x24, value)
success := call(gas(), token, 0, 0x00, 0x44, 0x00, 0x20)
// if call success and return is true, all is good.
// otherwise (not success or return is not true), we need to perform further checks
if iszero(and(success, eq(mload(0x00), 1))) {
// if the call was a failure and bubble is enabled, bubble the error
if and(iszero(success), bubble) {
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
// if the return value is not true, then the call is only successful if:
// - the token address has code
// - the returndata is empty
success := and(success, and(iszero(returndatasize()), gt(extcodesize(token), 0)))
}
mstore(0x40, fmp)
}
}
/**
* @dev Imitates a Solidity `token.transferFrom(from, to, value)` call, relaxing the requirement on the return
* value: the return value is optional (but if data is returned, it must not be false).
*
* @param token The token targeted by the call.
* @param from The sender of the tokens
* @param to The recipient of the tokens
* @param value The amount of token to transfer
* @param bubble Behavior switch if the transfer call reverts: bubble the revert reason or return a false boolean.
*/
function _safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value,
bool bubble
) private returns (bool success) {
bytes4 selector = IERC20.transferFrom.selector;
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(0x00, selector)
mstore(0x04, and(from, shr(96, not(0))))
mstore(0x24, and(to, shr(96, not(0))))
mstore(0x44, value)
success := call(gas(), token, 0, 0x00, 0x64, 0x00, 0x20)
// if call success and return is true, all is good.
// otherwise (not success or return is not true), we need to perform further checks
if iszero(and(success, eq(mload(0x00), 1))) {
// if the call was a failure and bubble is enabled, bubble the error
if and(iszero(success), bubble) {
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
// if the return value is not true, then the call is only successful if:
// - the token address has code
// - the returndata is empty
success := and(success, and(iszero(returndatasize()), gt(extcodesize(token), 0)))
}
mstore(0x40, fmp)
mstore(0x60, 0)
}
}
/**
* @dev Imitates a Solidity `token.approve(spender, value)` call, relaxing the requirement on the return value:
* the return value is optional (but if data is returned, it must not be false).
*
* @param token The token targeted by the call.
* @param spender The spender of the tokens
* @param value The amount of token to transfer
* @param bubble Behavior switch if the transfer call reverts: bubble the revert reason or return a false boolean.
*/
function _safeApprove(IERC20 token, address spender, uint256 value, bool bubble) private returns (bool success) {
bytes4 selector = IERC20.approve.selector;
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(0x00, selector)
mstore(0x04, and(spender, shr(96, not(0))))
mstore(0x24, value)
success := call(gas(), token, 0, 0x00, 0x44, 0x00, 0x20)
// if call success and return is true, all is good.
// otherwise (not success or return is not true), we need to perform further checks
if iszero(and(success, eq(mload(0x00), 1))) {
// if the call was a failure and bubble is enabled, bubble the error
if and(iszero(success), bubble) {
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
// if the return value is not true, then the call is only successful if:
// - the token address has code
// - the returndata is empty
success := and(success, and(iszero(returndatasize()), gt(extcodesize(token), 0)))
}
mstore(0x40, fmp)
}
}
}// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;
import "./PRBMath.sol";
/// @title PRBMathUD60x18
/// @author Paul Razvan Berg
/// @notice Smart contract library for advanced fixed-point math that works with uint256 numbers considered to have 18
/// trailing decimals. We call this number representation unsigned 60.18-decimal fixed-point, since there can be up to 60
/// digits in the integer part and up to 18 decimals in the fractional part. The numbers are bound by the minimum and the
/// maximum values permitted by the Solidity type uint256.
library PRBMathUD60x18 {
/// @dev Half the SCALE number.
uint256 internal constant HALF_SCALE = 5e17;
/// @dev log2(e) as an unsigned 60.18-decimal fixed-point number.
uint256 internal constant LOG2_E = 1_442695040888963407;
/// @dev The maximum value an unsigned 60.18-decimal fixed-point number can have.
uint256 internal constant MAX_UD60x18 =
115792089237316195423570985008687907853269984665640564039457_584007913129639935;
/// @dev The maximum whole value an unsigned 60.18-decimal fixed-point number can have.
uint256 internal constant MAX_WHOLE_UD60x18 =
115792089237316195423570985008687907853269984665640564039457_000000000000000000;
/// @dev How many trailing decimals can be represented.
uint256 internal constant SCALE = 1e18;
/// @notice Calculates the arithmetic average of x and y, rounding down.
/// @param x The first operand as an unsigned 60.18-decimal fixed-point number.
/// @param y The second operand as an unsigned 60.18-decimal fixed-point number.
/// @return result The arithmetic average as an unsigned 60.18-decimal fixed-point number.
function avg(uint256 x, uint256 y) internal pure returns (uint256 result) {
// The operations can never overflow.
unchecked {
// The last operand checks if both x and y are odd and if that is the case, we add 1 to the result. We need
// to do this because if both numbers are odd, the 0.5 remainder gets truncated twice.
result = (x >> 1) + (y >> 1) + (x & y & 1);
}
}
/// @notice Yields the least unsigned 60.18 decimal fixed-point number greater than or equal to x.
///
/// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts.
/// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.
///
/// Requirements:
/// - x must be less than or equal to MAX_WHOLE_UD60x18.
///
/// @param x The unsigned 60.18-decimal fixed-point number to ceil.
/// @param result The least integer greater than or equal to x, as an unsigned 60.18-decimal fixed-point number.
function ceil(uint256 x) internal pure returns (uint256 result) {
if (x > MAX_WHOLE_UD60x18) {
revert PRBMathUD60x18__CeilOverflow(x);
}
assembly {
// Equivalent to "x % SCALE" but faster.
let remainder := mod(x, SCALE)
// Equivalent to "SCALE - remainder" but faster.
let delta := sub(SCALE, remainder)
// Equivalent to "x + delta * (remainder > 0 ? 1 : 0)" but faster.
result := add(x, mul(delta, gt(remainder, 0)))
}
}
/// @notice Divides two unsigned 60.18-decimal fixed-point numbers, returning a new unsigned 60.18-decimal fixed-point number.
///
/// @dev Uses mulDiv to enable overflow-safe multiplication and division.
///
/// Requirements:
/// - The denominator cannot be zero.
///
/// @param x The numerator as an unsigned 60.18-decimal fixed-point number.
/// @param y The denominator as an unsigned 60.18-decimal fixed-point number.
/// @param result The quotient as an unsigned 60.18-decimal fixed-point number.
function div(uint256 x, uint256 y) internal pure returns (uint256 result) {
result = PRBMath.mulDiv(x, SCALE, y);
}
/// @notice Returns Euler's number as an unsigned 60.18-decimal fixed-point number.
/// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant).
function e() internal pure returns (uint256 result) {
result = 2_718281828459045235;
}
/// @notice Calculates the natural exponent of x.
///
/// @dev Based on the insight that e^x = 2^(x * log2(e)).
///
/// Requirements:
/// - All from "log2".
/// - x must be less than 133.084258667509499441.
///
/// @param x The exponent as an unsigned 60.18-decimal fixed-point number.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function exp(uint256 x) internal pure returns (uint256 result) {
// Without this check, the value passed to "exp2" would be greater than 192.
if (x >= 133_084258667509499441) {
revert PRBMathUD60x18__ExpInputTooBig(x);
}
// Do the fixed-point multiplication inline to save gas.
unchecked {
uint256 doubleScaleProduct = x * LOG2_E;
result = exp2((doubleScaleProduct + HALF_SCALE) / SCALE);
}
}
/// @notice Calculates the binary exponent of x using the binary fraction method.
///
/// @dev See https://ethereum.stackexchange.com/q/79903/24693.
///
/// Requirements:
/// - x must be 192 or less.
/// - The result must fit within MAX_UD60x18.
///
/// @param x The exponent as an unsigned 60.18-decimal fixed-point number.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function exp2(uint256 x) internal pure returns (uint256 result) {
// 2^192 doesn't fit within the 192.64-bit format used internally in this function.
if (x >= 192e18) {
revert PRBMathUD60x18__Exp2InputTooBig(x);
}
unchecked {
// Convert x to the 192.64-bit fixed-point format.
uint256 x192x64 = (x << 64) / SCALE;
// Pass x to the PRBMath.exp2 function, which uses the 192.64-bit fixed-point number representation.
result = PRBMath.exp2(x192x64);
}
}
/// @notice Yields the greatest unsigned 60.18 decimal fixed-point number less than or equal to x.
/// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts.
/// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.
/// @param x The unsigned 60.18-decimal fixed-point number to floor.
/// @param result The greatest integer less than or equal to x, as an unsigned 60.18-decimal fixed-point number.
function floor(uint256 x) internal pure returns (uint256 result) {
assembly {
// Equivalent to "x % SCALE" but faster.
let remainder := mod(x, SCALE)
// Equivalent to "x - remainder * (remainder > 0 ? 1 : 0)" but faster.
result := sub(x, mul(remainder, gt(remainder, 0)))
}
}
/// @notice Yields the excess beyond the floor of x.
/// @dev Based on the odd function definition https://en.wikipedia.org/wiki/Fractional_part.
/// @param x The unsigned 60.18-decimal fixed-point number to get the fractional part of.
/// @param result The fractional part of x as an unsigned 60.18-decimal fixed-point number.
function frac(uint256 x) internal pure returns (uint256 result) {
assembly {
result := mod(x, SCALE)
}
}
/// @notice Converts a number from basic integer form to unsigned 60.18-decimal fixed-point representation.
///
/// @dev Requirements:
/// - x must be less than or equal to MAX_UD60x18 divided by SCALE.
///
/// @param x The basic integer to convert.
/// @param result The same number in unsigned 60.18-decimal fixed-point representation.
function fromUint(uint256 x) internal pure returns (uint256 result) {
unchecked {
if (x > MAX_UD60x18 / SCALE) {
revert PRBMathUD60x18__FromUintOverflow(x);
}
result = x * SCALE;
}
}
/// @notice Calculates geometric mean of x and y, i.e. sqrt(x * y), rounding down.
///
/// @dev Requirements:
/// - x * y must fit within MAX_UD60x18, lest it overflows.
///
/// @param x The first operand as an unsigned 60.18-decimal fixed-point number.
/// @param y The second operand as an unsigned 60.18-decimal fixed-point number.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function gm(uint256 x, uint256 y) internal pure returns (uint256 result) {
if (x == 0) {
return 0;
}
unchecked {
// Checking for overflow this way is faster than letting Solidity do it.
uint256 xy = x * y;
if (xy / x != y) {
revert PRBMathUD60x18__GmOverflow(x, y);
}
// We don't need to multiply by the SCALE here because the x*y product had already picked up a factor of SCALE
// during multiplication. See the comments within the "sqrt" function.
result = PRBMath.sqrt(xy);
}
}
/// @notice Calculates 1 / x, rounding toward zero.
///
/// @dev Requirements:
/// - x cannot be zero.
///
/// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the inverse.
/// @return result The inverse as an unsigned 60.18-decimal fixed-point number.
function inv(uint256 x) internal pure returns (uint256 result) {
unchecked {
// 1e36 is SCALE * SCALE.
result = 1e36 / x;
}
}
/// @notice Calculates the natural logarithm of x.
///
/// @dev Based on the insight that ln(x) = log2(x) / log2(e).
///
/// Requirements:
/// - All from "log2".
///
/// Caveats:
/// - All from "log2".
/// - This doesn't return exactly 1 for 2.718281828459045235, for that we would need more fine-grained precision.
///
/// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the natural logarithm.
/// @return result The natural logarithm as an unsigned 60.18-decimal fixed-point number.
function ln(uint256 x) internal pure returns (uint256 result) {
// Do the fixed-point multiplication inline to save gas. This is overflow-safe because the maximum value that log2(x)
// can return is 196205294292027477728.
unchecked {
result = (log2(x) * SCALE) / LOG2_E;
}
}
/// @notice Calculates the common logarithm of x.
///
/// @dev First checks if x is an exact power of ten and it stops if yes. If it's not, calculates the common
/// logarithm based on the insight that log10(x) = log2(x) / log2(10).
///
/// Requirements:
/// - All from "log2".
///
/// Caveats:
/// - All from "log2".
///
/// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the common logarithm.
/// @return result The common logarithm as an unsigned 60.18-decimal fixed-point number.
function log10(uint256 x) internal pure returns (uint256 result) {
if (x < SCALE) {
revert PRBMathUD60x18__LogInputTooSmall(x);
}
// Note that the "mul" in this block is the assembly multiplication operation, not the "mul" function defined
// in this contract.
// prettier-ignore
assembly {
switch x
case 1 { result := mul(SCALE, sub(0, 18)) }
case 10 { result := mul(SCALE, sub(1, 18)) }
case 100 { result := mul(SCALE, sub(2, 18)) }
case 1000 { result := mul(SCALE, sub(3, 18)) }
case 10000 { result := mul(SCALE, sub(4, 18)) }
case 100000 { result := mul(SCALE, sub(5, 18)) }
case 1000000 { result := mul(SCALE, sub(6, 18)) }
case 10000000 { result := mul(SCALE, sub(7, 18)) }
case 100000000 { result := mul(SCALE, sub(8, 18)) }
case 1000000000 { result := mul(SCALE, sub(9, 18)) }
case 10000000000 { result := mul(SCALE, sub(10, 18)) }
case 100000000000 { result := mul(SCALE, sub(11, 18)) }
case 1000000000000 { result := mul(SCALE, sub(12, 18)) }
case 10000000000000 { result := mul(SCALE, sub(13, 18)) }
case 100000000000000 { result := mul(SCALE, sub(14, 18)) }
case 1000000000000000 { result := mul(SCALE, sub(15, 18)) }
case 10000000000000000 { result := mul(SCALE, sub(16, 18)) }
case 100000000000000000 { result := mul(SCALE, sub(17, 18)) }
case 1000000000000000000 { result := 0 }
case 10000000000000000000 { result := SCALE }
case 100000000000000000000 { result := mul(SCALE, 2) }
case 1000000000000000000000 { result := mul(SCALE, 3) }
case 10000000000000000000000 { result := mul(SCALE, 4) }
case 100000000000000000000000 { result := mul(SCALE, 5) }
case 1000000000000000000000000 { result := mul(SCALE, 6) }
case 10000000000000000000000000 { result := mul(SCALE, 7) }
case 100000000000000000000000000 { result := mul(SCALE, 8) }
case 1000000000000000000000000000 { result := mul(SCALE, 9) }
case 10000000000000000000000000000 { result := mul(SCALE, 10) }
case 100000000000000000000000000000 { result := mul(SCALE, 11) }
case 1000000000000000000000000000000 { result := mul(SCALE, 12) }
case 10000000000000000000000000000000 { result := mul(SCALE, 13) }
case 100000000000000000000000000000000 { result := mul(SCALE, 14) }
case 1000000000000000000000000000000000 { result := mul(SCALE, 15) }
case 10000000000000000000000000000000000 { result := mul(SCALE, 16) }
case 100000000000000000000000000000000000 { result := mul(SCALE, 17) }
case 1000000000000000000000000000000000000 { result := mul(SCALE, 18) }
case 10000000000000000000000000000000000000 { result := mul(SCALE, 19) }
case 100000000000000000000000000000000000000 { result := mul(SCALE, 20) }
case 1000000000000000000000000000000000000000 { result := mul(SCALE, 21) }
case 10000000000000000000000000000000000000000 { result := mul(SCALE, 22) }
case 100000000000000000000000000000000000000000 { result := mul(SCALE, 23) }
case 1000000000000000000000000000000000000000000 { result := mul(SCALE, 24) }
case 10000000000000000000000000000000000000000000 { result := mul(SCALE, 25) }
case 100000000000000000000000000000000000000000000 { result := mul(SCALE, 26) }
case 1000000000000000000000000000000000000000000000 { result := mul(SCALE, 27) }
case 10000000000000000000000000000000000000000000000 { result := mul(SCALE, 28) }
case 100000000000000000000000000000000000000000000000 { result := mul(SCALE, 29) }
case 1000000000000000000000000000000000000000000000000 { result := mul(SCALE, 30) }
case 10000000000000000000000000000000000000000000000000 { result := mul(SCALE, 31) }
case 100000000000000000000000000000000000000000000000000 { result := mul(SCALE, 32) }
case 1000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 33) }
case 10000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 34) }
case 100000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 35) }
case 1000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 36) }
case 10000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 37) }
case 100000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 38) }
case 1000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 39) }
case 10000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 40) }
case 100000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 41) }
case 1000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 42) }
case 10000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 43) }
case 100000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 44) }
case 1000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 45) }
case 10000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 46) }
case 100000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 47) }
case 1000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 48) }
case 10000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 49) }
case 100000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 50) }
case 1000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 51) }
case 10000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 52) }
case 100000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 53) }
case 1000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 54) }
case 10000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 55) }
case 100000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 56) }
case 1000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 57) }
case 10000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 58) }
case 100000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 59) }
default {
result := MAX_UD60x18
}
}
if (result == MAX_UD60x18) {
// Do the fixed-point division inline to save gas. The denominator is log2(10).
unchecked {
result = (log2(x) * SCALE) / 3_321928094887362347;
}
}
}
/// @notice Calculates the binary logarithm of x.
///
/// @dev Based on the iterative approximation algorithm.
/// https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation
///
/// Requirements:
/// - x must be greater than or equal to SCALE, otherwise the result would be negative.
///
/// Caveats:
/// - The results are nor perfectly accurate to the last decimal, due to the lossy precision of the iterative approximation.
///
/// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the binary logarithm.
/// @return result The binary logarithm as an unsigned 60.18-decimal fixed-point number.
function log2(uint256 x) internal pure returns (uint256 result) {
if (x < SCALE) {
revert PRBMathUD60x18__LogInputTooSmall(x);
}
unchecked {
// Calculate the integer part of the logarithm and add it to the result and finally calculate y = x * 2^(-n).
uint256 n = PRBMath.mostSignificantBit(x / SCALE);
// The integer part of the logarithm as an unsigned 60.18-decimal fixed-point number. The operation can't overflow
// because n is maximum 255 and SCALE is 1e18.
result = n * SCALE;
// This is y = x * 2^(-n).
uint256 y = x >> n;
// If y = 1, the fractional part is zero.
if (y == SCALE) {
return result;
}
// Calculate the fractional part via the iterative approximation.
// The "delta >>= 1" part is equivalent to "delta /= 2", but shifting bits is faster.
for (uint256 delta = HALF_SCALE; delta > 0; delta >>= 1) {
y = (y * y) / SCALE;
// Is y^2 > 2 and so in the range [2,4)?
if (y >= 2 * SCALE) {
// Add the 2^(-m) factor to the logarithm.
result += delta;
// Corresponds to z/2 on Wikipedia.
y >>= 1;
}
}
}
}
/// @notice Multiplies two unsigned 60.18-decimal fixed-point numbers together, returning a new unsigned 60.18-decimal
/// fixed-point number.
/// @dev See the documentation for the "PRBMath.mulDivFixedPoint" function.
/// @param x The multiplicand as an unsigned 60.18-decimal fixed-point number.
/// @param y The multiplier as an unsigned 60.18-decimal fixed-point number.
/// @return result The product as an unsigned 60.18-decimal fixed-point number.
function mul(uint256 x, uint256 y) internal pure returns (uint256 result) {
result = PRBMath.mulDivFixedPoint(x, y);
}
/// @notice Returns PI as an unsigned 60.18-decimal fixed-point number.
function pi() internal pure returns (uint256 result) {
result = 3_141592653589793238;
}
/// @notice Raises x to the power of y.
///
/// @dev Based on the insight that x^y = 2^(log2(x) * y).
///
/// Requirements:
/// - All from "exp2", "log2" and "mul".
///
/// Caveats:
/// - All from "exp2", "log2" and "mul".
/// - Assumes 0^0 is 1.
///
/// @param x Number to raise to given power y, as an unsigned 60.18-decimal fixed-point number.
/// @param y Exponent to raise x to, as an unsigned 60.18-decimal fixed-point number.
/// @return result x raised to power y, as an unsigned 60.18-decimal fixed-point number.
function pow(uint256 x, uint256 y) internal pure returns (uint256 result) {
if (x == 0) {
result = y == 0 ? SCALE : uint256(0);
} else {
result = exp2(mul(log2(x), y));
}
}
/// @notice Raises x (unsigned 60.18-decimal fixed-point number) to the power of y (basic unsigned integer) using the
/// famous algorithm "exponentiation by squaring".
///
/// @dev See https://en.wikipedia.org/wiki/Exponentiation_by_squaring
///
/// Requirements:
/// - The result must fit within MAX_UD60x18.
///
/// Caveats:
/// - All from "mul".
/// - Assumes 0^0 is 1.
///
/// @param x The base as an unsigned 60.18-decimal fixed-point number.
/// @param y The exponent as an uint256.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function powu(uint256 x, uint256 y) internal pure returns (uint256 result) {
// Calculate the first iteration of the loop in advance.
result = y & 1 > 0 ? x : SCALE;
// Equivalent to "for(y /= 2; y > 0; y /= 2)" but faster.
for (y >>= 1; y > 0; y >>= 1) {
x = PRBMath.mulDivFixedPoint(x, x);
// Equivalent to "y % 2 == 1" but faster.
if (y & 1 > 0) {
result = PRBMath.mulDivFixedPoint(result, x);
}
}
}
/// @notice Returns 1 as an unsigned 60.18-decimal fixed-point number.
function scale() internal pure returns (uint256 result) {
result = SCALE;
}
/// @notice Calculates the square root of x, rounding down.
/// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
///
/// Requirements:
/// - x must be less than MAX_UD60x18 / SCALE.
///
/// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the square root.
/// @return result The result as an unsigned 60.18-decimal fixed-point .
function sqrt(uint256 x) internal pure returns (uint256 result) {
unchecked {
if (x > MAX_UD60x18 / SCALE) {
revert PRBMathUD60x18__SqrtOverflow(x);
}
// Multiply x by the SCALE to account for the factor of SCALE that is picked up when multiplying two unsigned
// 60.18-decimal fixed-point numbers together (in this case, those two numbers are both the square root).
result = PRBMath.sqrt(x * SCALE);
}
}
/// @notice Converts a unsigned 60.18-decimal fixed-point number to basic integer form, rounding down in the process.
/// @param x The unsigned 60.18-decimal fixed-point number to convert.
/// @return result The same number in basic integer form.
function toUint(uint256 x) internal pure returns (uint256 result) {
unchecked {
result = x / SCALE;
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
pragma solidity ^0.8.20;
/**
* @dev Library for reading and writing primitive types to specific storage slots.
*
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
* This library helps with reading and writing to such slots without the need for inline assembly.
*
* The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
*
* Example usage to set ERC-1967 implementation slot:
* ```solidity
* contract ERC1967 {
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
*
* function _getImplementation() internal view returns (address) {
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
* }
*
* function _setImplementation(address newImplementation) internal {
* require(newImplementation.code.length > 0);
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
* }
* }
* ```
*
* TIP: Consider using this library along with {SlotDerivation}.
*/
library StorageSlot {
struct AddressSlot {
address value;
}
struct BooleanSlot {
bool value;
}
struct Bytes32Slot {
bytes32 value;
}
struct Uint256Slot {
uint256 value;
}
struct Int256Slot {
int256 value;
}
struct StringSlot {
string value;
}
struct BytesSlot {
bytes value;
}
/**
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
*/
function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `BooleanSlot` with member `value` located at `slot`.
*/
function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Bytes32Slot` with member `value` located at `slot`.
*/
function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Uint256Slot` with member `value` located at `slot`.
*/
function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Int256Slot` with member `value` located at `slot`.
*/
function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `StringSlot` with member `value` located at `slot`.
*/
function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns an `StringSlot` representation of the string storage pointer `store`.
*/
function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
assembly ("memory-safe") {
r.slot := store.slot
}
}
/**
* @dev Returns a `BytesSlot` with member `value` located at `slot`.
*/
function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
*/
function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
assembly ("memory-safe") {
r.slot := store.slot
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (utils/introspection/IERC165.sol)
pragma solidity >=0.4.16;
/**
* @dev Interface of the ERC-165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[ERC].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC721/IERC721Receiver.sol)
pragma solidity >=0.5.0;
/**
* @title ERC-721 token receiver interface
* @dev Interface for any contract that wants to support safeTransfers
* from ERC-721 asset contracts.
*/
interface IERC721Receiver {
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be
* reverted.
*
* The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
*/
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/math/Math.sol)
pragma solidity ^0.8.20;
import {Panic} from "../Panic.sol";
import {SafeCast} from "./SafeCast.sol";
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Floor, // Toward negative infinity
Ceil, // Toward positive infinity
Trunc, // Toward zero
Expand // Away from zero
}
/**
* @dev Return the 512-bit addition of two uint256.
*
* The result is stored in two 256 variables such that sum = high * 2²⁵⁶ + low.
*/
function add512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
assembly ("memory-safe") {
low := add(a, b)
high := lt(low, a)
}
}
/**
* @dev Return the 512-bit multiplication of two uint256.
*
* The result is stored in two 256 variables such that product = high * 2²⁵⁶ + low.
*/
function mul512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
// 512-bit multiply [high low] = x * y. Compute the product mod 2²⁵⁶ and mod 2²⁵⁶ - 1, then use
// the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = high * 2²⁵⁶ + low.
assembly ("memory-safe") {
let mm := mulmod(a, b, not(0))
low := mul(a, b)
high := sub(sub(mm, low), lt(mm, low))
}
}
/**
* @dev Returns the addition of two unsigned integers, with a success flag (no overflow).
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a + b;
success = c >= a;
result = c * SafeCast.toUint(success);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with a success flag (no overflow).
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a - b;
success = c <= a;
result = c * SafeCast.toUint(success);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with a success flag (no overflow).
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a * b;
assembly ("memory-safe") {
// Only true when the multiplication doesn't overflow
// (c / a == b) || (a == 0)
success := or(eq(div(c, a), b), iszero(a))
}
// equivalent to: success ? c : 0
result = c * SafeCast.toUint(success);
}
}
/**
* @dev Returns the division of two unsigned integers, with a success flag (no division by zero).
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
success = b > 0;
assembly ("memory-safe") {
// The `DIV` opcode returns zero when the denominator is 0.
result := div(a, b)
}
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero).
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
success = b > 0;
assembly ("memory-safe") {
// The `MOD` opcode returns zero when the denominator is 0.
result := mod(a, b)
}
}
}
/**
* @dev Unsigned saturating addition, bounds to `2²⁵⁶ - 1` instead of overflowing.
*/
function saturatingAdd(uint256 a, uint256 b) internal pure returns (uint256) {
(bool success, uint256 result) = tryAdd(a, b);
return ternary(success, result, type(uint256).max);
}
/**
* @dev Unsigned saturating subtraction, bounds to zero instead of overflowing.
*/
function saturatingSub(uint256 a, uint256 b) internal pure returns (uint256) {
(, uint256 result) = trySub(a, b);
return result;
}
/**
* @dev Unsigned saturating multiplication, bounds to `2²⁵⁶ - 1` instead of overflowing.
*/
function saturatingMul(uint256 a, uint256 b) internal pure returns (uint256) {
(bool success, uint256 result) = tryMul(a, b);
return ternary(success, result, type(uint256).max);
}
/**
* @dev Branchless ternary evaluation for `condition ? a : b`. Gas costs are constant.
*
* IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
* However, the compiler may optimize Solidity ternary operations (i.e. `condition ? a : b`) to only compute
* one branch when needed, making this function more expensive.
*/
function ternary(bool condition, uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
// branchless ternary works because:
// b ^ (a ^ b) == a
// b ^ 0 == b
return b ^ ((a ^ b) * SafeCast.toUint(condition));
}
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return ternary(a > b, a, b);
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return ternary(a < b, a, b);
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds towards infinity instead
* of rounding towards zero.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
if (b == 0) {
// Guarantee the same behavior as in a regular Solidity division.
Panic.panic(Panic.DIVISION_BY_ZERO);
}
// The following calculation ensures accurate ceiling division without overflow.
// Since a is non-zero, (a - 1) / b will not overflow.
// The largest possible result occurs when (a - 1) / b is type(uint256).max,
// but the largest value we can obtain is type(uint256).max - 1, which happens
// when a = type(uint256).max and b = 1.
unchecked {
return SafeCast.toUint(a > 0) * ((a - 1) / b + 1);
}
}
/**
* @dev Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or
* denominator == 0.
*
* Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
* Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
(uint256 high, uint256 low) = mul512(x, y);
// Handle non-overflow cases, 256 by 256 division.
if (high == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return low / denominator;
}
// Make sure the result is less than 2²⁵⁶. Also prevents denominator == 0.
if (denominator <= high) {
Panic.panic(ternary(denominator == 0, Panic.DIVISION_BY_ZERO, Panic.UNDER_OVERFLOW));
}
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [high low].
uint256 remainder;
assembly ("memory-safe") {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
high := sub(high, gt(remainder, low))
low := sub(low, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator.
// Always >= 1. See https://cs.stackexchange.com/q/138556/92363.
uint256 twos = denominator & (0 - denominator);
assembly ("memory-safe") {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [high low] by twos.
low := div(low, twos)
// Flip twos such that it is 2²⁵⁶ / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from high into low.
low |= high * twos;
// Invert denominator mod 2²⁵⁶. Now that denominator is an odd number, it has an inverse modulo 2²⁵⁶ such
// that denominator * inv ≡ 1 mod 2²⁵⁶. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv ≡ 1 mod 2⁴.
uint256 inverse = (3 * denominator) ^ 2;
// Use the 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.
inverse *= 2 - denominator * inverse; // inverse mod 2⁸
inverse *= 2 - denominator * inverse; // inverse mod 2¹⁶
inverse *= 2 - denominator * inverse; // inverse mod 2³²
inverse *= 2 - denominator * inverse; // inverse mod 2⁶⁴
inverse *= 2 - denominator * inverse; // inverse mod 2¹²⁸
inverse *= 2 - denominator * inverse; // inverse mod 2²⁵⁶
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2²⁵⁶. Since the preconditions guarantee that the outcome is
// less than 2²⁵⁶, this is the final result. We don't need to compute the high bits of the result and high
// is no longer required.
result = low * inverse;
return result;
}
}
/**
* @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
}
/**
* @dev Calculates floor(x * y >> n) with full precision. Throws if result overflows a uint256.
*/
function mulShr(uint256 x, uint256 y, uint8 n) internal pure returns (uint256 result) {
unchecked {
(uint256 high, uint256 low) = mul512(x, y);
if (high >= 1 << n) {
Panic.panic(Panic.UNDER_OVERFLOW);
}
return (high << (256 - n)) | (low >> n);
}
}
/**
* @dev Calculates x * y >> n with full precision, following the selected rounding direction.
*/
function mulShr(uint256 x, uint256 y, uint8 n, Rounding rounding) internal pure returns (uint256) {
return mulShr(x, y, n) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, 1 << n) > 0);
}
/**
* @dev Calculate the modular multiplicative inverse of a number in Z/nZ.
*
* If n is a prime, then Z/nZ is a field. In that case all elements are inversible, except 0.
* If n is not a prime, then Z/nZ is not a field, and some elements might not be inversible.
*
* If the input value is not inversible, 0 is returned.
*
* NOTE: If you know for sure that n is (big) a prime, it may be cheaper to use Fermat's little theorem and get the
* inverse using `Math.modExp(a, n - 2, n)`. See {invModPrime}.
*/
function invMod(uint256 a, uint256 n) internal pure returns (uint256) {
unchecked {
if (n == 0) return 0;
// The inverse modulo is calculated using the Extended Euclidean Algorithm (iterative version)
// Used to compute integers x and y such that: ax + ny = gcd(a, n).
// When the gcd is 1, then the inverse of a modulo n exists and it's x.
// ax + ny = 1
// ax = 1 + (-y)n
// ax ≡ 1 (mod n) # x is the inverse of a modulo n
// If the remainder is 0 the gcd is n right away.
uint256 remainder = a % n;
uint256 gcd = n;
// Therefore the initial coefficients are:
// ax + ny = gcd(a, n) = n
// 0a + 1n = n
int256 x = 0;
int256 y = 1;
while (remainder != 0) {
uint256 quotient = gcd / remainder;
(gcd, remainder) = (
// The old remainder is the next gcd to try.
remainder,
// Compute the next remainder.
// Can't overflow given that (a % gcd) * (gcd // (a % gcd)) <= gcd
// where gcd is at most n (capped to type(uint256).max)
gcd - remainder * quotient
);
(x, y) = (
// Increment the coefficient of a.
y,
// Decrement the coefficient of n.
// Can overflow, but the result is casted to uint256 so that the
// next value of y is "wrapped around" to a value between 0 and n - 1.
x - y * int256(quotient)
);
}
if (gcd != 1) return 0; // No inverse exists.
return ternary(x < 0, n - uint256(-x), uint256(x)); // Wrap the result if it's negative.
}
}
/**
* @dev Variant of {invMod}. More efficient, but only works if `p` is known to be a prime greater than `2`.
*
* From https://en.wikipedia.org/wiki/Fermat%27s_little_theorem[Fermat's little theorem], we know that if p is
* prime, then `a**(p-1) ≡ 1 mod p`. As a consequence, we have `a * a**(p-2) ≡ 1 mod p`, which means that
* `a**(p-2)` is the modular multiplicative inverse of a in Fp.
*
* NOTE: this function does NOT check that `p` is a prime greater than `2`.
*/
function invModPrime(uint256 a, uint256 p) internal view returns (uint256) {
unchecked {
return Math.modExp(a, p - 2, p);
}
}
/**
* @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m)
*
* Requirements:
* - modulus can't be zero
* - underlying staticcall to precompile must succeed
*
* IMPORTANT: The result is only valid if the underlying call succeeds. When using this function, make
* sure the chain you're using it on supports the precompiled contract for modular exponentiation
* at address 0x05 as specified in https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise,
* the underlying function will succeed given the lack of a revert, but the result may be incorrectly
* interpreted as 0.
*/
function modExp(uint256 b, uint256 e, uint256 m) internal view returns (uint256) {
(bool success, uint256 result) = tryModExp(b, e, m);
if (!success) {
Panic.panic(Panic.DIVISION_BY_ZERO);
}
return result;
}
/**
* @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m).
* It includes a success flag indicating if the operation succeeded. Operation will be marked as failed if trying
* to operate modulo 0 or if the underlying precompile reverted.
*
* IMPORTANT: The result is only valid if the success flag is true. When using this function, make sure the chain
* you're using it on supports the precompiled contract for modular exponentiation at address 0x05 as specified in
* https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise, the underlying function will succeed given the lack
* of a revert, but the result may be incorrectly interpreted as 0.
*/
function tryModExp(uint256 b, uint256 e, uint256 m) internal view returns (bool success, uint256 result) {
if (m == 0) return (false, 0);
assembly ("memory-safe") {
let ptr := mload(0x40)
// | Offset | Content | Content (Hex) |
// |-----------|------------|--------------------------------------------------------------------|
// | 0x00:0x1f | size of b | 0x0000000000000000000000000000000000000000000000000000000000000020 |
// | 0x20:0x3f | size of e | 0x0000000000000000000000000000000000000000000000000000000000000020 |
// | 0x40:0x5f | size of m | 0x0000000000000000000000000000000000000000000000000000000000000020 |
// | 0x60:0x7f | value of b | 0x<.............................................................b> |
// | 0x80:0x9f | value of e | 0x<.............................................................e> |
// | 0xa0:0xbf | value of m | 0x<.............................................................m> |
mstore(ptr, 0x20)
mstore(add(ptr, 0x20), 0x20)
mstore(add(ptr, 0x40), 0x20)
mstore(add(ptr, 0x60), b)
mstore(add(ptr, 0x80), e)
mstore(add(ptr, 0xa0), m)
// Given the result < m, it's guaranteed to fit in 32 bytes,
// so we can use the memory scratch space located at offset 0.
success := staticcall(gas(), 0x05, ptr, 0xc0, 0x00, 0x20)
result := mload(0x00)
}
}
/**
* @dev Variant of {modExp} that supports inputs of arbitrary length.
*/
function modExp(bytes memory b, bytes memory e, bytes memory m) internal view returns (bytes memory) {
(bool success, bytes memory result) = tryModExp(b, e, m);
if (!success) {
Panic.panic(Panic.DIVISION_BY_ZERO);
}
return result;
}
/**
* @dev Variant of {tryModExp} that supports inputs of arbitrary length.
*/
function tryModExp(
bytes memory b,
bytes memory e,
bytes memory m
) internal view returns (bool success, bytes memory result) {
if (_zeroBytes(m)) return (false, new bytes(0));
uint256 mLen = m.length;
// Encode call args in result and move the free memory pointer
result = abi.encodePacked(b.length, e.length, mLen, b, e, m);
assembly ("memory-safe") {
let dataPtr := add(result, 0x20)
// Write result on top of args to avoid allocating extra memory.
success := staticcall(gas(), 0x05, dataPtr, mload(result), dataPtr, mLen)
// Overwrite the length.
// result.length > returndatasize() is guaranteed because returndatasize() == m.length
mstore(result, mLen)
// Set the memory pointer after the returned data.
mstore(0x40, add(dataPtr, mLen))
}
}
/**
* @dev Returns whether the provided byte array is zero.
*/
function _zeroBytes(bytes memory byteArray) private pure returns (bool) {
for (uint256 i = 0; i < byteArray.length; ++i) {
if (byteArray[i] != 0) {
return false;
}
}
return true;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
* towards zero.
*
* This method is based on Newton's method for computing square roots; the algorithm is restricted to only
* using integer operations.
*/
function sqrt(uint256 a) internal pure returns (uint256) {
unchecked {
// Take care of easy edge cases when a == 0 or a == 1
if (a <= 1) {
return a;
}
// In this function, we use Newton's method to get a root of `f(x) := x² - a`. It involves building a
// sequence x_n that converges toward sqrt(a). For each iteration x_n, we also define the error between
// the current value as `ε_n = | x_n - sqrt(a) |`.
//
// For our first estimation, we consider `e` the smallest power of 2 which is bigger than the square root
// of the target. (i.e. `2**(e-1) ≤ sqrt(a) < 2**e`). We know that `e ≤ 128` because `(2¹²⁸)² = 2²⁵⁶` is
// bigger than any uint256.
//
// By noticing that
// `2**(e-1) ≤ sqrt(a) < 2**e → (2**(e-1))² ≤ a < (2**e)² → 2**(2*e-2) ≤ a < 2**(2*e)`
// we can deduce that `e - 1` is `log2(a) / 2`. We can thus compute `x_n = 2**(e-1)` using a method similar
// to the msb function.
uint256 aa = a;
uint256 xn = 1;
if (aa >= (1 << 128)) {
aa >>= 128;
xn <<= 64;
}
if (aa >= (1 << 64)) {
aa >>= 64;
xn <<= 32;
}
if (aa >= (1 << 32)) {
aa >>= 32;
xn <<= 16;
}
if (aa >= (1 << 16)) {
aa >>= 16;
xn <<= 8;
}
if (aa >= (1 << 8)) {
aa >>= 8;
xn <<= 4;
}
if (aa >= (1 << 4)) {
aa >>= 4;
xn <<= 2;
}
if (aa >= (1 << 2)) {
xn <<= 1;
}
// We now have x_n such that `x_n = 2**(e-1) ≤ sqrt(a) < 2**e = 2 * x_n`. This implies ε_n ≤ 2**(e-1).
//
// We can refine our estimation by noticing that the middle of that interval minimizes the error.
// If we move x_n to equal 2**(e-1) + 2**(e-2), then we reduce the error to ε_n ≤ 2**(e-2).
// This is going to be our x_0 (and ε_0)
xn = (3 * xn) >> 1; // ε_0 := | x_0 - sqrt(a) | ≤ 2**(e-2)
// From here, Newton's method give us:
// x_{n+1} = (x_n + a / x_n) / 2
//
// One should note that:
// x_{n+1}² - a = ((x_n + a / x_n) / 2)² - a
// = ((x_n² + a) / (2 * x_n))² - a
// = (x_n⁴ + 2 * a * x_n² + a²) / (4 * x_n²) - a
// = (x_n⁴ + 2 * a * x_n² + a² - 4 * a * x_n²) / (4 * x_n²)
// = (x_n⁴ - 2 * a * x_n² + a²) / (4 * x_n²)
// = (x_n² - a)² / (2 * x_n)²
// = ((x_n² - a) / (2 * x_n))²
// ≥ 0
// Which proves that for all n ≥ 1, sqrt(a) ≤ x_n
//
// This gives us the proof of quadratic convergence of the sequence:
// ε_{n+1} = | x_{n+1} - sqrt(a) |
// = | (x_n + a / x_n) / 2 - sqrt(a) |
// = | (x_n² + a - 2*x_n*sqrt(a)) / (2 * x_n) |
// = | (x_n - sqrt(a))² / (2 * x_n) |
// = | ε_n² / (2 * x_n) |
// = ε_n² / | (2 * x_n) |
//
// For the first iteration, we have a special case where x_0 is known:
// ε_1 = ε_0² / | (2 * x_0) |
// ≤ (2**(e-2))² / (2 * (2**(e-1) + 2**(e-2)))
// ≤ 2**(2*e-4) / (3 * 2**(e-1))
// ≤ 2**(e-3) / 3
// ≤ 2**(e-3-log2(3))
// ≤ 2**(e-4.5)
//
// For the following iterations, we use the fact that, 2**(e-1) ≤ sqrt(a) ≤ x_n:
// ε_{n+1} = ε_n² / | (2 * x_n) |
// ≤ (2**(e-k))² / (2 * 2**(e-1))
// ≤ 2**(2*e-2*k) / 2**e
// ≤ 2**(e-2*k)
xn = (xn + a / xn) >> 1; // ε_1 := | x_1 - sqrt(a) | ≤ 2**(e-4.5) -- special case, see above
xn = (xn + a / xn) >> 1; // ε_2 := | x_2 - sqrt(a) | ≤ 2**(e-9) -- general case with k = 4.5
xn = (xn + a / xn) >> 1; // ε_3 := | x_3 - sqrt(a) | ≤ 2**(e-18) -- general case with k = 9
xn = (xn + a / xn) >> 1; // ε_4 := | x_4 - sqrt(a) | ≤ 2**(e-36) -- general case with k = 18
xn = (xn + a / xn) >> 1; // ε_5 := | x_5 - sqrt(a) | ≤ 2**(e-72) -- general case with k = 36
xn = (xn + a / xn) >> 1; // ε_6 := | x_6 - sqrt(a) | ≤ 2**(e-144) -- general case with k = 72
// Because e ≤ 128 (as discussed during the first estimation phase), we know have reached a precision
// ε_6 ≤ 2**(e-144) < 1. Given we're operating on integers, then we can ensure that xn is now either
// sqrt(a) or sqrt(a) + 1.
return xn - SafeCast.toUint(xn > a / xn);
}
}
/**
* @dev Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
}
}
/**
* @dev Return the log in base 2 of a positive value rounded towards zero.
* Returns 0 if given 0.
*/
function log2(uint256 x) internal pure returns (uint256 r) {
// If value has upper 128 bits set, log2 result is at least 128
r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
// If upper 64 bits of 128-bit half set, add 64 to result
r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
// If upper 32 bits of 64-bit half set, add 32 to result
r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
// If upper 16 bits of 32-bit half set, add 16 to result
r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
// If upper 8 bits of 16-bit half set, add 8 to result
r |= SafeCast.toUint((x >> r) > 0xff) << 3;
// If upper 4 bits of 8-bit half set, add 4 to result
r |= SafeCast.toUint((x >> r) > 0xf) << 2;
// Shifts value right by the current result and use it as an index into this lookup table:
//
// | x (4 bits) | index | table[index] = MSB position |
// |------------|---------|-----------------------------|
// | 0000 | 0 | table[0] = 0 |
// | 0001 | 1 | table[1] = 0 |
// | 0010 | 2 | table[2] = 1 |
// | 0011 | 3 | table[3] = 1 |
// | 0100 | 4 | table[4] = 2 |
// | 0101 | 5 | table[5] = 2 |
// | 0110 | 6 | table[6] = 2 |
// | 0111 | 7 | table[7] = 2 |
// | 1000 | 8 | table[8] = 3 |
// | 1001 | 9 | table[9] = 3 |
// | 1010 | 10 | table[10] = 3 |
// | 1011 | 11 | table[11] = 3 |
// | 1100 | 12 | table[12] = 3 |
// | 1101 | 13 | table[13] = 3 |
// | 1110 | 14 | table[14] = 3 |
// | 1111 | 15 | table[15] = 3 |
//
// The lookup table is represented as a 32-byte value with the MSB positions for 0-15 in the last 16 bytes.
assembly ("memory-safe") {
r := or(r, byte(shr(r, x), 0x0000010102020202030303030303030300000000000000000000000000000000))
}
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
}
}
/**
* @dev Return the log in base 10 of a positive value rounded towards zero.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
}
}
/**
* @dev Return the log in base 256 of a positive value rounded towards zero.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 x) internal pure returns (uint256 r) {
// If value has upper 128 bits set, log2 result is at least 128
r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
// If upper 64 bits of 128-bit half set, add 64 to result
r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
// If upper 32 bits of 64-bit half set, add 32 to result
r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
// If upper 16 bits of 32-bit half set, add 16 to result
r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
// Add 1 if upper 8 bits of 16-bit half set, and divide accumulated result by 8
return (r >> 3) | SafeCast.toUint((x >> r) > 0xff);
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
}
}
/**
* @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
*/
function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
return uint8(rounding) % 2 == 1;
}
/**
* @dev Counts the number of leading zero bits in a uint256.
*/
function clz(uint256 x) internal pure returns (uint256) {
return ternary(x == 0, 256, 255 - log2(x));
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.
pragma solidity ^0.8.20;
/**
* @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeCast {
/**
* @dev Value doesn't fit in an uint of `bits` size.
*/
error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
/**
* @dev An int value doesn't fit in an uint of `bits` size.
*/
error SafeCastOverflowedIntToUint(int256 value);
/**
* @dev Value doesn't fit in an int of `bits` size.
*/
error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
/**
* @dev An uint value doesn't fit in an int of `bits` size.
*/
error SafeCastOverflowedUintToInt(uint256 value);
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*/
function toUint248(uint256 value) internal pure returns (uint248) {
if (value > type(uint248).max) {
revert SafeCastOverflowedUintDowncast(248, value);
}
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*/
function toUint240(uint256 value) internal pure returns (uint240) {
if (value > type(uint240).max) {
revert SafeCastOverflowedUintDowncast(240, value);
}
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*/
function toUint232(uint256 value) internal pure returns (uint232) {
if (value > type(uint232).max) {
revert SafeCastOverflowedUintDowncast(232, value);
}
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toUint224(uint256 value) internal pure returns (uint224) {
if (value > type(uint224).max) {
revert SafeCastOverflowedUintDowncast(224, value);
}
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*/
function toUint216(uint256 value) internal pure returns (uint216) {
if (value > type(uint216).max) {
revert SafeCastOverflowedUintDowncast(216, value);
}
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toUint208(uint256 value) internal pure returns (uint208) {
if (value > type(uint208).max) {
revert SafeCastOverflowedUintDowncast(208, value);
}
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*/
function toUint200(uint256 value) internal pure returns (uint200) {
if (value > type(uint200).max) {
revert SafeCastOverflowedUintDowncast(200, value);
}
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*/
function toUint192(uint256 value) internal pure returns (uint192) {
if (value > type(uint192).max) {
revert SafeCastOverflowedUintDowncast(192, value);
}
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*/
function toUint184(uint256 value) internal pure returns (uint184) {
if (value > type(uint184).max) {
revert SafeCastOverflowedUintDowncast(184, value);
}
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*/
function toUint176(uint256 value) internal pure returns (uint176) {
if (value > type(uint176).max) {
revert SafeCastOverflowedUintDowncast(176, value);
}
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*/
function toUint168(uint256 value) internal pure returns (uint168) {
if (value > type(uint168).max) {
revert SafeCastOverflowedUintDowncast(168, value);
}
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*/
function toUint160(uint256 value) internal pure returns (uint160) {
if (value > type(uint160).max) {
revert SafeCastOverflowedUintDowncast(160, value);
}
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*/
function toUint152(uint256 value) internal pure returns (uint152) {
if (value > type(uint152).max) {
revert SafeCastOverflowedUintDowncast(152, value);
}
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*/
function toUint144(uint256 value) internal pure returns (uint144) {
if (value > type(uint144).max) {
revert SafeCastOverflowedUintDowncast(144, value);
}
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*/
function toUint136(uint256 value) internal pure returns (uint136) {
if (value > type(uint136).max) {
revert SafeCastOverflowedUintDowncast(136, value);
}
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
if (value > type(uint128).max) {
revert SafeCastOverflowedUintDowncast(128, value);
}
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*/
function toUint120(uint256 value) internal pure returns (uint120) {
if (value > type(uint120).max) {
revert SafeCastOverflowedUintDowncast(120, value);
}
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*/
function toUint112(uint256 value) internal pure returns (uint112) {
if (value > type(uint112).max) {
revert SafeCastOverflowedUintDowncast(112, value);
}
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toUint104(uint256 value) internal pure returns (uint104) {
if (value > type(uint104).max) {
revert SafeCastOverflowedUintDowncast(104, value);
}
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toUint96(uint256 value) internal pure returns (uint96) {
if (value > type(uint96).max) {
revert SafeCastOverflowedUintDowncast(96, value);
}
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*/
function toUint88(uint256 value) internal pure returns (uint88) {
if (value > type(uint88).max) {
revert SafeCastOverflowedUintDowncast(88, value);
}
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*/
function toUint80(uint256 value) internal pure returns (uint80) {
if (value > type(uint80).max) {
revert SafeCastOverflowedUintDowncast(80, value);
}
return uint80(value);
}
/**
* @dev Returns the downcasted uint72 from uint256, reverting on
* overflow (when the input is greater than largest uint72).
*
* Counterpart to Solidity's `uint72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*/
function toUint72(uint256 value) internal pure returns (uint72) {
if (value > type(uint72).max) {
revert SafeCastOverflowedUintDowncast(72, value);
}
return uint72(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
if (value > type(uint64).max) {
revert SafeCastOverflowedUintDowncast(64, value);
}
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*/
function toUint56(uint256 value) internal pure returns (uint56) {
if (value > type(uint56).max) {
revert SafeCastOverflowedUintDowncast(56, value);
}
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*/
function toUint48(uint256 value) internal pure returns (uint48) {
if (value > type(uint48).max) {
revert SafeCastOverflowedUintDowncast(48, value);
}
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*/
function toUint40(uint256 value) internal pure returns (uint40) {
if (value > type(uint40).max) {
revert SafeCastOverflowedUintDowncast(40, value);
}
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
if (value > type(uint32).max) {
revert SafeCastOverflowedUintDowncast(32, value);
}
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*/
function toUint24(uint256 value) internal pure returns (uint24) {
if (value > type(uint24).max) {
revert SafeCastOverflowedUintDowncast(24, value);
}
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
if (value > type(uint16).max) {
revert SafeCastOverflowedUintDowncast(16, value);
}
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*/
function toUint8(uint256 value) internal pure returns (uint8) {
if (value > type(uint8).max) {
revert SafeCastOverflowedUintDowncast(8, value);
}
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*/
function toUint256(int256 value) internal pure returns (uint256) {
if (value < 0) {
revert SafeCastOverflowedIntToUint(value);
}
return uint256(value);
}
/**
* @dev Returns the downcasted int248 from int256, reverting on
* overflow (when the input is less than smallest int248 or
* greater than largest int248).
*
* Counterpart to Solidity's `int248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*/
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(248, value);
}
}
/**
* @dev Returns the downcasted int240 from int256, reverting on
* overflow (when the input is less than smallest int240 or
* greater than largest int240).
*
* Counterpart to Solidity's `int240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*/
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(240, value);
}
}
/**
* @dev Returns the downcasted int232 from int256, reverting on
* overflow (when the input is less than smallest int232 or
* greater than largest int232).
*
* Counterpart to Solidity's `int232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*/
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(232, value);
}
}
/**
* @dev Returns the downcasted int224 from int256, reverting on
* overflow (when the input is less than smallest int224 or
* greater than largest int224).
*
* Counterpart to Solidity's `int224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(224, value);
}
}
/**
* @dev Returns the downcasted int216 from int256, reverting on
* overflow (when the input is less than smallest int216 or
* greater than largest int216).
*
* Counterpart to Solidity's `int216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*/
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(216, value);
}
}
/**
* @dev Returns the downcasted int208 from int256, reverting on
* overflow (when the input is less than smallest int208 or
* greater than largest int208).
*
* Counterpart to Solidity's `int208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(208, value);
}
}
/**
* @dev Returns the downcasted int200 from int256, reverting on
* overflow (when the input is less than smallest int200 or
* greater than largest int200).
*
* Counterpart to Solidity's `int200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*/
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(200, value);
}
}
/**
* @dev Returns the downcasted int192 from int256, reverting on
* overflow (when the input is less than smallest int192 or
* greater than largest int192).
*
* Counterpart to Solidity's `int192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(192, value);
}
}
/**
* @dev Returns the downcasted int184 from int256, reverting on
* overflow (when the input is less than smallest int184 or
* greater than largest int184).
*
* Counterpart to Solidity's `int184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*/
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(184, value);
}
}
/**
* @dev Returns the downcasted int176 from int256, reverting on
* overflow (when the input is less than smallest int176 or
* greater than largest int176).
*
* Counterpart to Solidity's `int176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*/
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(176, value);
}
}
/**
* @dev Returns the downcasted int168 from int256, reverting on
* overflow (when the input is less than smallest int168 or
* greater than largest int168).
*
* Counterpart to Solidity's `int168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*/
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(168, value);
}
}
/**
* @dev Returns the downcasted int160 from int256, reverting on
* overflow (when the input is less than smallest int160 or
* greater than largest int160).
*
* Counterpart to Solidity's `int160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*/
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(160, value);
}
}
/**
* @dev Returns the downcasted int152 from int256, reverting on
* overflow (when the input is less than smallest int152 or
* greater than largest int152).
*
* Counterpart to Solidity's `int152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*/
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(152, value);
}
}
/**
* @dev Returns the downcasted int144 from int256, reverting on
* overflow (when the input is less than smallest int144 or
* greater than largest int144).
*
* Counterpart to Solidity's `int144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*/
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(144, value);
}
}
/**
* @dev Returns the downcasted int136 from int256, reverting on
* overflow (when the input is less than smallest int136 or
* greater than largest int136).
*
* Counterpart to Solidity's `int136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*/
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(136, value);
}
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(128, value);
}
}
/**
* @dev Returns the downcasted int120 from int256, reverting on
* overflow (when the input is less than smallest int120 or
* greater than largest int120).
*
* Counterpart to Solidity's `int120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*/
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(120, value);
}
}
/**
* @dev Returns the downcasted int112 from int256, reverting on
* overflow (when the input is less than smallest int112 or
* greater than largest int112).
*
* Counterpart to Solidity's `int112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*/
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(112, value);
}
}
/**
* @dev Returns the downcasted int104 from int256, reverting on
* overflow (when the input is less than smallest int104 or
* greater than largest int104).
*
* Counterpart to Solidity's `int104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(104, value);
}
}
/**
* @dev Returns the downcasted int96 from int256, reverting on
* overflow (when the input is less than smallest int96 or
* greater than largest int96).
*
* Counterpart to Solidity's `int96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(96, value);
}
}
/**
* @dev Returns the downcasted int88 from int256, reverting on
* overflow (when the input is less than smallest int88 or
* greater than largest int88).
*
* Counterpart to Solidity's `int88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*/
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(88, value);
}
}
/**
* @dev Returns the downcasted int80 from int256, reverting on
* overflow (when the input is less than smallest int80 or
* greater than largest int80).
*
* Counterpart to Solidity's `int80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*/
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(80, value);
}
}
/**
* @dev Returns the downcasted int72 from int256, reverting on
* overflow (when the input is less than smallest int72 or
* greater than largest int72).
*
* Counterpart to Solidity's `int72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*/
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(72, value);
}
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(64, value);
}
}
/**
* @dev Returns the downcasted int56 from int256, reverting on
* overflow (when the input is less than smallest int56 or
* greater than largest int56).
*
* Counterpart to Solidity's `int56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*/
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(56, value);
}
}
/**
* @dev Returns the downcasted int48 from int256, reverting on
* overflow (when the input is less than smallest int48 or
* greater than largest int48).
*
* Counterpart to Solidity's `int48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*/
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(48, value);
}
}
/**
* @dev Returns the downcasted int40 from int256, reverting on
* overflow (when the input is less than smallest int40 or
* greater than largest int40).
*
* Counterpart to Solidity's `int40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*/
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(40, value);
}
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(32, value);
}
}
/**
* @dev Returns the downcasted int24 from int256, reverting on
* overflow (when the input is less than smallest int24 or
* greater than largest int24).
*
* Counterpart to Solidity's `int24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*/
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(24, value);
}
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(16, value);
}
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(8, value);
}
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
if (value > uint256(type(int256).max)) {
revert SafeCastOverflowedUintToInt(value);
}
return int256(value);
}
/**
* @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
*/
function toUint(bool b) internal pure returns (uint256 u) {
assembly ("memory-safe") {
u := iszero(iszero(b))
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SignedMath.sol)
pragma solidity ^0.8.20;
import {SafeCast} from "./SafeCast.sol";
/**
* @dev Standard signed math utilities missing in the Solidity language.
*/
library SignedMath {
/**
* @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
*
* IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
* However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
* one branch when needed, making this function more expensive.
*/
function ternary(bool condition, int256 a, int256 b) internal pure returns (int256) {
unchecked {
// branchless ternary works because:
// b ^ (a ^ b) == a
// b ^ 0 == b
return b ^ ((a ^ b) * int256(SafeCast.toUint(condition)));
}
}
/**
* @dev Returns the largest of two signed numbers.
*/
function max(int256 a, int256 b) internal pure returns (int256) {
return ternary(a > b, a, b);
}
/**
* @dev Returns the smallest of two signed numbers.
*/
function min(int256 a, int256 b) internal pure returns (int256) {
return ternary(a < b, a, b);
}
/**
* @dev Returns the average of two signed numbers without overflow.
* The result is rounded towards zero.
*/
function average(int256 a, int256 b) internal pure returns (int256) {
// Formula from the book "Hacker's Delight"
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
/**
* @dev Returns the absolute unsigned value of a signed value.
*/
function abs(int256 n) internal pure returns (uint256) {
unchecked {
// Formula from the "Bit Twiddling Hacks" by Sean Eron Anderson.
// Since `n` is a signed integer, the generated bytecode will use the SAR opcode to perform the right shift,
// taking advantage of the most significant (or "sign" bit) in two's complement representation.
// This opcode adds new most significant bits set to the value of the previous most significant bit. As a result,
// the mask will either be `bytes32(0)` (if n is positive) or `~bytes32(0)` (if n is negative).
int256 mask = n >> 255;
// A `bytes32(0)` mask leaves the input unchanged, while a `~bytes32(0)` mask complements it.
return uint256((n + mask) ^ mask);
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/Bytes.sol)
pragma solidity ^0.8.24;
import {Math} from "./math/Math.sol";
/**
* @dev Bytes operations.
*/
library Bytes {
/**
* @dev Forward search for `s` in `buffer`
* * If `s` is present in the buffer, returns the index of the first instance
* * If `s` is not present in the buffer, returns type(uint256).max
*
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's `Array.indexOf`]
*/
function indexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256) {
return indexOf(buffer, s, 0);
}
/**
* @dev Forward search for `s` in `buffer` starting at position `pos`
* * If `s` is present in the buffer (at or after `pos`), returns the index of the next instance
* * If `s` is not present in the buffer (at or after `pos`), returns type(uint256).max
*
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's `Array.indexOf`]
*/
function indexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) {
uint256 length = buffer.length;
for (uint256 i = pos; i < length; ++i) {
if (bytes1(_unsafeReadBytesOffset(buffer, i)) == s) {
return i;
}
}
return type(uint256).max;
}
/**
* @dev Backward search for `s` in `buffer`
* * If `s` is present in the buffer, returns the index of the last instance
* * If `s` is not present in the buffer, returns type(uint256).max
*
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's `Array.lastIndexOf`]
*/
function lastIndexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256) {
return lastIndexOf(buffer, s, type(uint256).max);
}
/**
* @dev Backward search for `s` in `buffer` starting at position `pos`
* * If `s` is present in the buffer (at or before `pos`), returns the index of the previous instance
* * If `s` is not present in the buffer (at or before `pos`), returns type(uint256).max
*
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's `Array.lastIndexOf`]
*/
function lastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) {
unchecked {
uint256 length = buffer.length;
for (uint256 i = Math.min(Math.saturatingAdd(pos, 1), length); i > 0; --i) {
if (bytes1(_unsafeReadBytesOffset(buffer, i - 1)) == s) {
return i - 1;
}
}
return type(uint256).max;
}
}
/**
* @dev Copies the content of `buffer`, from `start` (included) to the end of `buffer` into a new bytes object in
* memory.
*
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`]
*/
function slice(bytes memory buffer, uint256 start) internal pure returns (bytes memory) {
return slice(buffer, start, buffer.length);
}
/**
* @dev Copies the content of `buffer`, from `start` (included) to `end` (excluded) into a new bytes object in
* memory. The `end` argument is truncated to the length of the `buffer`.
*
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`]
*/
function slice(bytes memory buffer, uint256 start, uint256 end) internal pure returns (bytes memory) {
// sanitize
end = Math.min(end, buffer.length);
start = Math.min(start, end);
// allocate and copy
bytes memory result = new bytes(end - start);
assembly ("memory-safe") {
mcopy(add(result, 0x20), add(add(buffer, 0x20), start), sub(end, start))
}
return result;
}
/**
* @dev Moves the content of `buffer`, from `start` (included) to the end of `buffer` to the start of that buffer.
*
* NOTE: This function modifies the provided buffer in place. If you need to preserve the original buffer, use {slice} instead
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`]
*/
function splice(bytes memory buffer, uint256 start) internal pure returns (bytes memory) {
return splice(buffer, start, buffer.length);
}
/**
* @dev Moves the content of `buffer`, from `start` (included) to end (excluded) to the start of that buffer. The
* `end` argument is truncated to the length of the `buffer`.
*
* NOTE: This function modifies the provided buffer in place. If you need to preserve the original buffer, use {slice} instead
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`]
*/
function splice(bytes memory buffer, uint256 start, uint256 end) internal pure returns (bytes memory) {
// sanitize
end = Math.min(end, buffer.length);
start = Math.min(start, end);
// allocate and copy
assembly ("memory-safe") {
mcopy(add(buffer, 0x20), add(add(buffer, 0x20), start), sub(end, start))
mstore(buffer, sub(end, start))
}
return buffer;
}
/**
* @dev Concatenate an array of bytes into a single bytes object.
*
* For fixed bytes types, we recommend using the solidity built-in `bytes.concat` or (equivalent)
* `abi.encodePacked`.
*
* NOTE: this could be done in assembly with a single loop that expands starting at the FMP, but that would be
* significantly less readable. It might be worth benchmarking the savings of the full-assembly approach.
*/
function concat(bytes[] memory buffers) internal pure returns (bytes memory) {
uint256 length = 0;
for (uint256 i = 0; i < buffers.length; ++i) {
length += buffers[i].length;
}
bytes memory result = new bytes(length);
uint256 offset = 0x20;
for (uint256 i = 0; i < buffers.length; ++i) {
bytes memory input = buffers[i];
assembly ("memory-safe") {
mcopy(add(result, offset), add(input, 0x20), mload(input))
}
unchecked {
offset += input.length;
}
}
return result;
}
/**
* @dev Returns true if the two byte buffers are equal.
*/
function equal(bytes memory a, bytes memory b) internal pure returns (bool) {
return a.length == b.length && keccak256(a) == keccak256(b);
}
/**
* @dev Reverses the byte order of a bytes32 value, converting between little-endian and big-endian.
* Inspired by https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel[Reverse Parallel]
*/
function reverseBytes32(bytes32 value) internal pure returns (bytes32) {
value = // swap bytes
((value >> 8) & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) |
((value & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
value = // swap 2-byte long pairs
((value >> 16) & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) |
((value & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
value = // swap 4-byte long pairs
((value >> 32) & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) |
((value & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32);
value = // swap 8-byte long pairs
((value >> 64) & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) |
((value & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64);
return (value >> 128) | (value << 128); // swap 16-byte long pairs
}
/// @dev Same as {reverseBytes32} but optimized for 128-bit values.
function reverseBytes16(bytes16 value) internal pure returns (bytes16) {
value = // swap bytes
((value & 0xFF00FF00FF00FF00FF00FF00FF00FF00) >> 8) |
((value & 0x00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
value = // swap 2-byte long pairs
((value & 0xFFFF0000FFFF0000FFFF0000FFFF0000) >> 16) |
((value & 0x0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
value = // swap 4-byte long pairs
((value & 0xFFFFFFFF00000000FFFFFFFF00000000) >> 32) |
((value & 0x00000000FFFFFFFF00000000FFFFFFFF) << 32);
return (value >> 64) | (value << 64); // swap 8-byte long pairs
}
/// @dev Same as {reverseBytes32} but optimized for 64-bit values.
function reverseBytes8(bytes8 value) internal pure returns (bytes8) {
value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8); // swap bytes
value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16); // swap 2-byte long pairs
return (value >> 32) | (value << 32); // swap 4-byte long pairs
}
/// @dev Same as {reverseBytes32} but optimized for 32-bit values.
function reverseBytes4(bytes4 value) internal pure returns (bytes4) {
value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); // swap bytes
return (value >> 16) | (value << 16); // swap 2-byte long pairs
}
/// @dev Same as {reverseBytes32} but optimized for 16-bit values.
function reverseBytes2(bytes2 value) internal pure returns (bytes2) {
return (value >> 8) | (value << 8);
}
/**
* @dev Counts the number of leading zero bits a bytes array. Returns `8 * buffer.length`
* if the buffer is all zeros.
*/
function clz(bytes memory buffer) internal pure returns (uint256) {
for (uint256 i = 0; i < buffer.length; i += 0x20) {
bytes32 chunk = _unsafeReadBytesOffset(buffer, i);
if (chunk != bytes32(0)) {
return Math.min(8 * i + Math.clz(uint256(chunk)), 8 * buffer.length);
}
}
return 8 * buffer.length;
}
/**
* @dev Reads a bytes32 from a bytes array without bounds checking.
*
* NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
* assembly block as such would prevent some optimizations.
*/
function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
// This is not memory safe in the general case, but all calls to this private function are within bounds.
assembly ("memory-safe") {
value := mload(add(add(buffer, 0x20), offset))
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC1363.sol)
pragma solidity >=0.6.2;
import {IERC20} from "./IERC20.sol";
import {IERC165} from "./IERC165.sol";
/**
* @title IERC1363
* @dev Interface of the ERC-1363 standard as defined in the https://eips.ethereum.org/EIPS/eip-1363[ERC-1363].
*
* Defines an extension interface for ERC-20 tokens that supports executing code on a recipient contract
* after `transfer` or `transferFrom`, or code on a spender contract after `approve`, in a single transaction.
*/
interface IERC1363 is IERC20, IERC165 {
/*
* Note: the ERC-165 identifier for this interface is 0xb0202a11.
* 0xb0202a11 ===
* bytes4(keccak256('transferAndCall(address,uint256)')) ^
* bytes4(keccak256('transferAndCall(address,uint256,bytes)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256,bytes)')) ^
* bytes4(keccak256('approveAndCall(address,uint256)')) ^
* bytes4(keccak256('approveAndCall(address,uint256,bytes)'))
*/
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @param data Additional data with no specified format, sent in call to `to`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param from The address which you want to send tokens from.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferFromAndCall(address from, address to, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param from The address which you want to send tokens from.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @param data Additional data with no specified format, sent in call to `to`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferFromAndCall(address from, address to, uint256 value, bytes calldata data) external returns (bool);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function approveAndCall(address spender, uint256 value) external returns (bool);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
* @param data Additional data with no specified format, sent in call to `spender`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function approveAndCall(address spender, uint256 value, bytes calldata data) external returns (bool);
}// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;
/// @notice Emitted when the result overflows uint256.
error PRBMath__MulDivFixedPointOverflow(uint256 prod1);
/// @notice Emitted when the result overflows uint256.
error PRBMath__MulDivOverflow(uint256 prod1, uint256 denominator);
/// @notice Emitted when one of the inputs is type(int256).min.
error PRBMath__MulDivSignedInputTooSmall();
/// @notice Emitted when the intermediary absolute result overflows int256.
error PRBMath__MulDivSignedOverflow(uint256 rAbs);
/// @notice Emitted when the input is MIN_SD59x18.
error PRBMathSD59x18__AbsInputTooSmall();
/// @notice Emitted when ceiling a number overflows SD59x18.
error PRBMathSD59x18__CeilOverflow(int256 x);
/// @notice Emitted when one of the inputs is MIN_SD59x18.
error PRBMathSD59x18__DivInputTooSmall();
/// @notice Emitted when one of the intermediary unsigned results overflows SD59x18.
error PRBMathSD59x18__DivOverflow(uint256 rAbs);
/// @notice Emitted when the input is greater than 133.084258667509499441.
error PRBMathSD59x18__ExpInputTooBig(int256 x);
/// @notice Emitted when the input is greater than 192.
error PRBMathSD59x18__Exp2InputTooBig(int256 x);
/// @notice Emitted when flooring a number underflows SD59x18.
error PRBMathSD59x18__FloorUnderflow(int256 x);
/// @notice Emitted when converting a basic integer to the fixed-point format overflows SD59x18.
error PRBMathSD59x18__FromIntOverflow(int256 x);
/// @notice Emitted when converting a basic integer to the fixed-point format underflows SD59x18.
error PRBMathSD59x18__FromIntUnderflow(int256 x);
/// @notice Emitted when the product of the inputs is negative.
error PRBMathSD59x18__GmNegativeProduct(int256 x, int256 y);
/// @notice Emitted when multiplying the inputs overflows SD59x18.
error PRBMathSD59x18__GmOverflow(int256 x, int256 y);
/// @notice Emitted when the input is less than or equal to zero.
error PRBMathSD59x18__LogInputTooSmall(int256 x);
/// @notice Emitted when one of the inputs is MIN_SD59x18.
error PRBMathSD59x18__MulInputTooSmall();
/// @notice Emitted when the intermediary absolute result overflows SD59x18.
error PRBMathSD59x18__MulOverflow(uint256 rAbs);
/// @notice Emitted when the intermediary absolute result overflows SD59x18.
error PRBMathSD59x18__PowuOverflow(uint256 rAbs);
/// @notice Emitted when the input is negative.
error PRBMathSD59x18__SqrtNegativeInput(int256 x);
/// @notice Emitted when the calculating the square root overflows SD59x18.
error PRBMathSD59x18__SqrtOverflow(int256 x);
/// @notice Emitted when addition overflows UD60x18.
error PRBMathUD60x18__AddOverflow(uint256 x, uint256 y);
/// @notice Emitted when ceiling a number overflows UD60x18.
error PRBMathUD60x18__CeilOverflow(uint256 x);
/// @notice Emitted when the input is greater than 133.084258667509499441.
error PRBMathUD60x18__ExpInputTooBig(uint256 x);
/// @notice Emitted when the input is greater than 192.
error PRBMathUD60x18__Exp2InputTooBig(uint256 x);
/// @notice Emitted when converting a basic integer to the fixed-point format format overflows UD60x18.
error PRBMathUD60x18__FromUintOverflow(uint256 x);
/// @notice Emitted when multiplying the inputs overflows UD60x18.
error PRBMathUD60x18__GmOverflow(uint256 x, uint256 y);
/// @notice Emitted when the input is less than 1.
error PRBMathUD60x18__LogInputTooSmall(uint256 x);
/// @notice Emitted when the calculating the square root overflows UD60x18.
error PRBMathUD60x18__SqrtOverflow(uint256 x);
/// @notice Emitted when subtraction underflows UD60x18.
error PRBMathUD60x18__SubUnderflow(uint256 x, uint256 y);
/// @dev Common mathematical functions used in both PRBMathSD59x18 and PRBMathUD60x18. Note that this shared library
/// does not always assume the signed 59.18-decimal fixed-point or the unsigned 60.18-decimal fixed-point
/// representation. When it does not, it is explicitly mentioned in the NatSpec documentation.
library PRBMath {
/// STRUCTS ///
struct SD59x18 {
int256 value;
}
struct UD60x18 {
uint256 value;
}
/// STORAGE ///
/// @dev How many trailing decimals can be represented.
uint256 internal constant SCALE = 1e18;
/// @dev Largest power of two divisor of SCALE.
uint256 internal constant SCALE_LPOTD = 262144;
/// @dev SCALE inverted mod 2^256.
uint256 internal constant SCALE_INVERSE =
78156646155174841979727994598816262306175212592076161876661_508869554232690281;
/// FUNCTIONS ///
/// @notice Calculates the binary exponent of x using the binary fraction method.
/// @dev Has to use 192.64-bit fixed-point numbers.
/// See https://ethereum.stackexchange.com/a/96594/24693.
/// @param x The exponent as an unsigned 192.64-bit fixed-point number.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function exp2(uint256 x) internal pure returns (uint256 result) {
unchecked {
// Start from 0.5 in the 192.64-bit fixed-point format.
result = 0x800000000000000000000000000000000000000000000000;
// Multiply the result by root(2, 2^-i) when the bit at position i is 1. None of the intermediary results overflows
// because the initial result is 2^191 and all magic factors are less than 2^65.
if (x & 0x8000000000000000 > 0) {
result = (result * 0x16A09E667F3BCC909) >> 64;
}
if (x & 0x4000000000000000 > 0) {
result = (result * 0x1306FE0A31B7152DF) >> 64;
}
if (x & 0x2000000000000000 > 0) {
result = (result * 0x1172B83C7D517ADCE) >> 64;
}
if (x & 0x1000000000000000 > 0) {
result = (result * 0x10B5586CF9890F62A) >> 64;
}
if (x & 0x800000000000000 > 0) {
result = (result * 0x1059B0D31585743AE) >> 64;
}
if (x & 0x400000000000000 > 0) {
result = (result * 0x102C9A3E778060EE7) >> 64;
}
if (x & 0x200000000000000 > 0) {
result = (result * 0x10163DA9FB33356D8) >> 64;
}
if (x & 0x100000000000000 > 0) {
result = (result * 0x100B1AFA5ABCBED61) >> 64;
}
if (x & 0x80000000000000 > 0) {
result = (result * 0x10058C86DA1C09EA2) >> 64;
}
if (x & 0x40000000000000 > 0) {
result = (result * 0x1002C605E2E8CEC50) >> 64;
}
if (x & 0x20000000000000 > 0) {
result = (result * 0x100162F3904051FA1) >> 64;
}
if (x & 0x10000000000000 > 0) {
result = (result * 0x1000B175EFFDC76BA) >> 64;
}
if (x & 0x8000000000000 > 0) {
result = (result * 0x100058BA01FB9F96D) >> 64;
}
if (x & 0x4000000000000 > 0) {
result = (result * 0x10002C5CC37DA9492) >> 64;
}
if (x & 0x2000000000000 > 0) {
result = (result * 0x1000162E525EE0547) >> 64;
}
if (x & 0x1000000000000 > 0) {
result = (result * 0x10000B17255775C04) >> 64;
}
if (x & 0x800000000000 > 0) {
result = (result * 0x1000058B91B5BC9AE) >> 64;
}
if (x & 0x400000000000 > 0) {
result = (result * 0x100002C5C89D5EC6D) >> 64;
}
if (x & 0x200000000000 > 0) {
result = (result * 0x10000162E43F4F831) >> 64;
}
if (x & 0x100000000000 > 0) {
result = (result * 0x100000B1721BCFC9A) >> 64;
}
if (x & 0x80000000000 > 0) {
result = (result * 0x10000058B90CF1E6E) >> 64;
}
if (x & 0x40000000000 > 0) {
result = (result * 0x1000002C5C863B73F) >> 64;
}
if (x & 0x20000000000 > 0) {
result = (result * 0x100000162E430E5A2) >> 64;
}
if (x & 0x10000000000 > 0) {
result = (result * 0x1000000B172183551) >> 64;
}
if (x & 0x8000000000 > 0) {
result = (result * 0x100000058B90C0B49) >> 64;
}
if (x & 0x4000000000 > 0) {
result = (result * 0x10000002C5C8601CC) >> 64;
}
if (x & 0x2000000000 > 0) {
result = (result * 0x1000000162E42FFF0) >> 64;
}
if (x & 0x1000000000 > 0) {
result = (result * 0x10000000B17217FBB) >> 64;
}
if (x & 0x800000000 > 0) {
result = (result * 0x1000000058B90BFCE) >> 64;
}
if (x & 0x400000000 > 0) {
result = (result * 0x100000002C5C85FE3) >> 64;
}
if (x & 0x200000000 > 0) {
result = (result * 0x10000000162E42FF1) >> 64;
}
if (x & 0x100000000 > 0) {
result = (result * 0x100000000B17217F8) >> 64;
}
if (x & 0x80000000 > 0) {
result = (result * 0x10000000058B90BFC) >> 64;
}
if (x & 0x40000000 > 0) {
result = (result * 0x1000000002C5C85FE) >> 64;
}
if (x & 0x20000000 > 0) {
result = (result * 0x100000000162E42FF) >> 64;
}
if (x & 0x10000000 > 0) {
result = (result * 0x1000000000B17217F) >> 64;
}
if (x & 0x8000000 > 0) {
result = (result * 0x100000000058B90C0) >> 64;
}
if (x & 0x4000000 > 0) {
result = (result * 0x10000000002C5C860) >> 64;
}
if (x & 0x2000000 > 0) {
result = (result * 0x1000000000162E430) >> 64;
}
if (x & 0x1000000 > 0) {
result = (result * 0x10000000000B17218) >> 64;
}
if (x & 0x800000 > 0) {
result = (result * 0x1000000000058B90C) >> 64;
}
if (x & 0x400000 > 0) {
result = (result * 0x100000000002C5C86) >> 64;
}
if (x & 0x200000 > 0) {
result = (result * 0x10000000000162E43) >> 64;
}
if (x & 0x100000 > 0) {
result = (result * 0x100000000000B1721) >> 64;
}
if (x & 0x80000 > 0) {
result = (result * 0x10000000000058B91) >> 64;
}
if (x & 0x40000 > 0) {
result = (result * 0x1000000000002C5C8) >> 64;
}
if (x & 0x20000 > 0) {
result = (result * 0x100000000000162E4) >> 64;
}
if (x & 0x10000 > 0) {
result = (result * 0x1000000000000B172) >> 64;
}
if (x & 0x8000 > 0) {
result = (result * 0x100000000000058B9) >> 64;
}
if (x & 0x4000 > 0) {
result = (result * 0x10000000000002C5D) >> 64;
}
if (x & 0x2000 > 0) {
result = (result * 0x1000000000000162E) >> 64;
}
if (x & 0x1000 > 0) {
result = (result * 0x10000000000000B17) >> 64;
}
if (x & 0x800 > 0) {
result = (result * 0x1000000000000058C) >> 64;
}
if (x & 0x400 > 0) {
result = (result * 0x100000000000002C6) >> 64;
}
if (x & 0x200 > 0) {
result = (result * 0x10000000000000163) >> 64;
}
if (x & 0x100 > 0) {
result = (result * 0x100000000000000B1) >> 64;
}
if (x & 0x80 > 0) {
result = (result * 0x10000000000000059) >> 64;
}
if (x & 0x40 > 0) {
result = (result * 0x1000000000000002C) >> 64;
}
if (x & 0x20 > 0) {
result = (result * 0x10000000000000016) >> 64;
}
if (x & 0x10 > 0) {
result = (result * 0x1000000000000000B) >> 64;
}
if (x & 0x8 > 0) {
result = (result * 0x10000000000000006) >> 64;
}
if (x & 0x4 > 0) {
result = (result * 0x10000000000000003) >> 64;
}
if (x & 0x2 > 0) {
result = (result * 0x10000000000000001) >> 64;
}
if (x & 0x1 > 0) {
result = (result * 0x10000000000000001) >> 64;
}
// We're doing two things at the same time:
//
// 1. Multiply the result by 2^n + 1, where "2^n" is the integer part and the one is added to account for
// the fact that we initially set the result to 0.5. This is accomplished by subtracting from 191
// rather than 192.
// 2. Convert the result to the unsigned 60.18-decimal fixed-point format.
//
// This works because 2^(191-ip) = 2^ip / 2^191, where "ip" is the integer part "2^n".
result *= SCALE;
result >>= (191 - (x >> 64));
}
}
/// @notice Finds the zero-based index of the first one in the binary representation of x.
/// @dev See the note on msb in the "Find First Set" Wikipedia article https://en.wikipedia.org/wiki/Find_first_set
/// @param x The uint256 number for which to find the index of the most significant bit.
/// @return msb The index of the most significant bit as an uint256.
function mostSignificantBit(uint256 x) internal pure returns (uint256 msb) {
if (x >= 2**128) {
x >>= 128;
msb += 128;
}
if (x >= 2**64) {
x >>= 64;
msb += 64;
}
if (x >= 2**32) {
x >>= 32;
msb += 32;
}
if (x >= 2**16) {
x >>= 16;
msb += 16;
}
if (x >= 2**8) {
x >>= 8;
msb += 8;
}
if (x >= 2**4) {
x >>= 4;
msb += 4;
}
if (x >= 2**2) {
x >>= 2;
msb += 2;
}
if (x >= 2**1) {
// No need to shift x any more.
msb += 1;
}
}
/// @notice Calculates floor(x*y÷denominator) with full precision.
///
/// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv.
///
/// Requirements:
/// - The denominator cannot be zero.
/// - The result must fit within uint256.
///
/// Caveats:
/// - This function does not work with fixed-point numbers.
///
/// @param x The multiplicand as an uint256.
/// @param y The multiplier as an uint256.
/// @param denominator The divisor as an uint256.
/// @return result The result as an uint256.
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 result) {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
unchecked {
result = prod0 / denominator;
}
return result;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
if (prod1 >= denominator) {
revert PRBMath__MulDivOverflow(prod1, denominator);
}
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
unchecked {
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 lpotdod = denominator & (~denominator + 1);
assembly {
// Divide denominator by lpotdod.
denominator := div(denominator, lpotdod)
// Divide [prod1 prod0] by lpotdod.
prod0 := div(prod0, lpotdod)
// Flip lpotdod such that it is 2^256 / lpotdod. If lpotdod is zero, then it becomes one.
lpotdod := add(div(sub(0, lpotdod), lpotdod), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * lpotdod;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the 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.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/// @notice Calculates floor(x*y÷1e18) with full precision.
///
/// @dev Variant of "mulDiv" with constant folding, i.e. in which the denominator is always 1e18. Before returning the
/// final result, we add 1 if (x * y) % SCALE >= HALF_SCALE. Without this, 6.6e-19 would be truncated to 0 instead of
/// being rounded to 1e-18. See "Listing 6" and text above it at https://accu.org/index.php/journals/1717.
///
/// Requirements:
/// - The result must fit within uint256.
///
/// Caveats:
/// - The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works.
/// - It is assumed that the result can never be type(uint256).max when x and y solve the following two equations:
/// 1. x * y = type(uint256).max * SCALE
/// 2. (x * y) % SCALE >= SCALE / 2
///
/// @param x The multiplicand as an unsigned 60.18-decimal fixed-point number.
/// @param y The multiplier as an unsigned 60.18-decimal fixed-point number.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function mulDivFixedPoint(uint256 x, uint256 y) internal pure returns (uint256 result) {
uint256 prod0;
uint256 prod1;
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
if (prod1 >= SCALE) {
revert PRBMath__MulDivFixedPointOverflow(prod1);
}
uint256 remainder;
uint256 roundUpUnit;
assembly {
remainder := mulmod(x, y, SCALE)
roundUpUnit := gt(remainder, 499999999999999999)
}
if (prod1 == 0) {
unchecked {
result = (prod0 / SCALE) + roundUpUnit;
return result;
}
}
assembly {
result := add(
mul(
or(
div(sub(prod0, remainder), SCALE_LPOTD),
mul(sub(prod1, gt(remainder, prod0)), add(div(sub(0, SCALE_LPOTD), SCALE_LPOTD), 1))
),
SCALE_INVERSE
),
roundUpUnit
)
}
}
/// @notice Calculates floor(x*y÷denominator) with full precision.
///
/// @dev An extension of "mulDiv" for signed numbers. Works by computing the signs and the absolute values separately.
///
/// Requirements:
/// - None of the inputs can be type(int256).min.
/// - The result must fit within int256.
///
/// @param x The multiplicand as an int256.
/// @param y The multiplier as an int256.
/// @param denominator The divisor as an int256.
/// @return result The result as an int256.
function mulDivSigned(
int256 x,
int256 y,
int256 denominator
) internal pure returns (int256 result) {
if (x == type(int256).min || y == type(int256).min || denominator == type(int256).min) {
revert PRBMath__MulDivSignedInputTooSmall();
}
// Get hold of the absolute values of x, y and the denominator.
uint256 ax;
uint256 ay;
uint256 ad;
unchecked {
ax = x < 0 ? uint256(-x) : uint256(x);
ay = y < 0 ? uint256(-y) : uint256(y);
ad = denominator < 0 ? uint256(-denominator) : uint256(denominator);
}
// Compute the absolute value of (x*y)÷denominator. The result must fit within int256.
uint256 rAbs = mulDiv(ax, ay, ad);
if (rAbs > uint256(type(int256).max)) {
revert PRBMath__MulDivSignedOverflow(rAbs);
}
// Get the signs of x, y and the denominator.
uint256 sx;
uint256 sy;
uint256 sd;
assembly {
sx := sgt(x, sub(0, 1))
sy := sgt(y, sub(0, 1))
sd := sgt(denominator, sub(0, 1))
}
// XOR over sx, sy and sd. This is checking whether there are one or three negative signs in the inputs.
// If yes, the result should be negative.
result = sx ^ sy ^ sd == 0 ? -int256(rAbs) : int256(rAbs);
}
/// @notice Calculates the square root of x, rounding down.
/// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
///
/// Caveats:
/// - This function does not work with fixed-point numbers.
///
/// @param x The uint256 number for which to calculate the square root.
/// @return result The result as an uint256.
function sqrt(uint256 x) internal pure returns (uint256 result) {
if (x == 0) {
return 0;
}
// Set the initial guess to the least power of two that is greater than or equal to sqrt(x).
uint256 xAux = uint256(x);
result = 1;
if (xAux >= 0x100000000000000000000000000000000) {
xAux >>= 128;
result <<= 64;
}
if (xAux >= 0x10000000000000000) {
xAux >>= 64;
result <<= 32;
}
if (xAux >= 0x100000000) {
xAux >>= 32;
result <<= 16;
}
if (xAux >= 0x10000) {
xAux >>= 16;
result <<= 8;
}
if (xAux >= 0x100) {
xAux >>= 8;
result <<= 4;
}
if (xAux >= 0x10) {
xAux >>= 4;
result <<= 2;
}
if (xAux >= 0x8) {
result <<= 1;
}
// The operations can never overflow because the result is max 2^127 when it enters this block.
unchecked {
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1; // Seven iterations should be enough
uint256 roundedDownResult = x / result;
return result >= roundedDownResult ? roundedDownResult : result;
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/Panic.sol)
pragma solidity ^0.8.20;
/**
* @dev Helper library for emitting standardized panic codes.
*
* ```solidity
* contract Example {
* using Panic for uint256;
*
* // Use any of the declared internal constants
* function foo() { Panic.GENERIC.panic(); }
*
* // Alternatively
* function foo() { Panic.panic(Panic.GENERIC); }
* }
* ```
*
* Follows the list from https://github.com/ethereum/solidity/blob/v0.8.24/libsolutil/ErrorCodes.h[libsolutil].
*
* _Available since v5.1._
*/
// slither-disable-next-line unused-state
library Panic {
/// @dev generic / unspecified error
uint256 internal constant GENERIC = 0x00;
/// @dev used by the assert() builtin
uint256 internal constant ASSERT = 0x01;
/// @dev arithmetic underflow or overflow
uint256 internal constant UNDER_OVERFLOW = 0x11;
/// @dev division or modulo by zero
uint256 internal constant DIVISION_BY_ZERO = 0x12;
/// @dev enum conversion error
uint256 internal constant ENUM_CONVERSION_ERROR = 0x21;
/// @dev invalid encoding in storage
uint256 internal constant STORAGE_ENCODING_ERROR = 0x22;
/// @dev empty array pop
uint256 internal constant EMPTY_ARRAY_POP = 0x31;
/// @dev array out of bounds access
uint256 internal constant ARRAY_OUT_OF_BOUNDS = 0x32;
/// @dev resource error (too large allocation or too large array)
uint256 internal constant RESOURCE_ERROR = 0x41;
/// @dev calling invalid internal function
uint256 internal constant INVALID_INTERNAL_FUNCTION = 0x51;
/// @dev Reverts with a panic code. Recommended to use with
/// the internal constants with predefined codes.
function panic(uint256 code) internal pure {
assembly ("memory-safe") {
mstore(0x00, 0x4e487b71)
mstore(0x20, code)
revert(0x1c, 0x24)
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC20.sol)
pragma solidity >=0.4.16;
import {IERC20} from "../token/ERC20/IERC20.sol";// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC165.sol)
pragma solidity >=0.4.16;
import {IERC165} from "../utils/introspection/IERC165.sol";{
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-std/=lib/forge-std/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"prb-math/=node_modules/prb-math/",
"halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/"
],
"optimizer": {
"enabled": false,
"runs": 200
},
"metadata": {
"useLiteralContent": false,
"bytecodeHash": "ipfs",
"appendCBOR": true
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"evmVersion": "prague",
"viaIR": false
}Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[{"internalType":"address","name":"_stableCoin","type":"address"},{"internalType":"address","name":"_csToken","type":"address"}],"stateMutability":"payable","type":"constructor"},{"inputs":[],"name":"CallNotFromBitsave","type":"error"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"CanNotWithdrawToken","type":"error"},{"inputs":[],"name":"InvalidTime","type":"error"},{"inputs":[],"name":"MasterCallRequired","type":"error"},{"inputs":[],"name":"NotEnoughToPayGasFee","type":"error"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"NotSupported","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"SafeERC20FailedOperation","type":"error"},{"inputs":[],"name":"UserNotRegistered","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"userAddress","type":"address"}],"name":"JoinedBitsave","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"string","name":"nameOfSaving","type":"string"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"address","name":"token","type":"address"}],"name":"SavingCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"string","name":"nameOfSaving","type":"string"},{"indexed":false,"internalType":"uint256","name":"amountAdded","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalAmountNow","type":"uint256"},{"indexed":false,"internalType":"address","name":"token","type":"address"}],"name":"SavingIncremented","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"string","name":"nameOfSaving","type":"string"}],"name":"SavingWithdrawn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"SystemFaucetDrip","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"TokenWithdrawal","type":"event"},{"inputs":[],"name":"ChildContractGasFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ChildCutPerFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"JoinLimitFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SavingFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"nameOfSaving","type":"string"},{"internalType":"uint256","name":"maturityTime","type":"uint256"},{"internalType":"uint8","name":"penaltyPercentage","type":"uint8"},{"internalType":"bool","name":"safeMode","type":"bool"},{"internalType":"address","name":"tokenToSave","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"createSaving","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"csToken","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"currentTotalValueLocked","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"currentVaultState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"dripFountain","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_joinFee","type":"uint256"},{"internalType":"uint256","name":"_savingFee","type":"uint256"},{"internalType":"uint256","name":"_childCutPerFee","type":"uint256"}],"name":"editFees","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_newCurrentVaultState","type":"uint256"},{"internalType":"uint256","name":"_newTotalValueLocked","type":"uint256"},{"internalType":"address","name":"_newCsToken","type":"address"}],"name":"editInternalData","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newStableCoin","type":"address"}],"name":"editStableCoin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"fountain","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUserChildContractAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"nameOfSavings","type":"string"},{"internalType":"address","name":"tokenToRetrieve","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"incrementSaving","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"joinBitsave","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"masterAddress","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardPool","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"originalToken","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"ownerAddress","type":"address"}],"name":"sendAsOriginalToken","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"stableCoin","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"userCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"nameOfSavings","type":"string"}],"name":"withdrawSaving","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]Contract Creation Code
60806040525f600955655af3107a4000600a556032600b55600b54600a5461002791906101c6565b600c55604051615d06380380615d06833981810160405281019061004b9190610254565b600161006961005e61015e60201b60201c565b61018760201b60201c565b5f0181905550815f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055503360025f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505f6003819055505f60068190555062d59f80600781905550620186a0600881905550346004819055505050610292565b5f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005f1b905090565b5f819050919050565b5f819050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b5f6101d082610190565b91506101db83610190565b9250826101eb576101ea610199565b5b828204905092915050565b5f5ffd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610223826101fa565b9050919050565b61023381610219565b811461023d575f5ffd5b50565b5f8151905061024e8161022a565b92915050565b5f5f6040838503121561026a576102696101f6565b5b5f61027785828601610240565b925050602061028885828601610240565b9150509250929050565b615a678061029f5f395ff3fe608060405260043610610138575f3560e01c806384762b0b116100aa578063d365a08e1161006e578063d365a08e146103c5578063de35c372146103ef578063e04605fd1461041f578063ea41367214610449578063f24053b314610473578063fed0b9251461049d5761013f565b806384762b0b146102f7578063992642e5146103215780639986e0e91461034b578063a63748e514610373578063bf66bfc71461039b5761013f565b8063372f9101116100fc578063372f9101146101fb5780634c3fa3041461023757806352f44e2e146102615780636405350a1461028957806366666aa9146102a55780637679489c146102cf5761013f565b8063025007391461014357806307973ccf1461016d57806309a5ff8c1461019757806316f94d5d146101c15780631fdc181f146101df5761013f565b3661013f57005b5f5ffd5b34801561014e575f5ffd5b506101576104c7565b6040516101649190611ed9565b60405180910390f35b348015610178575f5ffd5b506101816104cd565b60405161018e9190611ed9565b60405180910390f35b3480156101a2575f5ffd5b506101ab6104d3565b6040516101b89190611ed9565b60405180910390f35b6101c96104d9565b6040516101d69190611f31565b60405180910390f35b6101f960048036038101906101f491906120eb565b6106b2565b005b348015610206575f5ffd5b50610221600480360381019061021c9190612157565b610b01565b60405161022e91906121b8565b60405180910390f35b348015610242575f5ffd5b5061024b610cee565b6040516102589190611ed9565b60405180910390f35b34801561026c575f5ffd5b50610287600480360381019061028291906121d1565b610cf4565b005b6102a3600480360381019061029e919061225c565b610f8b565b005b3480156102b0575f5ffd5b506102b961124a565b6040516102c69190611ed9565b60405180910390f35b3480156102da575f5ffd5b506102f560048036038101906102f09190612301565b611250565b005b348015610302575f5ffd5b5061030b61135d565b6040516103189190611ed9565b60405180910390f35b34801561032c575f5ffd5b50610335611363565b60405161034291906123ac565b60405180910390f35b348015610356575f5ffd5b50610371600480360381019061036c91906121d1565b611387565b005b34801561037e575f5ffd5b50610399600480360381019061039491906123c5565b611483565b005b3480156103a6575f5ffd5b506103af611543565b6040516103bc9190611ed9565b60405180910390f35b3480156103d0575f5ffd5b506103d9611549565b6040516103e69190612435565b60405180910390f35b6104096004803603810190610404919061244e565b61156e565b60405161041691906121b8565b60405180910390f35b34801561042a575f5ffd5b506104336116b3565b60405161044091906123ac565b60405180910390f35b348015610454575f5ffd5b5061045d6116d8565b60405161046a9190611f31565b60405180910390f35b34801561047e575f5ffd5b5061048761173b565b6040516104949190611ed9565b60405180910390f35b3480156104a8575f5ffd5b506104b1611741565b6040516104be9190611ed9565b60405180910390f35b60085481565b60065481565b600b5481565b5f5f3390505f60055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161461057b5780925050506106af565b5f335f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff166040516105aa90611eb4565b6105b592919061249e565b604051809103905ff0801580156105ce573d5f5f3e3d5ffd5b5090508060055f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600160065f82825461065e91906124f2565b925050819055508273ffffffffffffffffffffffffffffffffffffffff167f0c5c0c4d53b63790e32770cf47fd115d706000fd7ab63fbdc8d176c5020d6b4660405160405180910390a28093505050505b90565b335f73ffffffffffffffffffffffffffffffffffffffff1660055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1603610774576040517f2163950f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f60055f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f8190505f8173ffffffffffffffffffffffffffffffffffffffff1663e18f5f65886040518263ffffffff1660e01b81526004016108129190612585565b602060405180830381865afa15801561082d573d5f5f3e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061085191906125b9565b90505f5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161490505f8690505f8473ffffffffffffffffffffffffffffffffffffffff16631bb862a58b6040518263ffffffff1660e01b81526004016108c39190612585565b602060405180830381865afa1580156108de573d5f5f3e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061090291906125f8565b9050801561092e575f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1698505b826109825761093e86838b611747565b61097d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016109749061266d565b60405180910390fd5b610986565b3491505b5f6109938986895f611783565b90508573ffffffffffffffffffffffffffffffffffffffff1663dc2379b9856109bc575f6109be565b825b8d846007546008546040518663ffffffff1660e01b81526004016109e5949392919061268b565b60206040518083038185885af1158015610a01573d5f5f3e3d5ffd5b50505050506040513d601f19601f82011682018060405250810190610a2691906126e9565b505f8673ffffffffffffffffffffffffffffffffffffffff1663e0ea6d408d6040518263ffffffff1660e01b8152600401610a619190612585565b602060405180830381865afa158015610a7c573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610aa091906126e9565b90508b604051610ab0919061274e565b60405180910390207ff6b98d8e0d83f8b1d1ff03a2fa36aba2c77ccf20e4ff035c0cf4aa92762e9eec8b838e604051610aeb93929190612764565b60405180910390a2505050505050505050505050565b5f335f73ffffffffffffffffffffffffffffffffffffffff1660055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1603610bc4576040517f2163950f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f60055f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690508073ffffffffffffffffffffffffffffffffffffffff1663372f9101856040518263ffffffff1660e01b8152600401610c5d9190612585565b5f604051808303815f875af1158015610c78573d5f5f3e3d5ffd5b505050506040513d5f823e3d601f19601f82011682018060405250810190610ca09190612807565b5083604051610caf919061274e565b60405180910390207f5b059b7fe1fea38d4de595079621498a365a8f1fa13e49af9f1e4b7197ab049a60405160405180910390a2600192505050919050565b60075481565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610d7a576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b610d826118d6565b5f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603610ef7575f4790505f5f9050600454821115610ea25760045482610dd5919061284e565b90505f60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1682604051610e1d906128ae565b5f6040518083038185875af1925050503d805f8114610e57576040519150601f19603f3d011682016040523d82523d5f602084013e610e5c565b606091505b5050905080610ea0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610e979061290c565b60405180910390fd5b505b8273ffffffffffffffffffffffffffffffffffffffff167fd8f920438b608c18ec52a9849fb52ba6f6bb47bf274f468d46f748340caefdb282604051610ee89190611ed9565b60405180910390a25050610f80565b5f610f01826118f8565b9050610f2f8260025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168361197d565b508173ffffffffffffffffffffffffffffffffffffffff167fd8f920438b608c18ec52a9849fb52ba6f6bb47bf274f468d46f748340caefdb282604051610f769190611ed9565b60405180910390a2505b610f88611a1e565b50565b335f73ffffffffffffffffffffffffffffffffffffffff1660055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff160361104d576040517f2163950f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b600a54341015611089576040517fd0c279a200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b854211156110c3576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8315611104576040517f329e2f150000000000000000000000000000000000000000000000000000000081526004016110fb90612974565b60405180910390fd5b5f61110e33611a38565b90505f61111f848684600a54611783565b90505f8290508073ffffffffffffffffffffffffffffffffffffffff1663436d4a6d5f73ffffffffffffffffffffffffffffffffffffffff168873ffffffffffffffffffffffffffffffffffffffff161461117a575f61117c565b835b8c8c428d8c898f6007546008546040518b63ffffffff1660e01b81526004016111ad999897969594939291906129a1565b60206040518083038185885af11580156111c9573d5f5f3e3d5ffd5b50505050506040513d601f19601f820116820180604052508101906111ee91906126e9565b50896040516111fd919061274e565b60405180910390207f4ad85a69286191006ea05981ae1848070dbf232e5e00ffdda4b857c16097f0d78388604051611236929190612a33565b60405180910390a250505050505050505050565b60035481565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146112d6576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b82600781905550816008819055505f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614611358578060015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b505050565b60095481565b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461140d576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161461148057805f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b50565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614611509576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8260098190555081600a8190555080600b819055505f81146115365780826115319190612a87565b611538565b5f5b600c81905550505050565b600c5481565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f815f60055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16148061163657503373ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614155b1561166d576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6116756118d6565b6116a0335f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1687611a9d565b92506116aa611a1e565b50509392505050565b60015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f60055f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60045481565b600a5481565b5f5f82905061177785858373ffffffffffffffffffffffffffffffffffffffff16611b969092919063ffffffff16565b60019150509392505050565b5f5f73ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16146118bc575f6117c3338688611a9d565b90508061180757846040517fa3adb6570000000000000000000000000000000000000000000000000000000081526004016117fe9190611f31565b60405180910390fd5b3073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f42856d0378dde02337bb59ae41747abc77ded8ebdbbc5cbdd1e53693b7554938886040516118649190611ed9565b60405180910390a3611877848787611747565b6118b6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016118ad9061266d565b60405180910390fd5b506118cb565b81346118c8919061284e565b94505b849050949350505050565b6118de611c48565b60026118f06118eb611c89565b611cb2565b5f0181905550565b5f5f8290508073ffffffffffffffffffffffffffffffffffffffff166370a08231306040518263ffffffff1660e01b81526004016119369190611f31565b602060405180830381865afa158015611951573d5f5f3e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061197591906126e9565b915050919050565b5f5f8490506119ad84848373ffffffffffffffffffffffffffffffffffffffff16611cbb9092919063ffffffff16565b600191508373ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff167f42856d0378dde02337bb59ae41747abc77ded8ebdbbc5cbdd1e53693b755493885604051611a0e9190611ed9565b60405180910390a3509392505050565b6001611a30611a2b611c89565b611cb2565b5f0181905550565b5f60055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050919050565b5f818373ffffffffffffffffffffffffffffffffffffffff1663dd62ed3e86306040518363ffffffff1660e01b8152600401611ada92919061249e565b602060405180830381865afa158015611af5573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611b1991906126e9565b10158390611b5d576040517fa3adb657000000000000000000000000000000000000000000000000000000008152600401611b549190611f31565b60405180910390fd5b50611b8b8430848673ffffffffffffffffffffffffffffffffffffffff16611d0e909392919063ffffffff16565b600190509392505050565b611ba28383835f611d63565b611c4357611bb383835f6001611d63565b611bf457826040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611beb9190611f31565b60405180910390fd5b611c018383836001611d63565b611c4257826040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611c399190611f31565b60405180910390fd5b5b505050565b611c50611dc5565b15611c87576040517f3ee5aeb500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b565b5f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005f1b905090565b5f819050919050565b611cc88383836001611de1565b611d0957826040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611d009190611f31565b60405180910390fd5b505050565b611d1c848484846001611e43565b611d5d57836040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611d549190611f31565b60405180910390fd5b50505050565b5f5f63095ea7b360e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611db7578383151615611dab573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f6002611dd8611dd3611c89565b611cb2565b5f015414905090565b5f5f63a9059cbb60e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611e35578383151615611e29573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f5f6323b872dd60e01b9050604051815f525f1960601c87166004525f1960601c86166024528460445260205f60645f5f8c5af1925060015f51148316611ea1578383151615611e95573d5f823e3d81fd5b5f883b113d1516831692505b806040525f606052505095945050505050565b612f7a80612ab883390190565b5f819050919050565b611ed381611ec1565b82525050565b5f602082019050611eec5f830184611eca565b92915050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f611f1b82611ef2565b9050919050565b611f2b81611f11565b82525050565b5f602082019050611f445f830184611f22565b92915050565b5f604051905090565b5f5ffd5b5f5ffd5b5f5ffd5b5f5ffd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b611fa982611f63565b810181811067ffffffffffffffff82111715611fc857611fc7611f73565b5b80604052505050565b5f611fda611f4a565b9050611fe68282611fa0565b919050565b5f67ffffffffffffffff82111561200557612004611f73565b5b61200e82611f63565b9050602081019050919050565b828183375f83830152505050565b5f61203b61203684611feb565b611fd1565b90508281526020810184848401111561205757612056611f5f565b5b61206284828561201b565b509392505050565b5f82601f83011261207e5761207d611f5b565b5b813561208e848260208601612029565b91505092915050565b6120a081611f11565b81146120aa575f5ffd5b50565b5f813590506120bb81612097565b92915050565b6120ca81611ec1565b81146120d4575f5ffd5b50565b5f813590506120e5816120c1565b92915050565b5f5f5f6060848603121561210257612101611f53565b5b5f84013567ffffffffffffffff81111561211f5761211e611f57565b5b61212b8682870161206a565b935050602061213c868287016120ad565b925050604061214d868287016120d7565b9150509250925092565b5f6020828403121561216c5761216b611f53565b5b5f82013567ffffffffffffffff81111561218957612188611f57565b5b6121958482850161206a565b91505092915050565b5f8115159050919050565b6121b28161219e565b82525050565b5f6020820190506121cb5f8301846121a9565b92915050565b5f602082840312156121e6576121e5611f53565b5b5f6121f3848285016120ad565b91505092915050565b5f60ff82169050919050565b612211816121fc565b811461221b575f5ffd5b50565b5f8135905061222c81612208565b92915050565b61223b8161219e565b8114612245575f5ffd5b50565b5f8135905061225681612232565b92915050565b5f5f5f5f5f5f60c0878903121561227657612275611f53565b5b5f87013567ffffffffffffffff81111561229357612292611f57565b5b61229f89828a0161206a565b96505060206122b089828a016120d7565b95505060406122c189828a0161221e565b94505060606122d289828a01612248565b93505060806122e389828a016120ad565b92505060a06122f489828a016120d7565b9150509295509295509295565b5f5f5f6060848603121561231857612317611f53565b5b5f612325868287016120d7565b9350506020612336868287016120d7565b9250506040612347868287016120ad565b9150509250925092565b5f819050919050565b5f61237461236f61236a84611ef2565b612351565b611ef2565b9050919050565b5f6123858261235a565b9050919050565b5f6123968261237b565b9050919050565b6123a68161238c565b82525050565b5f6020820190506123bf5f83018461239d565b92915050565b5f5f5f606084860312156123dc576123db611f53565b5b5f6123e9868287016120d7565b93505060206123fa868287016120d7565b925050604061240b868287016120d7565b9150509250925092565b5f61241f82611ef2565b9050919050565b61242f81612415565b82525050565b5f6020820190506124485f830184612426565b92915050565b5f5f5f6060848603121561246557612464611f53565b5b5f612472868287016120ad565b9350506020612483868287016120d7565b9250506040612494868287016120ad565b9150509250925092565b5f6040820190506124b15f830185611f22565b6124be6020830184611f22565b9392505050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f6124fc82611ec1565b915061250783611ec1565b925082820190508082111561251f5761251e6124c5565b5b92915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f61255782612525565b612561818561252f565b935061257181856020860161253f565b61257a81611f63565b840191505092915050565b5f6020820190508181035f83015261259d818461254d565b905092915050565b5f815190506125b381612097565b92915050565b5f602082840312156125ce576125cd611f53565b5b5f6125db848285016125a5565b91505092915050565b5f815190506125f281612232565b92915050565b5f6020828403121561260d5761260c611f53565b5b5f61261a848285016125e4565b91505092915050565b7f536176696e677320696e76616c696400000000000000000000000000000000005f82015250565b5f612657600f8361252f565b915061266282612623565b602082019050919050565b5f6020820190508181035f8301526126848161264b565b9050919050565b5f6080820190508181035f8301526126a3818761254d565b90506126b26020830186611eca565b6126bf6040830185611eca565b6126cc6060830184611eca565b95945050505050565b5f815190506126e3816120c1565b92915050565b5f602082840312156126fe576126fd611f53565b5b5f61270b848285016126d5565b91505092915050565b5f81905092915050565b5f61272882612525565b6127328185612714565b935061274281856020860161253f565b80840191505092915050565b5f612759828461271e565b915081905092915050565b5f6060820190506127775f830186611eca565b6127846020830185611eca565b6127916040830184611f22565b949350505050565b5f6127ab6127a684611feb565b611fd1565b9050828152602081018484840111156127c7576127c6611f5f565b5b6127d284828561253f565b509392505050565b5f82601f8301126127ee576127ed611f5b565b5b81516127fe848260208601612799565b91505092915050565b5f6020828403121561281c5761281b611f53565b5b5f82015167ffffffffffffffff81111561283957612838611f57565b5b612845848285016127da565b91505092915050565b5f61285882611ec1565b915061286383611ec1565b925082820390508181111561287b5761287a6124c5565b5b92915050565b5f81905092915050565b50565b5f6128995f83612881565b91506128a48261288b565b5f82019050919050565b5f6128b88261288e565b9150819050919050565b7f7472616e73666572206661696c656400000000000000000000000000000000005f82015250565b5f6128f6600f8361252f565b9150612901826128c2565b602082019050919050565b5f6020820190508181035f830152612923816128ea565b9050919050565b7f4e6f2073616665206d6f646520796574210000000000000000000000000000005f82015250565b5f61295e60118361252f565b91506129698261292a565b602082019050919050565b5f6020820190508181035f83015261298b81612952565b9050919050565b61299b816121fc565b82525050565b5f610120820190508181035f8301526129ba818c61254d565b90506129c9602083018b611eca565b6129d6604083018a611eca565b6129e36060830189612992565b6129f06080830188611f22565b6129fd60a0830187611eca565b612a0a60c08301866121a9565b612a1760e0830185611eca565b612a25610100830184611eca565b9a9950505050505050505050565b5f604082019050612a465f830185611eca565b612a536020830184611f22565b9392505050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b5f612a9182611ec1565b9150612a9c83611ec1565b925082612aac57612aab612a5a565b5b82820490509291505056fe6080604052604051612f7a380380612f7a833981810160405281019061002591906101a6565b600161004361003861011660201b60201c565b61013f60201b60201c565b5f0181905550335f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508160025f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505f60038190555050506101e4565b5f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005f1b905090565b5f819050919050565b5f5ffd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6101758261014c565b9050919050565b6101858161016b565b811461018f575f5ffd5b50565b5f815190506101a08161017c565b92915050565b5f5f604083850312156101bc576101bb610148565b5b5f6101c985828601610192565b92505060206101da85828601610192565b9150509250929050565b612d89806101f15f395ff3fe6080604052600436106100dc575f3560e01c806384febb691161007e578063d1ece79011610058578063d1ece790146102df578063dc2379b914610309578063e0ea6d4014610339578063e18f5f6514610375576100dc565b806384febb691461024f5780638f84aa091461028b578063992642e5146102b5576100dc565b8063372f9101116100ba578063372f910114610189578063436d4a6d146101b9578063567142be146101e9578063585dbffe14610213576100dc565b806313203929146100e05780631bb862a5146101235780632ae3c90f1461015f575b5f5ffd5b3480156100eb575f5ffd5b5061010660048036038101906101019190611f8a565b6103b1565b60405161011a989796959493929190612042565b60405180910390f35b34801561012e575f5ffd5b5061014960048036038101906101449190611f8a565b610444565b60405161015691906120be565b60405180910390f35b34801561016a575f5ffd5b5061017361047a565b60405161018091906120f7565b60405180910390f35b6101a3600480360381019061019e9190611f8a565b61049e565b6040516101b09190612170565b60405180910390f35b6101d360048036038101906101ce9190612244565b6109f5565b6040516101e09190612324565b60405180910390f35b3480156101f4575f5ffd5b506101fd610dd0565b60405161020a9190612324565b60405180910390f35b34801561021e575f5ffd5b5061023960048036038101906102349190611f8a565b610dd6565b604051610246919061240a565b60405180910390f35b34801561025a575f5ffd5b5061027560048036038101906102709190611f8a565b610ec9565b6040516102829190612324565b60405180910390f35b348015610296575f5ffd5b5061029f610ef3565b6040516102ac9190612424565b60405180910390f35b3480156102c0575f5ffd5b506102c9610f18565b6040516102d69190612498565b60405180910390f35b3480156102ea575f5ffd5b506102f3610f3d565b60405161030091906125db565b60405180910390f35b610323600480360381019061031e91906125fb565b61102a565b6040516103309190612324565b60405180910390f35b348015610344575f5ffd5b5061035f600480360381019061035a9190611f8a565b61149e565b60405161036c9190612324565b60405180910390f35b348015610380575f5ffd5b5061039b60048036038101906103969190611f8a565b6114c8565b6040516103a89190612424565b60405180910390f35b6004818051602081018201805184825260208301602085012081835280955050505050505f91509050805f015f9054906101000a900460ff1690806001015490806002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690806003015490806004015490806005015490806006015490806007015f9054906101000a900460ff16905088565b5f60048260405161045591906126b5565b90815260200160405180910390206007015f9054906101000a900460ff169050919050565b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60605f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610525576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b61052d611511565b5f60048360405161053e91906126b5565b90815260200160405180910390209050805f015f9054906101000a900460ff16610594576040517fd63d1e4800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f816001015490505f5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050826006015442101561065d576064836005015460646105df91906126f8565b84600101546105ee919061272b565b6105f89190612799565b9150610657836002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff165f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1684866001015461065291906126f8565b611533565b5061065e565b5b5f60048660405161066f91906126b5565b90815260200160405180910390205f015f6101000a81548160ff0219169083151502179055505f836002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f5f9050846007015f9054906101000a900460ff16156107c8576107225f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168560015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff166115d4565b508273ffffffffffffffffffffffffffffffffffffffff1663de35c372838660025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff166040518463ffffffff1660e01b8152600401610781939291906127c9565b6020604051808303815f875af115801561079d573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906107c19190612812565b9050610923565b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036108d0575f5f60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16866040516108429061286a565b5f6040518083038185875af1925050503d805f811461087c576040519150601f19603f3d011682016040523d82523d5f602084013e610881565b606091505b5091509150816108c6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108bd906128c8565b60405180910390fd5b8192505050610922565b61091f856002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1686611533565b90505b5b80156109e4575f60048860405161093a91906126b5565b90815260200160405180910390205f015f6101000a81548160ff0219169083151502179055508660405161096e91906126b5565b60405180910390207f5b059b7fe1fea38d4de595079621498a365a8f1fa13e49af9f1e4b7197ab049a60405160405180910390a26040518060400160405280601e81526020017f736176696e67732077697468647261776e207375636365737366756c6c790000815250955050505050506109e8565b5f5ffd5b6109f0611610565b919050565b5f5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610a7b576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b610a83611511565b60048a604051610a9391906126b5565b90815260200160405180910390205f015f9054906101000a900460ff1615610ae7576040517fd63d1e4800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b87891015610b21576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b42891015610b5b576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f8590508415610bb757610bb15f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168861162a565b50610c20565b5f73ffffffffffffffffffffffffffffffffffffffff168773ffffffffffffffffffffffffffffffffffffffff1614610c1b57610c155f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16888861162a565b50610c1f565b3490505b5b5f610c2e828c8c8888611723565b90506040518061010001604052806001151581526020018381526020018973ffffffffffffffffffffffffffffffffffffffff1681526020018281526020018b81526020018a60ff1681526020018c815260200187151581525060048d604051610c9891906126b5565b90815260200160405180910390205f820151815f015f6101000a81548160ff021916908315150217905550602082015181600101556040820151816002015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550606082015181600301556080820151816004015560a0820151816005015560c0820151816006015560e0820151816007015f6101000a81548160ff021916908315150217905550905050610d668c61175e565b8b604051610d7491906126b5565b60405180910390207f4ad85a69286191006ea05981ae1848070dbf232e5e00ffdda4b857c16097f0d7888a604051610dad9291906128e6565b60405180910390a2600192505050610dc3611610565b9998505050505050505050565b60035481565b610dde611dd3565b600482604051610dee91906126b5565b9081526020016040518091039020604051806101000160405290815f82015f9054906101000a900460ff1615151515815260200160018201548152602001600282015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200160038201548152602001600482015481526020016005820154815260200160068201548152602001600782015f9054906101000a900460ff1615151515815250509050919050565b5f600482604051610eda91906126b5565b9081526020016040518091039020600301549050919050565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b610f45611e2a565b60056040518060200160405290815f8201805480602002602001604051908101604052809291908181526020015f905b8282101561101d578382905f5260205f20018054610f929061293a565b80601f0160208091040260200160405190810160405280929190818152602001828054610fbe9061293a565b80156110095780601f10610fe057610100808354040283529160200191611009565b820191905f5260205f20905b815481529060010190602001808311610fec57829003601f168201915b505050505081526020019060010190610f75565b5050505081525050905090565b5f5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146110b0576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6110b8611511565b5f6004866040516110c991906126b5565b90815260200160405180910390209050805f015f9054906101000a900460ff1661111f576040517fd63d1e4800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b806006015442111561115d576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f5f73ffffffffffffffffffffffffffffffffffffffff16826002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16149050816007015f9054906101000a900460ff161561121a576112145f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168861162a565b506112bb565b806112735761126d5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16836002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168861162a565b506112ba565b853410156112b6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016112ad906129da565b60405180910390fd5b3495505b5b5f6112cd878460060154428989611723565b90508083600301546112df91906129f8565b83600301819055508683600101546112f791906129f8565b83600101819055508260048960405161131091906126b5565b90815260200160405180910390205f82015f9054906101000a900460ff16815f015f6101000a81548160ff02191690831515021790555060018201548160010155600282015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16816002015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060038201548160030155600482015481600401556005820154816005015560068201548160060155600782015f9054906101000a900460ff16816007015f6101000a81548160ff0219169083151502179055509050508760405161141a91906126b5565b60405180910390207ff6b98d8e0d83f8b1d1ff03a2fa36aba2c77ccf20e4ff035c0cf4aa92762e9eec888560010154866002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660405161147c93929190612a2b565b60405180910390a282600301549350505050611496611610565b949350505050565b5f6004826040516114af91906126b5565b9081526020016040518091039020600101549050919050565b5f6004826040516114d991906126b5565b90815260200160405180910390206002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050919050565b611519611795565b600261152b6115266117d6565b6117ff565b5f0181905550565b5f5f84905061156384848373ffffffffffffffffffffffffffffffffffffffff166118089092919063ffffffff16565b600191508373ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff167f42856d0378dde02337bb59ae41747abc77ded8ebdbbc5cbdd1e53693b7554938856040516115c49190612324565b60405180910390a3509392505050565b5f5f82905061160485858373ffffffffffffffffffffffffffffffffffffffff1661185b9092919063ffffffff16565b60019150509392505050565b600161162261161d6117d6565b6117ff565b5f0181905550565b5f818373ffffffffffffffffffffffffffffffffffffffff1663dd62ed3e86306040518363ffffffff1660e01b8152600401611667929190612a60565b602060405180830381865afa158015611682573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116a69190612a9b565b101583906116ea576040517fa3adb6570000000000000000000000000000000000000000000000000000000081526004016116e19190612424565b60405180910390fd5b506117188430848673ffffffffffffffffffffffffffffffffffffffff1661190d909392919063ffffffff16565b600190509392505050565b5f61173b86858761173491906126f8565b8585611962565b90508060035f82825461174e91906129f8565b9250508190555095945050505050565b60055f0181908060018154018082558091505060019003905f5260205f20015f9091909190915090816117919190612c5d565b5050565b61179d611a2c565b156117d4576040517f3ee5aeb500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b565b5f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005f1b905090565b5f819050919050565b6118158383836001611a48565b61185657826040517f5274afe700000000000000000000000000000000000000000000000000000000815260040161184d9190612424565b60405180910390fd5b505050565b6118678383835f611aaa565b6119085761187883835f6001611aaa565b6118b957826040517f5274afe70000000000000000000000000000000000000000000000000000000081526004016118b09190612424565b60405180910390fd5b6118c68383836001611aaa565b61190757826040517f5274afe70000000000000000000000000000000000000000000000000000000081526004016118fe9190612424565b60405180910390fd5b5b505050565b61191b848484846001611b0c565b61195c57836040517f5274afe70000000000000000000000000000000000000000000000000000000081526004016119539190612424565b60405180910390fd5b50505050565b5f5f611998606461198a868762e4e1c061197c91906126f8565b611b7d90919063ffffffff16565b611b9990919063ffffffff16565b90505f6119bd84836119aa919061272b565b6305f5e100611b7d90919063ffffffff16565b90505f6119d76301e1338088611b7d90919063ffffffff16565b9050611a1f611a1a683635c9adc5dea0000060646119f5919061272b565b83858c611a02919061272b565b611a0c919061272b565b611b7d90919063ffffffff16565b611bac565b9350505050949350505050565b5f6002611a3f611a3a6117d6565b6117ff565b5f015414905090565b5f5f63a9059cbb60e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611a9c578383151615611a90573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f5f63095ea7b360e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611afe578383151615611af2573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f5f6323b872dd60e01b9050604051815f525f1960601c87166004525f1960601c86166024528460445260205f60645f5f8c5af1925060015f51148316611b6a578383151615611b5e573d5f823e3d81fd5b5f883b113d1516831692505b806040525f606052505095945050505050565b5f611b9183670de0b6b3a764000084611bcd565b905092915050565b5f611ba48383611cdb565b905092915050565b5f670de0b6b3a76400008281611bc557611bc461276c565b5b049050919050565b5f5f5f5f198587098587029250828110838203039150505f8103611c0557838281611bfb57611bfa61276c565b5b0492505050611cd4565b838110611c4b5780846040517f773cc18c000000000000000000000000000000000000000000000000000000008152600401611c42929190612d2c565b60405180910390fd5b5f8486880990508281118203915080830392505f60018619018616905080860495508084049350600181825f0304019050808302841793505f600287600302189050808702600203810290508087026002038102905080870260020381029050808702600203810290508087026002038102905080870260020381029050808502955050505050505b9392505050565b5f5f5f5f19848609848602925082811083820303915050670de0b6b3a76400008110611d3e57806040517fd31b3402000000000000000000000000000000000000000000000000000000008152600401611d359190612324565b60405180910390fd5b5f5f670de0b6b3a764000086880991506706f05b59d3b1ffff821190505f8303611d885780670de0b6b3a76400008581611d7b57611d7a61276c565b5b0401945050505050611dcd565b807faccb18165bd6fe31ae1cf318dc5b51eee0e1ba569b88cd74c1773b91fac10669600162040000805f03040186851186030262040000858803041702019450505050505b92915050565b6040518061010001604052805f151581526020015f81526020015f73ffffffffffffffffffffffffffffffffffffffff1681526020015f81526020015f81526020015f81526020015f81526020015f151581525090565b6040518060200160405280606081525090565b5f604051905090565b5f5ffd5b5f5ffd5b5f5ffd5b5f5ffd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b611e9c82611e56565b810181811067ffffffffffffffff82111715611ebb57611eba611e66565b5b80604052505050565b5f611ecd611e3d565b9050611ed98282611e93565b919050565b5f67ffffffffffffffff821115611ef857611ef7611e66565b5b611f0182611e56565b9050602081019050919050565b828183375f83830152505050565b5f611f2e611f2984611ede565b611ec4565b905082815260208101848484011115611f4a57611f49611e52565b5b611f55848285611f0e565b509392505050565b5f82601f830112611f7157611f70611e4e565b5b8135611f81848260208601611f1c565b91505092915050565b5f60208284031215611f9f57611f9e611e46565b5b5f82013567ffffffffffffffff811115611fbc57611fbb611e4a565b5b611fc884828501611f5d565b91505092915050565b5f8115159050919050565b611fe581611fd1565b82525050565b5f819050919050565b611ffd81611feb565b82525050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61202c82612003565b9050919050565b61203c81612022565b82525050565b5f610100820190506120565f83018b611fdc565b612063602083018a611ff4565b6120706040830189612033565b61207d6060830188611ff4565b61208a6080830187611ff4565b61209760a0830186611ff4565b6120a460c0830185611ff4565b6120b160e0830184611fdc565b9998505050505050505050565b5f6020820190506120d15f830184611fdc565b92915050565b5f6120e182612003565b9050919050565b6120f1816120d7565b82525050565b5f60208201905061210a5f8301846120e8565b92915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f61214282612110565b61214c818561211a565b935061215c81856020860161212a565b61216581611e56565b840191505092915050565b5f6020820190508181035f8301526121888184612138565b905092915050565b61219981611feb565b81146121a3575f5ffd5b50565b5f813590506121b481612190565b92915050565b5f60ff82169050919050565b6121cf816121ba565b81146121d9575f5ffd5b50565b5f813590506121ea816121c6565b92915050565b6121f981612022565b8114612203575f5ffd5b50565b5f81359050612214816121f0565b92915050565b61222381611fd1565b811461222d575f5ffd5b50565b5f8135905061223e8161221a565b92915050565b5f5f5f5f5f5f5f5f5f6101208a8c03121561226257612261611e46565b5b5f8a013567ffffffffffffffff81111561227f5761227e611e4a565b5b61228b8c828d01611f5d565b995050602061229c8c828d016121a6565b98505060406122ad8c828d016121a6565b97505060606122be8c828d016121dc565b96505060806122cf8c828d01612206565b95505060a06122e08c828d016121a6565b94505060c06122f18c828d01612230565b93505060e06123028c828d016121a6565b9250506101006123148c828d016121a6565b9150509295985092959850929598565b5f6020820190506123375f830184611ff4565b92915050565b61234681611fd1565b82525050565b61235581611feb565b82525050565b61236481612022565b82525050565b61010082015f82015161237f5f85018261233d565b506020820151612392602085018261234c565b5060408201516123a5604085018261235b565b5060608201516123b8606085018261234c565b5060808201516123cb608085018261234c565b5060a08201516123de60a085018261234c565b5060c08201516123f160c085018261234c565b5060e082015161240460e085018261233d565b50505050565b5f6101008201905061241e5f83018461236a565b92915050565b5f6020820190506124375f830184612033565b92915050565b5f819050919050565b5f61246061245b61245684612003565b61243d565b612003565b9050919050565b5f61247182612446565b9050919050565b5f61248282612467565b9050919050565b61249281612478565b82525050565b5f6020820190506124ab5f830184612489565b92915050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f82825260208201905092915050565b5f6124f482612110565b6124fe81856124da565b935061250e81856020860161212a565b61251781611e56565b840191505092915050565b5f61252d83836124ea565b905092915050565b5f602082019050919050565b5f61254b826124b1565b61255581856124bb565b935083602082028501612567856124cb565b805f5b858110156125a257848403895281516125838582612522565b945061258e83612535565b925060208a0199505060018101905061256a565b50829750879550505050505092915050565b5f602083015f8301518482035f8601526125ce8282612541565b9150508091505092915050565b5f6020820190508181035f8301526125f381846125b4565b905092915050565b5f5f5f5f6080858703121561261357612612611e46565b5b5f85013567ffffffffffffffff8111156126305761262f611e4a565b5b61263c87828801611f5d565b945050602061264d878288016121a6565b935050604061265e878288016121a6565b925050606061266f878288016121a6565b91505092959194509250565b5f81905092915050565b5f61268f82612110565b612699818561267b565b93506126a981856020860161212a565b80840191505092915050565b5f6126c08284612685565b915081905092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61270282611feb565b915061270d83611feb565b9250828203905081811115612725576127246126cb565b5b92915050565b5f61273582611feb565b915061274083611feb565b925082820261274e81611feb565b91508282048414831517612765576127646126cb565b5b5092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b5f6127a382611feb565b91506127ae83611feb565b9250826127be576127bd61276c565b5b828204905092915050565b5f6060820190506127dc5f830186612033565b6127e96020830185611ff4565b6127f66040830184612033565b949350505050565b5f8151905061280c8161221a565b92915050565b5f6020828403121561282757612826611e46565b5b5f612834848285016127fe565b91505092915050565b5f81905092915050565b50565b5f6128555f8361283d565b915061286082612847565b5f82019050919050565b5f6128748261284a565b9150819050919050565b7f436f756c646e27742073656e642066756e6473000000000000000000000000005f82015250565b5f6128b260138361211a565b91506128bd8261287e565b602082019050919050565b5f6020820190508181035f8301526128df816128a6565b9050919050565b5f6040820190506128f95f830185611ff4565b6129066020830184612033565b9392505050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f600282049050600182168061295157607f821691505b6020821081036129645761296361290d565b5b50919050565b7f496e76616c696420736176696e6720696e6372656d656e742076616c756520735f8201527f656e740000000000000000000000000000000000000000000000000000000000602082015250565b5f6129c460238361211a565b91506129cf8261296a565b604082019050919050565b5f6020820190508181035f8301526129f1816129b8565b9050919050565b5f612a0282611feb565b9150612a0d83611feb565b9250828201905080821115612a2557612a246126cb565b5b92915050565b5f606082019050612a3e5f830186611ff4565b612a4b6020830185611ff4565b612a586040830184612033565b949350505050565b5f604082019050612a735f830185612033565b612a806020830184612033565b9392505050565b5f81519050612a9581612190565b92915050565b5f60208284031215612ab057612aaf611e46565b5b5f612abd84828501612a87565b91505092915050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302612b227fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82612ae7565b612b2c8683612ae7565b95508019841693508086168417925050509392505050565b5f612b5e612b59612b5484611feb565b61243d565b611feb565b9050919050565b5f819050919050565b612b7783612b44565b612b8b612b8382612b65565b848454612af3565b825550505050565b5f5f905090565b612ba2612b93565b612bad818484612b6e565b505050565b5b81811015612bd057612bc55f82612b9a565b600181019050612bb3565b5050565b601f821115612c1557612be681612ac6565b612bef84612ad8565b81016020851015612bfe578190505b612c12612c0a85612ad8565b830182612bb2565b50505b505050565b5f82821c905092915050565b5f612c355f1984600802612c1a565b1980831691505092915050565b5f612c4d8383612c26565b9150826002028217905092915050565b612c6682612110565b67ffffffffffffffff811115612c7f57612c7e611e66565b5b612c89825461293a565b612c94828285612bd4565b5f60209050601f831160018114612cc5575f8415612cb3578287015190505b612cbd8582612c42565b865550612d24565b601f198416612cd386612ac6565b5f5b82811015612cfa57848901518255600182019150602085019450602081019050612cd5565b86831015612d175784890151612d13601f891682612c26565b8355505b6001600288020188555050505b505050505050565b5f604082019050612d3f5f830185611ff4565b612d4c6020830184611ff4565b939250505056fea26469706673582212205cc4a3b0eb0f58c91f11b41812ce378bcb3389468526e399a18f1c4f01d919e064736f6c634300081b0033a26469706673582212207545be7eb4a4ecdb033a05673d8f587ca5c66d45c5014fe38f76fab4fb821e7364736f6c634300081b0033000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913
Deployed Bytecode
0x608060405260043610610138575f3560e01c806384762b0b116100aa578063d365a08e1161006e578063d365a08e146103c5578063de35c372146103ef578063e04605fd1461041f578063ea41367214610449578063f24053b314610473578063fed0b9251461049d5761013f565b806384762b0b146102f7578063992642e5146103215780639986e0e91461034b578063a63748e514610373578063bf66bfc71461039b5761013f565b8063372f9101116100fc578063372f9101146101fb5780634c3fa3041461023757806352f44e2e146102615780636405350a1461028957806366666aa9146102a55780637679489c146102cf5761013f565b8063025007391461014357806307973ccf1461016d57806309a5ff8c1461019757806316f94d5d146101c15780631fdc181f146101df5761013f565b3661013f57005b5f5ffd5b34801561014e575f5ffd5b506101576104c7565b6040516101649190611ed9565b60405180910390f35b348015610178575f5ffd5b506101816104cd565b60405161018e9190611ed9565b60405180910390f35b3480156101a2575f5ffd5b506101ab6104d3565b6040516101b89190611ed9565b60405180910390f35b6101c96104d9565b6040516101d69190611f31565b60405180910390f35b6101f960048036038101906101f491906120eb565b6106b2565b005b348015610206575f5ffd5b50610221600480360381019061021c9190612157565b610b01565b60405161022e91906121b8565b60405180910390f35b348015610242575f5ffd5b5061024b610cee565b6040516102589190611ed9565b60405180910390f35b34801561026c575f5ffd5b50610287600480360381019061028291906121d1565b610cf4565b005b6102a3600480360381019061029e919061225c565b610f8b565b005b3480156102b0575f5ffd5b506102b961124a565b6040516102c69190611ed9565b60405180910390f35b3480156102da575f5ffd5b506102f560048036038101906102f09190612301565b611250565b005b348015610302575f5ffd5b5061030b61135d565b6040516103189190611ed9565b60405180910390f35b34801561032c575f5ffd5b50610335611363565b60405161034291906123ac565b60405180910390f35b348015610356575f5ffd5b50610371600480360381019061036c91906121d1565b611387565b005b34801561037e575f5ffd5b50610399600480360381019061039491906123c5565b611483565b005b3480156103a6575f5ffd5b506103af611543565b6040516103bc9190611ed9565b60405180910390f35b3480156103d0575f5ffd5b506103d9611549565b6040516103e69190612435565b60405180910390f35b6104096004803603810190610404919061244e565b61156e565b60405161041691906121b8565b60405180910390f35b34801561042a575f5ffd5b506104336116b3565b60405161044091906123ac565b60405180910390f35b348015610454575f5ffd5b5061045d6116d8565b60405161046a9190611f31565b60405180910390f35b34801561047e575f5ffd5b5061048761173b565b6040516104949190611ed9565b60405180910390f35b3480156104a8575f5ffd5b506104b1611741565b6040516104be9190611ed9565b60405180910390f35b60085481565b60065481565b600b5481565b5f5f3390505f60055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161461057b5780925050506106af565b5f335f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff166040516105aa90611eb4565b6105b592919061249e565b604051809103905ff0801580156105ce573d5f5f3e3d5ffd5b5090508060055f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600160065f82825461065e91906124f2565b925050819055508273ffffffffffffffffffffffffffffffffffffffff167f0c5c0c4d53b63790e32770cf47fd115d706000fd7ab63fbdc8d176c5020d6b4660405160405180910390a28093505050505b90565b335f73ffffffffffffffffffffffffffffffffffffffff1660055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1603610774576040517f2163950f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f60055f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f8190505f8173ffffffffffffffffffffffffffffffffffffffff1663e18f5f65886040518263ffffffff1660e01b81526004016108129190612585565b602060405180830381865afa15801561082d573d5f5f3e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061085191906125b9565b90505f5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161490505f8690505f8473ffffffffffffffffffffffffffffffffffffffff16631bb862a58b6040518263ffffffff1660e01b81526004016108c39190612585565b602060405180830381865afa1580156108de573d5f5f3e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061090291906125f8565b9050801561092e575f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1698505b826109825761093e86838b611747565b61097d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016109749061266d565b60405180910390fd5b610986565b3491505b5f6109938986895f611783565b90508573ffffffffffffffffffffffffffffffffffffffff1663dc2379b9856109bc575f6109be565b825b8d846007546008546040518663ffffffff1660e01b81526004016109e5949392919061268b565b60206040518083038185885af1158015610a01573d5f5f3e3d5ffd5b50505050506040513d601f19601f82011682018060405250810190610a2691906126e9565b505f8673ffffffffffffffffffffffffffffffffffffffff1663e0ea6d408d6040518263ffffffff1660e01b8152600401610a619190612585565b602060405180830381865afa158015610a7c573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610aa091906126e9565b90508b604051610ab0919061274e565b60405180910390207ff6b98d8e0d83f8b1d1ff03a2fa36aba2c77ccf20e4ff035c0cf4aa92762e9eec8b838e604051610aeb93929190612764565b60405180910390a2505050505050505050505050565b5f335f73ffffffffffffffffffffffffffffffffffffffff1660055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1603610bc4576040517f2163950f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f60055f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690508073ffffffffffffffffffffffffffffffffffffffff1663372f9101856040518263ffffffff1660e01b8152600401610c5d9190612585565b5f604051808303815f875af1158015610c78573d5f5f3e3d5ffd5b505050506040513d5f823e3d601f19601f82011682018060405250810190610ca09190612807565b5083604051610caf919061274e565b60405180910390207f5b059b7fe1fea38d4de595079621498a365a8f1fa13e49af9f1e4b7197ab049a60405160405180910390a2600192505050919050565b60075481565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610d7a576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b610d826118d6565b5f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603610ef7575f4790505f5f9050600454821115610ea25760045482610dd5919061284e565b90505f60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1682604051610e1d906128ae565b5f6040518083038185875af1925050503d805f8114610e57576040519150601f19603f3d011682016040523d82523d5f602084013e610e5c565b606091505b5050905080610ea0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610e979061290c565b60405180910390fd5b505b8273ffffffffffffffffffffffffffffffffffffffff167fd8f920438b608c18ec52a9849fb52ba6f6bb47bf274f468d46f748340caefdb282604051610ee89190611ed9565b60405180910390a25050610f80565b5f610f01826118f8565b9050610f2f8260025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168361197d565b508173ffffffffffffffffffffffffffffffffffffffff167fd8f920438b608c18ec52a9849fb52ba6f6bb47bf274f468d46f748340caefdb282604051610f769190611ed9565b60405180910390a2505b610f88611a1e565b50565b335f73ffffffffffffffffffffffffffffffffffffffff1660055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff160361104d576040517f2163950f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b600a54341015611089576040517fd0c279a200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b854211156110c3576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8315611104576040517f329e2f150000000000000000000000000000000000000000000000000000000081526004016110fb90612974565b60405180910390fd5b5f61110e33611a38565b90505f61111f848684600a54611783565b90505f8290508073ffffffffffffffffffffffffffffffffffffffff1663436d4a6d5f73ffffffffffffffffffffffffffffffffffffffff168873ffffffffffffffffffffffffffffffffffffffff161461117a575f61117c565b835b8c8c428d8c898f6007546008546040518b63ffffffff1660e01b81526004016111ad999897969594939291906129a1565b60206040518083038185885af11580156111c9573d5f5f3e3d5ffd5b50505050506040513d601f19601f820116820180604052508101906111ee91906126e9565b50896040516111fd919061274e565b60405180910390207f4ad85a69286191006ea05981ae1848070dbf232e5e00ffdda4b857c16097f0d78388604051611236929190612a33565b60405180910390a250505050505050505050565b60035481565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146112d6576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b82600781905550816008819055505f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614611358578060015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b505050565b60095481565b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461140d576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161461148057805f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b50565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614611509576040517f33662b3400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8260098190555081600a8190555080600b819055505f81146115365780826115319190612a87565b611538565b5f5b600c81905550505050565b600c5481565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f815f60055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16148061163657503373ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614155b1561166d576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6116756118d6565b6116a0335f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1687611a9d565b92506116aa611a1e565b50509392505050565b60015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f60055f3373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60045481565b600a5481565b5f5f82905061177785858373ffffffffffffffffffffffffffffffffffffffff16611b969092919063ffffffff16565b60019150509392505050565b5f5f73ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16146118bc575f6117c3338688611a9d565b90508061180757846040517fa3adb6570000000000000000000000000000000000000000000000000000000081526004016117fe9190611f31565b60405180910390fd5b3073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f42856d0378dde02337bb59ae41747abc77ded8ebdbbc5cbdd1e53693b7554938886040516118649190611ed9565b60405180910390a3611877848787611747565b6118b6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016118ad9061266d565b60405180910390fd5b506118cb565b81346118c8919061284e565b94505b849050949350505050565b6118de611c48565b60026118f06118eb611c89565b611cb2565b5f0181905550565b5f5f8290508073ffffffffffffffffffffffffffffffffffffffff166370a08231306040518263ffffffff1660e01b81526004016119369190611f31565b602060405180830381865afa158015611951573d5f5f3e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061197591906126e9565b915050919050565b5f5f8490506119ad84848373ffffffffffffffffffffffffffffffffffffffff16611cbb9092919063ffffffff16565b600191508373ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff167f42856d0378dde02337bb59ae41747abc77ded8ebdbbc5cbdd1e53693b755493885604051611a0e9190611ed9565b60405180910390a3509392505050565b6001611a30611a2b611c89565b611cb2565b5f0181905550565b5f60055f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050919050565b5f818373ffffffffffffffffffffffffffffffffffffffff1663dd62ed3e86306040518363ffffffff1660e01b8152600401611ada92919061249e565b602060405180830381865afa158015611af5573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611b1991906126e9565b10158390611b5d576040517fa3adb657000000000000000000000000000000000000000000000000000000008152600401611b549190611f31565b60405180910390fd5b50611b8b8430848673ffffffffffffffffffffffffffffffffffffffff16611d0e909392919063ffffffff16565b600190509392505050565b611ba28383835f611d63565b611c4357611bb383835f6001611d63565b611bf457826040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611beb9190611f31565b60405180910390fd5b611c018383836001611d63565b611c4257826040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611c399190611f31565b60405180910390fd5b5b505050565b611c50611dc5565b15611c87576040517f3ee5aeb500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b565b5f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005f1b905090565b5f819050919050565b611cc88383836001611de1565b611d0957826040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611d009190611f31565b60405180910390fd5b505050565b611d1c848484846001611e43565b611d5d57836040517f5274afe7000000000000000000000000000000000000000000000000000000008152600401611d549190611f31565b60405180910390fd5b50505050565b5f5f63095ea7b360e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611db7578383151615611dab573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f6002611dd8611dd3611c89565b611cb2565b5f015414905090565b5f5f63a9059cbb60e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611e35578383151615611e29573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f5f6323b872dd60e01b9050604051815f525f1960601c87166004525f1960601c86166024528460445260205f60645f5f8c5af1925060015f51148316611ea1578383151615611e95573d5f823e3d81fd5b5f883b113d1516831692505b806040525f606052505095945050505050565b612f7a80612ab883390190565b5f819050919050565b611ed381611ec1565b82525050565b5f602082019050611eec5f830184611eca565b92915050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f611f1b82611ef2565b9050919050565b611f2b81611f11565b82525050565b5f602082019050611f445f830184611f22565b92915050565b5f604051905090565b5f5ffd5b5f5ffd5b5f5ffd5b5f5ffd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b611fa982611f63565b810181811067ffffffffffffffff82111715611fc857611fc7611f73565b5b80604052505050565b5f611fda611f4a565b9050611fe68282611fa0565b919050565b5f67ffffffffffffffff82111561200557612004611f73565b5b61200e82611f63565b9050602081019050919050565b828183375f83830152505050565b5f61203b61203684611feb565b611fd1565b90508281526020810184848401111561205757612056611f5f565b5b61206284828561201b565b509392505050565b5f82601f83011261207e5761207d611f5b565b5b813561208e848260208601612029565b91505092915050565b6120a081611f11565b81146120aa575f5ffd5b50565b5f813590506120bb81612097565b92915050565b6120ca81611ec1565b81146120d4575f5ffd5b50565b5f813590506120e5816120c1565b92915050565b5f5f5f6060848603121561210257612101611f53565b5b5f84013567ffffffffffffffff81111561211f5761211e611f57565b5b61212b8682870161206a565b935050602061213c868287016120ad565b925050604061214d868287016120d7565b9150509250925092565b5f6020828403121561216c5761216b611f53565b5b5f82013567ffffffffffffffff81111561218957612188611f57565b5b6121958482850161206a565b91505092915050565b5f8115159050919050565b6121b28161219e565b82525050565b5f6020820190506121cb5f8301846121a9565b92915050565b5f602082840312156121e6576121e5611f53565b5b5f6121f3848285016120ad565b91505092915050565b5f60ff82169050919050565b612211816121fc565b811461221b575f5ffd5b50565b5f8135905061222c81612208565b92915050565b61223b8161219e565b8114612245575f5ffd5b50565b5f8135905061225681612232565b92915050565b5f5f5f5f5f5f60c0878903121561227657612275611f53565b5b5f87013567ffffffffffffffff81111561229357612292611f57565b5b61229f89828a0161206a565b96505060206122b089828a016120d7565b95505060406122c189828a0161221e565b94505060606122d289828a01612248565b93505060806122e389828a016120ad565b92505060a06122f489828a016120d7565b9150509295509295509295565b5f5f5f6060848603121561231857612317611f53565b5b5f612325868287016120d7565b9350506020612336868287016120d7565b9250506040612347868287016120ad565b9150509250925092565b5f819050919050565b5f61237461236f61236a84611ef2565b612351565b611ef2565b9050919050565b5f6123858261235a565b9050919050565b5f6123968261237b565b9050919050565b6123a68161238c565b82525050565b5f6020820190506123bf5f83018461239d565b92915050565b5f5f5f606084860312156123dc576123db611f53565b5b5f6123e9868287016120d7565b93505060206123fa868287016120d7565b925050604061240b868287016120d7565b9150509250925092565b5f61241f82611ef2565b9050919050565b61242f81612415565b82525050565b5f6020820190506124485f830184612426565b92915050565b5f5f5f6060848603121561246557612464611f53565b5b5f612472868287016120ad565b9350506020612483868287016120d7565b9250506040612494868287016120ad565b9150509250925092565b5f6040820190506124b15f830185611f22565b6124be6020830184611f22565b9392505050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f6124fc82611ec1565b915061250783611ec1565b925082820190508082111561251f5761251e6124c5565b5b92915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f61255782612525565b612561818561252f565b935061257181856020860161253f565b61257a81611f63565b840191505092915050565b5f6020820190508181035f83015261259d818461254d565b905092915050565b5f815190506125b381612097565b92915050565b5f602082840312156125ce576125cd611f53565b5b5f6125db848285016125a5565b91505092915050565b5f815190506125f281612232565b92915050565b5f6020828403121561260d5761260c611f53565b5b5f61261a848285016125e4565b91505092915050565b7f536176696e677320696e76616c696400000000000000000000000000000000005f82015250565b5f612657600f8361252f565b915061266282612623565b602082019050919050565b5f6020820190508181035f8301526126848161264b565b9050919050565b5f6080820190508181035f8301526126a3818761254d565b90506126b26020830186611eca565b6126bf6040830185611eca565b6126cc6060830184611eca565b95945050505050565b5f815190506126e3816120c1565b92915050565b5f602082840312156126fe576126fd611f53565b5b5f61270b848285016126d5565b91505092915050565b5f81905092915050565b5f61272882612525565b6127328185612714565b935061274281856020860161253f565b80840191505092915050565b5f612759828461271e565b915081905092915050565b5f6060820190506127775f830186611eca565b6127846020830185611eca565b6127916040830184611f22565b949350505050565b5f6127ab6127a684611feb565b611fd1565b9050828152602081018484840111156127c7576127c6611f5f565b5b6127d284828561253f565b509392505050565b5f82601f8301126127ee576127ed611f5b565b5b81516127fe848260208601612799565b91505092915050565b5f6020828403121561281c5761281b611f53565b5b5f82015167ffffffffffffffff81111561283957612838611f57565b5b612845848285016127da565b91505092915050565b5f61285882611ec1565b915061286383611ec1565b925082820390508181111561287b5761287a6124c5565b5b92915050565b5f81905092915050565b50565b5f6128995f83612881565b91506128a48261288b565b5f82019050919050565b5f6128b88261288e565b9150819050919050565b7f7472616e73666572206661696c656400000000000000000000000000000000005f82015250565b5f6128f6600f8361252f565b9150612901826128c2565b602082019050919050565b5f6020820190508181035f830152612923816128ea565b9050919050565b7f4e6f2073616665206d6f646520796574210000000000000000000000000000005f82015250565b5f61295e60118361252f565b91506129698261292a565b602082019050919050565b5f6020820190508181035f83015261298b81612952565b9050919050565b61299b816121fc565b82525050565b5f610120820190508181035f8301526129ba818c61254d565b90506129c9602083018b611eca565b6129d6604083018a611eca565b6129e36060830189612992565b6129f06080830188611f22565b6129fd60a0830187611eca565b612a0a60c08301866121a9565b612a1760e0830185611eca565b612a25610100830184611eca565b9a9950505050505050505050565b5f604082019050612a465f830185611eca565b612a536020830184611f22565b9392505050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b5f612a9182611ec1565b9150612a9c83611ec1565b925082612aac57612aab612a5a565b5b82820490509291505056fe6080604052604051612f7a380380612f7a833981810160405281019061002591906101a6565b600161004361003861011660201b60201c565b61013f60201b60201c565b5f0181905550335f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508160025f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505f60038190555050506101e4565b5f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005f1b905090565b5f819050919050565b5f5ffd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6101758261014c565b9050919050565b6101858161016b565b811461018f575f5ffd5b50565b5f815190506101a08161017c565b92915050565b5f5f604083850312156101bc576101bb610148565b5b5f6101c985828601610192565b92505060206101da85828601610192565b9150509250929050565b612d89806101f15f395ff3fe6080604052600436106100dc575f3560e01c806384febb691161007e578063d1ece79011610058578063d1ece790146102df578063dc2379b914610309578063e0ea6d4014610339578063e18f5f6514610375576100dc565b806384febb691461024f5780638f84aa091461028b578063992642e5146102b5576100dc565b8063372f9101116100ba578063372f910114610189578063436d4a6d146101b9578063567142be146101e9578063585dbffe14610213576100dc565b806313203929146100e05780631bb862a5146101235780632ae3c90f1461015f575b5f5ffd5b3480156100eb575f5ffd5b5061010660048036038101906101019190611f8a565b6103b1565b60405161011a989796959493929190612042565b60405180910390f35b34801561012e575f5ffd5b5061014960048036038101906101449190611f8a565b610444565b60405161015691906120be565b60405180910390f35b34801561016a575f5ffd5b5061017361047a565b60405161018091906120f7565b60405180910390f35b6101a3600480360381019061019e9190611f8a565b61049e565b6040516101b09190612170565b60405180910390f35b6101d360048036038101906101ce9190612244565b6109f5565b6040516101e09190612324565b60405180910390f35b3480156101f4575f5ffd5b506101fd610dd0565b60405161020a9190612324565b60405180910390f35b34801561021e575f5ffd5b5061023960048036038101906102349190611f8a565b610dd6565b604051610246919061240a565b60405180910390f35b34801561025a575f5ffd5b5061027560048036038101906102709190611f8a565b610ec9565b6040516102829190612324565b60405180910390f35b348015610296575f5ffd5b5061029f610ef3565b6040516102ac9190612424565b60405180910390f35b3480156102c0575f5ffd5b506102c9610f18565b6040516102d69190612498565b60405180910390f35b3480156102ea575f5ffd5b506102f3610f3d565b60405161030091906125db565b60405180910390f35b610323600480360381019061031e91906125fb565b61102a565b6040516103309190612324565b60405180910390f35b348015610344575f5ffd5b5061035f600480360381019061035a9190611f8a565b61149e565b60405161036c9190612324565b60405180910390f35b348015610380575f5ffd5b5061039b60048036038101906103969190611f8a565b6114c8565b6040516103a89190612424565b60405180910390f35b6004818051602081018201805184825260208301602085012081835280955050505050505f91509050805f015f9054906101000a900460ff1690806001015490806002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690806003015490806004015490806005015490806006015490806007015f9054906101000a900460ff16905088565b5f60048260405161045591906126b5565b90815260200160405180910390206007015f9054906101000a900460ff169050919050565b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60605f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610525576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b61052d611511565b5f60048360405161053e91906126b5565b90815260200160405180910390209050805f015f9054906101000a900460ff16610594576040517fd63d1e4800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f816001015490505f5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050826006015442101561065d576064836005015460646105df91906126f8565b84600101546105ee919061272b565b6105f89190612799565b9150610657836002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff165f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1684866001015461065291906126f8565b611533565b5061065e565b5b5f60048660405161066f91906126b5565b90815260200160405180910390205f015f6101000a81548160ff0219169083151502179055505f836002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505f5f9050846007015f9054906101000a900460ff16156107c8576107225f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168560015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff166115d4565b508273ffffffffffffffffffffffffffffffffffffffff1663de35c372838660025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff166040518463ffffffff1660e01b8152600401610781939291906127c9565b6020604051808303815f875af115801561079d573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906107c19190612812565b9050610923565b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036108d0575f5f60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16866040516108429061286a565b5f6040518083038185875af1925050503d805f811461087c576040519150601f19603f3d011682016040523d82523d5f602084013e610881565b606091505b5091509150816108c6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108bd906128c8565b60405180910390fd5b8192505050610922565b61091f856002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1686611533565b90505b5b80156109e4575f60048860405161093a91906126b5565b90815260200160405180910390205f015f6101000a81548160ff0219169083151502179055508660405161096e91906126b5565b60405180910390207f5b059b7fe1fea38d4de595079621498a365a8f1fa13e49af9f1e4b7197ab049a60405160405180910390a26040518060400160405280601e81526020017f736176696e67732077697468647261776e207375636365737366756c6c790000815250955050505050506109e8565b5f5ffd5b6109f0611610565b919050565b5f5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610a7b576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b610a83611511565b60048a604051610a9391906126b5565b90815260200160405180910390205f015f9054906101000a900460ff1615610ae7576040517fd63d1e4800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b87891015610b21576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b42891015610b5b576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f8590508415610bb757610bb15f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168861162a565b50610c20565b5f73ffffffffffffffffffffffffffffffffffffffff168773ffffffffffffffffffffffffffffffffffffffff1614610c1b57610c155f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16888861162a565b50610c1f565b3490505b5b5f610c2e828c8c8888611723565b90506040518061010001604052806001151581526020018381526020018973ffffffffffffffffffffffffffffffffffffffff1681526020018281526020018b81526020018a60ff1681526020018c815260200187151581525060048d604051610c9891906126b5565b90815260200160405180910390205f820151815f015f6101000a81548160ff021916908315150217905550602082015181600101556040820151816002015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550606082015181600301556080820151816004015560a0820151816005015560c0820151816006015560e0820151816007015f6101000a81548160ff021916908315150217905550905050610d668c61175e565b8b604051610d7491906126b5565b60405180910390207f4ad85a69286191006ea05981ae1848070dbf232e5e00ffdda4b857c16097f0d7888a604051610dad9291906128e6565b60405180910390a2600192505050610dc3611610565b9998505050505050505050565b60035481565b610dde611dd3565b600482604051610dee91906126b5565b9081526020016040518091039020604051806101000160405290815f82015f9054906101000a900460ff1615151515815260200160018201548152602001600282015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200160038201548152602001600482015481526020016005820154815260200160068201548152602001600782015f9054906101000a900460ff1615151515815250509050919050565b5f600482604051610eda91906126b5565b9081526020016040518091039020600301549050919050565b60025f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b610f45611e2a565b60056040518060200160405290815f8201805480602002602001604051908101604052809291908181526020015f905b8282101561101d578382905f5260205f20018054610f929061293a565b80601f0160208091040260200160405190810160405280929190818152602001828054610fbe9061293a565b80156110095780601f10610fe057610100808354040283529160200191611009565b820191905f5260205f20905b815481529060010190602001808311610fec57829003601f168201915b505050505081526020019060010190610f75565b5050505081525050905090565b5f5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146110b0576040517f229e1dbd00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6110b8611511565b5f6004866040516110c991906126b5565b90815260200160405180910390209050805f015f9054906101000a900460ff1661111f576040517fd63d1e4800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b806006015442111561115d576040517f6f7eac2600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f5f73ffffffffffffffffffffffffffffffffffffffff16826002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16149050816007015f9054906101000a900460ff161561121a576112145f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168861162a565b506112bb565b806112735761126d5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16836002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff168861162a565b506112ba565b853410156112b6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016112ad906129da565b60405180910390fd5b3495505b5b5f6112cd878460060154428989611723565b90508083600301546112df91906129f8565b83600301819055508683600101546112f791906129f8565b83600101819055508260048960405161131091906126b5565b90815260200160405180910390205f82015f9054906101000a900460ff16815f015f6101000a81548160ff02191690831515021790555060018201548160010155600282015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16816002015f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060038201548160030155600482015481600401556005820154816005015560068201548160060155600782015f9054906101000a900460ff16816007015f6101000a81548160ff0219169083151502179055509050508760405161141a91906126b5565b60405180910390207ff6b98d8e0d83f8b1d1ff03a2fa36aba2c77ccf20e4ff035c0cf4aa92762e9eec888560010154866002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1660405161147c93929190612a2b565b60405180910390a282600301549350505050611496611610565b949350505050565b5f6004826040516114af91906126b5565b9081526020016040518091039020600101549050919050565b5f6004826040516114d991906126b5565b90815260200160405180910390206002015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050919050565b611519611795565b600261152b6115266117d6565b6117ff565b5f0181905550565b5f5f84905061156384848373ffffffffffffffffffffffffffffffffffffffff166118089092919063ffffffff16565b600191508373ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff167f42856d0378dde02337bb59ae41747abc77ded8ebdbbc5cbdd1e53693b7554938856040516115c49190612324565b60405180910390a3509392505050565b5f5f82905061160485858373ffffffffffffffffffffffffffffffffffffffff1661185b9092919063ffffffff16565b60019150509392505050565b600161162261161d6117d6565b6117ff565b5f0181905550565b5f818373ffffffffffffffffffffffffffffffffffffffff1663dd62ed3e86306040518363ffffffff1660e01b8152600401611667929190612a60565b602060405180830381865afa158015611682573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116a69190612a9b565b101583906116ea576040517fa3adb6570000000000000000000000000000000000000000000000000000000081526004016116e19190612424565b60405180910390fd5b506117188430848673ffffffffffffffffffffffffffffffffffffffff1661190d909392919063ffffffff16565b600190509392505050565b5f61173b86858761173491906126f8565b8585611962565b90508060035f82825461174e91906129f8565b9250508190555095945050505050565b60055f0181908060018154018082558091505060019003905f5260205f20015f9091909190915090816117919190612c5d565b5050565b61179d611a2c565b156117d4576040517f3ee5aeb500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b565b5f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005f1b905090565b5f819050919050565b6118158383836001611a48565b61185657826040517f5274afe700000000000000000000000000000000000000000000000000000000815260040161184d9190612424565b60405180910390fd5b505050565b6118678383835f611aaa565b6119085761187883835f6001611aaa565b6118b957826040517f5274afe70000000000000000000000000000000000000000000000000000000081526004016118b09190612424565b60405180910390fd5b6118c68383836001611aaa565b61190757826040517f5274afe70000000000000000000000000000000000000000000000000000000081526004016118fe9190612424565b60405180910390fd5b5b505050565b61191b848484846001611b0c565b61195c57836040517f5274afe70000000000000000000000000000000000000000000000000000000081526004016119539190612424565b60405180910390fd5b50505050565b5f5f611998606461198a868762e4e1c061197c91906126f8565b611b7d90919063ffffffff16565b611b9990919063ffffffff16565b90505f6119bd84836119aa919061272b565b6305f5e100611b7d90919063ffffffff16565b90505f6119d76301e1338088611b7d90919063ffffffff16565b9050611a1f611a1a683635c9adc5dea0000060646119f5919061272b565b83858c611a02919061272b565b611a0c919061272b565b611b7d90919063ffffffff16565b611bac565b9350505050949350505050565b5f6002611a3f611a3a6117d6565b6117ff565b5f015414905090565b5f5f63a9059cbb60e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611a9c578383151615611a90573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f5f63095ea7b360e01b9050604051815f525f1960601c86166004528460245260205f60445f5f8b5af1925060015f51148316611afe578383151615611af2573d5f823e3d81fd5b5f873b113d1516831692505b806040525050949350505050565b5f5f6323b872dd60e01b9050604051815f525f1960601c87166004525f1960601c86166024528460445260205f60645f5f8c5af1925060015f51148316611b6a578383151615611b5e573d5f823e3d81fd5b5f883b113d1516831692505b806040525f606052505095945050505050565b5f611b9183670de0b6b3a764000084611bcd565b905092915050565b5f611ba48383611cdb565b905092915050565b5f670de0b6b3a76400008281611bc557611bc461276c565b5b049050919050565b5f5f5f5f198587098587029250828110838203039150505f8103611c0557838281611bfb57611bfa61276c565b5b0492505050611cd4565b838110611c4b5780846040517f773cc18c000000000000000000000000000000000000000000000000000000008152600401611c42929190612d2c565b60405180910390fd5b5f8486880990508281118203915080830392505f60018619018616905080860495508084049350600181825f0304019050808302841793505f600287600302189050808702600203810290508087026002038102905080870260020381029050808702600203810290508087026002038102905080870260020381029050808502955050505050505b9392505050565b5f5f5f5f19848609848602925082811083820303915050670de0b6b3a76400008110611d3e57806040517fd31b3402000000000000000000000000000000000000000000000000000000008152600401611d359190612324565b60405180910390fd5b5f5f670de0b6b3a764000086880991506706f05b59d3b1ffff821190505f8303611d885780670de0b6b3a76400008581611d7b57611d7a61276c565b5b0401945050505050611dcd565b807faccb18165bd6fe31ae1cf318dc5b51eee0e1ba569b88cd74c1773b91fac10669600162040000805f03040186851186030262040000858803041702019450505050505b92915050565b6040518061010001604052805f151581526020015f81526020015f73ffffffffffffffffffffffffffffffffffffffff1681526020015f81526020015f81526020015f81526020015f81526020015f151581525090565b6040518060200160405280606081525090565b5f604051905090565b5f5ffd5b5f5ffd5b5f5ffd5b5f5ffd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b611e9c82611e56565b810181811067ffffffffffffffff82111715611ebb57611eba611e66565b5b80604052505050565b5f611ecd611e3d565b9050611ed98282611e93565b919050565b5f67ffffffffffffffff821115611ef857611ef7611e66565b5b611f0182611e56565b9050602081019050919050565b828183375f83830152505050565b5f611f2e611f2984611ede565b611ec4565b905082815260208101848484011115611f4a57611f49611e52565b5b611f55848285611f0e565b509392505050565b5f82601f830112611f7157611f70611e4e565b5b8135611f81848260208601611f1c565b91505092915050565b5f60208284031215611f9f57611f9e611e46565b5b5f82013567ffffffffffffffff811115611fbc57611fbb611e4a565b5b611fc884828501611f5d565b91505092915050565b5f8115159050919050565b611fe581611fd1565b82525050565b5f819050919050565b611ffd81611feb565b82525050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61202c82612003565b9050919050565b61203c81612022565b82525050565b5f610100820190506120565f83018b611fdc565b612063602083018a611ff4565b6120706040830189612033565b61207d6060830188611ff4565b61208a6080830187611ff4565b61209760a0830186611ff4565b6120a460c0830185611ff4565b6120b160e0830184611fdc565b9998505050505050505050565b5f6020820190506120d15f830184611fdc565b92915050565b5f6120e182612003565b9050919050565b6120f1816120d7565b82525050565b5f60208201905061210a5f8301846120e8565b92915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f61214282612110565b61214c818561211a565b935061215c81856020860161212a565b61216581611e56565b840191505092915050565b5f6020820190508181035f8301526121888184612138565b905092915050565b61219981611feb565b81146121a3575f5ffd5b50565b5f813590506121b481612190565b92915050565b5f60ff82169050919050565b6121cf816121ba565b81146121d9575f5ffd5b50565b5f813590506121ea816121c6565b92915050565b6121f981612022565b8114612203575f5ffd5b50565b5f81359050612214816121f0565b92915050565b61222381611fd1565b811461222d575f5ffd5b50565b5f8135905061223e8161221a565b92915050565b5f5f5f5f5f5f5f5f5f6101208a8c03121561226257612261611e46565b5b5f8a013567ffffffffffffffff81111561227f5761227e611e4a565b5b61228b8c828d01611f5d565b995050602061229c8c828d016121a6565b98505060406122ad8c828d016121a6565b97505060606122be8c828d016121dc565b96505060806122cf8c828d01612206565b95505060a06122e08c828d016121a6565b94505060c06122f18c828d01612230565b93505060e06123028c828d016121a6565b9250506101006123148c828d016121a6565b9150509295985092959850929598565b5f6020820190506123375f830184611ff4565b92915050565b61234681611fd1565b82525050565b61235581611feb565b82525050565b61236481612022565b82525050565b61010082015f82015161237f5f85018261233d565b506020820151612392602085018261234c565b5060408201516123a5604085018261235b565b5060608201516123b8606085018261234c565b5060808201516123cb608085018261234c565b5060a08201516123de60a085018261234c565b5060c08201516123f160c085018261234c565b5060e082015161240460e085018261233d565b50505050565b5f6101008201905061241e5f83018461236a565b92915050565b5f6020820190506124375f830184612033565b92915050565b5f819050919050565b5f61246061245b61245684612003565b61243d565b612003565b9050919050565b5f61247182612446565b9050919050565b5f61248282612467565b9050919050565b61249281612478565b82525050565b5f6020820190506124ab5f830184612489565b92915050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f82825260208201905092915050565b5f6124f482612110565b6124fe81856124da565b935061250e81856020860161212a565b61251781611e56565b840191505092915050565b5f61252d83836124ea565b905092915050565b5f602082019050919050565b5f61254b826124b1565b61255581856124bb565b935083602082028501612567856124cb565b805f5b858110156125a257848403895281516125838582612522565b945061258e83612535565b925060208a0199505060018101905061256a565b50829750879550505050505092915050565b5f602083015f8301518482035f8601526125ce8282612541565b9150508091505092915050565b5f6020820190508181035f8301526125f381846125b4565b905092915050565b5f5f5f5f6080858703121561261357612612611e46565b5b5f85013567ffffffffffffffff8111156126305761262f611e4a565b5b61263c87828801611f5d565b945050602061264d878288016121a6565b935050604061265e878288016121a6565b925050606061266f878288016121a6565b91505092959194509250565b5f81905092915050565b5f61268f82612110565b612699818561267b565b93506126a981856020860161212a565b80840191505092915050565b5f6126c08284612685565b915081905092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61270282611feb565b915061270d83611feb565b9250828203905081811115612725576127246126cb565b5b92915050565b5f61273582611feb565b915061274083611feb565b925082820261274e81611feb565b91508282048414831517612765576127646126cb565b5b5092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b5f6127a382611feb565b91506127ae83611feb565b9250826127be576127bd61276c565b5b828204905092915050565b5f6060820190506127dc5f830186612033565b6127e96020830185611ff4565b6127f66040830184612033565b949350505050565b5f8151905061280c8161221a565b92915050565b5f6020828403121561282757612826611e46565b5b5f612834848285016127fe565b91505092915050565b5f81905092915050565b50565b5f6128555f8361283d565b915061286082612847565b5f82019050919050565b5f6128748261284a565b9150819050919050565b7f436f756c646e27742073656e642066756e6473000000000000000000000000005f82015250565b5f6128b260138361211a565b91506128bd8261287e565b602082019050919050565b5f6020820190508181035f8301526128df816128a6565b9050919050565b5f6040820190506128f95f830185611ff4565b6129066020830184612033565b9392505050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f600282049050600182168061295157607f821691505b6020821081036129645761296361290d565b5b50919050565b7f496e76616c696420736176696e6720696e6372656d656e742076616c756520735f8201527f656e740000000000000000000000000000000000000000000000000000000000602082015250565b5f6129c460238361211a565b91506129cf8261296a565b604082019050919050565b5f6020820190508181035f8301526129f1816129b8565b9050919050565b5f612a0282611feb565b9150612a0d83611feb565b9250828201905080821115612a2557612a246126cb565b5b92915050565b5f606082019050612a3e5f830186611ff4565b612a4b6020830185611ff4565b612a586040830184612033565b949350505050565b5f604082019050612a735f830185612033565b612a806020830184612033565b9392505050565b5f81519050612a9581612190565b92915050565b5f60208284031215612ab057612aaf611e46565b5b5f612abd84828501612a87565b91505092915050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302612b227fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82612ae7565b612b2c8683612ae7565b95508019841693508086168417925050509392505050565b5f612b5e612b59612b5484611feb565b61243d565b611feb565b9050919050565b5f819050919050565b612b7783612b44565b612b8b612b8382612b65565b848454612af3565b825550505050565b5f5f905090565b612ba2612b93565b612bad818484612b6e565b505050565b5b81811015612bd057612bc55f82612b9a565b600181019050612bb3565b5050565b601f821115612c1557612be681612ac6565b612bef84612ad8565b81016020851015612bfe578190505b612c12612c0a85612ad8565b830182612bb2565b50505b505050565b5f82821c905092915050565b5f612c355f1984600802612c1a565b1980831691505092915050565b5f612c4d8383612c26565b9150826002028217905092915050565b612c6682612110565b67ffffffffffffffff811115612c7f57612c7e611e66565b5b612c89825461293a565b612c94828285612bd4565b5f60209050601f831160018114612cc5575f8415612cb3578287015190505b612cbd8582612c42565b865550612d24565b601f198416612cd386612ac6565b5f5b82811015612cfa57848901518255600182019150602085019450602081019050612cd5565b86831015612d175784890151612d13601f891682612c26565b8355505b6001600288020188555050505b505050505050565b5f604082019050612d3f5f830185611ff4565b612d4c6020830184611ff4565b939250505056fea26469706673582212205cc4a3b0eb0f58c91f11b41812ce378bcb3389468526e399a18f1c4f01d919e064736f6c634300081b0033a26469706673582212207545be7eb4a4ecdb033a05673d8f587ca5c66d45c5014fe38f76fab4fb821e7364736f6c634300081b0033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913
-----Decoded View---------------
Arg [0] : _stableCoin (address): 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
Arg [1] : _csToken (address): 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
-----Encoded View---------------
2 Constructor Arguments found :
Arg [0] : 000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913
Arg [1] : 000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913
Loading...
Loading
Loading...
Loading
Loading...
Loading
Net Worth in USD
$0.00
Net Worth in ETH
0
Multichain Portfolio | 32 Chains
| Chain | Token | Portfolio % | Price | Amount | Value |
|---|
Loading...
Loading
Loading...
Loading
Loading...
Loading
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.