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

BIP–48: Whitelist BEANwstETH and Migrate Unripe Liquidity #758

Merged
merged 244 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
244 commits
Select commit Hold shift + click to select a range
fd7c5fc
First attempt steth oracle + eth/usd changes
BrendanSanderson Nov 15, 2023
d58220f
Merge branch 'bip39-seedgauge' into add-steth-oracle
BrendanSanderson Jan 25, 2024
c1b6521
add oracle tests
BrendanSanderson Jan 26, 2024
3bcf6a2
merged with seedGauge
BrendanSanderson Jan 26, 2024
4a663dc
fix failing tests
BrendanSanderson Jan 26, 2024
f3cb50d
remove extraneous file
BrendanSanderson Feb 7, 2024
e36d92c
init migration
BrendanSanderson Feb 8, 2024
ef43b1d
Skip reset tests when RPC missing. minor fixes elsewhere.
publiuss Feb 8, 2024
1103605
Merge branch 'add-steth-oracle' into wsteth-migration
BrendanSanderson Feb 8, 2024
6eb4e60
skipping Bean:3crv -> Bean:Eth migration b/c it already happened
BrendanSanderson Feb 8, 2024
24b3602
update stem tests
BrendanSanderson Feb 8, 2024
924af92
Merge branch 'seedGauge-v0.1-review' into add-steth-oracle
BrendanSanderson Feb 10, 2024
6cdcff9
Merge branch 'add-steth-oracle' into wsteth-migration
BrendanSanderson Feb 10, 2024
11064bc
broken merge
BrendanSanderson Feb 10, 2024
deca380
update
BrendanSanderson Feb 12, 2024
c896cde
tests passing
BrendanSanderson Feb 12, 2024
053bdbc
add wells version
BrendanSanderson Feb 12, 2024
5817cb8
yarn updates
BrendanSanderson Feb 12, 2024
0fe0c71
Fix failing migration test
BrendanSanderson Feb 12, 2024
267077d
Comments
BrendanSanderson Feb 12, 2024
60ecae0
Merge branch 'seedGauge-V0.1-review' into add-steth-oracle
BrendanSanderson Feb 13, 2024
28d33a8
Merge branch 'add-steth-oracle' into wsteth-migration
BrendanSanderson Feb 13, 2024
f62086f
add tests for turning off bean:eth oracle
BrendanSanderson Feb 13, 2024
a570d17
Lib Eval fix, update USD Oracle, Reduce size of Season facet
BrendanSanderson Feb 13, 2024
dadef06
fix test
BrendanSanderson Feb 13, 2024
c35ad4f
Merge branch 'bip39-seedGauge' into add-steth-oracle
BrendanSanderson Feb 14, 2024
c7f7060
Merge branch 'add-steth-oracle' into wsteth-migration
BrendanSanderson Feb 14, 2024
350eab6
remove sop constants
BrendanSanderson Feb 14, 2024
9ddaa34
review changes
BrendanSanderson Feb 17, 2024
1f6135f
Changes for Review (#779)
Brean0 Feb 19, 2024
8787bd4
Merge branch 'add-steth-oracle' into wsteth-migration
Brean0 Feb 19, 2024
e1324cd
update tests from merge
Brean0 Feb 19, 2024
65f5ef2
generalize metadata tests
Brean0 Feb 19, 2024
bb600e7
Merge branch 'update-metadata-test' into wsteth-migration
Brean0 Feb 19, 2024
204b580
format app storage
BrendanSanderson Feb 20, 2024
4ea7406
rename urBeanEth to urLP
BrendanSanderson Feb 20, 2024
71930ff
updated documentation for init sol
BrendanSanderson Feb 20, 2024
e46ee6b
add helper init function
BrendanSanderson Feb 20, 2024
9a39c57
auto add new wells to oracle
BrendanSanderson Feb 20, 2024
e0a4006
Remove BEANSTALK_PUMP from C.sol
BrendanSanderson Feb 20, 2024
97f905d
Swap sync for addLiquidity
BrendanSanderson Feb 20, 2024
c797265
Fix failing test
BrendanSanderson Feb 20, 2024
7d11d62
Update Beanstalk Price
BrendanSanderson Feb 20, 2024
0874a98
fix appsStorage
Brean0 Feb 21, 2024
0ab6512
update comment
Brean0 Feb 21, 2024
dc43e94
update initial gauge points
Brean0 Feb 21, 2024
3ab6c01
Changes to Wsteth Migration (#783)
Brean0 Feb 21, 2024
9277577
Add whitelisted Well requirement to switch Barn Raise Well
BrendanSanderson Feb 21, 2024
e6513e4
add more validation
BrendanSanderson Feb 21, 2024
4faeb3d
Merge branch 'wsteth-migration-changes' of github.com:BeanstalkFarms/…
Brean0 Feb 21, 2024
41dc181
Add whitelisted Well requirement to switch Barn Raise Well (#785)
Brean0 Feb 23, 2024
399202a
Bean:Eth -> Bean:wStEth Migration (#765)
Brean0 Feb 23, 2024
1eeb90a
remove lib from oracle helpers
Brean0 Feb 23, 2024
2370d0c
LibWsteth Comments
Brean0 Feb 23, 2024
bcd3ef3
rearrange libWstethEth
Brean0 Feb 23, 2024
530c4cc
changes (#787)
Brean0 Feb 23, 2024
68388be
merged
BrendanSanderson Mar 21, 2024
1d0734b
remove s.barnRaiseWell
Brean0 Mar 21, 2024
b95a667
add LibGerminate
BrendanSanderson Mar 21, 2024
bb66b8d
Merge branch 'add-steth-oracle' into removeBarnRaiseStorage
BrendanSanderson Mar 21, 2024
ed0a1a0
remove barnRaiseWell from appstorage
BrendanSanderson Mar 21, 2024
8dc9cbf
remove s.barnRaiseWell (#800)
Brean0 Mar 21, 2024
4a251a8
fix tests.
Brean0 Mar 21, 2024
9e42ac9
Add soil fix changes
nickkatsios Mar 22, 2024
47e905e
Revert "Add soil fix changes"
nickkatsios Mar 22, 2024
a0409a6
Merge branch 'master' into add-steth-oracle-merge
Brean0 May 8, 2024
fc235a7
Merge branch 'bip39-seedGauge' into add-steth-oracle-merge
Brean0 May 8, 2024
a05b2f4
fix merge.
Brean0 May 8, 2024
4a7ad83
Fix M-02 wsteth/eth price in extreme conditions
pizzaman1337 Jun 11, 2024
f453c54
Fix M-03: Protocol unintentionally implements an asymmetric method of…
pizzaman1337 Jun 11, 2024
900c9c9
Update comment in previous commit to include audit comment
pizzaman1337 Jun 11, 2024
0c7fe76
L-01 and L-02
pizzaman1337 Jun 11, 2024
a934244
L-04 fix getBeanAmountOut
pizzaman1337 Jun 12, 2024
3bf4755
L-05 Fix lookback for wsteth
pizzaman1337 Jun 12, 2024
ce023d2
wstETH Migration Remediations (#922)
pizzaman1337 Jun 25, 2024
e669467
Revert "wstETH Migration Remediations"
Brean0 Jun 25, 2024
527b239
Revert "wstETH Migration Remediations" (#932)
Brean0 Jun 25, 2024
73d8a10
add-steth-oracle-merge (#858)
Brean0 Jun 25, 2024
d747a60
feat: add deploy-script
Space-Bean Jun 28, 2024
db13023
Merge branch 'master' into add-steth-oracle-merge
Brean0 Jun 29, 2024
0c52bc4
merge master.
Brean0 Jun 29, 2024
7df1462
Merge branch 'add-steth-oracle' into add-steth-oracle-merge
Brean0 Jun 29, 2024
4c9aba5
Merge branch 'add-steth-oracle-merge' into add-steth-oracle
Brean0 Jun 29, 2024
da46b76
Merge branch 'add-steth-oracle' into ui-dex-ui/add-steth-oracle
Space-Bean Jun 29, 2024
ec56116
feat: add deploy script
Space-Bean Jun 29, 2024
f3e27ae
feat: add lido abi / addresses / contracts
Space-Bean Jul 1, 2024
14063aa
feat: unwrap & send eth junction abi + contracts + address
Space-Bean Jul 1, 2024
4a5c1d1
feat: update contracts + add unwrap & send eth junction
Space-Bean Jul 1, 2024
a388b67
feat: add steth & wsteth tokens + remove unused abi
Space-Bean Jul 1, 2024
222260f
feat: add tokens
Space-Bean Jul 1, 2024
758234a
feat: update contracts
Space-Bean Jul 3, 2024
8142769
feat: add unwrapWsteth workflow step
Space-Bean Jul 3, 2024
b321e53
feat: add sdk presets + actions
Space-Bean Jul 3, 2024
dbb085a
feat: update sdktokens + pools
Space-Bean Jul 8, 2024
1512f5d
feat: add actions
Space-Bean Jul 8, 2024
e2038d5
feat: update actions
Space-Bean Jul 8, 2024
05fee67
feat: update sdk-wells tokens
Space-Bean Jul 8, 2024
d4b483c
feat: update WSTETH MockContract
Space-Bean Jul 8, 2024
e875f4c
feat: un-lint impersonate.js
Space-Bean Jul 8, 2024
e1115e3
feat: update blockchain-utils
Space-Bean Jul 8, 2024
3c1902d
feat: unlint
Space-Bean Jul 8, 2024
6dbd1fb
feat: unlint v2
Space-Bean Jul 8, 2024
4f7acf6
feat: update naming
Space-Bean Jul 8, 2024
78099bc
feat: add presets
Space-Bean Jul 8, 2024
3b5d28f
feat: [sdks]: STETH / WSTETH / LP Integration (#945)
Space-Bean Jul 8, 2024
08f80f8
feat: update basin ui
Space-Bean Jul 9, 2024
03c267e
feat: update graph
Space-Bean Jul 9, 2024
124eb46
feat: update ui token metadatas
Space-Bean Jul 9, 2024
12b70bc
feat: update ui constants
Space-Bean Jul 9, 2024
28e70b1
feat: update ui tokens
Space-Bean Jul 9, 2024
f6d9e0e
feat: [ui/dex-ui]: Integrate sdk changes to ui / dex-ui (#949)
Space-Bean Jul 9, 2024
4f54de3
feat: fix build error
Space-Bean Jul 11, 2024
89f374d
feat: declare mergeing token types
Space-Bean Jul 11, 2024
0414c17
feat: update dex-ui types
Space-Bean Jul 11, 2024
023c58a
feat: update ui constants
Space-Bean Jul 11, 2024
d1aa688
feat: lint silo/updater
Space-Bean Jul 11, 2024
44e5a99
Revert "Revert "wstETH Migration Remediations""
pizzaman1337 Jul 12, 2024
5b20cb7
Revert "Revert "wstETH Migration Remediations"" (#955)
pizzaman1337 Jul 12, 2024
33317fd
Merge branch 'add-steth-oracle' into ui-dex-ui/add-steth-oracle
Space-Bean Jul 12, 2024
31e4f92
Merge branch 'ui-dex-ui/add-steth-oracle' into spacebean/steth/update…
Space-Bean Jul 12, 2024
d64c135
feat: fix dex-ui remove liquidity
Space-Bean Jul 12, 2024
3ab5a27
Update wsteth well and barn raise well address
pizzaman1337 Jul 12, 2024
6aa757c
Update pump address
pizzaman1337 Jul 12, 2024
a8d0cbe
feat: update buy
Space-Bean Jul 13, 2024
e7c17bc
feat: update constants + farm steps
Space-Bean Jul 13, 2024
0e3f7df
feat: update addresses comments
Space-Bean Jul 13, 2024
899215f
feat: add getstartmintingseason hook
Space-Bean Jul 13, 2024
c9db3ac
feat: add to chop, convert, and deposit
Space-Bean Jul 13, 2024
7005476
feat: add update price contract
Space-Bean Jul 13, 2024
8c4e96b
feat: update useLPPositionSummary
Space-Bean Jul 14, 2024
dc2b5a7
feat: add new scoped query approach
Space-Bean Jul 14, 2024
8b4acad
feat: update query
Space-Bean Jul 14, 2024
cd6a32b
feat: inplement new query method
Space-Bean Jul 14, 2024
0008d0f
feat: update token names + symbols
Space-Bean Jul 14, 2024
c383f78
feat: update tokens + pools in UI
Space-Bean Jul 14, 2024
277910d
feat: update token image wsteth
Space-Bean Jul 14, 2024
137e2d9
feat: index by address instead of symbol
Space-Bean Jul 14, 2024
68eaa50
feat: update keys to use address instead of symbol
Space-Bean Jul 14, 2024
2e83c71
feat: update
Space-Bean Jul 14, 2024
76bd426
feat: update learn yield + static BEAN:ETH references
Space-Bean Jul 14, 2024
0675a1e
feat: update urbeanweth to urbeanwsteth
Space-Bean Jul 14, 2024
a790a99
feat: update urBEANWETH to urBeanWsTeth in sdk
Space-Bean Jul 14, 2024
6ea3192
feat: update cli to use urBeanWstETH
Space-Bean Jul 14, 2024
72c5fcd
feat: add missed urbeaneth fixes
Space-Bean Jul 14, 2024
4e0f687
feat: replace more missed beaneth references
Space-Bean Jul 14, 2024
64011f6
feat: urbeaneth passthrough v4
Space-Bean Jul 14, 2024
77922ae
feat: update unripe underlying poools
Space-Bean Jul 14, 2024
b7a2430
Update wells v1.1 package to prerelease1, fixes checksum issue
pizzaman1337 Jul 15, 2024
59b8157
Update gauge points and optimal percent deposited bdv for wsteth
pizzaman1337 Jul 15, 2024
dc5a17e
feat: declare types in UI
Space-Bean Jul 15, 2024
f53d041
feat: remove _source from token declaration
Space-Bean Jul 15, 2024
5f10bd8
feat: update useBeanstalkBalancesBreakdown
Space-Bean Jul 15, 2024
a24786f
feat: update deposit graph to allow wsteth -> bean:weth
Space-Bean Jul 15, 2024
4aa630d
feat: update wstethPool constant
Space-Bean Jul 15, 2024
f006e47
feat: remove unused library presets in sdk
Space-Bean Jul 15, 2024
09e4a64
feat: fix convert
Space-Bean Jul 15, 2024
66a17ac
feat: update minting season logic
Space-Bean Jul 15, 2024
1531974
feat: remove accidental transfer step to buy fert
Space-Bean Jul 15, 2024
614ffff
feat: fix dex build
Space-Bean Jul 15, 2024
7dfc7cd
feat: update transfer tokens
Space-Bean Jul 15, 2024
e130c01
Merge pull request #64 from AgrarianAlliance/add-steth-oracle-updates
pizzaman1337 Jul 15, 2024
da0b8c2
Merge pull request #63 from AgrarianAlliance/add-steth-oracle-addresses
pizzaman1337 Jul 15, 2024
d0ec587
feat: update token pikcer to use address not symbol
Space-Bean Jul 15, 2024
e2155a9
feat: [sdk/ui/dex-ui]: Fix deposit / Update Convert / Integrate Barn …
Space-Bean Jul 15, 2024
ab222b0
Add impersonateBeanWstethWell to fert test
pizzaman1337 Jul 15, 2024
26bbab8
feat: fix css errs
Space-Bean Jul 16, 2024
a8fcdf8
All tests passing again
pizzaman1337 Jul 16, 2024
d76fd45
Merge branch 'july-ebip' into add-steth-oracle
pizzaman1337 Jul 16, 2024
e6e3589
update wsteth-migration bip with proper linking.
Space-Bean Jul 16, 2024
2ae2505
Fix tests
pizzaman1337 Jul 16, 2024
71bf653
Merge branch 'ebip-17' into add-steth-oracle
pizzaman1337 Jul 17, 2024
b126bd8
Merge branch 'add-steth-oracle' of github.com:BeanstalkFarms/Beanstal…
pizzaman1337 Jul 17, 2024
2cebe70
Merge branch 'add-steth-oracle' origin into add-steth-oracle
pizzaman1337 Jul 17, 2024
c6d8b73
feat: update new whitelisted well components
Space-Bean Jul 18, 2024
64eaac4
feat: update dex-ui
Space-Bean Jul 18, 2024
d47810c
feat: update sdk addresses
Space-Bean Jul 18, 2024
53913cb
feat: update ui deposits
Space-Bean Jul 18, 2024
0052856
feat: update utils
Space-Bean Jul 18, 2024
349510c
merge add-steth-oracle
Space-Bean Jul 18, 2024
cc1077c
feat: fix merge conflicts
Space-Bean Jul 18, 2024
8315129
feat: fix build errs + imports
Space-Bean Jul 18, 2024
1204111
Merge branch 'master' into add-steth-oracle
pizzaman1337 Jul 18, 2024
a7a2411
feat: fix failing tests + update token name
Space-Bean Jul 18, 2024
051fb5d
feat: rename + fix errors v2
Space-Bean Jul 18, 2024
ce25abb
Revert M-02 codehawks suggestion in favor of returning 0 if max diffe…
pizzaman1337 Jul 18, 2024
c4d0b68
feat: update Fiat calculations
Space-Bean Jul 18, 2024
70b5773
Merge branch 'add-steth-oracle' into ui-dex-ui/add-steth-oracle
Space-Bean Jul 18, 2024
69724dd
Tests passing
pizzaman1337 Jul 18, 2024
467f4ad
feat: remove libchainlink oracle changes
Space-Bean Jul 18, 2024
3692fc7
Merge branch 'add-steth-oracle' into ui-dex-ui/add-steth-oracle
Space-Bean Jul 18, 2024
ae1e78d
Remove migration facet from deployment script
pizzaman1337 Jul 18, 2024
0eba48b
Merge branch 'add-steth-oracle' into ui-dex-ui/add-steth-oracle
Space-Bean Jul 18, 2024
7099445
Remove duplicate require statement
pizzaman1337 Jul 18, 2024
c9cc2ce
Add deployWstethMigration to hardhat config
pizzaman1337 Jul 18, 2024
073cd06
feat: update tokens
Space-Bean Jul 18, 2024
440c0a8
Merge branch 'add-steth-oracle' into ui-dex-ui/add-steth-oracle
Space-Bean Jul 18, 2024
dcd48d5
feat: unlint
Space-Bean Jul 18, 2024
4de9068
feat: skip convert tests
Space-Bean Jul 18, 2024
f95e657
Update selectors to remove
pizzaman1337 Jul 18, 2024
d958cf9
Set mock back to true
pizzaman1337 Jul 18, 2024
45afeaf
Add back oracle deployment
pizzaman1337 Jul 18, 2024
32b432c
feat: update deposit + tests
Space-Bean Jul 18, 2024
d747818
feat: patch sdk withdraw test
Space-Bean Jul 18, 2024
51a46c2
Merge branch 'add-steth-oracle' into ui-dex-ui/add-steth-oracle
Space-Bean Jul 18, 2024
8948d72
feat: update silo test
Space-Bean Jul 19, 2024
7afc749
test: update convert test
Space-Bean Jul 19, 2024
5cb4526
feat: update token logo
Space-Bean Jul 19, 2024
d8ee4a2
test: update silo test
Space-Bean Jul 22, 2024
36cfc6b
test: update silo test
Space-Bean Jul 22, 2024
3c3995f
feat: separate deploy ui script
Space-Bean Jul 22, 2024
180659b
feat: update tokens test
Space-Bean Jul 22, 2024
a5dcbb4
feat: update silo utils test
Space-Bean Jul 22, 2024
95429ff
test: update sdk transfer tests
Space-Bean Jul 22, 2024
1824c7e
feat: remove forge-std
Space-Bean Jul 22, 2024
6d2eeb6
feat: update convert test
Space-Bean Jul 22, 2024
239c790
feat: update transfer test + blockchain utils
Space-Bean Jul 22, 2024
b403343
feat: update deposit test
Space-Bean Jul 22, 2024
e1ba007
feat: update convert test
Space-Bean Jul 22, 2024
08d9c06
test: update transfer test
Space-Bean Jul 22, 2024
62e343c
test: update convert test
Space-Bean Jul 22, 2024
2eca14a
feat: update token image
Space-Bean Jul 22, 2024
bb18232
feat: [dex-ui]: fix silo balances not loading
Space-Bean Jul 23, 2024
b24e689
feat: [dex-ui]: use ledger
Space-Bean Jul 23, 2024
4706119
feat: create hook useIsMigrating
Space-Bean Jul 23, 2024
671ae4b
feat: remove console logs on add liquidity page
Space-Bean Jul 23, 2024
719b162
feat: remove beanwstETHcp2w.svg
Space-Bean Jul 23, 2024
4a42703
feat: re-add token img
Space-Bean Jul 23, 2024
2766c44
feat: wstETH migration UI final passthrough (#967)
Space-Bean Jul 23, 2024
5cb0583
feat: update addresses + abi
Space-Bean Jul 23, 2024
fbbbe59
feat: update scripts
Space-Bean Jul 23, 2024
c6f2d2a
feat: update scripts
Space-Bean Jul 23, 2024
b0113e8
feat: unlint hardhat config
Space-Bean Jul 23, 2024
56b0090
feat: update script
Space-Bean Jul 23, 2024
1e59193
feat: update token utils
Space-Bean Jul 24, 2024
58386c6
feat: add utils
Space-Bean Jul 24, 2024
79a540b
feat: CLI: update sunrise function cli
Space-Bean Jul 25, 2024
ec44e51
feat: update price button links
Space-Bean Jul 25, 2024
ced5de3
feat: bugg passthrough
Space-Bean Jul 25, 2024
8f2250e
feat: update token declarations
Space-Bean Jul 25, 2024
37995c5
feat: update well addresses section + remove duplicates
Space-Bean Jul 26, 2024
8a2d2ae
feat: add removed protocol libs
Space-Bean Jul 26, 2024
e5d74e2
feat: [UI/Dex-UI]: add-steth-oracle (#941)
Space-Bean Jul 26, 2024
37f884e
Merge branch 'master' into add-steth-oracle
Space-Bean Jul 26, 2024
af78b9a
feat: add abi
Space-Bean Jul 26, 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
7 changes: 1 addition & 6 deletions protocol/contracts/C.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ library C {
address internal constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address internal constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address internal constant UNIV3_ETH_USDC_POOL = 0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640; // 0.05% pool
address internal constant UNIV3_ETH_USDT_POOL = 0x11b815efB8f581194ae79006d24E0d814B7697F6; // 0.05% pool
address internal constant WSTETH = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;

// Use external contract for block.basefee as to avoid upgrading existing contracts to solidity v8
address private constant BASE_FEE_CONTRACT = 0x84292919cB64b590C0131550483707E43Ef223aC;
Expand Down Expand Up @@ -166,10 +165,6 @@ library C {
return IERC20(THREE_CRV);
}

function UniV3EthUsdc() internal pure returns (address){
return UNIV3_ETH_USDC_POOL;
}

function fertilizer() internal pure returns (IFertilizer) {
return IFertilizer(FERTILIZER);
}
Expand Down
104 changes: 68 additions & 36 deletions protocol/contracts/libraries/Oracle/LibChainlinkOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@ library LibChainlinkOracle {
// Uses the same timeout as Liquity's Chainlink timeout.
uint256 public constant CHAINLINK_TIMEOUT = 14400; // 4 hours: 60 * 60 * 4
Brean0 marked this conversation as resolved.
Show resolved Hide resolved

IChainlinkAggregator constant priceAggregator =
IChainlinkAggregator(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
uint256 constant PRECISION = 1e6; // use 6 decimal precision.
Brean0 marked this conversation as resolved.
Show resolved Hide resolved

// timeout for Oracles with a 1 hour heartbeat.
uint256 constant FOUR_HOUR_TIMEOUT = 14400;
// timeout for Oracles with a 1 day heartbeat.
uint256 constant FOUR_DAY_TIMEOUT = 345600;

/**
* @dev Returns the most recently reported ETH/USD price from the Chainlink Oracle.
* @dev Returns the price of a given `priceAggregator`
* Return value has 6 decimal precision.
* Returns 0 if Chainlink's price feed is broken or frozen.
**/
function getEthUsdPrice() internal view returns (uint256 price) {
function getPrice(
address priceAggregatorAddress,
uint256 maxTimeout
) internal view returns (uint256 price) {
IChainlinkAggregator priceAggregator = IChainlinkAggregator(priceAggregatorAddress);
// First, try to get current decimal precision:
uint8 decimals;
try priceAggregator.decimals() returns (uint8 _decimals) {
Expand All @@ -51,7 +58,7 @@ library LibChainlinkOracle {
) {
// Check for an invalid roundId that is 0
if (roundId == 0) return 0;
if (checkForInvalidTimestampOrAnswer(timestamp, answer, block.timestamp)) {
if (checkForInvalidTimestampOrAnswer(timestamp, answer, block.timestamp, maxTimeout)) {
return 0;
}
// Adjust to 6 decimal precision.
Expand All @@ -62,12 +69,25 @@ library LibChainlinkOracle {
}
}

struct TwapVariables {
Brean0 marked this conversation as resolved.
Show resolved Hide resolved
uint256 cumulativePrice;
uint256 endTimestamp;
uint256 lastTimestamp;
uint256 timestamp;
int256 answer;
Brean0 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @dev Returns the TWAP ETH/USD price from the Chainlink Oracle over the past `lookback` seconds.
* @dev Returns the TWAP price from the Chainlink Oracle over the past `lookback` seconds.
* Return value has 6 decimal precision.
* Returns 0 if Chainlink's price feed is broken or frozen.
**/
function getEthUsdTwap(uint256 lookback) internal view returns (uint256 price) {
function getTwap(
address priceAggregatorAddress,
uint256 maxTimeout,
uint256 lookback
) internal view returns (uint256 price) {
IChainlinkAggregator priceAggregator = IChainlinkAggregator(priceAggregatorAddress);
// First, try to get current decimal precision:
uint8 decimals;
try priceAggregator.decimals() returns (uint8 _decimals) {
Expand All @@ -88,62 +108,74 @@ library LibChainlinkOracle {
) {
// Check for an invalid roundId that is 0
if (roundId == 0) return 0;
if (checkForInvalidTimestampOrAnswer(timestamp, answer, block.timestamp)) {
if (checkForInvalidTimestampOrAnswer(timestamp, answer, block.timestamp, maxTimeout)) {
return 0;
}

uint256 endTimestamp = block.timestamp.sub(lookback);
TwapVariables memory t;

t.endTimestamp = block.timestamp.sub(lookback);
// Check if last round was more than `lookback` ago.
if (timestamp <= endTimestamp) {
if (timestamp <= t.endTimestamp) {
return uint256(answer).mul(PRECISION).div(10 ** decimals);
} else {
uint256 cumulativePrice;
uint256 lastTimestamp = block.timestamp;
t.lastTimestamp = block.timestamp;
// Loop through previous rounds and compute cumulative sum until
// a round at least `lookback` seconds ago is reached.
while (timestamp > endTimestamp) {
cumulativePrice = cumulativePrice.add(
uint256(answer).mul(lastTimestamp.sub(timestamp))
while (timestamp > t.endTimestamp) {
t.cumulativePrice = t.cumulativePrice.add(
uint256(answer).mul(t.lastTimestamp.sub(timestamp))
);
roundId -= 1;
try priceAggregator.getRoundData(roundId) returns (
uint80 /* roundId */,
int256 _answer,
uint256 /* startedAt */,
uint256 _timestamp,
uint80 /* answeredInRound */
) {
if (checkForInvalidTimestampOrAnswer(_timestamp, _answer, timestamp)) {
return 0;
}
lastTimestamp = timestamp;
timestamp = _timestamp;
answer = _answer;
} catch {
// If call to Chainlink aggregator reverts, return a price of 0 indicating failure
t.lastTimestamp = timestamp;
(answer, timestamp) = getRoundData(priceAggregator, roundId);
if (checkForInvalidTimestampOrAnswer(
timestamp,
answer,
t.lastTimestamp,
maxTimeout
)) {
return 0;
}
}
cumulativePrice = cumulativePrice.add(
uint256(answer).mul(lastTimestamp.sub(endTimestamp))
t.cumulativePrice = t.cumulativePrice.add(
uint256(answer).mul(t.lastTimestamp.sub(t.endTimestamp))
);
return cumulativePrice.mul(PRECISION).div(10 ** decimals).div(lookback);
return t.cumulativePrice.mul(PRECISION).div(10 ** decimals).div(lookback);
}
} catch {
// If call to Chainlink aggregator reverts, return a price of 0 indicating failure
return 0;
}
}

function getRoundData(
IChainlinkAggregator priceAggregator,
uint80 roundId
) private view returns (int256, uint256) {
try priceAggregator.getRoundData(roundId) returns (
uint80 /* roundId */,
int256 _answer,
uint256 /* startedAt */,
uint256 _timestamp,
uint80 /* answeredInRound */
) {
return (_answer, _timestamp);
} catch {
return (-1, 0);
}
}

function checkForInvalidTimestampOrAnswer(
uint256 timestamp,
int256 answer,
uint256 currentTimestamp
) private pure returns (bool) {
uint256 currentTimestamp,
uint256 maxTimeout
) private view returns (bool) {
Brean0 marked this conversation as resolved.
Show resolved Hide resolved
// Check for an invalid timeStamp that is 0, or in the future
if (timestamp == 0 || timestamp > currentTimestamp) return true;
// Check if Chainlink's price feed has timed out
if (currentTimestamp.sub(timestamp) > CHAINLINK_TIMEOUT) return true;
if (currentTimestamp.sub(timestamp) > maxTimeout) return true;
// Check for non-positive price
if (answer <= 0) return true;
}
Expand Down
95 changes: 35 additions & 60 deletions protocol/contracts/libraries/Oracle/LibEthUsdOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,61 @@ pragma solidity =0.7.6;
pragma experimental ABIEncoderV2;

import {LibChainlinkOracle} from "./LibChainlinkOracle.sol";
import {LibUniswapOracle} from "./LibUniswapOracle.sol";
import {SafeMath} from "@openzeppelin/contracts/math/SafeMath.sol";
import {LibAppStorage, AppStorage} from "contracts/libraries/LibAppStorage.sol";
import {C} from "contracts/C.sol";
import {LibOracleHelpers} from "contracts/libraries/Oracle/LibOracleHelpers.sol";

/**
* @title Eth Usd Oracle Library
* @notice Contains functionalty to fetch a manipulation resistant ETH/USD price.
* @dev
* The Oracle uses a greedy approach to return the average price between the
* current price returned ETH/USD Chainlink Oracle and either the ETH/USDC
* Uniswap V3 0.05% fee pool and the ETH/USDT Uniswap V3 0.05% fee pool depending
* on which is closer.
*
* on which is closer.
*
* If the prices in the ETH/USDC Uniswap V3 0.05% fee pool and USD/USDT Uniswap V3 0.05% fee pool are
* greater than `MAX_DIFFERENCE` apart, then the oracle uses the Chainlink price to maximize liveness.
*
*
* The approach is greedy as if the ETH/USDC Uniswap price is sufficiently close
* to the Chainlink Oracle price (See {MAX_GREEDY_DIFFERENCE}), then the Oracle
* will not check the ETH/USDT Uniswap Price to save gas.
*
*
* The oracle will fail if the Chainlink Oracle is broken or frozen (See: {LibChainlinkOracle}).
**/
library LibEthUsdOracle {

using SafeMath for uint256;

/////////////////// ORACLES ///////////////////
address constant ETH_USD_CHAINLINK_PRICE_AGGREGATOR =
0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419;
///////////////////////////////////////////////

// The maximum percent different such that it is acceptable to use the greedy approach.
uint256 constant MAX_GREEDY_DIFFERENCE = 0.003e18; // 0.3%
uint256 constant MAX_GREEDY_DIFFERENCE = 0.003e18; // 0.3%'
Brean0 marked this conversation as resolved.
Show resolved Hide resolved

// The maximum percent difference such that the oracle assumes no manipulation is occuring.
uint256 constant MAX_DIFFERENCE = 0.01e18; // 1%
uint256 constant ONE = 1e18;

// The lookback used for Uniswap Oracles when querying the instantaneous USD price.
uint32 constant INSTANT_LOOKBACK = 900;
function getEthUsdPriceFromStorageIfSaved() internal view returns (uint256) {
Brean0 marked this conversation as resolved.
Show resolved Hide resolved
AppStorage storage s = LibAppStorage.diamondStorage();

uint256 priceInStorage = s.usdTokenPrice[C.BEAN_ETH_WELL];

if (priceInStorage == 1) {
return getEthUsdPrice();
}
return priceInStorage;
}

/**
* @dev Returns the instantaneous ETH/USD price
* Return value has 6 decimal precision.
* Returns 0 if the Eth Usd Oracle cannot fetch a manipulation resistant price.
**/
function getEthUsdPrice() internal view returns (uint256) {
return getEthUsdPrice(0);
return LibChainlinkOracle.getPrice(ETH_USD_CHAINLINK_PRICE_AGGREGATOR, LibChainlinkOracle.FOUR_HOUR_TIMEOUT);
}

/**
Expand All @@ -59,55 +71,18 @@ library LibEthUsdOracle {
* A lookback of 900 seconds is used in Uniswap V3 pools for instantaneous price queries.
* If using a non-zero lookback, it is recommended to use a substantially large
* `lookback` (> 900 seconds) to protect against manipulation.
**/
**/
function getEthUsdPrice(uint256 lookback) internal view returns (uint256) {
uint256 chainlinkPrice = lookback > 0 ?
LibChainlinkOracle.getEthUsdTwap(lookback) :
LibChainlinkOracle.getEthUsdPrice();

// Check if the chainlink price is broken or frozen.
if (chainlinkPrice == 0) return 0;

// Use a lookback of 900 seconds for an instantaneous price query for manipulation resistance.
if (lookback == 0) lookback = INSTANT_LOOKBACK;
if (lookback > type(uint32).max) return 0;

uint256 usdcPrice = LibUniswapOracle.getEthUsdcPrice(uint32(lookback));
uint256 usdcChainlinkPercentDiff = getPercentDifference(usdcPrice, chainlinkPrice);

// Check if the USDC price and the Chainlink Price are sufficiently close enough
// to warrant using the greedy approach.
if (usdcChainlinkPercentDiff < MAX_GREEDY_DIFFERENCE) {
return chainlinkPrice.add(usdcPrice).div(2);
}

uint256 usdtPrice = LibUniswapOracle.getEthUsdtPrice(uint32(lookback));
uint256 usdtChainlinkPercentDiff = getPercentDifference(usdtPrice, chainlinkPrice);

// Check whether the USDT or USDC price is closer to the Chainlink price.
if (usdtChainlinkPercentDiff < usdcChainlinkPercentDiff) {
// Check whether the USDT price is too far from the Chainlink price.
if (usdtChainlinkPercentDiff < MAX_DIFFERENCE) {
return chainlinkPrice.add(usdtPrice).div(2);
}
return chainlinkPrice;
} else {
// Check whether the USDC price is too far from the Chainlink price.
if (usdcChainlinkPercentDiff < MAX_DIFFERENCE) {
return chainlinkPrice.add(usdcPrice).div(2);
}
return chainlinkPrice;
}
}

/**
* Gets the percent difference between two values with 18 decimal precision.
* @dev If x == 0 (Such as in the case of Uniswap Oracle failure), then the percent difference is calculated as 100%.
*/
function getPercentDifference(uint x, uint y) internal pure returns (uint256 percentDifference) {
percentDifference = x.mul(ONE).div(y);
percentDifference = x > y ?
percentDifference - ONE :
ONE - percentDifference; // SafeMath unnecessary due to conditional check
return
lookback > 0
? LibChainlinkOracle.getTwap(
ETH_USD_CHAINLINK_PRICE_AGGREGATOR,
LibChainlinkOracle.FOUR_HOUR_TIMEOUT,
lookback
)
: LibChainlinkOracle.getPrice(
ETH_USD_CHAINLINK_PRICE_AGGREGATOR,
LibChainlinkOracle.FOUR_HOUR_TIMEOUT
);
}
}
35 changes: 35 additions & 0 deletions protocol/contracts/libraries/Oracle/LibOracleHelpers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* SPDX-License-Identifier: MIT
**/

pragma solidity =0.7.6;
pragma experimental ABIEncoderV2;

import {LibChainlinkOracle} from "./LibChainlinkOracle.sol";
Brean0 marked this conversation as resolved.
Show resolved Hide resolved
import {LibUniswapOracle} from "./LibUniswapOracle.sol";
import {SafeMath} from "@openzeppelin/contracts/math/SafeMath.sol";
import {LibAppStorage, AppStorage} from "contracts/libraries/LibAppStorage.sol";
import {C} from "contracts/C.sol";
/**
* @title Oracle Helpers Library
* @author brendan
* @notice Contains functionalty common to multiple Oracle libraries.
**/
library LibOracleHelpers {

using SafeMath for uint256;

uint256 constant ONE = 1e18;

/**
* Gets the percent difference between two values with 18 decimal precision.
* @dev If x == 0 (Such as in the case of Uniswap Oracle failure), then the percent difference is calculated as 100%.
*/
function getPercentDifference(
uint x,
uint y
) internal pure returns (uint256 percentDifference) {
percentDifference = x.mul(ONE).div(y);
percentDifference = x > y ? percentDifference - ONE : ONE - percentDifference; // SafeMath unnecessary due to conditional check
}
}
Loading
Loading