Skip to content
Verified contractExactMIT

Config

Address0x566a2c83d6edcf89bc85a66ab0e0619b02c03366
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.30

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 100 runs
EVM version
prague
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,338,854
Creation transaction
0xdf33c375f0…7e9dfb3050

contracts/core/Config.sol

// SPDX-License-Identifier: MIT
//
//   Constant Finance — fixed-term, order-book lending on Ethereum
//   App:  https://app.constant.finance/en
//   X:    https://x.com/constant_fi
//
pragma solidity ^0.8.29;

import {TokenAmount} from "../utils/Types.sol";
import {IConfig} from "../interfaces/IConfig.sol";

/**
 * @title Config
 * @notice 管理抵押品配置的合约,包括白名单和LTV设置
 * @dev 配置在构造函数中设置后不可更改
 */
contract Config is IConfig {
    string public constant x = "https://x.com/constant_fi";
    // ERC20 抵押品配置
    struct ERC20CollateralConfig {
        bool whitelisted;
        uint32 ltv; // Loan-to-Value ratio (basis points, e.g., 8000 = 80%)
        uint32 liquidationThreshold; // 清算阈值
        uint32 liquidationBonusRate; // 清算奖励率
    }

    // NFT 抵押品配置
    struct NFTCollateralConfig {
        bool whitelisted;
        uint32 ltv; // Loan-to-Value ratio
        uint32 liquidationThreshold; // 清算阈值
        uint32 liquidationBonusRate; // 清算奖励率
    }

    // 用于构造函数参数的结构体
    struct ERC20ConfigParam {
        address token;
        bool whitelisted;
        uint32 ltv;
        uint32 liquidationThreshold;
        uint32 liquidationBonusRate;
    }

    struct NFTConfigParam {
        bytes32 collateralKey;
        bool whitelisted;
        uint32 ltv;
        uint32 liquidationThreshold;
        uint32 liquidationBonusRate;
    }

    struct FeeRateConfigParam {
        address token;
        uint32 feeRate;
    }

    // 存储配置
    mapping(address => ERC20CollateralConfig) public erc20CollateralConfigs;
    mapping(bytes32 => NFTCollateralConfig) public nftCollateralConfigs;

    // 手续费配置
    mapping(address => uint32) public feeRates; // token => feeRate (6位精度, 1000000 = 100%)
    mapping(address => uint32) public gasAmounts; // token => gas 手续费数量
    address public override feeCollector; // 手续费接收地址

    // 事件
    event ERC20CollateralConfigSet(
        address indexed token,
        bool whitelisted,
        uint32 ltv,
        uint32 liquidationThreshold,
        uint32 liquidationBonusRate
    );

    event NFTCollateralConfigSet(
        bytes32 indexed collateralKey,
        bool whitelisted,
        uint32 ltv,
        uint32 liquidationThreshold,
        uint32 liquidationBonusRate
    );

    /**
     * @notice 构造函数,初始化所有抵押品配置
     * @param erc20Configs ERC20 抵押品配置数组
     * @param nftConfigs NFT 抵押品配置数组
     * @param feeRateConfigs 手续费率配置数组
     * @param gasConfigs 各 token 的 gas 手续费数量配置
     * @param _feeCollector 手续费接收地址
     */
    constructor(
        ERC20ConfigParam[] memory erc20Configs,
        NFTConfigParam[] memory nftConfigs,
        FeeRateConfigParam[] memory feeRateConfigs,
        TokenAmount[] memory gasConfigs,
        address _feeCollector
    ) {
        // 初始化 ERC20 配置
        for (uint256 i = 0; i < erc20Configs.length; i++) {
            ERC20ConfigParam memory param = erc20Configs[i];
            require(param.token != address(0), "Invalid token address");
            require(param.ltv <= 10000, "LTV cannot exceed 100%");
            require(
                param.liquidationThreshold <= 10000,
                "Liquidation threshold cannot exceed 100%"
            );
            require(
                param.liquidationBonusRate <= 10000,
                "Liquidation bonus rate cannot exceed 100%"
            );

            erc20CollateralConfigs[param.token] = ERC20CollateralConfig({
                whitelisted: param.whitelisted,
                ltv: param.ltv,
                liquidationThreshold: param.liquidationThreshold,
                liquidationBonusRate: param.liquidationBonusRate
            });

            emit ERC20CollateralConfigSet(
                param.token,
                param.whitelisted,
                param.ltv,
                param.liquidationThreshold,
                param.liquidationBonusRate
            );
        }

        // 初始化 NFT 配置
        for (uint256 i = 0; i < nftConfigs.length; i++) {
            NFTConfigParam memory param = nftConfigs[i];
            require(
                param.collateralKey != bytes32(0),
                "Invalid NFT collateral key"
            );
            require(param.ltv <= 10000, "LTV cannot exceed 100%");
            require(
                param.liquidationThreshold <= 10000,
                "Liquidation threshold cannot exceed 100%"
            );
            require(
                param.liquidationBonusRate <= 10000,
                "Liquidation bonus rate cannot exceed 100%"
            );

            nftCollateralConfigs[param.collateralKey] = NFTCollateralConfig({
                whitelisted: param.whitelisted,
                ltv: param.ltv,
                liquidationThreshold: param.liquidationThreshold,
                liquidationBonusRate: param.liquidationBonusRate
            });

            emit NFTCollateralConfigSet(
                param.collateralKey,
                param.whitelisted,
                param.ltv,
                param.liquidationThreshold,
                param.liquidationBonusRate
            );
        }

        // 初始化手续费配置
        require(_feeCollector != address(0), "Invalid fee collector address");
        feeCollector = _feeCollector;

        for (uint256 i = 0; i < feeRateConfigs.length; i++) {
            FeeRateConfigParam memory param = feeRateConfigs[i];
            require(param.token != address(0), "Invalid token address");
            require(param.feeRate <= 1000000, "Fee rate cannot exceed 100%");

            feeRates[param.token] = param.feeRate;
        }

        for (uint256 i = 0; i < gasConfigs.length; i++) {
            TokenAmount memory param = gasConfigs[i];
            require(param.token != address(0), "Invalid token address");
            require(
                param.n <= type(uint32).max,
                "Gas amount exceeds uint32 max"
            );
            gasAmounts[param.token] = uint32(param.n);
        }
    }

    /**
     * @notice 获取代币的 gas 手续费数量
     * @param token 代币地址
     * @return gas 手续费数量
     */
    function getGasAmount(address token) external view override returns (uint32) {
        return gasAmounts[token];
    }

    /**
     * @notice 检查 ERC20 代币是否在白名单中
     * @param token ERC20 代币地址
     * @return 是否在白名单中
     */
    function isERC20CollateralWhitelisted(
        address token
    ) external view override returns (bool) {
        return erc20CollateralConfigs[token].whitelisted;
    }

    /**
     * @notice 检查 NFT 是否在白名单中
     * @param collateralKey Oracle / NFT adapter 生成的不透明抵押品配置 key
     * @return 是否在白名单中
     */
    function isNFTCollateralWhitelisted(
        bytes32 collateralKey
    ) external view override returns (bool) {
        return nftCollateralConfigs[collateralKey].whitelisted;
    }

    /**
     * @notice 获取 ERC20 抵押品配置
     * @param token ERC20 代币地址
     * @return 配置信息
     */
    function getERC20CollateralConfig(
        address token
    ) external view returns (ERC20CollateralConfig memory) {
        return erc20CollateralConfigs[token];
    }

    /**
     * @notice 获取 NFT 抵押品配置
     * @param collateralKey Oracle / NFT adapter 生成的不透明抵押品配置 key
     * @return 配置信息
     */
    function getNFTCollateralConfig(
        bytes32 collateralKey
    ) external view returns (NFTCollateralConfig memory) {
        return nftCollateralConfigs[collateralKey];
    }

    /**
     * @notice 获取 ERC20 抵押品的 LTV
     * @param token ERC20 代币地址
     * @return LTV 比率
     */
    function getERC20LTV(address token) external view returns (uint32) {
        return erc20CollateralConfigs[token].ltv;
    }

    /**
     * @notice 获取 NFT 抵押品的 LTV
     * @param collateralKey Oracle / NFT adapter 生成的不透明抵押品配置 key
     * @return LTV 比率
     */
    function getNFTLTV(
        bytes32 collateralKey
    ) external view returns (uint32) {
        return nftCollateralConfigs[collateralKey].ltv;
    }

    /**
     * @notice 获取 ERC20 抵押品的清算阈值
     * @param token ERC20 代币地址
     * @return 清算阈值
     */
    function getERC20LiquidationThreshold(
        address token
    ) external view returns (uint32) {
        return erc20CollateralConfigs[token].liquidationThreshold;
    }

    /**
     * @notice 获取 NFT 抵押品的清算阈值
     * @param collateralKey Oracle / NFT adapter 生成的不透明抵押品配置 key
     * @return 清算阈值
     */
    function getNFTLiquidationThreshold(
        bytes32 collateralKey
    ) external view override returns (uint32) {
        return nftCollateralConfigs[collateralKey].liquidationThreshold;
    }

    /**
     * @notice 获取 ERC20 抵押品的清算奖励率
     * @param token ERC20 代币地址
     * @return 清算奖励率
     */
    function getERC20LiquidationBonusRate(
        address token
    ) external view returns (uint32) {
        return erc20CollateralConfigs[token].liquidationBonusRate;
    }

    /**
     * @notice 批量获取 ERC20 抵押品的清算阈值与奖励率(用于加权计算,减少调用次数)
     * @param tokens ERC20 代币地址数组
     * @return liquidationThresholds 清算阈值数组
     * @return liquidationBonusRates 清算奖励率数组
     */
    function getERC20LiquidationConfigs(
        address[] calldata tokens
    )
        external
        view
        override
        returns (
            uint32[] memory liquidationThresholds,
            uint32[] memory liquidationBonusRates
        )
    {
        uint256 len = tokens.length;
        liquidationThresholds = new uint32[](len);
        liquidationBonusRates = new uint32[](len);
        for (uint256 i = 0; i < len; ++i) {
            ERC20CollateralConfig storage cfg = erc20CollateralConfigs[
                tokens[i]
            ];
            liquidationThresholds[i] = cfg.liquidationThreshold;
            liquidationBonusRates[i] = cfg.liquidationBonusRate;
        }
    }

    /**
     * @notice 获取 NFT 抵押品的清算奖励率
     * @param collateralKey Oracle / NFT adapter 生成的不透明抵押品配置 key
     * @return 清算奖励率
     */
    function getNFTLiquidationBonusRate(
        bytes32 collateralKey
    ) external view override returns (uint32) {
        return nftCollateralConfigs[collateralKey].liquidationBonusRate;
    }

    /**
     * @notice 获取代币的手续费率
     * @param token 代币地址
     * @return 手续费率 (6位精度, 1000000 = 100%)
     */
    function getFeeRate(address token) external view override returns (uint32) {
        return feeRates[token];
    }

    /**
     * @notice 获取最小清算规模(USD,18位精度)
     * @return 最小清算规模,0 表示无限制
     */
    function minLiquidationSize() external pure override returns (uint256) {
        return 0;
    }
}

