mToken

Git Source

Inherits: mTokenConfiguration, ReentrancyGuard

Title: Core mToken logic

Author: Merge Layers Inc.

Base ERC-20 compatible lending token logic

Functions

constructor

Sets initial borrow rate max mantissa

constructor() ;

transfer

Transfers amount tokens to the dst address

function transfer(address dst, uint256 amount) external override nonReentrant returns (bool);

Parameters

NameTypeDescription
dstaddressThe address of the recipient
amountuint256The number of tokens to transfer

Returns

NameTypeDescription
<none>boolsuccess Whether the transfer was successful or not

transferFrom

Transfers amount tokens from the src address to the dst address

function transferFrom(address src, address dst, uint256 amount) external override nonReentrant returns (bool);

Parameters

NameTypeDescription
srcaddressThe address from which tokens are transferred
dstaddressThe address to which tokens are transferred
amountuint256The number of tokens to transfer

Returns

NameTypeDescription
<none>boolsuccess Whether the transfer was successful or not

approve

Approves spender to spend amount tokens on behalf of the caller

function approve(address spender, uint256 amount) external override returns (bool);

Parameters

NameTypeDescription
spenderaddressThe address authorized to spend tokens
amountuint256The number of tokens to approve

Returns

NameTypeDescription
<none>boolsuccess Whether the approval was successful or not

totalBorrowsCurrent

Returns the total amount of borrows, accounting for interest

function totalBorrowsCurrent() external override nonReentrant returns (uint256);

Returns

NameTypeDescription
<none>uint256totalBorrowsCurrentAmount The total amount of borrows

borrowBalanceCurrent

Returns the current borrow balance for account, accounting for interest

function borrowBalanceCurrent(address account) external override nonReentrant returns (uint256);

Parameters

NameTypeDescription
accountaddressThe address to query the borrow balance for

Returns

NameTypeDescription
<none>uint256borrowBalance Current borrow balance

seize

Transfers collateral tokens (this market) to the liquidator.

Will fail unless called by another mToken during the process of liquidation. Its absolutely critical to use msg.sender as the borrowed mToken and not a parameter.

function seize(address liquidator, address borrower, uint256 seizeTokens) external override nonReentrant;

Parameters

NameTypeDescription
liquidatoraddressThe account receiving seized collateral
borroweraddressThe account having collateral seized
seizeTokensuint256The number of mTokens to seize

reduceReserves

Accrues interest and reduces reserves by transferring to admin

function reduceReserves(uint256 reduceAmount) external override nonReentrant;

Parameters

NameTypeDescription
reduceAmountuint256Amount of reduction to reserves

balanceOfUnderlying

Returns the underlying asset balance of the owner

function balanceOfUnderlying(address owner) external override returns (uint256);

Parameters

NameTypeDescription
owneraddressThe address to query the balance of underlying assets for

Returns

NameTypeDescription
<none>uint256balance The balance of underlying assets owned by owner

allowance

Returns the current allowance the spender has from the owner

function allowance(address owner, address spender) external view override returns (uint256);

Parameters

NameTypeDescription
owneraddressThe address of the token holder
spenderaddressThe address authorized to spend the tokens

Returns

NameTypeDescription
<none>uint256allowanceAmount The current remaining number of tokens spender can spend

balanceOf

Returns the value of tokens owned by account.

function balanceOf(address owner) external view override returns (uint256);

Parameters

NameTypeDescription
owneraddress

Returns

NameTypeDescription
<none>uint256balance Token balance of account

getAccountSnapshot

Returns the snapshot of account details for the given account

function getAccountSnapshot(address account)
    external
    view
    override
    returns (uint256 tokenBalance, uint256 borrowBalance, uint256 exchangeRate);

Parameters

NameTypeDescription
accountaddressThe address to query the account snapshot for

Returns

NameTypeDescription
tokenBalanceuint256Token balance
borrowBalanceuint256Borrow balance
exchangeRateuint256Exchange rate

borrowRatePerBlock

Returns the current borrow rate per block

function borrowRatePerBlock() external view override returns (uint256);

Returns

NameTypeDescription
<none>uint256borrowRate The current borrow rate per block, scaled by 1e18

supplyRatePerBlock

Returns the current supply rate per block

function supplyRatePerBlock() external view override returns (uint256);

Returns

NameTypeDescription
<none>uint256supplyRate The current supply rate per block, scaled by 1e18

borrowBalanceStored

Returns the stored borrow balance for account, without accruing interest

function borrowBalanceStored(address account) external view override returns (uint256);

Parameters

NameTypeDescription
accountaddressThe address to query the stored borrow balance for

Returns

NameTypeDescription
<none>uint256storedBalance The stored borrow balance

