Skip to content

Latest commit

 

History

History
618 lines (515 loc) · 18.6 KB

LoanSettings.md

File metadata and controls

618 lines (515 loc) · 18.6 KB

Loan Settings contract.

  • (LoanSettings.sol)

View Source: contracts/modules/LoanSettings.sol

↗ Extends: State, LoanSettingsEvents, ModuleCommonFunctionalities

LoanSettings contract

This contract code comes from bZx. bZx is a protocol for tokenized margin trading and lending https://bzx.network similar to the dYdX protocol.

  • This contract contains functions to get and set loan parameters.

Functions


constructor

Empty public constructor.

function () public nonpayable
Source Code
constructor() public {}

constructor

Fallback function is to react to receiving value (rBTC).

function () external nonpayable
Source Code
function() external {
        revert("LoanSettings - fallback not allowed");
    }

initialize

Set function selectors on target contract. *

function initialize(address target) external nonpayable onlyOwner 

Arguments

Name Type Description
target address The address of the target contract.
Source Code
function initialize(address target) external onlyOwner {
        address prevModuleContractAddress = logicTargets[this.setupLoanParams.selector];
        _setTarget(this.setupLoanParams.selector, target);
        _setTarget(this.disableLoanParams.selector, target);
        _setTarget(this.getLoanParams.selector, target);
        _setTarget(this.getLoanParamsList.selector, target);
        _setTarget(this.getTotalPrincipal.selector, target);
        _setTarget(this.minInitialMargin.selector, target);
        emit ProtocolModuleContractReplaced(prevModuleContractAddress, target, "LoanSettings");
    }

setupLoanParams

Setup loan parameters, by looping every loan and populating its parameters. *

function setupLoanParams(struct LoanParamsStruct.LoanParams[] loanParamsList) external nonpayable whenNotPaused 
returns(loanParamsIdList bytes32[])

Arguments

Name Type Description
loanParamsList struct LoanParamsStruct.LoanParams[] The array of loan parameters. *

Returns

loanParamsIdList The array of loan parameters IDs.

Source Code
function setupLoanParams(LoanParams[] calldata loanParamsList)
        external
        whenNotPaused
        returns (bytes32[] memory loanParamsIdList)
    {
        loanParamsIdList = new bytes32[](loanParamsList.length);
        for (uint256 i = 0; i < loanParamsList.length; i++) {
            loanParamsIdList[i] = _setupLoanParams(loanParamsList[i]);
        }
    }

disableLoanParams

Deactivate LoanParams for future loans. Active loans using it are unaffected. *

function disableLoanParams(bytes32[] loanParamsIdList) external nonpayable whenNotPaused 

Arguments

Name Type Description
loanParamsIdList bytes32[] The array of loan parameters IDs to deactivate.
Source Code
function disableLoanParams(bytes32[] calldata loanParamsIdList) external whenNotPaused {
        for (uint256 i = 0; i < loanParamsIdList.length; i++) {
            require(msg.sender == loanParams[loanParamsIdList[i]].owner, "unauthorized owner");
            loanParams[loanParamsIdList[i]].active = false;

            LoanParams memory loanParamsLocal = loanParams[loanParamsIdList[i]];
            emit LoanParamsDisabled(
                loanParamsLocal.id,
                loanParamsLocal.owner,
                loanParamsLocal.loanToken,
                loanParamsLocal.collateralToken,
                loanParamsLocal.minInitialMargin,
                loanParamsLocal.maintenanceMargin,
                loanParamsLocal.maxLoanTerm
            );
            emit LoanParamsIdDisabled(loanParamsLocal.id, loanParamsLocal.owner);
        }
    }

getLoanParams

Get loan parameters for every matching IDs. *

function getLoanParams(bytes32[] loanParamsIdList) public view
returns(loanParamsList struct LoanParamsStruct.LoanParams[])

Arguments

Name Type Description
loanParamsIdList bytes32[] The array of loan parameters IDs to match. *

Returns

loanParamsList The result array of loan parameters.

