Skip to main content

Positions

Overview

On Morpho, a position is created whenever a user supplies, provides collateral, borrows, or engages in both activities on a Morpho market.

Unlike protocols like Aave or Compound, positions in Morpho are not tokenized as aTokens or cTokens. Instead, they are tracked using a mapping at the singleton smart contract level. This approach simplifies the system and reduces dependency on ERC20 token standards, offering a more efficient way to manage user positions.

Details

Supply, Borrow, Withdraw, Repay

In Morpho, the operations of supply, borrow, withdraw, and repay do not involve minting ERC20 tokens like Compound's cTokens. Instead, the system tracks users' positions using a mapping at the smart contract level. It uses shares for the loan asset.

Why Use Shares?

Shares are used to manage and track the distribution of assets within the Morpho protocol. This design helps ensure precise accounting and mitigate potential issues. Here are the main points:

  1. Precision in Accounting:
  • Shares allow for high precision in calculating the value of assets and liabilities, ensuring accurate representation of supply and borrow balances, even with fractional values.
  • SharesMathLib provides functions (toSharesDown, toSharesUp, toAssetsDown, toAssetsUp) to convert between assets and shares, rounding up or down as needed. This ensures precise handling of all operations.
  1. Mitigation of Share Price Manipulation:
  • To prevent manipulation of the share price, an offset with virtual shares and virtual assets is used. This concept, borrowed from OpenZeppelin's ERC4626 standard, helps mitigate inflation attacks.
  • These virtual shares and assets ensure that even when the market is empty, a conversion rate between shares and assets is maintained.
  1. Flexible Operations:
  • Users can choose to specify the amount in either assets or shares, with the other parameter set to zero. This flexibility allows users to supply or borrow exactly the amount they need.
  • For instance, if a user wants to borrow a specific amount of assets, they can specify that amount in the assets parameter and set shares to 0. Conversely, if they want to specify the amount in terms of shares, they can set assets to 0. Note that it is particularly useful to fully close a position with a full repay by providing the number of shares, same for withdrawals.
  1. Simplified Interest Accrual:
  • Shares remain constant for a user if there are no interactions, simplifying the handling of interest accrual. This makes it easier to repay the exact amount without worrying about accruing interest on the assets.
  • This approach is particularly useful for withdrawals, as users can specify the amount in shares to avoid issues with rounding or changing asset values.

Example Usage

When someone supplies assets to a Morpho market (see the supply function in Morpho.sol):

Either assets or shares should be zero.

  • If assets > 0, the system calculates the corresponding shares.
  • If shares > 0, the system calculates the corresponding assets. Similarly, when borrowing, repaying or withdrawing from a market (see the respective function in Morpho.sol).

SharesMathLib.sol: Contains the functions for converting between shares and assets.

Morpho.sol: Contains the core functions for supply and borrow operations.

Morpho Documentation: Provides a technical reference for the supply function.

More information in the track position section in tutorials.