Skip to content

Commit 197d2f0

Browse files
committed
refactor: improve documentation and structure
1 parent aac4f2c commit 197d2f0

File tree

6 files changed

+42
-53
lines changed

6 files changed

+42
-53
lines changed

.github/workflows/foundry.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ jobs:
3434
run: |
3535
forge test -vvv
3636
id: test
37-
env:
38-
ETH_RPC_URL: ${{ secrets.ETH_RPC_URL }}
37+

src/morpho-pyth/MorphoPythOracle.sol

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
1515
/// @notice Morpho Blue oracle using Pyth Price Feeds.
1616
contract MorphoPythOracle is IMorphoPythOracle {
1717
using Math for uint256;
18+
1819
IPyth public immutable pyth;
20+
1921
using VaultLib for IERC4626;
2022

2123
/* IMMUTABLES */
@@ -73,18 +75,11 @@ contract MorphoPythOracle is IMorphoPythOracle {
7375
PythErrorsLib.VAULT_CONVERSION_SAMPLE_IS_NOT_ONE
7476
);
7577
require(
76-
address(quoteVault) != address(0) ||
77-
quoteVaultConversionSample == 1,
78+
address(quoteVault) != address(0) || quoteVaultConversionSample == 1,
7879
PythErrorsLib.VAULT_CONVERSION_SAMPLE_IS_NOT_ONE
7980
);
80-
require(
81-
baseVaultConversionSample != 0,
82-
PythErrorsLib.VAULT_CONVERSION_SAMPLE_IS_ZERO
83-
);
84-
require(
85-
quoteVaultConversionSample != 0,
86-
PythErrorsLib.VAULT_CONVERSION_SAMPLE_IS_ZERO
87-
);
81+
require(baseVaultConversionSample != 0, PythErrorsLib.VAULT_CONVERSION_SAMPLE_IS_ZERO);
82+
require(quoteVaultConversionSample != 0, PythErrorsLib.VAULT_CONVERSION_SAMPLE_IS_ZERO);
8883
BASE_VAULT = baseVault;
8984
BASE_VAULT_CONVERSION_SAMPLE = baseVaultConversionSample;
9085
QUOTE_VAULT = quoteVault;
@@ -95,17 +90,14 @@ contract MorphoPythOracle is IMorphoPythOracle {
9590
QUOTE_FEED_2 = quoteFeed2;
9691

9792
pyth = IPyth(pyth_);
98-
SCALE_FACTOR =
99-
(10 **
100-
(36 +
101-
quoteTokenDecimals +
102-
PythFeedLib.getDecimals(pyth, QUOTE_FEED_1) +
103-
PythFeedLib.getDecimals(pyth, QUOTE_FEED_2) -
104-
baseTokenDecimals -
105-
PythFeedLib.getDecimals(pyth, BASE_FEED_1) -
106-
PythFeedLib.getDecimals(pyth, BASE_FEED_2)) *
107-
quoteVaultConversionSample) /
108-
baseVaultConversionSample;
93+
SCALE_FACTOR = (
94+
10
95+
** (
96+
36 + quoteTokenDecimals + PythFeedLib.getDecimals(pyth, QUOTE_FEED_1)
97+
+ PythFeedLib.getDecimals(pyth, QUOTE_FEED_2) - baseTokenDecimals
98+
- PythFeedLib.getDecimals(pyth, BASE_FEED_1) - PythFeedLib.getDecimals(pyth, BASE_FEED_2)
99+
) * quoteVaultConversionSample
100+
) / baseVaultConversionSample;
109101

110102
PRICE_FEED_MAX_AGE = priceFeedMaxAge;
111103
}
@@ -114,22 +106,13 @@ contract MorphoPythOracle is IMorphoPythOracle {
114106

115107
/// @inheritdoc IOracle
116108
function price() external view returns (uint256) {
117-
return
118-
SCALE_FACTOR.mulDiv(
119-
BASE_VAULT.getAssets(BASE_VAULT_CONVERSION_SAMPLE) *
120-
PythFeedLib.getPrice(
121-
pyth,
122-
BASE_FEED_1,
123-
PRICE_FEED_MAX_AGE
124-
) *
125-
PythFeedLib.getPrice(pyth, BASE_FEED_2, PRICE_FEED_MAX_AGE),
126-
QUOTE_VAULT.getAssets(QUOTE_VAULT_CONVERSION_SAMPLE) *
127-
PythFeedLib.getPrice(
128-
pyth,
129-
QUOTE_FEED_1,
130-
PRICE_FEED_MAX_AGE
131-
) *
132-
PythFeedLib.getPrice(pyth, QUOTE_FEED_2, PRICE_FEED_MAX_AGE)
133-
);
109+
return SCALE_FACTOR.mulDiv(
110+
BASE_VAULT.getAssets(BASE_VAULT_CONVERSION_SAMPLE)
111+
* PythFeedLib.getPrice(pyth, BASE_FEED_1, PRICE_FEED_MAX_AGE)
112+
* PythFeedLib.getPrice(pyth, BASE_FEED_2, PRICE_FEED_MAX_AGE),
113+
QUOTE_VAULT.getAssets(QUOTE_VAULT_CONVERSION_SAMPLE)
114+
* PythFeedLib.getPrice(pyth, QUOTE_FEED_1, PRICE_FEED_MAX_AGE)
115+
* PythFeedLib.getPrice(pyth, QUOTE_FEED_2, PRICE_FEED_MAX_AGE)
116+
);
134117
}
135118
}

src/morpho-pyth/interfaces/IMorphoPythOracleFactory.sol

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ interface IMorphoPythOracleFactory {
2929
/// @param baseVaultConversionSample The sample amount of base vault shares used to convert to underlying.
3030
/// Pass 1 if the base asset is not a vault. Should be chosen such that converting `baseVaultConversionSample` to
3131
/// assets has enough precision.
32-
/// @param baseFeed1 First base feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds instead of passing 1.
33-
/// @param baseFeed2 Second base feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds instead of passing 1.
32+
/// @param baseFeed1 First base feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds instead
33+
/// of passing 1.
34+
/// @param baseFeed2 Second base feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds instead
35+
/// of passing 1.
3436
/// @param baseTokenDecimals Base token decimals.
3537
/// @param quoteVault Quote vault. Pass address zero to omit this parameter.
3638
/// @param quoteVaultConversionSample The sample amount of quote vault shares used to convert to underlying.
3739
/// Pass 1 if the quote asset is not a vault. Should be chosen such that converting `quoteVaultConversionSample` to
3840
/// assets has enough precision.
39-
/// @param quoteFeed1 First quote feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds instead of passing 1.
40-
/// @param quoteFeed2 Second quote feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds instead of passing 1.
41+
/// @param quoteFeed1 First quote feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds
42+
/// instead of passing 1.
43+
/// @param quoteFeed2 Second quote feed. Pass bytes32(0) if the price = 1. We recommend using stablecoin feeds
44+
/// instead of passing 1.
4145
/// @param quoteTokenDecimals Quote token decimals.
4246
/// @param priceFeedMaxAge The maximum age in secondsfor the oracles prices to be considered valid.
4347
/// @param salt The salt to use for the CREATE2.

src/morpho-pyth/libraries/PythFeedLib.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {PythErrorsLib} from "./PythErrorsLib.sol";
77
/// @title PythFeedLib
88
/// @author Pyth Data Association
99
/// @notice Library exposing functions to interact with a Pyth feed.
10+
1011
library PythFeedLib {
1112
/// @dev Returns the price of a `priceId`.
1213
/// @dev When `priceId` is the address zero, returns 1.
@@ -20,6 +21,7 @@ library PythFeedLib {
2021
}
2122
/// @dev Returns the number of decimals of a `priceId`.
2223
/// @dev When `priceId` is the address zero, returns 0.
24+
2325
function getDecimals(IPyth pyth, bytes32 priceId) internal view returns (uint256) {
2426
if (priceId == bytes32(0)) return 0;
2527

test/MorphoPythOracleTest.sol

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pragma solidity ^0.8.0;
2+
23
import "../lib/forge-std/src/Test.sol";
34
import "@pythnetwork/pyth-sdk-solidity/MockPyth.sol";
45
import "../src/morpho-pyth/MorphoPythOracle.sol";
@@ -77,14 +78,14 @@ contract MorphoPythOracleTest is Test {
7778
);
7879
assertEq(
7980
oracle.price(),
80-
((uint256(int256(mockPyth.getPriceUnsafe(pythWbtcUsdFeed).price))) *
81-
10 **
82-
(36 +
83-
pythUsdtUsdTokenDecimals +
84-
uint256(-1 * int256(-6)) -
85-
pythWbtcUsdTokenDecimals -
86-
uint256(-1 * int256(-8)))) /
87-
uint256(int256(mockPyth.getPriceUnsafe(pythUsdtUsdFeed).price))
81+
(
82+
(uint256(int256(mockPyth.getPriceUnsafe(pythWbtcUsdFeed).price)))
83+
* 10
84+
** (
85+
36 + pythUsdtUsdTokenDecimals + uint256(-1 * int256(-6)) - pythWbtcUsdTokenDecimals
86+
- uint256(-1 * int256(-8))
87+
)
88+
) / uint256(int256(mockPyth.getPriceUnsafe(pythUsdtUsdFeed).price))
8889
);
8990
}
9091
}

test/helpers/Constants.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ uint256 constant pythUsdtUsdTokenDecimals = 6;
1919
bytes32 constant pythCbethUsdFeed = 0x15ecddd26d49e1a8f1de9376ebebc03916ede873447c1255d2d5891b92ce5717;
2020
// Time constants
2121
uint256 constant oneHour = 3600;
22-
uint256 constant oneMinute = 60;
22+
uint256 constant oneMinute = 60;

0 commit comments

Comments
 (0)