contracts/interfaces/IConfig.sol

// SPDX-License-Identifier: MIT
//
//   Constant Finance — fixed-term, order-book lending on Ethereum
//   App:  https://app.constant.finance/en
//   X:    https://x.com/constant_fi
//
pragma solidity ^0.8.29;

/**
 * @title IConfig
 * @notice Config 合约接口,管理抵押品配置
 */
interface IConfig {
    function isERC20CollateralWhitelisted(
        address _token
    ) external view returns (bool);

    function isNFTCollateralWhitelisted(
        bytes32 _collateralKey
    ) external view returns (bool);

    function getFeeRate(address _token) external view returns (uint32);

    function getGasAmount(address _token) external view returns (uint32);

    function getERC20LiquidationConfigs(
        address[] calldata _tokens
    )
        external
        view
        returns (
            uint32[] memory _liquidationThresholds,
            uint32[] memory _liquidationBonusRates
        );

    function getNFTLiquidationBonusRate(
        bytes32 _collateralKey
    ) external view returns (uint32);

    function getNFTLiquidationThreshold(
        bytes32 _collateralKey
    ) external view returns (uint32);

    function feeCollector() external view returns (address);

    /// @notice Minimum total debt USD value for partial liquidation; below this, full liquidation is used
    function minLiquidationSize() external view returns (uint256);
}

