mTokenStorage

Git Source

Inherits: ImToken, ExponentialNoError

Title: mTokenStorage

Author: Merge Layers Inc.

Storage contract for mToken

State Variables

RESERVE_FACTOR_MAX_MANTISSA

Maximum fraction of interest that can be set aside for reserves

uint256 internal constant RESERVE_FACTOR_MAX_MANTISSA = 1e18

PROTOCOL_SEIZE_SHARE_MANTISSA

Share of seized collateral that is added to reserves

uint256 internal constant PROTOCOL_SEIZE_SHARE_MANTISSA = 2.8e16

admin

Administrator for this contract

address payable public admin

pendingAdmin

Pending administrator for this contract

address payable public pendingAdmin

operator

Contract which oversees inter-mToken operations

address public operator

rolesOperator

Roles manager

IRoles public rolesOperator

name

EIP-20 token name for this token

string public name

symbol

EIP-20 token symbol for this token

string public symbol

decimals

EIP-20 token decimals for this token

uint8 public decimals

interestRateModel

Model which tells what the current interest rate should be

address public interestRateModel

reserveFactorMantissa

Fraction of interest currently set aside for reserves

uint256 public reserveFactorMantissa

accrualBlockTimestamp

Block timestamp that interest was last accrued at

uint256 public accrualBlockTimestamp

borrowIndex

Accumulator of the total earned interest rate since the opening of the market

uint256 public borrowIndex

totalBorrows

Total amount of outstanding borrows of the underlying in this market

uint256 public totalBorrows

totalReserves

Total amount of reserves of the underlying held in this market

uint256 public totalReserves

totalSupply

Returns the value of tokens in existence.

uint256 public totalSupply

totalUnderlying

Returns the amount of underlying tokens

uint256 public totalUnderlying

borrowRateMaxMantissa

Maximum borrow rate that can ever be applied

uint256 public borrowRateMaxMantissa = 0.0005e16

accountBorrows

Mapping of account addresses to borrow snapshots

mapping(address account => BorrowSnapshot snapshot) internal accountBorrows

accountTokens

Mapping of account addresses to token balances

mapping(address account => uint256 tokens) internal accountTokens

transferAllowances

Mapping of owner to spender to allowance amount

mapping(address owner => mapping(address spender => uint256 amount)) internal transferAllowances

initialExchangeRateMantissa

Initial exchange rate used when minting the first mTokens (used when totalSupply = 0)

uint256 internal initialExchangeRateMantissa

__gap

uint256[50] private __gap

Functions

accrueInterest

Accrues interest on the contract's outstanding loans

function accrueInterest() external virtual;

_accrueInterest

Accrues interest up to the current timestamp

function _accrueInterest() internal;

_doTransferIn

Performs a transfer in, reverting upon failure

function _doTransferIn(address from, uint256 amount) internal virtual returns (uint256);

Parameters

NameTypeDescription
fromaddressSender address
amountuint256Amount to transfer in

Returns

NameTypeDescription
<none>uint256Amount actually transferred to the protocol

_doTransferOut

Performs a transfer out to recipient

function _doTransferOut(address payable to, uint256 amount) internal virtual;

Parameters

NameTypeDescription
toaddress payableRecipient address
amountuint256Amount to transfer

_exchangeRateStored

Calculates the exchange rate from the underlying to the MToken

This function does not accrue interest before calculating the exchange rate Can generate issues if inflated by an attacker when market is created Solution: use 0 collateral factor initially

function _exchangeRateStored() internal view virtual returns (uint256);

Returns

NameTypeDescription
<none>uint256calculated exchange rate scaled by 1e18

_getBlockTimestamp

Retrieves current block timestamp (virtual for tests)

function _getBlockTimestamp() internal view virtual returns (uint256);

Returns

NameTypeDescription
<none>uint256Current block timestamp

_getCashPrior

Gets balance of this contract in terms of the underlying

This excludes the value of the current message, if any

function _getCashPrior() internal view virtual returns (uint256);

Returns

NameTypeDescription
<none>uint256The quantity of underlying owned by this contract

Events

NewRolesOperator

Event emitted when rolesOperator is changed

event NewRolesOperator(address indexed oldRoles, address indexed newRoles);

Parameters

