MatchingEngine

Git Source

Inherits: MorphoUtils

Author: Morpho Labs.

Smart contract managing the matching engine.

Functions

_matchSuppliers

INTERNAL ///

Matches suppliers' liquidity waiting on Aave up to the given _amount and moves it to peer-to-peer.

Note: This function expects Aave's exchange rate and peer-to-peer indexes to have been updated.

function _matchSuppliers(address _poolToken, uint256 _amount, uint256 _maxGasForMatching)
    internal
    returns (uint256 matched, uint256 gasConsumedInMatching);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market from which to match suppliers.
_amountuint256The token amount to search for (in underlying).
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

Returns

NameTypeDescription
matcheduint256The amount of liquidity matched (in underlying).
gasConsumedInMatchinguint256The amount of gas consumed within the matching loop.

_unmatchSuppliers

Unmatches suppliers' liquidity in peer-to-peer up to the given _amount and moves it to Aave.

Note: This function expects Aave's exchange rate and peer-to-peer indexes to have been updated.

function _unmatchSuppliers(address _poolToken, uint256 _amount, uint256 _maxGasForMatching)
    internal
    returns (uint256 unmatched);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market from which to unmatch suppliers.
_amountuint256The amount to search for (in underlying).
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

Returns

NameTypeDescription
unmatcheduint256The amount unmatched (in underlying).

_matchBorrowers

Matches borrowers' liquidity waiting on Aave up to the given _amount and moves it to peer-to-peer.

Note: This function expects stored indexes to have been updated.

function _matchBorrowers(address _poolToken, uint256 _amount, uint256 _maxGasForMatching)
    internal
    returns (uint256 matched, uint256 gasConsumedInMatching);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market from which to match borrowers.
_amountuint256The amount to search for (in underlying).
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

Returns

NameTypeDescription
matcheduint256The amount of liquidity matched (in underlying).
gasConsumedInMatchinguint256The amount of gas consumed within the matching loop.

_unmatchBorrowers

Unmatches borrowers' liquidity in peer-to-peer for the given _amount and moves it to Aave.

Note: This function expects and peer-to-peer indexes to have been updated.

function _unmatchBorrowers(address _poolToken, uint256 _amount, uint256 _maxGasForMatching)
    internal
    returns (uint256 unmatched);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market from which to unmatch borrowers.
_amountuint256The amount to unmatch (in underlying).
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

Returns

NameTypeDescription
unmatcheduint256The amount unmatched (in underlying).

_updateSupplierInDS

Updates the given _user's position in the supplier data structures.

function _updateSupplierInDS(address _poolToken, address _user) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market on which to update the suppliers data structure.
_useraddressThe address of the user.

_updateBorrowerInDS

Updates the given _user's position in the borrower data structures.

function _updateBorrowerInDS(address _poolToken, address _user) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market on which to update the borrowers data structure.
_useraddressThe address of the user.

Events

SupplierPositionUpdated

Emitted when the position of a supplier is updated.

event SupplierPositionUpdated(
    address indexed _user, address indexed _poolToken, uint256 _balanceOnPool, uint256 _balanceInP2P
);

BorrowerPositionUpdated

Emitted when the position of a borrower is updated.

event BorrowerPositionUpdated(
    address indexed _user, address indexed _poolToken, uint256 _balanceOnPool, uint256 _balanceInP2P
);

Structs

UnmatchVars

STRUCTS ///

struct UnmatchVars {
    uint256 p2pIndex;
    uint256 toUnmatch;
    uint256 poolIndex;
}

MatchVars

struct MatchVars {
    uint256 p2pIndex;
    uint256 toMatch;
    uint256 poolIndex;
}