getCash

Returns the total amount of available cash in the contract

function getCash() external view override returns (uint256);

Returns

NameTypeDescription
<none>uint256cash The total amount of cash

exchangeRateStored

Returns the stored exchange rate, without accruing interest

function exchangeRateStored() external view override returns (uint256);

Returns

NameTypeDescription
<none>uint256exchangeRateStoredMantissa The stored exchange rate

exchangeRateCurrent

Returns the current exchange rate, with interest accrued

function exchangeRateCurrent() public override nonReentrant returns (uint256);

Returns

NameTypeDescription
<none>uint256exchangeRate The current exchange rate

_initializeMToken

Initialize the money market

function _initializeMToken(
    address operator_,
    address interestRateModel_,
    uint256 initialExchangeRateMantissa_,
    string memory name_,
    string memory symbol_,
    uint8 decimals_
) internal;

Parameters

NameTypeDescription
operator_addressThe address of the Operator
interestRateModel_addressThe address of the interest rate model
initialExchangeRateMantissa_uint256The initial exchange rate, scaled by 1e18
name_stringEIP-20 name of this token
symbol_stringEIP-20 symbol of this token
decimals_uint8EIP-20 decimal precision of this token

_mint

Sender supplies assets into the market and receives mTokens in exchange

Accrues interest whether or not the operation succeeds, unless reverted

function _mint(address user, address receiver, uint256 mintAmount, uint256 minAmountOut, bool doTransfer)
    internal
    nonReentrant;

Parameters

NameTypeDescription
useraddressThe user address
receiveraddressThe receiver address
mintAmountuint256The amount of the underlying asset to supply
minAmountOutuint256The minimum amount to be received
doTransferboolIf an actual transfer should be performed

_redeem

Sender redeems mTokens in exchange for the underlying asset

Accrues interest whether or not the operation succeeds, unless reverted

function _redeem(address user, uint256 redeemTokens, bool doTransfer)
    internal
    nonReentrant
    returns (uint256 underlyingAmount);

Parameters

NameTypeDescription
useraddressThe user address
redeemTokensuint256The number of mTokens to redeem into underlying
doTransferboolIf an actual transfer should be performed

Returns

NameTypeDescription
underlyingAmountuint256Amount of underlying redeemed

_redeemUnderlying

Sender redeems mTokens in exchange for a specified amount of underlying asset

Accrues interest whether or not the operation succeeds, unless reverted

function _redeemUnderlying(address user, uint256 redeemAmount, bool doTransfer) internal nonReentrant;

Parameters

NameTypeDescription
useraddressThe user address
redeemAmountuint256The amount of underlying to receive from redeeming mTokens
doTransferboolIf an actual transfer should be performed

_borrow

Sender borrows assets from the protocol to their own address

function _borrow(address user, uint256 borrowAmount, bool doTransfer) internal nonReentrant;

Parameters

NameTypeDescription
useraddressThe user address
borrowAmountuint256The amount of the underlying asset to borrow
doTransferboolIf an actual transfer should be performed

_borrowWithReceiver

Sender borrows assets from the protocol to their own address

function _borrowWithReceiver(address user, address receiver, uint256 borrowAmount) internal nonReentrant;

Parameters

NameTypeDescription
useraddressThe user address
receiveraddressThe underlying receiver address
borrowAmountuint256The amount of the underlying asset to borrow

_repay

Sender repays their own borrow

function _repay(uint256 repayAmount, bool doTransfer) internal nonReentrant returns (uint256);

Parameters

NameTypeDescription
repayAmountuint256The amount to repay, or type(uint256).max for the full outstanding amount
doTransferboolIf an actual transfer should be performed

Returns

NameTypeDescription
<none>uint256actualRepay Amount actually repaid

_repayBehalf

Sender repays a borrow belonging to borrower

function _repayBehalf(address borrower, uint256 repayAmount, bool doTransfer)
    internal
    nonReentrant
    returns (uint256);

Parameters

NameTypeDescription
borroweraddressthe account with the debt being payed off
repayAmountuint256The amount to repay, or type(uint256).max for the full outstanding amount
doTransferboolIf an actual transfer should be performed

Returns

NameTypeDescription
<none>uint256actualRepay Amount actually repaid

_liquidate

The sender liquidates the borrowers collateral. The collateral seized is transferred to the liquidator.

function _liquidate(
    address liquidator,
    address borrower,
    uint256 repayAmount,
    address mTokenCollateral,
    bool doTransfer
) internal nonReentrant;

Parameters

NameTypeDescription
liquidatoraddressThe liquidator address
borroweraddressThe borrower of this mToken to be liquidated
repayAmountuint256The amount of the underlying borrowed asset to repay
mTokenCollateraladdressThe market in which to seize collateral from the borrower
doTransferboolIf an actual transfer should be performed