Source Code
function getLoanParams(bytes32[] memory loanParamsIdList)
        public
        view
        returns (LoanParams[] memory loanParamsList)
    {
        loanParamsList = new LoanParams[](loanParamsIdList.length);
        uint256 itemCount;

        for (uint256 i = 0; i < loanParamsIdList.length; i++) {
            LoanParams memory loanParamsLocal = loanParams[loanParamsIdList[i]];
            if (loanParamsLocal.id == 0) {
                continue;
            }
            loanParamsList[itemCount] = loanParamsLocal;
            itemCount++;
        }

        if (itemCount < loanParamsList.length) {
            assembly {
                mstore(loanParamsList, itemCount)
            }
        }
    }

getLoanParamsList

Get loan parameters for an owner and a given page defined by an offset and a limit. *

function getLoanParamsList(address owner, uint256 start, uint256 count) external view
returns(loanParamsList bytes32[])

Arguments

Name Type Description
owner address The address of the loan owner.
start uint256 The page offset.
count uint256 The page limit. *

Returns

loanParamsList The result array of loan parameters.

Source Code
function getLoanParamsList(
        address owner,
        uint256 start,
        uint256 count
    ) external view returns (bytes32[] memory loanParamsList) {
        EnumerableBytes32Set.Bytes32Set storage set = userLoanParamSets[owner];
        uint256 end = start.add(count).min256(set.length());
        if (start >= end) {
            return loanParamsList;
        }

        loanParamsList = new bytes32[](count);
        uint256 itemCount;
        for (uint256 i = end - start; i > 0; i--) {
            if (itemCount == count) {
                break;
            }
            loanParamsList[itemCount] = set.get(i + start - 1);
            itemCount++;
        }

        if (itemCount < count) {
            assembly {
                mstore(loanParamsList, itemCount)
            }
        }
    }

getTotalPrincipal

Get the total principal of the loans by a lender. *

function getTotalPrincipal(address lender, address loanToken) external view
returns(uint256)

Arguments

Name Type Description
lender address The address of the lender.
loanToken address The address of the token instance. *

Returns

The total principal of the loans.

Source Code
function getTotalPrincipal(address lender, address loanToken) external view returns (uint256) {
        return lenderInterest[lender][loanToken].principalTotal;
    }

_setupLoanParams

Setup a loan parameters. *

function _setupLoanParams(struct LoanParamsStruct.LoanParams loanParamsLocal) internal nonpayable
returns(bytes32)

Arguments

Name Type Description
loanParamsLocal struct LoanParamsStruct.LoanParams The loan parameters. *

Returns

loanParamsId The loan parameters ID.

Source Code
function _setupLoanParams(LoanParams memory loanParamsLocal) internal returns (bytes32) {
        bytes32 loanParamsId =
            keccak256(
                abi.encodePacked(
                    loanParamsLocal.loanToken,
                    loanParamsLocal.collateralToken,
                    loanParamsLocal.minInitialMargin,
                    loanParamsLocal.maintenanceMargin,
                    loanParamsLocal.maxLoanTerm,
                    block.timestamp
                )
            );
        require(loanParams[loanParamsId].id == 0, "loanParams exists");

        require(
            loanParamsLocal.loanToken != address(0) &&
                loanParamsLocal.collateralToken != address(0) &&
                loanParamsLocal.minInitialMargin > loanParamsLocal.maintenanceMargin &&
                (loanParamsLocal.maxLoanTerm == 0 || loanParamsLocal.maxLoanTerm > 3600), /// A defined maxLoanTerm has to be greater than one hour.
            "invalid params"
        );

        loanParamsLocal.id = loanParamsId;
        loanParamsLocal.active = true;
        loanParamsLocal.owner = msg.sender;

        loanParams[loanParamsId] = loanParamsLocal;
        userLoanParamSets[msg.sender].addBytes32(loanParamsId);

        emit LoanParamsSetup(
            loanParamsId,
            loanParamsLocal.owner,
            loanParamsLocal.loanToken,
            loanParamsLocal.collateralToken,
            loanParamsLocal.minInitialMargin,
            loanParamsLocal.maintenanceMargin,
            loanParamsLocal.maxLoanTerm
        );
        emit LoanParamsIdSetup(loanParamsId, loanParamsLocal.owner);

        return loanParamsId;
    }

minInitialMargin

function minInitialMargin(bytes32 loanParamsId) external view
returns(uint256)

Arguments

Name Type Description
loanParamsId bytes32
Source Code
function minInitialMargin(bytes32 loanParamsId) external view returns (uint256) {
        return loanParams[loanParamsId].minInitialMargin;
    }

Contracts