contracts/utils/Types.sol

// SPDX-License-Identifier: MIT
//
//   Constant Finance — fixed-term, order-book lending on Ethereum
//   App:  https://app.constant.finance/en
//   X:    https://x.com/constant_fi
//
pragma solidity ^0.8.29;

struct TokenAmount {
    address token;
    uint256 n; // number of tokens or NFT token ID
}

struct CallbackParams {
    bool enabled; // whether Position should callback msg.sender
    bytes data; // callback payload; may be empty
}

struct TokenInterestRate {
    address token;
    uint64 interestRate; // borrower rate (per-second, 1e18 precision)
    uint64 supplierRate; // supplier rate; spread accrues in Position as protocol revenue
}

struct Position {
    address borrower;
    uint64 maturity;
    TokenAmount[] lentAmounts; // 借出金额,用于初始化 storage
    TokenInterestRate[] interestRates; // 每个聚合 debt token bucket 的利率
}

struct SupplierAllocation {
    address beneficiary; // Debt NFT 的实际经济权益人
    address nftRecipient; // Debt NFT 的链上接收者/托管人
    TokenAmount lentAmount; // 该 beneficiary 的出借 token 和本金
}

struct PositionInfo {
    TokenInterestRate[] interestRates; // 每个聚合 debt token bucket 的利率(lentAmounts 已移至 storage)
    uint64 maturity;
    address borrower;
    address positionBuilder;
    address config; // Config 合约地址
}

