Skip to content
Verified contractExactMIT

OfferLibrary

Address0xe523e37e05d27408eff49034baf76f0cb2b0f2dc
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.23

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.23+commit.f704f362
Language
solidity
Optimization
Yes, 200 runs
EVM version
shanghai
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
24,384,809
Creation transaction
0x738e233a3d…563b40aa18

118 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/market/libraries/OfferLibrary.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {UserCopyLimitOrderConfigs} from "@src/market/SizeStorage.sol";

import {State} from "@src/market/SizeStorage.sol";
import {ISize} from "@src/market/interfaces/ISize.sol";
import {Errors} from "@src/market/libraries/Errors.sol";
import {Math} from "@src/market/libraries/Math.sol";
import {
    VariablePoolBorrowRateParams, YieldCurve, YieldCurveLibrary
} from "@src/market/libraries/YieldCurveLibrary.sol";

struct LimitOrder {
    // The maximum due date of the limit order
    // Since the yield curve is defined in relative terms, users can protect themselves by
    //   setting a maximum timestamp for a loan to be matched
    uint256 maxDueDate;
    // The yield curve in relative terms
    YieldCurve curveRelativeTime;
}

struct CopyLimitOrderConfig {
    // the minimum tenor of the copied offer
    uint256 minTenor;
    // the maximum tenor of the copied offer
    uint256 maxTenor;
    // the minimum APR of the copied offer
    uint256 minAPR;
    // the maximum APR of the copied offer
    uint256 maxAPR;
    // the offset APR relative to the copied offer
    int256 offsetAPR;
}

