Skip to content

Commit 2d96b8e

Browse files
committed
docs: revamp scale factor comment
1 parent b7e95c3 commit 2d96b8e

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/ChainlinkOracle.sol

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ contract ChainlinkOracle is IOracle {
4545
BASE_FEED_2 = baseFeed2;
4646
QUOTE_FEED_1 = quoteFeed1;
4747
QUOTE_FEED_2 = quoteFeed2;
48-
// Let pB1 and pB2 be the base prices, and pQ1 and pQ2 the quote prices (price of 1e(decimals) asset), in a
49-
// common quote currency.
50-
// Chainlink feeds return pB1*b1FeedPrecision, pB2*b2FeedPrecision, pQ1*q1FeedPrecision and pQ2*q2FeedPrecision.
51-
// `price()` should return 1e36 * (pB1/1e(b1Decimals) * pB2/1e(b2Decimals)) / (pQ1/1e(q1Decimals) *
52-
// pQ2/1e(q2Decimals))
53-
// Yet `price()` returns (pB1*1e(b1FeedPrecision) * pB2*1e(b2FeedPrecision) * SCALE_FACTOR) /
54-
// (pQ1*1e(q1FeedPrecision) * pQ2*1e(q2FeedPrecision))
55-
// So 1e36 * (pB1/1e(b1Decimals) * pB2/1e(b2Decimals)) / (pQ1/1e(q1Decimals) * pQ2/1e(q2Decimals)) =
56-
// (pB1*1e(b1FeedPrecision) * pB2*1e(b2FeedPrecision) * SCALE_FACTOR) / (pQ1*1e(q1FeedPrecision) *
57-
// pQ2*1e(q2FeedPrecision))
58-
// So SCALE_FACTOR = 1e36 / 1e(b1Decimals) / 1e(b2Decimals) * 1e(q1Decimals) * 1e(q2Decimals) *
59-
// 1e(q1FeedPrecision) * 1e(q2FeedPrecision) / 1e(b1FeedPrecision) / 1e(b2FeedPrecision)
60-
// = 1e(36 + q1Decimals + q2Decimals + q1FeedPrecision + q2FeedPrecision - b1Decimals -
61-
// b2Decimals - b1FeedPrecision - b2FeedPrecision)
48+
// Let pB1 and pB2 be the base prices, and pQ1 and pQ2 the quote prices (price taking into account the
49+
// decimals of both tokens), in a common currency.
50+
// Let dB1, dB2, dB3, and dQ1, dQ2, dQ3 be the decimals of the tokens involved.
51+
// For example, pB1 is the number of 1e(dB2) of the second base asset that can be obtained from 1e(dB1) of
52+
// the first base asset.
53+
// We notably have dB3 = dQ3, because those two quantities are the decimals of the same common currency.
54+
// Let fpB1, fpB2, fpQ1 and fpQ2 be the feed precision of the corresponding prices.
55+
// Chainlink feeds return pB1*1e(fpB1), pB2*1e(fpB2), pQ1*1e(fpQ1) and pQ2*1e(fpQ2).
56+
// Because the Blue oracle does not take into account decimals, `price()` should return
57+
// 1e36 * (pB1*1e(dB2-dB1) * pB2*1e(dB3-dB2)) / (pQ1*1e(dQ2-dQ1) * pQ2*1e(dQ3-dQ2))
58+
// Yet `price()` returns (pB1*1e(fpB1) * pB2*1e(fpB2) * SCALE_FACTOR) / (pQ1*1e(fpQ1) * pQ2*1e(fpQ2))
59+
// So 1e36 * pB1 * pB2 * 1e(-dB1) / (pQ1 * pQ2 * 1e(-dQ1)) =
60+
// (pB1*1e(fpB1) * pB2*1e(fpB2) * SCALE_FACTOR) / (pQ1*1e(fpQ1) * pQ2*1e(fpQ2))
61+
// So SCALE_FACTOR = 1e36 / 1e(dB1) * 1e(dQ1) / 1e(fpB1) / 1e(fpB2) * 1e(fpQ1) * 1e(fpQ2)
62+
// = 1e(36 + dQ1 + fpQ1 + fpQ2 - dB1 - fpB1 - fpB2)
6263
SCALE_FACTOR = 10
6364
** (
6465
36 + quoteTokenDecimals + quoteFeed1.getDecimals() + quoteFeed2.getDecimals() - baseTokenDecimals

0 commit comments

Comments
 (0)