If one wstETH is worth 3,090 USDC, why does Morpho's oracle return ? The borrower posts the collateral in 18-decimal units. The loan is recorded in six-decimal USDC. price() has to take the core from one set of integer units to the other, with a further scale that Morpho removes at the end. A correct quote for one whole token is a quick way to catch a reversed feed or a misplaced power of ten.
Recall that there are two occurrences where oracle.price() gets invoked in Morpho.sol. The first occurrence is in liquidate():
function liquidate(
MarketParams memory marketParams,
address borrower,
uint256 seizedAssets,
uint256 repaidShares,
bytes calldata data
) external returns (uint256, uint256) {
Id id = marketParams.id();
require(market[id].lastUpdate != 0, ErrorsLib.MARKET_NOT_CREATED);
require(UtilsLib.exactlyOneZero(seizedAssets, repaidShares), ErrorsLib.INCONSISTENT_INPUT);
_accrueInterest(marketParams, id);
{
uint256 collateralPrice = IOracle(marketParams.oracle).price();
require(!_isHealthy(marketParams, id, borrower, collateralPrice), ErrorsLib.HEALTHY_POSITION);
...Note that fetching collateral price first and then calling the 4-parameter version of _isHealthy() is the same as calling the 3-parameter version of _isHealthy() directly:
function _isHealthy(MarketParams memory marketParams, Id id, address borrower) internal view returns (bool) {
if (position[id][borrower].borrowShares == 0) return true;
uint256 collateralPrice = IOracle(marketParams.oracle).price();
return _isHealthy(marketParams, id, borrower, collateralPrice);
}oracle.price() is also invoked in borrow() and withdrawCollateral() implicitly since they call the 3-parameter version of _isHealthy().
The core uses the quote to test borrowing capacity and to convert between repaid debt and seized collateral during liquidation. The three-argument health check returns immediately for a position with zero borrow shares. Interest accrual reads the IRM, and ordinary supply-share conversions use market totals. Morpho Blue health and liquidation calculations
Factory and oracle.price()#
Morpho provides a factory (MorphoChainlinkOracleV2Factory.sol) that allows users to deploy customized oracles themselves:
function createMorphoChainlinkOracleV2(
IERC4626 baseVault,
uint256 baseVaultConversionSample,
AggregatorV3Interface baseFeed1,
AggregatorV3Interface baseFeed2,
uint256 baseTokenDecimals,
IERC4626 quoteVault,
uint256 quoteVaultConversionSample,
AggregatorV3Interface quoteFeed1,
AggregatorV3Interface quoteFeed2,
uint256 quoteTokenDecimals,
bytes32 salt
) external returns (IMorphoChainlinkOracleV2 oracle) {
oracle = new MorphoChainlinkOracleV2{salt: salt}(
baseVault,
baseVaultConversionSample,
baseFeed1,
baseFeed2,
baseTokenDecimals,
quoteVault,
quoteVaultConversionSample,
quoteFeed1,
quoteFeed2,
quoteTokenDecimals
);
isMorphoChainlinkOracleV2[address(oracle)] = true;
}In the oracle implementation, price() is the only function (besides the constructor) in MorphoChainlinkOracleV2.sol:
function price() external view returns (uint256) {
return SCALE_FACTOR.mulDiv(
BASE_VAULT.getAssets(BASE_VAULT_CONVERSION_SAMPLE) * BASE_FEED_1.getPrice() * BASE_FEED_2.getPrice(),
QUOTE_VAULT.getAssets(QUOTE_VAULT_CONVERSION_SAMPLE) * QUOTE_FEED_1.getPrice() * QUOTE_FEED_2.getPrice()
);
}mulDiv is OpenZeppelin's (x * y) / z with full 512-bit intermediate precision — so SCALE_FACTOR.mulDiv(baseLeg, quoteLeg) computes SCALE_FACTOR * baseLeg / quoteLeg without overflow on the intermediate product.
To check a configured oracle, write down the unit of each leg before multiplying any feed answers.
From a bird's-eye view, price() can be read as one ratio:
where:
- base leg =
BASE_VAULT.getAssets(sample) * BASE_FEED_1.getPrice() * BASE_FEED_2.getPrice() - quote leg =
QUOTE_VAULT.getAssets(sample) * QUOTE_FEED_1.getPrice() * QUOTE_FEED_2.getPrice()
Everything else in the contract is just “wiring” those legs and precomputing SCALE_FACTOR so the output lands in Morpho’s expected format.
A quick Chainlink refresher (what getPrice() actually returns)#
Morpho’s Chainlink oracle does not talk to Chainlink feeds directly; it goes through ChainlinkDataFeedLib.
library ChainlinkDataFeedLib {
/// @dev Performs safety checks and returns the latest price of a `feed`.
/// @dev When `feed` is the address zero, returns 1.
/// @dev Notes on safety checks:
/// - L2s are not supported.
/// - Staleness is not checked because it's assumed that the Chainlink feed keeps its promises on this.
/// - The price is not checked to be in the min/max bounds because it's assumed that the Chainlink feed keeps its
/// promises on this.
function getPrice(AggregatorV3Interface feed) internal view returns (uint256) {
if (address(feed) == address(0)) return 1;
(, int256 answer,,,) = feed.latestRoundData();
require(answer >= 0, ErrorsLib.NEGATIVE_ANSWER);
return uint256(answer);
}
/// @dev Returns the number of decimals of a `feed`.
/// @dev When `feed` is the address zero, returns 0.
function getDecimals(AggregatorV3Interface feed) internal view returns (uint256) {
if (address(feed) == address(0)) return 0;
return feed.decimals();
}
}Reading that library clarifies several behavioral details that are easy to miss:
feed.latestRoundData()is called and onlyansweris used.- The library accepts zero and rejects negative answers. A zero base feed makes the numerator zero. A zero quote feed makes the denominator zero, so the price calculation reverts. These outcomes assume the other factors are valid.
- The library reads the answer without enforcing a maximum age using
updatedAt. Freshness assumptions belong to the chosen feeds and any additional checks in the integration. - When a feed address is
address(0),getPrice()returns 1 (a multiplicative no-op), andgetDecimals()returns 0.
The “address(0) means 1” convention is the key that makes “0/1/2 feeds per leg” possible with a single code path.
What does one raw collateral unit buy?#
As per the doc, Morpho expects oracle.price() to be:
This is exactly why Morpho stores it in a local variable named collateralPrice.
// Morpho.sol
uint256 collateralPrice = IOracle(marketParams.oracle).price();Morpho later consumes the oracle output by converting collateral → loan units using:
// Morpho.sol
uint256 maxBorrow = collateral.mulDivDown(collateralPrice, ORACLE_PRICE_SCALE).wMulDown(lltv);with ORACLE_PRICE_SCALE = 1e36. You can see the same conversion pattern in liquidation too:
uint256 seizedAssetsQuoted = seizedAssets.mulDivUp(collateralPrice, ORACLE_PRICE_SCALE);When the collateral or loan token is a vault share#
Because Morpho can use ERC4626 vault shares as either collateral or loan token.
Example: a collateral token might be an ERC4626 vault share token like sDAI, or a stablecoin yield vault share. In that case, the oracle must incorporate the conversion shares → underlying assets, because the external price feeds are usually expressed in terms of the underlying asset, not in “shares”.
That is exactly what VaultLib.getAssets does:
library VaultLib {
/// @dev Converts `shares` into the corresponding assets on the `vault`.
/// @dev When `vault` is the address zero, returns 1.
function getAssets(IERC4626 vault, uint256 shares) internal view returns (uint256) {
if (address(vault) == address(0)) return 1;
return vault.convertToAssets(shares);
}
}So the following code:
BASE_VAULT.getAssets(BASE_VAULT_CONVERSION_SAMPLE)means “how many underlying base-assets correspond to BASE_VAULT_CONVERSION_SAMPLE shares?”.
Here BASE_VAULT_CONVERSION_SAMPLE is typically just 1e18 as shown in the test case in MorphoChainlinkOracleV2FactoryTest.sol:
IMorphoChainlinkOracleV2 oracle = factory.createMorphoChainlinkOracleV2(
sDaiVault, 1e18, daiEthFeed, feedZero, 18, vaultZero, 1, usdcEthFeed, feedZero, 6, salt
);Same for the quote side. When there is no vault, the factor is 1 and disappears.
Put a unit on every feed#
The base is what the borrower posts. The quote is what they owe. A provider may label a feed “ETH/USD”. Its answer is measured in USD per ETH. Use those words when multiplying. For LINK collateral and a DAI loan, suppose the first two feeds report ETH per LINK and USD per ETH, and the quote feed reports USD per DAI. Then
The intermediate ETH and USD units cancel. An unused second quote feed has address zero and contributes 1. If there is a single feed that already reports DAI per LINK, it can supply the whole base leg with the other feeds omitted. If collateral consists of vault shares, multiply the base leg by the underlying assets per share. The contract evaluates those conversions with configured integer samples on each leg.
The two feed slots on each side support a direct pair or a pair routed through an intermediate asset. The two legs must meet in the same denomination. If a quote-side feed reports USD per USDC, dividing by its answer converts a USD valuation into USDC. That tells us which way the result should move if the stablecoin’s dollar price changes. MorphoChainlinkOracleV2 and its constructor
Where the scale factor comes from#
SCALE_FACTOR is a precomputed constant that makes the returned integer have the right meaning and scaling.
At runtime, the oracle multiplies:
- vault conversions (in token smallest units), and
- Chainlink feed answers (which are integers scaled by the feed’s
decimals()),
then divides base by quote.
But Morpho expects the oracle output (quoted in loan token per collateral token ) to be scaled by .
The constructor comment in MorphoChainlinkOracleV2.sol does the derivation in the general 2-hop case. Using the exact same symbols:
- Assets: with decimals .
- Base prices: .
- Quote prices: .
First, convert each hop into “per-1-asset” form by applying decimal shifts, then multiply along each path. Morpho’s target is:
Then simplify the exponent terms (this is exactly the step shown in the Solidity comment):
Now connect that target to what the contract computes at runtime.
Let be the feed precisions (i.e. feed.decimals()), so the feeds return:
Ignoring vaults for one line, the runtime structure is:
So SCALE_FACTOR is chosen such that this equals the target .
Solving for it (again, exactly matching the Solidity comment) gives:
Finally, incorporate the vault conversion samples.
At runtime the numerator contains the assets obtained from the base sample and the denominator contains the assets obtained from the quote sample. To normalize those two samples, the constructor multiplies the scale factor by the following ratio.
Putting everything together yields the compact exponent form used in the article and reflected in code:
And the code implements exactly that:
SCALE_FACTOR = 10 ** (
36
+ quoteTokenDecimals
+ quoteFeed1.getDecimals()
+ quoteFeed2.getDecimals()
- baseTokenDecimals
- baseFeed1.getDecimals()
- baseFeed2.getDecimals()
) * quoteVaultConversionSample / baseVaultConversionSample;An end-to-end example: wstETH collateral, USDC loan#
In this example we configure the oracle so that it returns wstETH price (as collateral token) quoted in USDC (as loan token). Then Morpho converts collateral to loan units as:
Step 1 — Choose feeds so units cancel to USDC/wstETH#
Pick USD as the common denomination. The base feeds report ETH per wstETH and USD per ETH. The quote feed reports USD per USDC. Leave the second quote feed empty. The feed labels in the configuration are wstETH/ETH, ETH/USD and USDC/USD respectively. Their answer units give
Assume no vault shares here, then:
BASE_VAULT = QUOTE_VAULT = address(0)BASE_VAULT_CONVERSION_SAMPLE = QUOTE_VAULT_CONVERSION_SAMPLE = 1
Step 2 — Plug in decimals#
- Token decimals:
baseTokenDecimals = 18(wstETH)quoteTokenDecimals = 6(USDC)
- Feed decimals (typical values; always read them from the feed):
wstETH/ETH: 18ETH/USD: 8USDC/USD: 8- any
address(0)feed has 0 decimals by definition (getDecimalsreturns 0)
So the constructor exponent becomes:
Therefore SCALE_FACTOR = 10^6 (since vault samples are both 1).
Step 3 — Do a numeric sanity check#
Suppose the market is roughly:
In feed terms (i.e., scaled by feed decimals), that’s roughly:
- wstETH/ETH:
getPrice() - ETH/USD:
getPrice() - USDC/USD:
getPrice()
Then the oracle’s runtime computation is:
Now validate the Morpho-side conversion on exactly 1 wstETH (which is smallest units):
That equals 3090 USDC in USDC smallest units (), which matches the intuitive spot price.
Change only the quote feed to 0.98 USD per USDC while wstETH is still worth 3,090 USD. It now takes more USDC to express the same collateral value. After rounding down to six decimals, one wstETH converts to 3,153.061224 USDC. A calculation that makes the USDC amount fall has inverted the quote leg.
Worth sharing?
Send this analysis to another reviewer.