struct PositionCreationInfo {
    TokenAmount[] lentAmounts; // 借出金额
    TokenInterestRate[] interestRates; // 利率
    uint256[] supplyOrderIDs; // 供应订单ID
    uint256[] nftTokenIds; // NFT Token ID 数组
    address[] suppliers; // Supplier 地址
    uint256 borrowOrderID; // 借贷订单ID
    uint64 maturity; // 到期时间
    address positionBuilder; // 仓位构建器地址
    TokenAmount[] erc20Collaterals; // erc20抵押品
    TokenAmount[] nftCollaterals; // nft抵押品
    address config; // Config 合约地址
}

struct SupplyOrder {
    uint256 orderID;
    TokenAmount amountsToSupply;
    uint64 term;
    uint64 interestRate;
    uint64 expireTime;
    address owner;
    address fundingAdapter; // address(0) = pull underlying directly from supplier
    // address(0): mint Debt NFT to supplier.
    // non-zero: mint Debt NFT to the signed custodial reinvestment handler.
    address autoReinvestHandler;
    bytes signature;
}

struct BorrowOrder {
    uint256 orderID;
    TokenBorrowSpec amountsToBorrow;
    uint64 term;
    uint64 expireTime;
    TokenAmount[] erc20Collaterals; // erc20抵押物
    TokenAmount[] nftCollaterals; // nft抵押物
    address owner;
    address feeToken; // required; address(0) reverts
    bytes signature;
}

// 杠杆 LP 借贷订单
struct LeverageLPBorrowOrder {
    uint256 orderID; // 订单 ID
    address token0; // 借贷代币0地址
    uint64 interestRate0; // 代币0利率(per-second,1e18 precision)
    address token1; // 借贷代币1地址
    uint64 interestRate1; // 代币1利率(per-second,1e18 precision)
    uint64 term; // 借贷期限(秒)
    uint32 leverage; // 杠杆倍数(6位精度,3000000 = 3x)
    uint256 minBorrowAmount0; // token0 最小借款数量(签名保护)
    uint256 maxBorrowAmount0; // token0 最大借款数量(签名保护)
    uint256 minBorrowAmount1; // token1 最小借款数量(签名保护)
    uint256 maxBorrowAmount1; // token1 最大借款数量(签名保护)
    uint256 activeLPCollateralIndex; // nftCollaterals 中用于计算借款并增加流动性的 LP NFT
    TokenAmount[] erc20Collaterals; // erc20抵押物
    TokenAmount[] nftCollaterals; // 完整 NFT 抵押物列表,包含一个活跃 LP NFT 和可选的额外抵押 NFT
    uint64 expireTime; // 订单过期时间
    address owner; // 订单所有者
    address feeToken; // required; address(0) reverts
    bytes signature; // 订单签名
}