_seize

Transfers collateral tokens (this market) to the liquidator.

Called only during an in-kind liquidation, or by liquidateBorrow during the liquidation of another mToken. It's absolutely critical to use msg.sender as the seizer mToken and not a parameter.

function _seize(address seizerToken, address liquidator, address borrower, uint256 seizeTokens) internal;

Parameters

NameTypeDescription
seizerTokenaddressThe contract seizing the collateral (i.e. borrowed mToken)
liquidatoraddressThe account receiving seized collateral
borroweraddressThe account having collateral seized
seizeTokensuint256The number of mTokens to seize

_addReserves

Accrues interest and reduces reserves by transferring from msg.sender

function _addReserves(uint256 addAmount) internal nonReentrant;

Parameters

NameTypeDescription
addAmountuint256Amount of addition to reserves

__liquidate

The liquidator liquidates the borrowers collateral.

The collateral seized is transferred to the liquidator.

function __liquidate(
    address liquidator,
    address borrower,
    uint256 repayAmount,
    address mTokenCollateral,
    bool doTransfer
) internal;

Parameters

NameTypeDescription
liquidatoraddressThe address repaying the borrow and seizing collateral
borroweraddressThe borrower of this mToken to be liquidated
repayAmountuint256The amount of the underlying borrowed asset to repay
mTokenCollateraladdressThe market in which to seize collateral from the borrower
doTransferboolIf an actual transfer should be performed

_borrowBalanceStored

Return the borrow balance of account based on stored data

function _borrowBalanceStored(address account) internal view returns (uint256);

Parameters

NameTypeDescription
accountaddressThe address whose balance should be calculated

Returns

NameTypeDescription
<none>uint256borrowBalance Borrow balance with interest applied

__repay

Borrows are repaid by another user (possibly the borrower).

function __repay(address payer, address borrower, uint256 repayAmount, bool doTransfer) private returns (uint256);

Parameters

NameTypeDescription
payeraddressthe account paying off the borrow
borroweraddressthe account with the debt being payed off
repayAmountuint256the amount of underlying tokens being returned, or type(uint256).max for the full outstanding amount
doTransferboolIf an actual transfer should be performed

Returns

NameTypeDescription
<none>uint256Amount actually repaid

__borrow

Users borrow assets from the protocol to their own address

function __borrow(address payable borrower, address payable receiver, uint256 borrowAmount, bool doTransfer)
    private;

Parameters

NameTypeDescription
borroweraddress payableBorrower address
receiveraddress payableReceiver address
borrowAmountuint256The amount of the underlying asset to borrow
doTransferboolIf an actual transfer should be performed

__redeem

Executes redemption and performs transfers

function __redeem(address payable redeemer, uint256 redeemTokensIn, uint256 redeemAmountIn, bool doTransfer)
    private
    returns (uint256 redeemAmount);

Parameters

NameTypeDescription
redeemeraddress payableAddress redeeming
redeemTokensInuint256Number of tokens to redeem (if non-zero)
redeemAmountInuint256Underlying amount to redeem (if non-zero)
doTransferboolIf an actual transfer should be performed

Returns

NameTypeDescription
redeemAmountuint256Underlying redeemed

__mint

User supplies assets into the market and receives mTokens in exchange

Assumes interest has already been accrued up to the current block

function __mint(address minter, address receiver, uint256 mintAmount, uint256 minAmountOut, bool doTransfer)
    private;

Parameters

NameTypeDescription
minteraddressThe address of the account which is supplying the assets
receiveraddressThe address of the account which is receiving the assets
mintAmountuint256The amount of the underlying asset to supply
minAmountOutuint256The min amount to be received
doTransferboolIf an actual transfer should be performed

_transferTokens

Transfer tokens tokens from src to dst by spender

Called by both transfer and transferFrom internally

function _transferTokens(address spender, address src, address dst, uint256 tokens) private;

Parameters

NameTypeDescription
spenderaddressThe address of the account performing the transfer
srcaddressThe address of the source account
dstaddressThe address of the destination account
tokensuint256The number of tokens to transfer

__calculateSeizeTokens

Calculates seize token amount for liquidation

function __calculateSeizeTokens(address mTokenBorrowed, address mTokenCollateral, uint256 actualRepayAmount)
    private
    view
    returns (uint256 seizeTokens);

Parameters

NameTypeDescription
mTokenBorrowedaddressThe market of the borrowed asset
mTokenCollateraladdressThe market of the collateral asset
actualRepayAmountuint256Actual amount repaid

Returns

NameTypeDescription
seizeTokensuint256Amount of collateral tokens to seize