Skip to content

Commit 92cfde4

Browse files
authored
Merge pull request #34 from morpho-org/fix/rubilmax
docs(oracle): clarify SCALE_FACTOR def
2 parents d0cde07 + 5aef5a6 commit 92cfde4

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/ChainlinkOracle.sol

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ contract ChainlinkOracle is IOracle {
5353
uint256 baseTokenDecimals,
5454
uint256 quoteTokenDecimals
5555
) {
56-
// The vault parameter is used for ERC4626 tokens, to price its shares.
57-
// It is used to price `VAULT_CONVERSION_SAMPLE` of the vault shares, so it requires dividing by that number,
58-
// hence the division by `VAULT_CONVERSION_SAMPLE` in the `SCALE_FACTOR` definition.
56+
// The ERC4626 vault parameter is used to price `VAULT_CONVERSION_SAMPLE` of its shares, so it requires dividing
57+
// by that number, hence the division by `VAULT_CONVERSION_SAMPLE` in the `SCALE_FACTOR` definition.
5958
// Verify that vault = address(0) => vaultConversionSample = 1.
6059
require(
6160
address(vault) != address(0) || vaultConversionSample == 1, ErrorsLib.VAULT_CONVERSION_SAMPLE_IS_NOT_ONE
@@ -68,21 +67,30 @@ contract ChainlinkOracle is IOracle {
6867
BASE_FEED_2 = baseFeed2;
6968
QUOTE_FEED_1 = quoteFeed1;
7069
QUOTE_FEED_2 = quoteFeed2;
71-
// Let pB1 and pB2 be the base prices, and pQ1 and pQ2 the quote prices (price taking into account the
72-
// decimals of both tokens), in a common currency.
73-
// We tackle the most general case in the remainder of this comment, where we assume that no feed is the address
74-
// zero. Similar explanations would hold in the case where some of the feeds are the address zero.
75-
// Let dB1, dB2, dB3, and dQ1, dQ2, dQ3 be the decimals of the tokens involved.
76-
// For example, pB1 is the number of 1e(dB2) of the second base asset that can be obtained from 1e(dB1) of
77-
// the first base asset.
78-
// We notably have dB3 = dQ3, because those two quantities are the decimals of the same common currency.
79-
// Let fpB1, fpB2, fpQ1 and fpQ2 be the feed precision of the corresponding prices.
80-
// Chainlink feeds return pB1*1e(fpB1), pB2*1e(fpB2), pQ1*1e(fpQ1) and pQ2*1e(fpQ2).
81-
// Because the Blue oracle does not take into account decimals, `price()` should return
82-
// 1e36 * (pB1*1e(dB2-dB1) * pB2*1e(dB3-dB2)) / (pQ1*1e(dQ2-dQ1) * pQ2*1e(dQ3-dQ2))
83-
// Yet `price()` returns (pB1*1e(fpB1) * pB2*1e(fpB2) * SCALE_FACTOR) / (pQ1*1e(fpQ1) * pQ2*1e(fpQ2))
84-
// So 1e36 * pB1 * pB2 * 1e(-dB1) / (pQ1 * pQ2 * 1e(-dQ1)) =
85-
// (pB1*1e(fpB1) * pB2*1e(fpB2) * SCALE_FACTOR) / (pQ1*1e(fpQ1) * pQ2*1e(fpQ2))
70+
71+
// In the following comment, we explain the general case (where we assume that no feed is the address zero)
72+
// how to scale the output price as Morpho Blue expects, given the input feed prices.
73+
// Similar explanations would hold in the case where some of the feeds are the address zero.
74+
75+
// Let B1, B2, Q1, Q2, C be 5 assets, each respectively having dB1, dB2, dQ1, dQ2, dC decimals.
76+
// Let pB1 and pB2 be the base prices, and pQ1 and pQ2 the quote prices, so that:
77+
// - pB1 is the quantity of 1e(dB2) assets B2 that can be exchanged for 1e(dB1) assets B1.
78+
// - pB2 is the quantity of 1e(dC) assets C that can be exchanged for 1e(dB2) assets B2.
79+
// - pQ1 is the quantity of 1e(dQ2) assets Q2 that can be exchanged for 1e(dQ1) assets Q1.
80+
// - pQ2 is the quantity of 1e(dC) assets C that can be exchanged for 1e(dQ2) assets B2.
81+
82+
// Morpho Blue expects `price()` to be the quantity of 1 asset Q1 that can be exchanged for 1 asset B1,
83+
// scaled by 1e36:
84+
// 1e36 * (pB1 * 1e(dB2 - dB1)) * (pB2 * 1e(dC - dB2)) / ((pQ1 * 1e(dQ2 - dQ1)) * (pQ2 * 1e(dC - dQ2)))
85+
// = 1e36 * (pB1 * 1e(-dB1) * pB2) / (pQ1 * 1e(-dQ1) * pQ2)
86+
87+
// Let fpB1, fpB2, fpQ1, fpQ2 be the feed precision of the respective prices pB1, pB2, pQ1, pQ2.
88+
// Chainlink feeds return pB1 * 1e(fpB1), pB2 * 1e(fpB2), pQ1 * 1e(fpQ1) and pQ2 * 1e(fpQ2).
89+
90+
// Based on the implementation of `price()` below, the value of `SCALE_FACTOR` should thus satisfy:
91+
// (pB1 * 1e(fpB1)) * (pB2 * 1e(fpB2)) * SCALE_FACTOR / ((pQ1 * 1e(fpQ1)) * (pQ2 * 1e(fpQ2)))
92+
// = 1e36 * (pB1 * 1e(-dB1) * pB2) / (pQ1 * 1e(-dQ1) * pQ2)
93+
8694
// So SCALE_FACTOR = 1e36 * 1e(-dB1) * 1e(dQ1) * 1e(-fpB1) * 1e(-fpB2) * 1e(fpQ1) * 1e(fpQ2)
8795
// = 1e(36 + dQ1 + fpQ1 + fpQ2 - dB1 - fpB1 - fpB2)
8896
SCALE_FACTOR = 10

0 commit comments

Comments
 (0)