/// @title OfferLibrary
/// @custom:security-contact [email protected]
/// @author Size (https://size.credit/)
library OfferLibrary {
    using YieldCurveLibrary for YieldCurve;

    /// @notice Check if the limit order is null
    /// @param self The limit order
    /// @return True if the limit order is null, false otherwise
    function isNull(LimitOrder memory self) internal pure returns (bool) {
        return self.maxDueDate == 0 && self.curveRelativeTime.isNull();
    }

    /// @notice Check if the copy limit order is null
    /// @param self The copy limit order
    /// @return True if the copy limit order is null, false otherwise
    function isNull(CopyLimitOrderConfig memory self) internal pure returns (bool) {
        return self.minTenor == 0 && self.maxTenor == 0 && self.minAPR == 0 && self.maxAPR == 0 && self.offsetAPR == 0;
    }

    /// @notice Validate the limit order
    /// @param self The limit order
    /// @param minTenor The minimum tenor
    /// @param maxTenor The maximum tenor
    function validateLimitOrder(LimitOrder memory self, uint256 minTenor, uint256 maxTenor) internal view {
        // validate maxDueDate
        if (self.maxDueDate == 0) {
            revert Errors.NULL_MAX_DUE_DATE();
        }
        if (self.maxDueDate < block.timestamp + minTenor) {
            revert Errors.PAST_MAX_DUE_DATE(self.maxDueDate);
        }

        // validate curveRelativeTime
        YieldCurveLibrary.validateYieldCurve(self.curveRelativeTime, minTenor, maxTenor);
    }

    /// @notice Validate the copy limit order configs
    /// @param copyLoanOfferConfig The copy loan offer config
    /// @param copyBorrowOfferConfig The copy borrow offer config
    /// @dev assert min <= max and no arbitrageable setup
    function validateCopyLimitOrderConfigs(
        CopyLimitOrderConfig memory copyLoanOfferConfig,
        CopyLimitOrderConfig memory copyBorrowOfferConfig
    ) internal pure {
        if (copyLoanOfferConfig.minTenor > copyLoanOfferConfig.maxTenor) {
            revert Errors.INVALID_TENOR_RANGE(copyLoanOfferConfig.minTenor, copyLoanOfferConfig.maxTenor);
        }
        if (copyLoanOfferConfig.minAPR > copyLoanOfferConfig.maxAPR) {
            revert Errors.INVALID_APR_RANGE(copyLoanOfferConfig.minAPR, copyLoanOfferConfig.maxAPR);
        }
        if (copyBorrowOfferConfig.minTenor > copyBorrowOfferConfig.maxTenor) {
            revert Errors.INVALID_TENOR_RANGE(copyBorrowOfferConfig.minTenor, copyBorrowOfferConfig.maxTenor);
        }
        if (copyBorrowOfferConfig.minAPR > copyBorrowOfferConfig.maxAPR) {
            revert Errors.INVALID_APR_RANGE(copyBorrowOfferConfig.minAPR, copyBorrowOfferConfig.maxAPR);
        }
        if (
            copyBorrowOfferConfig.minAPR > copyLoanOfferConfig.maxAPR
                && (copyBorrowOfferConfig.maxTenor >= copyLoanOfferConfig.minTenor)
                && (copyLoanOfferConfig.maxTenor >= copyBorrowOfferConfig.minTenor)
        ) {
            revert Errors.INVALID_OFFER_CONFIGS(
                copyBorrowOfferConfig.minTenor,
                copyBorrowOfferConfig.maxTenor,
                copyBorrowOfferConfig.minAPR,
                copyBorrowOfferConfig.maxAPR,
                copyLoanOfferConfig.minTenor,
                copyLoanOfferConfig.maxTenor,
                copyLoanOfferConfig.minAPR,
                copyLoanOfferConfig.maxAPR
            );
        }
    }

    function getUserDefinedBorrowOfferAPR(State storage state, address user, uint256 tenor)
        internal
        view
        returns (uint256 apr)
    {
        return getUserDefinedLimitOrderAPR(state, user, state.data.users[user].borrowOffer, tenor);
    }

    function getUserDefinedLoanOfferAPR(State storage state, address user, uint256 tenor)
        internal
        view
        returns (uint256 apr)
    {
        return getUserDefinedLimitOrderAPR(state, user, state.data.users[user].loanOffer, tenor);
    }

    function getUserDefinedLimitOrderAPR(State storage state, address user, LimitOrder memory limitOrder, uint256 tenor)
        internal
        view
        returns (uint256 apr)
    {
        if (tenor == 0) {
            revert Errors.NULL_TENOR();
        }
        if (isNull(limitOrder)) {
            revert Errors.INVALID_OFFER(user);
        }
        if (block.timestamp + tenor > limitOrder.maxDueDate) {
            revert Errors.DUE_DATE_GREATER_THAN_MAX_DUE_DATE(block.timestamp + tenor, limitOrder.maxDueDate);
        }
        VariablePoolBorrowRateParams memory params = VariablePoolBorrowRateParams({
            variablePoolBorrowRate: state.oracle.variablePoolBorrowRate,
            variablePoolBorrowRateUpdatedAt: state.oracle.variablePoolBorrowRateUpdatedAt,
            variablePoolBorrowRateStaleRateInterval: state.oracle.variablePoolBorrowRateStaleRateInterval
        });
        return limitOrder.curveRelativeTime.getAPR(params, tenor);
    }

    /// @notice Get the APR by tenor of a loan offer
    /// @param state The state
    /// @param user The user
    /// @param collectionId The collection id
    /// @param rateProvider The rate provider
    /// @param tenor The tenor
    /// @return apr The APR
    function getLoanOfferAPR(
        State storage state,
        address user,
        uint256 collectionId,
        address rateProvider,
        uint256 tenor
    ) public view returns (uint256 apr) {
        return state.data.sizeFactory.getLoanOfferAPR(user, collectionId, ISize(address(this)), rateProvider, tenor);
    }

    /// @notice Get the absolute rate per tenor of a loan offer
    /// @param state The state
    /// @param user The user
    /// @param collectionId The collection id
    /// @param rateProvider The rate provider
    /// @param tenor The tenor
    /// @return ratePerTenor The absolute rate
    function getLoanOfferRatePerTenor(
        State storage state,
        address user,
        uint256 collectionId,
        address rateProvider,
        uint256 tenor
    ) internal view returns (uint256 ratePerTenor) {
        uint256 apr = getLoanOfferAPR(state, user, collectionId, rateProvider, tenor);
        ratePerTenor = Math.aprToRatePerTenor(apr, tenor);
    }

    /// @notice Get the APR by tenor of a borrow offer
    /// @param state The state
    /// @param user The user
    /// @param collectionId The collection id
    /// @param rateProvider The rate provider
    /// @param tenor The tenor
    /// @return apr The APR
    function getBorrowOfferAPR(
        State storage state,
        address user,
        uint256 collectionId,
        address rateProvider,
        uint256 tenor
    ) public view returns (uint256 apr) {
        return state.data.sizeFactory.getBorrowOfferAPR(user, collectionId, ISize(address(this)), rateProvider, tenor);
    }

    /// @notice Get the absolute rate per tenor of a borrow offer
    /// @param state The state
    /// @param user The user
    /// @param collectionId The collection id
    /// @param rateProvider The rate provider
    /// @param tenor The tenor
    /// @return ratePerTenor The absolute rate
    function getBorrowOfferRatePerTenor(
        State storage state,
        address user,
        uint256 collectionId,
        address rateProvider,
        uint256 tenor
    ) internal view returns (uint256 ratePerTenor) {
        uint256 apr = getBorrowOfferAPR(state, user, collectionId, rateProvider, tenor);
        ratePerTenor = Math.aprToRatePerTenor(apr, tenor);
    }

    function isBorrowAPRLowerThanLoanOfferAPRs(State storage state, address user, uint256 borrowAPR, uint256 tenor)
        internal
        view
        returns (bool)
    {
        return state.data.sizeFactory.isBorrowAPRLowerThanLoanOfferAPRs(user, borrowAPR, ISize(address(this)), tenor);
    }

    function isLoanAPRGreaterThanBorrowOfferAPRs(State storage state, address user, uint256 loanAPR, uint256 tenor)
        internal
        view
        returns (bool)
    {
        return state.data.sizeFactory.isLoanAPRGreaterThanBorrowOfferAPRs(user, loanAPR, ISize(address(this)), tenor);
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "shanghai",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "@chainlink/=lib/chainlink/",
    "@src/=src/",
    "@test/=test/",
    "@deprecated/=deprecated/",
    "@script/=script/",
    "@solady/=lib/solady/",
    "@chimera/=lib/chimera/src/",
    "@crytic/properties/=lib/properties/",
    "@aave/=lib/aave-v3-core/contracts/",
    "halmos-cheatcodes/=lib/halmos-cheatcodes/src/",
    "@uniswap/v3-core/=lib/v3-core/",
    "@uniswap/v3-periphery/=lib/v3-periphery/",
    "@pendle/=lib/pendle-core-v2-public/",
    "@tenderly-utils/=lib/tenderly-utils/src/",
    "@safe-utils/=lib/safe-utils/src/",
    "safe-smart-account/=lib/safe-utils/lib/safe-smart-account/contracts/",
    "@solidity-http/=lib/solidity-http/src/",
    "@ERC-7540-Reference/=lib/ERC-7540-Reference/",
    "@solmate/=lib/solmate/",
    "solmate/mixins/=lib/solmate/src/tokens/",
    "solmate/tokens/=lib/solmate/src/tokens/",
    "solmate/utils/=lib/solmate/src/utils/",
    "solmate/auth/=lib/solmate/src/auth/",
    "halmos-helpers-lib/=lib/halmos-helpers-lib/src/",
    "ERC-7540-Reference/=lib/ERC-7540-Reference/src/",
    "ERC4626/=lib/properties/lib/ERC4626/contracts/",
    "aave-v3-core/=lib/aave-v3-core/",
    "chainlink/=lib/chainlink/contracts/",
    "chimera/=lib/chimera/src/",
    "ds-test/=lib/solmate/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "pendle-core-v2-public/=lib/pendle-core-v2-public/contracts/",
    "properties/=lib/properties/contracts/",
    "safe-utils/=lib/safe-utils/src/",
    "solady/=lib/solady/",
    "solidity-http/=lib/solidity-http/src/",
    "solidity-stringutils/=lib/solidity-http/lib/solidity-stringutils/",
    "tenderly-utils/=lib/tenderly-utils/src/",
    "v3-core/=lib/v3-core/contracts/",
    "v3-periphery/=lib/v3-periphery/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.