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-38: Migrate urBEAN3CRV to urBEANETH #655

Merged
merged 103 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
ed128bb
update IWell.sol
BrendanSanderson Sep 5, 2023
c4e84bd
Mint Fertilizer with WETH and add liquidity to Bean:Eth Well instead …
BrendanSanderson Sep 5, 2023
f1efc24
Add migrate function
BrendanSanderson Sep 5, 2023
9efae4f
Use sync instead of add liquidity in Well convert
BrendanSanderson Sep 5, 2023
5603486
Convert into/from Bean:Eth for Unripe assets
BrendanSanderson Sep 5, 2023
4b024b6
Script and Helper function updates
BrendanSanderson Sep 5, 2023
054e305
Update Unripe BDV Function
BrendanSanderson Sep 5, 2023
9917856
Add migration init script
BrendanSanderson Sep 5, 2023
37343e2
fix build
BrendanSanderson Sep 5, 2023
adc02b8
rename init, update tests/scripts
BrendanSanderson Sep 5, 2023
f14ab7c
remove extra line
BrendanSanderson Sep 6, 2023
b727a7d
update abi
BrendanSanderson Sep 6, 2023
87219b9
update verbose flag
BrendanSanderson Sep 6, 2023
900b9d5
update mint eth amount
BrendanSanderson Sep 6, 2023
9530ba2
add migration test
BrendanSanderson Sep 6, 2023
6c57d1e
revert on 0 before migration completed
BrendanSanderson Sep 10, 2023
46fb327
update tests
BrendanSanderson Sep 10, 2023
a6918cd
Bean eth migration script
BrendanSanderson Sep 11, 2023
7dc47b5
all tests working
BrendanSanderson Sep 11, 2023
bd4e6ed
documentation + style updates
BrendanSanderson Sep 11, 2023
0115767
remove addFertilizerOwner function
BrendanSanderson Sep 11, 2023
af59a90
update ABI
BrendanSanderson Sep 11, 2023
1bdf75c
transfer WETH directly to Well
BrendanSanderson Sep 11, 2023
1d5a628
add comment to
BrendanSanderson Sep 11, 2023
cdc38f6
update comments
BrendanSanderson Sep 11, 2023
3c69f7e
add tests to demonstrate success after migration
BrendanSanderson Sep 12, 2023
12c608a
more convert tests
BrendanSanderson Sep 12, 2023
bddb296
change metadata
Brean0 Sep 18, 2023
7c4918c
update tests
Brean0 Sep 18, 2023
d128fe4
Merge pull request #2 from AgrarianAlliance/urBeanEthMetadata
BrendanSanderson Sep 18, 2023
853cace
Add SwitchUnderlyingToken event and switchUnderlyingToken function
BrendanSanderson Sep 18, 2023
64d781e
Add switch underlying token tests
BrendanSanderson Sep 18, 2023
a150d87
protocol: add bip38 migration script
0xalecks Sep 20, 2023
cca6250
reset withdraw seasons in InitMigrateUnripeBean3CrvToBeanEth
BrendanSanderson Sep 22, 2023
968f783
update comments
BrendanSanderson Sep 22, 2023
ba1d42b
remove seeds reference from InitBipBasinIntegration
BrendanSanderson Sep 22, 2023
c03f635
update file name
BrendanSanderson Sep 22, 2023
8aae31d
remove unnecssary conditional
BrendanSanderson Sep 22, 2023
ff742a6
add safe cast
BrendanSanderson Sep 22, 2023
373c094
fix natspec typo
BrendanSanderson Sep 22, 2023
d27567c
update natspec comment in _mow
BrendanSanderson Sep 22, 2023
81e452e
update sciNotation comment
Brean0 Sep 25, 2023
e155521
added proper testnet mocks for quicker metadata testing.
Brean0 Sep 25, 2023
4489cb8
require > 0 fertilizer bought in mintFertilizer
BrendanSanderson Sep 25, 2023
47fef03
update metadata to put values in traits rather than in description.
Brean0 Sep 26, 2023
ccbba64
Merge pull request #6 from AgrarianAlliance/cyfrin-remediations
BrendanSanderson Sep 26, 2023
d211b46
add migrated BDV counter
BrendanSanderson Sep 26, 2023
14c3291
Merge pull request #7 from AgrarianAlliance/bdv-migration-fix
BrendanSanderson Sep 28, 2023
7606673
add UsdOracle contract
BrendanSanderson Sep 28, 2023
46fb727
Merge pull request #3 from AgrarianAlliance/bean3crv-beaneth-migratio…
BrendanSanderson Oct 2, 2023
f7d0d13
revert projects/ui/src/graph/graphql.schema.json
BrendanSanderson Oct 11, 2023
7281684
update IWell.sol
BrendanSanderson Sep 5, 2023
7aa54c0
Mint Fertilizer with WETH and add liquidity to Bean:Eth Well instead …
BrendanSanderson Sep 5, 2023
92d2daf
Add migrate function
BrendanSanderson Sep 5, 2023
2f327c6
Use sync instead of add liquidity in Well convert
BrendanSanderson Sep 5, 2023
59642b1
Convert into/from Bean:Eth for Unripe assets
BrendanSanderson Sep 5, 2023
2ec0e7a
Script and Helper function updates
BrendanSanderson Sep 5, 2023
8ed8099
Update Unripe BDV Function
BrendanSanderson Sep 5, 2023
6998f47
Add migration init script
BrendanSanderson Sep 5, 2023
cca2825
fix build
BrendanSanderson Sep 5, 2023
cafa651
rename init, update tests/scripts
BrendanSanderson Sep 5, 2023
d86a0f0
remove extra line
BrendanSanderson Sep 6, 2023
1d72413
update abi
BrendanSanderson Sep 6, 2023
ec44f70
update verbose flag
BrendanSanderson Sep 6, 2023
11f9b2e
update mint eth amount
BrendanSanderson Sep 6, 2023
39bf809
add migration test
BrendanSanderson Sep 6, 2023
20ea20d
revert on 0 before migration completed
BrendanSanderson Sep 10, 2023
4bb12a4
update tests
BrendanSanderson Sep 10, 2023
71901cb
Bean eth migration script
BrendanSanderson Sep 11, 2023
2788237
all tests working
BrendanSanderson Sep 11, 2023
78ed9f4
documentation + style updates
BrendanSanderson Sep 11, 2023
c01f2ec
remove addFertilizerOwner function
BrendanSanderson Sep 11, 2023
6482ed7
update ABI
BrendanSanderson Sep 11, 2023
4869bc1
transfer WETH directly to Well
BrendanSanderson Sep 11, 2023
5066c4a
add comment to
BrendanSanderson Sep 11, 2023
33e551c
update comments
BrendanSanderson Sep 11, 2023
c011dff
add tests to demonstrate success after migration
BrendanSanderson Sep 12, 2023
515fd39
more convert tests
BrendanSanderson Sep 12, 2023
7867320
protocol: add bip38 migration script
0xalecks Sep 20, 2023
0a1b6d1
change metadata
Brean0 Sep 18, 2023
03859b8
update tests
Brean0 Sep 18, 2023
3da3d73
Add SwitchUnderlyingToken event and switchUnderlyingToken function
BrendanSanderson Sep 18, 2023
237e827
Add switch underlying token tests
BrendanSanderson Sep 18, 2023
2143faf
reset withdraw seasons in InitMigrateUnripeBean3CrvToBeanEth
BrendanSanderson Sep 22, 2023
2fd5f8f
update comments
BrendanSanderson Sep 22, 2023
b4ab34e
remove seeds reference from InitBipBasinIntegration
BrendanSanderson Sep 22, 2023
fb9cc3d
update file name
BrendanSanderson Sep 22, 2023
75fb041
remove unnecssary conditional
BrendanSanderson Sep 22, 2023
9dc6d4b
add safe cast
BrendanSanderson Sep 22, 2023
6b976ce
fix natspec typo
BrendanSanderson Sep 22, 2023
883e6e1
update natspec comment in _mow
BrendanSanderson Sep 22, 2023
9f91f96
update sciNotation comment
Brean0 Sep 25, 2023
615ba1c
added proper testnet mocks for quicker metadata testing.
Brean0 Sep 25, 2023
1b55b22
require > 0 fertilizer bought in mintFertilizer
BrendanSanderson Sep 25, 2023
2f2994b
update metadata to put values in traits rather than in description.
Brean0 Sep 26, 2023
81c527d
add migrated BDV counter
BrendanSanderson Sep 26, 2023
e771833
add UsdOracle contract
BrendanSanderson Sep 28, 2023
fa06ae1
revert projects/ui/src/graph/graphql.schema.json
BrendanSanderson Oct 11, 2023
a82a94b
Merge branch 'bip-38' of https://github.com/AgrarianAlliance/Beanstal…
BrendanSanderson Oct 11, 2023
70b4d7a
remove goerli private key
BrendanSanderson Oct 11, 2023
258508b
Merge branch 'master' into bip-38
BrendanSanderson Oct 11, 2023
2dfa201
update BIP-38 deploy script
BrendanSanderson Oct 11, 2023
cd9de38
Merge branch 'bip-38' of https://github.com/beanstalkfarms/beanstalk …
BrendanSanderson Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 107 additions & 26 deletions protocol/abi/Beanstalk.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@
"name": "Pick",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "underlyingToken",
"type": "address"
}
],
"name": "SwitchUnderlyingToken",
"type": "event"
},
{
"inputs": [
{
Expand Down Expand Up @@ -128,6 +147,24 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "unripeToken",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "addMigratedUnderlying",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -480,46 +517,41 @@
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint128",
"name": "id",
"type": "uint128"
"internalType": "address",
"name": "unripeToken",
"type": "address"
},
{
"indexed": false,
"internalType": "uint128",
"name": "bpf",
"type": "uint128"
"internalType": "address",
"name": "newUnderlyingToken",
"type": "address"
}
],
"name": "SetFertilizer",
"type": "event"
"name": "switchUnderlyingToken",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint128",
"name": "id",
"type": "uint128"
},
{
"indexed": false,
"internalType": "uint128",
"name": "amount",
"name": "bpf",
"type": "uint128"
},
{
"internalType": "uint256",
"name": "minLP",
"type": "uint256"
}
],
"name": "addFertilizerOwner",
"outputs": [],
"stateMutability": "payable",
"type": "function"
"name": "SetFertilizer",
"type": "event"
},
{
"inputs": [
Expand Down Expand Up @@ -800,6 +832,25 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "wethAmountIn",
"type": "uint256"
}
],
"name": "getMintFertilizerOut",
"outputs": [
{
"internalType": "uint256",
"name": "fertilizerAmountOut",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -835,13 +886,18 @@
{
"inputs": [
{
"internalType": "uint128",
"name": "amount",
"type": "uint128"
"internalType": "uint256",
"name": "wethAmountIn",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "minLP",
"name": "minFertilizerOut",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "minLPTokensOut",
"type": "uint256"
},
{
Expand All @@ -851,7 +907,13 @@
}
],
"name": "mintFertilizer",
"outputs": [],
"outputs": [
{
"internalType": "uint256",
"name": "fertilizerAmountOut",
"type": "uint256"
}
],
"stateMutability": "payable",
"type": "function"
},
Expand Down Expand Up @@ -4358,6 +4420,25 @@
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "token",
"type": "address"
}
],
"name": "totalMigratedBdv",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"anonymous": false,
"inputs": [
Expand Down
4 changes: 2 additions & 2 deletions protocol/contracts/C.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ library C {
//////////////////// Well ////////////////////

uint256 internal constant WELL_MINIMUM_BEAN_BALANCE = 1000_000_000; // 1,000 Beans
address constant internal BEANSTALK_PUMP = 0xBA510f10E3095B83a0F33aa9ad2544E22570a87C;
address constant BEAN_ETH_WELL = 0xBEA0e11282e2bB5893bEcE110cF199501e872bAd;
address internal constant BEANSTALK_PUMP = 0xBA510f10E3095B83a0F33aa9ad2544E22570a87C;
address internal constant BEAN_ETH_WELL = 0xBEA0e11282e2bB5893bEcE110cF199501e872bAd;

function getSeasonPeriod() internal pure returns (uint256) {
return CURRENT_SEASON_PERIOD;
Expand Down
4 changes: 4 additions & 0 deletions protocol/contracts/beanstalk/AppStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ contract Storage {
* @param ownerCandidate Stores a candidate address to transfer ownership to. The owner must claim the ownership transfer.
* @param wellOracleSnapshots A mapping from Well Oracle address to the Well Oracle Snapshot.
* @param beanEthPrice Stores the beanEthPrice during the sunrise() function. Returns 1 otherwise.
* @param migratedBdvs Stores the total migrated BDV since the implementation of the migrated BDV counter. See {LibLegacyTokenSilo.incrementMigratedBdv} for more info.
*/
struct AppStorage {
uint8 deprecated_index;
Expand Down Expand Up @@ -547,4 +548,7 @@ struct AppStorage {
// Well
mapping(address => bytes) wellOracleSnapshots;
uint256 beanEthPrice;

// Silo V3 BDV Migration
mapping(address => uint256) migratedBdvs;
}
98 changes: 67 additions & 31 deletions protocol/contracts/beanstalk/barn/FertilizerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,45 @@
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/SafeCast.sol";
import {SafeMath} from "@openzeppelin/contracts/math/SafeMath.sol";
import {IFertilizer} from "contracts/interfaces/IFertilizer.sol";
import {AppStorage} from "../AppStorage.sol";
import "contracts/libraries/Token/LibTransfer.sol";
import "contracts/libraries/LibFertilizer.sol";
import "contracts/C.sol";
import {LibTransfer} from "contracts/libraries/Token/LibTransfer.sol";
import {LibEthUsdOracle} from "contracts/libraries/Oracle/LibEthUsdOracle.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";

/**
* @author Publius
* @title Handles Sprouting Beans from Sprout Tokens
* @title FertilizerFacet handles Minting Fertilizer and Rinsing Sprouts earned from Fertilizer.
**/

contract FertilizerFacet {
using SafeMath for uint256;
using SafeCast for uint256;
using LibSafeMath128 for uint128;

event SetFertilizer(uint128 id, uint128 bpf);

uint256 private constant FERTILIZER_AMOUNT_PRECISION = 1e24;

AppStorage internal s;

struct Supply {
uint128 endBpf;
uint256 supply;
}


/**
* @notice Rinses Rinsable Sprouts earned from Fertilizer.
* @param ids The ids of the Fertilizer to rinse.
* @param mode The balance to transfer Beans to; see {LibTrasfer.To}
*/
function claimFertilized(uint256[] calldata ids, LibTransfer.To mode)
external
payable
Expand All @@ -37,41 +52,49 @@ contract FertilizerFacet {
LibTransfer.sendToken(C.bean(), amount, msg.sender, mode);
}

/**
* @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`.
* @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(
uint128 amount,
uint256 minLP,
uint256 wethAmountIn,
uint256 minFertilizerOut,
uint256 minLPTokensOut,
LibTransfer.From mode
) external payable {
uint128 remaining = uint128(LibFertilizer.remainingRecapitalization().div(1e6)); // remaining <= 77_000_000 so downcasting is safe.
if (amount > remaining) amount = remaining;
amount = uint128(LibTransfer.receiveToken(
C.usdc(),
uint256(amount).mul(1e6),
) 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),
msg.sender,
mode
).div(1e6)); // return value <= amount, so downcasting is safe.
uint128 id = LibFertilizer.addFertilizer(
uint128(s.season.current),
amount,
minLP
C.BEAN_ETH_WELL,
uint256(wethAmountIn),
mode,
LibTransfer.To.EXTERNAL
);
C.fertilizer().beanstalkMint(msg.sender, uint256(id), amount, s.bpf);
}

function addFertilizerOwner(
uint128 id,
uint128 amount,
uint256 minLP
) external payable {
LibDiamond.enforceIsContractOwner();
C.usdc().transferFrom(
msg.sender,
address(this),
uint256(amount).mul(1e6)
fertilizerAmountOut = getMintFertilizerOut(wethAmountIn);

require(fertilizerAmountOut >= minFertilizerOut, "Fertilizer: Not enough bought.");
require(fertilizerAmountOut > 0, "Fertilizer: None bought.");

uint128 remaining = uint128(LibFertilizer.remainingRecapitalization().div(1e6)); // remaining <= 77_000_000 so downcasting is safe.
require(fertilizerAmountOut <= remaining, "Fertilizer: Not enough remaining.");

uint128 id = LibFertilizer.addFertilizer(
uint128(s.season.current),
fertilizerAmountOut,
minLPTokensOut
);
LibFertilizer.addFertilizer(id, amount, minLP);
C.fertilizer().beanstalkMint(msg.sender, uint256(id), (fertilizerAmountOut).toUint128(), s.bpf);
}

/**
* @dev Callback from Fertilizer contract in `claimFertilized` function.
*/
function payFertilizer(address account, uint256 amount) external payable {
require(msg.sender == C.fertilizerAddress());
LibTransfer.sendToken(
Expand All @@ -82,6 +105,19 @@ contract FertilizerFacet {
);
}

/**
* @dev Returns the amount of Fertilizer that can be purchased with `wethAmountIn` WETH.
* Can be used to help calculate `minFertilizerOut` in `mintFertilizer`.
* `wethAmountIn` has 18 decimals, `getEthUsdPrice()` has 6 decimals and `fertilizerAmountOut` has 0 decimals.
*/
function getMintFertilizerOut(
uint256 wethAmountIn
) public view returns (uint256 fertilizerAmountOut) {
fertilizerAmountOut = wethAmountIn.mul(
LibEthUsdOracle.getEthUsdPrice()
).div(FERTILIZER_AMOUNT_PRECISION);
}

function totalFertilizedBeans() external view returns (uint256 beans) {
return s.fertilizedIndex;
}
Expand Down