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

Bean:Eth -> Bean:wStEth Migration #765

Merged
merged 40 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
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
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
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
c7f7060
Merge branch 'add-steth-oracle' into wsteth-migration
BrendanSanderson Feb 14, 2024
350eab6
remove sop constants
BrendanSanderson Feb 14, 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
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
61 changes: 58 additions & 3 deletions protocol/abi/Beanstalk.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
},
{
"inputs": [],
"name": "getLockedBeansUnderlyingUnripeBeanEth",
"name": "getLockedBeansUnderlyingUnripeLP",
"outputs": [
{
"internalType": "uint256",
Expand Down Expand Up @@ -592,6 +592,30 @@
"name": "SetFertilizer",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "tokenAmountIn",
"type": "uint256"
},
{
"internalType": "address",
"name": "barnRaiseToken",
"type": "address"
}
],
"name": "_getMintFertilizerOut",
"outputs": [
{
"internalType": "uint256",
"name": "fertilizerAmountOut",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -756,6 +780,32 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getBarnRaiseToken",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getBarnRaiseWell",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getCurrentHumidity",
Expand Down Expand Up @@ -875,7 +925,7 @@
"inputs": [
{
"internalType": "uint256",
"name": "wethAmountIn",
"name": "tokenAmountIn",
"type": "uint256"
}
],
Expand Down Expand Up @@ -926,7 +976,7 @@
"inputs": [
{
"internalType": "uint256",
"name": "wethAmountIn",
"name": "tokenAmountIn",
"type": "uint256"
},
{
Expand Down Expand Up @@ -7444,6 +7494,11 @@
"name": "stemScaleSeason",
"type": "uint16"
},
{
"internalType": "uint32",
"name": "beanEthStartMintingSeason",
"type": "uint32"
},
{
"internalType": "uint256",
"name": "start",
Expand Down
5 changes: 5 additions & 0 deletions protocol/contracts/C.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ library C {
uint256 internal constant WELL_MINIMUM_BEAN_BALANCE = 1000_000_000; // 1,000 Beans
address internal constant BEANSTALK_PUMP = 0xBA510f10E3095B83a0F33aa9ad2544E22570a87C;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update to legacy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed BEANSTALK_PUMP from here:
e0a4006

address internal constant BEAN_ETH_WELL = 0xBEA0e11282e2bB5893bEcE110cF199501e872bAd;
address internal constant BEAN_WSTETH_WELL = 0xa61Ef2313C1eC9c8cf2E1cAC986539d136b1393E; // TODO: Set
// The index of the Bean and Weth token addresses in all BEAN/ETH Wells.
uint256 internal constant BEAN_INDEX = 0;
uint256 internal constant ETH_INDEX = 1;

//////////////////// Season of Plenty ////////////////////
address internal constant SOP_WELL = BEAN_WSTETH_WELL;
address internal constant SOP_TOKEN = WSTETH;

function getSeasonPeriod() internal pure returns (uint256) {
return CURRENT_SEASON_PERIOD;
}
Expand Down
5 changes: 5 additions & 0 deletions protocol/contracts/beanstalk/AppStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ contract Account {
* @param isApprovedForAll A mapping of ERC1155 operator to approved status. ERC1155 compatability.
* @param farmerGerminating A Farmer's germinating stalk. Seperated into odd and even stalk.
* @param deposits SiloV3.1 deposits. A mapping from depositId to Deposit. SiloV3.1 introduces greater precision for deposits.
* @param barnRaiseWell The Well that the Barn Raise adds liquidity to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be in the appStorage natspec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was on natspec of AppStorage and State. Fixed here:
204b580#diff-9314f54b6e0c572c10e8145556daa1904a394fe43008d9ebf3181c618e51bb46L124

*/
struct State {
Field field; // A Farmer's Field storage.
Expand Down Expand Up @@ -342,6 +343,7 @@ contract Storage {
* @param abovePeg Boolean indicating whether the previous Season was above or below peg.
* @param stemStartSeason // season in which the stem storage method was introduced.
* @param stemScaleSeason // season in which the stem v1.1 was introduced, where stems are not truncated anymore.
* @param beanEthStartMintingSeason // Season to start minting in Bean:Eth pool after migrating liquidity out of the pool to protect against Pump failure.
* This allows for greater precision of stems, and requires a soft migration (see {LibTokenSilo.removeDepositFromAccount})
* @param start The timestamp of the Beanstalk deployment rounded down to the nearest hour.
* @param period The length of each season in Beanstalk in seconds.
Expand All @@ -359,6 +361,7 @@ contract Storage {
bool abovePeg; // | 1 (24)
uint16 stemStartSeason; // | 2 (26)
uint16 stemScaleSeason; //──────────┘ 2 (28/32)
uint32 beanEthStartMintingSeason; //──────────┘ 2 (28/32) NOTE: Reset and delete after Bean:wStEth migration has been completed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uint256 start;
uint256 period;
uint256 timestamp;
Expand Down Expand Up @@ -650,4 +653,6 @@ struct AppStorage {
mapping(uint32 => Storage.Sr) unclaimedGerminating;

Storage.WhitelistStatus[] whitelistStatuses;

address barnRaiseWell;
}
59 changes: 40 additions & 19 deletions protocol/contracts/beanstalk/barn/FertilizerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import {SafeMath} from "@openzeppelin/contracts/math/SafeMath.sol";
import {IFertilizer} from "contracts/interfaces/IFertilizer.sol";
import {AppStorage} from "../AppStorage.sol";
import {LibTransfer} from "contracts/libraries/Token/LibTransfer.sol";
import {LibEthUsdOracle} from "contracts/libraries/Oracle/LibEthUsdOracle.sol";
import {LibUsdOracle} from "contracts/libraries/Oracle/LibUsdOracle.sol";
import {LibFertilizer} from "contracts/libraries/LibFertilizer.sol";
import {LibSafeMath128} from "contracts/libraries/LibSafeMath128.sol";
import {C} from "contracts/C.sol";
import {LibDiamond} from "contracts/libraries/LibDiamond.sol";
import {IWell} from "contracts/interfaces/basin/IWell.sol";
import {LibBarnRaise} from "contracts/libraries/LibBarnRaise.sol";

/**
* @author Publius
Expand Down Expand Up @@ -53,30 +55,31 @@ contract FertilizerFacet {
}

/**
* @notice Purchase Fertilizer from the Barn Raise with WETH.
* @param wethAmountIn Amount of WETH to buy Fertilizer with 18 decimal precision.
* @param minFertilizerOut The minimum amount of Fertilizer to purchase. Protects against a significant ETH/USD price decrease.
* @param minLPTokensOut The minimum amount of LP tokens to receive after adding liquidity with `weth`.
* @notice Purchase Fertilizer from the Barn Raise with the Barn Raise token.
* @param tokenAmountIn Amount of tokens to buy Fertilizer with 18 decimal precision.
* @param minFertilizerOut The minimum amount of Fertilizer to purchase. Protects against a significant Barn Raise Token/USD price decrease.
* @param minLPTokensOut The minimum amount of LP tokens to receive after adding liquidity with Barn Raise tokens.
* @param mode The balance to transfer Beans to; see {LibTrasfer.To}
* @dev The # of Fertilizer minted is equal to the value of the Ether paid in USD.
*/
function mintFertilizer(
uint256 wethAmountIn,
uint256 tokenAmountIn,
uint256 minFertilizerOut,
uint256 minLPTokensOut,
LibTransfer.From mode
) external payable returns (uint256 fertilizerAmountOut) {
// Transfer the WETH directly to the Well for gas efficiency purposes. The WETH is later synced in {LibFertilizer.addUnderlying}.
wethAmountIn = LibTransfer.transferToken(
IERC20(C.WETH),
// Transfer Barn Raise tokens directly to the Well for gas efficiency purposes. The tokens are later synced in {LibFertilizer.addUnderlying}.
address barnRaiseToken = LibBarnRaise.getBarnRaiseToken();
tokenAmountIn = LibTransfer.transferToken(
IERC20(barnRaiseToken),
msg.sender,
C.BEAN_ETH_WELL,
uint256(wethAmountIn),
LibBarnRaise.getBarnRaiseWell(),
uint256(tokenAmountIn),
mode,
LibTransfer.To.EXTERNAL
);

fertilizerAmountOut = getMintFertilizerOut(wethAmountIn);
fertilizerAmountOut = _getMintFertilizerOut(tokenAmountIn, barnRaiseToken);

require(fertilizerAmountOut >= minFertilizerOut, "Fertilizer: Not enough bought.");
require(fertilizerAmountOut > 0, "Fertilizer: None bought.");
Expand Down Expand Up @@ -106,16 +109,26 @@ contract FertilizerFacet {
}

/**
* @dev Returns the amount of Fertilizer that can be purchased with `wethAmountIn` WETH.
* @dev Returns the amount of Fertilizer that can be purchased with `tokenAmountIn` Barn Raise tokens.
* Can be used to help calculate `minFertilizerOut` in `mintFertilizer`.
* `wethAmountIn` has 18 decimals, `getEthUsdPrice()` has 6 decimals and `fertilizerAmountOut` has 0 decimals.
* `tokenAmountIn` has 18 decimals, `getEthUsdPrice()` has 6 decimals and `fertilizerAmountOut` has 0 decimals.
*/
function getMintFertilizerOut(
uint256 wethAmountIn
function getMintFertilizerOut(uint256 tokenAmountIn)
external
view
returns (uint256 fertilizerAmountOut)
{
address barnRaiseToken = LibBarnRaise.getBarnRaiseToken();
return _getMintFertilizerOut(tokenAmountIn, barnRaiseToken);
}

function _getMintFertilizerOut(
uint256 tokenAmountIn,
address barnRaiseToken
) public view returns (uint256 fertilizerAmountOut) {
fertilizerAmountOut = wethAmountIn.mul(
LibEthUsdOracle.getEthUsdPrice()
).div(FERTILIZER_AMOUNT_PRECISION);
fertilizerAmountOut = tokenAmountIn.div(
LibUsdOracle.getUsdPrice(barnRaiseToken)
);
}

function totalFertilizedBeans() external view returns (uint256 beans) {
Expand Down Expand Up @@ -226,4 +239,12 @@ contract FertilizerFacet {
idx = LibFertilizer.getNext(idx);
}
}

function getBarnRaiseWell() external view returns (address) {
return LibBarnRaise.getBarnRaiseWell();
}

function getBarnRaiseToken() external view returns (address) {
return LibBarnRaise.getBarnRaiseToken();
}
}
7 changes: 4 additions & 3 deletions protocol/contracts/beanstalk/barn/UnripeFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {C} from "contracts/C.sol";
import {ReentrancyGuard} from "contracts/beanstalk/ReentrancyGuard.sol";
import {LibLockedUnderlying} from "contracts/libraries/LibLockedUnderlying.sol";
import {LibChop} from "contracts/libraries/LibChop.sol";
import {LibBarnRaise} from "contracts/libraries/LibBarnRaise.sol";

/**
* @title UnripeFacet
Expand Down Expand Up @@ -354,7 +355,7 @@ contract UnripeFacet is ReentrancyGuard {
* Tokens.
*/
function getLockedBeans() external view returns (uint256) {
uint256[] memory twaReserves = LibWell.getTwaReservesFromBeanstalkPump(C.BEAN_ETH_WELL);
uint256[] memory twaReserves = LibWell.getTwaReservesFromBeanstalkPump(LibBarnRaise.getBarnRaiseWell());
return LibUnripe.getLockedBeans(twaReserves);
}

Expand All @@ -371,8 +372,8 @@ contract UnripeFacet is ReentrancyGuard {
/**
* @notice Returns the number of Beans that are locked underneath the Unripe LP Token.
*/
function getLockedBeansUnderlyingUnripeBeanEth() external view returns (uint256) {
uint256[] memory twaReserves = LibWell.getTwaReservesFromBeanstalkPump(C.BEAN_ETH_WELL);
function getLockedBeansUnderlyingUnripeLP() external view returns (uint256) {
uint256[] memory twaReserves = LibWell.getTwaReservesFromBeanstalkPump(LibBarnRaise.getBarnRaiseWell());
return LibUnripe.getLockedBeansFromLP(twaReserves);
}
}
14 changes: 2 additions & 12 deletions protocol/contracts/beanstalk/init/InitBipSeedGauge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@ import {Weather} from "contracts/beanstalk/sun/SeasonFacet/Weather.sol";
import {LibSafeMathSigned96} from "contracts/libraries/LibSafeMathSigned96.sol";
import {LibSafeMath128} from "contracts/libraries/LibSafeMath128.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/SafeCast.sol";
import {ILiquidityWeightFacet} from "contracts/beanstalk/sun/LiquidityWeightFacet.sol";
import {IGaugePointFacet} from "contracts/beanstalk/sun/GaugePointFacet.sol";
import {InitWhitelistStatuses} from "contracts/beanstalk/init/InitWhitelistStatuses.sol";

interface IGaugePointFacet {
function defaultGaugePointFunction(
uint256 currentGaugePoints,
uint256 optimalPercentDepositedBdv,
uint256 percentOfDepositedBdv
) external pure returns (uint256 newGaugePoints);
}

interface ILiquidityWeightFacet {
function maxWeight() external pure returns (uint256);
}

/**
* @author Brean, Brendan
* @title InitBipSeedGauge initalizes the seed gauge, updates siloSetting Struct
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
SPDX-License-Identifier: MIT
*/

pragma solidity =0.7.6;
pragma experimental ABIEncoderV2;

import {AppStorage} from "contracts/beanstalk/AppStorage.sol";
import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import {C} from "contracts/C.sol";
import {LibDiamond} from "contracts/libraries/LibDiamond.sol";
import {LibUnripe} from "contracts/libraries/LibUnripe.sol";
import {LibSafeMath32} from "contracts/libraries/LibSafeMath32.sol";
import {LibWhitelist} from "contracts/libraries/Silo/LibWhitelist.sol";
import {BDVFacet} from "contracts/beanstalk/silo/BDVFacet.sol";
import {ILiquidityWeightFacet} from "contracts/beanstalk/sun/LiquidityWeightFacet.sol";
import {IGaugePointFacet} from "contracts/beanstalk/sun/GaugePointFacet.sol";

/**
* Initializes the Migration of the Unripe LP underlying tokens from Bean:Eth to Bean:Steth.
*/
contract InitMigrateUnripeBeanEthToBeanSteth {
using SafeERC20 for IERC20;
using LibSafeMath32 for uint32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed here:
71930ff


// gauge point factor is used to scale up the gauge points of the bean and bean3crv pools.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here:
71930ff

uint128 internal constant BEAN_WSTETH_INITIAL_GAUGE_POINTS = 1000e18;

uint32 constant BEAN_ETH_PUMP_CATCH_UP_SEASONS = 24;
uint32 constant private STALK_ISSUED_PER_BDV = 10000;
uint64 constant private OPTIMAL_PERCENT_DEPOSITED_BDV = 5e6;
uint64 constant private MAX_PERCENT_DEPOSITED_BDV = 100e6;

AppStorage internal s;

function init() external {

// Turn off Bean:Eth Minting while Multi Flow Pump catches up
delete s.wellOracleSnapshots[C.BEAN_ETH_WELL];
s.season.beanEthStartMintingSeason = s.season.current + BEAN_ETH_PUMP_CATCH_UP_SEASONS;

LibWhitelist.whitelistToken(
C.BEAN_WSTETH_WELL,
BDVFacet.wellBdv.selector,
STALK_ISSUED_PER_BDV,
0, // No need to set Stalk issued per BDV
0x01,
IGaugePointFacet.defaultGaugePointFunction.selector,
ILiquidityWeightFacet.maxWeight.selector,
BEAN_WSTETH_INITIAL_GAUGE_POINTS,
OPTIMAL_PERCENT_DEPOSITED_BDV
);

LibWhitelist.updateOptimalPercentDepositedBdvForToken(
C.BEAN_ETH_WELL,
MAX_PERCENT_DEPOSITED_BDV - OPTIMAL_PERCENT_DEPOSITED_BDV
);

// Migrate to BEAN_STETH;
uint256 balanceOfUnderlying = s.u[C.UNRIPE_LP].balanceOfUnderlying;
IERC20(s.u[C.UNRIPE_LP].underlyingToken).safeTransfer(
LibDiamond.diamondStorage().contractOwner,
balanceOfUnderlying
);
LibUnripe.decrementUnderlying(C.UNRIPE_LP, balanceOfUnderlying);
LibUnripe.switchUnderlyingToken(C.UNRIPE_LP, C.BEAN_WSTETH_WELL);

s.barnRaiseWell = C.BEAN_WSTETH_WELL;
}
}
3 changes: 2 additions & 1 deletion protocol/contracts/beanstalk/silo/BDVFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "contracts/C.sol";
import "contracts/libraries/Curve/LibBeanMetaCurve.sol";
import "contracts/libraries/LibUnripe.sol";
import "contracts/libraries/Well/LibWellBdv.sol";
import {LibBarnRaise} from "contracts/libraries/LibBarnRaise.sol";

/**
* @title BDVFacet
Expand Down Expand Up @@ -37,7 +38,7 @@ contract BDVFacet {
amount,
IBean(C.UNRIPE_LP).totalSupply()
);
amount = LibWellBdv.bdv(C.BEAN_ETH_WELL, amount);
amount = LibWellBdv.bdv(LibBarnRaise.getBarnRaiseWell(), amount);
return amount;
}

Expand Down
Loading
Loading