NameTypeDescription
oldRolesaddressPrevious roles contract
newRolesaddressNew roles contract

NewOperator

Event emitted when Operator is changed

event NewOperator(address indexed oldOperator, address indexed newOperator);

Parameters

NameTypeDescription
oldOperatoraddressPrevious operator address
newOperatoraddressNew operator address

NewPendingAdmin

Event emitted when pending admin is updated

event NewPendingAdmin(address indexed pendingAdmin);

Parameters

NameTypeDescription
pendingAdminaddressThe new pending admin address

AdminAccepted

Event emitted when admin is accepted

event AdminAccepted(address indexed admin);

Parameters

NameTypeDescription
adminaddressThe new admin address

Transfer

EIP20 Transfer event

event Transfer(address indexed from, address indexed to, uint256 amount);

Parameters

NameTypeDescription
fromaddressSender address
toaddressReceiver address
amountuint256Amount transferred

Approval

EIP20 Approval event

event Approval(address indexed owner, address indexed spender, uint256 amount);

Parameters

NameTypeDescription
owneraddressToken owner
spenderaddressSpender address
amountuint256Allowed amount

AccrueInterest

Event emitted when interest is accrued

event AccrueInterest(uint256 cashPrior, uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows);

Parameters

NameTypeDescription
cashPrioruint256Cash prior to accrual
interestAccumulateduint256Interest accumulated during accrual
borrowIndexuint256New borrow index
totalBorrowsuint256Total borrows after accrual

Mint

Event emitted when tokens are minted

event Mint(address indexed minter, address indexed receiver, uint256 mintAmount, uint256 mintTokens);

Parameters

NameTypeDescription
minteraddressAddress initiating mint
receiveraddressReceiver of minted tokens
mintAmountuint256Amount of underlying supplied
mintTokensuint256Amount of mTokens minted

Redeem

Event emitted when tokens are redeemed

event Redeem(address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens);

Parameters

NameTypeDescription
redeemeraddressAddress redeeming tokens
redeemAmountuint256Amount of underlying redeemed
redeemTokensuint256Number of tokens redeemed

Borrow

Event emitted when underlying is borrowed

event Borrow(address indexed borrower, uint256 borrowAmount, uint256 accountBorrows, uint256 totalBorrows);

Parameters

NameTypeDescription
borroweraddressBorrower address
borrowAmountuint256Amount borrowed
accountBorrowsuint256Account borrow balance
totalBorrowsuint256Total borrows after action

RepayBorrow

Event emitted when a borrow is repaid

event RepayBorrow(
    address indexed payer,
    address indexed borrower,
    uint256 repayAmount,
    uint256 accountBorrows,
    uint256 totalBorrows
);

Parameters

NameTypeDescription
payeraddressAddress paying back
borroweraddressBorrower whose debt is reduced
repayAmountuint256Amount repaid
accountBorrowsuint256Borrower's balance after repay
totalBorrowsuint256Total borrows after repay

LiquidateBorrow

Event emitted when a borrow is liquidated

event LiquidateBorrow(
    address indexed liquidator,
    address indexed borrower,
    uint256 repayAmount,
    address indexed mTokenCollateral,
    uint256 seizeTokens
);

Parameters

NameTypeDescription
liquidatoraddressLiquidator address
borroweraddressBorrower being liquidated
repayAmountuint256Amount repaid
mTokenCollateraladdressCollateral market
seizeTokensuint256Tokens seized

NewMarketInterestRateModel

Event emitted when interestRateModel is changed

event NewMarketInterestRateModel(address indexed oldInterestRateModel, address indexed newInterestRateModel);

Parameters

NameTypeDescription
oldInterestRateModeladdressPrevious interest rate model
newInterestRateModeladdressNew interest rate model

NewReserveFactor

Event emitted when the reserve factor is changed

event NewReserveFactor(uint256 oldReserveFactorMantissa, uint256 newReserveFactorMantissa);

Parameters

NameTypeDescription
oldReserveFactorMantissauint256Previous reserve factor
newReserveFactorMantissauint256New reserve factor

ReservesAdded

Event emitted when the reserves are added

event ReservesAdded(address indexed benefactor, uint256 addAmount, uint256 newTotalReserves);

Parameters

NameTypeDescription
benefactoraddressAddress adding reserves
addAmountuint256Amount added
newTotalReservesuint256Total reserves after addition

