Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Myso oracle #605

Open
wants to merge 58 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
8d2b348
package-lock update
jpick713 Sep 5, 2023
2c0605a
Merge branch 'main' of github.com:mysofinance/v2 into main
jpick713 Oct 17, 2023
12d1e9f
Merge branch 'main' of github.com:mysofinance/v2 into main
jpick713 Feb 13, 2024
272a65b
added in myso oracle contracts
jpick713 Mar 18, 2024
4ab992e
added updates to myso oracle
jpick713 Mar 18, 2024
94642b5
add tests, finish contract, and update readme
jpick713 Mar 18, 2024
9f71301
added meth, rpl, and usdc plus test
jpick713 Mar 20, 2024
56e4457
added usdt and dai to test
jpick713 Mar 21, 2024
8f5e125
removed comments, shortened update time, updated tests, condensed con…
jpick713 Mar 21, 2024
e7d218f
remove unused variable commented out
jpick713 Mar 22, 2024
9e84949
added ankrEth to oracle
jpick713 Mar 27, 2024
f22af37
added start of math calculation in test token manager
jpick713 Mar 29, 2024
7ced3d9
refactored to use total loan value and have calcs in oracle
jpick713 Mar 29, 2024
6e3044d
added some constants to test token manager
jpick713 Mar 30, 2024
f0caf0c
added setter for IOO vault and included into borrow check
jpick713 Mar 31, 2024
059a41e
added a test net myso oracle in
jpick713 Apr 8, 2024
b29d0c3
fixed test for oracle with new curve
jpick713 Apr 9, 2024
d270927
myt token manager
asardon Apr 9, 2024
48a513a
added borrows and examined price along curve
jpick713 Apr 9, 2024
d109488
Merge branch 'myso-oracle' into myt-token-manager
jpick713 Apr 10, 2024
634d6b2
Merge pull request #606 from mysofinance/myt-token-manager
jpick713 Apr 10, 2024
f6e20dd
added extra note
jpick713 Apr 10, 2024
739b419
updated token manager
asardon Apr 10, 2024
9bf44a0
Merge branch 'myt-token-manager' of https://github.com/mysofinance/v2…
asardon Apr 10, 2024
1eeb59d
updated token manager
asardon Apr 10, 2024
cabcf89
updated token manager
asardon Apr 10, 2024
fac1cb4
Merge branch 'myso-oracle' into myt-token-manager
jpick713 Apr 10, 2024
2fe6a9c
Merge pull request #607 from mysofinance/myt-token-manager
jpick713 Apr 10, 2024
7481fe2
updated token manager
asardon Apr 11, 2024
251f69f
Merge branch 'myt-token-manager' of https://github.com/mysofinance/v2…
asardon Apr 11, 2024
bdaa74f
access via sig
asardon Apr 11, 2024
3ff1f21
access via sig
asardon Apr 11, 2024
75eaa96
access via sig
asardon Apr 11, 2024
22bad12
added tests
asardon Apr 11, 2024
a7e5b07
added tests
asardon Apr 11, 2024
be25020
Merge pull request #608 from mysofinance/myt-token-manager
jpick713 Apr 11, 2024
7cd513b
Merge pull request #609 from mysofinance/myso-token-manager-sig
jpick713 Apr 11, 2024
1d7bda8
added one-off signatures
asardon Apr 12, 2024
bc19f95
added public already claimed getter
asardon Apr 12, 2024
10ba136
Merge pull request #610 from mysofinance/myso-token-manager-sig
jpick713 Apr 12, 2024
79f5d11
added rsweth and fsusd to oracle
jpick713 Apr 16, 2024
8c3e15e
Merge branch 'myso-oracle' of github.com:mysofinance/v2 into myso-oracle
jpick713 Apr 16, 2024
9a32576
simplified cap handling for myso token manager and added test
theuprotocol Apr 16, 2024
5bcdc63
Merge pull request #611 from mysofinance/myso-token-manager-sig
jpick713 Apr 16, 2024
298bfa9
updated default behavior to ignore caps when set to 0
theuprotocol Apr 17, 2024
f16aa5d
updated deployment script
theuprotocol Apr 17, 2024
d2e3756
added crvusd to oracle
jpick713 Apr 22, 2024
fd1bffc
Merge branch 'myso-oracle' of github.com:mysofinance/v2 into myso-oracle
jpick713 Apr 22, 2024
3f5ad21
Merge pull request #612 from mysofinance/myso-token-manager-sig
jpick713 Apr 22, 2024
c1d245a
started adding in arbitrum oracle
jpick713 Apr 22, 2024
2561f3d
add missing interfaces
jpick713 Apr 22, 2024
817fc96
added arbitrum oracle tests
jpick713 Apr 22, 2024
4e1b146
add deployment scripts, bnb myso oracle, arb token manager with p2poo…
asardon May 2, 2024
a62d1ca
add mantle oracle
jpick713 May 3, 2024
f8dcfb2
Merge branch 'myso-oracle' of github.com:mysofinance/v2 into myso-oracle
jpick713 May 3, 2024
31eaea7
added in hardcoded IOO mantle oracle
jpick713 May 3, 2024
1ac9d9e
remove unnecessary imports
jpick713 May 3, 2024
7ffb1dd
added edits to bnb oracle
jpick713 May 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The protocol supports two different models, each targeted at different use cases
## Quick Start
```
npm i
npx hardhat test
npx hardhat test --grep "IOO price correctly"
```

