Skip to content

Commit e7bbd2f

Browse files
committed
added factory contract
1 parent 05cd9a4 commit e7bbd2f

File tree

6 files changed

+154
-2
lines changed

6 files changed

+154
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ docs/
1212

1313
# Dotenv file
1414
.env
15+
16+
# Node modules
17+
node_modules/

src/morpho-pyth/MorphoPythOracle.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
22
pragma solidity ^0.8.0;
33

4-
import {IOracle} from "../../lib/morpho-blue/src/interfaces/IOracle.sol";
4+
import {IMorphoPythOracle} from "./interfaces/IMorphoPythOracle.sol";
55
import {Math} from "../../lib/openzeppelin-contracts/contracts/utils/math/Math.sol";
66
import {IERC4626, VaultLib} from "./libraries/VaultLib.sol";
77
import {PythErrorsLib} from "./libraries/PythErrorsLib.sol";
88
import {PythFeedLib} from "./libraries/PythFeedLib.sol";
99

1010
import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
1111

12-
contract MorphoPythOracle is IOracle {
12+
contract MorphoPythOracle is IMorphoPythOracle {
1313
using Math for uint256;
1414
IPyth public immutable pyth;
1515
using VaultLib for IERC4626;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity 0.8.21;
3+
4+
import {IMorphoPythOracle} from "./interfaces/IMorphoPythOracle.sol";
5+
import {IMorphoPythOracleFactory} from "./interfaces/IMorphoPythOracleFactory.sol";
6+
import {IERC4626} from "./libraries/VaultLib.sol";
7+
8+
import {MorphoPythOracle} from "./MorphoPythOracle.sol";
9+
10+
/// @title MorphoPythOracleFactory
11+
/// @author Morpho Labs
12+
/// @notice This contract allows to create MorphoPythOracle oracles, and to index them easily.
13+
contract MorphoPythOracleFactory is IMorphoPythOracleFactory {
14+
/* STORAGE */
15+
16+
/// @inheritdoc IMorphoPythOracleFactory
17+
mapping(address => bool) public isMorphoPythOracle;
18+
19+
/* EXTERNAL */
20+
21+
/// @inheritdoc IMorphoPythOracleFactory
22+
function createMorphoPythOracle(
23+
address pyth,
24+
IERC4626 baseVault,
25+
uint256 baseVaultConversionSample,
26+
bytes32 baseFeed1,
27+
bytes32 baseFeed2,
28+
uint256 baseTokenDecimals,
29+
IERC4626 quoteVault,
30+
uint256 quoteVaultConversionSample,
31+
bytes32 quoteFeed1,
32+
bytes32 quoteFeed2,
33+
uint256 quoteTokenDecimals,
34+
uint256 priceFeedMaxAge,
35+
bytes32 salt
36+
) external returns (IMorphoPythOracle oracle) {
37+
oracle = new MorphoPythOracle{salt: salt}(
38+
pyth,
39+
baseVault,
40+
baseVaultConversionSample,
41+
baseFeed1,
42+
baseFeed2,
43+
baseTokenDecimals,
44+
quoteVault,
45+
quoteVaultConversionSample,
46+
quoteFeed1,
47+
quoteFeed2,
48+
quoteTokenDecimals,
49+
priceFeedMaxAge
50+
);
51+
52+
isMorphoPythOracle[address(oracle)] = true;
53+
54+
emit CreateMorphoPythOracle(msg.sender, address(oracle));
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity >=0.5.0;
3+
4+
import {IERC4626} from "../../interfaces/IERC4626.sol";
5+
import {IOracle} from "../../../lib/morpho-blue/src/interfaces/IOracle.sol";
6+
import {IPyth} from "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
7+
8+
/// @title IMorphoPythOracle
9+
/// @author Morpho Labs
10+
/// @custom:contact [email protected]
11+
/// @notice Interface of MorphoPythOracle.
12+
interface IMorphoPythOracle is IOracle {
13+
/// @notice Returns the address of the Pyth contract.
14+
function pyth() external view returns (IPyth);
15+
16+
/// @notice Returns the address of the base ERC4626 vault.
17+
function BASE_VAULT() external view returns (IERC4626);
18+
19+
/// @notice Returns the base vault conversion sample.
20+
function BASE_VAULT_CONVERSION_SAMPLE() external view returns (uint256);
21+
22+
/// @notice Returns the address of the quote ERC4626 vault.
23+
function QUOTE_VAULT() external view returns (IERC4626);
24+
25+
/// @notice Returns the quote vault conversion sample.
26+
function QUOTE_VAULT_CONVERSION_SAMPLE() external view returns (uint256);
27+
28+
/// @notice Returns the address of the first base feed.
29+
function BASE_FEED_1() external view returns (bytes32);
30+
31+
/// @notice Returns the address of the second base feed.
32+
function BASE_FEED_2() external view returns (bytes32);
33+
34+
/// @notice Returns the address of the first quote feed.
35+
function QUOTE_FEED_1() external view returns (bytes32);
36+
37+
/// @notice Returns the address of the second quote feed.
38+
function QUOTE_FEED_2() external view returns (bytes32);
39+
40+
/// @notice Returns the price scale factor, calculated at contract creation.
41+
function SCALE_FACTOR() external view returns (uint256);
42+
43+
/// @notice Returns the maximum age of the price feed.
44+
function PRICE_FEED_MAX_AGE() external view returns (uint256);
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity >=0.5.0;
3+
4+
import {IERC4626} from "../libraries/VaultLib.sol";
5+
import {IMorphoPythOracle} from "./IMorphoPythOracle.sol";
6+
7+
/// @title IMorphoPythOracleFactory
8+
/// @author Morpho Labs
9+
/// @notice Interface for MorphoPythOracleFactory
10+
interface IMorphoPythOracleFactory {
11+
/// @notice Emitted when a new Pyth oracle is created.
12+
/// @param oracle The address of the Pyth oracle.
13+
/// @param caller The caller of the function.
14+
event CreateMorphoPythOracle(address caller, address oracle);
15+
16+
/// @notice Whether a Pyth oracle vault was created with the factory.
17+
function isMorphoPythOracle(address target) external view returns (bool);
18+
19+
function createMorphoPythOracle(
20+
address pyth,
21+
IERC4626 baseVault,
22+
uint256 baseVaultConversionSample,
23+
bytes32 baseFeed1,
24+
bytes32 baseFeed2,
25+
uint256 baseTokenDecimals,
26+
IERC4626 quoteVault,
27+
uint256 quoteVaultConversionSample,
28+
bytes32 quoteFeed1,
29+
bytes32 quoteFeed2,
30+
uint256 quoteTokenDecimals,
31+
uint256 priceFeedMaxAge,
32+
bytes32 salt
33+
) external returns (IMorphoPythOracle oracle);
34+
}

test/MorphoPythOracleTest.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ contract MorphoPythOracleTest is Test {
6161
}
6262

6363
function testPythOracleWbtcUsdt() public {
64+
oracle = new MorphoPythOracle(
65+
address(mockPyth),
66+
vaultZero,
67+
1,
68+
pythWbtcUsdFeed,
69+
pythFeedZero,
70+
pythWbtcUsdTokenDecimals,
71+
vaultZero,
72+
1,
73+
pythUsdtUsdFeed,
74+
pythFeedZero,
75+
pythUsdtUsdTokenDecimals,
76+
oneHour
77+
);
6478
assertEq(
6579
oracle.price(),
6680
((uint256(int256(mockPyth.getPriceUnsafe(pythWbtcUsdFeed).price))) *

0 commit comments

Comments
 (0)