Skip to content

Latest commit

 

History

History
444 lines (395 loc) · 15.6 KB

LoanTokenLogicWrbtc.md

File metadata and controls

444 lines (395 loc) · 15.6 KB

LoanTokenLogicWrbtc.sol

View Source: contracts/connectors/loantoken/modules/beaconLogicWRBTC/LoanTokenLogicWrbtc.sol

↗ Extends: LoanTokenLogicStandard

LoanTokenLogicWrbtc contract

Functions


getListFunctionSignatures

This function is MANDATORY, which will be called by LoanTokenLogicBeacon and be registered. Every new public function, the sginature needs to be included in this function. *

function getListFunctionSignatures() external pure
returns(functionSignatures bytes4[], moduleName bytes32)
Source Code
function getListFunctionSignatures()
        external
        pure
        returns (bytes4[] memory functionSignatures, bytes32 moduleName)
    {
        bytes4[] memory res = new bytes4[](32);

        // Loan Token Logic Standard
        res[0] = this.mint.selector;
        res[1] = this.burn.selector;
        res[2] = this.borrow.selector;
        res[3] = this.marginTrade.selector;
        res[4] = this.marginTradeAffiliate.selector;
        res[5] = this.transfer.selector;
        res[6] = this.transferFrom.selector;
        res[7] = this.profitOf.selector;
        res[8] = this.tokenPrice.selector;
        res[9] = this.checkpointPrice.selector;
        res[10] = this.marketLiquidity.selector;
        res[11] = this.avgBorrowInterestRate.selector;
        res[12] = this.borrowInterestRate.selector;
        res[13] = this.nextBorrowInterestRate.selector;
        res[14] = this.supplyInterestRate.selector;
        res[15] = this.nextSupplyInterestRate.selector;
        res[16] = this.totalSupplyInterestRate.selector;
        res[17] = this.totalAssetBorrow.selector;
        res[18] = this.totalAssetSupply.selector;
        res[19] = this.getMaxEscrowAmount.selector;
        res[20] = this.assetBalanceOf.selector;
        res[21] = this.getEstimatedMarginDetails.selector;
        res[22] = this.getDepositAmountForBorrow.selector;
        res[23] = this.getBorrowAmountForDeposit.selector;
        res[24] = this.checkPriceDivergence.selector;
        res[25] = this.calculateSupplyInterestRate.selector;

        // Loan Token WRBTC
        res[26] = this.mintWithBTC.selector;
        res[27] = this.burnToBTC.selector;

        // Advanced Token
        res[28] = this.approve.selector;

        // Advanced Token Storage
        res[29] = this.totalSupply.selector;
        res[30] = this.balanceOf.selector;
        res[31] = this.allowance.selector;

        return (res, stringToBytes32("LoanTokenLogicWrbtc"));
    }

mintWithBTC

function mintWithBTC(address receiver, bool useLM) external payable nonReentrant globallyNonReentrant 
returns(mintAmount uint256)

Arguments

Name Type Description
receiver address
useLM bool
Source Code
function mintWithBTC(address receiver, bool useLM)
        external
        payable
        nonReentrant
        globallyNonReentrant
        returns (uint256 mintAmount)
    {
        if (useLM) return _mintWithLM(receiver, msg.value);
        else return _mintToken(receiver, msg.value);
    }

burnToBTC

function burnToBTC(address receiver, uint256 burnAmount, bool useLM) external nonpayable nonReentrant globallyNonReentrant 
returns(loanAmountPaid uint256)

Arguments

Name Type Description
receiver address
burnAmount uint256
useLM bool
Source Code
function burnToBTC(
        address receiver,
        uint256 burnAmount,
        bool useLM
    ) external nonReentrant globallyNonReentrant returns (uint256 loanAmountPaid) {
        if (useLM) loanAmountPaid = _burnFromLM(burnAmount);
        else loanAmountPaid = _burnToken(burnAmount);

        if (loanAmountPaid != 0) {
            IWrbtcERC20(wrbtcTokenAddress).withdraw(loanAmountPaid);
            Address.sendValue(receiver, loanAmountPaid);
        }
    }

_verifyTransfers

⤾ overrides LoanTokenLogicStandard._verifyTransfers

Handle transfers prior to adding newPrincipal to loanTokenSent. *

function _verifyTransfers(address collateralTokenAddress, struct MarginTradeStructHelpers.SentAddresses sentAddresses, struct MarginTradeStructHelpers.SentAmounts sentAmounts, uint256 withdrawalAmount) internal nonpayable
returns(msgValue uint256)

Arguments

Name Type Description
collateralTokenAddress address The address of the collateral token.
sentAddresses struct MarginTradeStructHelpers.SentAddresses The struct which contains addresses of - lender - borrower - receiver - manager *
sentAmounts struct MarginTradeStructHelpers.SentAmounts The struct which contains uint256 of: - interestRate - newPrincipal - interestInitialAmount - loanTokenSent - collateralTokenSent *
withdrawalAmount uint256 The amount to withdraw. *

Returns

msgValue The amount of value sent.

Source Code
function _verifyTransfers(
        address collateralTokenAddress,
        MarginTradeStructHelpers.SentAddresses memory sentAddresses,
        MarginTradeStructHelpers.SentAmounts memory sentAmounts,
        uint256 withdrawalAmount
    ) internal returns (uint256 msgValue) {
        address _wrbtcToken = wrbtcTokenAddress;
        address _loanTokenAddress = _wrbtcToken;
        address receiver = sentAddresses.receiver;
        uint256 newPrincipal = sentAmounts.newPrincipal;
        uint256 loanTokenSent = sentAmounts.loanTokenSent;
        uint256 collateralTokenSent = sentAmounts.collateralTokenSent;

        require(_loanTokenAddress != collateralTokenAddress, "26");

        msgValue = msg.value;

        if (withdrawalAmount != 0) {
            /// withdrawOnOpen == true
            IWrbtcERC20(_wrbtcToken).withdraw(withdrawalAmount);
            Address.sendValue(receiver, withdrawalAmount);
            if (newPrincipal > withdrawalAmount) {
                _safeTransfer(
                    _loanTokenAddress,
                    sovrynContractAddress,
                    newPrincipal - withdrawalAmount,
                    ""
                );
            }
        } else {
            _safeTransfer(_loanTokenAddress, sovrynContractAddress, newPrincipal, "27");
        }

        if (collateralTokenSent != 0) {
            _safeTransferFrom(
                collateralTokenAddress,
                msg.sender,
                sovrynContractAddress,
                collateralTokenSent,
                "28"
            );
        }

        if (loanTokenSent != 0) {
            if (msgValue != 0 && msgValue >= loanTokenSent) {
                IWrbtc(_wrbtcToken).deposit.value(loanTokenSent)();
                _safeTransfer(_loanTokenAddress, sovrynContractAddress, loanTokenSent, "29");
                msgValue -= loanTokenSent;
            } else {
                _safeTransferFrom(
                    _loanTokenAddress,
                    msg.sender,
                    sovrynContractAddress,
                    loanTokenSent,
                    "29"
                );
            }
        }
    }

Contracts