Overview
S Balance
S Value
$0.00More Info
Private Name Tags
ContractCreator
Loading...
Loading
Similar Match Source Code This contract matches the deployed Bytecode of the Source Code for Contract 0x6aE8FA03...c14ACE33E The constructor portion of the code might be different and could alter the actual behaviour of the contract
Contract Name:
DebitaV3Loan
Compiler Version
v0.8.20+commit.a1b79de6
Optimization Enabled:
Yes with 200 runs
Other Settings:
shanghai EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
pragma solidity ^0.8.0; import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import "@openzeppelin/contracts/proxy/utils/Initializable.sol"; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol"; import "@openzeppelin/contracts/utils/math/Math.sol"; interface Aggregator { function s_AuctionFactory() external view returns (address); function emitLoanUpdated(address _loan) external; function feePerDay() external view returns (uint); function maxFEE() external view returns (uint); function minFEE() external view returns (uint); } interface IveNFTEqualizer { struct receiptInstance { uint receiptID; uint attachedNFT; uint lockedAmount; uint lockedDate; uint decimals; address vault; address underlying; } function getDataByReceipt( uint receiptID ) external returns (receiptInstance memory); } interface AuctionFactory { function createAuction( uint _veNFTID, address _veNFTAddress, address liquidationToken, uint _initAmount, uint _floorAmount, uint _duration ) external returns (address); function getLiquidationFloorPrice( uint initAmount ) external view returns (uint); } interface IOwnerships { function ownerOf(uint256 tokenId) external view returns (address); function transferFrom(address from, address to, uint256 tokenId) external; function burn(uint256 tokenId) external; function mint(address to) external returns (uint256); } interface DLOImplementation { struct LendInfo { address lendOrderAddress; bool perpetual; bool lonelyLender; bool[] oraclesPerPairActivated; uint[] maxLTVs; uint apr; uint maxDuration; uint minDuration; address owner; address principle; address[] acceptedCollaterals; address[] oracle_Collaterals; uint[] maxRatio; address oracle_Principle; uint startedLendingAmount; uint availableAmount; } function addFunds(uint amount) external; function acceptLendingOffer(uint amount) external; function getLendInfo() external returns (LendInfo memory); } contract DebitaV3Loan is Initializable, ReentrancyGuard { address public s_OwnershipContract; address public AggregatorContract; uint feeLender; address feeAddress; uint creationBlock; struct infoOfOffers { address principle; // principle of the accepted offer address lendOffer; // address of the lendOffer contract uint principleAmount; // amount of principle taken uint lenderID; // id of the lender ownership uint apr; // apr of the accepted offer uint ratio; // ratio of the accepted offer uint collateralUsed; // collateral amount used uint maxDeadline; // max deadline of the accepted offer bool paid; // if the offer has been paid bool collateralClaimed; // if the collateral has been claimed bool debtClaimed; // if the debt has been claimed uint interestToClaim; // available interest to claim uint interestPaid; // interest paid } struct LoanData { address collateral; // collateral of the loan address[] principles; // principles of the loan address valuableCollateralAsset; // valuable collateral of the loan (Underlying in case of fNFT) bool isCollateralNFT; // if the collateral is NFT bool auctionInitialized; // if the auction has been initialized bool extended; // if the loan has been extended uint startedAt; // timestamp of the loan uint initialDuration; // the initial duration that the borrower took the loan uint borrowerID; // id of the borrower ownership uint NftID; // id of the NFT that is being used as collateral (if isCollateralNFT is true) uint collateralAmount; // collateral amount of the loan (1 if nft) uint collateralValuableAmount; // valuable collateral amount of the loan (if erc20 --> same as collateralAmount, if NFT --> underlying amount) uint valuableCollateralUsed; // valuable collateral used in the loan uint totalCountPaid; // total count of offers paid uint[] principlesAmount; // total amount of borrowed per principles infoOfOffers[] _acceptedOffers; } struct AuctionData { address auctionAddress; address liquidationAddress; uint soldAmount; uint tokenPerCollateralUsed; bool alreadySold; } LoanData public loanData; AuctionData public auctionData; uint offersCollateralClaimed_Borrower; function initialize( address _collateral, address[] memory _principles, bool _isCollateralNFT, uint _NftID, uint _collateralAmount, uint _valuableCollateralAmount, uint valuableCollateralUsed, address valuableAsset, uint _initialDuration, uint[] memory _principlesAmount, uint _borrowerID, infoOfOffers[] memory _acceptedOffers, address m_OwnershipContract, uint feeInterestLender, address _feeAddress ) public initializer nonReentrant { // set LoanData and acceptedOffers require(_acceptedOffers.length < 30, "Too many offers"); loanData = LoanData({ collateral: _collateral, principles: _principles, valuableCollateralAsset: valuableAsset, isCollateralNFT: _isCollateralNFT, auctionInitialized: false, extended: false, startedAt: block.timestamp, borrowerID: _borrowerID, NftID: _NftID, collateralAmount: _collateralAmount, collateralValuableAmount: _valuableCollateralAmount, valuableCollateralUsed: valuableCollateralUsed, initialDuration: _initialDuration, totalCountPaid: 0, principlesAmount: _principlesAmount, _acceptedOffers: _acceptedOffers }); s_OwnershipContract = m_OwnershipContract; feeLender = feeInterestLender; AggregatorContract = msg.sender; feeAddress = _feeAddress; creationBlock = block.number; } /** @notice Function to pay the debt of the loan @param indexes indexes of the offers to pay (only the borrower can call this function) If he misses one deadline, the loan will be counted as defaulted and the collateral will be claimed by the lenders */ function payDebt(uint[] memory indexes) public nonReentrant { IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); require(creationBlock != block.number, "Same block"); require( ownershipContract.ownerOf(loanData.borrowerID) == msg.sender, "Not borrower" ); // check next deadline require( nextDeadline() >= block.timestamp, "Deadline passed to pay Debt" ); for (uint i; i < indexes.length; i++) { uint index = indexes[i]; // get offer data on memory infoOfOffers memory offer = loanData._acceptedOffers[index]; // change the offer to paid on storage loanData._acceptedOffers[index].paid = true; // check if it has been already paid require(offer.paid == false, "Already paid"); require(offer.maxDeadline > block.timestamp, "Deadline passed"); uint interest = calculateInterestToPay(index); uint feeOnInterest = (interest * feeLender) / 10000; uint total = offer.principleAmount + interest - feeOnInterest; address currentOwnerOfOffer; try ownershipContract.ownerOf(offer.lenderID) returns ( address _lenderOwner ) { currentOwnerOfOffer = _lenderOwner; } catch {} DLOImplementation lendOffer = DLOImplementation(offer.lendOffer); DLOImplementation.LendInfo memory lendInfo = lendOffer .getLendInfo(); SafeERC20.safeTransferFrom( IERC20(offer.principle), msg.sender, address(this), total ); // if the lender is the owner of the offer and the offer is perpetual, then add the funds to the offer if (lendInfo.perpetual && lendInfo.owner == currentOwnerOfOffer) { // add not claimed interest to the offer uint notClaimedInterest = loanData ._acceptedOffers[index] .interestToClaim; loanData._acceptedOffers[index].interestToClaim = 0; loanData._acceptedOffers[index].debtClaimed = true; IERC20(offer.principle).approve( address(lendOffer), total + notClaimedInterest ); lendOffer.addFunds(total + notClaimedInterest); } else { loanData._acceptedOffers[index].interestToClaim += interest - feeOnInterest; } SafeERC20.safeTransferFrom( IERC20(offer.principle), msg.sender, feeAddress, feeOnInterest ); loanData._acceptedOffers[index].interestPaid += interest; } // update total count paid loanData.totalCountPaid += indexes.length; Aggregator(AggregatorContract).emitLoanUpdated(address(this)); // check owner } function claimInterest(uint index) internal { IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); infoOfOffers memory offer = loanData._acceptedOffers[index]; uint interest = offer.interestToClaim; require(interest > 0, "No interest to claim"); loanData._acceptedOffers[index].interestToClaim = 0; SafeERC20.safeTransfer(IERC20(offer.principle), msg.sender, interest); Aggregator(AggregatorContract).emitLoanUpdated(address(this)); } function claimDebt(uint index) external nonReentrant { IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); infoOfOffers memory offer = loanData._acceptedOffers[index]; require( ownershipContract.ownerOf(offer.lenderID) == msg.sender, "Not lender" ); // check if the offer has been paid, if not just call claimInterest function if (offer.paid) { _claimDebt(index); } else { // if not already full paid, claim interest claimInterest(index); } } function _claimDebt(uint index) internal { LoanData memory m_loan = loanData; IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); infoOfOffers memory offer = m_loan._acceptedOffers[index]; require( ownershipContract.ownerOf(offer.lenderID) == msg.sender, "Not lender" ); require(offer.paid == true, "Not paid"); require(offer.debtClaimed == false, "Already claimed"); loanData._acceptedOffers[index].debtClaimed = true; ownershipContract.burn(offer.lenderID); uint interest = offer.interestToClaim; offer.interestToClaim = 0; SafeERC20.safeTransfer( IERC20(offer.principle), msg.sender, interest + offer.principleAmount ); Aggregator(AggregatorContract).emitLoanUpdated(address(this)); } // only the auction contract can call this function /** @notice Function to handle the auction sell of the collateral. Only the auction contract can call this function @param amount amount of collateral sold on the auction */ function handleAuctionSell(uint amount) external nonReentrant { require( msg.sender == auctionData.auctionAddress, "Not auction contract" ); require(auctionData.alreadySold == false, "Already sold"); LoanData memory m_loan = loanData; IveNFTEqualizer.receiptInstance memory nftData = IveNFTEqualizer( m_loan.collateral ).getDataByReceipt(m_loan.NftID); uint PRECISION = 10 ** nftData.decimals; auctionData.soldAmount = amount; auctionData.alreadySold = true; auctionData.tokenPerCollateralUsed = ((amount * PRECISION) / (loanData.valuableCollateralUsed)); Aggregator(AggregatorContract).emitLoanUpdated(address(this)); } /** @notice Function to claim the collateral as lender, only in case of default. Only lenders can call this function @param index index of the offer to claim the collateral */ function claimCollateralAsLender(uint index) external nonReentrant { LoanData memory m_loan = loanData; infoOfOffers memory offer = m_loan._acceptedOffers[index]; IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); require( ownershipContract.ownerOf(offer.lenderID) == msg.sender, "Not lender" ); // burn ownership ownershipContract.burn(offer.lenderID); uint _nextDeadline = nextDeadline(); require(offer.paid == false, "Already paid"); require( _nextDeadline < block.timestamp && _nextDeadline != 0, "Deadline not passed" ); require(offer.collateralClaimed == false, "Already executed"); // claim collateral if (m_loan.isCollateralNFT) { claimCollateralAsNFTLender(index); } else { loanData._acceptedOffers[index].collateralClaimed = true; uint decimals = ERC20(loanData.collateral).decimals(); SafeERC20.safeTransfer( IERC20(loanData.collateral), msg.sender, (offer.principleAmount * (10 ** decimals)) / offer.ratio ); } Aggregator(AggregatorContract).emitLoanUpdated(address(this)); } function claimCollateralAsNFTLender(uint index) internal returns (bool) { LoanData memory m_loan = loanData; infoOfOffers memory offer = m_loan._acceptedOffers[index]; loanData._acceptedOffers[index].collateralClaimed = true; if (m_loan.auctionInitialized) { // if the auction has been initialized // check if the auction has been sold require(auctionData.alreadySold, "Not sold on auction"); uint decimalsCollateral = IveNFTEqualizer(loanData.collateral) .getDataByReceipt(loanData.NftID) .decimals; uint payment = (auctionData.tokenPerCollateralUsed * offer.collateralUsed) / (10 ** decimalsCollateral); SafeERC20.safeTransfer( IERC20(auctionData.liquidationAddress), msg.sender, payment ); return true; } else if ( m_loan._acceptedOffers.length == 1 && !m_loan.auctionInitialized ) { // if there is only one offer and the auction has not been initialized // send the NFT to the lender IERC721(m_loan.collateral).transferFrom( address(this), msg.sender, m_loan.NftID ); return true; } return false; } /** @notice Function to create an auction for the collateral in case of a default. Only lenders can call this functions in case the borrower missed their deadline or if the borrower wants to liquidate the collateral and offers length is more than 1 (It will go anyway to the auction if the borrower has more than 1 offer) @param indexOfLender index of the lender in the acceptedOffers array */ function createAuctionForCollateral( uint indexOfLender ) external nonReentrant { LoanData memory m_loan = loanData; address lenderAddress = safeGetOwner( m_loan._acceptedOffers[indexOfLender].lenderID ); address borrowerAddress = safeGetOwner(m_loan.borrowerID); bool hasLenderRightToInitAuction = lenderAddress == msg.sender && m_loan._acceptedOffers[indexOfLender].paid == false; bool hasBorrowerRightToInitAuction = borrowerAddress == msg.sender && m_loan._acceptedOffers.length > 1; // check if collateral is actually NFT require(m_loan.isCollateralNFT, "Collateral is not NFT"); // check that total count paid is not equal to the total offers require( m_loan.totalCountPaid != m_loan._acceptedOffers.length, "Already paid everything" ); // check if the deadline has passed require(nextDeadline() < block.timestamp, "Deadline not passed"); // check if the auction has not been already initialized require(m_loan.auctionInitialized == false, "Already initialized"); // check if the lender has the right to initialize the auction // check if the borrower has the right to initialize the auction require( hasLenderRightToInitAuction || hasBorrowerRightToInitAuction, "Not involved" ); // collateral has to be NFT AuctionFactory auctionFactory = AuctionFactory( Aggregator(AggregatorContract).s_AuctionFactory() ); loanData.auctionInitialized = true; IveNFTEqualizer.receiptInstance memory receiptInfo = IveNFTEqualizer( m_loan.collateral ).getDataByReceipt(m_loan.NftID); // calculate floor amount for liquidations uint floorAmount = auctionFactory.getLiquidationFloorPrice( receiptInfo.lockedAmount ); // create auction and save the information IERC721(m_loan.collateral).approve( address(auctionFactory), m_loan.NftID ); address liveAuction = auctionFactory.createAuction( m_loan.NftID, m_loan.collateral, receiptInfo.underlying, receiptInfo.lockedAmount, floorAmount, 864000 ); auctionData = AuctionData({ auctionAddress: liveAuction, liquidationAddress: receiptInfo.underlying, soldAmount: 0, tokenPerCollateralUsed: 0, alreadySold: false }); Aggregator(AggregatorContract).emitLoanUpdated(address(this)); // emit event here } // function to claim collateral as borrower /** @notice Function to claim the collateral as borrower. Only the borrower can call this function @param indexs indexes of the offers to claim the collateral */ function claimCollateralAsBorrower( uint[] memory indexs ) external nonReentrant { IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); require( ownershipContract.ownerOf(loanData.borrowerID) == msg.sender, "Not borrower" ); // if the collateral is nft, it has another logic if (loanData.isCollateralNFT) { claimCollateralNFTAsBorrower(indexs); } else { claimCollateralERC20AsBorrower(indexs); } offersCollateralClaimed_Borrower += indexs.length; // In case every offer has been claimed & paid, burn the borrower ownership if ( offersCollateralClaimed_Borrower == loanData._acceptedOffers.length ) { ownershipContract.burn(loanData.borrowerID); } Aggregator(AggregatorContract).emitLoanUpdated(address(this)); } function claimCollateralERC20AsBorrower(uint[] memory indexs) internal { require(loanData.isCollateralNFT == false, "Collateral is NFT"); uint collateralToSend; for (uint i; i < indexs.length; i++) { infoOfOffers memory offer = loanData._acceptedOffers[indexs[i]]; require(offer.paid == true, "Not paid"); require(offer.collateralClaimed == false, "Already executed"); loanData._acceptedOffers[indexs[i]].collateralClaimed = true; uint decimalsCollateral = ERC20(loanData.collateral).decimals(); collateralToSend += (offer.principleAmount * (10 ** decimalsCollateral)) / offer.ratio; } SafeERC20.safeTransfer( IERC20(loanData.collateral), msg.sender, collateralToSend ); } // function to extend the loan (only the borrower can call this function) // extend the loan to the max deadline of each offer function extendLoan() public { IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); LoanData memory m_loan = loanData; require( ownershipContract.ownerOf(loanData.borrowerID) == msg.sender, "Not borrower" ); require( nextDeadline() > block.timestamp, "Deadline passed to extend loan" ); require(loanData.extended == false, "Already extended"); // at least 10% of the loan duration has to be transcurred in order to extend the loan uint minimalDurationPayment = (m_loan.initialDuration * 1000) / 10000; require( (block.timestamp - m_loan.startedAt) > minimalDurationPayment, "Not enough time" ); loanData.extended = true; // calculate fees to pay to us uint feePerDay = Aggregator(AggregatorContract).feePerDay(); uint minFEE = Aggregator(AggregatorContract).minFEE(); uint maxFee = Aggregator(AggregatorContract).maxFEE(); uint PorcentageOfFeePaid = Math.ceilDiv( (m_loan.initialDuration), 86400 ) * feePerDay; // adjust fees if (PorcentageOfFeePaid > maxFee) { PorcentageOfFeePaid = maxFee; } else if (PorcentageOfFeePaid < minFEE) { PorcentageOfFeePaid = minFEE; } // calculate interest to pay to Debita and the subtract to the lenders for (uint i; i < m_loan._acceptedOffers.length; i++) { infoOfOffers memory offer = m_loan._acceptedOffers[i]; // if paid, skip // if not paid, calculate interest to pay if (!offer.paid) { uint alreadyUsedTime = block.timestamp - m_loan.startedAt; uint interestOfUsedTime = calculateInterestToPay(i); uint interestToPayToDebita = (interestOfUsedTime * feeLender) / 10000; uint misingBorrowFee; // if user already paid the max fee, then we dont have to charge them again if (PorcentageOfFeePaid != maxFee) { // calculate difference from fee paid for the initialDuration vs the extra fee they should pay because of the extras days of extending the loan. MAXFEE shouldnt be higher than extra fee + PorcentageOfFeePaid uint feeOfMaxDeadline = Math.ceilDiv( ((offer.maxDeadline - m_loan.startedAt)), 86400 ) * feePerDay; if (feeOfMaxDeadline > maxFee) { feeOfMaxDeadline = maxFee; } else if (feeOfMaxDeadline < minFEE) { feeOfMaxDeadline = minFEE; } misingBorrowFee = feeOfMaxDeadline - PorcentageOfFeePaid; } uint principleAmount = offer.principleAmount; uint feeAmount = (principleAmount * misingBorrowFee) / 10000; SafeERC20.safeTransferFrom( IERC20(offer.principle), msg.sender, address(this), interestOfUsedTime - interestToPayToDebita ); SafeERC20.safeTransferFrom( IERC20(offer.principle), msg.sender, feeAddress, interestToPayToDebita + feeAmount ); /* CHECK IF CURRENT LENDER IS THE OWNER OF THE OFFER & IF IT'S PERPETUAL FOR INTEREST */ DLOImplementation lendOffer = DLOImplementation( offer.lendOffer ); DLOImplementation.LendInfo memory lendInfo = lendOffer .getLendInfo(); address currentOwnerOfOffer; try ownershipContract.ownerOf(offer.lenderID) returns ( address _lenderOwner ) { currentOwnerOfOffer = _lenderOwner; } catch {} if ( lendInfo.perpetual && lendInfo.owner == currentOwnerOfOffer ) { IERC20(offer.principle).approve( address(lendOffer), interestOfUsedTime - interestToPayToDebita ); lendOffer.addFunds( interestOfUsedTime - interestToPayToDebita ); } else { loanData._acceptedOffers[i].interestToClaim += interestOfUsedTime - interestToPayToDebita; } loanData._acceptedOffers[i].interestPaid += interestOfUsedTime; } } Aggregator(AggregatorContract).emitLoanUpdated(address(this)); } function claimCollateralNFTAsBorrower(uint[] memory indexes) internal { if (auctionData.alreadySold) { // in case of a partial default, borrower can claim the collateral of the offers that have been paid for (uint i; i < indexes.length; i++) { // load storage for each index LoanData memory m_loan = loanData; infoOfOffers memory offer = m_loan._acceptedOffers[indexes[i]]; // check payment require(offer.paid == true, "Not paid"); // not claimed yet require(offer.collateralClaimed == false, "Already executed"); loanData._acceptedOffers[indexes[i]].collateralClaimed = true; uint decimalsCollateral = IveNFTEqualizer(loanData.collateral) .getDataByReceipt(loanData.NftID) .decimals; uint collateralUsed = offer.collateralUsed; uint payment = (auctionData.tokenPerCollateralUsed * collateralUsed) / (10 ** decimalsCollateral); SafeERC20.safeTransfer( IERC20(auctionData.liquidationAddress), msg.sender, payment ); } } else { LoanData memory m_loan = loanData; // In case of NFT, borrower has to pay all the offers to claim the collateral require( m_loan.totalCountPaid == m_loan._acceptedOffers.length, "Not paid" ); for (uint i; i < m_loan._acceptedOffers.length; i++) { // check payment one by one require(m_loan._acceptedOffers[i].paid == true, "Not paid"); // not claimed yet require( m_loan._acceptedOffers[i].collateralClaimed == false, "Already executed" ); loanData._acceptedOffers[i].collateralClaimed = true; } // send NFT to the borrower IERC721(m_loan.collateral).transferFrom( address(this), msg.sender, m_loan.NftID ); } } // calculate interest to pay function calculateInterestToPay(uint index) public view returns (uint) { infoOfOffers memory offer = loanData._acceptedOffers[index]; uint anualInterest = (offer.principleAmount * offer.apr) / 10000; // check already duration uint activeTime = block.timestamp - loanData.startedAt; uint minimalDurationPayment = (loanData.initialDuration * 1000) / 10000; uint maxDuration = offer.maxDeadline - loanData.startedAt; if (activeTime > maxDuration) { activeTime = maxDuration; } else if (activeTime < minimalDurationPayment) { activeTime = minimalDurationPayment; } uint interest = (anualInterest * activeTime) / 31536000; // subtract already paid interest return interest - offer.interestPaid; } /** @notice Function to get the next deadline of the loan */ function nextDeadline() public view returns (uint) { uint _nextDeadline; LoanData memory m_loan = loanData; if (m_loan.extended) { for (uint i; i < m_loan._acceptedOffers.length; i++) { if ( _nextDeadline == 0 && m_loan._acceptedOffers[i].paid == false ) { _nextDeadline = m_loan._acceptedOffers[i].maxDeadline; } else if ( m_loan._acceptedOffers[i].paid == false && _nextDeadline > m_loan._acceptedOffers[i].maxDeadline ) { _nextDeadline = m_loan._acceptedOffers[i].maxDeadline; } } } else { _nextDeadline = m_loan.startedAt + m_loan.initialDuration; } return _nextDeadline; } function getLoanData() public view returns (LoanData memory) { return loanData; } function getAuctionData() public view returns (AuctionData memory) { return auctionData; } function safeGetOwner(uint tokenId) internal view returns (address) { IOwnerships ownershipContract = IOwnerships(s_OwnershipContract); try ownershipContract.ownerOf(tokenId) returns (address owner) { return owner; } catch { return address(0); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol) pragma solidity ^0.8.20; /** * @dev Contract module that helps prevent reentrant calls to a function. * * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier * available, which can be applied to functions to make sure there are no nested * (reentrant) calls to them. * * Note that because there is a single `nonReentrant` guard, functions marked as * `nonReentrant` may not call one another. This can be worked around by making * those functions `private`, and then adding `external` `nonReentrant` entry * points to them. * * TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at, * consider using {ReentrancyGuardTransient} instead. * * TIP: If you would like to learn more about reentrancy and alternative ways * to protect against it, check out our blog post * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. */ abstract contract ReentrancyGuard { // 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; uint256 private _status; /** * @dev Unauthorized reentrant call. */ error ReentrancyGuardReentrantCall(); constructor() { _status = NOT_ENTERED; } /** * @dev Prevents a contract from calling itself, directly or indirectly. * Calling a `nonReentrant` function from another `nonReentrant` * function is not supported. It is possible to prevent this from happening * by making the `nonReentrant` function external, and making it call a * `private` function that does the actual work. */ modifier nonReentrant() { _nonReentrantBefore(); _; _nonReentrantAfter(); } function _nonReentrantBefore() private { // On the first call to nonReentrant, _status will be NOT_ENTERED if (_status == ENTERED) { revert ReentrancyGuardReentrantCall(); } // Any calls to nonReentrant after this point will fail _status = ENTERED; } function _nonReentrantAfter() private { // By storing the original value once again, a refund is triggered (see // https://eips.ethereum.org/EIPS/eip-2200) _status = NOT_ENTERED; } /** * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a * `nonReentrant` function in the call stack. */ function _reentrancyGuardEntered() internal view returns (bool) { return _status == ENTERED; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (proxy/utils/Initializable.sol) pragma solidity ^0.8.20; /** * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect. * * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in * case an upgrade adds a module that needs to be initialized. * * For example: * * [.hljs-theme-light.nopadding] * ```solidity * contract MyToken is ERC20Upgradeable { * function initialize() initializer public { * __ERC20_init("MyToken", "MTK"); * } * } * * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable { * function initializeV2() reinitializer(2) public { * __ERC20Permit_init("MyToken"); * } * } * ``` * * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}. * * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity. * * [CAUTION] * ==== * Avoid leaving a contract uninitialized. * * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed: * * [.hljs-theme-light.nopadding] * ``` * /// @custom:oz-upgrades-unsafe-allow constructor * constructor() { * _disableInitializers(); * } * ``` * ==== */ abstract contract Initializable { /** * @dev Storage of the initializable contract. * * It's implemented on a custom ERC-7201 namespace to reduce the risk of storage collisions * when using with upgradeable contracts. * * @custom:storage-location erc7201:openzeppelin.storage.Initializable */ struct InitializableStorage { /** * @dev Indicates that the contract has been initialized. */ uint64 _initialized; /** * @dev Indicates that the contract is in the process of being initialized. */ bool _initializing; } // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Initializable")) - 1)) & ~bytes32(uint256(0xff)) bytes32 private constant INITIALIZABLE_STORAGE = 0xf0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00; /** * @dev The contract is already initialized. */ error InvalidInitialization(); /** * @dev The contract is not initializing. */ error NotInitializing(); /** * @dev Triggered when the contract has been initialized or reinitialized. */ event Initialized(uint64 version); /** * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope, * `onlyInitializing` functions can be used to initialize parent contracts. * * Similar to `reinitializer(1)`, except that in the context of a constructor an `initializer` may be invoked any * number of times. This behavior in the constructor can be useful during testing and is not expected to be used in * production. * * Emits an {Initialized} event. */ modifier initializer() { // solhint-disable-next-line var-name-mixedcase InitializableStorage storage $ = _getInitializableStorage(); // Cache values to avoid duplicated sloads bool isTopLevelCall = !$._initializing; uint64 initialized = $._initialized; // Allowed calls: // - initialSetup: the contract is not in the initializing state and no previous version was // initialized // - construction: the contract is initialized at version 1 (no reininitialization) and the // current contract is just being deployed bool initialSetup = initialized == 0 && isTopLevelCall; bool construction = initialized == 1 && address(this).code.length == 0; if (!initialSetup && !construction) { revert InvalidInitialization(); } $._initialized = 1; if (isTopLevelCall) { $._initializing = true; } _; if (isTopLevelCall) { $._initializing = false; emit Initialized(1); } } /** * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be * used to initialize parent contracts. * * A reinitializer may be used after the original initialization step. This is essential to configure modules that * are added through upgrades and that require initialization. * * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer` * cannot be nested. If one is invoked in the context of another, execution will revert. * * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in * a contract, executing them in the right order is up to the developer or operator. * * WARNING: Setting the version to 2**64 - 1 will prevent any future reinitialization. * * Emits an {Initialized} event. */ modifier reinitializer(uint64 version) { // solhint-disable-next-line var-name-mixedcase InitializableStorage storage $ = _getInitializableStorage(); if ($._initializing || $._initialized >= version) { revert InvalidInitialization(); } $._initialized = version; $._initializing = true; _; $._initializing = false; emit Initialized(version); } /** * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the * {initializer} and {reinitializer} modifiers, directly or indirectly. */ modifier onlyInitializing() { _checkInitializing(); _; } /** * @dev Reverts if the contract is not in an initializing state. See {onlyInitializing}. */ function _checkInitializing() internal view virtual { if (!_isInitializing()) { revert NotInitializing(); } } /** * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call. * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized * to any version. It is recommended to use this to lock implementation contracts that are designed to be called * through proxies. * * Emits an {Initialized} event the first time it is successfully executed. */ function _disableInitializers() internal virtual { // solhint-disable-next-line var-name-mixedcase InitializableStorage storage $ = _getInitializableStorage(); if ($._initializing) { revert InvalidInitialization(); } if ($._initialized != type(uint64).max) { $._initialized = type(uint64).max; emit Initialized(type(uint64).max); } } /** * @dev Returns the highest version that has been initialized. See {reinitializer}. */ function _getInitializedVersion() internal view returns (uint64) { return _getInitializableStorage()._initialized; } /** * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}. */ function _isInitializing() internal view returns (bool) { return _getInitializableStorage()._initializing; } /** * @dev Pointer to storage slot. Allows integrators to override it with a custom storage location. * * NOTE: Consider following the ERC-7201 formula to derive storage locations. */ function _initializableStorageSlot() internal pure virtual returns (bytes32) { return INITIALIZABLE_STORAGE; } /** * @dev Returns a pointer to the storage namespace. */ // solhint-disable-next-line var-name-mixedcase function _getInitializableStorage() private pure returns (InitializableStorage storage $) { bytes32 slot = _initializableStorageSlot(); assembly { $.slot := slot } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.2.0) (token/ERC20/ERC20.sol) pragma solidity ^0.8.20; import {IERC20} from "./IERC20.sol"; import {IERC20Metadata} from "./extensions/IERC20Metadata.sol"; import {Context} from "../../utils/Context.sol"; import {IERC20Errors} from "../../interfaces/draft-IERC6093.sol"; /** * @dev Implementation of the {IERC20} interface. * * This implementation is agnostic to the way tokens are created. This means * that a supply mechanism has to be added in a derived contract using {_mint}. * * TIP: For a detailed writeup see our guide * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How * to implement supply mechanisms]. * * The default value of {decimals} is 18. To change this, you should override * this function so it returns a different value. * * We have followed general OpenZeppelin Contracts guidelines: functions revert * instead returning `false` on failure. This behavior is nonetheless * conventional and does not conflict with the expectations of ERC-20 * applications. */ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors { mapping(address account => uint256) private _balances; mapping(address account => mapping(address spender => uint256)) private _allowances; uint256 private _totalSupply; string private _name; string private _symbol; /** * @dev Sets the values for {name} and {symbol}. * * All two of these values are immutable: they can only be set once during * construction. */ constructor(string memory name_, string memory symbol_) { _name = name_; _symbol = symbol_; } /** * @dev Returns the name of the token. */ function name() public view virtual returns (string memory) { return _name; } /** * @dev Returns the symbol of the token, usually a shorter version of the * name. */ function symbol() public view virtual returns (string memory) { return _symbol; } /** * @dev Returns the number of decimals used to get its user representation. * For example, if `decimals` equals `2`, a balance of `505` tokens should * be displayed to a user as `5.05` (`505 / 10 ** 2`). * * Tokens usually opt for a value of 18, imitating the relationship between * Ether and Wei. This is the default value returned by this function, unless * it's overridden. * * NOTE: This information is only used for _display_ purposes: it in * no way affects any of the arithmetic of the contract, including * {IERC20-balanceOf} and {IERC20-transfer}. */ function decimals() public view virtual returns (uint8) { return 18; } /** * @dev See {IERC20-totalSupply}. */ function totalSupply() public view virtual returns (uint256) { return _totalSupply; } /** * @dev See {IERC20-balanceOf}. */ function balanceOf(address account) public view virtual returns (uint256) { return _balances[account]; } /** * @dev See {IERC20-transfer}. * * Requirements: * * - `to` cannot be the zero address. * - the caller must have a balance of at least `value`. */ function transfer(address to, uint256 value) public virtual returns (bool) { address owner = _msgSender(); _transfer(owner, to, value); return true; } /** * @dev See {IERC20-allowance}. */ function allowance(address owner, address spender) public view virtual returns (uint256) { return _allowances[owner][spender]; } /** * @dev See {IERC20-approve}. * * NOTE: If `value` is the maximum `uint256`, the allowance is not updated on * `transferFrom`. This is semantically equivalent to an infinite approval. * * Requirements: * * - `spender` cannot be the zero address. */ function approve(address spender, uint256 value) public virtual returns (bool) { address owner = _msgSender(); _approve(owner, spender, value); return true; } /** * @dev See {IERC20-transferFrom}. * * Skips emitting an {Approval} event indicating an allowance update. This is not * required by the ERC. See {xref-ERC20-_approve-address-address-uint256-bool-}[_approve]. * * NOTE: Does not update the allowance if the current allowance * is the maximum `uint256`. * * Requirements: * * - `from` and `to` cannot be the zero address. * - `from` must have a balance of at least `value`. * - the caller must have allowance for ``from``'s tokens of at least * `value`. */ function transferFrom(address from, address to, uint256 value) public virtual returns (bool) { address spender = _msgSender(); _spendAllowance(from, spender, value); _transfer(from, to, value); return true; } /** * @dev Moves a `value` amount of tokens from `from` to `to`. * * This internal function is equivalent to {transfer}, and can be used to * e.g. implement automatic token fees, slashing mechanisms, etc. * * Emits a {Transfer} event. * * NOTE: This function is not virtual, {_update} should be overridden instead. */ function _transfer(address from, address to, uint256 value) internal { if (from == address(0)) { revert ERC20InvalidSender(address(0)); } if (to == address(0)) { revert ERC20InvalidReceiver(address(0)); } _update(from, to, value); } /** * @dev Transfers a `value` amount of tokens from `from` to `to`, or alternatively mints (or burns) if `from` * (or `to`) is the zero address. All customizations to transfers, mints, and burns should be done by overriding * this function. * * Emits a {Transfer} event. */ function _update(address from, address to, uint256 value) internal virtual { if (from == address(0)) { // Overflow check required: The rest of the code assumes that totalSupply never overflows _totalSupply += value; } else { uint256 fromBalance = _balances[from]; if (fromBalance < value) { revert ERC20InsufficientBalance(from, fromBalance, value); } unchecked { // Overflow not possible: value <= fromBalance <= totalSupply. _balances[from] = fromBalance - value; } } if (to == address(0)) { unchecked { // Overflow not possible: value <= totalSupply or value <= fromBalance <= totalSupply. _totalSupply -= value; } } else { unchecked { // Overflow not possible: balance + value is at most totalSupply, which we know fits into a uint256. _balances[to] += value; } } emit Transfer(from, to, value); } /** * @dev Creates a `value` amount of tokens and assigns them to `account`, by transferring it from address(0). * Relies on the `_update` mechanism * * Emits a {Transfer} event with `from` set to the zero address. * * NOTE: This function is not virtual, {_update} should be overridden instead. */ function _mint(address account, uint256 value) internal { if (account == address(0)) { revert ERC20InvalidReceiver(address(0)); } _update(address(0), account, value); } /** * @dev Destroys a `value` amount of tokens from `account`, lowering the total supply. * Relies on the `_update` mechanism. * * Emits a {Transfer} event with `to` set to the zero address. * * NOTE: This function is not virtual, {_update} should be overridden instead */ function _burn(address account, uint256 value) internal { if (account == address(0)) { revert ERC20InvalidSender(address(0)); } _update(account, address(0), value); } /** * @dev Sets `value` as the allowance of `spender` over the `owner` s tokens. * * This internal function is equivalent to `approve`, and can be used to * e.g. set automatic allowances for certain subsystems, etc. * * Emits an {Approval} event. * * Requirements: * * - `owner` cannot be the zero address. * - `spender` cannot be the zero address. * * Overrides to this logic should be done to the variant with an additional `bool emitEvent` argument. */ function _approve(address owner, address spender, uint256 value) internal { _approve(owner, spender, value, true); } /** * @dev Variant of {_approve} with an optional flag to enable or disable the {Approval} event. * * By default (when calling {_approve}) the flag is set to true. On the other hand, approval changes made by * `_spendAllowance` during the `transferFrom` operation set the flag to false. This saves gas by not emitting any * `Approval` event during `transferFrom` operations. * * Anyone who wishes to continue emitting `Approval` events on the`transferFrom` operation can force the flag to * true using the following override: * * ```solidity * function _approve(address owner, address spender, uint256 value, bool) internal virtual override { * super._approve(owner, spender, value, true); * } * ``` * * Requirements are the same as {_approve}. */ function _approve(address owner, address spender, uint256 value, bool emitEvent) internal virtual { if (owner == address(0)) { revert ERC20InvalidApprover(address(0)); } if (spender == address(0)) { revert ERC20InvalidSpender(address(0)); } _allowances[owner][spender] = value; if (emitEvent) { emit Approval(owner, spender, value); } } /** * @dev Updates `owner` s allowance for `spender` based on spent `value`. * * Does not update the allowance value in case of infinite allowance. * Revert if not enough allowance is available. * * Does not emit an {Approval} event. */ function _spendAllowance(address owner, address spender, uint256 value) internal virtual { uint256 currentAllowance = allowance(owner, spender); if (currentAllowance < type(uint256).max) { if (currentAllowance < value) { revert ERC20InsufficientAllowance(spender, currentAllowance, value); } unchecked { _approve(owner, spender, currentAllowance - value, false); } } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/IERC721.sol) pragma solidity ^0.8.20; 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.2.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 { _callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value))); } /** * @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 { _callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value))); } /** * @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 _callOptionalReturnBool(token, abi.encodeCall(token.transfer, (to, value))); } /** * @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 _callOptionalReturnBool(token, abi.encodeCall(token.transferFrom, (from, to, value))); } /** * @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 { bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value)); if (!_callOptionalReturnBool(token, approvalCall)) { _callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0))); _callOptionalReturn(token, approvalCall); } } /** * @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 rely 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 rely 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}. * Opposedly, 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 high-level call (i.e. a regular function call to a contract), 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 data The call data (encoded using abi.encode or one of its variants). * * This is a variant of {_callOptionalReturnBool} that reverts if call fails to meet the requirements. */ function _callOptionalReturn(IERC20 token, bytes memory data) private { uint256 returnSize; uint256 returnValue; assembly ("memory-safe") { let success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20) // bubble errors if iszero(success) { let ptr := mload(0x40) returndatacopy(ptr, 0, returndatasize()) revert(ptr, returndatasize()) } returnSize := returndatasize() returnValue := mload(0) } if (returnSize == 0 ? address(token).code.length == 0 : returnValue != 1) { revert SafeERC20FailedOperation(address(token)); } } /** * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), 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 data The call data (encoded using abi.encode or one of its variants). * * This is a variant of {_callOptionalReturn} that silently catches all reverts and returns a bool instead. */ function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) { bool success; uint256 returnSize; uint256 returnValue; assembly ("memory-safe") { success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20) returnSize := returndatasize() returnValue := mload(0) } return success && (returnSize == 0 ? address(token).code.length > 0 : returnValue == 1); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/utils/ERC721Holder.sol) pragma solidity ^0.8.20; import {IERC721Receiver} from "../IERC721Receiver.sol"; /** * @dev Implementation of the {IERC721Receiver} interface. * * Accepts all token transfers. * Make sure the contract is able to use its token with {IERC721-safeTransferFrom}, {IERC721-approve} or * {IERC721-setApprovalForAll}. */ abstract contract ERC721Holder is IERC721Receiver { /** * @dev See {IERC721Receiver-onERC721Received}. * * Always returns `IERC721Receiver.onERC721Received.selector`. */ function onERC721Received(address, address, uint256, bytes memory) public virtual returns (bytes4) { return this.onERC721Received.selector; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.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 an 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 an 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 an 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 `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, 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; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol) pragma solidity ^0.8.20; /** * @dev Interface of the ERC-20 standard as defined in the ERC. */ interface IERC20 { /** * @dev Emitted when `value` tokens are moved from one account (`from`) to * another (`to`). * * Note that `value` may be zero. */ event Transfer(address indexed from, address indexed to, uint256 value); /** * @dev Emitted when the allowance of a `spender` for an `owner` is set by * a call to {approve}. `value` is the new allowance. */ event Approval(address indexed owner, address indexed spender, uint256 value); /** * @dev Returns the value of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the value of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves a `value` amount of tokens from the caller's account to `to`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address to, uint256 value) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets a `value` amount of tokens as the allowance of `spender` over the * caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 value) external returns (bool); /** * @dev Moves a `value` amount of tokens from `from` to `to` using the * allowance mechanism. `value` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom(address from, address to, uint256 value) external returns (bool); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/extensions/IERC20Metadata.sol) pragma solidity ^0.8.20; import {IERC20} from "../IERC20.sol"; /** * @dev Interface for the optional metadata functions from the ERC-20 standard. */ interface IERC20Metadata is IERC20 { /** * @dev Returns the name of the token. */ function name() external view returns (string memory); /** * @dev Returns the symbol of the token. */ function symbol() external view returns (string memory); /** * @dev Returns the decimals places of the token. */ function decimals() external view returns (uint8); }
// 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.1.0) (interfaces/draft-IERC6093.sol) pragma solidity ^0.8.20; /** * @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-20. * 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.1.0) (utils/introspection/IERC165.sol) pragma solidity ^0.8.20; /** * @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.1.0) (interfaces/IERC1363.sol) pragma solidity ^0.8.20; 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: MIT // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/IERC721Receiver.sol) pragma solidity ^0.8.20; /** * @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.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.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.0.0) (interfaces/IERC20.sol) pragma solidity ^0.8.20; import {IERC20} from "../token/ERC20/IERC20.sol";
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC165.sol) pragma solidity ^0.8.20; import {IERC165} from "../utils/introspection/IERC165.sol";
{ "remappings": [ "@pythnetwork/pyth-sdk-solidity/=node_modules/@pythnetwork/pyth-sdk-solidity/", "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/", "@contracts/=contracts/", "@aerodrome/=lib/contracts/contracts/", "forge-std/=lib/forge-std/src/", "@redstone-finance/evm-connector/dist/contracts/=lib/redstone-oracles-monorepo/packages/evm-connector/contracts/", "@chainlink/=lib/foundry-chainlink-toolkit/", "@opengsn/=lib/contracts/lib/gsn/packages/", "@uniswap/v3-core/=lib/contracts/lib/v3-core/", "chainlink-brownie-contracts/=lib/foundry-chainlink-toolkit/lib/chainlink-brownie-contracts/contracts/src/v0.6/vendor/@arbitrum/nitro-contracts/src/", "contracts/=lib/contracts/contracts/", "ds-test/=lib/contracts/lib/ds-test/src/", "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/", "foundry-chainlink-toolkit/=lib/foundry-chainlink-toolkit/", "gsn/=lib/contracts/lib/", "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/", "openzeppelin-contracts/=lib/openzeppelin-contracts/", "redstone-oracles-monorepo/=lib/redstone-oracles-monorepo/", "utils/=lib/contracts/test/utils/", "v3-core/=lib/contracts/lib/v3-core/" ], "optimizer": { "enabled": true, "runs": 200 }, "metadata": { "useLiteralContent": false, "bytecodeHash": "ipfs", "appendCBOR": true }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "evmVersion": "shanghai", "viaIR": true, "libraries": {} }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[],"name":"InvalidInitialization","type":"error"},{"inputs":[],"name":"NotInitializing","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"SafeERC20FailedOperation","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint64","name":"version","type":"uint64"}],"name":"Initialized","type":"event"},{"inputs":[],"name":"AggregatorContract","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"auctionData","outputs":[{"internalType":"address","name":"auctionAddress","type":"address"},{"internalType":"address","name":"liquidationAddress","type":"address"},{"internalType":"uint256","name":"soldAmount","type":"uint256"},{"internalType":"uint256","name":"tokenPerCollateralUsed","type":"uint256"},{"internalType":"bool","name":"alreadySold","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"calculateInterestToPay","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"indexs","type":"uint256[]"}],"name":"claimCollateralAsBorrower","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"claimCollateralAsLender","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"claimDebt","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"indexOfLender","type":"uint256"}],"name":"createAuctionForCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"extendLoan","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getAuctionData","outputs":[{"components":[{"internalType":"address","name":"auctionAddress","type":"address"},{"internalType":"address","name":"liquidationAddress","type":"address"},{"internalType":"uint256","name":"soldAmount","type":"uint256"},{"internalType":"uint256","name":"tokenPerCollateralUsed","type":"uint256"},{"internalType":"bool","name":"alreadySold","type":"bool"}],"internalType":"struct DebitaV3Loan.AuctionData","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLoanData","outputs":[{"components":[{"internalType":"address","name":"collateral","type":"address"},{"internalType":"address[]","name":"principles","type":"address[]"},{"internalType":"address","name":"valuableCollateralAsset","type":"address"},{"internalType":"bool","name":"isCollateralNFT","type":"bool"},{"internalType":"bool","name":"auctionInitialized","type":"bool"},{"internalType":"bool","name":"extended","type":"bool"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"initialDuration","type":"uint256"},{"internalType":"uint256","name":"borrowerID","type":"uint256"},{"internalType":"uint256","name":"NftID","type":"uint256"},{"internalType":"uint256","name":"collateralAmount","type":"uint256"},{"internalType":"uint256","name":"collateralValuableAmount","type":"uint256"},{"internalType":"uint256","name":"valuableCollateralUsed","type":"uint256"},{"internalType":"uint256","name":"totalCountPaid","type":"uint256"},{"internalType":"uint256[]","name":"principlesAmount","type":"uint256[]"},{"components":[{"internalType":"address","name":"principle","type":"address"},{"internalType":"address","name":"lendOffer","type":"address"},{"internalType":"uint256","name":"principleAmount","type":"uint256"},{"internalType":"uint256","name":"lenderID","type":"uint256"},{"internalType":"uint256","name":"apr","type":"uint256"},{"internalType":"uint256","name":"ratio","type":"uint256"},{"internalType":"uint256","name":"collateralUsed","type":"uint256"},{"internalType":"uint256","name":"maxDeadline","type":"uint256"},{"internalType":"bool","name":"paid","type":"bool"},{"internalType":"bool","name":"collateralClaimed","type":"bool"},{"internalType":"bool","name":"debtClaimed","type":"bool"},{"internalType":"uint256","name":"interestToClaim","type":"uint256"},{"internalType":"uint256","name":"interestPaid","type":"uint256"}],"internalType":"struct DebitaV3Loan.infoOfOffers[]","name":"_acceptedOffers","type":"tuple[]"}],"internalType":"struct DebitaV3Loan.LoanData","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"handleAuctionSell","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_collateral","type":"address"},{"internalType":"address[]","name":"_principles","type":"address[]"},{"internalType":"bool","name":"_isCollateralNFT","type":"bool"},{"internalType":"uint256","name":"_NftID","type":"uint256"},{"internalType":"uint256","name":"_collateralAmount","type":"uint256"},{"internalType":"uint256","name":"_valuableCollateralAmount","type":"uint256"},{"internalType":"uint256","name":"valuableCollateralUsed","type":"uint256"},{"internalType":"address","name":"valuableAsset","type":"address"},{"internalType":"uint256","name":"_initialDuration","type":"uint256"},{"internalType":"uint256[]","name":"_principlesAmount","type":"uint256[]"},{"internalType":"uint256","name":"_borrowerID","type":"uint256"},{"components":[{"internalType":"address","name":"principle","type":"address"},{"internalType":"address","name":"lendOffer","type":"address"},{"internalType":"uint256","name":"principleAmount","type":"uint256"},{"internalType":"uint256","name":"lenderID","type":"uint256"},{"internalType":"uint256","name":"apr","type":"uint256"},{"internalType":"uint256","name":"ratio","type":"uint256"},{"internalType":"uint256","name":"collateralUsed","type":"uint256"},{"internalType":"uint256","name":"maxDeadline","type":"uint256"},{"internalType":"bool","name":"paid","type":"bool"},{"internalType":"bool","name":"collateralClaimed","type":"bool"},{"internalType":"bool","name":"debtClaimed","type":"bool"},{"internalType":"uint256","name":"interestToClaim","type":"uint256"},{"internalType":"uint256","name":"interestPaid","type":"uint256"}],"internalType":"struct DebitaV3Loan.infoOfOffers[]","name":"_acceptedOffers","type":"tuple[]"},{"internalType":"address","name":"m_OwnershipContract","type":"address"},{"internalType":"uint256","name":"feeInterestLender","type":"uint256"},{"internalType":"address","name":"_feeAddress","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"loanData","outputs":[{"internalType":"address","name":"collateral","type":"address"},{"internalType":"address","name":"valuableCollateralAsset","type":"address"},{"internalType":"bool","name":"isCollateralNFT","type":"bool"},{"internalType":"bool","name":"auctionInitialized","type":"bool"},{"internalType":"bool","name":"extended","type":"bool"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"initialDuration","type":"uint256"},{"internalType":"uint256","name":"borrowerID","type":"uint256"},{"internalType":"uint256","name":"NftID","type":"uint256"},{"internalType":"uint256","name":"collateralAmount","type":"uint256"},{"internalType":"uint256","name":"collateralValuableAmount","type":"uint256"},{"internalType":"uint256","name":"valuableCollateralUsed","type":"uint256"},{"internalType":"uint256","name":"totalCountPaid","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nextDeadline","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"indexes","type":"uint256[]"}],"name":"payDebt","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"s_OwnershipContract","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]
Deployed Bytecode
0x60806040526004361015610011575f80fd5b5f803560e01c806310bf6bc4146130d357806314db92ba14612ba1578063192c36351461233e57806320517984146123235780633e681f99146122fa5780634a5908181461225657806360d3f8b51461202d5780636a67acc514611f845780637cad3aa9146118cd57806382f1989614611220578063a1aa707914610ea4578063a3f14e9614610a48578063a46551f414610a21578063b237b173146109d2578063b753cff2146109a95763faa09e77146100ca575f80fd5b34610789578060031936011261078957600154604051906001600160a01b03166100f3826132a2565b6006546001600160a01b03168252604051600780548083525f9182525f805160206143b783398151915291839160208301915b81811061098757505061013b925003826132d9565b602083015260085460018060a01b038116604084015260ff8160a01c161515606084015260ff8160a81c161515608084015260ff8160b01c16151560a084015260095460c0840152600a5460e0840152600b5480610100850152600c54610120850152600d54610140850152600e54610160850152600f546101808501526010546101a085015260405180816020601154928381520160115f525f80516020614397833981519152925f5b81811061096e5750506101fb925003826132d9565b6101c085015260125461020d816132fa565b9061021b60405192836132d9565b8082526020820160125f525f805160206143778339815191525f915b83831061095057505050506101e0850152604051906331a9108f60e11b82526004820152602081602481865afa80156106ca578590610911575b61028691506001600160a01b031633146133e1565b61028e613f57565b4210156108cc5760ff8160b01c166108945760e08301516103e8908181029181830414901517156108805761271090046102cc60c085015142613584565b11156108495760ff60b01b1916600160b01b176008556002546040516339659f4160e11b81526001600160a01b03919091169190602081600481865afa9081156106ca578591610817575b506040516316c9e7ed60e01b815292602084600481845afa9384156107d75786946107e2575b506020600491604051928380926339be6be960e01b82525afa9081156107d75786916107a1575b506103898261038460e08801516001620151805f19830104019015150290565b613390565b8181111561078f575080945b865b6101e082015190888251821015610739576103b3828a9461342a565b51610100810151156103d1575b50506103cc915061341c565b610397565b6103df60c086015142613584565b508861046761271061041f896103f488613eb0565b98836104026003548c613390565b04958d8c898b9585036106d5575b92505050604091500151613390565b0461044160018060a01b03855116610437858a613584565b90309033906142ae565b83516004546001600160a01b039182169291169061045f9085613577565b9133906142ae565b60208201516040516349b1b18760e01b81526001600160a01b039091169290848160048183885af19081156106ca5785916106a8575b50848b602060608501516024604051809481936331a9108f60e11b835260048301525afa879181610668575b50610660575b50602082015115159182610644575b50501561061d5751839190602090610536906001600160a01b0316610503848a613584565b60405163095ea7b360e01b81526001600160a01b0388166004820152602481019190915294859283919082906044820190565b03925af18015610612576105d3575b610550915085613584565b813b156105cf57829160248392604051948593849263be99970560e01b845260048401525af180156105c4576105ac575b50506103cc915b6105a0600a61059684613452565b5001918254613577565b9055879150885f6103c0565b6105b59061325f565b6105c057885f610581565b8880fd5b6040513d84823e3d90fd5b8280fd5b6020823d60201161060a575b816105ec602093836132d9565b810103126106065761060061055092613591565b50610545565b8380fd5b3d91506105df565b6040513d86823e3d90fd5b50846103cc95935061062f9250613584565b61063d600961059685613452565b9055610588565b61010001516001600160a01b0391821691161490505f806104de565b90505f6104cf565b9091506020813d6020116106a0575b81610684602093836132d9565b8101031261069c57610695906133cd565b905f6104c9565b8780fd5b3d9150610677565b6106c491503d8087833e6106bc81836132d9565b810190613662565b5f61049d565b6040513d87823e3d90fd5b61071995506106f5610708939260c060e061038494015191015190613584565b6001620151805f19830104019015150290565b908d8211156107235750508b613584565b8e8e8d8c89610410565b808210610731575b50613584565b90508f61072b565b60025481906001600160a01b0316803b1561078c57818091602460405180948193637372483d60e01b83523060048401525af180156105c4576107795750f35b6107829061325f565b6107895780f35b80fd5b50fd5b94848610156103955794508394610395565b90506020813d6020116107cf575b816107bc602093836132d9565b810103126107cb57515f610364565b5f80fd5b3d91506107af565b6040513d88823e3d90fd5b9093506020813d60201161080f575b816107fe602093836132d9565b810103126107cb575192602061033d565b3d91506107f1565b90506020813d602011610841575b81610832602093836132d9565b810103126107cb57515f610317565b3d9150610825565b60405162461bcd60e51b815260206004820152600f60248201526e4e6f7420656e6f7567682074696d6560881b6044820152606490fd5b634e487b7160e01b85526011600452602485fd5b60405162461bcd60e51b815260206004820152601060248201526f105b1c9958591e48195e1d195b99195960821b6044820152606490fd5b60405162461bcd60e51b815260206004820152601e60248201527f446561646c696e652070617373656420746f20657874656e64206c6f616e00006044820152606490fd5b506020813d602011610948575b8161092b602093836132d9565b810103126109445761093f610286916133cd565b610271565b8480fd5b3d915061091e565b600b602060019261096085613478565b815201920192019190610237565b84548352600194850194869450602090930192016101e6565b84546001600160a01b0316835260019485019486945060209093019201610126565b50346107895780600319360112610789576001546040516001600160a01b039091168152602090f35b503461078957806003193601126107895760a0600180821b03806013541690601454166015546016549060ff601754169260405194855260208501526040840152606083015215156080820152f35b5034610789576020366003190112610789576020610a40600435613eb0565b604051908152f35b503461078957806003193601126107895760606101e0604051610a6a816132a2565b83815282602082015283604082015283838201528360808201528360a08201528360c08201528360e08201528361010082015283610120820152836101408201528361016082015283610180820152836101a0820152826101c0820152015260405190610ad6826132a2565b6006546001600160a01b03168252604051600780548083525f9182525f805160206143b783398151915291839160208301915b818110610e82575050610b1e925003826132d9565b602083015260ff60085460018060a01b0381166040850152818160a01c1615156060850152818160a81c161515608085015260b01c16151560a083015260095460c0830152600a5460e0830152600b54610100830152600c54610120830152600d54610140830152600e54610160830152600f546101808301526010546101a083015260405180816020601154928381520160115f525f80516020614397833981519152925f5b818110610e69575050610bda925003826132d9565b6101c0830152601254610bec816132fa565b90610bfa60405192836132d9565b8082526020820160125f525f805160206143778339815191525f915b838310610e4b57505050506101e08301526040519182916020835260018060a01b03825116602084015260208201519161020060408501526102208401835180915260206102408601940190835b818110610e295750505060018060a01b0360408201511660608501526060810151151560808501526080810151151560a085015260a0810151151560c085015260c081015160e085015260e08101516101008501526101008101516101208501526101208101516101408501526101408101516101608501526101608101516101808501526101808101516101a08501526101a08101516101c08501526101c081015192601f19858203016101e086015260208085519283815201940190835b818110610e10575050506101e0015191601f1984820301610200850152602080845192838152019301915b818110610d5d575050500390f35b9193509160206101a06001926101808751858060a01b038151168352858060a01b0385820151168584015260408101516040840152606081015160608401526080810151608084015260a081015160a084015260c081015160c084015260e081015160e08401526101008101511515610100840152610120810151151561012084015261014081015115156101408401526101608101516101608401520151610180820152019401910191849392610d4f565b8251865287965060209586019590920191600101610d24565b82516001600160a01b0316865287965060209586019590920191600101610c64565b600b6020600192610e5b85613478565b815201920192019190610c16565b8454835260019485019486945060209093019201610bc5565b84546001600160a01b0316835260019485019486945060209093019201610b09565b503461078957602090816003193601126107895760043591610ec4614355565b6001546001600160a01b0390811682610ee5610edf87613452565b50613478565b91606083015190604051958680926331a9108f60e11b94858352600483015260249889915afa80156112155787906111db575b610f2691508516331461381e565b610100928301511561111357610f3a613857565b91610f4f886101e0876001541695015161342a565b519360608501928351604051918252600482015282818981885afa9081156111085789916110c7575b50610f9891610f8d600192893391161461381e565b860151151514613a05565b61014084015161109257506008610faf8798613452565b5001805462ff000019166201000017905551813b1561108e5785918583926040519485938492630852cd8d60e31b845260048401525af19081156106ca578591611075575b50508061101661016061101e9301868151915260408584511693015190613577565b903390614264565b6002541690813b1561107157604051637372483d60e01b81523060048201529183918391829084905af180156105c45761105d575b50505b6001815580f35b6110669061325f565b61078957805f611053565b5050fd5b61107e9061325f565b61108957835f610ff4565b505050fd5b8580fd5b606490600f876040519262461bcd60e51b845260048401528201526e105b1c9958591e4818db185a5b5959608a1b6044820152fd5b90508281813d8311611101575b6110de81836132d9565b810103126105c057610f9891610f8d6110f86001936133cd565b92505091610f78565b503d6110d4565b6040513d8b823e3d90fd5b50949050611123610edf82613452565b906101608201519586156111a1575094611151918560096111448299613452565b5001558333915116614264565b6002541690813b1561107157604051637372483d60e01b81523060048201529183918391829084905af180156105c45761118d575b5050611056565b6111969061325f565b61078957805f611186565b6064906014866040519262461bcd60e51b84526004840152820152734e6f20696e74657265737420746f20636c61696d60601b6044820152fd5b508281813d831161120e575b6111f181836132d9565b8101031261120a57611205610f26916133cd565b610f18565b8680fd5b503d6111e7565b6040513d89823e3d90fd5b5034610789576020366003190112610789576004356001600160401b0381116118c957611251903690600401613311565b611259614355565b60018060a01b036001541690600b54604051906331a9108f60e11b82526004820152602081602481865afa801561061257849061188e575b6112a691506001600160a01b031633146133e1565b60085460a01c60ff16156117625760175460ff16156114a85782805b82518110156113e9576112ec6101e06112d9613857565b01516112e5838661342a565b519061342a565b5190610100611302600182850151151514613a05565b61131161012084015115613b06565b6008611326611320848861342a565b51613452565b50019061ff00198254161790558260e060018060a01b03600654166024600c54604051948593849263359549b360e21b845260048401525af1908115610612576113b59361139761139160c060806113b09661139d968b916113ba575b500151930151601654613390565b91613ab6565b90613559565b60145433906001600160a01b0316614264565b61341c565b6112c2565b6113dc915060e03d60e0116113e2575b6113d481836132d9565b810190613a3c565b5f611383565b503d6113ca565b5091906113fa905b51601854613577565b8060185560125414611460575b506002546001600160a01b0316803b1561078c57818091602460405180948193637372483d60e01b83523060048401525af180156105c45761144c575b506001905580f35b6114559061325f565b61078957805f611444565b600b54813b15611071578291602483926040519485938492630852cd8d60e31b845260048401525af180156105c457156114075761149d9061325f565b61078957805f611407565b9091604051906114b7826132a2565b6006546001600160a01b03168252604051600780548083525f9182525f805160206143b783398151915291839160208301915b8181106117405750506114ff925003826132d9565b602083015260ff60085460018060a01b0381166040850152818160a01c1615156060850152818160a81c161515608085015260b01c16151560a083015260095460c0830152600a5460e0830152600b54610100830152600c54610120830152600d54610140830152600e54610160830152600f546101808301526010546101a083015260405180816020601154928381520160115f525f80516020614397833981519152925f5b8181106117275750506115bb925003826132d9565b6101c0830152601254936115ce856132fa565b946115dc60405196876132d9565b8086526020860160125f525f805160206143778339815191525f915b838310611709575050505061161d6101e08401958087526101a0850151905114613a05565b815b85518051821015611689579061164b6001610100611640846116849661342a565b510151151514613a05565b61166561012061165c838a5161342a565b51015115613b06565b600861167082613452565b5001805461ff00191661010017905561341c565b61161f565b5050825161012090930151919450849390929091906001600160a01b0316803b15610944576040516323b872dd60e01b8152306004820152336024820152604481019290925284908290606490829084905af19081156106125784916116f5575b50506113fa906113f1565b6116fe9061325f565b61107157825f6116ea565b600b602060019261171985613478565b8152019201920191906115f8565b84548352600194850194869450602090930192016115a6565b84546001600160a01b03168352600194850194869450602090930192016114ea565b829081805b8251841015611868576004611782610edf611320878761342a565b91610100611797600182860151151514613a05565b6117a661012085015115613b06565b60086117b5611320898961342a565b5001805461ff001916909117905560065460405163313ce56760e01b81529260209184919082906001600160a01b03165afa918215610612576118339360a061181e61182d95611827948991611839575b5061181860ff60408601519216613ab6565b90613390565b91015190613559565b90613577565b9361341c565b92611767565b61185b915060203d602011611861575b61185381836132d9565b810190613b45565b5f611806565b503d611849565b600654919493506113fa9291611889919033906001600160a01b0316614264565b6113f1565b506020813d6020116118c1575b816118a8602093836132d9565b81010312610606576118bc6112a6916133cd565b611291565b3d915061189b565b5080fd5b5034610789576020366003190112610789576118e7614355565b6040516118f3816132a2565b6006546001600160a01b03168152604051600780548083525f9182525f805160206143b783398151915291839160208301915b818110611f6257505061193b925003826132d9565b602082015260085460018060a01b038116604083015260ff8160a01c161515606083015260ff8160a81c161515608083015260ff8160b01c16151560a083015260095460c0830152600a5460e0830152600b54610100830152600c54610120830152600d54610140830152600e54610160830152600f546101808301526010546101a083015260405180816020601154928381520160115f525f80516020614397833981519152925f5b818110611f495750506119fa925003826132d9565b6101c0830152601254611a0c816132fa565b90611a1a60405192836132d9565b8082526020820160125f525f805160206143778339815191525f915b838310611f2b57505050506101e08301818152611a636060611a5b600435809561342a565b5101516141ea565b611a716101008601516141ea565b6001600160a01b0390911633149283611f0f575b506001600160a01b031633149081611f01575b606085015115611ec4576101a085015190515114611e7f57611ac2611abb613f57565b4211613ac4565b6080840151611e44578115611e3c575b5015611e08576002546040516322e3347d60e21b8152849391602090829060049082906001600160a01b03165afa908115610612578491611dce575b5060ff60a81b19909216600160a81b17600855805161012082015160405163359549b360e21b81526004810191909152936001600160a01b039384169360e09286926024928492165af1928315610612578493611dad575b506040830192848451936040519463da96865d60e01b86526004860152602085602481845afa9485156105c4578295611d76575b508351610120850151906001600160a01b0316803b156106065760405163095ea7b360e01b81526001600160a01b0384166004820152602481019290925283908290604490829084905af18015611d5257611d5d575b509260c49160209460c06101208401519360018060a01b0390511695019660018060a01b038851169851604051998a9788966347ca909960e01b885260048801526024870152604486015260648501526084840152620d2f0060a48401525af1918215611d52578392611d16575b50516040516001600160a01b03909116918390608090611c7d846132be565b60018060a01b03169283815284602082015282604082015282606082015201526001600160601b0360a01b908160135416176013556014541617601455806015558060165560ff196017541660ff82151516176017558060018060a01b0360025416803b1561078c57818091602460405180948193637372483d60e01b83523060048401525af180156105c45761144c57506001905580f35b9091506020813d602011611d4a575b81611d32602093836132d9565b810103126105cf57611d43906133cd565b905f611c5e565b3d9150611d25565b6040513d85823e3d90fd5b91611d6a8195929361325f565b6106065792905f611bf0565b915093506020813d602011611da5575b81611d93602093836132d9565b810103126107cb57859051935f611b9a565b3d9150611d86565b611dc791935060e03d60e0116113e2576113d481836132d9565b915f611b66565b90506020813d602011611e00575b81611de9602093836132d9565b8101031261108957611dfa906133cd565b5f611b0e565b3d9150611ddc565b60405162461bcd60e51b815260206004820152600c60248201526b139bdd081a5b9d9bdb1d995960a21b6044820152606490fd5b90505f611ad2565b60405162461bcd60e51b8152602060048201526013602482015272105b1c9958591e481a5b9a5d1a585b1a5e9959606a1b6044820152606490fd5b60405162461bcd60e51b815260206004820152601760248201527f416c726561647920706169642065766572797468696e670000000000000000006044820152606490fd5b60405162461bcd60e51b815260206004820152601560248201527410dbdb1b185d195c985b081a5cc81b9bdd08139195605a1b6044820152606490fd5b905060018151511190611a98565b610100919350611f2090835161342a565b51015115915f611a85565b600b6020600192611f3b85613478565b815201920192019190611a36565b84548352600194850194869450602090930192016119e5565b84546001600160a01b0316835260019485019486945060209093019201611926565b50346107895780600319360112610789576080604051611fa3816132be565b828152826020820152826040820152826060820152015260a0604051611fc8816132be565b600180831b03908160135416918282528060145416906020830191825260155490604084019182526016549260608501938452608060ff6017541695019415158552604051958652511660208501525160408401525160608301525115156080820152f35b5034610789578060208060031936011261078c576004359061204d614355565b612055613857565b91612065816101e085015161342a565b516001546060820180516040516331a9108f60e11b815260048101919091526001600160a01b039692871691908681602481865afa8015611108578990612220575b6120b591508816331461381e565b51813b1561069c578791602483926040519485938492630852cd8d60e31b845260048401525af190811561121557879161220c575b505060606120f6613f57565b9161211b6101009361210b858701511561351e565b4281109081612202575b50613ac4565b61212a61012085015115613b06565b01511561217a57505061213d9150613b5e565b505b60025416803b1561078c57818091602460405180948193637372483d60e01b83523060048401525af180156105c45761144c57506001905580f35b9091612187600891613452565b5001805461ff001916909117905560065460405163313ce56760e01b8152908416918382600481865afa9182156107d75761181e6110169360a0926121e0978a926121e5575b505061181860ff60408601519216613ab6565b61213f565b6121fb9250803d106118615761185381836132d9565b5f806121cd565b905015155f612115565b6122159061325f565b61108e57855f6120ea565b508681813d831161224f575b61223681836132d9565b810103126105c05761224a6120b5916133cd565b6120a7565b503d61222c565b50346107895780600319360112610789576101a060018060a01b03806006541690600854600954600a54600b54600c5490600d5492600e549460ff600f5497601054996040519b8c52811660208c0152818160a01c16151560408c0152818160a81c16151560608c015260b01c16151560808a015260a089015260c088015260e0870152610100860152610120850152610140840152610160830152610180820152f35b50346107895780600319360112610789576002546040516001600160a01b039091168152602090f35b50346107895780600319360112610789576020610a40613f57565b5034610789576101e036600319011261078957600435906001600160a01b03821682036107cb576024356001600160401b0381116118c957366023820112156118c957806004013590612390826132fa565b9061239e60405192836132d9565b828252602082016024819460051b8301019136831161108e57602401905b828210612b89575050506044359182151583036107cb5760e435906001600160a01b03821682036107cb57610124356001600160401b03811161108e57612407903690600401613311565b926001600160401b03610164351161108e573660236101643501121561108e57610164356004013596612439886132fa565b97612447604051998a6132d9565b80895260208901903660246101a08302610164350101116105c05760246101643501915b60246101a083026101643501018310612ab5575050610184359890506001600160a01b03891689036107cb576101c435956001600160a01b03871687036107cb575f805160206143d783398151915254976001600160401b0389161580612aa7575b60016001600160401b038b16149081612a9d575b159081612a94575b50612a825760016001600160401b03198a16175f805160206143d78339815191525560ff8960401c1615612a56575b612520614355565b601e83511015612a1f5760405196612537886132a2565b6001600160a01b039485168089526020890187905294166040880152151560608701526080860189905260a086018990524260c08701526101043560e08701526101443561010087015260643561012087015260843561014087015260a43561016087015260c4356101808701526101a086018990526101c08601526101e0850152600680546001600160a01b031916909117905551906001600160401b0382116129ba57600160401b82116129ba57600754826007558083106129f8575b5060078652855b8281106129ce5750505060018060a01b036040820151166008549060ff60a01b6060840151151560a01b1660ff60a81b6080850151151560a81b169160ff60b01b60a0860151151560b01b169368ffffffffffffffffff60b81b161717171760085560c081015160095560e0810151600a55610100810151600b55610120810151600c55610140810151600d55610160810151600e55610180810151600f556101a08101516010556101c08101518051906001600160401b0382116129ba57600160401b82116129ba5760209060115483601155808410612993575b500160118652855b828110612972575050506101e0015193845194600160401b861161295e57601254866012558087106128c2575b5060200194601285525f805160206143778339815191529585905b8282106127dd57600180546001600160a01b038681166001600160a01b03199283161783556101a435600355600280543390841617905560048054918916919092161790554360055587558660ff604088901c16156127855780f35b68ff0000000000000000195f805160206143d783398151915254165f805160206143d7833981519152557fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d2602060405160018152a180f35b600b60208261018060019451858060a01b038151168d6001600160601b0360a01b905416178d55858d01868060a01b0385830151166001600160601b0360a01b8254161790558c600260408301519101558c600360608301519101558c600460808301519101558c600560a08301519101558c600660c08301519101558c600760e083015191015560088d016101008201511515815461ff00610120850151151560081b169060ff62ff0000610140870151151560101b1693169062ffffff191617171790558c60096101608301519101550151600a8c015501980191019096612729565b600b81810204810361294a57600b87810204870361294a575f8051602061437783398151915287600b0281015b82600b028201811061290257505061270e565b8088600b925588600182015588600282015588600382015588600482015588600582015588600682015588600782015588600882015588600982015588600a820155016128ef565b634e487b7160e01b86526011600452602486fd5b634e487b7160e01b85526041600452602485fd5b60019060208351930192815f805160206143978339815191520155016126e1565b6129b49060115f52845f8051602061439783398151915291820191016133b7565b5f6126d9565b634e487b7160e01b86526041600452602486fd5b81516001600160a01b03165f805160206143b78339815191528201556020909101906001016125fd565b612a199060075f52835f805160206143b783398151915291820191016133b7565b5f6125f6565b60405162461bcd60e51b815260206004820152600f60248201526e546f6f206d616e79206f666665727360881b6044820152606490fd5b68ffffffffffffffffff19891668010000000000000001175f805160206143d783398151915255612518565b60405163f92ee8a960e01b8152600490fd5b9050155f6124e9565b303b1591506124e1565b5060ff8960401c16156124cd565b6101a083360312612b85576101a0806020602493604051612ad581613286565b612ade8861336f565b8152612aeb83890161336f565b8382015260408801356040820152606080890135908201526080808901359082015260a0808901359082015260c0808901359082015260e088013560e0820152610100612b39818a01613383565b90820152610120612b4b818a01613383565b90820152610140612b5d818a01613383565b908201526101608089013590820152610180808901359082015281520194019391505061246b565b8980fd5b60208091612b968461336f565b8152019101906123bc565b5034610789576020366003190112610789576004356001600160401b0381116118c957612bd2903690600401613311565b612bda614355565b60018060a01b03600154169160055443146130a157600b54604051906331a9108f60e11b82526004820152602081602481875afa80156105c4578290613066575b612c3091506001600160a01b031633146133e1565b612c38613f57565b421161302157805b8251811015612fb757612c53818461342a565b51612c60610edf82613452565b906008612c6c82613452565b5001600160ff19825416179055612c886101008301511561351e565b4260e08301511115612f8057612c9d81613eb0565b91612710612cad60035485613390565b0490612cc682612cc1866040850151613577565b613584565b869089602060608501516024604051809481936331a9108f60e11b835260048301525afa899181612f44575b50612f3c575b5060208301516040516349b1b18760e01b81526001600160a01b0390911692898260048183885af1918215612ec5578a92612f20575b50612d4483303360018060a01b038951166142ae565b602082015115159182612f04575b505015612ed057612d6285613452565b50908860098093015492612d7588613452565b5001556008612d8387613452565b5001805462ff000019166201000017905583518990602090612de5906001600160a01b0316612db28686613577565b60405163095ea7b360e01b81526001600160a01b0389166004820152602481019190915293849283919082906044820190565b03925af18015612ec557612e86575b5090612dff91613577565b813b1561069c57879160248392604051948593849263be99970560e01b845260048401525af1801561121557908791612e72575b5050610596612e6d959493612e61612e6694600a945b516004546001600160a01b03908116913391166142ae565b613452565b905561341c565b612c40565b612e7b9061325f565b61108e57855f612e33565b6020813d602011612ebd575b81612e9f602093836132d9565b81010312612b855790612eb5612dff9392613591565b509091612df4565b3d9150612e92565b6040513d8c823e3d90fd5b50506105968493612e6184600a94612eef612e6697612e6d9b9a613584565b612efd600961059687613452565b9055612e49565b61010001516001600160a01b0391821691161490505f80612d52565b612f359192503d808c833e6106bc81836132d9565b905f612d2e565b91505f612cf8565b9091506020813d602011612f78575b81612f60602093836132d9565b81010312612b8557612f71906133cd565b905f612cf2565b3d9150612f53565b60405162461bcd60e51b815260206004820152600f60248201526e111958591b1a5b99481c185cdcd959608a1b6044820152606490fd5b50612fc58251601054613577565b60105560025481906001600160a01b0316803b1561078c57818091602460405180948193637372483d60e01b83523060048401525af180156105c45761300d57506001905580f35b6130169061325f565b610789578082611444565b60405162461bcd60e51b815260206004820152601b60248201527f446561646c696e652070617373656420746f20706179204465627400000000006044820152606490fd5b506020813d602011613099575b81613080602093836132d9565b810103126118c957613094612c30916133cd565b612c1b565b3d9150613073565b60405162461bcd60e51b815260206004820152600a60248201526953616d6520626c6f636b60b01b6044820152606490fd5b5034610789576020366003190112610789576004356130f0614355565b6013546001600160a01b0390811633036132235760ff601754166131ef5782918260e061311b613857565b602461012086835116920151604051948593849263359549b360e21b845260048401525af19182156106125761316360806131839461317a9488916131d0575b500151613ab6565b9080601555600160ff196017541617601755613390565b600f5490613559565b60165560025416803b1561078c578190602460405180948193637372483d60e01b83523060048401525af180156105c4576131c1575b506001815580f35b6131ca9061325f565b5f6131b9565b6131e9915060e03d60e0116113e2576113d481836132d9565b5f61315b565b60405162461bcd60e51b815260206004820152600c60248201526b105b1c9958591e481cdbdb1960a21b6044820152606490fd5b60405162461bcd60e51b8152602060048201526014602482015273139bdd08185d58dd1a5bdb8818dbdb9d1c9858dd60621b6044820152606490fd5b6001600160401b03811161327257604052565b634e487b7160e01b5f52604160045260245ffd5b6101a081019081106001600160401b0382111761327257604052565b61020081019081106001600160401b0382111761327257604052565b60a081019081106001600160401b0382111761327257604052565b90601f801991011681019081106001600160401b0382111761327257604052565b6001600160401b0381116132725760051b60200190565b81601f820112156107cb57803591613328836132fa565b9261333660405194856132d9565b808452602092838086019260051b8201019283116107cb578301905b828210613360575050505090565b81358152908301908301613352565b35906001600160a01b03821682036107cb57565b359081151582036107cb57565b818102929181159184041417156133a357565b634e487b7160e01b5f52601160045260245ffd5b8181106133c2575050565b5f81556001016133b7565b51906001600160a01b03821682036107cb57565b156133e857565b60405162461bcd60e51b815260206004820152600c60248201526b2737ba103137b93937bbb2b960a11b6044820152606490fd5b5f1981146133a35760010190565b805182101561343e5760209160051b010190565b634e487b7160e01b5f52603260045260245ffd5b60125481101561343e57600b9060125f52025f8051602061437783398151915201905f90565b9060405161348581613286565b610180600a829460018060a01b038082541685526001820154166020850152600281015460408501526003810154606085015260048101546080850152600581015460a0850152600681015460c0850152600781015460e085015260ff60088201548181161515610100870152818160081c16151561012087015260101c16151561014085015260098101546101608501520154910152565b1561352557565b60405162461bcd60e51b815260206004820152600c60248201526b105b1c9958591e481c185a5960a21b6044820152606490fd5b8115613563570490565b634e487b7160e01b5f52601260045260245ffd5b919082018092116133a357565b919082039182116133a357565b519081151582036107cb57565b81601f820112156107cb578051916135b5836132fa565b926135c360405194856132d9565b808452602092838086019260051b8201019283116107cb578301905b8282106135ed575050505090565b815181529083019083016135df565b81601f820112156107cb57805191613613836132fa565b9261362160405194856132d9565b808452602092838086019260051b8201019283116107cb578301905b82821061364b575050505090565b838091613657846133cd565b81520191019061363d565b60209182828203126107cb5781516001600160401b03928382116107cb570191610200838303126107cb576040519361369a856132a2565b6136a3846133cd565b85526136b0818501613591565b818601526136c060408501613591565b604086015260608401518281116107cb5784019083601f830112156107cb578151916136eb836132fa565b926136f960405194856132d9565b808452828085019160051b830101918683116107cb578301905b8282106138075750505050606085015260808301518181116107cb578261373b91850161359e565b608085015260a083015160a085015260c083015160c085015260e083015160e085015261010061376c8185016133cd565b9085015261012061377e8185016133cd565b90850152610140808401518281116107cb578361379c9186016135fc565b90850152610160808401518281116107cb57836137ba9186016135fc565b9085015261018091828401519182116107cb576137d891840161359e565b908301526101a06137ea8183016133cd565b908301526101c080820151908301526101e0809101519082015290565b83809161381384613591565b815201910190613713565b1561382557565b60405162461bcd60e51b815260206004820152600a6024820152692737ba103632b73232b960b11b6044820152606490fd5b60405190613864826132a2565b6006546001600160a01b039081168352604051600780548083525f91825260209386939091858201905f805160206143b783398151915290855b888282106139ef5750505050906138ba8160ff949303826132d9565b858501526008549081166040850152818160a01c1615156060850152818160a81c161515608085015260b01c16151560a083015260095460c0830152600a5460e0830152600b8054610100840152600c54610120840152600d54610140840152600e54610160840152600f546101808401526010546101a08401526040518081866011549283815201601186525f8051602061439783398151915292865b898282106139d95750505061396f925003826132d9565b6101c084015260125491613982836132fa565b9461399060405196876132d9565b838652601282525f805160206143778339815191528187015b8584106139bd575050505050506101e00152565b84836001926139cb85613478565b8152019201930192906139a9565b8554845260019586019587955093019201613958565b835487168552909301926001928301920161389e565b15613a0c57565b60405162461bcd60e51b8152602060048201526008602482015267139bdd081c185a5960c21b6044820152606490fd5b908160e09103126107cb576040519060e082018281106001600160401b0382111761327257613aae9160c0916040528051845260208101516020850152604081015160408501526060810151606085015260808101516080850152613aa360a082016133cd565b60a0850152016133cd565b60c082015290565b604d81116133a357600a0a90565b15613acb57565b60405162461bcd60e51b8152602060048201526013602482015272111958591b1a5b99481b9bdd081c185cdcd959606a1b6044820152606490fd5b15613b0d57565b60405162461bcd60e51b815260206004820152601060248201526f105b1c9958591e48195e1958dd5d195960821b6044820152606490fd5b908160209103126107cb575160ff811681036107cb5790565b906040805192613b6d846132a2565b60018060a01b0391826006541685528051809560075496878352602080930197815f9960078b525f805160206143b7833981519152928b5b87828210613e9857505050613bbc925003826132d9565b82820152600854908582168482015260ff8260a01c161515606082015260ff6080820192818160a81c161515845260b01c16151560a082015260095460c0820152600a5460e0820152600b8054926101009384840152600c54936101208401948552600d54610140850152600e54610160850152600f546101808501526010546101a085015286518b81809289601154918281520190601184525f80516020614397833981519152935b8b828210613e8257505050613c7d925003826132d9565b6101c0850152601254613c8f816132fa565b93613c9c895195866132d9565b81855260128d528c5f80516020614377833981519152898088015b858410613e64575050505050506008613ce2613cdb8a6101e088019680885261342a565b5199613452565b5001805461ff001916909117905551159081613dd0575050505060ff6017541615613d9857508460e084600654166024600c548551948593849263359549b360e21b845260048401525af1918215613d8f57506113916080613d60949361139793613d6c989991613d71575b5001519260c060165491015190613390565b90601454163390614264565b600190565b613d89915060e03d81116113e2576113d481836132d9565b5f613d4e565b513d87823e3d90fd5b606491519062461bcd60e51b8252600482015260136024820152722737ba1039b7b6321037b71030bab1ba34b7b760691b6044820152fd5b9092955060019193509796979593955151149081613e5c575b50613df5575050505090565b51169051813b156106065782516323b872dd60e01b81523060048201523360248201526044810191909152929081908490606490829084905af1918215613e52575050613e43575b50600190565b613e4c9061325f565b5f613e3d565b51903d90823e3d90fd5b90505f613de9565b6001918591613e7285613478565b8152019201920191908a90613cb7565b8554845260019586019587955093019201613c66565b85548c16845260019586019587955093019201613ba5565b613ebf610edf613ed592613452565b6040810151612710928391608084015190613390565b049160095492613ee58442613584565b91600a54916103e8928381029381850414901517156133a357613f3a9561018094613f1f613f30946301e1338096049260e0890151613584565b9182821115613f3d57505090613390565b0491015190613584565b90565b81819294935010613f4f575b50613390565b91505f613f49565b5f90604051613f65816132a2565b60018060a01b0390816006541681526040519081600754808452816020809501600789525f805160206143b783398151915292895b878282106141d257505050613fb1925003826132d9565b82820152600854928316604082015260ff8360a01c161515606082015260ff8360a81c161515608082015260ff60a082019360b01c161515835260095460c08201908152600a549060e094858401928352600b938454946101009586830152600c54610120830152600d54610140830152600e54610160830152600f546101808301526010546101a08301526040518a8180928a601154918281520190601184525f80516020614397833981519152935b8c8282106141bc57505050614079925003826132d9565b6101c08301526012549061408c826132fa565b9761409a604051998a6132d9565b82895260128c528b905f80516020614377833981519152818b015b8584106141a0575050505050506101e0019485525115155f1461418c5750505f945b825180518710156141845782878315928361416d575b5050505f14614115575061410f8361410687855161342a565b5101519561341c565b946140d7565b948161412282855161342a565b5101511580614156575b61413a575b61410f9061341c565b945061410f8361414b87855161342a565b510151959050614131565b508361416382855161342a565b510151861161412c565b61417892935061342a565b5101511582875f6140ed565b509450505050565b9250925050613f3a92935051905190613577565b84836001926141ae85613478565b8152019201930192906140b5565b8554845260019586019587955093019201614062565b85548a16845260019586019587955093019201613f9a565b6001546040516331a9108f60e11b81526004810192909252602090829060249082906001600160a01b03165afa5f9181614229575b50613f3a57505f90565b90916020823d821161425c575b81614243602093836132d9565b810103126107895750614255906133cd565b905f61421f565b3d9150614236565b60405163a9059cbb60e01b60208201526001600160a01b03909216602483015260448201929092526142ac916142a782606481015b03601f1981018452836132d9565b6142f0565b565b6040516323b872dd60e01b60208201526001600160a01b03928316602482015292909116604483015260648201929092526142ac916142a78260848101614299565b905f602091828151910182855af11561434a575f513d61434157506001600160a01b0381163b155b61431f5750565b604051635274afe760e01b81526001600160a01b039091166004820152602490fd5b60011415614318565b6040513d5f823e3d90fd5b60025f54146143645760025f55565b604051633ee5aeb560e01b8152600490fdfebb8a6a4669ba250d26cd7a459eca9d215f8307e33aebe50379bc5a3617ec344431ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68a66cc928b5edb82af9bd49922954155ab7b0942694bea4ce44661d9a8736c688f0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00a264697066735822122071cf381ef959f7af52855afa1a983776f1590b62cbfd84ee684f8985c74d2b1e64736f6c63430008140033
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 31 Chains
Chain | Token | Portfolio % | Price | Amount | Value |
---|
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.