struct MatchedSupplyOrder {
    uint256 originalOrderID;
    bytes32 orderID;
    address token;
    uint256 lentAmount;
    uint64 interestRate; // per-second interest rate (1e18 precision)
    address owner;
    address fundingAdapter;
    address autoReinvestHandler;
}

struct SyncEntry {
    address nftAddr;
    uint256 nftId;
}

// 多资产借款规格:token、借款额、利率绑定在一起,避免数组对不齐
struct TokenBorrowSpec {
    address token;
    uint256 amount;
    uint64 interestRate;
}

struct TokenRateSpec {
    address token;
    uint64 interestRate;
}

struct RefinancingOrder {
    uint256 orderID;
    address owner;
    address oldPosition;
    TokenRateSpec[] tokensToRefinance;
    uint64 term;
    uint64 expireTime;
    TokenAmount[] erc20Collaterals;
    TokenAmount[] nftCollaterals;
    bytes signature;
}

/// @notice Compact compatibility view for a debt NFT's current principal.
struct LentInfo {
    uint96 index;
    uint160 amount;
}

/// @notice Complete accounting state for one debt NFT.
struct DebtState {
    bool found;
    bool burned;
    bool mintFinalized;
    bool debtFinalized;
    bool finalizationEligible;

    address position;
    address underlying;
    address owner;

    uint96 queueIndex;

    uint256 initialPrincipal;
    uint256 remainingPrincipal;
    uint256 principalAhead;

    uint256 totalAccruedInterest;
    uint256 totalFundedInterest;
    uint256 unfundedInterest;
    uint256 claimableInterest;
    uint256 claimablePrincipal;
    uint256 finalRoundingBonus;

    uint256 claimedInterest;
    uint256 claimedPrincipal;

    uint256 faceValue;
    uint256 claimableValue;

    uint64 supplierRate;
    uint64 borrowerRate;
    uint64 maturity;
    uint64 lastAccrualTime;
    uint64 accountingTimestamp;
}

/// @notice Committed aggregate accounting for one Position debt pool.
struct SettlementPoolView {
    bool found;
    bool mintFinalized;
    bool debtFinalized;

    address position;

    uint256 initialPrincipal;
    uint256 remainingPrincipal;

    uint256 totalInterestAccrued;
    uint256 totalInterestFunded;
    uint256 totalInterestClaimed;

    uint256 totalPrincipalRecorded;
    uint256 totalPrincipalClaimed;

    uint256 accruedInterestIndex;
    uint256 fundedInterestIndex;
    uint256 accruedIndexRemainder;
    uint256 fundedIndexRemainder;

    uint256 interestLiability;
    uint256 principalLiability;

    uint256 principalCursor;
    uint256 nftCount;
    uint256 finalClaimCount;

    uint64 borrowerRate;
    uint64 supplierRate;
    uint64 maturity;
    uint64 lastAccrualTime;
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 100,
    "enabled": true
  },
  "evmVersion": "prague",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin/contracts/",
    "@chainlink/contracts/=lib/chainlink/contracts/",
    "@uniswap/v3-core/=lib/v3-core/",
    "@uniswap/v3-periphery/=lib/v3-periphery/",
    "v3-core/=lib/v3-core/contracts/",
    "v3-periphery/=lib/v3-periphery/contracts/",
    "forge-std/=lib/forge-std/src/",
    "chainlink/=lib/chainlink/",
    "openzeppelin/=lib/openzeppelin/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.