## Contract Files
Expand Down
11 changes: 11 additions & 0 deletions contracts/peer-to-peer/interfaces/oracles/IMETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

interface IMETH {
/**
* @notice gets amount of Eth for given mEth
* @param mETHAmount amount of mEth
* @return amount of stEth
*/
function mETHToETH(uint256 mETHAmount) external view returns (uint256);
}
13 changes: 13 additions & 0 deletions contracts/peer-to-peer/interfaces/oracles/IWSTETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

interface IWSTETH {
/**
* @notice gets amount of stEth for given wstEth
* @param _wstETHAmount amount of wstEth
* @return amount of stEth
*/
function getStETHByWstETH(
uint256 _wstETHAmount
) external view returns (uint256);
}
183 changes: 183 additions & 0 deletions contracts/peer-to-peer/oracles/custom/MysoOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.19;

import {ChainlinkBase} from "../chainlink/ChainlinkBase.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IWSTETH} from "../../interfaces/oracles/IWSTETH.sol";
import {IMETH} from "../../interfaces/oracles/IMETH.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

/**
* @dev supports oracles which are compatible with v2v3 or v3 interfaces
*/
contract MysoOracle is ChainlinkBase, Ownable {
struct MysoPrice {
jpick713 marked this conversation as resolved.
Show resolved Hide resolved
uint112 prePrice;
uint112 postPrice;
uint32 switchTime;
}

// solhint-disable var-name-mixedcase
address internal constant MYSO = 0x00000000000000000000000000000000DeaDBeef; // TODO: put in real myso address
address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address internal constant WSTETH =
0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
address internal constant METH = 0xd5F7838F5C461fefF7FE49ea5ebaF7728bB0ADfa;
address internal constant RPL = 0xD33526068D116cE69F19A9ee46F0bd304F21A51f;
address internal constant METH_STAKING_CONTRACT =
0xe3cBd06D7dadB3F4e6557bAb7EdD924CD1489E8f;
uint256 internal constant MYSO_IOO_BASE_CURRENCY_UNIT = 1e18; // 18 decimals for ETH based oracles
address internal constant ETH_USD_CHAINLINK =
0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419;
address internal constant STETH_ETH_CHAINLINK =
0x86392dC19c0b719886221c78AB11eb8Cf5c52812;
address internal constant RPL_USD_CHAINLINK =
0x4E155eD98aFE9034b7A5962f6C84c86d869daA9d;

uint256 internal constant MYSO_PRICE_TIME_LOCK = 5 minutes;

MysoPrice public mysoPrice;
//address public owner;
jpick713 marked this conversation as resolved.
Show resolved Hide resolved

event MysoPriceUpdated(
uint112 prePrice,
uint112 postPrice,
uint32 switchTime
);

error NoMyso();

/**
* @dev constructor for MysoOracle
* @param _tokenAddrs array of token addresses
* @param _oracleAddrs array of oracle addresses
* @param _mysoUsdPrice initial price of myso in usd (use 8 decimals like chainlink) (eg. 0.50 USD = 0.5 * 1e8)
*/
constructor(
jpick713 marked this conversation as resolved.
Show resolved Hide resolved
address[] memory _tokenAddrs,
address[] memory _oracleAddrs,
uint112 _mysoUsdPrice,
address _owner
)
ChainlinkBase(_tokenAddrs, _oracleAddrs, MYSO_IOO_BASE_CURRENCY_UNIT)
Ownable()
{
mysoPrice = MysoPrice(
_mysoUsdPrice,
_mysoUsdPrice,
uint32(block.timestamp)
);
jpick713 marked this conversation as resolved.
Show resolved Hide resolved
_transferOwnership(_owner);
}

/**
* @dev updates postPrice and switchTime
* only updates prePrice if the switchTime has passed
* @param _newMysoUsdPrice initial price of myso in usd (use 8 decimals like chainlink) (eg. 0.50 USD = 0.5 * 1e8)
*/

function setMysoPrice(uint112 _newMysoUsdPrice) external onlyOwner {
MysoPrice memory currMysoPrice = mysoPrice;
uint32 newTimeStamp = uint32(block.timestamp + MYSO_PRICE_TIME_LOCK);
// if the switchTime has not yet passed, update only postPrice with new price,
// leave prePrice the same and update switchTime
// else if the switchTime has passed (or exactly equal), update the prePrice with postPrice,
// update the postPrice with new price, and update switchTime
uint112 prePrice = block.timestamp < currMysoPrice.switchTime
? currMysoPrice.prePrice
: mysoPrice.postPrice;
mysoPrice = MysoPrice(prePrice, _newMysoUsdPrice, newTimeStamp);
emit MysoPriceUpdated(prePrice, _newMysoUsdPrice, newTimeStamp);
}

function getPrice(
address collToken,
address loanToken
) external view override returns (uint256 collTokenPriceInLoanToken) {
(uint256 priceOfCollToken, uint256 priceOfLoanToken) = getRawPrices(
collToken,
loanToken
);
uint256 loanTokenDecimals = (loanToken == MYSO)
? 18
: IERC20Metadata(loanToken).decimals();
collTokenPriceInLoanToken =
(priceOfCollToken * 10 ** loanTokenDecimals) /
priceOfLoanToken;
}

function getRawPrices(
address collToken,
address loanToken
)
public
view
override
returns (uint256 collTokenPriceRaw, uint256 loanTokenPriceRaw)
{
// must have at least one token is MYSO to use this oracle
if (collToken != MYSO && loanToken != MYSO) {
revert NoMyso();
}
jpick713 marked this conversation as resolved.
Show resolved Hide resolved
(collTokenPriceRaw, loanTokenPriceRaw) = (
_getPriceOfToken(collToken),
_getPriceOfToken(loanToken)
);
}

function _getPriceOfToken(
address token
) internal view virtual override returns (uint256 tokenPriceRaw) {
if (token == MYSO) {
tokenPriceRaw = _getMysoPriceInEth();
} else if (token == WETH) {
tokenPriceRaw = 1e18;
} else if (token == WSTETH) {
tokenPriceRaw = _getWstEthPrice();
} else if (token == METH) {
tokenPriceRaw = IMETH(METH_STAKING_CONTRACT).mETHToETH(1e18);
} else if (token == RPL) {
tokenPriceRaw = _getRPLPriceInEth();
} else {
tokenPriceRaw = super._getPriceOfToken(token);
}
}

function _getWstEthPrice() internal view returns (uint256 wstEthPriceRaw) {
uint256 stEthAmountPerWstEth = IWSTETH(WSTETH).getStETHByWstETH(1e18);
uint256 stEthPriceInEth = _checkAndReturnLatestRoundData(
(STETH_ETH_CHAINLINK)
);
wstEthPriceRaw = Math.mulDiv(
stEthPriceInEth,
stEthAmountPerWstEth,
1e18
);
}

function _getMysoPriceInEth()
internal
view
returns (uint256 mysoPriceInEth)
{
uint256 mysoPriceInUsd = block.timestamp < mysoPrice.switchTime
? mysoPrice.prePrice
: mysoPrice.postPrice;
uint256 ethPriceInUsd = _checkAndReturnLatestRoundData(
ETH_USD_CHAINLINK
);
mysoPriceInEth = Math.mulDiv(mysoPriceInUsd, 1e18, ethPriceInUsd);
}

function _getRPLPriceInEth() internal view returns (uint256 rplPriceRaw) {
uint256 rplPriceInUSD = _checkAndReturnLatestRoundData(
(RPL_USD_CHAINLINK)
);
uint256 ethPriceInUsd = _checkAndReturnLatestRoundData(
ETH_USD_CHAINLINK
);
rplPriceRaw = Math.mulDiv(rplPriceInUSD, 1e18, ethPriceInUsd);
}
}
11 changes: 11 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ export const getRecentMainnetForkingConfig = () => {
return { chainId: chainId, url: url, blockNumber: blockNumber }
}

export const getMysoOracleMainnetForkingConfig = () => {
const INFURA_API_KEY = process.env.INFURA_API_KEY
if (INFURA_API_KEY === undefined) {
throw new Error('Invalid hardhat.config.ts! Need to set `INFURA_API_KEY`!')
}
const chainId = 1
const url = `https://mainnet.infura.io/v3/${INFURA_API_KEY}`
const blockNumber = 19300000 // 2024-02-24 (9PM UTC)
return { chainId: chainId, url: url, blockNumber: blockNumber }
}

export const getArbitrumForkingConfig = () => {
const ALCHEMY_API_KEY = process.env.ALCHEMY_API_KEY
if (ALCHEMY_API_KEY === undefined) {
Expand Down
Loading
Loading