ReservesReduced

Event emitted when the reserves are reduced

event ReservesReduced(address indexed admin, uint256 reduceAmount, uint256 newTotalReserves);

Parameters

NameTypeDescription
adminaddressAddress removing reserves
reduceAmountuint256Amount removed
newTotalReservesuint256Total reserves after reduction

NewBorrowRateMaxMantissa

Event emitted when the borrow max mantissa is updated

event NewBorrowRateMaxMantissa(uint256 oldVal, uint256 maxMantissa);

Parameters

NameTypeDescription
oldValuint256Previous max mantissa
maxMantissauint256New max mantissa

SameChainFlowStateUpdated

Event emitted when same chain flow state is enabled or disabled

event SameChainFlowStateUpdated(address indexed sender, bool _oldState, bool _newState);

Parameters

NameTypeDescription
senderaddressAddress updating the state
_oldStateboolPrevious state
_newStateboolNew state

ZkVerifierUpdated

Event emitted when zkVerifier is updated

event ZkVerifierUpdated(address indexed oldVerifier, address indexed newVerifier);

Parameters

NameTypeDescription
oldVerifieraddressPrevious verifier
newVerifieraddressNew verifier

Errors

mt_OnlyAdmin

Thrown when caller is not admin

error mt_OnlyAdmin();

mt_RedeemEmpty

Thrown when redeem amounts are zero

error mt_RedeemEmpty();

mt_InvalidInput

Thrown when provided input is invalid

error mt_InvalidInput();

mt_OnlyAdminOrRole

Thrown when caller lacks required role

error mt_OnlyAdminOrRole();

mt_PriceFetchFailed

Thrown when price fetch fails

error mt_PriceFetchFailed();

mt_TransferNotValid

Thrown when transfer is not valid

error mt_TransferNotValid();

mt_MinAmountNotValid

Thrown when minimum amount is not met

error mt_MinAmountNotValid();

mt_BorrowRateTooHigh

Thrown when borrow rate exceeds maximum

error mt_BorrowRateTooHigh();

mt_AlreadyInitialized

Thrown when contract is already initialized

error mt_AlreadyInitialized();

mt_ReserveFactorTooHigh

Thrown when reserve factor exceeds limit

error mt_ReserveFactorTooHigh();

mt_ExchangeRateNotValid

Thrown when exchange rate is invalid

error mt_ExchangeRateNotValid();

mt_MarketMethodNotValid

Thrown when market method call is invalid

error mt_MarketMethodNotValid();

mt_LiquidateSeizeTooMuch

Thrown when liquidation seizes too much collateral

error mt_LiquidateSeizeTooMuch();

mt_RedeemCashNotAvailable

Thrown when redeem cash is unavailable

error mt_RedeemCashNotAvailable();

mt_BorrowCashNotAvailable

Thrown when borrow cash is unavailable

error mt_BorrowCashNotAvailable();

mt_ReserveCashNotAvailable

Thrown when reserve cash is unavailable

error mt_ReserveCashNotAvailable();

mt_RedeemTransferOutNotPossible

Thrown when redeem transfer out is not possible

error mt_RedeemTransferOutNotPossible();

mt_SameChainOperationsAreDisabled

Thrown when same chain operations are disabled

error mt_SameChainOperationsAreDisabled();

mt_CollateralBlockTimestampNotValid

Thrown when collateral block timestamp is invalid

error mt_CollateralBlockTimestampNotValid();

mt_AddressNotValid

Thrown when configuration address is invalid

error mt_AddressNotValid();

mt_OperatorNotValid

Thrown when address is not valid

error mt_OperatorNotValid();

mt_NameNotValid

Error thrown when name is not valid

error mt_NameNotValid();

mt_SymbolNotValid

Error thrown when symbol is not valid

error mt_SymbolNotValid();

mt_DecimalsNotValid

Error thrown when decimals are not valid

error mt_DecimalsNotValid();

Structs

BorrowSnapshot

Container for borrow balance information

struct BorrowSnapshot {
    /// @notice Total balance (with accrued interest), after applying the most recent balance-changing action
    uint256 principal;
    /// @notice Global borrowIndex as of the most recent balance-changing action
    uint256 interestIndex;
}