Skip to content

Commit

Permalink
add beanstalkPriceTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Brean0 committed Aug 2, 2023
1 parent e7785d0 commit 8f821ee
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 27 deletions.
14 changes: 7 additions & 7 deletions protocol/abi/Beanstalk.json
Original file line number Diff line number Diff line change
Expand Up @@ -4375,9 +4375,9 @@
},
{
"indexed": false,
"internalType": "int128",
"internalType": "int96",
"name": "stem",
"type": "int128"
"type": "int96"
},
{
"indexed": false,
Expand Down Expand Up @@ -5692,7 +5692,7 @@
"type": "address"
}
],
"name": "getLastStem",
"name": "getLastMowedStem",
"outputs": [
{
"internalType": "int96",
Expand Down Expand Up @@ -6025,9 +6025,9 @@
"name": "seasonToStem",
"outputs": [
{
"internalType": "int128",
"internalType": "int96",
"name": "stem",
"type": "int128"
"type": "int96"
}
],
"stateMutability": "view",
Expand Down Expand Up @@ -6057,9 +6057,9 @@
"name": "stemTipForToken",
"outputs": [
{
"internalType": "int128",
"internalType": "int96",
"name": "_stemTip",
"type": "int128"
"type": "int96"
}
],
"stateMutability": "view",
Expand Down
1 change: 1 addition & 0 deletions protocol/contracts/beanstalk/init/InitDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ contract InitDiamond {
s.season.withdrawSeasons = 25;
s.season.period = C.getSeasonPeriod();
s.season.timestamp = block.timestamp;
s.season.stemStartSeason = 0;
s.season.start = s.season.period > 0 ?
(block.timestamp / s.season.period) * s.season.period :
block.timestamp;
Expand Down
1 change: 1 addition & 0 deletions protocol/contracts/ecosystem/price/BeanstalkPrice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract BeanstalkPrice is CurvePrice, WellPrice {
p.ps[0] = getCurve();
p.ps[1] = getConstantProductWell(C.BEAN_ETH_WELL);

// assumes that liquidity and prices on all pools uses the same precision.
for (uint256 i = 0; i < p.ps.length; i++) {
p.price += p.ps[i].price.mul(p.ps[i].liquidity);
p.liquidity += p.ps[i].liquidity;
Expand Down
51 changes: 33 additions & 18 deletions protocol/contracts/ecosystem/price/WellPrice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ import {LibWellMinting} from "../../libraries/Minting/LibWellMinting.sol";
import {LibWell} from "../../libraries/Well/LibWell.sol";
import {C} from "../../C.sol";

interface IERC20D {
function decimals() external view returns (uint8);
interface IBEANSTALK {
function bdv(address token, uint256 amount) external view returns (uint256);

function poolDeltaB(address pool) external view returns (int256);
}

interface IBDV {
function bdv(address token, uint256 amount) external view returns (uint256);
interface dec{
function decimals() external view returns (uint256);
}

contract WellPrice {

using SafeMath for uint256;

address[2] private tokens = [0xBEA0000029AD1c77D3d5D23Ba2D8893dB9d1Efab, 0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490];
address private constant BEANSTALK = 0xC1E088fC1323b20BCBee9bd1B9fC9546db5624C5;
uint256 private constant WELL_DECIMALS = 1e18;
uint256 private constant PRICE_PRECISION = 1e6;

struct Pool {
address pool;
Expand All @@ -39,27 +43,38 @@ contract WellPrice {
function getConstantProductWell(address wellAddress) public view returns (P.Pool memory pool) {
IWell well = IWell(wellAddress);
pool.pool = wellAddress;

IERC20[] memory wellTokens = well.tokens();
pool.tokens = [address(wellTokens[0]), address(wellTokens[1])];

uint256[] memory wellBalances = well.getReserves();
pool.balances = [wellBalances[0], wellBalances[1]];
{
uint256[] memory wellBalances = well.getReserves();
pool.balances = [wellBalances[0], wellBalances[1]];
}

uint256 beanIndex = LibWell.getBeanIndex(wellTokens);
uint256 tknIndex = beanIndex == 0 ? 1 : 0;

// swap 1 bean of the opposite asset to get the usd price
// price = amtOut/tknOutPrice
pool.price =
well.getSwapOut(wellTokens[beanIndex], wellTokens[tknIndex], 1e6) // 1e18
.mul(PRICE_PRECISION) // 1e6
.div(LibUsdOracle.getUsdPrice(address(wellTokens[tknIndex]))); // 1e18

// liquidity is calculated by beanAmt * beanPrice * 2
pool.liquidity =
pool.balances[beanIndex] // 1e6
.mul(pool.price) // 1e6
.div(PRICE_PRECISION)
.mul(2);

pool.deltaB = IBEANSTALK(BEANSTALK).poolDeltaB(wellAddress);

pool.lpUsd = pool.liquidity.mul(WELL_DECIMALS).div(IERC20(wellAddress).totalSupply());

// swap 1 bean of the opposite asset to get the price
uint256 amtOut = well.getSwapOut(wellTokens[beanIndex], wellTokens[beanIndex == 0 ? 1 : 0], 1e6);

// get price of other token to price pool
uint256 tknUsdPrice = LibUsdOracle.getUsdPrice(address(wellTokens[beanIndex == 0 ? 1 : 0]));
pool.price = amtOut.mul(tknUsdPrice).div(1e12);
pool.lpBdv = IBEANSTALK(BEANSTALK).bdv(wellAddress, WELL_DECIMALS);

pool.liquidity = pool.balances[beanIndex] + pool.balances[beanIndex == 0 ? 1 : 0] * tknUsdPrice;
pool.deltaB = LibWellMinting.check(wellAddress);
pool.lpUsd = pool.liquidity.mul(pool.price).div(1e6);
pool.lpBdv = pool.liquidity;
}

}
18 changes: 17 additions & 1 deletion protocol/scripts/impersonate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ const {
ETH_USDC_UNISWAP_V3,
ETH_USDT_UNISWAP_V3,
USDT,
ETH_USD_CHAINLINK_AGGREGATOR
ETH_USD_CHAINLINK_AGGREGATOR,
BEAN_ETH_WELL
} = require('../test/utils/constants');
const { deployWell } = require('../utils/well.js');
const { impersonateSigner, mintEth } = require('../utils');

const { getSigner } = '../utils'
Expand Down Expand Up @@ -95,6 +97,8 @@ async function weth() {
WETH,
JSON.parse(tokenJson).deployedBytecode,
]);
const weth = await ethers.getContractAt("MockToken", WETH);
await weth.setDecimals(18);
}

async function router() {
Expand Down Expand Up @@ -268,6 +272,15 @@ async function ethUsdtUniswap() {
]);
}

async function beanEthWell() {
const well = await deployWell([BEAN, WETH]);
const bytecode = await ethers.provider.getCode(well.address)
await network.provider.send("hardhat_setCode", [
BEAN_ETH_WELL,
bytecode,
]);
}

async function ethUsdChainlinkAggregator() {
let chainlinkAggregatorJson = fs.readFileSync(`./artifacts/contracts/mocks/chainlink/MockChainlinkAggregator.sol/MockChainlinkAggregator.json`);

Expand All @@ -279,6 +292,8 @@ async function ethUsdChainlinkAggregator() {
await ethUsdChainlinkAggregator.setDecimals(6)
}



exports.impersonateRouter = router
exports.impersonateBean = bean
exports.impersonateCurve = curve
Expand All @@ -295,3 +310,4 @@ exports.impersonateEthUsdcUniswap = ethUsdcUniswap
exports.impersonateEthUsdtUniswap = ethUsdtUniswap
exports.impersonateBeanstalk = impersonateBeanstalk
exports.impersonateEthUsdChainlinkAggregator = ethUsdChainlinkAggregator
exports.impersonateBeanEthWell = beanEthWell
Loading

0 comments on commit 8f821ee

Please sign in to comment.